20#include "llvm/ADT/StringSet.h"
32 return cast_if_present<CXXMethodDecl>(
46 auto *MD = dyn_cast<CXXMethodDecl>(FD);
47 if (MD && MD->isCXXInstanceMember())
63static const LifetimeBoundAttr *
71 if (
auto *LBAttr = ATL.getAttrAs<LifetimeBoundAttr>())
73 TL = ATL.getModifiedLoc();
78const LifetimeBoundAttr *
86const LifetimeBoundAttr *
91 auto CheckRedecls = [](
const FunctionDecl *F) ->
const LifetimeBoundAttr * {
98 if (
const auto *
Attr = CheckRedecls(FD))
101 return CheckRedecls(Pattern);
112 FunctionCallInfo Info;
121 Info.FD = dyn_cast_or_null<FunctionDecl>(AC->getDecl());
125 if (
const auto *MCE = dyn_cast<CXXMemberCallExpr>(
Call))
126 Info.Args.push_back(MCE->getImplicitObjectArgument());
128 Info.Args.append(AC->arg_begin(), AC->arg_end());
130 if (
const auto *OCE = dyn_cast<CXXOperatorCallExpr>(
Call))
133 if (OCE->getOperator() == OO_Call && Info.FD->isStatic())
134 Info.Args.erase(Info.Args.begin());
139std::optional<LifetimeBoundParamInfo>
143 if (!FD || I >= Args.size())
148 if (
const auto *
Method = dyn_cast<CXXMethodDecl>(FD);
155 return LifetimeBoundParamInfo(
Method);
158 if ((I - 1) <
Method->getNumParams())
160 PVD =
Method->getParamDecl(I - 1);
165 }
else if (I < FD->getNumParams()) {
170 if (PVD && PVD->
hasAttr<clang::LifetimeBoundAttr>())
171 return LifetimeBoundParamInfo(PVD);
176std::optional<LifetimeBoundParamInfo>
178 if (!
Call || !Source)
185 for (
unsigned I = 0; I < CallInfo.Args.size(); ++I)
186 if (CallInfo.Args[I]->IgnoreParenImpCasts() ==
187 Source->IgnoreParenImpCasts())
188 if (std::optional<LifetimeBoundParamInfo> ParamInfo =
197 if (DC->isStdNamespace())
199 if (
const auto *ND = dyn_cast<NamespaceDecl>(DC))
201 StringRef Name = II->getName();
202 if (Name.size() >= 2 && Name.front() ==
'_' &&
220 bool RunningUnderLifetimeSafety) {
225 const bool IsGslOwnerImplicitObject =
227 (RunningUnderLifetimeSafety &&
229 if (
auto *Conv = dyn_cast<CXXConversionDecl>(Callee))
230 if (
isGslPointerType(Conv->getConversionType()) && IsGslOwnerImplicitObject)
233 !IsGslOwnerImplicitObject)
237 static const llvm::StringSet<> IteratorMembers = {
238 "begin",
"end",
"rbegin",
"rend",
"cbegin",
"cend",
"crbegin",
"crend"};
239 static const llvm::StringSet<> InnerPointerGetters = {
241 "c_str",
"data",
"get"};
242 static const llvm::StringSet<> ContainerFindFns = {
244 "find",
"equal_range",
"lower_bound",
"upper_bound"};
248 if (RunningUnderLifetimeSafety &&
253 switch (Callee->getOverloadedOperator()) {
264 if (Callee->getIdentifier() &&
265 (IteratorMembers.contains(Callee->getName()) ||
266 InnerPointerGetters.contains(Callee->getName())))
274 if (!Callee->getIdentifier())
276 return RunningUnderLifetimeSafety
277 ? IsGslOwnerImplicitObject &&
278 Callee->getOverloadedOperator() ==
279 OverloadedOperatorKind::OO_Arrow
281 return IteratorMembers.contains(Callee->getName()) ||
282 InnerPointerGetters.contains(Callee->getName()) ||
283 ContainerFindFns.contains(Callee->getName());
285 if (Callee->getReturnType()->isReferenceType()) {
286 if (!Callee->getIdentifier()) {
287 auto OO = Callee->getOverloadedOperator();
288 if (!IsGslOwnerImplicitObject)
290 return OO == OverloadedOperatorKind::OO_Subscript ||
291 OO == OverloadedOperatorKind::OO_Star;
293 return llvm::StringSwitch<bool>(Callee->getName())
294 .Cases({
"front",
"back",
"at",
"top",
"value"},
true)
309 if (llvm::StringSwitch<bool>(FD->
getName())
328 if (!RD || !RD->isInStdNamespace())
330 if (!RD->hasAttr<PointerAttr>() && !RD->hasAttr<OwnerAttr>())
338 return llvm::StringSwitch<bool>(FD->
getName())
339 .Cases({
"begin",
"rbegin",
"cbegin",
"crbegin"},
true)
340 .Cases({
"end",
"rend",
"cend",
"crend"},
true)
345 return llvm::StringSwitch<bool>(FD->
getName())
346 .Cases({
"get",
"any_cast"},
true)
360 return RD->
hasAttr<PointerAttr>() &&
388 if (
auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
389 Result |= CTSD->getSpecializedTemplate()->getTemplatedDecl()->hasAttr<
T>();
405 if (
const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(&RD))
406 return CTSD->getSpecializedTemplate()->getName();
434 static const llvm::StringSet<>
Vector = {
435 "insert",
"emplace",
"emplace_back",
436 "push_back",
"insert_range",
441 "reserve",
"resize",
"shrink_to_fit",
443 "assign",
"assign_range"};
447 static const llvm::StringSet<> Deque = {
448 "insert",
"emplace",
"insert_range",
452 "resize",
"shrink_to_fit",
454 "assign",
"assign_range"};
456 static const llvm::StringSet<> String = {
458 "insert",
"push_back",
"append",
"replace",
"replace_with_range",
459 "insert_range",
"append_range",
461 "pop_back",
"erase",
"clear",
463 "reserve",
"resize",
"resize_and_overwrite",
"shrink_to_fit",
465 "swap",
"assign",
"assign_range"};
469 static const llvm::StringSet<> PriorityQueue = {
477 static const llvm::StringSet<> NodeBased = {
483 static const llvm::StringSet<> Flat = {
484 "insert",
"emplace",
"emplace_hint",
485 "try_emplace",
"insert_or_assign",
486 "insert_range",
"merge",
488 "extract",
"erase",
"clear",
492 static const llvm::StringSet<> UniquePtr = {
495 const StringRef RecordName =
getName(*RD);
498 const llvm::StringSet<> *InvalidatingMethods =
499 llvm::StringSwitch<const llvm::StringSet<> *>(RecordName)
501 .Case(
"basic_string", &String)
502 .Case(
"deque", &Deque)
503 .Case(
"priority_queue", &PriorityQueue)
504 .Cases({
"set",
"multiset",
"map",
"multimap",
"unordered_set",
505 "unordered_multiset",
"unordered_map",
"unordered_multimap"},
507 .Cases({
"flat_map",
"flat_set",
"flat_multimap",
"flat_multiset"},
509 .Case(
"unique_ptr", &UniquePtr)
512 if (!InvalidatingMethods)
525 return RecordName ==
"flat_map";
534 return InvalidatingMethods->contains(MD.
getName());
547 return Name ==
"function" || Name ==
"move_only_function";
554 case Builtin::BImove:
555 case Builtin::BImove_if_noexcept:
556 case Builtin::BIforward:
557 case Builtin::BIforward_like:
558 case Builtin::BIas_const:
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::Expr interface and subclasses for C++ expressions.
Defines an enumeration for C++ overloaded operators.
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 ...
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.
static bool hasSameUnqualifiedType(QualType T1, QualType T2)
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
static std::optional< AnyCall > forExpr(const Expr *E)
If E is a generic call (to ObjC method /function/block/etc), return a constructed AnyCall object.
Attr - This represents one attribute.
Type source information for an attributed type.
Represents a static or instance method of a struct/union/class.
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Represents a C++ struct/union/class.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Decl - This represents one declaration (or definition), e.g.
bool isInStdNamespace() const
ASTContext & getASTContext() const LLVM_READONLY
DeclContext * getDeclContext()
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
This represents one expression.
const CXXRecordDecl * getBestDynamicClassType() const
For an expression of class type or pointer to class type, return the most derived class decl the expr...
Represents a function declaration or definition.
const ParmVarDecl * getParamDecl(unsigned i) const
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
QualType getReturnType() const
FunctionDecl * getTemplateInstantiationPattern(bool ForDefinition=true) const
Retrieve the function declaration from which this function could be instantiated, if it is an instant...
FunctionDecl * getMostRecentDecl()
Returns the most recent (re)declaration of this declaration.
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
OverloadedOperatorKind getOverloadedOperator() const
getOverloadedOperator - Which C++ overloaded operator this function represents, if any.
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
size_t param_size() const
One of these records is kept for each identifier that is lexed.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Represents a parameter to a function.
A (possibly-)qualified type.
Base wrapper for a particular "section" of type source info.
T getAsAdjusted() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
A container of type source information.
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
The base class of the type hierarchy.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
bool isPointerType() const
bool isReferenceType() const
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
bool isLValueReferenceType() const
bool isNullPtrType() const
FunctionCallInfo getFunctionCallInfo(const Expr *Call)
const LifetimeBoundAttr * getDirectImplicitObjectLifetimeBoundAttr(const FunctionDecl *FD)
bool isGslPointerType(QualType QT)
bool isStdCallableWrapperType(const CXXRecordDecl *RD)
bool shouldTrackFirstArgument(const FunctionDecl *FD)
static StringRef getName(const CXXRecordDecl &RD)
bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD)
bool shouldTrackImplicitObjectArg(const Expr &ImplicitObjectArgument, const CXXMethodDecl *Callee, bool RunningUnderLifetimeSafety)
static const LifetimeBoundAttr * getLifetimeBoundAttrFromFunctionType(const TypeSourceInfo &TSI)
Check if a function has a lifetimebound attribute on its function type (which represents the implicit...
bool isPointerLikeType(QualType QT)
bool isNormalAssignmentOperator(const FunctionDecl *FD)
bool isUniquePtrRelease(const CXXMethodDecl &MD)
static bool isRecordWithAttr(const CXXRecordDecl *RD)
static bool isReferenceOrPointerLikeType(QualType QT)
bool isStdReferenceCast(const FunctionDecl *FD)
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
const LifetimeBoundAttr * getImplicitObjectParamLifetimeBoundAttr(const FunctionDecl *FD)
const FunctionDecl * getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD)
bool isInvalidationMethod(const CXXMethodDecl &MD)
std::optional< LifetimeBoundParamInfo > getTrackingInfoForCallArg(const Expr *Call, const Expr *Source)
bool destructsFirstArg(const FunctionDecl &FD)
bool isGslOwnerType(QualType QT)
std::optional< LifetimeBoundParamInfo > getTrackedArgInfo(const FunctionDecl *FD, llvm::ArrayRef< const Expr * > Args, unsigned I)
bool isInStlNamespace(const Decl *D)
bool shouldTrackSecondArgument(const FunctionDecl *FD)
static bool isStdUniquePtr(const CXXRecordDecl &RD)
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
@ OO_None
Not an overloaded operator.
bool isa(CodeGen::Address addr)
bool isCompoundAssignmentOperator(OverloadedOperatorKind Kind)
Determine if this is a compound assignment operator.
LLVM_READONLY bool isUppercase(unsigned char c)
Return true if this character is an uppercase ASCII letter: [A-Z].
@ Vector
'vector' clause, allowed on 'loop', Combined, and 'routine' directives.
@ Default
Set to the current date and time.
@ Result
The result type of a method or function.
const FunctionProtoType * T