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