27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/TimeProfiler.h"
42 if (
const auto *ND = dyn_cast<NamedDecl>(D))
43 Name = ND->getQualifiedNameAsString();
48 DEBUG_WITH_TYPE(Name.c_str(), FactMgr.
dump(Cfg, AC, LPA));
55 : AC(AC), SemaHelper(SemaHelper), LSOpts(LSOpts) {}
58 llvm::TimeTraceScope TimeProfile(
"LifetimeSafetyAnalysis");
60 const CFG &Cfg = *AC.getCFG();
61 if (LSOpts.MaxCFGBlocks > 0 && Cfg.
getNumBlockIDs() > LSOpts.MaxCFGBlocks) {
63 "LifetimeSafety", std::string FuncName =
"<unknown>";
64 if (
const Decl *D = AC.getDecl())
if (
const auto *ND =
65 dyn_cast<NamedDecl>(D))
66 FuncName = ND->getQualifiedNameAsString();
67 llvm::dbgs() <<
"LifetimeSafety: Skipping function " << FuncName
69 <<
" blocks (threshold: " << LSOpts.MaxCFGBlocks <<
")\n");
73 FactMgr = std::make_unique<FactManager>(AC, Cfg);
89 LoanPropagation = std::make_unique<LoanPropagationAnalysis>(
90 Cfg, AC, *FactMgr, Factory.OriginMapFactory, Factory.LoanSetFactory);
92 LiveOrigins = std::make_unique<LiveOriginsAnalysis>(
93 Cfg, AC, *FactMgr, Factory.LivenessMapFactory);
95 MovedLoans = std::make_unique<MovedLoansAnalysis>(
96 Cfg, AC, *FactMgr, *LoanPropagation, *LiveOrigins, FactMgr->getLoanMgr(),
97 Factory.MovedLoansMapFactory);
102 DEBUG_WITH_TYPE(
"PrintCFG", Cfg.
dump(AC.getASTContext().getLangOpts(),
105 DEBUG_WITH_TYPE(
"LifetimeFacts",
106 FactMgr->dump(Cfg, AC, LoanPropagation.get()));
110 DEBUG_WITH_TYPE(
"EnableFilterByFunctionName",
112 DEBUG_WITH_TYPE(
"LiveOrigins",
113 LiveOrigins->dump(llvm::dbgs(), FactMgr->getTestPoints()));
119 if (FunctionBody ==
nullptr)
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
C Language Family Type Representation.
const LangOptions & getLangOpts() const
AnalysisDeclContext contains the context data for the function, method or block under analysis.
const Decl * getDecl() const
ASTContext & getASTContext() const
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
void dump(const LangOptions &LO, bool ShowColors) const
dump - A simple pretty printer of a CFG that outputs to stderr.
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Decl - This represents one declaration (or definition), e.g.
Stmt - This represents one statement.
Abstract interface for operations requiring Sema access.
OriginManager & getOriginMgr()
void dump(const CFG &Cfg, AnalysisDeclContext &AC, const LoanPropagationAnalysis *LPA=nullptr) const
Running the lifetime safety analysis and querying its results.
FactManager & getFactManager()
LifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetySemaHelper *SemaHelper, const LifetimeSafetyOpts &LSOpts)
Manages the creation, storage, and retrieval of origins for pointer-like variables and expressions.
void collectMissingOrigins(Stmt &FunctionBody, LifetimeSafetyStats &LSStats)
Collects statistics about expressions that lack associated origins.
static void DebugOnlyFunction(AnalysisDeclContext &AC, const CFG &Cfg, FactManager &FactMgr, const LoanPropagationAnalysis *LPA)
void runLifetimeChecker(const LoanPropagationAnalysis &LoanPropagation, const MovedLoansAnalysis &MovedLoans, const LiveOriginsAnalysis &LiveOrigins, FactManager &FactMgr, AnalysisDeclContext &ADC, LifetimeSafetySemaHelper *SemaHelper, const LifetimeSafetyOpts &LSOpts)
Runs the lifetime checker, which detects use-after-free errors by examining loan expiration points an...
void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM, LifetimeSafetyStats &Stats)
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetySemaHelper *SemaHelper, const LifetimeSafetyOpts &Opts, LifetimeSafetyStats &Stats, bool CollectStats)
The main entry point for the analysis.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
A structure to hold the statistics related to LifetimeAnalysis.