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);
119 for (
const auto *FD : RD->fields())
161 : AST(AC.getASTContext()) {
162 collectLifetimeAnnotatedOriginTypes(AC);
163 initializeThisOrigins(AC.
getDecl());
166void OriginManager::initializeThisOrigins(
const Decl *D) {
167 const auto *MD = llvm::dyn_cast_or_null<CXXMethodDecl>(D);
168 if (!MD || !MD->isInstance())
174 ThisOrigins = buildListForType(MD->getThisType(), MD);
180 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
183OriginList *OriginManager::createNode(
const Expr *E,
QualType QT) {
186 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
195 assert(
hasOrigins(QT) &&
"buildListForType called for non-pointer type");
211 auto It = DeclToList.find(D);
212 if (It != DeclToList.end())
214 return DeclToList[D] = buildListForType(D->
getType(), D);
218 if (
auto *ParenIgnored = E->
IgnoreParens(); ParenIgnored != E)
227 auto It = ExprToList.find(E);
228 if (It != ExprToList.end())
239 const ValueDecl *ReferencedDecl =
nullptr;
240 if (
auto *DRE = dyn_cast<DeclRefExpr>(E))
241 ReferencedDecl = DRE->getDecl();
242 else if (
auto *ME = dyn_cast<MemberExpr>(E))
243 if (
auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
245 ReferencedDecl = Field;
246 if (ReferencedDecl) {
265 return ExprToList[E] = Head;
272 Type = AST.getLValueReferenceType(
Type);
273 return ExprToList[E] = buildListForType(
Type, E);
280 OS <<
"Decl: " << VD->getNameAsString();
283 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
284 if (
const ValueDecl *VD = DRE->getDecl())
285 OS <<
", Decl: " << VD->getNameAsString();
296 assert(ID.Value < AllOrigins.size());
297 return AllOrigins[ID.Value];
302 MissingOriginCollector Collector(this->ExprToList, *
this, LSStats);
303 Collector.TraverseStmt(
const_cast<Stmt *
>(&FunctionBody));
306void OriginManager::collectLifetimeAnnotatedOriginTypes(
308 LifetimeAnnotatedOriginTypeCollector Collector;
310 Collector.TraverseStmt(Body);
311 if (
const auto *CD = dyn_cast<CXXConstructorDecl>(AC.
getDecl()))
312 for (
const auto *
Init : CD->inits())
313 Collector.TraverseStmt(
Init->getInit());
314 for (
QualType QT : Collector.getCollectedTypes())
315 registerLifetimeAnnotatedOriginType(QT);
318void 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.