20#include "llvm/ADT/StringMap.h"
26class MissingOriginCollector
27 :
public RecursiveASTVisitor<MissingOriginCollector> {
29 MissingOriginCollector(
30 const llvm::DenseMap<const clang::Expr *, OriginList *> &ExprToOriginList,
31 LifetimeSafetyStats &LSStats)
32 : ExprToOriginList(ExprToOriginList), LSStats(LSStats) {}
33 bool VisitExpr(Expr *E) {
37 if (!ExprToOriginList.contains(E)) {
39 LSStats.ExprTypeToMissingOriginCount[E->getType().getTypePtr()]++;
40 LSStats.ExprStmtClassToMissingOriginCount[std::string(
41 E->getStmtClassName())]++;
47 const llvm::DenseMap<const clang::Expr *, OriginList *> &ExprToOriginList;
48 LifetimeSafetyStats &LSStats;
90 if (
const auto *MD = llvm::dyn_cast_or_null<CXXMethodDecl>(D);
91 MD && MD->isInstance())
92 ThisOrigins = buildListForType(MD->getThisType(), MD);
101OriginList *OriginManager::createNode(
const Expr *E,
QualType QT) {
104 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
108OriginList *OriginManager::buildListForType(
QualType QT,
const T *Node) {
109 assert(
hasOrigins(QT) &&
"buildListForType called for non-pointer type");
110 OriginList *Head = createNode(Node, QT);
117 Head->setInnerOriginList(buildListForType(PointeeTy, Node));
125 auto It = DeclToList.find(D);
126 if (It != DeclToList.end())
128 return DeclToList[D] = buildListForType(D->
getType(), D);
132 if (
auto *ParenIgnored = E->
IgnoreParens(); ParenIgnored != E)
141 auto It = ExprToList.find(E);
142 if (It != ExprToList.end())
148 if (llvm::isa<CXXThisExpr>(E)) {
149 assert(ThisOrigins &&
"origins for 'this' should be set for a method decl");
154 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
173 return ExprToList[E] = Head;
180 Type = AST.getLValueReferenceType(
Type);
181 return ExprToList[E] = buildListForType(
Type, E);
188 OS <<
"Decl: " << VD->getNameAsString();
191 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
192 if (
const ValueDecl *VD = DRE->getDecl())
193 OS <<
", Decl: " << VD->getNameAsString();
204 assert(ID.Value < AllOrigins.size());
205 return AllOrigins[ID.Value];
210 MissingOriginCollector Collector(this->ExprToList, LSStats);
211 Collector.TraverseStmt(
const_cast<Stmt *
>(&FunctionBody));
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.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
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 * 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.
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.
const Origin & getOrigin(OriginID ID) const
void collectMissingOrigins(Stmt &FunctionBody, LifetimeSafetyStats &LSStats)
Collects statistics about expressions that lack associated origins.
void dump(OriginID OID, llvm::raw_ostream &OS) const
OriginManager(ASTContext &AST, const Decl *D)
utils::ID< struct OriginTag > OriginID
bool doesDeclHaveStorage(const ValueDecl *D)
Returns true if the declaration has its own storage that can be borrowed.
bool hasOrigins(QualType QT)
bool isGslPointerType(QualType QT)
const FunctionProtoType * T
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.