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/ProgramState.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 ProgramState *State;
111 /// Preds - The predecessors of this node.
114 /// Succs - The successors of this node.
119 explicit ExplodedNode(const ProgramPoint &loc, const ProgramState *state)
120 : Location(loc), State(state) {
121 const_cast<ProgramState*>(State)->incrementReferenceCount();
125 const_cast<ProgramState*>(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 template <typename T>
142 T &getAnalysis() const {
143 return *getLocationContext()->getAnalysis<T>();
146 const ProgramState *getState() const { return State; }
148 template <typename T>
149 const T* getLocationAs() const { return llvm::dyn_cast<T>(&Location); }
151 static void Profile(llvm::FoldingSetNodeID &ID,
152 const ProgramPoint &Loc, const ProgramState *state) {
154 ID.AddPointer(state);
157 void Profile(llvm::FoldingSetNodeID& ID) const {
158 Profile(ID, getLocation(), getState());
161 /// addPredeccessor - Adds a predecessor to the current node, and
162 /// in tandem add this node as a successor of the other node.
163 void addPredecessor(ExplodedNode *V, ExplodedGraph &G);
165 unsigned succ_size() const { return Succs.size(); }
166 unsigned pred_size() const { return Preds.size(); }
167 bool succ_empty() const { return Succs.empty(); }
168 bool pred_empty() const { return Preds.empty(); }
170 bool isSink() const { return Succs.getFlag(); }
171 void markAsSink() { Succs.setFlag(); }
173 ExplodedNode *getFirstPred() {
174 return pred_empty() ? NULL : *(pred_begin());
177 const ExplodedNode *getFirstPred() const {
178 return const_cast<ExplodedNode*>(this)->getFirstPred();
181 // Iterators over successor and predecessor vertices.
182 typedef ExplodedNode** succ_iterator;
183 typedef const ExplodedNode* const * const_succ_iterator;
184 typedef ExplodedNode** pred_iterator;
185 typedef const ExplodedNode* const * const_pred_iterator;
187 pred_iterator pred_begin() { return Preds.begin(); }
188 pred_iterator pred_end() { return Preds.end(); }
190 const_pred_iterator pred_begin() const {
191 return const_cast<ExplodedNode*>(this)->pred_begin();
193 const_pred_iterator pred_end() const {
194 return const_cast<ExplodedNode*>(this)->pred_end();
197 succ_iterator succ_begin() { return Succs.begin(); }
198 succ_iterator succ_end() { return Succs.end(); }
200 const_succ_iterator succ_begin() const {
201 return const_cast<ExplodedNode*>(this)->succ_begin();
203 const_succ_iterator succ_end() const {
204 return const_cast<ExplodedNode*>(this)->succ_end();
214 virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst) = 0;
217 static void SetAuditor(Auditor* A);
220 void replaceSuccessor(ExplodedNode *node) { Succs.replaceNode(node); }
221 void replacePredecessor(ExplodedNode *node) { Preds.replaceNode(node); }
224 // FIXME: Is this class necessary?
225 class InterExplodedGraphMap {
226 llvm::DenseMap<const ExplodedNode*, ExplodedNode*> M;
227 friend class ExplodedGraph;
230 ExplodedNode *getMappedNode(const ExplodedNode *N) const;
232 InterExplodedGraphMap() {}
233 virtual ~InterExplodedGraphMap() {}
236 class ExplodedGraph {
238 friend class CoreEngine;
241 typedef SmallVector<ExplodedNode*,2> RootsTy;
242 typedef SmallVector<ExplodedNode*,10> EndNodesTy;
244 /// Roots - The roots of the simulation graph. Usually there will be only
245 /// one, but clients are free to establish multiple subgraphs within a single
246 /// SimulGraph. Moreover, these subgraphs can often merge when paths from
247 /// different roots reach the same state at the same program location.
250 /// EndNodes - The nodes in the simulation graph which have been
251 /// specially marked as the endpoint of an abstract simulation path.
254 /// Nodes - The nodes in the graph.
255 llvm::FoldingSet<ExplodedNode> Nodes;
257 /// BVC - Allocator and context for allocating nodes and their predecessor
258 /// and successor groups.
259 BumpVectorContext BVC;
261 /// NumNodes - The number of nodes in the graph.
264 /// A list of recently allocated nodes that can potentially be recycled.
265 void *recentlyAllocatedNodes;
267 /// A list of nodes that can be reused.
270 /// A flag that indicates whether nodes should be recycled.
274 /// getNode - Retrieve the node associated with a (Location,State) pair,
275 /// where the 'Location' is a ProgramPoint in the CFG. If no node for
276 /// this pair exists, it is created. IsNew is set to true if
277 /// the node was freshly created.
279 ExplodedNode *getNode(const ProgramPoint &L, const ProgramState *State,
282 ExplodedGraph* MakeEmptyGraph() const {
283 return new ExplodedGraph();
286 /// addRoot - Add an untyped node to the set of roots.
287 ExplodedNode *addRoot(ExplodedNode *V) {
292 /// addEndOfPath - Add an untyped node to the set of EOP nodes.
293 ExplodedNode *addEndOfPath(ExplodedNode *V) {
294 EndNodes.push_back(V);
299 : NumNodes(0), recentlyAllocatedNodes(0),
300 freeNodes(0), reclaimNodes(false) {}
304 unsigned num_roots() const { return Roots.size(); }
305 unsigned num_eops() const { return EndNodes.size(); }
307 bool empty() const { return NumNodes == 0; }
308 unsigned size() const { return NumNodes; }
311 typedef ExplodedNode NodeTy;
312 typedef llvm::FoldingSet<ExplodedNode> AllNodesTy;
313 typedef NodeTy** roots_iterator;
314 typedef NodeTy* const * const_roots_iterator;
315 typedef NodeTy** eop_iterator;
316 typedef NodeTy* const * const_eop_iterator;
317 typedef AllNodesTy::iterator node_iterator;
318 typedef AllNodesTy::const_iterator const_node_iterator;
320 node_iterator nodes_begin() { return Nodes.begin(); }
322 node_iterator nodes_end() { return Nodes.end(); }
324 const_node_iterator nodes_begin() const { return Nodes.begin(); }
326 const_node_iterator nodes_end() const { return Nodes.end(); }
328 roots_iterator roots_begin() { return Roots.begin(); }
330 roots_iterator roots_end() { return Roots.end(); }
332 const_roots_iterator roots_begin() const { return Roots.begin(); }
334 const_roots_iterator roots_end() const { return Roots.end(); }
336 eop_iterator eop_begin() { return EndNodes.begin(); }
338 eop_iterator eop_end() { return EndNodes.end(); }
340 const_eop_iterator eop_begin() const { return EndNodes.begin(); }
342 const_eop_iterator eop_end() const { return EndNodes.end(); }
344 llvm::BumpPtrAllocator & getAllocator() { return BVC.getAllocator(); }
345 BumpVectorContext &getNodeAllocator() { return BVC; }
347 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> NodeMap;
349 std::pair<ExplodedGraph*, InterExplodedGraphMap*>
350 Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd,
351 llvm::DenseMap<const void*, const void*> *InverseMap = 0) const;
353 ExplodedGraph* TrimInternal(const ExplodedNode* const * NBeg,
354 const ExplodedNode* const * NEnd,
355 InterExplodedGraphMap *M,
356 llvm::DenseMap<const void*, const void*> *InverseMap) const;
358 /// Enable tracking of recently allocated nodes for potential reclamation
359 /// when calling reclaimRecentlyAllocatedNodes().
360 void enableNodeReclamation() { reclaimNodes = true; }
362 /// Reclaim "uninteresting" nodes created since the last time this method
364 void reclaimRecentlyAllocatedNodes();
367 class ExplodedNodeSet {
368 typedef llvm::SmallPtrSet<ExplodedNode*,5> ImplTy;
372 ExplodedNodeSet(ExplodedNode *N) {
373 assert (N && !static_cast<ExplodedNode*>(N)->isSink());
379 inline void Add(ExplodedNode *N) {
380 if (N && !static_cast<ExplodedNode*>(N)->isSink()) Impl.insert(N);
383 ExplodedNodeSet &operator=(const ExplodedNodeSet &X) {
388 typedef ImplTy::iterator iterator;
389 typedef ImplTy::const_iterator const_iterator;
391 unsigned size() const { return Impl.size(); }
392 bool empty() const { return Impl.empty(); }
394 void clear() { Impl.clear(); }
395 void insert(const ExplodedNodeSet &S) {
399 Impl.insert(S.begin(), S.end());
402 inline iterator begin() { return Impl.begin(); }
403 inline iterator end() { return Impl.end(); }
405 inline const_iterator begin() const { return Impl.begin(); }
406 inline const_iterator end() const { return Impl.end(); }
409 } // end GR namespace
411 } // end clang namespace
416 template<> struct GraphTraits<clang::ento::ExplodedNode*> {
417 typedef clang::ento::ExplodedNode NodeType;
418 typedef NodeType::succ_iterator ChildIteratorType;
419 typedef llvm::df_iterator<NodeType*> nodes_iterator;
421 static inline NodeType* getEntryNode(NodeType* N) {
425 static inline ChildIteratorType child_begin(NodeType* N) {
426 return N->succ_begin();
429 static inline ChildIteratorType child_end(NodeType* N) {
430 return N->succ_end();
433 static inline nodes_iterator nodes_begin(NodeType* N) {
437 static inline nodes_iterator nodes_end(NodeType* N) {
442 template<> struct GraphTraits<const clang::ento::ExplodedNode*> {
443 typedef const clang::ento::ExplodedNode NodeType;
444 typedef NodeType::const_succ_iterator ChildIteratorType;
445 typedef llvm::df_iterator<NodeType*> nodes_iterator;
447 static inline NodeType* getEntryNode(NodeType* N) {
451 static inline ChildIteratorType child_begin(NodeType* N) {
452 return N->succ_begin();
455 static inline ChildIteratorType child_end(NodeType* N) {
456 return N->succ_end();
459 static inline nodes_iterator nodes_begin(NodeType* N) {
463 static inline nodes_iterator nodes_end(NodeType* N) {
468 } // end llvm namespace