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