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::lifetimes {
17
18const FunctionDecl *
20 return FD != nullptr ? FD->getMostRecentDecl() : nullptr;
21}
22
23const CXXMethodDecl *
25 const FunctionDecl *FD = CMD;
26 return cast_if_present<CXXMethodDecl>(
28}
29
32 bool IsAssignment = OO == OO_Equal || isCompoundAssignmentOperator(OO);
33 if (!IsAssignment)
34 return false;
35 QualType RetT = FD->getReturnType();
36 if (!RetT->isLValueReferenceType())
37 return false;
38 ASTContext &Ctx = FD->getASTContext();
39 QualType LHST;
40 auto *MD = dyn_cast<CXXMethodDecl>(FD);
41 if (MD && MD->isCXXInstanceMember())
42 LHST = Ctx.getLValueReferenceType(MD->getFunctionObjectParameterType());
43 else
44 LHST = FD->getParamDecl(0)->getType();
45 return Ctx.hasSameType(RetT, LHST);
46}
47
50 return CMD && isNormalAssignmentOperator(CMD) && CMD->param_size() == 1 &&
51 CMD->getParamDecl(0)->hasAttr<clang::LifetimeBoundAttr>();
52}
53
56 const TypeSourceInfo *TSI = FD->getTypeSourceInfo();
57 if (!TSI)
58 return false;
59 // Don't declare this variable in the second operand of the for-statement;
60 // GCC miscompiles that by ending its lifetime before evaluating the
61 // third operand. See gcc.gnu.org/PR86769.
63 for (TypeLoc TL = TSI->getTypeLoc();
64 (ATL = TL.getAsAdjusted<AttributedTypeLoc>());
65 TL = ATL.getModifiedLoc()) {
66 if (ATL.getAttrAs<clang::LifetimeBoundAttr>())
67 return true;
68 }
69
71}
72
73} // namespace clang::lifetimes
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: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
bool isLValueReferenceType() const
Definition TypeBase.h:8543
QualType getType() const
Definition Decl.h:723
bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD)
bool isNormalAssignmentOperator(const FunctionDecl *FD)
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
const FunctionDecl * getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD)
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
bool isCompoundAssignmentOperator(OverloadedOperatorKind Kind)
Determine if this is a compound assignment operator.