1 //===--- CFG.h - Classes for representing and building CFGs------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the CFG and CFGBuilder classes for representing and
11 // building Control-Flow Graphs (CFGs) from ASTs.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_CFG_H
16 #define LLVM_CLANG_CFG_H
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 "llvm/ADT/OwningPtr.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/BitVector.h"
25 #include "clang/AST/Stmt.h"
26 #include "clang/Analysis/Support/BumpVector.h"
27 #include "clang/Basic/SourceLocation.h"
32 class CXXDestructorDecl;
38 class CXXCtorInitializer;
39 class CXXBaseSpecifier;
40 class CXXBindTemporaryExpr;
46 /// CFGElement - Represents a top-level expression in a basic block.
59 DTOR_BEGIN = AutomaticObjectDtor,
60 DTOR_END = TemporaryDtor
64 // The int bits are used to mark the kind.
65 llvm::PointerIntPair<void *, 2> Data1;
66 llvm::PointerIntPair<void *, 2> Data2;
68 CFGElement(Kind kind, const void *Ptr1, const void *Ptr2 = 0)
69 : Data1(const_cast<void*>(Ptr1), ((unsigned) kind) & 0x3),
70 Data2(const_cast<void*>(Ptr2), (((unsigned) kind) >> 2) & 0x3) {}
75 Kind getKind() const {
76 unsigned x = Data2.getInt();
82 bool isValid() const { return getKind() != Invalid; }
84 operator bool() const { return isValid(); }
86 template<class ElemTy> const ElemTy *getAs() const {
87 if (llvm::isa<ElemTy>(this))
88 return static_cast<const ElemTy*>(this);
92 static bool classof(const CFGElement *E) { return true; }
95 class CFGStmt : public CFGElement {
97 CFGStmt(Stmt *S) : CFGElement(Statement, S) {}
99 const Stmt *getStmt() const {
100 return static_cast<const Stmt *>(Data1.getPointer());
103 static bool classof(const CFGElement *E) {
104 return E->getKind() == Statement;
108 /// CFGInitializer - Represents C++ base or member initializer from
109 /// constructor's initialization list.
110 class CFGInitializer : public CFGElement {
112 CFGInitializer(CXXCtorInitializer *initializer)
113 : CFGElement(Initializer, initializer) {}
115 CXXCtorInitializer* getInitializer() const {
116 return static_cast<CXXCtorInitializer*>(Data1.getPointer());
119 static bool classof(const CFGElement *E) {
120 return E->getKind() == Initializer;
124 /// CFGImplicitDtor - Represents C++ object destructor implicitly generated
125 /// by compiler on various occasions.
126 class CFGImplicitDtor : public CFGElement {
128 CFGImplicitDtor(Kind kind, const void *data1, const void *data2 = 0)
129 : CFGElement(kind, data1, data2) {
130 assert(kind >= DTOR_BEGIN && kind <= DTOR_END);
134 const CXXDestructorDecl *getDestructorDecl(ASTContext &astContext) const;
135 bool isNoReturn(ASTContext &astContext) const;
137 static bool classof(const CFGElement *E) {
138 Kind kind = E->getKind();
139 return kind >= DTOR_BEGIN && kind <= DTOR_END;
143 /// CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated
144 /// for automatic object or temporary bound to const reference at the point
145 /// of leaving its local scope.
146 class CFGAutomaticObjDtor: public CFGImplicitDtor {
148 CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
149 : CFGImplicitDtor(AutomaticObjectDtor, var, stmt) {}
151 const VarDecl *getVarDecl() const {
152 return static_cast<VarDecl*>(Data1.getPointer());
155 // Get statement end of which triggered the destructor call.
156 const Stmt *getTriggerStmt() const {
157 return static_cast<Stmt*>(Data2.getPointer());
160 static bool classof(const CFGElement *elem) {
161 return elem->getKind() == AutomaticObjectDtor;
165 /// CFGBaseDtor - Represents C++ object destructor implicitly generated for
166 /// base object in destructor.
167 class CFGBaseDtor : public CFGImplicitDtor {
169 CFGBaseDtor(const CXXBaseSpecifier *base)
170 : CFGImplicitDtor(BaseDtor, base) {}
172 const CXXBaseSpecifier *getBaseSpecifier() const {
173 return static_cast<const CXXBaseSpecifier*>(Data1.getPointer());
176 static bool classof(const CFGElement *E) {
177 return E->getKind() == BaseDtor;
181 /// CFGMemberDtor - Represents C++ object destructor implicitly generated for
182 /// member object in destructor.
183 class CFGMemberDtor : public CFGImplicitDtor {
185 CFGMemberDtor(const FieldDecl *field)
186 : CFGImplicitDtor(MemberDtor, field, 0) {}
188 const FieldDecl *getFieldDecl() const {
189 return static_cast<const FieldDecl*>(Data1.getPointer());
192 static bool classof(const CFGElement *E) {
193 return E->getKind() == MemberDtor;
197 /// CFGTemporaryDtor - Represents C++ object destructor implicitly generated
198 /// at the end of full expression for temporary object.
199 class CFGTemporaryDtor : public CFGImplicitDtor {
201 CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
202 : CFGImplicitDtor(TemporaryDtor, expr, 0) {}
204 const CXXBindTemporaryExpr *getBindTemporaryExpr() const {
205 return static_cast<const CXXBindTemporaryExpr *>(Data1.getPointer());
208 static bool classof(const CFGElement *E) {
209 return E->getKind() == TemporaryDtor;
213 /// CFGTerminator - Represents CFGBlock terminator statement.
215 /// TemporaryDtorsBranch bit is set to true if the terminator marks a branch
216 /// in control flow of destructors of temporaries. In this case terminator
217 /// statement is the same statement that branches control flow in evaluation
218 /// of matching full expression.
219 class CFGTerminator {
220 llvm::PointerIntPair<Stmt *, 1> Data;
223 CFGTerminator(Stmt *S, bool TemporaryDtorsBranch = false)
224 : Data(S, TemporaryDtorsBranch) {}
226 Stmt *getStmt() { return Data.getPointer(); }
227 const Stmt *getStmt() const { return Data.getPointer(); }
229 bool isTemporaryDtorsBranch() const { return Data.getInt(); }
231 operator Stmt *() { return getStmt(); }
232 operator const Stmt *() const { return getStmt(); }
234 Stmt *operator->() { return getStmt(); }
235 const Stmt *operator->() const { return getStmt(); }
237 Stmt &operator*() { return *getStmt(); }
238 const Stmt &operator*() const { return *getStmt(); }
240 operator bool() const { return getStmt(); }
243 /// CFGBlock - Represents a single basic block in a source-level CFG.
246 /// (1) A set of statements/expressions (which may contain subexpressions).
247 /// (2) A "terminator" statement (not in the set of statements).
248 /// (3) A list of successors and predecessors.
250 /// Terminator: The terminator represents the type of control-flow that occurs
251 /// at the end of the basic block. The terminator is a Stmt* referring to an
252 /// AST node that has control-flow: if-statements, breaks, loops, etc.
253 /// If the control-flow is conditional, the condition expression will appear
254 /// within the set of statements in the block (usually the last statement).
256 /// Predecessors: the order in the set of predecessors is arbitrary.
258 /// Successors: the order in the set of successors is NOT arbitrary. We
259 /// currently have the following orderings based on the terminator:
261 /// Terminator Successor Ordering
262 /// -----------------------------------------------------
263 /// if Then Block; Else Block
264 /// ? operator LHS expression; RHS expression
265 /// &&, || expression that uses result of && or ||, RHS
267 /// But note that any of that may be NULL in case of optimized-out edges.
271 typedef BumpVector<CFGElement> ImplTy;
274 ElementList(BumpVectorContext &C) : Impl(C, 4) {}
276 typedef std::reverse_iterator<ImplTy::iterator> iterator;
277 typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
278 typedef ImplTy::iterator reverse_iterator;
279 typedef ImplTy::const_iterator const_reverse_iterator;
281 void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
282 reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E,
283 BumpVectorContext &C) {
284 return Impl.insert(I, Cnt, E, C);
287 CFGElement front() const { return Impl.back(); }
288 CFGElement back() const { return Impl.front(); }
290 iterator begin() { return Impl.rbegin(); }
291 iterator end() { return Impl.rend(); }
292 const_iterator begin() const { return Impl.rbegin(); }
293 const_iterator end() const { return Impl.rend(); }
294 reverse_iterator rbegin() { return Impl.begin(); }
295 reverse_iterator rend() { return Impl.end(); }
296 const_reverse_iterator rbegin() const { return Impl.begin(); }
297 const_reverse_iterator rend() const { return Impl.end(); }
299 CFGElement operator[](size_t i) const {
300 assert(i < Impl.size());
301 return Impl[Impl.size() - 1 - i];
304 size_t size() const { return Impl.size(); }
305 bool empty() const { return Impl.empty(); }
308 /// Stmts - The set of statements in the basic block.
309 ElementList Elements;
311 /// Label - An (optional) label that prefixes the executable
312 /// statements in the block. When this variable is non-NULL, it is
313 /// either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
316 /// Terminator - The terminator for a basic block that
317 /// indicates the type of control-flow that occurs between a block
318 /// and its successors.
319 CFGTerminator Terminator;
321 /// LoopTarget - Some blocks are used to represent the "loop edge" to
322 /// the start of a loop from within the loop body. This Stmt* will be
323 /// refer to the loop statement for such blocks (and be null otherwise).
324 const Stmt *LoopTarget;
326 /// BlockID - A numerical ID assigned to a CFGBlock during construction
330 /// Predecessors/Successors - Keep track of the predecessor / successor
332 typedef BumpVector<CFGBlock*> AdjacentBlocks;
333 AdjacentBlocks Preds;
334 AdjacentBlocks Succs;
336 /// NoReturn - This bit is set when the basic block contains a function call
337 /// or implicit destructor that is attributed as 'noreturn'. In that case,
338 /// control cannot technically ever proceed past this block. All such blocks
339 /// will have a single immediate successor: the exit block. This allows them
340 /// to be easily reached from the exit block and using this bit quickly
341 /// recognized without scanning the contents of the block.
343 /// Optimization Note: This bit could be profitably folded with Terminator's
344 /// storage if the memory usage of CFGBlock becomes an issue.
345 unsigned HasNoReturnElement : 1;
348 explicit CFGBlock(unsigned blockid, BumpVectorContext &C)
349 : Elements(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
350 BlockID(blockid), Preds(C, 1), Succs(C, 1), HasNoReturnElement(false) {}
353 // Statement iterators
354 typedef ElementList::iterator iterator;
355 typedef ElementList::const_iterator const_iterator;
356 typedef ElementList::reverse_iterator reverse_iterator;
357 typedef ElementList::const_reverse_iterator const_reverse_iterator;
359 CFGElement front() const { return Elements.front(); }
360 CFGElement back() const { return Elements.back(); }
362 iterator begin() { return Elements.begin(); }
363 iterator end() { return Elements.end(); }
364 const_iterator begin() const { return Elements.begin(); }
365 const_iterator end() const { return Elements.end(); }
367 reverse_iterator rbegin() { return Elements.rbegin(); }
368 reverse_iterator rend() { return Elements.rend(); }
369 const_reverse_iterator rbegin() const { return Elements.rbegin(); }
370 const_reverse_iterator rend() const { return Elements.rend(); }
372 unsigned size() const { return Elements.size(); }
373 bool empty() const { return Elements.empty(); }
375 CFGElement operator[](size_t i) const { return Elements[i]; }
378 typedef AdjacentBlocks::iterator pred_iterator;
379 typedef AdjacentBlocks::const_iterator const_pred_iterator;
380 typedef AdjacentBlocks::reverse_iterator pred_reverse_iterator;
381 typedef AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator;
383 typedef AdjacentBlocks::iterator succ_iterator;
384 typedef AdjacentBlocks::const_iterator const_succ_iterator;
385 typedef AdjacentBlocks::reverse_iterator succ_reverse_iterator;
386 typedef AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator;
388 pred_iterator pred_begin() { return Preds.begin(); }
389 pred_iterator pred_end() { return Preds.end(); }
390 const_pred_iterator pred_begin() const { return Preds.begin(); }
391 const_pred_iterator pred_end() const { return Preds.end(); }
393 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); }
394 pred_reverse_iterator pred_rend() { return Preds.rend(); }
395 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
396 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
398 succ_iterator succ_begin() { return Succs.begin(); }
399 succ_iterator succ_end() { return Succs.end(); }
400 const_succ_iterator succ_begin() const { return Succs.begin(); }
401 const_succ_iterator succ_end() const { return Succs.end(); }
403 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); }
404 succ_reverse_iterator succ_rend() { return Succs.rend(); }
405 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
406 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
408 unsigned succ_size() const { return Succs.size(); }
409 bool succ_empty() const { return Succs.empty(); }
411 unsigned pred_size() const { return Preds.size(); }
412 bool pred_empty() const { return Preds.empty(); }
415 class FilterOptions {
418 IgnoreDefaultsWithCoveredEnums = 0;
421 unsigned IgnoreDefaultsWithCoveredEnums : 1;
424 static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
425 const CFGBlock *Dst);
427 template <typename IMPL, bool IsPred>
428 class FilteredCFGBlockIterator {
431 const FilterOptions F;
432 const CFGBlock *From;
434 explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
435 const CFGBlock *from,
436 const FilterOptions &f)
437 : I(i), E(e), F(f), From(from) {}
439 bool hasMore() const { return I != E; }
441 FilteredCFGBlockIterator &operator++() {
442 do { ++I; } while (hasMore() && Filter(*I));
446 const CFGBlock *operator*() const { return *I; }
448 bool Filter(const CFGBlock *To) {
449 return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
453 typedef FilteredCFGBlockIterator<const_pred_iterator, true>
454 filtered_pred_iterator;
456 typedef FilteredCFGBlockIterator<const_succ_iterator, false>
457 filtered_succ_iterator;
459 filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
460 return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
463 filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
464 return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
467 // Manipulation of block contents
469 void setTerminator(Stmt *Statement) { Terminator = Statement; }
470 void setLabel(Stmt *Statement) { Label = Statement; }
471 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
472 void setHasNoReturnElement() { HasNoReturnElement = true; }
474 CFGTerminator getTerminator() { return Terminator; }
475 const CFGTerminator getTerminator() const { return Terminator; }
477 Stmt *getTerminatorCondition();
479 const Stmt *getTerminatorCondition() const {
480 return const_cast<CFGBlock*>(this)->getTerminatorCondition();
483 const Stmt *getLoopTarget() const { return LoopTarget; }
485 Stmt *getLabel() { return Label; }
486 const Stmt *getLabel() const { return Label; }
488 bool hasNoReturnElement() const { return HasNoReturnElement; }
490 unsigned getBlockID() const { return BlockID; }
492 void dump(const CFG *cfg, const LangOptions &LO) const;
493 void print(raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const;
494 void printTerminator(raw_ostream &OS, const LangOptions &LO) const;
496 void addSuccessor(CFGBlock *Block, BumpVectorContext &C) {
498 Block->Preds.push_back(this, C);
499 Succs.push_back(Block, C);
502 void appendStmt(Stmt *statement, BumpVectorContext &C) {
503 Elements.push_back(CFGStmt(statement), C);
506 void appendInitializer(CXXCtorInitializer *initializer,
507 BumpVectorContext &C) {
508 Elements.push_back(CFGInitializer(initializer), C);
511 void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) {
512 Elements.push_back(CFGBaseDtor(BS), C);
515 void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C) {
516 Elements.push_back(CFGMemberDtor(FD), C);
519 void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C) {
520 Elements.push_back(CFGTemporaryDtor(E), C);
523 void appendAutomaticObjDtor(VarDecl *VD, Stmt *S, BumpVectorContext &C) {
524 Elements.push_back(CFGAutomaticObjDtor(VD, S), C);
527 // Destructors must be inserted in reversed order. So insertion is in two
528 // steps. First we prepare space for some number of elements, then we insert
529 // the elements beginning at the last position in prepared space.
530 iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt,
531 BumpVectorContext &C) {
532 return iterator(Elements.insert(I.base(), Cnt, CFGElement(), C));
534 iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S) {
535 *I = CFGAutomaticObjDtor(VD, S);
540 /// CFG - Represents a source-level, intra-procedural CFG that represents the
541 /// control-flow of a Stmt. The Stmt can represent an entire function body,
542 /// or a single expression. A CFG will always contain one empty block that
543 /// represents the Exit point of the CFG. A CFG will also contain a designated
544 /// Entry block. The CFG solely represents control-flow; it consists of
545 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
546 /// was constructed from.
549 //===--------------------------------------------------------------------===//
550 // CFG Construction & Manipulation.
551 //===--------------------------------------------------------------------===//
554 llvm::BitVector alwaysAddMask;
556 typedef llvm::DenseMap<const Stmt *, const CFGBlock*> ForcedBlkExprs;
557 ForcedBlkExprs **forcedBlkExprs;
559 bool PruneTriviallyFalseEdges;
561 bool AddInitializers;
562 bool AddImplicitDtors;
564 bool alwaysAdd(const Stmt *stmt) const {
565 return alwaysAddMask[stmt->getStmtClass()];
568 BuildOptions &setAlwaysAdd(Stmt::StmtClass stmtClass, bool val = true) {
569 alwaysAddMask[stmtClass] = val;
573 BuildOptions &setAllAlwaysAdd() {
579 : alwaysAddMask(Stmt::lastStmtConstant, false)
580 ,forcedBlkExprs(0), PruneTriviallyFalseEdges(true)
582 ,AddInitializers(false)
583 ,AddImplicitDtors(false) {}
586 /// buildCFG - Builds a CFG from an AST. The responsibility to free the
587 /// constructed CFG belongs to the caller.
588 static CFG* buildCFG(const Decl *D, Stmt *AST, ASTContext *C,
589 const BuildOptions &BO);
591 /// createBlock - Create a new block in the CFG. The CFG owns the block;
592 /// the caller should not directly free it.
593 CFGBlock *createBlock();
595 /// setEntry - Set the entry block of the CFG. This is typically used
596 /// only during CFG construction. Most CFG clients expect that the
597 /// entry block has no predecessors and contains no statements.
598 void setEntry(CFGBlock *B) { Entry = B; }
600 /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
601 /// This is typically used only during CFG construction.
602 void setIndirectGotoBlock(CFGBlock *B) { IndirectGotoBlock = B; }
604 //===--------------------------------------------------------------------===//
606 //===--------------------------------------------------------------------===//
608 typedef BumpVector<CFGBlock*> CFGBlockListTy;
609 typedef CFGBlockListTy::iterator iterator;
610 typedef CFGBlockListTy::const_iterator const_iterator;
611 typedef std::reverse_iterator<iterator> reverse_iterator;
612 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
614 CFGBlock & front() { return *Blocks.front(); }
615 CFGBlock & back() { return *Blocks.back(); }
617 iterator begin() { return Blocks.begin(); }
618 iterator end() { return Blocks.end(); }
619 const_iterator begin() const { return Blocks.begin(); }
620 const_iterator end() const { return Blocks.end(); }
622 reverse_iterator rbegin() { return Blocks.rbegin(); }
623 reverse_iterator rend() { return Blocks.rend(); }
624 const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
625 const_reverse_iterator rend() const { return Blocks.rend(); }
627 CFGBlock & getEntry() { return *Entry; }
628 const CFGBlock & getEntry() const { return *Entry; }
629 CFGBlock & getExit() { return *Exit; }
630 const CFGBlock & getExit() const { return *Exit; }
632 CFGBlock * getIndirectGotoBlock() { return IndirectGotoBlock; }
633 const CFGBlock * getIndirectGotoBlock() const { return IndirectGotoBlock; }
635 typedef std::vector<const CFGBlock*>::const_iterator try_block_iterator;
636 try_block_iterator try_blocks_begin() const {
637 return TryDispatchBlocks.begin();
639 try_block_iterator try_blocks_end() const {
640 return TryDispatchBlocks.end();
643 void addTryDispatchBlock(const CFGBlock *block) {
644 TryDispatchBlocks.push_back(block);
647 //===--------------------------------------------------------------------===//
648 // Member templates useful for various batch operations over CFGs.
649 //===--------------------------------------------------------------------===//
651 template <typename CALLBACK>
652 void VisitBlockStmts(CALLBACK& O) const {
653 for (const_iterator I=begin(), E=end(); I != E; ++I)
654 for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
656 if (const CFGStmt *stmt = BI->getAs<CFGStmt>())
657 O(const_cast<Stmt*>(stmt->getStmt()));
661 //===--------------------------------------------------------------------===//
662 // CFG Introspection.
663 //===--------------------------------------------------------------------===//
665 struct BlkExprNumTy {
667 explicit BlkExprNumTy(signed idx) : Idx(idx) {}
668 explicit BlkExprNumTy() : Idx(-1) {}
669 operator bool() const { return Idx >= 0; }
670 operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
673 bool isBlkExpr(const Stmt *S) { return getBlkExprNum(S); }
674 bool isBlkExpr(const Stmt *S) const {
675 return const_cast<CFG*>(this)->isBlkExpr(S);
677 BlkExprNumTy getBlkExprNum(const Stmt *S);
678 unsigned getNumBlkExprs();
680 /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
682 unsigned getNumBlockIDs() const { return NumBlockIDs; }
684 //===--------------------------------------------------------------------===//
685 // CFG Debugging: Pretty-Printing and Visualization.
686 //===--------------------------------------------------------------------===//
688 void viewCFG(const LangOptions &LO) const;
689 void print(raw_ostream &OS, const LangOptions &LO) const;
690 void dump(const LangOptions &LO) const;
692 //===--------------------------------------------------------------------===//
693 // Internal: constructors and data.
694 //===--------------------------------------------------------------------===//
696 CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
697 BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
701 llvm::BumpPtrAllocator& getAllocator() {
702 return BlkBVC.getAllocator();
705 BumpVectorContext &getBumpVectorContext() {
712 CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch
713 // for indirect gotos
714 unsigned NumBlockIDs;
716 // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
717 // It represents a map from Expr* to integers to record the set of
718 // block-level expressions and their "statement number" in the CFG.
721 BumpVectorContext BlkBVC;
723 CFGBlockListTy Blocks;
725 /// C++ 'try' statements are modeled with an indirect dispatch block.
726 /// This is the collection of such blocks present in the CFG.
727 std::vector<const CFGBlock *> TryDispatchBlocks;
730 } // end namespace clang
732 //===----------------------------------------------------------------------===//
733 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
734 //===----------------------------------------------------------------------===//
738 /// Implement simplify_type for CFGTerminator, so that we can dyn_cast from
739 /// CFGTerminator to a specific Stmt class.
740 template <> struct simplify_type<const ::clang::CFGTerminator> {
741 typedef const ::clang::Stmt *SimpleType;
742 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
743 return Val.getStmt();
747 template <> struct simplify_type< ::clang::CFGTerminator> {
748 typedef ::clang::Stmt *SimpleType;
749 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
750 return const_cast<SimpleType>(Val.getStmt());
754 // Traits for: CFGBlock
756 template <> struct GraphTraits< ::clang::CFGBlock *> {
757 typedef ::clang::CFGBlock NodeType;
758 typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
760 static NodeType* getEntryNode(::clang::CFGBlock *BB)
763 static inline ChildIteratorType child_begin(NodeType* N)
764 { return N->succ_begin(); }
766 static inline ChildIteratorType child_end(NodeType* N)
767 { return N->succ_end(); }
770 template <> struct GraphTraits< const ::clang::CFGBlock *> {
771 typedef const ::clang::CFGBlock NodeType;
772 typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
774 static NodeType* getEntryNode(const clang::CFGBlock *BB)
777 static inline ChildIteratorType child_begin(NodeType* N)
778 { return N->succ_begin(); }
780 static inline ChildIteratorType child_end(NodeType* N)
781 { return N->succ_end(); }
784 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
785 typedef const ::clang::CFGBlock NodeType;
786 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
788 static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
791 static inline ChildIteratorType child_begin(NodeType* N)
792 { return N->pred_begin(); }
794 static inline ChildIteratorType child_end(NodeType* N)
795 { return N->pred_end(); }
800 template <> struct GraphTraits< ::clang::CFG* >
801 : public GraphTraits< ::clang::CFGBlock *> {
803 typedef ::clang::CFG::iterator nodes_iterator;
805 static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
806 static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); }
807 static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); }
810 template <> struct GraphTraits<const ::clang::CFG* >
811 : public GraphTraits<const ::clang::CFGBlock *> {
813 typedef ::clang::CFG::const_iterator nodes_iterator;
815 static NodeType *getEntryNode( const ::clang::CFG* F) {
816 return &F->getEntry();
818 static nodes_iterator nodes_begin( const ::clang::CFG* F) {
821 static nodes_iterator nodes_end( const ::clang::CFG* F) {
826 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
827 : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
829 typedef ::clang::CFG::const_iterator nodes_iterator;
831 static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
832 static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();}
833 static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); }
835 } // end llvm namespace