22#include "llvm/ADT/StringMap.h"
28class MissingOriginCollector
29 :
public RecursiveASTVisitor<MissingOriginCollector> {
31 MissingOriginCollector(
32 const llvm::DenseMap<const clang::Expr *, OriginList *> &ExprToOriginList,
33 const OriginManager &OM, LifetimeSafetyStats &LSStats)
34 : ExprToOriginList(ExprToOriginList), OM(OM), LSStats(LSStats) {}
35 bool VisitExpr(Expr *E) {
36 if (!OM.hasOrigins(E))
39 if (!ExprToOriginList.contains(E)) {
41 LSStats.ExprTypeToMissingOriginCount[E->getType().getTypePtr()]++;
42 LSStats.ExprStmtClassToMissingOriginCount[std::string(
43 E->getStmtClassName())]++;
49 const llvm::DenseMap<const clang::Expr *, OriginList *> &ExprToOriginList;
50 const OriginManager &OM;
51 LifetimeSafetyStats &LSStats;
54class LifetimeAnnotatedOriginTypeCollector
55 :
public RecursiveASTVisitor<LifetimeAnnotatedOriginTypeCollector> {
57 bool VisitCallExpr(
const CallExpr *CE) {
60 if (
const auto *FD = CE->getDirectCallee())
61 collect(FD, FD->getReturnType());
65 bool VisitCXXConstructExpr(
const CXXConstructExpr *CCE) {
66 collect(CCE->getConstructor(), CCE->getType());
70 bool shouldVisitLambdaBody()
const {
return false; }
71 bool shouldVisitTemplateInstantiations()
const {
return true; }
73 const llvm::SmallVector<QualType> &getCollectedTypes()
const {
74 return CollectedTypes;
78 llvm::SmallVector<QualType> CollectedTypes;
80 void collect(
const FunctionDecl *FD, QualType RetType) {
85 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD);
88 CollectedTypes.push_back(RetType);
92 for (
const auto *Param : FD->parameters()) {
93 if (Param->hasAttr<LifetimeBoundAttr>()) {
94 CollectedTypes.push_back(RetType);
106 if (!IntrinsicOnly &&
120 for (
const auto *FD : RD->fields())
162 : AST(AC.getASTContext()) {
163 collectLifetimeAnnotatedOriginTypes(AC);
164 initializeThisOrigins(AC.
getDecl());
167void OriginManager::initializeThisOrigins(
const Decl *D) {
168 const auto *MD = llvm::dyn_cast_or_null<CXXMethodDecl>(D);
169 if (!MD || !MD->isInstance())
175 ThisOrigins = buildListForType(MD->getThisType(), MD);
181 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
184OriginList *OriginManager::createNode(
const Expr *E,
QualType QT) {
187 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
196 assert(
hasOrigins(QT) &&
"buildListForType called for non-pointer type");
212 auto It = DeclToList.find(D);
213 if (It != DeclToList.end())
215 return DeclToList[D] = buildListForType(D->
getType(), D);
219 if (
auto *ParenIgnored = E->
IgnoreParens(); ParenIgnored != E)
228 auto It = ExprToList.find(E);
229 if (It != ExprToList.end())
240 const ValueDecl *ReferencedDecl =
nullptr;
241 if (
auto *DRE = dyn_cast<DeclRefExpr>(E))
242 ReferencedDecl = DRE->getDecl();
243 else if (
auto *ME = dyn_cast<MemberExpr>(E))
244 if (
auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
246 ReferencedDecl = Field;
247 if (ReferencedDecl) {
266 return ExprToList[E] = Head;
273 Type = AST.getLValueReferenceType(
Type);
274 return ExprToList[E] = buildListForType(
Type, E);
281 OS <<
"Decl: " << VD->getNameAsString();
284 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
285 if (
const ValueDecl *VD = DRE->getDecl())
286 OS <<
", Decl: " << VD->getNameAsString();
297 assert(ID.Value < AllOrigins.size());
298 return AllOrigins[ID.Value];
303 MissingOriginCollector Collector(this->ExprToList, *
this, LSStats);
304 Collector.TraverseStmt(
const_cast<Stmt *
>(&FunctionBody));
307void OriginManager::collectLifetimeAnnotatedOriginTypes(
309 LifetimeAnnotatedOriginTypeCollector Collector;
311 Collector.TraverseStmt(Body);
312 if (
const auto *CD = dyn_cast<CXXConstructorDecl>(AC.
getDecl()))
313 for (
const auto *
Init : CD->inits())
314 Collector.TraverseStmt(
Init->getInit());
315 for (
QualType QT : Collector.getCollectedTypes())
316 registerLifetimeAnnotatedOriginType(QT);
319void OriginManager::registerLifetimeAnnotatedOriginType(
QualType QT) {
Defines the clang::ASTContext interface.
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
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.
C Language Family Type Representation.
AnalysisDeclContext contains the context data for the function, method or block under analysis.
const Decl * getDecl() const
Represents a C++ struct/union/class.
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Decl - This represents one declaration (or definition), e.g.
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
This represents one expression.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
A (possibly-)qualified type.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
QualType getCanonicalType() const
const Type * getTypePtrOrNull() const
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Stmt - This represents one statement.
const char * getStmtClassName() const
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 isReferenceType() const
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool isPointerOrReferenceType() const
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
A list of origins representing levels of indirection for pointer-like types.
void setInnerOriginList(OriginList *Inner)
OriginList * getOrCreateList(const ValueDecl *D)
Gets or creates the OriginList for a given ValueDecl.
OriginList * createSingleOriginList(OriginID OID)
Wraps an existing OriginID in a new single-element OriginList, so a fact can refer to a single level ...
const Origin & getOrigin(OriginID ID) const
void collectMissingOrigins(Stmt &FunctionBody, LifetimeSafetyStats &LSStats)
Collects statistics about expressions that lack associated origins.
OriginManager(const AnalysisDeclContext &AC)
void dump(OriginID OID, llvm::raw_ostream &OS) const
bool hasOrigins(QualType QT, bool IntrinsicOnly=false) const
Determines whether a type can carry lifetime origins.
utils::ID< struct OriginTag > OriginID
bool doesDeclHaveStorage(const ValueDecl *D)
Returns true if the declaration has its own storage that can be borrowed.
bool isGslPointerType(QualType QT)
bool isStdCallableWrapperType(const CXXRecordDecl *RD)
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
const FunctionDecl * getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD)
bool isa(CodeGen::Address addr)
A structure to hold the statistics related to LifetimeAnalysis.
An Origin is a symbolic identifier that represents the set of possible loans a pointer-like object co...
const clang::Expr * getExpr() const
const clang::ValueDecl * getDecl() const
const Type * Ty
The type at this indirection level.