1 //=-- ExplodedGraph.h - Local, Path-Sens. "Exploded Graph" -*- C++ -*-------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the template classes ExplodedNode and ExplodedGraph,
11 // which represent a path-sensitive, intra-procedural "exploded graph."
12 // See "Precise interprocedural dataflow analysis via graph reachability"
13 // by Reps, Horwitz, and Sagiv
14 // (http://portal.acm.org/citation.cfm?id=199462) for the definition of an
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CLANG_GR_EXPLODEDGRAPH
20 #define LLVM_CLANG_GR_EXPLODEDGRAPH
22 #include "clang/Analysis/ProgramPoint.h"
23 #include "clang/Analysis/AnalysisContext.h"
24 #include "clang/AST/Decl.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/Support/Allocator.h"
29 #include "llvm/ADT/OwningPtr.h"
30 #include "llvm/ADT/GraphTraits.h"
31 #include "llvm/ADT/DepthFirstIterator.h"
32 #include "llvm/Support/Casting.h"
33 #include "clang/Analysis/Support/BumpVector.h"
34 #include "clang/StaticAnalyzer/Core/PathSensitive/GRState.h"
44 //===----------------------------------------------------------------------===//
45 // ExplodedGraph "implementation" classes. These classes are not typed to
46 // contain a specific kind of state. Typed-specialized versions are defined
47 // on top of these classes.
48 //===----------------------------------------------------------------------===//
50 // ExplodedNode is not constified all over the engine because we need to add
51 // successors to it at any time after creating it.
53 class ExplodedNode : public llvm::FoldingSetNode {
54 friend class ExplodedGraph;
55 friend class CoreEngine;
56 friend class StmtNodeBuilder;
57 friend class BranchNodeBuilder;
58 friend class IndirectGotoNodeBuilder;
59 friend class SwitchNodeBuilder;
60 friend class EndOfFunctionNodeBuilder;
63 enum { Size1 = 0x0, SizeOther = 0x1, AuxFlag = 0x2, Mask = 0x3 };
66 unsigned getKind() const {
70 void* getPtr() const {
72 return reinterpret_cast<void*>(P & ~Mask);
75 ExplodedNode *getNode() const {
76 return reinterpret_cast<ExplodedNode*>(getPtr());
82 ExplodedNode **begin() const;
84 ExplodedNode **end() const;
86 unsigned size() const;
88 bool empty() const { return (P & ~Mask) == 0; }
90 void addNode(ExplodedNode* N, ExplodedGraph &G);
92 void replaceNode(ExplodedNode *node);
99 bool getFlag() const {
100 return P & AuxFlag ? true : false;
104 /// Location - The program location (within a function body) associated
106 const ProgramPoint Location;
108 /// State - The state associated with this node.
109 const GRState* State;
111 /// Preds - The predecessors of this node.
114 /// Succs - The successors of this node.
119 explicit ExplodedNode(const ProgramPoint& loc, const GRState* state)
120 : Location(loc), State(state) {
121 const_cast<GRState*>(State)->incrementReferenceCount();
125 const_cast<GRState*>(State)->decrementReferenceCount();
128 /// getLocation - Returns the edge associated with the given node.
129 ProgramPoint getLocation() const { return Location; }
131 const LocationContext *getLocationContext() const {
132 return getLocation().getLocationContext();
135 const Decl &getCodeDecl() const { return *getLocationContext()->getDecl(); }
137 CFG &getCFG() const { return *getLocationContext()->getCFG(); }
139 ParentMap &getParentMap() const {return getLocationContext()->getParentMap();}
141 LiveVariables &getLiveVariables() const {
142 return *getLocationContext()->getLiveVariables();
145 const GRState* getState() const { return State; }
147 template <typename T>
148 const T* getLocationAs() const { return llvm::dyn_cast<T>(&Location); }
150 static void Profile(llvm::FoldingSetNodeID &ID,
151 const ProgramPoint& Loc, const GRState* state) {
153 ID.AddPointer(state);
156 void Profile(llvm::FoldingSetNodeID& ID) const {
157 Profile(ID, getLocation(), getState());
160 /// addPredeccessor - Adds a predecessor to the current node, and
161 /// in tandem add this node as a successor of the other node.
162 void addPredecessor(ExplodedNode* V, ExplodedGraph &G);
164 unsigned succ_size() const { return Succs.size(); }
165 unsigned pred_size() const { return Preds.size(); }
166 bool succ_empty() const { return Succs.empty(); }
167 bool pred_empty() const { return Preds.empty(); }
169 bool isSink() const { return Succs.getFlag(); }
170 void markAsSink() { Succs.setFlag(); }
172 ExplodedNode* getFirstPred() {
173 return pred_empty() ? NULL : *(pred_begin());
176 const ExplodedNode* getFirstPred() const {
177 return const_cast<ExplodedNode*>(this)->getFirstPred();
180 // Iterators over successor and predecessor vertices.
181 typedef ExplodedNode** succ_iterator;
182 typedef const ExplodedNode* const * const_succ_iterator;
183 typedef ExplodedNode** pred_iterator;
184 typedef const ExplodedNode* const * const_pred_iterator;
186 pred_iterator pred_begin() { return Preds.begin(); }
187 pred_iterator pred_end() { return Preds.end(); }
189 const_pred_iterator pred_begin() const {
190 return const_cast<ExplodedNode*>(this)->pred_begin();
192 const_pred_iterator pred_end() const {
193 return const_cast<ExplodedNode*>(this)->pred_end();
196 succ_iterator succ_begin() { return Succs.begin(); }
197 succ_iterator succ_end() { return Succs.end(); }
199 const_succ_iterator succ_begin() const {
200 return const_cast<ExplodedNode*>(this)->succ_begin();
202 const_succ_iterator succ_end() const {
203 return const_cast<ExplodedNode*>(this)->succ_end();
213 virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst) = 0;
216 static void SetAuditor(Auditor* A);
219 void replaceSuccessor(ExplodedNode *node) { Succs.replaceNode(node); }
220 void replacePredecessor(ExplodedNode *node) { Preds.replaceNode(node); }
223 // FIXME: Is this class necessary?
224 class InterExplodedGraphMap {
225 llvm::DenseMap<const ExplodedNode*, ExplodedNode*> M;
226 friend class ExplodedGraph;
229 ExplodedNode* getMappedNode(const ExplodedNode* N) const;
231 InterExplodedGraphMap() {}
232 virtual ~InterExplodedGraphMap() {}
235 class ExplodedGraph {
237 friend class CoreEngine;
240 typedef llvm::SmallVector<ExplodedNode*,2> RootsTy;
241 typedef llvm::SmallVector<ExplodedNode*,10> EndNodesTy;
243 /// Roots - The roots of the simulation graph. Usually there will be only
244 /// one, but clients are free to establish multiple subgraphs within a single
245 /// SimulGraph. Moreover, these subgraphs can often merge when paths from
246 /// different roots reach the same state at the same program location.
249 /// EndNodes - The nodes in the simulation graph which have been
250 /// specially marked as the endpoint of an abstract simulation path.
253 /// Nodes - The nodes in the graph.
254 llvm::FoldingSet<ExplodedNode> Nodes;
256 /// BVC - Allocator and context for allocating nodes and their predecessor
257 /// and successor groups.
258 BumpVectorContext BVC;
260 /// NumNodes - The number of nodes in the graph.
263 /// A list of recently allocated nodes that can potentially be recycled.
264 void *recentlyAllocatedNodes;
266 /// A list of nodes that can be reused.
269 /// A flag that indicates whether nodes should be recycled.
273 /// getNode - Retrieve the node associated with a (Location,State) pair,
274 /// where the 'Location' is a ProgramPoint in the CFG. If no node for
275 /// this pair exists, it is created. IsNew is set to true if
276 /// the node was freshly created.
278 ExplodedNode* getNode(const ProgramPoint& L, const GRState *State,
281 ExplodedGraph* MakeEmptyGraph() const {
282 return new ExplodedGraph();
285 /// addRoot - Add an untyped node to the set of roots.
286 ExplodedNode* addRoot(ExplodedNode* V) {
291 /// addEndOfPath - Add an untyped node to the set of EOP nodes.
292 ExplodedNode* addEndOfPath(ExplodedNode* V) {
293 EndNodes.push_back(V);
298 : NumNodes(0), recentlyAllocatedNodes(0),
299 freeNodes(0), reclaimNodes(false) {}
303 unsigned num_roots() const { return Roots.size(); }
304 unsigned num_eops() const { return EndNodes.size(); }
306 bool empty() const { return NumNodes == 0; }
307 unsigned size() const { return NumNodes; }
310 typedef ExplodedNode NodeTy;
311 typedef llvm::FoldingSet<ExplodedNode> AllNodesTy;
312 typedef NodeTy** roots_iterator;
313 typedef NodeTy* const * const_roots_iterator;
314 typedef NodeTy** eop_iterator;
315 typedef NodeTy* const * const_eop_iterator;
316 typedef AllNodesTy::iterator node_iterator;
317 typedef AllNodesTy::const_iterator const_node_iterator;
319 node_iterator nodes_begin() { return Nodes.begin(); }
321 node_iterator nodes_end() { return Nodes.end(); }
323 const_node_iterator nodes_begin() const { return Nodes.begin(); }
325 const_node_iterator nodes_end() const { return Nodes.end(); }
327 roots_iterator roots_begin() { return Roots.begin(); }
329 roots_iterator roots_end() { return Roots.end(); }
331 const_roots_iterator roots_begin() const { return Roots.begin(); }
333 const_roots_iterator roots_end() const { return Roots.end(); }
335 eop_iterator eop_begin() { return EndNodes.begin(); }
337 eop_iterator eop_end() { return EndNodes.end(); }
339 const_eop_iterator eop_begin() const { return EndNodes.begin(); }
341 const_eop_iterator eop_end() const { return EndNodes.end(); }
343 llvm::BumpPtrAllocator & getAllocator() { return BVC.getAllocator(); }
344 BumpVectorContext &getNodeAllocator() { return BVC; }
346 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> NodeMap;
348 std::pair<ExplodedGraph*, InterExplodedGraphMap*>
349 Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd,
350 llvm::DenseMap<const void*, const void*> *InverseMap = 0) const;
352 ExplodedGraph* TrimInternal(const ExplodedNode* const * NBeg,
353 const ExplodedNode* const * NEnd,
354 InterExplodedGraphMap *M,
355 llvm::DenseMap<const void*, const void*> *InverseMap) const;
357 /// Enable tracking of recently allocated nodes for potential reclamation
358 /// when calling reclaimRecentlyAllocatedNodes().
359 void enableNodeReclamation() { reclaimNodes = true; }
361 /// Reclaim "uninteresting" nodes created since the last time this method
363 void reclaimRecentlyAllocatedNodes();
366 class ExplodedNodeSet {
367 typedef llvm::SmallPtrSet<ExplodedNode*,5> ImplTy;
371 ExplodedNodeSet(ExplodedNode* N) {
372 assert (N && !static_cast<ExplodedNode*>(N)->isSink());
378 inline void Add(ExplodedNode* N) {
379 if (N && !static_cast<ExplodedNode*>(N)->isSink()) Impl.insert(N);
382 ExplodedNodeSet& operator=(const ExplodedNodeSet &X) {
387 typedef ImplTy::iterator iterator;
388 typedef ImplTy::const_iterator const_iterator;
390 unsigned size() const { return Impl.size(); }
391 bool empty() const { return Impl.empty(); }
393 void clear() { Impl.clear(); }
394 void insert(const ExplodedNodeSet &S) {
398 Impl.insert(S.begin(), S.end());
401 inline iterator begin() { return Impl.begin(); }
402 inline iterator end() { return Impl.end(); }
404 inline const_iterator begin() const { return Impl.begin(); }
405 inline const_iterator end() const { return Impl.end(); }
408 } // end GR namespace
410 } // end clang namespace
415 template<> struct GraphTraits<clang::ento::ExplodedNode*> {
416 typedef clang::ento::ExplodedNode NodeType;
417 typedef NodeType::succ_iterator ChildIteratorType;
418 typedef llvm::df_iterator<NodeType*> nodes_iterator;
420 static inline NodeType* getEntryNode(NodeType* N) {
424 static inline ChildIteratorType child_begin(NodeType* N) {
425 return N->succ_begin();
428 static inline ChildIteratorType child_end(NodeType* N) {
429 return N->succ_end();
432 static inline nodes_iterator nodes_begin(NodeType* N) {
436 static inline nodes_iterator nodes_end(NodeType* N) {
441 template<> struct GraphTraits<const clang::ento::ExplodedNode*> {
442 typedef const clang::ento::ExplodedNode NodeType;
443 typedef NodeType::const_succ_iterator ChildIteratorType;
444 typedef llvm::df_iterator<NodeType*> nodes_iterator;
446 static inline NodeType* getEntryNode(NodeType* N) {
450 static inline ChildIteratorType child_begin(NodeType* N) {
451 return N->succ_begin();
454 static inline ChildIteratorType child_end(NodeType* N) {
455 return N->succ_end();
458 static inline nodes_iterator nodes_begin(NodeType* N) {
462 static inline nodes_iterator nodes_end(NodeType* N) {
467 } // end llvm namespace