[clang] 838a8a7 - [analyzer] Avoid redundant ProgramState refcounting and copies (#244848)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 14:51:13 PST 2026
Author: Gábor Horváth
Date: 2026-11-23T22:51:07Z
New Revision: 838a8a74fb6b6f319593b5af9fbd725a45b665f8
URL: https://github.com/llvm/llvm-project/commit/838a8a74fb6b6f319593b5af9fbd725a45b665f8
DIFF: https://github.com/llvm/llvm-project/commit/838a8a74fb6b6f319593b5af9fbd725a45b665f8.diff
LOG: [analyzer] Avoid redundant ProgramState refcounting and copies (#244848)
Every ProgramState retain and release was an out-of-line call, and
creating a state copied it twice: callers build a temporary copy, and
getPersistentState copied that again into the uniqued slot before the
temporary was destroyed. Each copy also retains the store through a
virtual call, and RegionStore's retain/release built a temporary
RegionBindingsRef, adding two more refcount operations.
- Inline the ProgramState retain and the release fast path. The
IntrusiveRefCntPtrInfo members are templates so they are instantiated
only where ProgramState is complete; freeing stays out of line.
- Give ProgramState a move constructor and move the temporary into the
persistent slot.
- Give StoreRef move operations and use them when consuming a StoreRef.
- Retain/release the RegionStore tree root directly.
- Pass ProgramStateRef by const reference along the node creation path
(CheckerContext::addTransition and friends, CoreEngine::makeNode,
ExplodedGraph::getNode).
On some LLVM TUs (GVN, LiveVariables, RegionStore, SemaExpr) this
reduces the instructions of the static analyzer by 2.1-3.0% and the
cycles by 1.8-2.9%; peak memory is unchanged. The analyzer statistics
and diagnostics are unchanged.
Assisted-by: Claude Opus 5.5
---------
Co-authored-by: Gabor Horvath <gaborh at apple.com>
Added:
Modified:
clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/Store.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h
clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
clang/lib/StaticAnalyzer/Core/Checker.cpp
clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp
clang/lib/StaticAnalyzer/Core/ExprEngineObjC.cpp
clang/lib/StaticAnalyzer/Core/ProgramState.cpp
clang/lib/StaticAnalyzer/Core/RegionStore.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
index 3ea2ad1310def..02b59ced4321f 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
@@ -191,7 +191,7 @@ class CheckerContext {
/// When \p Tag is nullptr, node creation will default to using the
/// active checker as the tag -- but if there is no state change,
/// the predecessor node may be returned even if it isn't tagged so.
- ExplodedNode *addTransition(ProgramStateRef State = nullptr,
+ ExplodedNode *addTransition(const ProgramStateRef &State = nullptr,
const ProgramPointTag *Tag = nullptr) {
if ((!State || State == getState()) && !Tag)
return getPredecessor();
@@ -214,7 +214,7 @@ class CheckerContext {
/// used as a workaround when the node wouldn't be unique otherwise.
/// When \p Tag is nullptr, node creation will default to using the
/// active checker as the tag.
- ExplodedNode *addTransition(ProgramStateRef State, ExplodedNode *Pred,
+ ExplodedNode *addTransition(const ProgramStateRef &State, ExplodedNode *Pred,
const ProgramPointTag *Tag = nullptr) {
if (!Pred)
Pred = getPredecessor();
@@ -241,7 +241,7 @@ class CheckerContext {
/// @param Tag A ProgramPointTag that will be attached to the node; nullptr
/// (the default) means using the active checker as the tag. Using
/// another tag has no compelling use case (apart from debugging).
- ExplodedNode *addSink(ProgramStateRef State = nullptr,
+ ExplodedNode *addSink(const ProgramStateRef &State = nullptr,
ExplodedNode *Pred = nullptr,
const ProgramPointTag *Tag = nullptr) {
return addTransitionImpl(State, true, Pred, Tag);
@@ -263,7 +263,7 @@ class CheckerContext {
/// When \p Tag is nullptr, node creation will default to using the
/// active checker as the tag.
ExplodedNode *
- generateNonFatalErrorNode(ProgramStateRef State = nullptr,
+ generateNonFatalErrorNode(const ProgramStateRef &State = nullptr,
const ProgramPointTag *Tag = nullptr) {
return addTransitionImpl(State, false, nullptr, Tag);
}
@@ -284,8 +284,7 @@ class CheckerContext {
/// When \p Tag is nullptr, node creation will default to using the
/// active checker as the tag.
ExplodedNode *
- generateNonFatalErrorNode(ProgramStateRef State,
- ExplodedNode *Pred,
+ generateNonFatalErrorNode(const ProgramStateRef &State, ExplodedNode *Pred,
const ProgramPointTag *Tag = nullptr) {
return addTransitionImpl(State, false, Pred, Tag);
}
@@ -436,19 +435,17 @@ class CheckerContext {
std::string getMacroNameOrSpelling(SourceLocation &Loc);
private:
- ExplodedNode *addTransitionImpl(ProgramStateRef State, bool MarkAsSink,
+ ExplodedNode *addTransitionImpl(const ProgramStateRef &State, bool MarkAsSink,
ExplodedNode *P, const ProgramPointTag *Tag) {
Changed = true;
const ProgramPoint &LocalLoc = (Tag ? Location.withTag(Tag) : Location);
if (!P)
P = getPredecessor();
- if (!State)
- State = P->getState();
Frontier.erase(P);
- ExplodedNode *N =
- Eng.getCoreEngine().makeNode(LocalLoc, State, P, MarkAsSink);
+ ExplodedNode *N = Eng.getCoreEngine().makeNode(
+ LocalLoc, State ? State.get() : P->getState().get(), P, MarkAsSink);
Frontier.insert(N);
return N;
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h
index 04906ef6c7189..6f0497426855b 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h
@@ -171,10 +171,10 @@ class CoreEngine {
auto aborted_blocks() const { return llvm::iterator_range(blocksAborted); }
- ExplodedNode *makeNode(const ProgramPoint &Loc, ProgramStateRef State,
+ ExplodedNode *makeNode(const ProgramPoint &Loc, const ProgramState *State,
ExplodedNode *Pred, bool MarkAsSink = false) const;
- ExplodedNode *makePostStmtNode(const Stmt *S, ProgramStateRef State,
+ ExplodedNode *makePostStmtNode(const Stmt *S, const ProgramState *State,
ExplodedNode *Pred,
bool MarkAsSink = false) const {
PostStmt Loc(S, Pred->getStackFrame(), /*tag=*/nullptr);
@@ -188,7 +188,7 @@ class CoreEngine {
const StackFrame *SF = Pred->getStackFrame();
State = State->BindExpr(E, SF, V);
const auto &L = ProgramPoint::getProgramPoint(E, K, SF, /*tag=*/nullptr);
- return makeNode(L, State, Pred);
+ return makeNode(L, State.get(), Pred);
}
ExplodedNode *
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
index 1a8cf6fc37667..c5929e2ffb7d1 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
@@ -173,18 +173,16 @@ class ExplodedNode : public llvm::FoldingSetNode {
return getState()->getSVal(E, getStackFrame());
}
- static void Profile(llvm::FoldingSetNodeID &ID,
- const ProgramPoint &Loc,
- const ProgramStateRef &state,
- bool IsSink) {
+ static void Profile(llvm::FoldingSetNodeID &ID, const ProgramPoint &Loc,
+ const ProgramState *state, bool IsSink) {
ID.Add(Loc);
- ID.AddPointer(state.get());
+ ID.AddPointer(state);
ID.AddBoolean(IsSink);
}
void Profile(llvm::FoldingSetNodeID& ID) const {
// We avoid copy constructors by not using accessors.
- Profile(ID, Location, State, isSink());
+ Profile(ID, Location, State.get(), isSink());
}
/// addPredeccessor - Adds a predecessor to the current node, and
@@ -346,9 +344,8 @@ class ExplodedGraph {
/// Retrieve the node associated with a (Location, State) pair, where the
/// 'Location' is a ProgramPoint in the CFG. If no node for this pair exists,
/// it is created. IsNew is set to true if the node was freshly created.
- ExplodedNode *getNode(const ProgramPoint &L, ProgramStateRef State,
- bool IsSink = false,
- bool* IsNew = nullptr);
+ ExplodedNode *getNode(const ProgramPoint &L, const ProgramState *State,
+ bool IsSink = false, bool *IsNew = nullptr);
/// Create a node for a (Location, State) pair, but don't store it for
/// deduplication later. This is useful when copying some nodes from an
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
index 3458fa9fe27a4..e789a49f4cd50 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
@@ -125,7 +125,7 @@ class ProgramState : public llvm::FoldingSetNode {
}
ProgramStateRef cloneAsPosteriorlyOverconstrained() const;
- unsigned refCount;
+ mutable unsigned refCount;
/// makeWithStore - Return a ProgramState with the same values as the current
/// state with the exception of using the specified Store.
@@ -142,6 +142,7 @@ class ProgramState : public llvm::FoldingSetNode {
/// Copy ctor - We must explicitly define this or else the "Next" ptr
/// in FoldingSetNode will also get copied.
ProgramState(const ProgramState &RHS);
+ ProgramState(ProgramState &&RHS);
~ProgramState();
@@ -483,8 +484,10 @@ class ProgramState : public llvm::FoldingSetNode {
void dump() const;
private:
- friend void ProgramStateRetain(const ProgramState *state);
- friend void ProgramStateRelease(const ProgramState *state);
+ friend struct llvm::IntrusiveRefCntPtrInfo<const ProgramState>;
+
+ void retain() const { ++refCount; }
+ void release() const;
SVal desugarReference(SVal Val) const;
SVal wrapSymbolicRegion(SVal Base) const;
@@ -496,7 +499,7 @@ class ProgramState : public llvm::FoldingSetNode {
class ProgramStateManager {
friend class ProgramState;
- friend void ProgramStateRelease(const ProgramState *state);
+
private:
/// Eng - The ExprEngine that owns this state manager.
ExprEngine *Eng; /* Can be null. */
@@ -527,6 +530,9 @@ class ProgramStateManager {
/// A vector of ProgramStates that we can reuse.
std::vector<ProgramState *> freeStates;
+ /// Called when the reference count of \p State drops to zero.
+ void freeState(ProgramState *State);
+
public:
ProgramStateManager(ASTContext &Ctx,
StoreManagerCreator CreateStoreManager,
@@ -598,7 +604,7 @@ class ProgramStateManager {
StoreMgr->iterBindings(state->getStore(), F);
}
- ProgramStateRef getPersistentState(ProgramState &Impl);
+ ProgramStateRef getPersistentState(ProgramState &&Impl);
ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState,
ProgramStateRef GDMState);
@@ -690,6 +696,12 @@ class ProgramStateManager {
// Out-of-line method definitions for ProgramState.
//===----------------------------------------------------------------------===//
+inline void ProgramState::release() const {
+ assert(refCount > 0);
+ if (--refCount == 0)
+ stateMgr->freeState(const_cast<ProgramState *>(this));
+}
+
inline ConstraintManager &ProgramState::getConstraintManager() const {
return stateMgr->getConstraintManager();
}
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h
index 0ea26bf2e509b..a3ba1ac67f55a 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h
@@ -16,19 +16,17 @@ namespace clang {
namespace ento {
class ProgramState;
class ProgramStateManager;
- void ProgramStateRetain(const ProgramState *state);
- void ProgramStateRelease(const ProgramState *state);
}
}
namespace llvm {
template <> struct IntrusiveRefCntPtrInfo<const clang::ento::ProgramState> {
- static void retain(const clang::ento::ProgramState *state) {
- clang::ento::ProgramStateRetain(state);
- }
- static void release(const clang::ento::ProgramState *state) {
- clang::ento::ProgramStateRelease(state);
- }
+ // These are templates so that their bodies are only instantiated where
+ // ProgramState is complete, which lets them be inlined. Destroying a
+ // ProgramStateRef needs the complete type, including in a function that
+ // takes one by value, as the callee destroys the arguments in some ABIs.
+ template <typename T> static void retain(T *state) { state->retain(); }
+ template <typename T> static void release(T *state) { state->release(); }
};
}
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/Store.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/Store.h
index cda21138fe5e3..555d5d59d447c 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/Store.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/Store.h
@@ -301,9 +301,8 @@ inline StoreRef::StoreRef(Store store, StoreManager & smgr)
mgr.incrementReferenceCount(store);
}
-inline StoreRef::StoreRef(const StoreRef &sr)
- : store(sr.store), mgr(sr.mgr)
-{
+inline StoreRef::StoreRef(const StoreRef &Other)
+ : store(Other.store), mgr(Other.mgr) {
if (store)
mgr.incrementReferenceCount(store);
}
@@ -313,12 +312,23 @@ inline StoreRef::~StoreRef() {
mgr.decrementReferenceCount(store);
}
-inline StoreRef &StoreRef::operator=(StoreRef const &newStore) {
- assert(&newStore.mgr == &mgr);
- if (store != newStore.store) {
- mgr.incrementReferenceCount(newStore.store);
+inline StoreRef &StoreRef::operator=(StoreRef const &Other) {
+ assert(&Other.mgr == &mgr);
+ if (store != Other.store) {
+ mgr.incrementReferenceCount(Other.store);
mgr.decrementReferenceCount(store);
- store = newStore.getStore();
+ store = Other.getStore();
+ }
+ return *this;
+}
+
+inline StoreRef &StoreRef::operator=(StoreRef &&Other) {
+ assert(&Other.mgr == &mgr);
+ if (store != Other.store) {
+ if (store)
+ mgr.decrementReferenceCount(store);
+ store = Other.store;
+ Other.store = nullptr;
}
return *this;
}
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h
index a2dd05cfdf4c9..4b4fcf8e0d221 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h
@@ -32,8 +32,12 @@ class StoreRef {
public:
StoreRef(Store store, StoreManager &smgr);
- StoreRef(const StoreRef &sr);
- StoreRef &operator=(StoreRef const &newStore);
+ StoreRef(const StoreRef &Other);
+ StoreRef(StoreRef &&Other) : store(Other.store), mgr(Other.mgr) {
+ Other.store = nullptr;
+ }
+ StoreRef &operator=(StoreRef const &Other);
+ StoreRef &operator=(StoreRef &&Other);
~StoreRef();
bool operator==(const StoreRef &x) const {
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
index acc6baa83df84..c5419db957615 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
@@ -636,7 +636,7 @@ class SymbolReaper {
/// Set to the value of the symbolic store after
/// StoreManager::removeDeadBindings has been called.
- void setReapedStore(StoreRef st) { reapedStore = st; }
+ void setReapedStore(StoreRef st) { reapedStore = std::move(st); }
private:
bool isLazilyCopiedRegion(const MemRegion *region) const;
diff --git a/clang/lib/StaticAnalyzer/Core/Checker.cpp b/clang/lib/StaticAnalyzer/Core/Checker.cpp
index f06ae7d093b60..ee06c0e9f5bb7 100644
--- a/clang/lib/StaticAnalyzer/Core/Checker.cpp
+++ b/clang/lib/StaticAnalyzer/Core/Checker.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
using namespace clang;
using namespace ento;
diff --git a/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp b/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
index 04b700726fbc6..9fbb3e13db0a6 100644
--- a/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
@@ -114,7 +114,7 @@ bool CoreEngine::ExecuteWorkList(const StackFrame *SF, unsigned MaxSteps,
InitState = ExprEng.getInitialState(SF);
bool IsNew;
- ExplodedNode *Node = G.getNode(StartLoc, InitState, false, &IsNew);
+ ExplodedNode *Node = G.getNode(StartLoc, InitState.get(), false, &IsNew);
assert(IsNew);
G.designateAsRoot(Node);
@@ -286,7 +286,7 @@ void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) {
},
/*IsPrunable=*/true));
// Perform the transition.
- Pred = makeNode(P, Pred->getState(), Pred);
+ Pred = makeNode(P, Pred->getState().get(), Pred);
if (!Pred)
return;
}
@@ -395,7 +395,7 @@ void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
for (const CFGBlock *Succ : B->succs()) {
if (Succ) {
BlockEdge BE(B, Succ, Pred->getStackFrame());
- if (ExplodedNode *N = makeNode(BE, Pred->State, Pred))
+ if (ExplodedNode *N = makeNode(BE, Pred->State.get(), Pred))
WList->enqueue(N);
}
}
@@ -476,7 +476,7 @@ void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
"Blocks with no terminator should have at most 1 successor.");
BlockEdge BE(B, *(B->succ_begin()), Pred->getStackFrame());
- if (ExplodedNode *N = makeNode(BE, Pred->State, Pred))
+ if (ExplodedNode *N = makeNode(BE, Pred->State.get(), Pred))
WList->enqueue(N);
}
@@ -560,8 +560,8 @@ void CoreEngine::HandleVirtualBaseBranch(const CFGBlock *B,
}
ExplodedNode *CoreEngine::makeNode(const ProgramPoint &Loc,
- ProgramStateRef State, ExplodedNode *Pred,
- bool MarkAsSink) const {
+ const ProgramState *State,
+ ExplodedNode *Pred, bool MarkAsSink) const {
bool IsPO = State->isPosteriorlyOverconstrained();
bool IsNew;
@@ -614,7 +614,7 @@ void CoreEngine::enqueueStmtNode(ExplodedNode *N,
return;
}
- ExplodedNode *Succ = makeNode(Loc, N->getState(), N);
+ ExplodedNode *Succ = makeNode(Loc, N->getState().get(), N);
if (Succ)
WList->enqueue(Succ, Block, Idx+1);
@@ -662,7 +662,7 @@ void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set, const ReturnStmt *RS
if (SF->getParent()) {
// Use the callee stack frame.
CallExitBegin Loc(SF, RS);
- if (ExplodedNode *Succ = makeNode(Loc, Node->getState(), Node))
+ if (ExplodedNode *Succ = makeNode(Loc, Node->getState().get(), Node))
WList->enqueue(Succ);
} else {
// TODO: We should run remove dead bindings here.
diff --git a/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp b/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
index a73593baaa184..50b341c13a4cc 100644
--- a/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
@@ -108,8 +108,8 @@ bool ExplodedGraph::shouldCollect(const ExplodedNode *node) {
return false;
// Conditions 5, 6, and 7.
- ProgramStateRef state = node->getState();
- ProgramStateRef pred_state = pred->getState();
+ const ProgramState *state = node->getState().get();
+ const ProgramState *pred_state = pred->getState().get();
if (state->store != pred_state->store || state->GDM != pred_state->GDM ||
progPoint.getStackFrame() != pred->getStackFrame())
return false;
@@ -388,9 +388,8 @@ const Stmt *ExplodedNode::getCurrentOrPreviousStmtForDiagnostics() const {
}
ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L,
- ProgramStateRef State,
- bool IsSink,
- bool* IsNew) {
+ const ProgramState *State, bool IsSink,
+ bool *IsNew) {
// Profile 'State' to determine if we already have an existing node.
llvm::FoldingSetNodeID profile;
llvm::FoldingSetInsertToken InsertToken;
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
index 87ff7efcb389b..c59297fbeb149 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -1091,7 +1091,7 @@ void ExprEngine::removeDead(ExplodedNode *Pred, ExplodedNodeSet &Out,
StateMgr.getPersistentStateWithGDM(CleanedState, CheckerState);
const ProgramPoint &L = ProgramPoint::getProgramPoint(
DiagnosticStmt, K, I->getStackFrame(), cleanupNodeTag());
- Out.insert(Engine.makeNode(L, CleanedCheckerSt, I));
+ Out.insert(Engine.makeNode(L, CleanedCheckerSt.get(), I));
}
}
@@ -1317,7 +1317,7 @@ void ExprEngine::ProcessLoopExit(const Stmt* S, ExplodedNode *Pred) {
NewState = processLoopEnd(S, NewState);
LoopExit PP(S, Pred->getStackFrame());
- if (ExplodedNode *N = Engine.makeNode(PP, NewState, Pred))
+ if (ExplodedNode *N = Engine.makeNode(PP, NewState.get(), Pred))
Engine.enqueueStmtNode(N, getCurrBlock(), currStmtIdx);
}
@@ -1326,7 +1326,7 @@ void ExprEngine::ProcessLifetimeEnd(const Stmt *S, const VarDecl *D,
PrettyStackTraceStmt CrashInfo(getContext().getSourceManager(), S,
"Error evaluating end of a lifetime");
LifetimeEnd PP(S, D, Pred->getStackFrame());
- ExplodedNode *Src = Engine.makeNode(PP, Pred->getState(), Pred);
+ ExplodedNode *Src = Engine.makeNode(PP, Pred->getState().get(), Pred);
ExplodedNodeSet Dst;
getCheckerManager().runCheckersForLifetimeEnd(Dst, Src, D, *this);
@@ -1361,7 +1361,7 @@ void ExprEngine::ProcessInitializer(const CFGInitializer CFGInit,
// But we still need to stop tracking the object under construction.
State = finishObjectConstruction(State, BMI, SF);
PostStore PS(Init, SF, /*Loc*/ nullptr, /*tag*/ nullptr);
- Tmp.insert(Engine.makeNode(PS, State, Pred));
+ Tmp.insert(Engine.makeNode(PS, State.get(), Pred));
} else {
const ValueDecl *Field;
if (BMI->isIndirectMemberInitializer()) {
@@ -1415,7 +1415,7 @@ void ExprEngine::ProcessInitializer(const CFGInitializer CFGInit,
ExplodedNodeSet Dst;
for (ExplodedNode *Pred : Tmp)
- Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
+ Dst.insert(Engine.makeNode(PP, Pred->getState().get(), Pred));
// Enqueue the new nodes onto the work list.
Engine.enqueueStmtNodes(Dst, getCurrBlock(), currStmtIdx);
}
@@ -1498,7 +1498,7 @@ void ExprEngine::ProcessNewAllocator(const CXXNewExpr *NE,
const StackFrame *SF = Pred->getStackFrame();
PostImplicitCall PP(NE->getOperatorNew(), NE->getBeginLoc(), SF,
getCFGElementRef());
- Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
+ Dst.insert(Engine.makeNode(PP, Pred->getState().get(), Pred));
}
Engine.enqueueStmtNodes(Dst, getCurrBlock(), currStmtIdx);
}
@@ -1547,7 +1547,7 @@ void ExprEngine::ProcessAutomaticObjDtor(const CFGAutomaticObjDtor Dtor,
"which shouldn't be in the CFG.");
PostImplicitCall PP(DtorDecl, varDecl->getLocation(), SF,
getCFGElementRef(), &PT);
- Engine.makeNode(PP, Pred->getState(), Pred, /*MarkAsSink=*/true);
+ Engine.makeNode(PP, Pred->getState().get(), Pred, /*MarkAsSink=*/true);
return;
}
}
@@ -1562,7 +1562,7 @@ void ExprEngine::ProcessAutomaticObjDtor(const CFGAutomaticObjDtor Dtor,
"Prepare for object destruction");
PreImplicitCall PP(DtorDecl, varDecl->getLocation(), SF, getCFGElementRef(),
&PT);
- Pred = Engine.makeNode(PP, state, Pred);
+ Pred = Engine.makeNode(PP, state.get(), Pred);
if (!Pred)
return;
@@ -1589,7 +1589,7 @@ void ExprEngine::ProcessDeleteDtor(const CFGDeleteDtor Dtor,
const CXXDestructorDecl *Dtor = RD->getDestructor();
PostImplicitCall PP(Dtor, DE->getBeginLoc(), SF, getCFGElementRef());
- Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
+ Dst.insert(Engine.makeNode(PP, Pred->getState().get(), Pred));
return;
}
@@ -1622,7 +1622,7 @@ void ExprEngine::ProcessDeleteDtor(const CFGDeleteDtor Dtor,
"ExprEngine", "Skipping 0 length array delete destruction");
PostImplicitCall PP(getDtorDecl(DTy), DE->getBeginLoc(), SF,
getCFGElementRef(), &PT);
- Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
+ Dst.insert(Engine.makeNode(PP, Pred->getState().get(), Pred));
return;
}
@@ -1635,7 +1635,7 @@ void ExprEngine::ProcessDeleteDtor(const CFGDeleteDtor Dtor,
"Prepare for object destruction");
PreImplicitCall PP(getDtorDecl(DTy), DE->getBeginLoc(), SF,
getCFGElementRef(), &PT);
- Pred = Engine.makeNode(PP, State, Pred);
+ Pred = Engine.makeNode(PP, State.get(), Pred);
if (!Pred)
return;
@@ -1693,7 +1693,7 @@ void ExprEngine::ProcessMemberDtor(const CFGMemberDtor D,
"shouldn't be in the CFG.");
PostImplicitCall PP(DtorDecl, Member->getLocation(), SF,
getCFGElementRef(), &PT);
- Engine.makeNode(PP, Pred->getState(), Pred, /*MarkAsSink=*/true);
+ Engine.makeNode(PP, Pred->getState().get(), Pred, /*MarkAsSink=*/true);
return;
}
}
@@ -1707,7 +1707,7 @@ void ExprEngine::ProcessMemberDtor(const CFGMemberDtor D,
"Prepare for object destruction");
PreImplicitCall PP(DtorDecl, Member->getLocation(), SF, getCFGElementRef(),
&PT);
- Pred = Engine.makeNode(PP, State, Pred);
+ Pred = Engine.makeNode(PP, State.get(), Pred);
if (!Pred)
return;
@@ -1738,11 +1738,11 @@ void ExprEngine::ProcessTemporaryDtor(const CFGTemporaryDtor D,
State = cleanupElidedDestructor(State, BTE, SF);
PostImplicitCall PP(D.getDestructorDecl(getContext()), BTE->getBeginLoc(),
SF, getCFGElementRef());
- Dst.insert(Engine.makeNode(PP, State, Pred));
+ Dst.insert(Engine.makeNode(PP, State.get(), Pred));
return;
}
- ExplodedNode *CleanPred = Engine.makePostStmtNode(BTE, State, Pred);
+ ExplodedNode *CleanPred = Engine.makePostStmtNode(BTE, State.get(), Pred);
if (!CleanPred) {
// FIXME: We can get a null node here due to temporaries being
// bound to default parameters.
@@ -1797,7 +1797,7 @@ void ExprEngine::processCleanupTemporaryBranch(const CXXBindTemporaryExpr *BTE,
std::optional<SVal> Obj = getObjectUnderConstruction(State, BTE, SF);
if (const CFGBlock *DstBlock = Obj ? DstT : DstF) {
BlockEdge BE(getCurrBlock(), DstBlock, SF);
- Dst.insert(Engine.makeNode(BE, State, Pred));
+ Dst.insert(Engine.makeNode(BE, State.get(), Pred));
}
}
@@ -1822,7 +1822,7 @@ void ExprEngine::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *BTE,
// temporary destructor nodes.
State = addObjectUnderConstruction(State, BTE, SF, UnknownVal());
}
- Dst.insert(Engine.makePostStmtNode(BTE, State, Pred));
+ Dst.insert(Engine.makePostStmtNode(BTE, State.get(), Pred));
}
ProgramStateRef ExprEngine::escapeValues(ProgramStateRef State,
@@ -1995,7 +1995,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
case Stmt::OMPMetaDirectiveClass:
case Stmt::HLSLOutArgExprClass: {
const ExplodedNode *Node = Engine.makePostStmtNode(
- S, Pred->getState(), Pred, /*MarkAsSink=*/true);
+ S, Pred->getState().get(), Pred, /*MarkAsSink=*/true);
Engine.addAbortedBlock(Node, getCurrBlock());
break;
}
@@ -2151,7 +2151,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
if (IsTemporary)
State = createTemporaryRegionIfNeeded(State, SF, cast<Expr>(S),
cast<Expr>(S));
- Dst.insert(Engine.makePostStmtNode(S, State, Pred));
+ Dst.insert(Engine.makePostStmtNode(S, State.get(), Pred));
break;
}
@@ -2181,7 +2181,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
State = escapeValues(State, Val, PSK_EscapeOther);
}
- Dst.insert(Engine.makePostStmtNode(S, State, Pred));
+ Dst.insert(Engine.makePostStmtNode(S, State.get(), Pred));
break;
}
@@ -2347,7 +2347,8 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
case Stmt::CXXThrowExprClass:
// FIXME: This is not complete. We basically treat @throw as
// an abort.
- Engine.makePostStmtNode(S, Pred->getState(), Pred, /*MarkAsSink=*/true);
+ Engine.makePostStmtNode(S, Pred->getState().get(), Pred,
+ /*MarkAsSink=*/true);
break;
case Stmt::ReturnStmtClass:
@@ -2445,7 +2446,8 @@ bool ExprEngine::replayWithoutInlining(ExplodedNode *N,
// Make the new node a successor of BeforeProcessingCall.
bool IsNew = false;
- ExplodedNode *NewNode = G.getNode(NewNodeLoc, NewNodeState, false, &IsNew);
+ ExplodedNode *NewNode =
+ G.getNode(NewNodeLoc, NewNodeState.get(), false, &IsNew);
// We cached out at this point. Caching out is common due to us backtracking
// from the inlined function, which might spawn several paths.
// NOTE: We must return before the `addPredecessor()` call, otherwise the
@@ -2478,7 +2480,7 @@ ExplodedNode *ExprEngine::processCFGBlockEntrance(const BlockEntrance &BE,
State = updateLoopStack(Term, AMgr.getASTContext(), Pred, MaxBlockVisit);
// Is we are inside an unrolled loop then no need the check the counters.
if (isUnrolledState(State))
- return Engine.makeNode(BE, State, Pred);
+ return Engine.makeNode(BE, State.get(), Pred);
}
// If this block is terminated by a loop and it has already been visited the
@@ -2486,7 +2488,7 @@ ExplodedNode *ExprEngine::processCFGBlockEntrance(const BlockEntrance &BE,
unsigned int BlockCount = getNumVisitedCurrent();
if (BlockCount == MaxBlockVisit - 1 && AMgr.options.ShouldWidenLoops) {
if (!isa_and_nonnull<ForStmt, WhileStmt, DoStmt, CXXForRangeStmt>(Term))
- return Engine.makeNode(BE, State, Pred);
+ return Engine.makeNode(BE, State.get(), Pred);
// FIXME:
// We cannot use the CFG element from the via `ExprEngine::getCFGElementRef`
@@ -2496,17 +2498,17 @@ ExplodedNode *ExprEngine::processCFGBlockEntrance(const BlockEntrance &BE,
// Here we just pass the the first CFG element in the block.
ProgramStateRef WidenedState = getWidenedLoopState(
State, SF, BlockCount, *getCurrBlock()->ref_begin());
- return Engine.makeNode(BE, WidenedState, Pred);
+ return Engine.makeNode(BE, WidenedState.get(), Pred);
}
// If we did not reach MaxBlockVisitOnPath, continue the analysis normally.
if (BlockCount < MaxBlockVisit)
- return Engine.makeNode(BE, State, Pred);
+ return Engine.makeNode(BE, State.get(), Pred);
// ... otherwise, discard this execution path.
static SimpleProgramPointTag Tag(TagProviderName, "Block count exceeded");
const ExplodedNode *Sink =
- Engine.makeNode(BE.withTag(&Tag), State, Pred, /*MarkAsSink=*/true);
+ Engine.makeNode(BE.withTag(&Tag), State.get(), Pred, /*MarkAsSink=*/true);
if (!SF->inTopFrame()) {
// FIXME: This will unconditionally prevent inlining this function (even
@@ -2757,7 +2759,7 @@ void ExprEngine::processBranch(
return;
}
BlockEdge BE(getCurrBlock(), DstT, SF);
- Dst.insert(Engine.makeNode(BE, Pred->getState(), Pred));
+ Dst.insert(Engine.makeNode(BE, Pred->getState().get(), Pred));
return;
}
@@ -2822,7 +2824,7 @@ void ExprEngine::processBranch(
if (!SkipTrueBranch || AMgr.options.ShouldWidenLoops) {
if (DstT) {
BlockEdge BE(getCurrBlock(), DstT, SF);
- Dst.insert(Engine.makeNode(BE, StTrue, PredN));
+ Dst.insert(Engine.makeNode(BE, StTrue.get(), PredN));
}
} else if (!AMgr.options.InlineFunctionsWithAmbiguousLoops) {
// FIXME: There is an ancient and arbitrary heuristic in
@@ -2864,7 +2866,7 @@ void ExprEngine::processBranch(
AMgr.options.ShouldAssumeAtLeastOneIteration;
if (!SkipFalseBranch && DstF) {
BlockEdge BE(getCurrBlock(), DstF, SF);
- Dst.insert(Engine.makeNode(BE, StFalse, PredN));
+ Dst.insert(Engine.makeNode(BE, StFalse.get(), PredN));
}
}
}
@@ -2888,7 +2890,7 @@ void ExprEngine::processStaticInitializer(const DeclStmt *DS,
if (const CFGBlock *DstBlock = InitHasRun ? DstT : DstF) {
BlockEdge BE(getCurrBlock(), DstBlock, Pred->getStackFrame());
- Dst.insert(Engine.makeNode(BE, State, Pred));
+ Dst.insert(Engine.makeNode(BE, State.get(), Pred));
}
}
@@ -2920,7 +2922,7 @@ void ExprEngine::processIndirectGoto(ExplodedNodeSet &Dst, const Expr *Tgt,
// FIXME: If 'V' was a symbolic value, then record that on this execution
// path it is equal to the address of the label leading to 'Succ'.
BlockEdge BE(getCurrBlock(), Succ, Pred->getStackFrame());
- Dst.insert(Engine.makeNode(BE, State, Pred));
+ Dst.insert(Engine.makeNode(BE, State.get(), Pred));
}
}
}
@@ -2972,7 +2974,7 @@ void ExprEngine::processEndOfFunction(ExplodedNode *Pred,
// Perform the transition with cleanups.
if (State != Pred->getState()) {
- Pred = Engine.makeNode(Pred->getLocation(), State, Pred);
+ Pred = Engine.makeNode(Pred->getLocation(), State.get(), Pred);
if (!Pred) {
// The node with clean temporaries already exists. We might have reached
// it on a path on which we initialize
diff erent temporaries.
@@ -3066,7 +3068,7 @@ void ExprEngine::processSwitch(const SwitchStmt *Switch, ExplodedNode *Pred,
if (StateMatching) {
BlockEdge BE(SwitchBlock, CaseBlock, SF);
- Dst.insert(Engine.makeNode(BE, StateMatching, Node));
+ Dst.insert(Engine.makeNode(BE, StateMatching.get(), Node));
}
// If _not_ entering the current case is infeasible, then we are done
@@ -3094,7 +3096,7 @@ void ExprEngine::processSwitch(const SwitchStmt *Switch, ExplodedNode *Pred,
}
BlockEdge BE(SwitchBlock, DefaultBlock, SF);
- Dst.insert(Engine.makeNode(BE, State, Node));
+ Dst.insert(Engine.makeNode(BE, State.get(), Node));
}
}
@@ -3408,7 +3410,7 @@ void ExprEngine::VisitArraySubscriptExpr(const ArraySubscriptExpr *A,
Engine.makeNodeWithBinding(Pred, A, V, ProgramPoint::PostLValueKind));
} else if (IsVectorType) {
// FIXME: non-glvalue vector reads are not modelled.
- Dst.insert(Engine.makePostStmtNode(A, state, Pred));
+ Dst.insert(Engine.makePostStmtNode(A, state.get(), Pred));
} else {
llvm_unreachable("Array subscript should be an lValue when not \
a vector and not a forbidden lvalue type");
@@ -3643,7 +3645,7 @@ void ExprEngine::evalBind(ExplodedNodeSet &Dst, const Stmt *StoreE,
}
PostStore PS(StoreE, SF, Location.getAsRegion(), /*tag=*/nullptr);
- Dst.insert(Engine.makeNode(PS, State, PredI));
+ Dst.insert(Engine.makeNode(PS, State.get(), PredI));
}
}
@@ -3713,7 +3715,7 @@ void ExprEngine::evalLoad(ExplodedNodeSet &Dst,
const auto *SF = I->getStackFrame();
PostLoad Loc(NodeEx, SF, tag);
- Dst.insert(Engine.makeNode(Loc, state->BindExpr(BoundEx, SF, V), I));
+ Dst.insert(Engine.makeNode(Loc, state->BindExpr(BoundEx, SF, V).get(), I));
}
}
@@ -3742,7 +3744,7 @@ void ExprEngine::evalLocation(ExplodedNodeSet &Dst, const Stmt *NodeEx,
static SimpleProgramPointTag tag(TagProviderName, "Location");
PostStmt Loc(NodeEx, Pred->getStackFrame(), &tag);
- Src.insert(Engine.makeNode(Loc, state, Pred));
+ Src.insert(Engine.makeNode(Loc, state.get(), Pred));
}
ExplodedNodeSet Tmp;
@@ -3798,7 +3800,7 @@ void ExprEngine::evalEagerlyAssumeBifurcation(ExplodedNodeSet &Dst,
SVal Val = svalBuilder.makeIntVal(1U, Ex->getType());
StateTrue = StateTrue->BindExpr(Ex, SF, Val);
PostStmt PostStmtTrue(Ex, SF, TrueTag);
- Dst.insert(Engine.makeNode(PostStmtTrue, StateTrue, Pred));
+ Dst.insert(Engine.makeNode(PostStmtTrue, StateTrue.get(), Pred));
}
// Next, assume that the condition is false.
@@ -3806,7 +3808,7 @@ void ExprEngine::evalEagerlyAssumeBifurcation(ExplodedNodeSet &Dst,
SVal Val = svalBuilder.makeIntVal(0U, Ex->getType());
StateFalse = StateFalse->BindExpr(Ex, SF, Val);
PostStmt PostStmtFalse(Ex, SF, FalseTag);
- Dst.insert(Engine.makeNode(PostStmtFalse, StateFalse, Pred));
+ Dst.insert(Engine.makeNode(PostStmtFalse, StateFalse.get(), Pred));
}
} else {
Dst.insert(Pred);
@@ -3852,12 +3854,12 @@ void ExprEngine::VisitGCCAsmStmt(const GCCAsmStmt *A, ExplodedNode *Pred,
/*CausedByPointerEscape=*/true);
}
- Dst.insert(Engine.makePostStmtNode(A, state, Pred));
+ Dst.insert(Engine.makePostStmtNode(A, state.get(), Pred));
}
void ExprEngine::VisitMSAsmStmt(const MSAsmStmt *A, ExplodedNode *Pred,
ExplodedNodeSet &Dst) {
- Dst.insert(Engine.makePostStmtNode(A, Pred->getState(), Pred));
+ Dst.insert(Engine.makePostStmtNode(A, Pred->getState().get(), Pred));
}
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
index d89c80ca01754..d39c3fbf1773b 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
@@ -88,7 +88,7 @@ void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
State = escapeValues(State, RightV, PSK_EscapeOther);
}
- Tmp2.insert(Engine.makePostStmtNode(B, State, N));
+ Tmp2.insert(Engine.makePostStmtNode(B, State.get(), N));
continue;
}
@@ -241,7 +241,7 @@ void ExprEngine::handleLValueBitCast(ProgramStateRef state, const Expr *Ex,
if (V.isUnknown() && !OrigV.isUnknown()) {
state = escapeValues(state, OrigV, PSK_EscapeOther);
}
- Dst.insert(Engine.makePostStmtNode(CastE, state, Pred));
+ Dst.insert(Engine.makePostStmtNode(CastE, state.get(), Pred));
}
void ExprEngine::VisitCastExpr(const CastExpr *CastE, ExplodedNode *Pred,
@@ -420,7 +420,7 @@ void ExprEngine::VisitCastExpr(const CastExpr *CastE, ExplodedNode *Pred,
if (T->isReferenceType()) {
// A bad_cast exception is thrown if input value is a reference.
// Currently, we model this, by generating a sink.
- Engine.makePostStmtNode(CastE, State, Pred, /*MarkAsSink=*/true);
+ Engine.makePostStmtNode(CastE, State.get(), Pred, /*MarkAsSink=*/true);
return;
} else {
// If the cast fails on a pointer, bind to 0.
@@ -438,7 +438,7 @@ void ExprEngine::VisitCastExpr(const CastExpr *CastE, ExplodedNode *Pred,
// Else, bind to the derived region value.
State = State->BindExpr(CastE, SF, val);
}
- Dst.insert(Engine.makePostStmtNode(CastE, State, Pred));
+ Dst.insert(Engine.makePostStmtNode(CastE, State.get(), Pred));
return;
}
case CK_BaseToDerived: {
@@ -595,7 +595,7 @@ void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
state = finishObjectConstruction(state, DS, SF);
// We constructed the object directly in the variable.
// No need to bind anything.
- dstEvaluated.insert(Engine.makePostStmtNode(DS, state, UpdatedN));
+ dstEvaluated.insert(Engine.makePostStmtNode(DS, state.get(), UpdatedN));
} else {
// Recover some path-sensitivity if a scalar value evaluated to
// UnknownVal.
@@ -615,7 +615,7 @@ void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
}
}
else {
- dstEvaluated.insert(Engine.makePostStmtNode(DS, state, N));
+ dstEvaluated.insert(Engine.makePostStmtNode(DS, state.get(), N));
}
}
@@ -650,7 +650,7 @@ void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
// logical operators on vectors are not short-circuit. Currently they are
// modeled as short-circuit in Clang CFG but this is incorrect.
// Do not set the value for the expression. It'd be UnknownVal by default.
- Dst.insert(Engine.makePostStmtNode(B, state, Pred));
+ Dst.insert(Engine.makePostStmtNode(B, state.get(), Pred));
return;
}
@@ -662,7 +662,7 @@ void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
(void) P;
if (N->pred_size() != 1) {
// We failed to track back where we came from.
- Dst.insert(Engine.makePostStmtNode(B, state, Pred));
+ Dst.insert(Engine.makePostStmtNode(B, state.get(), Pred));
return;
}
N = *N->pred_begin();
@@ -670,7 +670,7 @@ void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
if (N->pred_size() != 1) {
// We failed to track back where we came from.
- Dst.insert(Engine.makePostStmtNode(B, state, Pred));
+ Dst.insert(Engine.makePostStmtNode(B, state.get(), Pred));
return;
}
@@ -963,7 +963,7 @@ void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, ExplodedNode *Pred,
state = state->BindExpr(U, SF, Result);
break;
}
- EvalSet.insert(Engine.makePostStmtNode(U, state, N));
+ EvalSet.insert(Engine.makePostStmtNode(U, state.get(), N));
break;
}
}
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
index 609b3882e3867..0817489fd2527 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
@@ -38,7 +38,7 @@ void ExprEngine::VisitMaterializeTemporaryExpr(
const StackFrame *SF = Pred->getStackFrame();
State = createTemporaryRegionIfNeeded(State, SF, TempExpr, MTE);
- Dst.insert(Engine.makePostStmtNode(MTE, State, Pred));
+ Dst.insert(Engine.makePostStmtNode(MTE, State.get(), Pred));
}
void ExprEngine::performTrivialCopy(ExplodedNodeSet &Dst, ExplodedNode *Pred,
@@ -98,7 +98,7 @@ void ExprEngine::performTrivialCopy(ExplodedNodeSet &Dst, ExplodedNode *Pred,
State = State->BindExpr(CallExpr, SF, ThisVal);
else
State = bindReturnValue(Call, SF, State);
- Dst.insert(Engine.makePostStmtNode(CallExpr, State, N));
+ Dst.insert(Engine.makePostStmtNode(CallExpr, State.get(), N));
}
}
@@ -539,7 +539,7 @@ void ExprEngine::handleConstructor(const Expr *E, ExplodedNode *Pred,
State = finishObjectConstruction(State, CE, SF);
if (auto L = Target.getAs<Loc>())
State = State->BindExpr(CE, SF, State->getSVal(*L, CE->getType()));
- Dst.insert(Engine.makePostStmtNode(CE, State, Pred));
+ Dst.insert(Engine.makePostStmtNode(CE, State.get(), Pred));
return;
}
}
@@ -579,7 +579,7 @@ void ExprEngine::handleConstructor(const Expr *E, ExplodedNode *Pred,
static SimpleProgramPointTag T{"ExprEngine",
"Skipping 0 size array construction"};
PostStmt Loc(CE, Pred->getStackFrame(), &T);
- Dst.insert(Engine.makeNode(Loc, State, Pred));
+ Dst.insert(Engine.makeNode(Loc, State.get(), Pred));
return;
}
@@ -657,7 +657,7 @@ void ExprEngine::handleConstructor(const Expr *E, ExplodedNode *Pred,
if (State != Pred->getState()) {
static SimpleProgramPointTag T("ExprEngine",
"Prepare for object construction");
- Pred = Engine.makeNode(PreStmt(E, SF, &T), State, Pred);
+ Pred = Engine.makeNode(PreStmt(E, SF, &T), State.get(), Pred);
if (!Pred)
return;
}
@@ -699,7 +699,7 @@ void ExprEngine::handleConstructor(const Expr *E, ExplodedNode *Pred,
}
PreStmt P(CE, N->getStackFrame(), /*tag=*/nullptr);
- PreInitialized.insert(Engine.makeNode(P, State, N));
+ PreInitialized.insert(Engine.makeNode(P, State.get(), N));
}
} else {
PreInitialized = DstPreVisit;
@@ -751,7 +751,7 @@ void ExprEngine::handleConstructor(const Expr *E, ExplodedNode *Pred,
"We should not have inlined this constructor!");
for (ExplodedNode *N : DstEvaluated) {
- Engine.makePostStmtNode(E, N->getState(), N, /*MarkAsSink=*/true);
+ Engine.makePostStmtNode(E, N->getState().get(), N, /*MarkAsSink=*/true);
}
// There is no need to run the PostCall and PostStmt checker
@@ -810,7 +810,7 @@ void ExprEngine::VisitCXXDestructor(QualType ObjectType,
// FIXME: PostImplicitCall with a null decl may crash elsewhere anyway.
PostImplicitCall PP(/*Decl=*/nullptr, S->getEndLoc(), SF,
getCFGElementRef(), &T);
- Dst.insert(Engine.makeNode(PP, Pred->getState(), Pred));
+ Dst.insert(Engine.makeNode(PP, Pred->getState().get(), Pred));
return;
}
@@ -825,7 +825,8 @@ void ExprEngine::VisitCXXDestructor(QualType ObjectType,
Dest = MRMgr.getCXXTempObjectRegion(E, Pred->getStackFrame());
} else {
static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
- Engine.makeNode(Pred->getLocation().withTag(&T), Pred->getState(), Pred,
+ Engine.makeNode(Pred->getLocation().withTag(&T), Pred->getState().get(),
+ Pred,
/*MarkAsSink=*/true);
return;
}
@@ -917,7 +918,7 @@ void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
}
DstPostValue.insert(Engine.makePostStmtNode(
- CNE, addObjectUnderConstruction(State, CNE, SF, RetVal), I));
+ CNE, addObjectUnderConstruction(State, CNE, SF, RetVal).get(), I));
}
ExplodedNodeSet DstPostPostCallCallback;
@@ -1011,7 +1012,7 @@ void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
// If the array is list initialized, we bind the initializer list to the
// memory region here, otherwise we would lose it.
if (isInitList) {
- Pred = Engine.makePostStmtNode(CNE, State, Pred);
+ Pred = Engine.makePostStmtNode(CNE, State.get(), Pred);
SVal V = State->getSVal(Init, SF);
ExplodedNodeSet Evaluated;
@@ -1095,7 +1096,7 @@ void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred,
ProgramStateRef state = Pred->getState();
state = state->bindLoc(state->getLValue(VD, SF), V, SF);
- Dst.insert(Engine.makePostStmtNode(CS, state, Pred));
+ Dst.insert(Engine.makePostStmtNode(CS, state.get(), Pred));
}
void ExprEngine::VisitCXXParenListInitExpr(const CXXParenListInitExpr *E,
@@ -1121,7 +1122,7 @@ void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
if (!AMgr.options.ShouldInlineLambdas) {
const ExplodedNode *Node = Engine.makePostStmtNode(
- LE, Pred->getState(), Pred, /*MarkAsSink=*/true);
+ LE, Pred->getState().get(), Pred, /*MarkAsSink=*/true);
Engine.addAbortedBlock(Node, getCurrBlock());
return;
}
@@ -1212,5 +1213,5 @@ void ExprEngine::VisitAttributedStmt(const AttributedStmt *A,
}
if (State)
- Dst.insert(Engine.makePostStmtNode(A, State, Pred));
+ Dst.insert(Engine.makePostStmtNode(A, State.get(), Pred));
}
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp
index 86facd6c838b3..2fa7cfdcab3f4 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp
@@ -55,7 +55,7 @@ void ExprEngine::processCallEnter(CallEnter CE, ExplodedNode *Pred) {
// Construct a new node, notify checkers that analysis of the function has
// begun, and add the resultant nodes to the worklist.
- ExplodedNode *Node = Engine.makeNode(Loc, Pred->getState(), Pred);
+ ExplodedNode *Node = Engine.makeNode(Loc, Pred->getState().get(), Pred);
if (Node) {
// FIXME: In the `processBeginOfFunction` callback
// `ExprEngine::getCurrStackFrame()` can be
diff erent from the
@@ -353,7 +353,7 @@ void ExprEngine::processCallExit(ExplodedNode *CEBNode) {
: ProgramPoint{EpsilonPoint(CalleeSF, /*Data1=*/nullptr,
/*Data2=*/nullptr, &RetValBind)};
- ExplodedNode *BoundRetNode = Engine.makeNode(Loc, State, CEBNode);
+ ExplodedNode *BoundRetNode = Engine.makeNode(Loc, State.get(), CEBNode);
if (!BoundRetNode)
return;
@@ -378,7 +378,7 @@ void ExprEngine::processCallExit(ExplodedNode *CEBNode) {
CallExitEnd Loc(CalleeSF, CallerSF);
ProgramStateRef CEEState = (N == CEBNode) ? State : N->getState();
- ExplodedNode *CEENode = Engine.makeNode(Loc, CEEState, N);
+ ExplodedNode *CEENode = Engine.makeNode(Loc, CEEState.get(), N);
if (!CEENode)
continue;
@@ -541,7 +541,7 @@ void ExprEngine::inlineCall(WorkList *WList, const CallEvent &Call,
// formal arguments.
State = State->enterStackFrame(Call, CalleeSF);
- if (ExplodedNode *N = Engine.makeNode(Loc, State, Pred))
+ if (ExplodedNode *N = Engine.makeNode(Loc, State.get(), Pred))
WList->enqueue(N);
NumInlinedCalls++;
@@ -587,7 +587,7 @@ void ExprEngine::VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred,
createTemporaryRegionIfNeeded(State, SF, OCE->getArg(0));
if (NewState != State) {
PreStmt PS(OCE, SF, /*tag=*/nullptr);
- Pred = Engine.makeNode(PS, NewState, Pred);
+ Pred = Engine.makeNode(PS, NewState.get(), Pred);
if (!Pred)
return; // Cached out.
}
@@ -661,7 +661,7 @@ void ExprEngine::finishArgumentConstruction(ExplodedNodeSet &Dst,
const StackFrame *SF = Call.getStackFrame();
static SimpleProgramPointTag Tag("ExprEngine",
"Finish argument construction");
- Dst.insert(Engine.makeNode(PreStmt(E, SF, &Tag), CleanedState, Pred));
+ Dst.insert(Engine.makeNode(PreStmt(E, SF, &Tag), CleanedState.get(), Pred));
}
void ExprEngine::evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred,
@@ -724,7 +724,7 @@ void ExprEngine::evalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred,
PSK_EscapeOutParameters, &*Call);
if (State != I->getState())
- I = Engine.makeNode(I->getLocation(), State, I);
+ I = Engine.makeNode(I->getLocation(), State.get(), I);
Dst.insert(I);
}
@@ -829,7 +829,7 @@ ExplodedNode *ExprEngine::conservativeEvalCall(const CallEvent &Call,
// And make the result node.
static SimpleProgramPointTag PT("ExprEngine", "Conservative eval call");
- return Engine.makeNode(Call.getProgramPoint(false, &PT), State, Pred);
+ return Engine.makeNode(Call.getProgramPoint(false, &PT), State.get(), Pred);
}
ExprEngine::CallInlinePolicy
@@ -1297,7 +1297,7 @@ void ExprEngine::dynDispatchBifurcate(const MemRegion *BifurReg,
void ExprEngine::VisitReturnStmt(const ReturnStmt *RS, ExplodedNode *Pred,
ExplodedNodeSet &Dst) {
if (RS->getRetValue()) {
- Dst.insert(Engine.makePostStmtNode(RS, Pred->getState(), Pred));
+ Dst.insert(Engine.makePostStmtNode(RS, Pred->getState().get(), Pred));
} else {
Dst.insert(Pred);
}
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineObjC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineObjC.cpp
index ada445815b1d5..3012f514a1bab 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineObjC.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineObjC.cpp
@@ -58,7 +58,7 @@ void ExprEngine::populateObjCForDestinationSet(const ObjCForCollectionStmt *S,
State = State->bindLoc(ElementV, V, SF);
}
- Dst.insert(Engine.makePostStmtNode(S, State, Pred));
+ Dst.insert(Engine.makePostStmtNode(S, State.get(), Pred));
}
void ExprEngine::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S,
@@ -184,7 +184,7 @@ void ExprEngine::VisitObjCMessage(const ObjCMessageExpr *ME,
// Receiver is definitely nil, so run ObjCMessageNil callbacks and return.
if (nilState && !notNilState) {
PreStmt PS(ME, Pred->getStackFrame(), nullptr);
- Pred = Engine.makeNode(PS, nilState, Pred);
+ Pred = Engine.makeNode(PS, nilState.get(), Pred);
if (!Pred)
return;
@@ -199,7 +199,7 @@ void ExprEngine::VisitObjCMessage(const ObjCMessageExpr *ME,
// Generate a transition to the non-nil state, dropping any potential
// nil flow.
if (notNilState != State) {
- Pred = Engine.makePostStmtNode(ME, notNilState, Pred);
+ Pred = Engine.makePostStmtNode(ME, notNilState.get(), Pred);
if (!Pred)
return;
}
@@ -226,7 +226,7 @@ void ExprEngine::VisitObjCMessage(const ObjCMessageExpr *ME,
UpdatedMsg->getReceiverSVal().isUndef())) {
// If we raise an exception, for now treat it as a sink.
// Eventually we will want to handle exceptions properly.
- Engine.makePostStmtNode(ME, State, Pred, /*MarkAsSink=*/true);
+ Engine.makePostStmtNode(ME, State.get(), Pred, /*MarkAsSink=*/true);
continue;
}
diff --git a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
index 2a4d2de0cf225..eaa623a3a9e12 100644
--- a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
@@ -23,25 +23,11 @@
using namespace clang;
using namespace ento;
-namespace clang { namespace ento {
-/// Increments the number of times this state is referenced.
-
-void ProgramStateRetain(const ProgramState *state) {
- ++const_cast<ProgramState*>(state)->refCount;
-}
-
-/// Decrement the number of times this state is referenced.
-void ProgramStateRelease(const ProgramState *state) {
- assert(state->refCount > 0);
- ProgramState *s = const_cast<ProgramState*>(state);
- if (--s->refCount == 0) {
- ProgramStateManager &Mgr = s->getStateManager();
- Mgr.StateSet.erase(s);
- s->~ProgramState();
- Mgr.freeStates.push_back(s);
- }
+void ProgramStateManager::freeState(ProgramState *State) {
+ StateSet.erase(State);
+ State->~ProgramState();
+ freeStates.push_back(State);
}
-}}
ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
StoreRef st, GenericDataMap gdm)
@@ -59,6 +45,13 @@ ProgramState::ProgramState(const ProgramState &RHS)
stateMgr->getStoreManager().incrementReferenceCount(store);
}
+ProgramState::ProgramState(ProgramState &&RHS)
+ : stateMgr(RHS.stateMgr), Env(std::move(RHS.Env)), store(RHS.store),
+ GDM(std::move(RHS.GDM)),
+ PosteriorlyOverconstrained(RHS.PosteriorlyOverconstrained), refCount(0) {
+ RHS.store = nullptr;
+}
+
ProgramState::~ProgramState() {
if (store)
stateMgr->getStoreManager().decrementReferenceCount(store);
@@ -104,9 +97,9 @@ ProgramStateRef ProgramStateManager::removeDeadBindingsFromEnvironmentAndStore(
StoreRef newStore =
StoreMgr->removeDeadBindings(NewState.getStore(), SF, SymReaper);
NewState.setStore(newStore);
- SymReaper.setReapedStore(newStore);
+ SymReaper.setReapedStore(std::move(newStore));
- return getPersistentState(NewState);
+ return getPersistentState(std::move(NewState));
}
ProgramStateRef ProgramState::bindLoc(Loc LV, SVal V, const StackFrame *SF,
@@ -301,7 +294,7 @@ ProgramStateRef ProgramState::BindExpr(const Expr *E, const StackFrame *SF,
ProgramState NewSt = *this;
NewSt.Env = std::move(NewEnv);
- return getStateManager().getPersistentState(NewSt);
+ return getStateManager().getPersistentState(std::move(NewSt));
}
[[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
@@ -388,7 +381,7 @@ ProgramStateRef ProgramStateManager::getInitialState(const StackFrame *InitSF) {
StoreMgr->getInitialStore(InitSF),
GDMFactory.getEmptyMap());
- return getPersistentState(State);
+ return getPersistentState(std::move(State));
}
ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
@@ -396,10 +389,10 @@ ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
ProgramStateRef GDMState) {
ProgramState NewState(*FromState);
NewState.GDM = GDMState->GDM;
- return getPersistentState(NewState);
+ return getPersistentState(std::move(NewState));
}
-ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
+ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &&State) {
llvm::FoldingSetNodeID ID;
State.Profile(ID);
@@ -416,7 +409,7 @@ ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
else {
newState = Alloc.Allocate<ProgramState>();
}
- new (newState) ProgramState(State);
+ new (newState) ProgramState(std::move(State));
StateSet.insert(newState, InsertToken);
return newState;
}
@@ -424,7 +417,7 @@ ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
ProgramState NewSt(*this);
NewSt.setStore(store);
- return getStateManager().getPersistentState(NewSt);
+ return getStateManager().getPersistentState(std::move(NewSt));
}
ProgramStateRef ProgramState::makeWithStore(const BindResult &BindRes) const {
@@ -442,7 +435,7 @@ ProgramStateRef ProgramState::makeWithStore(const BindResult &BindRes) const {
ProgramStateRef ProgramState::cloneAsPosteriorlyOverconstrained() const {
ProgramState NewSt(*this);
NewSt.PosteriorlyOverconstrained = true;
- return getStateManager().getPersistentState(NewSt);
+ return getStateManager().getPersistentState(std::move(NewSt));
}
void ProgramState::setStore(const StoreRef &newStore) {
@@ -553,8 +546,8 @@ ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, const void *Key,
return St;
ProgramState NewSt = *St;
- NewSt.GDM = M2;
- return getPersistentState(NewSt);
+ NewSt.GDM = std::move(M2);
+ return getPersistentState(std::move(NewSt));
}
ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state,
@@ -566,8 +559,8 @@ ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state,
return state;
ProgramState NewState = *state;
- NewState.GDM = NewM;
- return getPersistentState(NewState);
+ NewState.GDM = std::move(NewM);
+ return getPersistentState(std::move(NewState));
}
bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
diff --git a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
index 37516c12843d9..e2d6d7eb86f35 100644
--- a/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
+++ b/clang/lib/StaticAnalyzer/Core/RegionStore.cpp
@@ -694,14 +694,16 @@ class RegionStoreManager : public StoreManager {
StoreRef killBinding(Store ST, Loc L) override;
void incrementReferenceCount(Store store) override {
- getRegionBindings(store).manualRetain();
+ if (RegionBindings::TreeTy *T = getRegionBindingsTree(store))
+ T->retain();
}
/// If the StoreManager supports it, decrement the reference count of
/// the specified Store object. If the reference count hits 0, the memory
/// associated with the object is recycled.
void decrementReferenceCount(Store store) override {
- getRegionBindings(store).manualRelease();
+ if (RegionBindings::TreeTy *T = getRegionBindingsTree(store))
+ T->release();
}
bool includedInBindings(Store store, const MemRegion *region) const override;
@@ -810,6 +812,13 @@ class RegionStoreManager : public StoreManager {
RBFactory.getTreeFactory(), Ptr.getInt()};
}
+ static RegionBindings::TreeTy *getRegionBindingsTree(Store store) {
+ llvm::PointerIntPair<Store, 1, bool> Ptr;
+ Ptr.setFromOpaqueValue(const_cast<void *>(store));
+ return static_cast<RegionBindings::TreeTy *>(
+ const_cast<void *>(Ptr.getPointer()));
+ }
+
LimitedRegionBindingsRef
getRegionBindings(Store store,
SmallVectorImpl<SVal> &EscapedValuesDuringBind) const {
More information about the cfe-commits
mailing list