ExplodedGraph.h revision 5fe4d9deb543a19f557e3d85c5f33867af97cd96
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//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_ANALYSIS_EXPLODEDGRAPH
16#define LLVM_CLANG_ANALYSIS_EXPLODEDGRAPH
17
18#include "clang/Analysis/ProgramPoint.h"
19#include "clang/Analysis/PathSensitive/AnalysisContext.h"
20#include "clang/AST/Decl.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/ADT/SmallPtrSet.h"
24#include "llvm/Support/Allocator.h"
25#include "llvm/ADT/OwningPtr.h"
26#include "llvm/ADT/GraphTraits.h"
27#include "llvm/ADT/DepthFirstIterator.h"
28#include "llvm/Support/Casting.h"
29#include "clang/Analysis/Support/BumpVector.h"
30
31namespace clang {
32
33class GRState;
34class CFG;
35class ASTContext;
36class ExplodedGraph;
37
38//===----------------------------------------------------------------------===//
39// ExplodedGraph "implementation" classes.  These classes are not typed to
40// contain a specific kind of state.  Typed-specialized versions are defined
41// on top of these classes.
42//===----------------------------------------------------------------------===//
43
44class ExplodedNode : public llvm::FoldingSetNode {
45  friend class ExplodedGraph;
46  friend class GRCoreEngine;
47  friend class GRStmtNodeBuilder;
48  friend class GRBranchNodeBuilder;
49  friend class GRIndirectGotoNodeBuilder;
50  friend class GRSwitchNodeBuilder;
51  friend class GREndPathNodeBuilder;
52
53  class NodeGroup {
54    enum { Size1 = 0x0, SizeOther = 0x1, AuxFlag = 0x2, Mask = 0x3 };
55    uintptr_t P;
56
57    unsigned getKind() const {
58      return P & 0x1;
59    }
60
61    void* getPtr() const {
62      assert (!getFlag());
63      return reinterpret_cast<void*>(P & ~Mask);
64    }
65
66    ExplodedNode *getNode() const {
67      return reinterpret_cast<ExplodedNode*>(getPtr());
68    }
69
70  public:
71    NodeGroup() : P(0) {}
72
73    ExplodedNode **begin() const;
74
75    ExplodedNode **end() const;
76
77    unsigned size() const;
78
79    bool empty() const { return (P & ~Mask) == 0; }
80
81    void addNode(ExplodedNode* N, ExplodedGraph &G);
82
83    void setFlag() {
84      assert(P == 0);
85      P = AuxFlag;
86    }
87
88    bool getFlag() const {
89      return P & AuxFlag ? true : false;
90    }
91  };
92
93  /// Location - The program location (within a function body) associated
94  ///  with this node.
95  const ProgramPoint Location;
96
97  /// State - The state associated with this node.
98  const GRState* State;
99
100  /// Preds - The predecessors of this node.
101  NodeGroup Preds;
102
103  /// Succs - The successors of this node.
104  NodeGroup Succs;
105
106public:
107
108  explicit ExplodedNode(const ProgramPoint& loc, const GRState* state)
109    : Location(loc), State(state) {}
110
111  /// getLocation - Returns the edge associated with the given node.
112  ProgramPoint getLocation() const { return Location; }
113
114  const LocationContext *getLocationContext() const {
115    return getLocation().getLocationContext();
116  }
117
118  const Decl &getCodeDecl() const { return *getLocationContext()->getDecl(); }
119
120  CFG &getCFG() const { return *getLocationContext()->getCFG(); }
121
122  ParentMap &getParentMap() const {return getLocationContext()->getParentMap();}
123
124  LiveVariables &getLiveVariables() const {
125    return *getLocationContext()->getLiveVariables();
126  }
127
128  const GRState* getState() const { return State; }
129
130  template <typename T>
131  const T* getLocationAs() const { return llvm::dyn_cast<T>(&Location); }
132
133  static void Profile(llvm::FoldingSetNodeID &ID,
134                      const ProgramPoint& Loc, const GRState* state) {
135    ID.Add(Loc);
136    ID.AddPointer(state);
137  }
138
139  void Profile(llvm::FoldingSetNodeID& ID) const {
140    Profile(ID, getLocation(), getState());
141  }
142
143  /// addPredeccessor - Adds a predecessor to the current node, and
144  ///  in tandem add this node as a successor of the other node.
145  void addPredecessor(ExplodedNode* V, ExplodedGraph &G);
146
147  unsigned succ_size() const { return Succs.size(); }
148  unsigned pred_size() const { return Preds.size(); }
149  bool succ_empty() const { return Succs.empty(); }
150  bool pred_empty() const { return Preds.empty(); }
151
152  bool isSink() const { return Succs.getFlag(); }
153  void markAsSink() { Succs.setFlag(); }
154
155  ExplodedNode* getFirstPred() {
156    return pred_empty() ? NULL : *(pred_begin());
157  }
158
159  const ExplodedNode* getFirstPred() const {
160    return const_cast<ExplodedNode*>(this)->getFirstPred();
161  }
162
163  // Iterators over successor and predecessor vertices.
164  typedef ExplodedNode**       succ_iterator;
165  typedef const ExplodedNode* const * const_succ_iterator;
166  typedef ExplodedNode**       pred_iterator;
167  typedef const ExplodedNode* const * const_pred_iterator;
168
169  pred_iterator pred_begin() { return Preds.begin(); }
170  pred_iterator pred_end() { return Preds.end(); }
171
172  const_pred_iterator pred_begin() const {
173    return const_cast<ExplodedNode*>(this)->pred_begin();
174  }
175  const_pred_iterator pred_end() const {
176    return const_cast<ExplodedNode*>(this)->pred_end();
177  }
178
179  succ_iterator succ_begin() { return Succs.begin(); }
180  succ_iterator succ_end() { return Succs.end(); }
181
182  const_succ_iterator succ_begin() const {
183    return const_cast<ExplodedNode*>(this)->succ_begin();
184  }
185  const_succ_iterator succ_end() const {
186    return const_cast<ExplodedNode*>(this)->succ_end();
187  }
188
189  // For debugging.
190
191public:
192
193  class Auditor {
194  public:
195    virtual ~Auditor();
196    virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst) = 0;
197  };
198
199  static void SetAuditor(Auditor* A);
200};
201
202// FIXME: Is this class necessary?
203class InterExplodedGraphMap {
204  llvm::DenseMap<const ExplodedNode*, ExplodedNode*> M;
205  friend class ExplodedGraph;
206
207public:
208  ExplodedNode* getMappedNode(const ExplodedNode* N) const;
209
210  InterExplodedGraphMap() {};
211  virtual ~InterExplodedGraphMap() {}
212};
213
214class ExplodedGraph {
215protected:
216  friend class GRCoreEngine;
217
218  // Type definitions.
219  typedef llvm::SmallVector<ExplodedNode*,2>    RootsTy;
220  typedef llvm::SmallVector<ExplodedNode*,10>   EndNodesTy;
221
222  /// Roots - The roots of the simulation graph. Usually there will be only
223  /// one, but clients are free to establish multiple subgraphs within a single
224  /// SimulGraph. Moreover, these subgraphs can often merge when paths from
225  /// different roots reach the same state at the same program location.
226  RootsTy Roots;
227
228  /// EndNodes - The nodes in the simulation graph which have been
229  ///  specially marked as the endpoint of an abstract simulation path.
230  EndNodesTy EndNodes;
231
232  /// Nodes - The nodes in the graph.
233  llvm::FoldingSet<ExplodedNode> Nodes;
234
235  /// BVC - Allocator and context for allocating nodes and their predecessor
236  /// and successor groups.
237  BumpVectorContext BVC;
238
239  /// Ctx - The ASTContext used to "interpret" CodeDecl.
240  ASTContext& Ctx;
241
242  /// NumNodes - The number of nodes in the graph.
243  unsigned NumNodes;
244
245public:
246  /// getNode - Retrieve the node associated with a (Location,State) pair,
247  ///  where the 'Location' is a ProgramPoint in the CFG.  If no node for
248  ///  this pair exists, it is created.  IsNew is set to true if
249  ///  the node was freshly created.
250
251  ExplodedNode* getNode(const ProgramPoint& L, const GRState *State,
252                        bool* IsNew = 0);
253
254  ExplodedGraph* MakeEmptyGraph() const {
255    return new ExplodedGraph(Ctx);
256  }
257
258  /// addRoot - Add an untyped node to the set of roots.
259  ExplodedNode* addRoot(ExplodedNode* V) {
260    Roots.push_back(V);
261    return V;
262  }
263
264  /// addEndOfPath - Add an untyped node to the set of EOP nodes.
265  ExplodedNode* addEndOfPath(ExplodedNode* V) {
266    EndNodes.push_back(V);
267    return V;
268  }
269
270  ExplodedGraph(ASTContext& ctx) : Ctx(ctx), NumNodes(0) {}
271
272  ~ExplodedGraph() {}
273
274  unsigned num_roots() const { return Roots.size(); }
275  unsigned num_eops() const { return EndNodes.size(); }
276
277  bool empty() const { return NumNodes == 0; }
278  unsigned size() const { return NumNodes; }
279
280  // Iterators.
281  typedef ExplodedNode                        NodeTy;
282  typedef llvm::FoldingSet<ExplodedNode>      AllNodesTy;
283  typedef NodeTy**                            roots_iterator;
284  typedef NodeTy* const *                     const_roots_iterator;
285  typedef NodeTy**                            eop_iterator;
286  typedef NodeTy* const *                     const_eop_iterator;
287  typedef AllNodesTy::iterator                node_iterator;
288  typedef AllNodesTy::const_iterator          const_node_iterator;
289
290  node_iterator nodes_begin() { return Nodes.begin(); }
291
292  node_iterator nodes_end() { return Nodes.end(); }
293
294  const_node_iterator nodes_begin() const { return Nodes.begin(); }
295
296  const_node_iterator nodes_end() const { return Nodes.end(); }
297
298  roots_iterator roots_begin() { return Roots.begin(); }
299
300  roots_iterator roots_end() { return Roots.end(); }
301
302  const_roots_iterator roots_begin() const { return Roots.begin(); }
303
304  const_roots_iterator roots_end() const { return Roots.end(); }
305
306  eop_iterator eop_begin() { return EndNodes.begin(); }
307
308  eop_iterator eop_end() { return EndNodes.end(); }
309
310  const_eop_iterator eop_begin() const { return EndNodes.begin(); }
311
312  const_eop_iterator eop_end() const { return EndNodes.end(); }
313
314  llvm::BumpPtrAllocator & getAllocator() { return BVC.getAllocator(); }
315  BumpVectorContext &getNodeAllocator() { return BVC; }
316
317  ASTContext& getContext() { return Ctx; }
318
319  typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> NodeMap;
320
321  std::pair<ExplodedGraph*, InterExplodedGraphMap*>
322  Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd,
323       llvm::DenseMap<const void*, const void*> *InverseMap = 0) const;
324
325  ExplodedGraph* TrimInternal(const ExplodedNode* const * NBeg,
326                              const ExplodedNode* const * NEnd,
327                              InterExplodedGraphMap *M,
328                    llvm::DenseMap<const void*, const void*> *InverseMap) const;
329};
330
331class ExplodedNodeSet {
332  typedef llvm::SmallPtrSet<ExplodedNode*,5> ImplTy;
333  ImplTy Impl;
334
335public:
336  ExplodedNodeSet(ExplodedNode* N) {
337    assert (N && !static_cast<ExplodedNode*>(N)->isSink());
338    Impl.insert(N);
339  }
340
341  ExplodedNodeSet() {}
342
343  inline void Add(ExplodedNode* N) {
344    if (N && !static_cast<ExplodedNode*>(N)->isSink()) Impl.insert(N);
345  }
346
347  ExplodedNodeSet& operator=(const ExplodedNodeSet &X) {
348    Impl = X.Impl;
349    return *this;
350  }
351
352  typedef ImplTy::iterator       iterator;
353  typedef ImplTy::const_iterator const_iterator;
354
355  inline unsigned size() const { return Impl.size();  }
356  inline bool empty()    const { return Impl.empty(); }
357
358  inline void clear() { Impl.clear(); }
359
360  inline iterator begin() { return Impl.begin(); }
361  inline iterator end()   { return Impl.end();   }
362
363  inline const_iterator begin() const { return Impl.begin(); }
364  inline const_iterator end()   const { return Impl.end();   }
365};
366
367} // end clang namespace
368
369// GraphTraits
370
371namespace llvm {
372  template<> struct GraphTraits<clang::ExplodedNode*> {
373    typedef clang::ExplodedNode NodeType;
374    typedef NodeType::succ_iterator  ChildIteratorType;
375    typedef llvm::df_iterator<NodeType*>      nodes_iterator;
376
377    static inline NodeType* getEntryNode(NodeType* N) {
378      return N;
379    }
380
381    static inline ChildIteratorType child_begin(NodeType* N) {
382      return N->succ_begin();
383    }
384
385    static inline ChildIteratorType child_end(NodeType* N) {
386      return N->succ_end();
387    }
388
389    static inline nodes_iterator nodes_begin(NodeType* N) {
390      return df_begin(N);
391    }
392
393    static inline nodes_iterator nodes_end(NodeType* N) {
394      return df_end(N);
395    }
396  };
397
398  template<> struct GraphTraits<const clang::ExplodedNode*> {
399    typedef const clang::ExplodedNode NodeType;
400    typedef NodeType::const_succ_iterator   ChildIteratorType;
401    typedef llvm::df_iterator<NodeType*>       nodes_iterator;
402
403    static inline NodeType* getEntryNode(NodeType* N) {
404      return N;
405    }
406
407    static inline ChildIteratorType child_begin(NodeType* N) {
408      return N->succ_begin();
409    }
410
411    static inline ChildIteratorType child_end(NodeType* N) {
412      return N->succ_end();
413    }
414
415    static inline nodes_iterator nodes_begin(NodeType* N) {
416      return df_begin(N);
417    }
418
419    static inline nodes_iterator nodes_end(NodeType* N) {
420      return df_end(N);
421    }
422  };
423
424} // end llvm namespace
425
426#endif
427