19#include "llvm/ADT/StringMap.h"
25class MissingOriginCollector
26 :
public RecursiveASTVisitor<MissingOriginCollector> {
28 MissingOriginCollector(
29 const llvm::DenseMap<const clang::Expr *, OriginList *> &ExprToOriginList,
30 LifetimeSafetyStats &LSStats)
31 : ExprToOriginList(ExprToOriginList), LSStats(LSStats) {}
32 bool VisitExpr(Expr *E) {
36 if (!ExprToOriginList.contains(E)) {
38 LSStats.ExprTypeToMissingOriginCount[E->getType().getTypePtr()]++;
39 LSStats.ExprStmtClassToMissingOriginCount[std::string(
40 E->getStmtClassName())]++;
46 const llvm::DenseMap<const clang::Expr *, OriginList *> &ExprToOriginList;
47 LifetimeSafetyStats &LSStats;
91 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
94OriginList *OriginManager::createNode(
const Expr *E,
QualType QT) {
97 return new (ListAllocator.Allocate<OriginList>()) OriginList(NewID);
101OriginList *OriginManager::buildListForType(QualType QT,
const T *Node) {
102 assert(
hasOrigins(QT) &&
"buildListForType called for non-pointer type");
103 OriginList *Head = createNode(Node, QT);
105 if (QT->isPointerOrReferenceType()) {
106 QualType PointeeTy = QT->getPointeeType();
110 Head->setInnerOriginList(buildListForType(PointeeTy, Node));
118 auto It = DeclToList.find(D);
119 if (It != DeclToList.end())
121 return DeclToList[D] = buildListForType(D->
getType(), D);
125 if (
auto *ParenIgnored = E->
IgnoreParens(); ParenIgnored != E)
131 auto It = ExprToList.find(E);
132 if (It != ExprToList.end())
138 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
157 return ExprToList[E] = Head;
164 Type = AST.getLValueReferenceType(
Type);
165 return ExprToList[E] = buildListForType(
Type, E);
172 OS <<
"Decl: " << VD->getNameAsString();
175 if (
auto *DRE = dyn_cast<DeclRefExpr>(E)) {
176 if (
const ValueDecl *VD = DRE->getDecl())
177 OS <<
", Decl: " << VD->getNameAsString();
188 assert(ID.Value < AllOrigins.size());
189 return AllOrigins[ID.Value];
194 MissingOriginCollector Collector(this->ExprToList, LSStats);
195 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.
C Language Family Type Representation.
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
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
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)
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.