21#include "llvm/ADT/StringMap.h"
27class MissingOriginCollector
28 :
public RecursiveASTVisitor<MissingOriginCollector> {
30 MissingOriginCollector(
31 const llvm::DenseMap<const clang::Expr *, OriginList *> &ExprToOriginList,
32 LifetimeSafetyStats &LSStats)
33 : ExprToOriginList(ExprToOriginList), LSStats(LSStats) {}
34 bool VisitExpr(Expr *E) {
38 if (!ExprToOriginList.contains(E)) {
40 LSStats.ExprTypeToMissingOriginCount[E->getType().getTypePtr()]++;
41 LSStats.ExprStmtClassToMissingOriginCount[std::string(
42 E->getStmtClassName())]++;
48 const llvm::DenseMap<const clang::Expr *, OriginList *> &ExprToOriginList;
49 LifetimeSafetyStats &LSStats;
92 const auto *MD = llvm::dyn_cast_or_null<CXXMethodDecl>(D);
93 if (!MD || !MD->isInstance())
99 ThisOrigins = buildListForType(MD->getThisType(), MD);
108OriginList *OriginManager::createNode(
const Expr *E,
QualType QT) {
111 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
115OriginList *OriginManager::buildListForType(
QualType QT,
const T *Node) {
116 assert(
hasOrigins(QT) &&
"buildListForType called for non-pointer type");
117 OriginList *Head = createNode(Node, QT);
124 Head->setInnerOriginList(buildListForType(PointeeTy, Node));
132 auto It = DeclToList.find(D);
133 if (It != DeclToList.end())
135 return DeclToList[D] = buildListForType(D->
getType(), D);
139 if (
auto *ParenIgnored = E->
IgnoreParens(); ParenIgnored != E)
148 auto It = ExprToList.find(E);
149 if (It != ExprToList.end())
160 const ValueDecl *ReferencedDecl =
nullptr;
161 if (
auto *DRE = dyn_cast<DeclRefExpr>(E))
162 ReferencedDecl = DRE->getDecl();
163 else if (
auto *ME = dyn_cast<MemberExpr>(E))
164 if (
auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
166 ReferencedDecl = Field;
167 if (ReferencedDecl) {
186 return ExprToList[E] = Head;
193 Type = AST.getLValueReferenceType(
Type);
194 return ExprToList[E] = buildListForType(
Type, E);
201 OS <<
"Decl: " << VD->getNameAsString();
204 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
205 if (
const ValueDecl *VD = DRE->getDecl())
206 OS <<
", Decl: " << VD->getNameAsString();
217 assert(ID.Value < AllOrigins.size());
218 return AllOrigins[ID.Value];
223 MissingOriginCollector Collector(this->ExprToList, LSStats);
224 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 ...
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 * 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)
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.