CFG.h revision 8599e7677e067fd01d3b2ee4c0875747d367fd8e
1//===--- CFG.h - Classes for representing and building CFGs------*- 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 CFG and CFGBuilder classes for representing and 11// building Control-Flow Graphs (CFGs) from ASTs. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_CFG_H 16#define LLVM_CLANG_CFG_H 17 18#include "llvm/ADT/PointerIntPair.h" 19#include "llvm/ADT/GraphTraits.h" 20#include "llvm/Support/Allocator.h" 21#include "llvm/Support/Casting.h" 22#include "clang/Analysis/Support/BumpVector.h" 23#include "clang/Basic/SourceLocation.h" 24#include <cassert> 25 26namespace llvm { 27 class raw_ostream; 28} 29 30namespace clang { 31 class Decl; 32 class Stmt; 33 class Expr; 34 class FieldDecl; 35 class VarDecl; 36 class CXXBaseOrMemberInitializer; 37 class CXXBaseSpecifier; 38 class CXXBindTemporaryExpr; 39 class CFG; 40 class PrinterHelper; 41 class LangOptions; 42 class ASTContext; 43 44/// CFGElement - Represents a top-level expression in a basic block. 45class CFGElement { 46public: 47 enum Kind { 48 // main kind 49 Statement, 50 StatementAsLValue, 51 Initializer, 52 Dtor, 53 // dtor kind 54 AutomaticObjectDtor, 55 BaseDtor, 56 MemberDtor, 57 TemporaryDtor, 58 DTOR_BEGIN = AutomaticObjectDtor 59 }; 60 61protected: 62 // The int bits are used to mark the main kind. 63 llvm::PointerIntPair<void *, 2> Data1; 64 // The int bits are used to mark the dtor kind. 65 llvm::PointerIntPair<void *, 2> Data2; 66 67 CFGElement(void *Ptr, unsigned Int) : Data1(Ptr, Int) {} 68 CFGElement(void *Ptr1, unsigned Int1, void *Ptr2, unsigned Int2) 69 : Data1(Ptr1, Int1), Data2(Ptr2, Int2) {} 70 71public: 72 CFGElement() {} 73 74 Kind getKind() const { return static_cast<Kind>(Data1.getInt()); } 75 76 Kind getDtorKind() const { 77 assert(getKind() == Dtor); 78 return static_cast<Kind>(Data2.getInt() + DTOR_BEGIN); 79 } 80 81 bool isValid() const { return Data1.getPointer(); } 82 83 operator bool() const { return isValid(); } 84 85 template<class ElemTy> ElemTy getAs() const { 86 if (llvm::isa<ElemTy>(this)) 87 return *static_cast<const ElemTy*>(this); 88 return ElemTy(); 89 } 90 91 static bool classof(const CFGElement *E) { return true; } 92}; 93 94class CFGStmt : public CFGElement { 95public: 96 CFGStmt() {} 97 CFGStmt(Stmt *S, bool asLValue) : CFGElement(S, asLValue) {} 98 99 Stmt *getStmt() const { return static_cast<Stmt *>(Data1.getPointer()); } 100 101 operator Stmt*() const { return getStmt(); } 102 103 bool asLValue() const { 104 return static_cast<Kind>(Data1.getInt()) == StatementAsLValue; 105 } 106 107 static bool classof(const CFGElement *E) { 108 return E->getKind() == Statement || E->getKind() == StatementAsLValue; 109 } 110}; 111 112/// CFGInitializer - Represents C++ base or member initializer from 113/// constructor's initialization list. 114class CFGInitializer : public CFGElement { 115public: 116 CFGInitializer() {} 117 CFGInitializer(CXXBaseOrMemberInitializer* I) 118 : CFGElement(I, Initializer) {} 119 120 CXXBaseOrMemberInitializer* getInitializer() const { 121 return static_cast<CXXBaseOrMemberInitializer*>(Data1.getPointer()); 122 } 123 operator CXXBaseOrMemberInitializer*() const { return getInitializer(); } 124 125 static bool classof(const CFGElement *E) { 126 return E->getKind() == Initializer; 127 } 128}; 129 130/// CFGImplicitDtor - Represents C++ object destructor implicitly generated 131/// by compiler on various occasions. 132class CFGImplicitDtor : public CFGElement { 133protected: 134 CFGImplicitDtor(unsigned K, void* P, void* S) 135 : CFGElement(P, Dtor, S, K - DTOR_BEGIN) {} 136 137public: 138 CFGImplicitDtor() {} 139 140 static bool classof(const CFGElement *E) { 141 return E->getKind() == Dtor; 142 } 143}; 144 145/// CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated 146/// for automatic object or temporary bound to const reference at the point 147/// of leaving its local scope. 148class CFGAutomaticObjDtor: public CFGImplicitDtor { 149public: 150 CFGAutomaticObjDtor() {} 151 CFGAutomaticObjDtor(VarDecl* VD, Stmt* S) 152 : CFGImplicitDtor(AutomaticObjectDtor, VD, S) {} 153 154 VarDecl* getVarDecl() const { 155 return static_cast<VarDecl*>(Data1.getPointer()); 156 } 157 158 // Get statement end of which triggered the destructor call. 159 Stmt* getTriggerStmt() const { 160 return static_cast<Stmt*>(Data2.getPointer()); 161 } 162 163 static bool classof(const CFGElement *E) { 164 return E->getKind() == Dtor && E->getDtorKind() == AutomaticObjectDtor; 165 } 166}; 167 168/// CFGBaseDtor - Represents C++ object destructor implicitly generated for 169/// base object in destructor. 170class CFGBaseDtor : public CFGImplicitDtor { 171public: 172 CFGBaseDtor() {} 173 CFGBaseDtor(const CXXBaseSpecifier *BS) 174 : CFGImplicitDtor(BaseDtor, const_cast<CXXBaseSpecifier*>(BS), NULL) {} 175 176 const CXXBaseSpecifier *getBaseSpecifier() const { 177 return static_cast<const CXXBaseSpecifier*>(Data1.getPointer()); 178 } 179 180 static bool classof(const CFGElement *E) { 181 return E->getKind() == Dtor && E->getDtorKind() == BaseDtor; 182 } 183}; 184 185/// CFGMemberDtor - Represents C++ object destructor implicitly generated for 186/// member object in destructor. 187class CFGMemberDtor : public CFGImplicitDtor { 188public: 189 CFGMemberDtor() {} 190 CFGMemberDtor(FieldDecl *FD) 191 : CFGImplicitDtor(MemberDtor, FD, NULL) {} 192 193 FieldDecl *getFieldDecl() const { 194 return static_cast<FieldDecl*>(Data1.getPointer()); 195 } 196 197 static bool classof(const CFGElement *E) { 198 return E->getKind() == Dtor && E->getDtorKind() == MemberDtor; 199 } 200}; 201 202/// CFGTemporaryDtor - Represents C++ object destructor implicitly generated 203/// at the end of full expression for temporary object. 204class CFGTemporaryDtor : public CFGImplicitDtor { 205public: 206 CFGTemporaryDtor() {} 207 CFGTemporaryDtor(CXXBindTemporaryExpr *E) 208 : CFGImplicitDtor(TemporaryDtor, E, NULL) {} 209 210 CXXBindTemporaryExpr *getBindTemporaryExpr() const { 211 return static_cast<CXXBindTemporaryExpr *>(Data1.getPointer()); 212 } 213 214 static bool classof(const CFGElement *E) { 215 return E->getKind() == Dtor && E->getDtorKind() == TemporaryDtor; 216 } 217}; 218 219/// CFGTerminator - Represents CFGBlock terminator statement. 220/// 221/// TemporaryDtorsBranch bit is set to true if the terminator marks a branch 222/// in control flow of destructors of temporaries. In this case terminator 223/// statement is the same statement that branches control flow in evaluation 224/// of matching full expression. 225class CFGTerminator { 226 llvm::PointerIntPair<Stmt *, 1> Data; 227public: 228 CFGTerminator() {} 229 CFGTerminator(Stmt *S, bool TemporaryDtorsBranch = false) 230 : Data(S, TemporaryDtorsBranch) {} 231 232 Stmt *getStmt() { return Data.getPointer(); } 233 const Stmt *getStmt() const { return Data.getPointer(); } 234 235 bool isTemporaryDtorsBranch() const { return Data.getInt(); } 236 237 operator Stmt *() { return getStmt(); } 238 operator const Stmt *() const { return getStmt(); } 239 240 Stmt *operator->() { return getStmt(); } 241 const Stmt *operator->() const { return getStmt(); } 242 243 Stmt &operator*() { return *getStmt(); } 244 const Stmt &operator*() const { return *getStmt(); } 245 246 operator bool() const { return getStmt(); } 247}; 248 249/// CFGBlock - Represents a single basic block in a source-level CFG. 250/// It consists of: 251/// 252/// (1) A set of statements/expressions (which may contain subexpressions). 253/// (2) A "terminator" statement (not in the set of statements). 254/// (3) A list of successors and predecessors. 255/// 256/// Terminator: The terminator represents the type of control-flow that occurs 257/// at the end of the basic block. The terminator is a Stmt* referring to an 258/// AST node that has control-flow: if-statements, breaks, loops, etc. 259/// If the control-flow is conditional, the condition expression will appear 260/// within the set of statements in the block (usually the last statement). 261/// 262/// Predecessors: the order in the set of predecessors is arbitrary. 263/// 264/// Successors: the order in the set of successors is NOT arbitrary. We 265/// currently have the following orderings based on the terminator: 266/// 267/// Terminator Successor Ordering 268/// ----------------------------------------------------- 269/// if Then Block; Else Block 270/// ? operator LHS expression; RHS expression 271/// &&, || expression that uses result of && or ||, RHS 272/// 273class CFGBlock { 274 class ElementList { 275 typedef BumpVector<CFGElement> ImplTy; 276 ImplTy Impl; 277 public: 278 ElementList(BumpVectorContext &C) : Impl(C, 4) {} 279 280 typedef std::reverse_iterator<ImplTy::iterator> iterator; 281 typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator; 282 typedef ImplTy::iterator reverse_iterator; 283 typedef ImplTy::const_iterator const_reverse_iterator; 284 285 void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); } 286 reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E, 287 BumpVectorContext& C) { 288 return Impl.insert(I, Cnt, E, C); 289 } 290 291 CFGElement front() const { return Impl.back(); } 292 CFGElement back() const { return Impl.front(); } 293 294 iterator begin() { return Impl.rbegin(); } 295 iterator end() { return Impl.rend(); } 296 const_iterator begin() const { return Impl.rbegin(); } 297 const_iterator end() const { return Impl.rend(); } 298 reverse_iterator rbegin() { return Impl.begin(); } 299 reverse_iterator rend() { return Impl.end(); } 300 const_reverse_iterator rbegin() const { return Impl.begin(); } 301 const_reverse_iterator rend() const { return Impl.end(); } 302 303 CFGElement operator[](size_t i) const { 304 assert(i < Impl.size()); 305 return Impl[Impl.size() - 1 - i]; 306 } 307 308 size_t size() const { return Impl.size(); } 309 bool empty() const { return Impl.empty(); } 310 }; 311 312 /// Stmts - The set of statements in the basic block. 313 ElementList Elements; 314 315 /// Label - An (optional) label that prefixes the executable 316 /// statements in the block. When this variable is non-NULL, it is 317 /// either an instance of LabelStmt, SwitchCase or CXXCatchStmt. 318 Stmt *Label; 319 320 /// Terminator - The terminator for a basic block that 321 /// indicates the type of control-flow that occurs between a block 322 /// and its successors. 323 CFGTerminator Terminator; 324 325 /// LoopTarget - Some blocks are used to represent the "loop edge" to 326 /// the start of a loop from within the loop body. This Stmt* will be 327 /// refer to the loop statement for such blocks (and be null otherwise). 328 const Stmt *LoopTarget; 329 330 /// BlockID - A numerical ID assigned to a CFGBlock during construction 331 /// of the CFG. 332 unsigned BlockID; 333 334 /// Predecessors/Successors - Keep track of the predecessor / successor 335 /// CFG blocks. 336 typedef BumpVector<CFGBlock*> AdjacentBlocks; 337 AdjacentBlocks Preds; 338 AdjacentBlocks Succs; 339 340public: 341 explicit CFGBlock(unsigned blockid, BumpVectorContext &C) 342 : Elements(C), Label(NULL), Terminator(NULL), LoopTarget(NULL), 343 BlockID(blockid), Preds(C, 1), Succs(C, 1) {} 344 ~CFGBlock() {} 345 346 // Statement iterators 347 typedef ElementList::iterator iterator; 348 typedef ElementList::const_iterator const_iterator; 349 typedef ElementList::reverse_iterator reverse_iterator; 350 typedef ElementList::const_reverse_iterator const_reverse_iterator; 351 352 CFGElement front() const { return Elements.front(); } 353 CFGElement back() const { return Elements.back(); } 354 355 iterator begin() { return Elements.begin(); } 356 iterator end() { return Elements.end(); } 357 const_iterator begin() const { return Elements.begin(); } 358 const_iterator end() const { return Elements.end(); } 359 360 reverse_iterator rbegin() { return Elements.rbegin(); } 361 reverse_iterator rend() { return Elements.rend(); } 362 const_reverse_iterator rbegin() const { return Elements.rbegin(); } 363 const_reverse_iterator rend() const { return Elements.rend(); } 364 365 unsigned size() const { return Elements.size(); } 366 bool empty() const { return Elements.empty(); } 367 368 CFGElement operator[](size_t i) const { return Elements[i]; } 369 370 // CFG iterators 371 typedef AdjacentBlocks::iterator pred_iterator; 372 typedef AdjacentBlocks::const_iterator const_pred_iterator; 373 typedef AdjacentBlocks::reverse_iterator pred_reverse_iterator; 374 typedef AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator; 375 376 typedef AdjacentBlocks::iterator succ_iterator; 377 typedef AdjacentBlocks::const_iterator const_succ_iterator; 378 typedef AdjacentBlocks::reverse_iterator succ_reverse_iterator; 379 typedef AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator; 380 381 pred_iterator pred_begin() { return Preds.begin(); } 382 pred_iterator pred_end() { return Preds.end(); } 383 const_pred_iterator pred_begin() const { return Preds.begin(); } 384 const_pred_iterator pred_end() const { return Preds.end(); } 385 386 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); } 387 pred_reverse_iterator pred_rend() { return Preds.rend(); } 388 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); } 389 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); } 390 391 succ_iterator succ_begin() { return Succs.begin(); } 392 succ_iterator succ_end() { return Succs.end(); } 393 const_succ_iterator succ_begin() const { return Succs.begin(); } 394 const_succ_iterator succ_end() const { return Succs.end(); } 395 396 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); } 397 succ_reverse_iterator succ_rend() { return Succs.rend(); } 398 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); } 399 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); } 400 401 unsigned succ_size() const { return Succs.size(); } 402 bool succ_empty() const { return Succs.empty(); } 403 404 unsigned pred_size() const { return Preds.size(); } 405 bool pred_empty() const { return Preds.empty(); } 406 407 408 class FilterOptions { 409 public: 410 FilterOptions() { 411 IgnoreDefaultsWithCoveredEnums = 0; 412 } 413 414 unsigned IgnoreDefaultsWithCoveredEnums : 1; 415 }; 416 417 static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src, 418 const CFGBlock *Dst); 419 420 template <typename IMPL, bool IsPred> 421 class FilteredCFGBlockIterator { 422 private: 423 IMPL I, E; 424 const FilterOptions F; 425 const CFGBlock *From; 426 public: 427 explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e, 428 const CFGBlock *from, 429 const FilterOptions &f) 430 : I(i), E(e), F(f), From(from) {} 431 432 bool hasMore() const { return I != E; } 433 434 FilteredCFGBlockIterator &operator++() { 435 do { ++I; } while (hasMore() && Filter(*I)); 436 return *this; 437 } 438 439 const CFGBlock *operator*() const { return *I; } 440 private: 441 bool Filter(const CFGBlock *To) { 442 return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To); 443 } 444 }; 445 446 typedef FilteredCFGBlockIterator<const_pred_iterator, true> 447 filtered_pred_iterator; 448 449 typedef FilteredCFGBlockIterator<const_succ_iterator, false> 450 filtered_succ_iterator; 451 452 filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const { 453 return filtered_pred_iterator(pred_begin(), pred_end(), this, f); 454 } 455 456 filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const { 457 return filtered_succ_iterator(succ_begin(), succ_end(), this, f); 458 } 459 460 // Manipulation of block contents 461 462 void setTerminator(Stmt* Statement) { Terminator = Statement; } 463 void setLabel(Stmt* Statement) { Label = Statement; } 464 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; } 465 466 CFGTerminator getTerminator() { return Terminator; } 467 const CFGTerminator getTerminator() const { return Terminator; } 468 469 Stmt* getTerminatorCondition(); 470 471 const Stmt* getTerminatorCondition() const { 472 return const_cast<CFGBlock*>(this)->getTerminatorCondition(); 473 } 474 475 const Stmt *getLoopTarget() const { return LoopTarget; } 476 477 bool hasBinaryBranchTerminator() const; 478 479 Stmt* getLabel() { return Label; } 480 const Stmt* getLabel() const { return Label; } 481 482 unsigned getBlockID() const { return BlockID; } 483 484 void dump(const CFG *cfg, const LangOptions &LO) const; 485 void print(llvm::raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const; 486 void printTerminator(llvm::raw_ostream &OS, const LangOptions &LO) const; 487 488 void addSuccessor(CFGBlock* Block, BumpVectorContext &C) { 489 if (Block) 490 Block->Preds.push_back(this, C); 491 Succs.push_back(Block, C); 492 } 493 494 void appendStmt(Stmt* Statement, BumpVectorContext &C, bool asLValue) { 495 Elements.push_back(CFGStmt(Statement, asLValue), C); 496 } 497 498 void appendInitializer(CXXBaseOrMemberInitializer *I, BumpVectorContext& C) { 499 Elements.push_back(CFGInitializer(I), C); 500 } 501 502 void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) { 503 Elements.push_back(CFGBaseDtor(BS), C); 504 } 505 506 void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C) { 507 Elements.push_back(CFGMemberDtor(FD), C); 508 } 509 510 void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C) { 511 Elements.push_back(CFGTemporaryDtor(E), C); 512 } 513 514 // Destructors must be inserted in reversed order. So insertion is in two 515 // steps. First we prepare space for some number of elements, then we insert 516 // the elements beginning at the last position in prepared space. 517 iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt, 518 BumpVectorContext& C) { 519 return iterator(Elements.insert(I.base(), Cnt, CFGElement(), C)); 520 } 521 iterator insertAutomaticObjDtor(iterator I, VarDecl* VD, Stmt* S) { 522 *I = CFGAutomaticObjDtor(VD, S); 523 return ++I; 524 } 525}; 526 527/// CFG - Represents a source-level, intra-procedural CFG that represents the 528/// control-flow of a Stmt. The Stmt can represent an entire function body, 529/// or a single expression. A CFG will always contain one empty block that 530/// represents the Exit point of the CFG. A CFG will also contain a designated 531/// Entry block. The CFG solely represents control-flow; it consists of 532/// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG 533/// was constructed from. 534class CFG { 535public: 536 //===--------------------------------------------------------------------===// 537 // CFG Construction & Manipulation. 538 //===--------------------------------------------------------------------===// 539 540 class BuildOptions { 541 public: 542 bool PruneTriviallyFalseEdges:1; 543 bool AddEHEdges:1; 544 bool AddInitializers:1; 545 bool AddImplicitDtors:1; 546 547 BuildOptions() 548 : PruneTriviallyFalseEdges(true) 549 , AddEHEdges(false) 550 , AddInitializers(false) 551 , AddImplicitDtors(false) {} 552 }; 553 554 /// buildCFG - Builds a CFG from an AST. The responsibility to free the 555 /// constructed CFG belongs to the caller. 556 static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C, 557 BuildOptions BO = BuildOptions()); 558 559 /// createBlock - Create a new block in the CFG. The CFG owns the block; 560 /// the caller should not directly free it. 561 CFGBlock* createBlock(); 562 563 /// setEntry - Set the entry block of the CFG. This is typically used 564 /// only during CFG construction. Most CFG clients expect that the 565 /// entry block has no predecessors and contains no statements. 566 void setEntry(CFGBlock *B) { Entry = B; } 567 568 /// setIndirectGotoBlock - Set the block used for indirect goto jumps. 569 /// This is typically used only during CFG construction. 570 void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; } 571 572 //===--------------------------------------------------------------------===// 573 // Block Iterators 574 //===--------------------------------------------------------------------===// 575 576 typedef BumpVector<CFGBlock*> CFGBlockListTy; 577 typedef CFGBlockListTy::iterator iterator; 578 typedef CFGBlockListTy::const_iterator const_iterator; 579 typedef std::reverse_iterator<iterator> reverse_iterator; 580 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 581 582 CFGBlock& front() { return *Blocks.front(); } 583 CFGBlock& back() { return *Blocks.back(); } 584 585 iterator begin() { return Blocks.begin(); } 586 iterator end() { return Blocks.end(); } 587 const_iterator begin() const { return Blocks.begin(); } 588 const_iterator end() const { return Blocks.end(); } 589 590 reverse_iterator rbegin() { return Blocks.rbegin(); } 591 reverse_iterator rend() { return Blocks.rend(); } 592 const_reverse_iterator rbegin() const { return Blocks.rbegin(); } 593 const_reverse_iterator rend() const { return Blocks.rend(); } 594 595 CFGBlock& getEntry() { return *Entry; } 596 const CFGBlock& getEntry() const { return *Entry; } 597 CFGBlock& getExit() { return *Exit; } 598 const CFGBlock& getExit() const { return *Exit; } 599 600 CFGBlock* getIndirectGotoBlock() { return IndirectGotoBlock; } 601 const CFGBlock* getIndirectGotoBlock() const { return IndirectGotoBlock; } 602 603 //===--------------------------------------------------------------------===// 604 // Member templates useful for various batch operations over CFGs. 605 //===--------------------------------------------------------------------===// 606 607 template <typename CALLBACK> 608 void VisitBlockStmts(CALLBACK& O) const { 609 for (const_iterator I=begin(), E=end(); I != E; ++I) 610 for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end(); 611 BI != BE; ++BI) { 612 if (CFGStmt S = BI->getAs<CFGStmt>()) 613 O(S); 614 } 615 } 616 617 //===--------------------------------------------------------------------===// 618 // CFG Introspection. 619 //===--------------------------------------------------------------------===// 620 621 struct BlkExprNumTy { 622 const signed Idx; 623 explicit BlkExprNumTy(signed idx) : Idx(idx) {} 624 explicit BlkExprNumTy() : Idx(-1) {} 625 operator bool() const { return Idx >= 0; } 626 operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; } 627 }; 628 629 bool isBlkExpr(const Stmt* S) { return getBlkExprNum(S); } 630 BlkExprNumTy getBlkExprNum(const Stmt* S); 631 unsigned getNumBlkExprs(); 632 633 /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which 634 /// start at 0). 635 unsigned getNumBlockIDs() const { return NumBlockIDs; } 636 637 //===--------------------------------------------------------------------===// 638 // CFG Debugging: Pretty-Printing and Visualization. 639 //===--------------------------------------------------------------------===// 640 641 void viewCFG(const LangOptions &LO) const; 642 void print(llvm::raw_ostream& OS, const LangOptions &LO) const; 643 void dump(const LangOptions &LO) const; 644 645 //===--------------------------------------------------------------------===// 646 // Internal: constructors and data. 647 //===--------------------------------------------------------------------===// 648 649 CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0), 650 BlkExprMap(NULL), Blocks(BlkBVC, 10) {} 651 652 ~CFG(); 653 654 llvm::BumpPtrAllocator& getAllocator() { 655 return BlkBVC.getAllocator(); 656 } 657 658 BumpVectorContext &getBumpVectorContext() { 659 return BlkBVC; 660 } 661 662private: 663 CFGBlock* Entry; 664 CFGBlock* Exit; 665 CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch 666 // for indirect gotos 667 unsigned NumBlockIDs; 668 669 // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h. 670 // It represents a map from Expr* to integers to record the set of 671 // block-level expressions and their "statement number" in the CFG. 672 void* BlkExprMap; 673 674 BumpVectorContext BlkBVC; 675 676 CFGBlockListTy Blocks; 677 678}; 679} // end namespace clang 680 681//===----------------------------------------------------------------------===// 682// GraphTraits specializations for CFG basic block graphs (source-level CFGs) 683//===----------------------------------------------------------------------===// 684 685namespace llvm { 686 687/// Implement simplify_type for CFGTerminator, so that we can dyn_cast from 688/// CFGTerminator to a specific Stmt class. 689template <> struct simplify_type<const ::clang::CFGTerminator> { 690 typedef const ::clang::Stmt *SimpleType; 691 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) { 692 return Val.getStmt(); 693 } 694}; 695 696template <> struct simplify_type< ::clang::CFGTerminator> { 697 typedef ::clang::Stmt *SimpleType; 698 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) { 699 return const_cast<SimpleType>(Val.getStmt()); 700 } 701}; 702 703// Traits for: CFGBlock 704 705template <> struct GraphTraits< ::clang::CFGBlock* > { 706 typedef ::clang::CFGBlock NodeType; 707 typedef ::clang::CFGBlock::succ_iterator ChildIteratorType; 708 709 static NodeType* getEntryNode(::clang::CFGBlock* BB) 710 { return BB; } 711 712 static inline ChildIteratorType child_begin(NodeType* N) 713 { return N->succ_begin(); } 714 715 static inline ChildIteratorType child_end(NodeType* N) 716 { return N->succ_end(); } 717}; 718 719template <> struct GraphTraits< const ::clang::CFGBlock* > { 720 typedef const ::clang::CFGBlock NodeType; 721 typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType; 722 723 static NodeType* getEntryNode(const clang::CFGBlock* BB) 724 { return BB; } 725 726 static inline ChildIteratorType child_begin(NodeType* N) 727 { return N->succ_begin(); } 728 729 static inline ChildIteratorType child_end(NodeType* N) 730 { return N->succ_end(); } 731}; 732 733template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > { 734 typedef const ::clang::CFGBlock NodeType; 735 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType; 736 737 static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G) 738 { return G.Graph; } 739 740 static inline ChildIteratorType child_begin(NodeType* N) 741 { return N->pred_begin(); } 742 743 static inline ChildIteratorType child_end(NodeType* N) 744 { return N->pred_end(); } 745}; 746 747// Traits for: CFG 748 749template <> struct GraphTraits< ::clang::CFG* > 750 : public GraphTraits< ::clang::CFGBlock* > { 751 752 typedef ::clang::CFG::iterator nodes_iterator; 753 754 static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); } 755 static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); } 756 static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); } 757}; 758 759template <> struct GraphTraits<const ::clang::CFG* > 760 : public GraphTraits<const ::clang::CFGBlock* > { 761 762 typedef ::clang::CFG::const_iterator nodes_iterator; 763 764 static NodeType *getEntryNode( const ::clang::CFG* F) { 765 return &F->getEntry(); 766 } 767 static nodes_iterator nodes_begin( const ::clang::CFG* F) { 768 return F->begin(); 769 } 770 static nodes_iterator nodes_end( const ::clang::CFG* F) { 771 return F->end(); 772 } 773}; 774 775template <> struct GraphTraits<Inverse<const ::clang::CFG*> > 776 : public GraphTraits<Inverse<const ::clang::CFGBlock*> > { 777 778 typedef ::clang::CFG::const_iterator nodes_iterator; 779 780 static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); } 781 static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();} 782 static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); } 783}; 784} // end llvm namespace 785#endif 786