]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / include / clang / StaticAnalyzer / Core / PathSensitive / ExplodedGraph.h
1 //=-- ExplodedGraph.h - Local, Path-Sens. "Exploded Graph" -*- C++ -*-------==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
15 //  exploded graph.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_CLANG_GR_EXPLODEDGRAPH
20 #define LLVM_CLANG_GR_EXPLODEDGRAPH
21
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"
35
36 namespace clang {
37
38 class CFG;
39
40 namespace ento {
41
42 class ExplodedGraph;
43
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 //===----------------------------------------------------------------------===//
49
50 // ExplodedNode is not constified all over the engine because we need to add
51 // successors to it at any time after creating it.
52
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;
61
62   class NodeGroup {
63     enum { Size1 = 0x0, SizeOther = 0x1, AuxFlag = 0x2, Mask = 0x3 };
64     uintptr_t P;
65
66     unsigned getKind() const {
67       return P & 0x1;
68     }
69
70     void* getPtr() const {
71       assert (!getFlag());
72       return reinterpret_cast<void*>(P & ~Mask);
73     }
74
75     ExplodedNode *getNode() const {
76       return reinterpret_cast<ExplodedNode*>(getPtr());
77     }
78     
79   public:
80     NodeGroup() : P(0) {}
81
82     ExplodedNode **begin() const;
83
84     ExplodedNode **end() const;
85
86     unsigned size() const;
87
88     bool empty() const { return (P & ~Mask) == 0; }
89
90     void addNode(ExplodedNode* N, ExplodedGraph &G);
91
92     void replaceNode(ExplodedNode *node);
93
94     void setFlag() {
95       assert(P == 0);
96       P = AuxFlag;
97     }
98
99     bool getFlag() const {
100       return P & AuxFlag ? true : false;
101     }
102   };
103
104   /// Location - The program location (within a function body) associated
105   ///  with this node.
106   const ProgramPoint Location;
107
108   /// State - The state associated with this node.
109   const GRState* State;
110
111   /// Preds - The predecessors of this node.
112   NodeGroup Preds;
113
114   /// Succs - The successors of this node.
115   NodeGroup Succs;
116
117 public:
118
119   explicit ExplodedNode(const ProgramPoint& loc, const GRState* state)
120     : Location(loc), State(state) {
121     const_cast<GRState*>(State)->incrementReferenceCount();
122   }
123   
124   ~ExplodedNode() {
125     const_cast<GRState*>(State)->decrementReferenceCount();
126   }
127
128   /// getLocation - Returns the edge associated with the given node.
129   ProgramPoint getLocation() const { return Location; }
130
131   const LocationContext *getLocationContext() const {
132     return getLocation().getLocationContext();
133   }
134
135   const Decl &getCodeDecl() const { return *getLocationContext()->getDecl(); }
136
137   CFG &getCFG() const { return *getLocationContext()->getCFG(); }
138
139   ParentMap &getParentMap() const {return getLocationContext()->getParentMap();}
140
141   LiveVariables &getLiveVariables() const { 
142     return *getLocationContext()->getLiveVariables(); 
143   }
144
145   const GRState* getState() const { return State; }
146
147   template <typename T>
148   const T* getLocationAs() const { return llvm::dyn_cast<T>(&Location); }
149
150   static void Profile(llvm::FoldingSetNodeID &ID,
151                       const ProgramPoint& Loc, const GRState* state) {
152     ID.Add(Loc);
153     ID.AddPointer(state);
154   }
155
156   void Profile(llvm::FoldingSetNodeID& ID) const {
157     Profile(ID, getLocation(), getState());
158   }
159
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);
163
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(); }
168
169   bool isSink() const { return Succs.getFlag(); }
170   void markAsSink() { Succs.setFlag(); }
171
172   ExplodedNode* getFirstPred() {
173     return pred_empty() ? NULL : *(pred_begin());
174   }
175
176   const ExplodedNode* getFirstPred() const {
177     return const_cast<ExplodedNode*>(this)->getFirstPred();
178   }
179
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;
185
186   pred_iterator pred_begin() { return Preds.begin(); }
187   pred_iterator pred_end() { return Preds.end(); }
188
189   const_pred_iterator pred_begin() const {
190     return const_cast<ExplodedNode*>(this)->pred_begin();
191   }
192   const_pred_iterator pred_end() const {
193     return const_cast<ExplodedNode*>(this)->pred_end();
194   }
195
196   succ_iterator succ_begin() { return Succs.begin(); }
197   succ_iterator succ_end() { return Succs.end(); }
198
199   const_succ_iterator succ_begin() const {
200     return const_cast<ExplodedNode*>(this)->succ_begin();
201   }
202   const_succ_iterator succ_end() const {
203     return const_cast<ExplodedNode*>(this)->succ_end();
204   }
205
206   // For debugging.
207
208 public:
209
210   class Auditor {
211   public:
212     virtual ~Auditor();
213     virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst) = 0;
214   };
215
216   static void SetAuditor(Auditor* A);
217   
218 private:
219   void replaceSuccessor(ExplodedNode *node) { Succs.replaceNode(node); }
220   void replacePredecessor(ExplodedNode *node) { Preds.replaceNode(node); }
221 };
222
223 // FIXME: Is this class necessary?
224 class InterExplodedGraphMap {
225   llvm::DenseMap<const ExplodedNode*, ExplodedNode*> M;
226   friend class ExplodedGraph;
227
228 public:
229   ExplodedNode* getMappedNode(const ExplodedNode* N) const;
230
231   InterExplodedGraphMap() {}
232   virtual ~InterExplodedGraphMap() {}
233 };
234
235 class ExplodedGraph {
236 protected:
237   friend class CoreEngine;
238
239   // Type definitions.
240   typedef llvm::SmallVector<ExplodedNode*,2>    RootsTy;
241   typedef llvm::SmallVector<ExplodedNode*,10>   EndNodesTy;
242
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.
247   RootsTy Roots;
248
249   /// EndNodes - The nodes in the simulation graph which have been
250   ///  specially marked as the endpoint of an abstract simulation path.
251   EndNodesTy EndNodes;
252
253   /// Nodes - The nodes in the graph.
254   llvm::FoldingSet<ExplodedNode> Nodes;
255
256   /// BVC - Allocator and context for allocating nodes and their predecessor
257   /// and successor groups.
258   BumpVectorContext BVC;
259
260   /// NumNodes - The number of nodes in the graph.
261   unsigned NumNodes;
262   
263   /// A list of recently allocated nodes that can potentially be recycled.
264   void *recentlyAllocatedNodes;
265   
266   /// A list of nodes that can be reused.
267   void *freeNodes;
268   
269   /// A flag that indicates whether nodes should be recycled.
270   bool reclaimNodes;
271
272 public:
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.
277
278   ExplodedNode* getNode(const ProgramPoint& L, const GRState *State,
279                         bool* IsNew = 0);
280
281   ExplodedGraph* MakeEmptyGraph() const {
282     return new ExplodedGraph();
283   }
284
285   /// addRoot - Add an untyped node to the set of roots.
286   ExplodedNode* addRoot(ExplodedNode* V) {
287     Roots.push_back(V);
288     return V;
289   }
290
291   /// addEndOfPath - Add an untyped node to the set of EOP nodes.
292   ExplodedNode* addEndOfPath(ExplodedNode* V) {
293     EndNodes.push_back(V);
294     return V;
295   }
296
297   ExplodedGraph()
298     : NumNodes(0), recentlyAllocatedNodes(0),
299       freeNodes(0), reclaimNodes(false) {}
300
301   ~ExplodedGraph();
302   
303   unsigned num_roots() const { return Roots.size(); }
304   unsigned num_eops() const { return EndNodes.size(); }
305
306   bool empty() const { return NumNodes == 0; }
307   unsigned size() const { return NumNodes; }
308
309   // Iterators.
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;
318
319   node_iterator nodes_begin() { return Nodes.begin(); }
320
321   node_iterator nodes_end() { return Nodes.end(); }
322
323   const_node_iterator nodes_begin() const { return Nodes.begin(); }
324
325   const_node_iterator nodes_end() const { return Nodes.end(); }
326
327   roots_iterator roots_begin() { return Roots.begin(); }
328
329   roots_iterator roots_end() { return Roots.end(); }
330
331   const_roots_iterator roots_begin() const { return Roots.begin(); }
332
333   const_roots_iterator roots_end() const { return Roots.end(); }
334
335   eop_iterator eop_begin() { return EndNodes.begin(); }
336
337   eop_iterator eop_end() { return EndNodes.end(); }
338
339   const_eop_iterator eop_begin() const { return EndNodes.begin(); }
340
341   const_eop_iterator eop_end() const { return EndNodes.end(); }
342
343   llvm::BumpPtrAllocator & getAllocator() { return BVC.getAllocator(); }
344   BumpVectorContext &getNodeAllocator() { return BVC; }
345
346   typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> NodeMap;
347
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;
351
352   ExplodedGraph* TrimInternal(const ExplodedNode* const * NBeg,
353                               const ExplodedNode* const * NEnd,
354                               InterExplodedGraphMap *M,
355                     llvm::DenseMap<const void*, const void*> *InverseMap) const;
356
357   /// Enable tracking of recently allocated nodes for potential reclamation
358   /// when calling reclaimRecentlyAllocatedNodes().
359   void enableNodeReclamation() { reclaimNodes = true; }
360
361   /// Reclaim "uninteresting" nodes created since the last time this method
362   /// was called.
363   void reclaimRecentlyAllocatedNodes();
364 };
365
366 class ExplodedNodeSet {
367   typedef llvm::SmallPtrSet<ExplodedNode*,5> ImplTy;
368   ImplTy Impl;
369
370 public:
371   ExplodedNodeSet(ExplodedNode* N) {
372     assert (N && !static_cast<ExplodedNode*>(N)->isSink());
373     Impl.insert(N);
374   }
375
376   ExplodedNodeSet() {}
377
378   inline void Add(ExplodedNode* N) {
379     if (N && !static_cast<ExplodedNode*>(N)->isSink()) Impl.insert(N);
380   }
381
382   ExplodedNodeSet& operator=(const ExplodedNodeSet &X) {
383     Impl = X.Impl;
384     return *this;
385   }
386
387   typedef ImplTy::iterator       iterator;
388   typedef ImplTy::const_iterator const_iterator;
389
390   unsigned size() const { return Impl.size();  }
391   bool empty()    const { return Impl.empty(); }
392
393   void clear() { Impl.clear(); }
394   void insert(const ExplodedNodeSet &S) {
395     if (empty())
396       Impl = S.Impl;
397     else
398       Impl.insert(S.begin(), S.end());
399   }
400
401   inline iterator begin() { return Impl.begin(); }
402   inline iterator end()   { return Impl.end();   }
403
404   inline const_iterator begin() const { return Impl.begin(); }
405   inline const_iterator end()   const { return Impl.end();   }
406 };
407
408 } // end GR namespace
409
410 } // end clang namespace
411
412 // GraphTraits
413
414 namespace llvm {
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;
419
420     static inline NodeType* getEntryNode(NodeType* N) {
421       return N;
422     }
423
424     static inline ChildIteratorType child_begin(NodeType* N) {
425       return N->succ_begin();
426     }
427
428     static inline ChildIteratorType child_end(NodeType* N) {
429       return N->succ_end();
430     }
431
432     static inline nodes_iterator nodes_begin(NodeType* N) {
433       return df_begin(N);
434     }
435
436     static inline nodes_iterator nodes_end(NodeType* N) {
437       return df_end(N);
438     }
439   };
440
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;
445
446     static inline NodeType* getEntryNode(NodeType* N) {
447       return N;
448     }
449
450     static inline ChildIteratorType child_begin(NodeType* N) {
451       return N->succ_begin();
452     }
453
454     static inline ChildIteratorType child_end(NodeType* N) {
455       return N->succ_end();
456     }
457
458     static inline nodes_iterator nodes_begin(NodeType* N) {
459       return df_begin(N);
460     }
461
462     static inline nodes_iterator nodes_end(NodeType* N) {
463       return df_end(N);
464     }
465   };
466
467 } // end llvm namespace
468
469 #endif