clang 22.0.0git
LifetimeAnnotations.cpp
Go to the documentation of this file.
1//===- LifetimeAnnotations.cpp - -*--------------- C++------------------*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
10#include "clang/AST/Attr.h"
11#include "clang/AST/Decl.h"
12#include "clang/AST/DeclCXX.h"
13#include "clang/AST/Type.h"
14#include "clang/AST/TypeLoc.h"
15
16namespace clang {
17namespace lifetimes {
18
19const FunctionDecl *
21 return FD != nullptr ? FD->getMostRecentDecl() : nullptr;
22}
23
24const CXXMethodDecl *
26 const FunctionDecl *FD = CMD;
27 return cast_if_present<CXXMethodDecl>(
29}
30
33 bool IsAssignment = OO == OO_Equal || isCompoundAssignmentOperator(OO);
34 if (!IsAssignment)
35 return false;
36 QualType RetT = FD->getReturnType();
37 if (!RetT->isLValueReferenceType())
38 return false;
39 ASTContext &Ctx = FD->getASTContext();
40 QualType LHST;
41 auto *MD = dyn_cast<CXXMethodDecl>(FD);
42 if (MD && MD->isCXXInstanceMember())
43 LHST = Ctx.getLValueReferenceType(MD->getFunctionObjectParameterType());
44 else
45 LHST = FD->getParamDecl(0)->getType();
46 return Ctx.hasSameType(RetT, LHST);
47}
48
51 return CMD && isNormalAssignmentOperator(CMD) && CMD->param_size() == 1 &&
52 CMD->getParamDecl(0)->hasAttr<clang::LifetimeBoundAttr>();
53}
54
57 const TypeSourceInfo *TSI = FD->getTypeSourceInfo();
58 if (!TSI)
59 return false;
60 // Don't declare this variable in the second operand of the for-statement;
61 // GCC miscompiles that by ending its lifetime before evaluating the
62 // third operand. See gcc.gnu.org/PR86769.
64 for (TypeLoc TL = TSI->getTypeLoc();
65 (ATL = TL.getAsAdjusted<AttributedTypeLoc>());
66 TL = ATL.getModifiedLoc()) {
67 if (ATL.getAttrAs<clang::LifetimeBoundAttr>())
68 return true;
69 }
70
72}
73
74} // namespace lifetimes
75} // namespace clang
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::TypeLoc interface and its subclasses.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:188
bool hasSameType(QualType T1, QualType T2) const
Determine whether the given types T1 and T2 are equivalent.
QualType getLValueReferenceType(QualType T, bool SpelledAsLValue=true) const
Return the uniqued reference to the type for an lvalue reference to the specified type.
Type source information for an attributed type.
Definition TypeLoc.h:1017
TypeLoc getModifiedLoc() const
The modified type, which is generally canonically different from the attribute type.
Definition TypeLoc.h:1031
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:524
bool hasAttr() const
Definition DeclBase.h:577
OverloadedOperatorKind getCXXOverloadedOperator() const
If this name is the name of an overloadable operator in C++ (e.g., operator+), retrieve the kind of o...
TypeSourceInfo * getTypeSourceInfo() const
Definition Decl.h:808
Represents a function declaration or definition.
Definition Decl.h:1999
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2794
QualType getReturnType() const
Definition Decl.h:2842
FunctionDecl * getMostRecentDecl()
Returns the most recent (re)declaration of this declaration.
size_t param_size() const
Definition Decl.h:2787
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:339
A (possibly-)qualified type.
Definition TypeBase.h:937
Base wrapper for a particular "section" of type source info.
Definition TypeLoc.h:59
T getAsAdjusted() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition TypeLoc.h:2715
A container of type source information.
Definition TypeBase.h:8256
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition TypeLoc.h:272
bool isLValueReferenceType() const
Definition TypeBase.h:8550
QualType getType() const
Definition Decl.h:722
bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD)
Returns true if this is an assignment operator where the parameter has the lifetimebound attribute.
bool isNormalAssignmentOperator(const FunctionDecl *FD)
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
Returns true if the implicit object parameter (this) should be considered lifetimebound,...
const FunctionDecl * getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD)
Returns the most recent declaration of the method to ensure all lifetime-bound attributes from redecl...
The JSON file list parser is used to communicate input to InstallAPI.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
bool isCompoundAssignmentOperator(OverloadedOperatorKind Kind)
Determine if this is a compound assignment operator.