27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/TimeProfiler.h"
41 if (
const auto *ND = dyn_cast<NamedDecl>(D))
42 Name = ND->getQualifiedNameAsString();
47 DEBUG_WITH_TYPE(Name.c_str(), FactMgr.
dump(Cfg, AC));
54 : AC(AC), SemaHelper(SemaHelper), LSOpts(LSOpts) {}
57 llvm::TimeTraceScope TimeProfile(
"LifetimeSafetyAnalysis");
59 const CFG &Cfg = *AC.getCFG();
60 if (LSOpts.MaxCFGBlocks > 0 && Cfg.
getNumBlockIDs() > LSOpts.MaxCFGBlocks) {
62 "LifetimeSafety", std::string FuncName =
"<unknown>";
63 if (
const Decl *D = AC.getDecl())
if (
const auto *ND =
64 dyn_cast<NamedDecl>(D))
65 FuncName = ND->getQualifiedNameAsString();
66 llvm::dbgs() <<
"LifetimeSafety: Skipping function " << FuncName
68 <<
" blocks (threshold: " << LSOpts.MaxCFGBlocks <<
")\n");
72 FactMgr = std::make_unique<FactManager>(AC, Cfg);
88 LoanPropagation = std::make_unique<LoanPropagationAnalysis>(
89 Cfg, AC, *FactMgr, Factory.OriginMapFactory, Factory.LoanSetFactory);
91 LiveOrigins = std::make_unique<LiveOriginsAnalysis>(
92 Cfg, AC, *FactMgr, Factory.LivenessMapFactory);
94 MovedLoans = std::make_unique<MovedLoansAnalysis>(
95 Cfg, AC, *FactMgr, *LoanPropagation, *LiveOrigins, FactMgr->getLoanMgr(),
96 Factory.MovedLoansMapFactory);
101 DEBUG_WITH_TYPE(
"PrintCFG", Cfg.
dump(AC.getASTContext().getLangOpts(),
104 DEBUG_WITH_TYPE(
"LifetimeFacts", FactMgr->dump(Cfg, AC));
108 DEBUG_WITH_TYPE(
"EnableFilterByFunctionName",
110 DEBUG_WITH_TYPE(
"LiveOrigins",
111 LiveOrigins->dump(llvm::dbgs(), FactMgr->getTestPoints()));
117 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
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.
void runLifetimeChecker(const LoanPropagationAnalysis &LoanPropagation, const MovedLoansAnalysis &MovedLoans, const LiveOriginsAnalysis &LiveOrigins, FactManager &FactMgr, AnalysisDeclContext &ADC, LifetimeSafetySemaHelper *SemaHelper)
Runs the lifetime checker, which detects use-after-free errors by examining loan expiration points an...
static void DebugOnlyFunction(AnalysisDeclContext &AC, const CFG &Cfg, FactManager &FactMgr)
void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM, LifetimeSafetyStats &Stats)
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetySemaHelper *SemaHelper, LifetimeSafetyStats &Stats, bool CollectStats)
The main entry point for the analysis.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
size_t MaxCFGBlocks
Maximum number of CFG blocks to analyze.
A structure to hold the statistics related to LifetimeAnalysis.