Debian Codegraph @

clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp

raw
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 //===----------------------------------------------------------------------===//
8 #include "clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h"
9 #include "clang/AST/ASTContext.h"
10 #include "clang/AST/Attr.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/AST/DeclTemplate.h"
14 #include "clang/AST/Type.h"
15 #include "clang/AST/TypeLoc.h"
16
17 namespace clang::lifetimes {
18
19 const FunctionDecl *
20 getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD) {
21 return FD != nullptr ? FD->getMostRecentDecl() : nullptr;
22 }
23
24 const CXXMethodDecl *
25 getDeclWithMergedLifetimeBoundAttrs(const CXXMethodDecl *CMD) {
26 const FunctionDecl *FD = CMD;
27 return cast_if_present<CXXMethodDecl>(
28 getDeclWithMergedLifetimeBoundAttrs(FD));
29 }
30
31 bool isNormalAssignmentOperator(const FunctionDecl *FD) {
32 OverloadedOperatorKind OO = FD->getDeclName().getCXXOverloadedOperator();
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
49 bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD) {
50 CMD = getDeclWithMergedLifetimeBoundAttrs(CMD);
51 return CMD && isNormalAssignmentOperator(CMD) && CMD->param_size() == 1 &&
52 CMD->getParamDecl(0)->hasAttr<clang::LifetimeBoundAttr>();
53 }
54
55 const LifetimeBoundAttr *
56 getLifetimeBoundAttrFromFunctionType(const TypeSourceInfo &TSI) {
57 // Walk through the type layers looking for a lifetimebound attribute.
58 TypeLoc TL = TSI.getTypeLoc();
59 while (true) {
60 auto ATL = TL.getAsAdjusted<AttributedTypeLoc>();
61 if (!ATL)
62 break;
63 if (auto *LBAttr = ATL.getAttrAs<LifetimeBoundAttr>())
64 return LBAttr;
65 TL = ATL.getModifiedLoc();
66 }
67 return nullptr;
68 }
69
70 bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) {
71 FD = getDeclWithMergedLifetimeBoundAttrs(FD);
72 const TypeSourceInfo *TSI = FD->getTypeSourceInfo();
73 if (!TSI)
74 return false;
75 return getLifetimeBoundAttrFromFunctionType(*TSI) != nullptr ||
76 isNormalAssignmentOperator(FD);
77 }
78
79 bool isInStlNamespace(const Decl *D) {
80 const DeclContext *DC = D->getDeclContext();
81 if (!DC)
82 return false;
83 if (const auto *ND = dyn_cast<NamespaceDecl>(DC))
84 if (const IdentifierInfo *II = ND->getIdentifier()) {
85 StringRef Name = II->getName();
86 if (Name.size() >= 2 && Name.front() == '_' &&
87 (Name[1] == '_' || isUppercase(Name[1])))
88 return true;
89 }
90 return DC->isStdNamespace();
91 }
92
93 bool isPointerLikeType(QualType QT) {
94 return isGslPointerType(QT) || QT->isPointerType() || QT->isNullPtrType();
95 }
96
97 bool shouldTrackImplicitObjectArg(const CXXMethodDecl *Callee) {
98 if (auto *Conv = dyn_cast_or_null<CXXConversionDecl>(Callee))
99 if (isGslPointerType(Conv->getConversionType()) &&
100 Callee->getParent()->hasAttr<OwnerAttr>())
101 return true;
102 if (!isInStlNamespace(Callee->getParent()))
103 return false;
104 if (!isGslPointerType(Callee->getFunctionObjectParameterType()) &&
105 !isGslOwnerType(Callee->getFunctionObjectParameterType()))
106 return false;
107 if (isPointerLikeType(Callee->getReturnType())) {
108 if (!Callee->getIdentifier())
109 return false;
110 return llvm::StringSwitch<bool>(Callee->getName())
111 .Cases(
112 {// Begin and end iterators.
113 "begin", "end", "rbegin", "rend", "cbegin", "cend", "crbegin",
114 "crend",
115 // Inner pointer getters.
116 "c_str", "data", "get",
117 // Map and set types.
118 "find", "equal_range", "lower_bound", "upper_bound"},
119 true)
120 .Default(false);
121 }
122 if (Callee->getReturnType()->isReferenceType()) {
123 if (!Callee->getIdentifier()) {
124 auto OO = Callee->getOverloadedOperator();
125 if (!Callee->getParent()->hasAttr<OwnerAttr>())
126 return false;
127 return OO == OverloadedOperatorKind::OO_Subscript ||
128 OO == OverloadedOperatorKind::OO_Star;
129 }
130 return llvm::StringSwitch<bool>(Callee->getName())
131 .Cases({"front", "back", "at", "top", "value"}, true)
132 .Default(false);
133 }
134 return false;
135 }
136
137 bool shouldTrackFirstArgument(const FunctionDecl *FD) {
138 if (!FD->getIdentifier() || FD->getNumParams() != 1)
139 return false;
140 const auto *RD = FD->getParamDecl(0)->getType()->getPointeeCXXRecordDecl();
141 if (!FD->isInStdNamespace() || !RD || !RD->isInStdNamespace())
142 return false;
143 if (!RD->hasAttr<PointerAttr>() && !RD->hasAttr<OwnerAttr>())
144 return false;
145 if (FD->getReturnType()->isPointerType() ||
146 isGslPointerType(FD->getReturnType())) {
147 return llvm::StringSwitch<bool>(FD->getName())
148 .Cases({"begin", "rbegin", "cbegin", "crbegin"}, true)
149 .Cases({"end", "rend", "cend", "crend"}, true)
150 .Case("data", true)
151 .Default(false);
152 }
153 if (FD->getReturnType()->isReferenceType()) {
154 return llvm::StringSwitch<bool>(FD->getName())
155 .Cases({"get", "any_cast"}, true)
156 .Default(false);
157 }
158 return false;
159 }
160
161 template <typename T> static bool isRecordWithAttr(QualType Type) {
162 auto *RD = Type->getAsCXXRecordDecl();
163 if (!RD)
164 return false;
165 // Generally, if a primary template class declaration is annotated with an
166 // attribute, all its specializations generated from template instantiations
167 // should inherit the attribute.
168 //
169 // However, since lifetime analysis occurs during parsing, we may encounter
170 // cases where a full definition of the specialization is not required. In
171 // such cases, the specialization declaration remains incomplete and lacks the
172 // attribute. Therefore, we fall back to checking the primary template class.
173 //
174 // Note: it is possible for a specialization declaration to have an attribute
175 // even if the primary template does not.
176 //
177 // FIXME: What if the primary template and explicit specialization
178 // declarations have conflicting attributes? We should consider diagnosing
179 // this scenario.
180 bool Result = RD->hasAttr<T>();
181
182 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
183 Result |= CTSD->getSpecializedTemplate()->getTemplatedDecl()->hasAttr<T>();
184
185 return Result;
186 }
187
188 bool isGslPointerType(QualType QT) { return isRecordWithAttr<PointerAttr>(QT); }
189 bool isGslOwnerType(QualType QT) { return isRecordWithAttr<OwnerAttr>(QT); }
190
191 } // namespace clang::lifetimes