ExplodedGraph.h revision 2d950b15b2b2b650b102ecf0c6b50b45e0cb6a8a
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/ProgramState.h"
35
36namespace clang {
37
38class CFG;
39
40namespace ento {
41
42class 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
53class ExplodedNode : public llvm::FoldingSetNode {
54  friend class ExplodedGraph;
55  friend class CoreEngine;
56  friend class NodeBuilder;
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 ProgramState *State;
110
111  /// Preds - The predecessors of this node.
112  NodeGroup Preds;
113
114  /// Succs - The successors of this node.
115  NodeGroup Succs;
116
117public:
118
119  explicit ExplodedNode(const ProgramPoint &loc, const ProgramState *state)
120    : Location(loc), State(state) {
121    const_cast<ProgramState*>(State)->incrementReferenceCount();
122  }
123
124  ~ExplodedNode() {
125    const_cast<ProgramState*>(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  template <typename T>
142  T &getAnalysis() const {
143    return *getLocationContext()->getAnalysis<T>();
144  }
145
146  const ProgramState *getState() const { return State; }
147
148  template <typename T>
149  const T* getLocationAs() const { return llvm::dyn_cast<T>(&Location); }
150
151  static void Profile(llvm::FoldingSetNodeID &ID,
152                      const ProgramPoint &Loc, const ProgramState *state) {
153    ID.Add(Loc);
154    ID.AddPointer(state);
155  }
156
157  void Profile(llvm::FoldingSetNodeID& ID) const {
158    Profile(ID, getLocation(), getState());
159  }
160
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);
164
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(); }
169
170  bool isSink() const { return Succs.getFlag(); }
171  void markAsSink() { Succs.setFlag(); }
172
173  ExplodedNode *getFirstPred() {
174    return pred_empty() ? NULL : *(pred_begin());
175  }
176
177  const ExplodedNode *getFirstPred() const {
178    return const_cast<ExplodedNode*>(this)->getFirstPred();
179  }
180
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;
186
187  pred_iterator pred_begin() { return Preds.begin(); }
188  pred_iterator pred_end() { return Preds.end(); }
189
190  const_pred_iterator pred_begin() const {
191    return const_cast<ExplodedNode*>(this)->pred_begin();
192  }
193  const_pred_iterator pred_end() const {
194    return const_cast<ExplodedNode*>(this)->pred_end();
195  }
196
197  succ_iterator succ_begin() { return Succs.begin(); }
198  succ_iterator succ_end() { return Succs.end(); }
199
200  const_succ_iterator succ_begin() const {
201    return const_cast<ExplodedNode*>(this)->succ_begin();
202  }
203  const_succ_iterator succ_end() const {
204    return const_cast<ExplodedNode*>(this)->succ_end();
205  }
206
207  // For debugging.
208
209public:
210
211  class Auditor {
212  public:
213    virtual ~Auditor();
214    virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst) = 0;
215  };
216
217  static void SetAuditor(Auditor* A);
218
219private:
220  void replaceSuccessor(ExplodedNode *node) { Succs.replaceNode(node); }
221  void replacePredecessor(ExplodedNode *node) { Preds.replaceNode(node); }
222};
223
224// FIXME: Is this class necessary?
225class InterExplodedGraphMap {
226  llvm::DenseMap<const ExplodedNode*, ExplodedNode*> M;
227  friend class ExplodedGraph;
228
229public:
230  ExplodedNode *getMappedNode(const ExplodedNode *N) const;
231
232  InterExplodedGraphMap() {}
233  virtual ~InterExplodedGraphMap() {}
234};
235
236class ExplodedGraph {
237protected:
238  friend class CoreEngine;
239
240  // Type definitions.
241  typedef SmallVector<ExplodedNode*,2>    RootsTy;
242  typedef SmallVector<ExplodedNode*,10>   EndNodesTy;
243
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.
248  RootsTy Roots;
249
250  /// EndNodes - The nodes in the simulation graph which have been
251  ///  specially marked as the endpoint of an abstract simulation path.
252  EndNodesTy EndNodes;
253
254  /// Nodes - The nodes in the graph.
255  llvm::FoldingSet<ExplodedNode> Nodes;
256
257  /// BVC - Allocator and context for allocating nodes and their predecessor
258  /// and successor groups.
259  BumpVectorContext BVC;
260
261  /// NumNodes - The number of nodes in the graph.
262  unsigned NumNodes;
263
264  /// A list of recently allocated nodes that can potentially be recycled.
265  void *recentlyAllocatedNodes;
266
267  /// A list of nodes that can be reused.
268  void *freeNodes;
269
270  /// A flag that indicates whether nodes should be recycled.
271  bool reclaimNodes;
272
273public:
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.
278
279  ExplodedNode *getNode(const ProgramPoint &L, const ProgramState *State,
280                        bool* IsNew = 0);
281
282  ExplodedGraph* MakeEmptyGraph() const {
283    return new ExplodedGraph();
284  }
285
286  /// addRoot - Add an untyped node to the set of roots.
287  ExplodedNode *addRoot(ExplodedNode *V) {
288    Roots.push_back(V);
289    return V;
290  }
291
292  /// addEndOfPath - Add an untyped node to the set of EOP nodes.
293  ExplodedNode *addEndOfPath(ExplodedNode *V) {
294    EndNodes.push_back(V);
295    return V;
296  }
297
298  ExplodedGraph()
299    : NumNodes(0), recentlyAllocatedNodes(0),
300      freeNodes(0), reclaimNodes(false) {}
301
302  ~ExplodedGraph();
303
304  unsigned num_roots() const { return Roots.size(); }
305  unsigned num_eops() const { return EndNodes.size(); }
306
307  bool empty() const { return NumNodes == 0; }
308  unsigned size() const { return NumNodes; }
309
310  // Iterators.
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;
319
320  node_iterator nodes_begin() { return Nodes.begin(); }
321
322  node_iterator nodes_end() { return Nodes.end(); }
323
324  const_node_iterator nodes_begin() const { return Nodes.begin(); }
325
326  const_node_iterator nodes_end() const { return Nodes.end(); }
327
328  roots_iterator roots_begin() { return Roots.begin(); }
329
330  roots_iterator roots_end() { return Roots.end(); }
331
332  const_roots_iterator roots_begin() const { return Roots.begin(); }
333
334  const_roots_iterator roots_end() const { return Roots.end(); }
335
336  eop_iterator eop_begin() { return EndNodes.begin(); }
337
338  eop_iterator eop_end() { return EndNodes.end(); }
339
340  const_eop_iterator eop_begin() const { return EndNodes.begin(); }
341
342  const_eop_iterator eop_end() const { return EndNodes.end(); }
343
344  llvm::BumpPtrAllocator & getAllocator() { return BVC.getAllocator(); }
345  BumpVectorContext &getNodeAllocator() { return BVC; }
346
347  typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> NodeMap;
348
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;
352
353  ExplodedGraph* TrimInternal(const ExplodedNode* const * NBeg,
354                              const ExplodedNode* const * NEnd,
355                              InterExplodedGraphMap *M,
356                    llvm::DenseMap<const void*, const void*> *InverseMap) const;
357
358  /// Enable tracking of recently allocated nodes for potential reclamation
359  /// when calling reclaimRecentlyAllocatedNodes().
360  void enableNodeReclamation() { reclaimNodes = true; }
361
362  /// Reclaim "uninteresting" nodes created since the last time this method
363  /// was called.
364  void reclaimRecentlyAllocatedNodes();
365};
366
367class ExplodedNodeSet {
368  typedef llvm::SmallPtrSet<ExplodedNode*,5> ImplTy;
369  ImplTy Impl;
370
371public:
372  ExplodedNodeSet(ExplodedNode *N) {
373    assert (N && !static_cast<ExplodedNode*>(N)->isSink());
374    Impl.insert(N);
375  }
376
377  ExplodedNodeSet() {}
378
379  inline void Add(ExplodedNode *N) {
380    if (N && !static_cast<ExplodedNode*>(N)->isSink()) Impl.insert(N);
381  }
382
383  typedef ImplTy::iterator       iterator;
384  typedef ImplTy::const_iterator const_iterator;
385
386  unsigned size() const { return Impl.size();  }
387  bool empty()    const { return Impl.empty(); }
388  bool erase(ExplodedNode *N) { return Impl.erase(N); }
389
390  void clear() { Impl.clear(); }
391  void insert(const ExplodedNodeSet &S) {
392    assert(&S != this);
393    if (empty())
394      Impl = S.Impl;
395    else
396      Impl.insert(S.begin(), S.end());
397  }
398
399  inline iterator begin() { return Impl.begin(); }
400  inline iterator end()   { return Impl.end();   }
401
402  inline const_iterator begin() const { return Impl.begin(); }
403  inline const_iterator end()   const { return Impl.end();   }
404};
405
406} // end GR namespace
407
408} // end clang namespace
409
410// GraphTraits
411
412namespace llvm {
413  template<> struct GraphTraits<clang::ento::ExplodedNode*> {
414    typedef clang::ento::ExplodedNode NodeType;
415    typedef NodeType::succ_iterator  ChildIteratorType;
416    typedef llvm::df_iterator<NodeType*>      nodes_iterator;
417
418    static inline NodeType* getEntryNode(NodeType* N) {
419      return N;
420    }
421
422    static inline ChildIteratorType child_begin(NodeType* N) {
423      return N->succ_begin();
424    }
425
426    static inline ChildIteratorType child_end(NodeType* N) {
427      return N->succ_end();
428    }
429
430    static inline nodes_iterator nodes_begin(NodeType* N) {
431      return df_begin(N);
432    }
433
434    static inline nodes_iterator nodes_end(NodeType* N) {
435      return df_end(N);
436    }
437  };
438
439  template<> struct GraphTraits<const clang::ento::ExplodedNode*> {
440    typedef const clang::ento::ExplodedNode NodeType;
441    typedef NodeType::const_succ_iterator   ChildIteratorType;
442    typedef llvm::df_iterator<NodeType*>       nodes_iterator;
443
444    static inline NodeType* getEntryNode(NodeType* N) {
445      return N;
446    }
447
448    static inline ChildIteratorType child_begin(NodeType* N) {
449      return N->succ_begin();
450    }
451
452    static inline ChildIteratorType child_end(NodeType* N) {
453      return N->succ_end();
454    }
455
456    static inline nodes_iterator nodes_begin(NodeType* N) {
457      return df_begin(N);
458    }
459
460    static inline nodes_iterator nodes_end(NodeType* N) {
461      return df_end(N);
462    }
463  };
464
465} // end llvm namespace
466
467#endif
468