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 "clang/AST/Stmt.h"
25 #include "clang/Analysis/Support/BumpVector.h"
26 #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);
93 class CFGStmt : public CFGElement {
95 CFGStmt(Stmt *S) : CFGElement(Statement, S) {}
97 const Stmt *getStmt() const {
98 return static_cast<const Stmt *>(Data1.getPointer());
101 static bool classof(const CFGElement *E) {
102 return E->getKind() == Statement;
106 /// CFGInitializer - Represents C++ base or member initializer from
107 /// constructor's initialization list.
108 class CFGInitializer : public CFGElement {
110 CFGInitializer(CXXCtorInitializer *initializer)
111 : CFGElement(Initializer, initializer) {}
113 CXXCtorInitializer* getInitializer() const {
114 return static_cast<CXXCtorInitializer*>(Data1.getPointer());
117 static bool classof(const CFGElement *E) {
118 return E->getKind() == Initializer;
122 /// CFGImplicitDtor - Represents C++ object destructor implicitly generated
123 /// by compiler on various occasions.
124 class CFGImplicitDtor : public CFGElement {
126 CFGImplicitDtor(Kind kind, const void *data1, const void *data2 = 0)
127 : CFGElement(kind, data1, data2) {
128 assert(kind >= DTOR_BEGIN && kind <= DTOR_END);
132 const CXXDestructorDecl *getDestructorDecl(ASTContext &astContext) const;
133 bool isNoReturn(ASTContext &astContext) const;
135 static bool classof(const CFGElement *E) {
136 Kind kind = E->getKind();
137 return kind >= DTOR_BEGIN && kind <= DTOR_END;
141 /// CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated
142 /// for automatic object or temporary bound to const reference at the point
143 /// of leaving its local scope.
144 class CFGAutomaticObjDtor: public CFGImplicitDtor {
146 CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
147 : CFGImplicitDtor(AutomaticObjectDtor, var, stmt) {}
149 const VarDecl *getVarDecl() const {
150 return static_cast<VarDecl*>(Data1.getPointer());
153 // Get statement end of which triggered the destructor call.
154 const Stmt *getTriggerStmt() const {
155 return static_cast<Stmt*>(Data2.getPointer());
158 static bool classof(const CFGElement *elem) {
159 return elem->getKind() == AutomaticObjectDtor;
163 /// CFGBaseDtor - Represents C++ object destructor implicitly generated for
164 /// base object in destructor.
165 class CFGBaseDtor : public CFGImplicitDtor {
167 CFGBaseDtor(const CXXBaseSpecifier *base)
168 : CFGImplicitDtor(BaseDtor, base) {}
170 const CXXBaseSpecifier *getBaseSpecifier() const {
171 return static_cast<const CXXBaseSpecifier*>(Data1.getPointer());
174 static bool classof(const CFGElement *E) {
175 return E->getKind() == BaseDtor;
179 /// CFGMemberDtor - Represents C++ object destructor implicitly generated for
180 /// member object in destructor.
181 class CFGMemberDtor : public CFGImplicitDtor {
183 CFGMemberDtor(const FieldDecl *field)
184 : CFGImplicitDtor(MemberDtor, field, 0) {}
186 const FieldDecl *getFieldDecl() const {
187 return static_cast<const FieldDecl*>(Data1.getPointer());
190 static bool classof(const CFGElement *E) {
191 return E->getKind() == MemberDtor;
195 /// CFGTemporaryDtor - Represents C++ object destructor implicitly generated
196 /// at the end of full expression for temporary object.
197 class CFGTemporaryDtor : public CFGImplicitDtor {
199 CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
200 : CFGImplicitDtor(TemporaryDtor, expr, 0) {}
202 const CXXBindTemporaryExpr *getBindTemporaryExpr() const {
203 return static_cast<const CXXBindTemporaryExpr *>(Data1.getPointer());
206 static bool classof(const CFGElement *E) {
207 return E->getKind() == TemporaryDtor;
211 /// CFGTerminator - Represents CFGBlock terminator statement.
213 /// TemporaryDtorsBranch bit is set to true if the terminator marks a branch
214 /// in control flow of destructors of temporaries. In this case terminator
215 /// statement is the same statement that branches control flow in evaluation
216 /// of matching full expression.
217 class CFGTerminator {
218 llvm::PointerIntPair<Stmt *, 1> Data;
221 CFGTerminator(Stmt *S, bool TemporaryDtorsBranch = false)
222 : Data(S, TemporaryDtorsBranch) {}
224 Stmt *getStmt() { return Data.getPointer(); }
225 const Stmt *getStmt() const { return Data.getPointer(); }
227 bool isTemporaryDtorsBranch() const { return Data.getInt(); }
229 operator Stmt *() { return getStmt(); }
230 operator const Stmt *() const { return getStmt(); }
232 Stmt *operator->() { return getStmt(); }
233 const Stmt *operator->() const { return getStmt(); }
235 Stmt &operator*() { return *getStmt(); }
236 const Stmt &operator*() const { return *getStmt(); }
238 operator bool() const { return getStmt(); }
241 /// CFGBlock - Represents a single basic block in a source-level CFG.
244 /// (1) A set of statements/expressions (which may contain subexpressions).
245 /// (2) A "terminator" statement (not in the set of statements).
246 /// (3) A list of successors and predecessors.
248 /// Terminator: The terminator represents the type of control-flow that occurs
249 /// at the end of the basic block. The terminator is a Stmt* referring to an
250 /// AST node that has control-flow: if-statements, breaks, loops, etc.
251 /// If the control-flow is conditional, the condition expression will appear
252 /// within the set of statements in the block (usually the last statement).
254 /// Predecessors: the order in the set of predecessors is arbitrary.
256 /// Successors: the order in the set of successors is NOT arbitrary. We
257 /// currently have the following orderings based on the terminator:
259 /// Terminator Successor Ordering
260 /// -----------------------------------------------------
261 /// if Then Block; Else Block
262 /// ? operator LHS expression; RHS expression
263 /// &&, || expression that uses result of && or ||, RHS
265 /// But note that any of that may be NULL in case of optimized-out edges.
269 typedef BumpVector<CFGElement> ImplTy;
272 ElementList(BumpVectorContext &C) : Impl(C, 4) {}
274 typedef std::reverse_iterator<ImplTy::iterator> iterator;
275 typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
276 typedef ImplTy::iterator reverse_iterator;
277 typedef ImplTy::const_iterator const_reverse_iterator;
278 typedef ImplTy::const_reference const_reference;
280 void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
281 reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E,
282 BumpVectorContext &C) {
283 return Impl.insert(I, Cnt, E, C);
286 const_reference front() const { return Impl.back(); }
287 const_reference back() const { return Impl.front(); }
289 iterator begin() { return Impl.rbegin(); }
290 iterator end() { return Impl.rend(); }
291 const_iterator begin() const { return Impl.rbegin(); }
292 const_iterator end() const { return Impl.rend(); }
293 reverse_iterator rbegin() { return Impl.begin(); }
294 reverse_iterator rend() { return Impl.end(); }
295 const_reverse_iterator rbegin() const { return Impl.begin(); }
296 const_reverse_iterator rend() const { return Impl.end(); }
298 CFGElement operator[](size_t i) const {
299 assert(i < Impl.size());
300 return Impl[Impl.size() - 1 - i];
303 size_t size() const { return Impl.size(); }
304 bool empty() const { return Impl.empty(); }
307 /// Stmts - The set of statements in the basic block.
308 ElementList Elements;
310 /// Label - An (optional) label that prefixes the executable
311 /// statements in the block. When this variable is non-NULL, it is
312 /// either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
315 /// Terminator - The terminator for a basic block that
316 /// indicates the type of control-flow that occurs between a block
317 /// and its successors.
318 CFGTerminator Terminator;
320 /// LoopTarget - Some blocks are used to represent the "loop edge" to
321 /// the start of a loop from within the loop body. This Stmt* will be
322 /// refer to the loop statement for such blocks (and be null otherwise).
323 const Stmt *LoopTarget;
325 /// BlockID - A numerical ID assigned to a CFGBlock during construction
329 /// Predecessors/Successors - Keep track of the predecessor / successor
331 typedef BumpVector<CFGBlock*> AdjacentBlocks;
332 AdjacentBlocks Preds;
333 AdjacentBlocks Succs;
335 /// NoReturn - This bit is set when the basic block contains a function call
336 /// or implicit destructor that is attributed as 'noreturn'. In that case,
337 /// control cannot technically ever proceed past this block. All such blocks
338 /// will have a single immediate successor: the exit block. This allows them
339 /// to be easily reached from the exit block and using this bit quickly
340 /// recognized without scanning the contents of the block.
342 /// Optimization Note: This bit could be profitably folded with Terminator's
343 /// storage if the memory usage of CFGBlock becomes an issue.
344 unsigned HasNoReturnElement : 1;
346 /// Parent - The parent CFG that owns this CFGBlock.
350 explicit CFGBlock(unsigned blockid, BumpVectorContext &C, CFG *parent)
351 : Elements(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
352 BlockID(blockid), Preds(C, 1), Succs(C, 1), HasNoReturnElement(false),
356 // Statement iterators
357 typedef ElementList::iterator iterator;
358 typedef ElementList::const_iterator const_iterator;
359 typedef ElementList::reverse_iterator reverse_iterator;
360 typedef ElementList::const_reverse_iterator const_reverse_iterator;
362 CFGElement front() const { return Elements.front(); }
363 CFGElement back() const { return Elements.back(); }
365 iterator begin() { return Elements.begin(); }
366 iterator end() { return Elements.end(); }
367 const_iterator begin() const { return Elements.begin(); }
368 const_iterator end() const { return Elements.end(); }
370 reverse_iterator rbegin() { return Elements.rbegin(); }
371 reverse_iterator rend() { return Elements.rend(); }
372 const_reverse_iterator rbegin() const { return Elements.rbegin(); }
373 const_reverse_iterator rend() const { return Elements.rend(); }
375 unsigned size() const { return Elements.size(); }
376 bool empty() const { return Elements.empty(); }
378 CFGElement operator[](size_t i) const { return Elements[i]; }
381 typedef AdjacentBlocks::iterator pred_iterator;
382 typedef AdjacentBlocks::const_iterator const_pred_iterator;
383 typedef AdjacentBlocks::reverse_iterator pred_reverse_iterator;
384 typedef AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator;
386 typedef AdjacentBlocks::iterator succ_iterator;
387 typedef AdjacentBlocks::const_iterator const_succ_iterator;
388 typedef AdjacentBlocks::reverse_iterator succ_reverse_iterator;
389 typedef AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator;
391 pred_iterator pred_begin() { return Preds.begin(); }
392 pred_iterator pred_end() { return Preds.end(); }
393 const_pred_iterator pred_begin() const { return Preds.begin(); }
394 const_pred_iterator pred_end() const { return Preds.end(); }
396 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); }
397 pred_reverse_iterator pred_rend() { return Preds.rend(); }
398 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
399 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
401 succ_iterator succ_begin() { return Succs.begin(); }
402 succ_iterator succ_end() { return Succs.end(); }
403 const_succ_iterator succ_begin() const { return Succs.begin(); }
404 const_succ_iterator succ_end() const { return Succs.end(); }
406 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); }
407 succ_reverse_iterator succ_rend() { return Succs.rend(); }
408 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
409 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
411 unsigned succ_size() const { return Succs.size(); }
412 bool succ_empty() const { return Succs.empty(); }
414 unsigned pred_size() const { return Preds.size(); }
415 bool pred_empty() const { return Preds.empty(); }
418 class FilterOptions {
421 IgnoreDefaultsWithCoveredEnums = 0;
424 unsigned IgnoreDefaultsWithCoveredEnums : 1;
427 static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
428 const CFGBlock *Dst);
430 template <typename IMPL, bool IsPred>
431 class FilteredCFGBlockIterator {
434 const FilterOptions F;
435 const CFGBlock *From;
437 explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
438 const CFGBlock *from,
439 const FilterOptions &f)
440 : I(i), E(e), F(f), From(from) {}
442 bool hasMore() const { return I != E; }
444 FilteredCFGBlockIterator &operator++() {
445 do { ++I; } while (hasMore() && Filter(*I));
449 const CFGBlock *operator*() const { return *I; }
451 bool Filter(const CFGBlock *To) {
452 return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
456 typedef FilteredCFGBlockIterator<const_pred_iterator, true>
457 filtered_pred_iterator;
459 typedef FilteredCFGBlockIterator<const_succ_iterator, false>
460 filtered_succ_iterator;
462 filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
463 return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
466 filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
467 return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
470 // Manipulation of block contents
472 void setTerminator(Stmt *Statement) { Terminator = Statement; }
473 void setLabel(Stmt *Statement) { Label = Statement; }
474 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
475 void setHasNoReturnElement() { HasNoReturnElement = true; }
477 CFGTerminator getTerminator() { return Terminator; }
478 const CFGTerminator getTerminator() const { return Terminator; }
480 Stmt *getTerminatorCondition();
482 const Stmt *getTerminatorCondition() const {
483 return const_cast<CFGBlock*>(this)->getTerminatorCondition();
486 const Stmt *getLoopTarget() const { return LoopTarget; }
488 Stmt *getLabel() { return Label; }
489 const Stmt *getLabel() const { return Label; }
491 bool hasNoReturnElement() const { return HasNoReturnElement; }
493 unsigned getBlockID() const { return BlockID; }
495 CFG *getParent() const { return Parent; }
497 void dump(const CFG *cfg, const LangOptions &LO, bool ShowColors = false) const;
498 void print(raw_ostream &OS, const CFG* cfg, const LangOptions &LO,
499 bool ShowColors) const;
500 void printTerminator(raw_ostream &OS, const LangOptions &LO) const;
502 void addSuccessor(CFGBlock *Block, BumpVectorContext &C) {
504 Block->Preds.push_back(this, C);
505 Succs.push_back(Block, C);
508 void appendStmt(Stmt *statement, BumpVectorContext &C) {
509 Elements.push_back(CFGStmt(statement), C);
512 void appendInitializer(CXXCtorInitializer *initializer,
513 BumpVectorContext &C) {
514 Elements.push_back(CFGInitializer(initializer), C);
517 void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) {
518 Elements.push_back(CFGBaseDtor(BS), C);
521 void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C) {
522 Elements.push_back(CFGMemberDtor(FD), C);
525 void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C) {
526 Elements.push_back(CFGTemporaryDtor(E), C);
529 void appendAutomaticObjDtor(VarDecl *VD, Stmt *S, BumpVectorContext &C) {
530 Elements.push_back(CFGAutomaticObjDtor(VD, S), C);
533 // Destructors must be inserted in reversed order. So insertion is in two
534 // steps. First we prepare space for some number of elements, then we insert
535 // the elements beginning at the last position in prepared space.
536 iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt,
537 BumpVectorContext &C) {
538 return iterator(Elements.insert(I.base(), Cnt, CFGElement(), C));
540 iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S) {
541 *I = CFGAutomaticObjDtor(VD, S);
546 /// CFG - Represents a source-level, intra-procedural CFG that represents the
547 /// control-flow of a Stmt. The Stmt can represent an entire function body,
548 /// or a single expression. A CFG will always contain one empty block that
549 /// represents the Exit point of the CFG. A CFG will also contain a designated
550 /// Entry block. The CFG solely represents control-flow; it consists of
551 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
552 /// was constructed from.
555 //===--------------------------------------------------------------------===//
556 // CFG Construction & Manipulation.
557 //===--------------------------------------------------------------------===//
560 std::bitset<Stmt::lastStmtConstant> alwaysAddMask;
562 typedef llvm::DenseMap<const Stmt *, const CFGBlock*> ForcedBlkExprs;
563 ForcedBlkExprs **forcedBlkExprs;
565 bool PruneTriviallyFalseEdges;
567 bool AddInitializers;
568 bool AddImplicitDtors;
569 bool AddTemporaryDtors;
571 bool alwaysAdd(const Stmt *stmt) const {
572 return alwaysAddMask[stmt->getStmtClass()];
575 BuildOptions &setAlwaysAdd(Stmt::StmtClass stmtClass, bool val = true) {
576 alwaysAddMask[stmtClass] = val;
580 BuildOptions &setAllAlwaysAdd() {
586 : forcedBlkExprs(0), PruneTriviallyFalseEdges(true)
588 ,AddInitializers(false)
589 ,AddImplicitDtors(false)
590 ,AddTemporaryDtors(false) {}
593 /// \brief Provides a custom implementation of the iterator class to have the
594 /// same interface as Function::iterator - iterator returns CFGBlock
595 /// (not a pointer to CFGBlock).
596 class graph_iterator {
598 typedef const CFGBlock value_type;
599 typedef value_type& reference;
600 typedef value_type* pointer;
601 typedef BumpVector<CFGBlock*>::iterator ImplTy;
603 graph_iterator(const ImplTy &i) : I(i) {}
605 bool operator==(const graph_iterator &X) const { return I == X.I; }
606 bool operator!=(const graph_iterator &X) const { return I != X.I; }
608 reference operator*() const { return **I; }
609 pointer operator->() const { return *I; }
610 operator CFGBlock* () { return *I; }
612 graph_iterator &operator++() { ++I; return *this; }
613 graph_iterator &operator--() { --I; return *this; }
619 class const_graph_iterator {
621 typedef const CFGBlock value_type;
622 typedef value_type& reference;
623 typedef value_type* pointer;
624 typedef BumpVector<CFGBlock*>::const_iterator ImplTy;
626 const_graph_iterator(const ImplTy &i) : I(i) {}
628 bool operator==(const const_graph_iterator &X) const { return I == X.I; }
629 bool operator!=(const const_graph_iterator &X) const { return I != X.I; }
631 reference operator*() const { return **I; }
632 pointer operator->() const { return *I; }
633 operator CFGBlock* () const { return *I; }
635 const_graph_iterator &operator++() { ++I; return *this; }
636 const_graph_iterator &operator--() { --I; return *this; }
642 /// buildCFG - Builds a CFG from an AST. The responsibility to free the
643 /// constructed CFG belongs to the caller.
644 static CFG* buildCFG(const Decl *D, Stmt *AST, ASTContext *C,
645 const BuildOptions &BO);
647 /// createBlock - Create a new block in the CFG. The CFG owns the block;
648 /// the caller should not directly free it.
649 CFGBlock *createBlock();
651 /// setEntry - Set the entry block of the CFG. This is typically used
652 /// only during CFG construction. Most CFG clients expect that the
653 /// entry block has no predecessors and contains no statements.
654 void setEntry(CFGBlock *B) { Entry = B; }
656 /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
657 /// This is typically used only during CFG construction.
658 void setIndirectGotoBlock(CFGBlock *B) { IndirectGotoBlock = B; }
660 //===--------------------------------------------------------------------===//
662 //===--------------------------------------------------------------------===//
664 typedef BumpVector<CFGBlock*> CFGBlockListTy;
665 typedef CFGBlockListTy::iterator iterator;
666 typedef CFGBlockListTy::const_iterator const_iterator;
667 typedef std::reverse_iterator<iterator> reverse_iterator;
668 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
670 CFGBlock & front() { return *Blocks.front(); }
671 CFGBlock & back() { return *Blocks.back(); }
673 iterator begin() { return Blocks.begin(); }
674 iterator end() { return Blocks.end(); }
675 const_iterator begin() const { return Blocks.begin(); }
676 const_iterator end() const { return Blocks.end(); }
678 graph_iterator nodes_begin() { return graph_iterator(Blocks.begin()); }
679 graph_iterator nodes_end() { return graph_iterator(Blocks.end()); }
680 const_graph_iterator nodes_begin() const {
681 return const_graph_iterator(Blocks.begin());
683 const_graph_iterator nodes_end() const {
684 return const_graph_iterator(Blocks.end());
687 reverse_iterator rbegin() { return Blocks.rbegin(); }
688 reverse_iterator rend() { return Blocks.rend(); }
689 const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
690 const_reverse_iterator rend() const { return Blocks.rend(); }
692 CFGBlock & getEntry() { return *Entry; }
693 const CFGBlock & getEntry() const { return *Entry; }
694 CFGBlock & getExit() { return *Exit; }
695 const CFGBlock & getExit() const { return *Exit; }
697 CFGBlock * getIndirectGotoBlock() { return IndirectGotoBlock; }
698 const CFGBlock * getIndirectGotoBlock() const { return IndirectGotoBlock; }
700 typedef std::vector<const CFGBlock*>::const_iterator try_block_iterator;
701 try_block_iterator try_blocks_begin() const {
702 return TryDispatchBlocks.begin();
704 try_block_iterator try_blocks_end() const {
705 return TryDispatchBlocks.end();
708 void addTryDispatchBlock(const CFGBlock *block) {
709 TryDispatchBlocks.push_back(block);
712 //===--------------------------------------------------------------------===//
713 // Member templates useful for various batch operations over CFGs.
714 //===--------------------------------------------------------------------===//
716 template <typename CALLBACK>
717 void VisitBlockStmts(CALLBACK& O) const {
718 for (const_iterator I=begin(), E=end(); I != E; ++I)
719 for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
721 if (const CFGStmt *stmt = BI->getAs<CFGStmt>())
722 O(const_cast<Stmt*>(stmt->getStmt()));
726 //===--------------------------------------------------------------------===//
727 // CFG Introspection.
728 //===--------------------------------------------------------------------===//
730 struct BlkExprNumTy {
732 explicit BlkExprNumTy(signed idx) : Idx(idx) {}
733 explicit BlkExprNumTy() : Idx(-1) {}
734 operator bool() const { return Idx >= 0; }
735 operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
738 bool isBlkExpr(const Stmt *S) { return getBlkExprNum(S); }
739 bool isBlkExpr(const Stmt *S) const {
740 return const_cast<CFG*>(this)->isBlkExpr(S);
742 BlkExprNumTy getBlkExprNum(const Stmt *S);
743 unsigned getNumBlkExprs();
745 /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
747 unsigned getNumBlockIDs() const { return NumBlockIDs; }
749 /// size - Return the total number of CFGBlocks within the CFG
750 /// This is simply a renaming of the getNumBlockIDs(). This is necessary
751 /// because the dominator implementation needs such an interface.
752 unsigned size() const { return NumBlockIDs; }
754 //===--------------------------------------------------------------------===//
755 // CFG Debugging: Pretty-Printing and Visualization.
756 //===--------------------------------------------------------------------===//
758 void viewCFG(const LangOptions &LO) const;
759 void print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const;
760 void dump(const LangOptions &LO, bool ShowColors) const;
762 //===--------------------------------------------------------------------===//
763 // Internal: constructors and data.
764 //===--------------------------------------------------------------------===//
766 CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
767 BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
771 llvm::BumpPtrAllocator& getAllocator() {
772 return BlkBVC.getAllocator();
775 BumpVectorContext &getBumpVectorContext() {
782 CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch
783 // for indirect gotos
784 unsigned NumBlockIDs;
786 // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
787 // It represents a map from Expr* to integers to record the set of
788 // block-level expressions and their "statement number" in the CFG.
791 BumpVectorContext BlkBVC;
793 CFGBlockListTy Blocks;
795 /// C++ 'try' statements are modeled with an indirect dispatch block.
796 /// This is the collection of such blocks present in the CFG.
797 std::vector<const CFGBlock *> TryDispatchBlocks;
800 } // end namespace clang
802 //===----------------------------------------------------------------------===//
803 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
804 //===----------------------------------------------------------------------===//
808 /// Implement simplify_type for CFGTerminator, so that we can dyn_cast from
809 /// CFGTerminator to a specific Stmt class.
810 template <> struct simplify_type<const ::clang::CFGTerminator> {
811 typedef const ::clang::Stmt *SimpleType;
812 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
813 return Val.getStmt();
817 template <> struct simplify_type< ::clang::CFGTerminator> {
818 typedef ::clang::Stmt *SimpleType;
819 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
820 return const_cast<SimpleType>(Val.getStmt());
824 // Traits for: CFGBlock
826 template <> struct GraphTraits< ::clang::CFGBlock *> {
827 typedef ::clang::CFGBlock NodeType;
828 typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
830 static NodeType* getEntryNode(::clang::CFGBlock *BB)
833 static inline ChildIteratorType child_begin(NodeType* N)
834 { return N->succ_begin(); }
836 static inline ChildIteratorType child_end(NodeType* N)
837 { return N->succ_end(); }
840 template <> struct GraphTraits< const ::clang::CFGBlock *> {
841 typedef const ::clang::CFGBlock NodeType;
842 typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
844 static NodeType* getEntryNode(const clang::CFGBlock *BB)
847 static inline ChildIteratorType child_begin(NodeType* N)
848 { return N->succ_begin(); }
850 static inline ChildIteratorType child_end(NodeType* N)
851 { return N->succ_end(); }
854 template <> struct GraphTraits<Inverse< ::clang::CFGBlock*> > {
855 typedef ::clang::CFGBlock NodeType;
856 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
858 static NodeType *getEntryNode(Inverse< ::clang::CFGBlock*> G)
861 static inline ChildIteratorType child_begin(NodeType* N)
862 { return N->pred_begin(); }
864 static inline ChildIteratorType child_end(NodeType* N)
865 { return N->pred_end(); }
868 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
869 typedef const ::clang::CFGBlock NodeType;
870 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
872 static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
875 static inline ChildIteratorType child_begin(NodeType* N)
876 { return N->pred_begin(); }
878 static inline ChildIteratorType child_end(NodeType* N)
879 { return N->pred_end(); }
884 template <> struct GraphTraits< ::clang::CFG* >
885 : public GraphTraits< ::clang::CFGBlock *> {
887 typedef ::clang::CFG::graph_iterator nodes_iterator;
889 static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
890 static nodes_iterator nodes_begin(::clang::CFG* F) { return F->nodes_begin();}
891 static nodes_iterator nodes_end(::clang::CFG* F) { return F->nodes_end(); }
892 static unsigned size(::clang::CFG* F) { return F->size(); }
895 template <> struct GraphTraits<const ::clang::CFG* >
896 : public GraphTraits<const ::clang::CFGBlock *> {
898 typedef ::clang::CFG::const_graph_iterator nodes_iterator;
900 static NodeType *getEntryNode( const ::clang::CFG* F) {
901 return &F->getEntry();
903 static nodes_iterator nodes_begin( const ::clang::CFG* F) {
904 return F->nodes_begin();
906 static nodes_iterator nodes_end( const ::clang::CFG* F) {
907 return F->nodes_end();
909 static unsigned size(const ::clang::CFG* F) {
914 template <> struct GraphTraits<Inverse< ::clang::CFG*> >
915 : public GraphTraits<Inverse< ::clang::CFGBlock*> > {
917 typedef ::clang::CFG::graph_iterator nodes_iterator;
919 static NodeType *getEntryNode( ::clang::CFG* F) { return &F->getExit(); }
920 static nodes_iterator nodes_begin( ::clang::CFG* F) {return F->nodes_begin();}
921 static nodes_iterator nodes_end( ::clang::CFG* F) { return F->nodes_end(); }
924 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
925 : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
927 typedef ::clang::CFG::const_graph_iterator nodes_iterator;
929 static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
930 static nodes_iterator nodes_begin(const ::clang::CFG* F) {
931 return F->nodes_begin();
933 static nodes_iterator nodes_end(const ::clang::CFG* F) {
934 return F->nodes_end();
937 } // end llvm namespace