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"
14#include "clang/AST/Type.h"
15#include "clang/AST/TypeLoc.h"
16
17namespace clang::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
74template <typename T> static bool isRecordWithAttr(QualType Type) {
75 auto *RD = Type->getAsCXXRecordDecl();
76 if (!RD)
77 return false;
78 // Generally, if a primary template class declaration is annotated with an
79 // attribute, all its specializations generated from template instantiations
80 // should inherit the attribute.
81 //
82 // However, since lifetime analysis occurs during parsing, we may encounter
83 // cases where a full definition of the specialization is not required. In
84 // such cases, the specialization declaration remains incomplete and lacks the
85 // attribute. Therefore, we fall back to checking the primary template class.
86 //
87 // Note: it is possible for a specialization declaration to have an attribute
88 // even if the primary template does not.
89 //
90 // FIXME: What if the primary template and explicit specialization
91 // declarations have conflicting attributes? We should consider diagnosing
92 // this scenario.
93 bool Result = RD->hasAttr<T>();
94
95 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
96 Result |= CTSD->getSpecializedTemplate()->getTemplatedDecl()->hasAttr<T>();
97
98 return Result;
99}
100
103
104} // namespace clang::lifetimes
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
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:220
QualType getLValueReferenceType(QualType T, bool SpelledAsLValue=true) const
Return the uniqued reference to the type for an lvalue reference to the specified type.
static bool hasSameType(QualType T1, QualType T2)
Determine whether the given types T1 and T2 are equivalent.
Type source information for an attributed type.
Definition TypeLoc.h:1008
TypeLoc getModifiedLoc() const
The modified type, which is generally canonically different from the attribute type.
Definition TypeLoc.h:1022
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:546
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:809
Represents a function declaration or definition.
Definition Decl.h:2000
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2797
QualType getReturnType() const
Definition Decl.h:2845
FunctionDecl * getMostRecentDecl()
Returns the most recent (re)declaration of this declaration.
size_t param_size() const
Definition Decl.h:2790
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:340
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:2706
A container of type source information.
Definition TypeBase.h:8249
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition TypeLoc.h:267
The base class of the type hierarchy.
Definition TypeBase.h:1833
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isLValueReferenceType() const
Definition TypeBase.h:8543
QualType getType() const
Definition Decl.h:723
static bool isRecordWithAttr(QualType Type)
bool isGslPointerType(QualType QT)
bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD)
bool isNormalAssignmentOperator(const FunctionDecl *FD)
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
const FunctionDecl * getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD)
bool isGslOwnerType(QualType QT)
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
bool isCompoundAssignmentOperator(OverloadedOperatorKind Kind)
Determine if this is a compound assignment operator.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
const FunctionProtoType * T