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; }
72 const llvm::SmallVector<QualType> &getCollectedTypes()
const {
73 return CollectedTypes;
77 llvm::SmallVector<QualType> CollectedTypes;
79 void collect(
const FunctionDecl *FD, QualType RetType) {
84 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD);
87 CollectedTypes.push_back(RetType);
91 for (
const auto *Param : FD->parameters()) {
92 if (Param->hasAttr<LifetimeBoundAttr>()) {
93 CollectedTypes.push_back(RetType);
118 for (
const auto *FD : RD->fields())
160 : AST(AC.getASTContext()) {
161 collectLifetimeAnnotatedOriginTypes(AC);
162 initializeThisOrigins(AC.
getDecl());
165void OriginManager::initializeThisOrigins(
const Decl *D) {
166 const auto *MD = llvm::dyn_cast_or_null<CXXMethodDecl>(D);
167 if (!MD || !MD->isInstance())
173 ThisOrigins = buildListForType(MD->getThisType(), MD);
179 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
182OriginList *OriginManager::createNode(
const Expr *E,
QualType QT) {
185 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
194 assert(
hasOrigins(QT) &&
"buildListForType called for non-pointer type");
210 auto It = DeclToList.find(D);
211 if (It != DeclToList.end())
213 return DeclToList[D] = buildListForType(D->
getType(), D);
217 if (
auto *ParenIgnored = E->
IgnoreParens(); ParenIgnored != E)
226 auto It = ExprToList.find(E);
227 if (It != ExprToList.end())
238 const ValueDecl *ReferencedDecl =
nullptr;
239 if (
auto *DRE = dyn_cast<DeclRefExpr>(E))
240 ReferencedDecl = DRE->getDecl();
241 else if (
auto *ME = dyn_cast<MemberExpr>(E))
242 if (
auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
244 ReferencedDecl = Field;
245 if (ReferencedDecl) {
264 return ExprToList[E] = Head;
271 Type = AST.getLValueReferenceType(
Type);
272 return ExprToList[E] = buildListForType(
Type, E);
279 OS <<
"Decl: " << VD->getNameAsString();
282 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
283 if (
const ValueDecl *VD = DRE->getDecl())
284 OS <<
", Decl: " << VD->getNameAsString();
295 assert(ID.Value < AllOrigins.size());
296 return AllOrigins[ID.Value];
301 MissingOriginCollector Collector(this->ExprToList, *
this, LSStats);
302 Collector.TraverseStmt(
const_cast<Stmt *
>(&FunctionBody));
305void OriginManager::collectLifetimeAnnotatedOriginTypes(
307 LifetimeAnnotatedOriginTypeCollector Collector;
309 Collector.TraverseStmt(Body);
310 if (
const auto *CD = dyn_cast<CXXConstructorDecl>(AC.
getDecl()))
311 for (
const auto *
Init : CD->inits())
312 Collector.TraverseStmt(
Init->getInit());
313 for (
QualType QT : Collector.getCollectedTypes())
314 registerLifetimeAnnotatedOriginType(QT);
317void 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 ...
bool hasOrigins(QualType QT) const
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
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.