15#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_DATAFLOW_H
16#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_DATAFLOW_H
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/TimeProfiler.h"
59template <
typename Derived,
typename LatticeType, Direction Dir>
91 Derived &D =
static_cast<Derived &
>(*this);
92 llvm::TimeTraceScope Time(D.getAnalysisName());
94 PointToState.resize(
FactMgr.getNumFacts());
95 InStates.resize(Cfg.getNumBlockIDs());
96 OutStates.resize(Cfg.getNumBlockIDs());
103 const CFGBlock *Start = isForward() ? &Cfg.getEntry() : &Cfg.getExit();
104 InStates[Start->
getBlockID()] = D.getInitialState();
105 W.enqueueBlock(Start);
112 if constexpr (!isForward()) {
114 if (!InStates[B->getBlockID()])
121 template <
typename WorklistT>
void drainWorklist(Derived &D, WorklistT &W) {
122 while (
const CFGBlock *B = W.dequeue()) {
123 std::optional<Lattice> StateIn =
getInState(B);
125 transferBlock(B, StateIn ? *StateIn : D.getInitialState());
126 OutStates[B->getBlockID()] = StateOut;
127 for (
const CFGBlock *AdjacentB : isForward() ? B->succs() : B->preds()) {
130 std::optional<Lattice> OldInState =
getInState(AdjacentB);
132 !OldInState ? StateOut : D.join(*OldInState, StateOut);
135 if (!OldInState || NewInState != *OldInState) {
136 InStates[AdjacentB->getBlockID()] = NewInState;
137 W.enqueueBlock(AdjacentB);
157 const Derived *D =
static_cast<const Derived *
>(
this);
158 llvm::dbgs() <<
"==========================================\n";
159 llvm::dbgs() << D->getAnalysisName() <<
" results:\n";
160 llvm::dbgs() <<
"==========================================\n";
161 const CFGBlock &B = isForward() ? Cfg.getExit() : Cfg.getEntry();
168 Lattice transferBlock(
const CFGBlock *
Block, Lattice State) {
170 if constexpr (isForward()) {
171 for (
const Fact *F : Facts) {
172 State = transferFact(State, F);
173 PointToState[F->getID().Value] = State;
176 for (
const Fact *F : llvm::reverse(Facts)) {
178 PointToState[F->getID().Value] = State;
179 State = transferFact(State, F);
187 Derived *D =
static_cast<Derived *
>(
this);
188 switch (F->getKind()) {
190 return D->transfer(In, *F->getAs<IssueFact>());
192 return D->transfer(In, *F->getAs<ExpireFact>());
194 return D->transfer(In, *F->getAs<OriginFlowFact>());
196 return D->transfer(In, *F->getAs<MovedOriginFact>());
198 return D->transfer(In, *F->getAs<OriginEscapesFact>());
200 return D->transfer(In, *F->getAs<UseFact>());
202 return D->transfer(In, *F->getAs<TestPointFact>());
204 return D->transfer(In, *F->getAs<InvalidateOriginFact>());
206 return D->transfer(In, *F->getAs<KillOriginFact>());
208 llvm_unreachable(
"Unknown fact kind");
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a single basic block in a source-level CFG.
unsigned getBlockID() const
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Lattice transfer(Lattice In, const InvalidateOriginFact &)
DataflowAnalysis(const CFG &Cfg, AnalysisDeclContext &AC, FactManager &FactMgr)
Lattice transfer(Lattice In, const KillOriginFact &)
Lattice transferAtBlockExit(Lattice In)
std::optional< Lattice > getInState(const CFGBlock *B) const
Lattice transfer(Lattice In, const OriginFlowFact &)
Lattice transfer(Lattice In, const MovedOriginFact &)
Lattice transfer(Lattice In, const TestPointFact &)
Lattice getOutState(const CFGBlock *B) const
Lattice transfer(Lattice In, const UseFact &)
Lattice transfer(Lattice In, const IssueFact &)
Lattice transfer(Lattice In, const OriginEscapesFact &)
Lattice transfer(Lattice In, const ExpireFact &)
DataflowAnalysis< Derived, Lattice, Dir > Base
Lattice getState(ProgramPoint P) const
When an AccessPath expires (e.g., a variable goes out of scope), all loans that are associated with t...
llvm::ArrayRef< const Fact * > getFacts(const CFGBlock *B) const
An abstract base class for a single, atomic lifetime-relevant event.
@ InvalidateOrigin
An origin is invalidated (e.g. vector resized, delete called).
@ TestPoint
A marker for a specific point in the code, for testing.
@ Expire
A loan expires as its underlying storage is freed (e.g., variable goes out of scope).
@ Issue
A new loan is issued from a borrow expression (e.g., &x).
@ OriginFlow
An origin is propagated from a source to a destination (e.g., p = q).
@ MovedOrigin
An origin that is moved (e.g., passed to an rvalue reference parameter).
@ Use
An origin is used (eg. appears as l-value expression like DeclRefExpr).
@ OriginEscapes
An origin that escapes the function scope (e.g., via return).
@ KillOrigin
All loans of an origin are cleared.
Represents that an origin's storage has been invalidated by a container operation (e....
All loans are cleared from an origin (e.g., assigning a callable without tracked origins to std::func...
Top-level origin of the expression which was found to be moved, e.g, when being used as an argument t...
Represents that an origin escapes the current scope through various means.
A dummy-fact used to mark a specific point in the code for testing.
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
A worklist implementation for backward dataflow analysis.
A worklist implementation for forward dataflow analysis.