xref: /openbsd-src/gnu/llvm/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick //  This file defines the template classes ExplodedNode and ExplodedGraph,
10e5dd7070Spatrick //  which represent a path-sensitive, intra-procedural "exploded graph."
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick 
14e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
15e5dd7070Spatrick #include "clang/AST/Expr.h"
16e5dd7070Spatrick #include "clang/AST/ExprObjC.h"
17e5dd7070Spatrick #include "clang/AST/ParentMap.h"
18e5dd7070Spatrick #include "clang/AST/Stmt.h"
19e5dd7070Spatrick #include "clang/Analysis/CFGStmtMap.h"
20e5dd7070Spatrick #include "clang/Analysis/ProgramPoint.h"
21e5dd7070Spatrick #include "clang/Analysis/Support/BumpVector.h"
22e5dd7070Spatrick #include "clang/Basic/LLVM.h"
23e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
24e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
25e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
26e5dd7070Spatrick #include "llvm/ADT/DenseSet.h"
27e5dd7070Spatrick #include "llvm/ADT/FoldingSet.h"
28e5dd7070Spatrick #include "llvm/ADT/PointerUnion.h"
29e5dd7070Spatrick #include "llvm/ADT/SmallVector.h"
30e5dd7070Spatrick #include "llvm/Support/Casting.h"
31e5dd7070Spatrick #include <cassert>
32e5dd7070Spatrick #include <memory>
33*12c85518Srobert #include <optional>
34e5dd7070Spatrick 
35e5dd7070Spatrick using namespace clang;
36e5dd7070Spatrick using namespace ento;
37e5dd7070Spatrick 
38e5dd7070Spatrick //===----------------------------------------------------------------------===//
39e5dd7070Spatrick // Cleanup.
40e5dd7070Spatrick //===----------------------------------------------------------------------===//
41e5dd7070Spatrick 
42e5dd7070Spatrick ExplodedGraph::ExplodedGraph() = default;
43e5dd7070Spatrick 
44e5dd7070Spatrick ExplodedGraph::~ExplodedGraph() = default;
45e5dd7070Spatrick 
46e5dd7070Spatrick //===----------------------------------------------------------------------===//
47e5dd7070Spatrick // Node reclamation.
48e5dd7070Spatrick //===----------------------------------------------------------------------===//
49e5dd7070Spatrick 
isInterestingLValueExpr(const Expr * Ex)50e5dd7070Spatrick bool ExplodedGraph::isInterestingLValueExpr(const Expr *Ex) {
51e5dd7070Spatrick   if (!Ex->isLValue())
52e5dd7070Spatrick     return false;
53*12c85518Srobert   return isa<DeclRefExpr, MemberExpr, ObjCIvarRefExpr, ArraySubscriptExpr>(Ex);
54e5dd7070Spatrick }
55e5dd7070Spatrick 
shouldCollect(const ExplodedNode * node)56e5dd7070Spatrick bool ExplodedGraph::shouldCollect(const ExplodedNode *node) {
57e5dd7070Spatrick   // First, we only consider nodes for reclamation of the following
58e5dd7070Spatrick   // conditions apply:
59e5dd7070Spatrick   //
60e5dd7070Spatrick   // (1) 1 predecessor (that has one successor)
61e5dd7070Spatrick   // (2) 1 successor (that has one predecessor)
62e5dd7070Spatrick   //
63e5dd7070Spatrick   // If a node has no successor it is on the "frontier", while a node
64e5dd7070Spatrick   // with no predecessor is a root.
65e5dd7070Spatrick   //
66e5dd7070Spatrick   // After these prerequisites, we discard all "filler" nodes that
67e5dd7070Spatrick   // are used only for intermediate processing, and are not essential
68e5dd7070Spatrick   // for analyzer history:
69e5dd7070Spatrick   //
70e5dd7070Spatrick   // (a) PreStmtPurgeDeadSymbols
71e5dd7070Spatrick   //
72e5dd7070Spatrick   // We then discard all other nodes where *all* of the following conditions
73e5dd7070Spatrick   // apply:
74e5dd7070Spatrick   //
75e5dd7070Spatrick   // (3) The ProgramPoint is for a PostStmt, but not a PostStore.
76e5dd7070Spatrick   // (4) There is no 'tag' for the ProgramPoint.
77e5dd7070Spatrick   // (5) The 'store' is the same as the predecessor.
78e5dd7070Spatrick   // (6) The 'GDM' is the same as the predecessor.
79e5dd7070Spatrick   // (7) The LocationContext is the same as the predecessor.
80e5dd7070Spatrick   // (8) Expressions that are *not* lvalue expressions.
81e5dd7070Spatrick   // (9) The PostStmt isn't for a non-consumed Stmt or Expr.
82e5dd7070Spatrick   // (10) The successor is neither a CallExpr StmtPoint nor a CallEnter or
83e5dd7070Spatrick   //      PreImplicitCall (so that we would be able to find it when retrying a
84e5dd7070Spatrick   //      call with no inlining).
85e5dd7070Spatrick   // FIXME: It may be safe to reclaim PreCall and PostCall nodes as well.
86e5dd7070Spatrick 
87e5dd7070Spatrick   // Conditions 1 and 2.
88e5dd7070Spatrick   if (node->pred_size() != 1 || node->succ_size() != 1)
89e5dd7070Spatrick     return false;
90e5dd7070Spatrick 
91e5dd7070Spatrick   const ExplodedNode *pred = *(node->pred_begin());
92e5dd7070Spatrick   if (pred->succ_size() != 1)
93e5dd7070Spatrick     return false;
94e5dd7070Spatrick 
95e5dd7070Spatrick   const ExplodedNode *succ = *(node->succ_begin());
96e5dd7070Spatrick   if (succ->pred_size() != 1)
97e5dd7070Spatrick     return false;
98e5dd7070Spatrick 
99e5dd7070Spatrick   // Now reclaim any nodes that are (by definition) not essential to
100e5dd7070Spatrick   // analysis history and are not consulted by any client code.
101e5dd7070Spatrick   ProgramPoint progPoint = node->getLocation();
102e5dd7070Spatrick   if (progPoint.getAs<PreStmtPurgeDeadSymbols>())
103e5dd7070Spatrick     return !progPoint.getTag();
104e5dd7070Spatrick 
105e5dd7070Spatrick   // Condition 3.
106e5dd7070Spatrick   if (!progPoint.getAs<PostStmt>() || progPoint.getAs<PostStore>())
107e5dd7070Spatrick     return false;
108e5dd7070Spatrick 
109e5dd7070Spatrick   // Condition 4.
110e5dd7070Spatrick   if (progPoint.getTag())
111e5dd7070Spatrick     return false;
112e5dd7070Spatrick 
113e5dd7070Spatrick   // Conditions 5, 6, and 7.
114e5dd7070Spatrick   ProgramStateRef state = node->getState();
115e5dd7070Spatrick   ProgramStateRef pred_state = pred->getState();
116e5dd7070Spatrick   if (state->store != pred_state->store || state->GDM != pred_state->GDM ||
117e5dd7070Spatrick       progPoint.getLocationContext() != pred->getLocationContext())
118e5dd7070Spatrick     return false;
119e5dd7070Spatrick 
120e5dd7070Spatrick   // All further checks require expressions. As per #3, we know that we have
121e5dd7070Spatrick   // a PostStmt.
122e5dd7070Spatrick   const Expr *Ex = dyn_cast<Expr>(progPoint.castAs<PostStmt>().getStmt());
123e5dd7070Spatrick   if (!Ex)
124e5dd7070Spatrick     return false;
125e5dd7070Spatrick 
126e5dd7070Spatrick   // Condition 8.
127e5dd7070Spatrick   // Do not collect nodes for "interesting" lvalue expressions since they are
128e5dd7070Spatrick   // used extensively for generating path diagnostics.
129e5dd7070Spatrick   if (isInterestingLValueExpr(Ex))
130e5dd7070Spatrick     return false;
131e5dd7070Spatrick 
132e5dd7070Spatrick   // Condition 9.
133e5dd7070Spatrick   // Do not collect nodes for non-consumed Stmt or Expr to ensure precise
134e5dd7070Spatrick   // diagnostic generation; specifically, so that we could anchor arrows
135e5dd7070Spatrick   // pointing to the beginning of statements (as written in code).
136e5dd7070Spatrick   const ParentMap &PM = progPoint.getLocationContext()->getParentMap();
137e5dd7070Spatrick   if (!PM.isConsumedExpr(Ex))
138e5dd7070Spatrick     return false;
139e5dd7070Spatrick 
140e5dd7070Spatrick   // Condition 10.
141e5dd7070Spatrick   const ProgramPoint SuccLoc = succ->getLocation();
142*12c85518Srobert   if (std::optional<StmtPoint> SP = SuccLoc.getAs<StmtPoint>())
143e5dd7070Spatrick     if (CallEvent::isCallStmt(SP->getStmt()))
144e5dd7070Spatrick       return false;
145e5dd7070Spatrick 
146e5dd7070Spatrick   // Condition 10, continuation.
147e5dd7070Spatrick   if (SuccLoc.getAs<CallEnter>() || SuccLoc.getAs<PreImplicitCall>())
148e5dd7070Spatrick     return false;
149e5dd7070Spatrick 
150e5dd7070Spatrick   return true;
151e5dd7070Spatrick }
152e5dd7070Spatrick 
collectNode(ExplodedNode * node)153e5dd7070Spatrick void ExplodedGraph::collectNode(ExplodedNode *node) {
154e5dd7070Spatrick   // Removing a node means:
155e5dd7070Spatrick   // (a) changing the predecessors successor to the successor of this node
156e5dd7070Spatrick   // (b) changing the successors predecessor to the predecessor of this node
157e5dd7070Spatrick   // (c) Putting 'node' onto freeNodes.
158e5dd7070Spatrick   assert(node->pred_size() == 1 || node->succ_size() == 1);
159e5dd7070Spatrick   ExplodedNode *pred = *(node->pred_begin());
160e5dd7070Spatrick   ExplodedNode *succ = *(node->succ_begin());
161e5dd7070Spatrick   pred->replaceSuccessor(succ);
162e5dd7070Spatrick   succ->replacePredecessor(pred);
163e5dd7070Spatrick   FreeNodes.push_back(node);
164e5dd7070Spatrick   Nodes.RemoveNode(node);
165e5dd7070Spatrick   --NumNodes;
166e5dd7070Spatrick   node->~ExplodedNode();
167e5dd7070Spatrick }
168e5dd7070Spatrick 
reclaimRecentlyAllocatedNodes()169e5dd7070Spatrick void ExplodedGraph::reclaimRecentlyAllocatedNodes() {
170e5dd7070Spatrick   if (ChangedNodes.empty())
171e5dd7070Spatrick     return;
172e5dd7070Spatrick 
173e5dd7070Spatrick   // Only periodically reclaim nodes so that we can build up a set of
174e5dd7070Spatrick   // nodes that meet the reclamation criteria.  Freshly created nodes
175e5dd7070Spatrick   // by definition have no successor, and thus cannot be reclaimed (see below).
176e5dd7070Spatrick   assert(ReclaimCounter > 0);
177e5dd7070Spatrick   if (--ReclaimCounter != 0)
178e5dd7070Spatrick     return;
179e5dd7070Spatrick   ReclaimCounter = ReclaimNodeInterval;
180e5dd7070Spatrick 
181e5dd7070Spatrick   for (const auto node : ChangedNodes)
182e5dd7070Spatrick     if (shouldCollect(node))
183e5dd7070Spatrick       collectNode(node);
184e5dd7070Spatrick   ChangedNodes.clear();
185e5dd7070Spatrick }
186e5dd7070Spatrick 
187e5dd7070Spatrick //===----------------------------------------------------------------------===//
188e5dd7070Spatrick // ExplodedNode.
189e5dd7070Spatrick //===----------------------------------------------------------------------===//
190e5dd7070Spatrick 
191e5dd7070Spatrick // An NodeGroup's storage type is actually very much like a TinyPtrVector:
192e5dd7070Spatrick // it can be either a pointer to a single ExplodedNode, or a pointer to a
193e5dd7070Spatrick // BumpVector allocated with the ExplodedGraph's allocator. This allows the
194e5dd7070Spatrick // common case of single-node NodeGroups to be implemented with no extra memory.
195e5dd7070Spatrick //
196e5dd7070Spatrick // Consequently, each of the NodeGroup methods have up to four cases to handle:
197e5dd7070Spatrick // 1. The flag is set and this group does not actually contain any nodes.
198e5dd7070Spatrick // 2. The group is empty, in which case the storage value is null.
199e5dd7070Spatrick // 3. The group contains a single node.
200e5dd7070Spatrick // 4. The group contains more than one node.
201e5dd7070Spatrick using ExplodedNodeVector = BumpVector<ExplodedNode *>;
202e5dd7070Spatrick using GroupStorage = llvm::PointerUnion<ExplodedNode *, ExplodedNodeVector *>;
203e5dd7070Spatrick 
addPredecessor(ExplodedNode * V,ExplodedGraph & G)204e5dd7070Spatrick void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) {
205e5dd7070Spatrick   assert(!V->isSink());
206e5dd7070Spatrick   Preds.addNode(V, G);
207e5dd7070Spatrick   V->Succs.addNode(this, G);
208e5dd7070Spatrick }
209e5dd7070Spatrick 
replaceNode(ExplodedNode * node)210e5dd7070Spatrick void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) {
211e5dd7070Spatrick   assert(!getFlag());
212e5dd7070Spatrick 
213e5dd7070Spatrick   GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
214e5dd7070Spatrick   assert(Storage.is<ExplodedNode *>());
215e5dd7070Spatrick   Storage = node;
216e5dd7070Spatrick   assert(Storage.is<ExplodedNode *>());
217e5dd7070Spatrick }
218e5dd7070Spatrick 
addNode(ExplodedNode * N,ExplodedGraph & G)219e5dd7070Spatrick void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) {
220e5dd7070Spatrick   assert(!getFlag());
221e5dd7070Spatrick 
222e5dd7070Spatrick   GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
223e5dd7070Spatrick   if (Storage.isNull()) {
224e5dd7070Spatrick     Storage = N;
225e5dd7070Spatrick     assert(Storage.is<ExplodedNode *>());
226e5dd7070Spatrick     return;
227e5dd7070Spatrick   }
228e5dd7070Spatrick 
229e5dd7070Spatrick   ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>();
230e5dd7070Spatrick 
231e5dd7070Spatrick   if (!V) {
232e5dd7070Spatrick     // Switch from single-node to multi-node representation.
233e5dd7070Spatrick     ExplodedNode *Old = Storage.get<ExplodedNode *>();
234e5dd7070Spatrick 
235e5dd7070Spatrick     BumpVectorContext &Ctx = G.getNodeAllocator();
236e5dd7070Spatrick     V = G.getAllocator().Allocate<ExplodedNodeVector>();
237e5dd7070Spatrick     new (V) ExplodedNodeVector(Ctx, 4);
238e5dd7070Spatrick     V->push_back(Old, Ctx);
239e5dd7070Spatrick 
240e5dd7070Spatrick     Storage = V;
241e5dd7070Spatrick     assert(!getFlag());
242e5dd7070Spatrick     assert(Storage.is<ExplodedNodeVector *>());
243e5dd7070Spatrick   }
244e5dd7070Spatrick 
245e5dd7070Spatrick   V->push_back(N, G.getNodeAllocator());
246e5dd7070Spatrick }
247e5dd7070Spatrick 
size() const248e5dd7070Spatrick unsigned ExplodedNode::NodeGroup::size() const {
249e5dd7070Spatrick   if (getFlag())
250e5dd7070Spatrick     return 0;
251e5dd7070Spatrick 
252e5dd7070Spatrick   const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
253e5dd7070Spatrick   if (Storage.isNull())
254e5dd7070Spatrick     return 0;
255e5dd7070Spatrick   if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
256e5dd7070Spatrick     return V->size();
257e5dd7070Spatrick   return 1;
258e5dd7070Spatrick }
259e5dd7070Spatrick 
begin() const260e5dd7070Spatrick ExplodedNode * const *ExplodedNode::NodeGroup::begin() const {
261e5dd7070Spatrick   if (getFlag())
262e5dd7070Spatrick     return nullptr;
263e5dd7070Spatrick 
264e5dd7070Spatrick   const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
265e5dd7070Spatrick   if (Storage.isNull())
266e5dd7070Spatrick     return nullptr;
267e5dd7070Spatrick   if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
268e5dd7070Spatrick     return V->begin();
269e5dd7070Spatrick   return Storage.getAddrOfPtr1();
270e5dd7070Spatrick }
271e5dd7070Spatrick 
end() const272e5dd7070Spatrick ExplodedNode * const *ExplodedNode::NodeGroup::end() const {
273e5dd7070Spatrick   if (getFlag())
274e5dd7070Spatrick     return nullptr;
275e5dd7070Spatrick 
276e5dd7070Spatrick   const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
277e5dd7070Spatrick   if (Storage.isNull())
278e5dd7070Spatrick     return nullptr;
279e5dd7070Spatrick   if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
280e5dd7070Spatrick     return V->end();
281e5dd7070Spatrick   return Storage.getAddrOfPtr1() + 1;
282e5dd7070Spatrick }
283e5dd7070Spatrick 
isTrivial() const284e5dd7070Spatrick bool ExplodedNode::isTrivial() const {
285e5dd7070Spatrick   return pred_size() == 1 && succ_size() == 1 &&
286e5dd7070Spatrick          getFirstPred()->getState()->getID() == getState()->getID() &&
287e5dd7070Spatrick          getFirstPred()->succ_size() == 1;
288e5dd7070Spatrick }
289e5dd7070Spatrick 
getCFGBlock() const290e5dd7070Spatrick const CFGBlock *ExplodedNode::getCFGBlock() const {
291e5dd7070Spatrick   ProgramPoint P = getLocation();
292e5dd7070Spatrick   if (auto BEP = P.getAs<BlockEntrance>())
293e5dd7070Spatrick     return BEP->getBlock();
294e5dd7070Spatrick 
295e5dd7070Spatrick   // Find the node's current statement in the CFG.
296e5dd7070Spatrick   // FIXME: getStmtForDiagnostics() does nasty things in order to provide
297e5dd7070Spatrick   // a valid statement for body farms, do we need this behavior here?
298e5dd7070Spatrick   if (const Stmt *S = getStmtForDiagnostics())
299e5dd7070Spatrick     return getLocationContext()
300e5dd7070Spatrick         ->getAnalysisDeclContext()
301e5dd7070Spatrick         ->getCFGStmtMap()
302e5dd7070Spatrick         ->getBlock(S);
303e5dd7070Spatrick 
304e5dd7070Spatrick   return nullptr;
305e5dd7070Spatrick }
306e5dd7070Spatrick 
307e5dd7070Spatrick static const LocationContext *
findTopAutosynthesizedParentContext(const LocationContext * LC)308e5dd7070Spatrick findTopAutosynthesizedParentContext(const LocationContext *LC) {
309e5dd7070Spatrick   assert(LC->getAnalysisDeclContext()->isBodyAutosynthesized());
310e5dd7070Spatrick   const LocationContext *ParentLC = LC->getParent();
311e5dd7070Spatrick   assert(ParentLC && "We don't start analysis from autosynthesized code");
312e5dd7070Spatrick   while (ParentLC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
313e5dd7070Spatrick     LC = ParentLC;
314e5dd7070Spatrick     ParentLC = LC->getParent();
315e5dd7070Spatrick     assert(ParentLC && "We don't start analysis from autosynthesized code");
316e5dd7070Spatrick   }
317e5dd7070Spatrick   return LC;
318e5dd7070Spatrick }
319e5dd7070Spatrick 
getStmtForDiagnostics() const320e5dd7070Spatrick const Stmt *ExplodedNode::getStmtForDiagnostics() const {
321e5dd7070Spatrick   // We cannot place diagnostics on autosynthesized code.
322e5dd7070Spatrick   // Put them onto the call site through which we jumped into autosynthesized
323e5dd7070Spatrick   // code for the first time.
324e5dd7070Spatrick   const LocationContext *LC = getLocationContext();
325e5dd7070Spatrick   if (LC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
326e5dd7070Spatrick     // It must be a stack frame because we only autosynthesize functions.
327e5dd7070Spatrick     return cast<StackFrameContext>(findTopAutosynthesizedParentContext(LC))
328e5dd7070Spatrick         ->getCallSite();
329e5dd7070Spatrick   }
330e5dd7070Spatrick   // Otherwise, see if the node's program point directly points to a statement.
331e5dd7070Spatrick   // FIXME: Refactor into a ProgramPoint method?
332e5dd7070Spatrick   ProgramPoint P = getLocation();
333e5dd7070Spatrick   if (auto SP = P.getAs<StmtPoint>())
334e5dd7070Spatrick     return SP->getStmt();
335e5dd7070Spatrick   if (auto BE = P.getAs<BlockEdge>())
336e5dd7070Spatrick     return BE->getSrc()->getTerminatorStmt();
337e5dd7070Spatrick   if (auto CE = P.getAs<CallEnter>())
338e5dd7070Spatrick     return CE->getCallExpr();
339e5dd7070Spatrick   if (auto CEE = P.getAs<CallExitEnd>())
340e5dd7070Spatrick     return CEE->getCalleeContext()->getCallSite();
341e5dd7070Spatrick   if (auto PIPP = P.getAs<PostInitializer>())
342e5dd7070Spatrick     return PIPP->getInitializer()->getInit();
343e5dd7070Spatrick   if (auto CEB = P.getAs<CallExitBegin>())
344e5dd7070Spatrick     return CEB->getReturnStmt();
345e5dd7070Spatrick   if (auto FEP = P.getAs<FunctionExitPoint>())
346e5dd7070Spatrick     return FEP->getStmt();
347e5dd7070Spatrick 
348e5dd7070Spatrick   return nullptr;
349e5dd7070Spatrick }
350e5dd7070Spatrick 
getNextStmtForDiagnostics() const351e5dd7070Spatrick const Stmt *ExplodedNode::getNextStmtForDiagnostics() const {
352e5dd7070Spatrick   for (const ExplodedNode *N = getFirstSucc(); N; N = N->getFirstSucc()) {
353e5dd7070Spatrick     if (const Stmt *S = N->getStmtForDiagnostics()) {
354e5dd7070Spatrick       // Check if the statement is '?' or '&&'/'||'.  These are "merges",
355e5dd7070Spatrick       // not actual statement points.
356e5dd7070Spatrick       switch (S->getStmtClass()) {
357e5dd7070Spatrick         case Stmt::ChooseExprClass:
358e5dd7070Spatrick         case Stmt::BinaryConditionalOperatorClass:
359e5dd7070Spatrick         case Stmt::ConditionalOperatorClass:
360e5dd7070Spatrick           continue;
361e5dd7070Spatrick         case Stmt::BinaryOperatorClass: {
362e5dd7070Spatrick           BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
363e5dd7070Spatrick           if (Op == BO_LAnd || Op == BO_LOr)
364e5dd7070Spatrick             continue;
365e5dd7070Spatrick           break;
366e5dd7070Spatrick         }
367e5dd7070Spatrick         default:
368e5dd7070Spatrick           break;
369e5dd7070Spatrick       }
370e5dd7070Spatrick       // We found the statement, so return it.
371e5dd7070Spatrick       return S;
372e5dd7070Spatrick     }
373e5dd7070Spatrick   }
374e5dd7070Spatrick 
375e5dd7070Spatrick   return nullptr;
376e5dd7070Spatrick }
377e5dd7070Spatrick 
getPreviousStmtForDiagnostics() const378e5dd7070Spatrick const Stmt *ExplodedNode::getPreviousStmtForDiagnostics() const {
379e5dd7070Spatrick   for (const ExplodedNode *N = getFirstPred(); N; N = N->getFirstPred())
380e5dd7070Spatrick     if (const Stmt *S = N->getStmtForDiagnostics())
381e5dd7070Spatrick       return S;
382e5dd7070Spatrick 
383e5dd7070Spatrick   return nullptr;
384e5dd7070Spatrick }
385e5dd7070Spatrick 
getCurrentOrPreviousStmtForDiagnostics() const386e5dd7070Spatrick const Stmt *ExplodedNode::getCurrentOrPreviousStmtForDiagnostics() const {
387e5dd7070Spatrick   if (const Stmt *S = getStmtForDiagnostics())
388e5dd7070Spatrick     return S;
389e5dd7070Spatrick 
390e5dd7070Spatrick   return getPreviousStmtForDiagnostics();
391e5dd7070Spatrick }
392e5dd7070Spatrick 
getNode(const ProgramPoint & L,ProgramStateRef State,bool IsSink,bool * IsNew)393e5dd7070Spatrick ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L,
394e5dd7070Spatrick                                      ProgramStateRef State,
395e5dd7070Spatrick                                      bool IsSink,
396e5dd7070Spatrick                                      bool* IsNew) {
397e5dd7070Spatrick   // Profile 'State' to determine if we already have an existing node.
398e5dd7070Spatrick   llvm::FoldingSetNodeID profile;
399e5dd7070Spatrick   void *InsertPos = nullptr;
400e5dd7070Spatrick 
401e5dd7070Spatrick   NodeTy::Profile(profile, L, State, IsSink);
402e5dd7070Spatrick   NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
403e5dd7070Spatrick 
404e5dd7070Spatrick   if (!V) {
405e5dd7070Spatrick     if (!FreeNodes.empty()) {
406e5dd7070Spatrick       V = FreeNodes.back();
407e5dd7070Spatrick       FreeNodes.pop_back();
408e5dd7070Spatrick     }
409e5dd7070Spatrick     else {
410e5dd7070Spatrick       // Allocate a new node.
411e5dd7070Spatrick       V = (NodeTy*) getAllocator().Allocate<NodeTy>();
412e5dd7070Spatrick     }
413e5dd7070Spatrick 
414e5dd7070Spatrick     ++NumNodes;
415e5dd7070Spatrick     new (V) NodeTy(L, State, NumNodes, IsSink);
416e5dd7070Spatrick 
417e5dd7070Spatrick     if (ReclaimNodeInterval)
418e5dd7070Spatrick       ChangedNodes.push_back(V);
419e5dd7070Spatrick 
420e5dd7070Spatrick     // Insert the node into the node set and return it.
421e5dd7070Spatrick     Nodes.InsertNode(V, InsertPos);
422e5dd7070Spatrick 
423e5dd7070Spatrick     if (IsNew) *IsNew = true;
424e5dd7070Spatrick   }
425e5dd7070Spatrick   else
426e5dd7070Spatrick     if (IsNew) *IsNew = false;
427e5dd7070Spatrick 
428e5dd7070Spatrick   return V;
429e5dd7070Spatrick }
430e5dd7070Spatrick 
createUncachedNode(const ProgramPoint & L,ProgramStateRef State,int64_t Id,bool IsSink)431e5dd7070Spatrick ExplodedNode *ExplodedGraph::createUncachedNode(const ProgramPoint &L,
432e5dd7070Spatrick                                                 ProgramStateRef State,
433e5dd7070Spatrick                                                 int64_t Id,
434e5dd7070Spatrick                                                 bool IsSink) {
435e5dd7070Spatrick   NodeTy *V = (NodeTy *) getAllocator().Allocate<NodeTy>();
436e5dd7070Spatrick   new (V) NodeTy(L, State, Id, IsSink);
437e5dd7070Spatrick   return V;
438e5dd7070Spatrick }
439e5dd7070Spatrick 
440e5dd7070Spatrick std::unique_ptr<ExplodedGraph>
trim(ArrayRef<const NodeTy * > Sinks,InterExplodedGraphMap * ForwardMap,InterExplodedGraphMap * InverseMap) const441e5dd7070Spatrick ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks,
442e5dd7070Spatrick                     InterExplodedGraphMap *ForwardMap,
443e5dd7070Spatrick                     InterExplodedGraphMap *InverseMap) const {
444e5dd7070Spatrick   if (Nodes.empty())
445e5dd7070Spatrick     return nullptr;
446e5dd7070Spatrick 
447e5dd7070Spatrick   using Pass1Ty = llvm::DenseSet<const ExplodedNode *>;
448e5dd7070Spatrick   Pass1Ty Pass1;
449e5dd7070Spatrick 
450e5dd7070Spatrick   using Pass2Ty = InterExplodedGraphMap;
451e5dd7070Spatrick   InterExplodedGraphMap Pass2Scratch;
452e5dd7070Spatrick   Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch;
453e5dd7070Spatrick 
454e5dd7070Spatrick   SmallVector<const ExplodedNode*, 10> WL1, WL2;
455e5dd7070Spatrick 
456e5dd7070Spatrick   // ===- Pass 1 (reverse DFS) -===
457e5dd7070Spatrick   for (const auto Sink : Sinks)
458e5dd7070Spatrick     if (Sink)
459e5dd7070Spatrick       WL1.push_back(Sink);
460e5dd7070Spatrick 
461e5dd7070Spatrick   // Process the first worklist until it is empty.
462e5dd7070Spatrick   while (!WL1.empty()) {
463e5dd7070Spatrick     const ExplodedNode *N = WL1.pop_back_val();
464e5dd7070Spatrick 
465e5dd7070Spatrick     // Have we already visited this node?  If so, continue to the next one.
466e5dd7070Spatrick     if (!Pass1.insert(N).second)
467e5dd7070Spatrick       continue;
468e5dd7070Spatrick 
469e5dd7070Spatrick     // If this is a root enqueue it to the second worklist.
470e5dd7070Spatrick     if (N->Preds.empty()) {
471e5dd7070Spatrick       WL2.push_back(N);
472e5dd7070Spatrick       continue;
473e5dd7070Spatrick     }
474e5dd7070Spatrick 
475e5dd7070Spatrick     // Visit our predecessors and enqueue them.
476e5dd7070Spatrick     WL1.append(N->Preds.begin(), N->Preds.end());
477e5dd7070Spatrick   }
478e5dd7070Spatrick 
479e5dd7070Spatrick   // We didn't hit a root? Return with a null pointer for the new graph.
480e5dd7070Spatrick   if (WL2.empty())
481e5dd7070Spatrick     return nullptr;
482e5dd7070Spatrick 
483e5dd7070Spatrick   // Create an empty graph.
484e5dd7070Spatrick   std::unique_ptr<ExplodedGraph> G = MakeEmptyGraph();
485e5dd7070Spatrick 
486e5dd7070Spatrick   // ===- Pass 2 (forward DFS to construct the new graph) -===
487e5dd7070Spatrick   while (!WL2.empty()) {
488e5dd7070Spatrick     const ExplodedNode *N = WL2.pop_back_val();
489e5dd7070Spatrick 
490e5dd7070Spatrick     // Skip this node if we have already processed it.
491e5dd7070Spatrick     if (Pass2.find(N) != Pass2.end())
492e5dd7070Spatrick       continue;
493e5dd7070Spatrick 
494e5dd7070Spatrick     // Create the corresponding node in the new graph and record the mapping
495e5dd7070Spatrick     // from the old node to the new node.
496e5dd7070Spatrick     ExplodedNode *NewN = G->createUncachedNode(N->getLocation(), N->State,
497e5dd7070Spatrick                                                N->getID(), N->isSink());
498e5dd7070Spatrick     Pass2[N] = NewN;
499e5dd7070Spatrick 
500e5dd7070Spatrick     // Also record the reverse mapping from the new node to the old node.
501e5dd7070Spatrick     if (InverseMap) (*InverseMap)[NewN] = N;
502e5dd7070Spatrick 
503e5dd7070Spatrick     // If this node is a root, designate it as such in the graph.
504e5dd7070Spatrick     if (N->Preds.empty())
505e5dd7070Spatrick       G->addRoot(NewN);
506e5dd7070Spatrick 
507e5dd7070Spatrick     // In the case that some of the intended predecessors of NewN have already
508e5dd7070Spatrick     // been created, we should hook them up as predecessors.
509e5dd7070Spatrick 
510e5dd7070Spatrick     // Walk through the predecessors of 'N' and hook up their corresponding
511e5dd7070Spatrick     // nodes in the new graph (if any) to the freshly created node.
512e5dd7070Spatrick     for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
513e5dd7070Spatrick          I != E; ++I) {
514e5dd7070Spatrick       Pass2Ty::iterator PI = Pass2.find(*I);
515e5dd7070Spatrick       if (PI == Pass2.end())
516e5dd7070Spatrick         continue;
517e5dd7070Spatrick 
518e5dd7070Spatrick       NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G);
519e5dd7070Spatrick     }
520e5dd7070Spatrick 
521e5dd7070Spatrick     // In the case that some of the intended successors of NewN have already
522e5dd7070Spatrick     // been created, we should hook them up as successors.  Otherwise, enqueue
523e5dd7070Spatrick     // the new nodes from the original graph that should have nodes created
524e5dd7070Spatrick     // in the new graph.
525e5dd7070Spatrick     for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end();
526e5dd7070Spatrick          I != E; ++I) {
527e5dd7070Spatrick       Pass2Ty::iterator PI = Pass2.find(*I);
528e5dd7070Spatrick       if (PI != Pass2.end()) {
529e5dd7070Spatrick         const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G);
530e5dd7070Spatrick         continue;
531e5dd7070Spatrick       }
532e5dd7070Spatrick 
533e5dd7070Spatrick       // Enqueue nodes to the worklist that were marked during pass 1.
534e5dd7070Spatrick       if (Pass1.count(*I))
535e5dd7070Spatrick         WL2.push_back(*I);
536e5dd7070Spatrick     }
537e5dd7070Spatrick   }
538e5dd7070Spatrick 
539e5dd7070Spatrick   return G;
540e5dd7070Spatrick }
541