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 "clang/AST/Stmt.h"
19 #include "clang/Analysis/Support/BumpVector.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/GraphTraits.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/OwningPtr.h"
25 #include "llvm/ADT/PointerIntPair.h"
26 #include "llvm/Support/Allocator.h"
27 #include "llvm/Support/Casting.h"
33 class CXXDestructorDecl;
39 class CXXCtorInitializer;
40 class CXXBaseSpecifier;
41 class CXXBindTemporaryExpr;
47 /// CFGElement - Represents a top-level expression in a basic block.
60 DTOR_BEGIN = AutomaticObjectDtor,
61 DTOR_END = TemporaryDtor
65 // The int bits are used to mark the kind.
66 llvm::PointerIntPair<void *, 2> Data1;
67 llvm::PointerIntPair<void *, 2> Data2;
69 CFGElement(Kind kind, const void *Ptr1, const void *Ptr2 = 0)
70 : Data1(const_cast<void*>(Ptr1), ((unsigned) kind) & 0x3),
71 Data2(const_cast<void*>(Ptr2), (((unsigned) kind) >> 2) & 0x3) {}
76 /// \brief Convert to the specified CFGElement type, asserting that this
77 /// CFGElement is of the desired type.
80 assert(T::isKind(*this));
87 /// \brief Convert to the specified CFGElement type, returning an invalid
88 /// CFGElement if this CFGElement is not of the desired type.
91 if (!T::isKind(*this))
99 Kind getKind() const {
100 unsigned x = Data2.getInt();
106 bool isValid() const { return getKind() != Invalid; }
108 operator bool() const { return isValid(); }
111 class CFGStmt : public CFGElement {
113 CFGStmt(Stmt *S) : CFGElement(Statement, S) {}
115 const Stmt *getStmt() const {
116 return static_cast<const Stmt *>(Data1.getPointer());
120 friend class CFGElement;
122 static bool isKind(const CFGElement &E) {
123 return E.getKind() == Statement;
127 /// CFGInitializer - Represents C++ base or member initializer from
128 /// constructor's initialization list.
129 class CFGInitializer : public CFGElement {
131 CFGInitializer(CXXCtorInitializer *initializer)
132 : CFGElement(Initializer, initializer) {}
134 CXXCtorInitializer* getInitializer() const {
135 return static_cast<CXXCtorInitializer*>(Data1.getPointer());
139 friend class CFGElement;
141 static bool isKind(const CFGElement &E) {
142 return E.getKind() == Initializer;
146 /// CFGImplicitDtor - Represents C++ object destructor implicitly generated
147 /// by compiler on various occasions.
148 class CFGImplicitDtor : public CFGElement {
151 CFGImplicitDtor(Kind kind, const void *data1, const void *data2 = 0)
152 : CFGElement(kind, data1, data2) {
153 assert(kind >= DTOR_BEGIN && kind <= DTOR_END);
157 const CXXDestructorDecl *getDestructorDecl(ASTContext &astContext) const;
158 bool isNoReturn(ASTContext &astContext) const;
161 friend class CFGElement;
162 static bool isKind(const CFGElement &E) {
163 Kind kind = E.getKind();
164 return kind >= DTOR_BEGIN && kind <= DTOR_END;
168 /// CFGAutomaticObjDtor - Represents C++ object destructor implicitly generated
169 /// for automatic object or temporary bound to const reference at the point
170 /// of leaving its local scope.
171 class CFGAutomaticObjDtor: public CFGImplicitDtor {
173 CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
174 : CFGImplicitDtor(AutomaticObjectDtor, var, stmt) {}
176 const VarDecl *getVarDecl() const {
177 return static_cast<VarDecl*>(Data1.getPointer());
180 // Get statement end of which triggered the destructor call.
181 const Stmt *getTriggerStmt() const {
182 return static_cast<Stmt*>(Data2.getPointer());
186 friend class CFGElement;
187 CFGAutomaticObjDtor() {}
188 static bool isKind(const CFGElement &elem) {
189 return elem.getKind() == AutomaticObjectDtor;
193 /// CFGBaseDtor - Represents C++ object destructor implicitly generated for
194 /// base object in destructor.
195 class CFGBaseDtor : public CFGImplicitDtor {
197 CFGBaseDtor(const CXXBaseSpecifier *base)
198 : CFGImplicitDtor(BaseDtor, base) {}
200 const CXXBaseSpecifier *getBaseSpecifier() const {
201 return static_cast<const CXXBaseSpecifier*>(Data1.getPointer());
205 friend class CFGElement;
207 static bool isKind(const CFGElement &E) {
208 return E.getKind() == BaseDtor;
212 /// CFGMemberDtor - Represents C++ object destructor implicitly generated for
213 /// member object in destructor.
214 class CFGMemberDtor : public CFGImplicitDtor {
216 CFGMemberDtor(const FieldDecl *field)
217 : CFGImplicitDtor(MemberDtor, field, 0) {}
219 const FieldDecl *getFieldDecl() const {
220 return static_cast<const FieldDecl*>(Data1.getPointer());
224 friend class CFGElement;
226 static bool isKind(const CFGElement &E) {
227 return E.getKind() == MemberDtor;
231 /// CFGTemporaryDtor - Represents C++ object destructor implicitly generated
232 /// at the end of full expression for temporary object.
233 class CFGTemporaryDtor : public CFGImplicitDtor {
235 CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
236 : CFGImplicitDtor(TemporaryDtor, expr, 0) {}
238 const CXXBindTemporaryExpr *getBindTemporaryExpr() const {
239 return static_cast<const CXXBindTemporaryExpr *>(Data1.getPointer());
243 friend class CFGElement;
244 CFGTemporaryDtor() {}
245 static bool isKind(const CFGElement &E) {
246 return E.getKind() == TemporaryDtor;
250 /// CFGTerminator - Represents CFGBlock terminator statement.
252 /// TemporaryDtorsBranch bit is set to true if the terminator marks a branch
253 /// in control flow of destructors of temporaries. In this case terminator
254 /// statement is the same statement that branches control flow in evaluation
255 /// of matching full expression.
256 class CFGTerminator {
257 llvm::PointerIntPair<Stmt *, 1> Data;
260 CFGTerminator(Stmt *S, bool TemporaryDtorsBranch = false)
261 : Data(S, TemporaryDtorsBranch) {}
263 Stmt *getStmt() { return Data.getPointer(); }
264 const Stmt *getStmt() const { return Data.getPointer(); }
266 bool isTemporaryDtorsBranch() const { return Data.getInt(); }
268 operator Stmt *() { return getStmt(); }
269 operator const Stmt *() const { return getStmt(); }
271 Stmt *operator->() { return getStmt(); }
272 const Stmt *operator->() const { return getStmt(); }
274 Stmt &operator*() { return *getStmt(); }
275 const Stmt &operator*() const { return *getStmt(); }
277 operator bool() const { return getStmt(); }
280 /// CFGBlock - Represents a single basic block in a source-level CFG.
283 /// (1) A set of statements/expressions (which may contain subexpressions).
284 /// (2) A "terminator" statement (not in the set of statements).
285 /// (3) A list of successors and predecessors.
287 /// Terminator: The terminator represents the type of control-flow that occurs
288 /// at the end of the basic block. The terminator is a Stmt* referring to an
289 /// AST node that has control-flow: if-statements, breaks, loops, etc.
290 /// If the control-flow is conditional, the condition expression will appear
291 /// within the set of statements in the block (usually the last statement).
293 /// Predecessors: the order in the set of predecessors is arbitrary.
295 /// Successors: the order in the set of successors is NOT arbitrary. We
296 /// currently have the following orderings based on the terminator:
298 /// Terminator Successor Ordering
299 /// -----------------------------------------------------
300 /// if Then Block; Else Block
301 /// ? operator LHS expression; RHS expression
302 /// &&, || expression that uses result of && or ||, RHS
304 /// But note that any of that may be NULL in case of optimized-out edges.
308 typedef BumpVector<CFGElement> ImplTy;
311 ElementList(BumpVectorContext &C) : Impl(C, 4) {}
313 typedef std::reverse_iterator<ImplTy::iterator> iterator;
314 typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
315 typedef ImplTy::iterator reverse_iterator;
316 typedef ImplTy::const_iterator const_reverse_iterator;
317 typedef ImplTy::const_reference const_reference;
319 void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
320 reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E,
321 BumpVectorContext &C) {
322 return Impl.insert(I, Cnt, E, C);
325 const_reference front() const { return Impl.back(); }
326 const_reference back() const { return Impl.front(); }
328 iterator begin() { return Impl.rbegin(); }
329 iterator end() { return Impl.rend(); }
330 const_iterator begin() const { return Impl.rbegin(); }
331 const_iterator end() const { return Impl.rend(); }
332 reverse_iterator rbegin() { return Impl.begin(); }
333 reverse_iterator rend() { return Impl.end(); }
334 const_reverse_iterator rbegin() const { return Impl.begin(); }
335 const_reverse_iterator rend() const { return Impl.end(); }
337 CFGElement operator[](size_t i) const {
338 assert(i < Impl.size());
339 return Impl[Impl.size() - 1 - i];
342 size_t size() const { return Impl.size(); }
343 bool empty() const { return Impl.empty(); }
346 /// Stmts - The set of statements in the basic block.
347 ElementList Elements;
349 /// Label - An (optional) label that prefixes the executable
350 /// statements in the block. When this variable is non-NULL, it is
351 /// either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
354 /// Terminator - The terminator for a basic block that
355 /// indicates the type of control-flow that occurs between a block
356 /// and its successors.
357 CFGTerminator Terminator;
359 /// LoopTarget - Some blocks are used to represent the "loop edge" to
360 /// the start of a loop from within the loop body. This Stmt* will be
361 /// refer to the loop statement for such blocks (and be null otherwise).
362 const Stmt *LoopTarget;
364 /// BlockID - A numerical ID assigned to a CFGBlock during construction
368 /// Predecessors/Successors - Keep track of the predecessor / successor
370 typedef BumpVector<CFGBlock*> AdjacentBlocks;
371 AdjacentBlocks Preds;
372 AdjacentBlocks Succs;
374 /// NoReturn - This bit is set when the basic block contains a function call
375 /// or implicit destructor that is attributed as 'noreturn'. In that case,
376 /// control cannot technically ever proceed past this block. All such blocks
377 /// will have a single immediate successor: the exit block. This allows them
378 /// to be easily reached from the exit block and using this bit quickly
379 /// recognized without scanning the contents of the block.
381 /// Optimization Note: This bit could be profitably folded with Terminator's
382 /// storage if the memory usage of CFGBlock becomes an issue.
383 unsigned HasNoReturnElement : 1;
385 /// Parent - The parent CFG that owns this CFGBlock.
389 explicit CFGBlock(unsigned blockid, BumpVectorContext &C, CFG *parent)
390 : Elements(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
391 BlockID(blockid), Preds(C, 1), Succs(C, 1), HasNoReturnElement(false),
395 // Statement iterators
396 typedef ElementList::iterator iterator;
397 typedef ElementList::const_iterator const_iterator;
398 typedef ElementList::reverse_iterator reverse_iterator;
399 typedef ElementList::const_reverse_iterator const_reverse_iterator;
401 CFGElement front() const { return Elements.front(); }
402 CFGElement back() const { return Elements.back(); }
404 iterator begin() { return Elements.begin(); }
405 iterator end() { return Elements.end(); }
406 const_iterator begin() const { return Elements.begin(); }
407 const_iterator end() const { return Elements.end(); }
409 reverse_iterator rbegin() { return Elements.rbegin(); }
410 reverse_iterator rend() { return Elements.rend(); }
411 const_reverse_iterator rbegin() const { return Elements.rbegin(); }
412 const_reverse_iterator rend() const { return Elements.rend(); }
414 unsigned size() const { return Elements.size(); }
415 bool empty() const { return Elements.empty(); }
417 CFGElement operator[](size_t i) const { return Elements[i]; }
420 typedef AdjacentBlocks::iterator pred_iterator;
421 typedef AdjacentBlocks::const_iterator const_pred_iterator;
422 typedef AdjacentBlocks::reverse_iterator pred_reverse_iterator;
423 typedef AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator;
425 typedef AdjacentBlocks::iterator succ_iterator;
426 typedef AdjacentBlocks::const_iterator const_succ_iterator;
427 typedef AdjacentBlocks::reverse_iterator succ_reverse_iterator;
428 typedef AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator;
430 pred_iterator pred_begin() { return Preds.begin(); }
431 pred_iterator pred_end() { return Preds.end(); }
432 const_pred_iterator pred_begin() const { return Preds.begin(); }
433 const_pred_iterator pred_end() const { return Preds.end(); }
435 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); }
436 pred_reverse_iterator pred_rend() { return Preds.rend(); }
437 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
438 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
440 succ_iterator succ_begin() { return Succs.begin(); }
441 succ_iterator succ_end() { return Succs.end(); }
442 const_succ_iterator succ_begin() const { return Succs.begin(); }
443 const_succ_iterator succ_end() const { return Succs.end(); }
445 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); }
446 succ_reverse_iterator succ_rend() { return Succs.rend(); }
447 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
448 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
450 unsigned succ_size() const { return Succs.size(); }
451 bool succ_empty() const { return Succs.empty(); }
453 unsigned pred_size() const { return Preds.size(); }
454 bool pred_empty() const { return Preds.empty(); }
457 class FilterOptions {
460 IgnoreDefaultsWithCoveredEnums = 0;
463 unsigned IgnoreDefaultsWithCoveredEnums : 1;
466 static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
467 const CFGBlock *Dst);
469 template <typename IMPL, bool IsPred>
470 class FilteredCFGBlockIterator {
473 const FilterOptions F;
474 const CFGBlock *From;
476 explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
477 const CFGBlock *from,
478 const FilterOptions &f)
479 : I(i), E(e), F(f), From(from) {}
481 bool hasMore() const { return I != E; }
483 FilteredCFGBlockIterator &operator++() {
484 do { ++I; } while (hasMore() && Filter(*I));
488 const CFGBlock *operator*() const { return *I; }
490 bool Filter(const CFGBlock *To) {
491 return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
495 typedef FilteredCFGBlockIterator<const_pred_iterator, true>
496 filtered_pred_iterator;
498 typedef FilteredCFGBlockIterator<const_succ_iterator, false>
499 filtered_succ_iterator;
501 filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
502 return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
505 filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
506 return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
509 // Manipulation of block contents
511 void setTerminator(Stmt *Statement) { Terminator = Statement; }
512 void setLabel(Stmt *Statement) { Label = Statement; }
513 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
514 void setHasNoReturnElement() { HasNoReturnElement = true; }
516 CFGTerminator getTerminator() { return Terminator; }
517 const CFGTerminator getTerminator() const { return Terminator; }
519 Stmt *getTerminatorCondition();
521 const Stmt *getTerminatorCondition() const {
522 return const_cast<CFGBlock*>(this)->getTerminatorCondition();
525 const Stmt *getLoopTarget() const { return LoopTarget; }
527 Stmt *getLabel() { return Label; }
528 const Stmt *getLabel() const { return Label; }
530 bool hasNoReturnElement() const { return HasNoReturnElement; }
532 unsigned getBlockID() const { return BlockID; }
534 CFG *getParent() const { return Parent; }
536 void dump(const CFG *cfg, const LangOptions &LO, bool ShowColors = false) const;
537 void print(raw_ostream &OS, const CFG* cfg, const LangOptions &LO,
538 bool ShowColors) const;
539 void printTerminator(raw_ostream &OS, const LangOptions &LO) const;
541 void addSuccessor(CFGBlock *Block, BumpVectorContext &C) {
543 Block->Preds.push_back(this, C);
544 Succs.push_back(Block, C);
547 void appendStmt(Stmt *statement, BumpVectorContext &C) {
548 Elements.push_back(CFGStmt(statement), C);
551 void appendInitializer(CXXCtorInitializer *initializer,
552 BumpVectorContext &C) {
553 Elements.push_back(CFGInitializer(initializer), C);
556 void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) {
557 Elements.push_back(CFGBaseDtor(BS), C);
560 void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C) {
561 Elements.push_back(CFGMemberDtor(FD), C);
564 void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C) {
565 Elements.push_back(CFGTemporaryDtor(E), C);
568 void appendAutomaticObjDtor(VarDecl *VD, Stmt *S, BumpVectorContext &C) {
569 Elements.push_back(CFGAutomaticObjDtor(VD, S), C);
572 // Destructors must be inserted in reversed order. So insertion is in two
573 // steps. First we prepare space for some number of elements, then we insert
574 // the elements beginning at the last position in prepared space.
575 iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt,
576 BumpVectorContext &C) {
577 return iterator(Elements.insert(I.base(), Cnt, CFGElement(), C));
579 iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S) {
580 *I = CFGAutomaticObjDtor(VD, S);
585 /// CFG - Represents a source-level, intra-procedural CFG that represents the
586 /// control-flow of a Stmt. The Stmt can represent an entire function body,
587 /// or a single expression. A CFG will always contain one empty block that
588 /// represents the Exit point of the CFG. A CFG will also contain a designated
589 /// Entry block. The CFG solely represents control-flow; it consists of
590 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
591 /// was constructed from.
594 //===--------------------------------------------------------------------===//
595 // CFG Construction & Manipulation.
596 //===--------------------------------------------------------------------===//
599 std::bitset<Stmt::lastStmtConstant> alwaysAddMask;
601 typedef llvm::DenseMap<const Stmt *, const CFGBlock*> ForcedBlkExprs;
602 ForcedBlkExprs **forcedBlkExprs;
604 bool PruneTriviallyFalseEdges;
606 bool AddInitializers;
607 bool AddImplicitDtors;
608 bool AddTemporaryDtors;
610 bool alwaysAdd(const Stmt *stmt) const {
611 return alwaysAddMask[stmt->getStmtClass()];
614 BuildOptions &setAlwaysAdd(Stmt::StmtClass stmtClass, bool val = true) {
615 alwaysAddMask[stmtClass] = val;
619 BuildOptions &setAllAlwaysAdd() {
625 : forcedBlkExprs(0), PruneTriviallyFalseEdges(true)
627 ,AddInitializers(false)
628 ,AddImplicitDtors(false)
629 ,AddTemporaryDtors(false) {}
632 /// \brief Provides a custom implementation of the iterator class to have the
633 /// same interface as Function::iterator - iterator returns CFGBlock
634 /// (not a pointer to CFGBlock).
635 class graph_iterator {
637 typedef const CFGBlock value_type;
638 typedef value_type& reference;
639 typedef value_type* pointer;
640 typedef BumpVector<CFGBlock*>::iterator ImplTy;
642 graph_iterator(const ImplTy &i) : I(i) {}
644 bool operator==(const graph_iterator &X) const { return I == X.I; }
645 bool operator!=(const graph_iterator &X) const { return I != X.I; }
647 reference operator*() const { return **I; }
648 pointer operator->() const { return *I; }
649 operator CFGBlock* () { return *I; }
651 graph_iterator &operator++() { ++I; return *this; }
652 graph_iterator &operator--() { --I; return *this; }
658 class const_graph_iterator {
660 typedef const CFGBlock value_type;
661 typedef value_type& reference;
662 typedef value_type* pointer;
663 typedef BumpVector<CFGBlock*>::const_iterator ImplTy;
665 const_graph_iterator(const ImplTy &i) : I(i) {}
667 bool operator==(const const_graph_iterator &X) const { return I == X.I; }
668 bool operator!=(const const_graph_iterator &X) const { return I != X.I; }
670 reference operator*() const { return **I; }
671 pointer operator->() const { return *I; }
672 operator CFGBlock* () const { return *I; }
674 const_graph_iterator &operator++() { ++I; return *this; }
675 const_graph_iterator &operator--() { --I; return *this; }
681 /// buildCFG - Builds a CFG from an AST. The responsibility to free the
682 /// constructed CFG belongs to the caller.
683 static CFG* buildCFG(const Decl *D, Stmt *AST, ASTContext *C,
684 const BuildOptions &BO);
686 /// createBlock - Create a new block in the CFG. The CFG owns the block;
687 /// the caller should not directly free it.
688 CFGBlock *createBlock();
690 /// setEntry - Set the entry block of the CFG. This is typically used
691 /// only during CFG construction. Most CFG clients expect that the
692 /// entry block has no predecessors and contains no statements.
693 void setEntry(CFGBlock *B) { Entry = B; }
695 /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
696 /// This is typically used only during CFG construction.
697 void setIndirectGotoBlock(CFGBlock *B) { IndirectGotoBlock = B; }
699 //===--------------------------------------------------------------------===//
701 //===--------------------------------------------------------------------===//
703 typedef BumpVector<CFGBlock*> CFGBlockListTy;
704 typedef CFGBlockListTy::iterator iterator;
705 typedef CFGBlockListTy::const_iterator const_iterator;
706 typedef std::reverse_iterator<iterator> reverse_iterator;
707 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
709 CFGBlock & front() { return *Blocks.front(); }
710 CFGBlock & back() { return *Blocks.back(); }
712 iterator begin() { return Blocks.begin(); }
713 iterator end() { return Blocks.end(); }
714 const_iterator begin() const { return Blocks.begin(); }
715 const_iterator end() const { return Blocks.end(); }
717 graph_iterator nodes_begin() { return graph_iterator(Blocks.begin()); }
718 graph_iterator nodes_end() { return graph_iterator(Blocks.end()); }
719 const_graph_iterator nodes_begin() const {
720 return const_graph_iterator(Blocks.begin());
722 const_graph_iterator nodes_end() const {
723 return const_graph_iterator(Blocks.end());
726 reverse_iterator rbegin() { return Blocks.rbegin(); }
727 reverse_iterator rend() { return Blocks.rend(); }
728 const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
729 const_reverse_iterator rend() const { return Blocks.rend(); }
731 CFGBlock & getEntry() { return *Entry; }
732 const CFGBlock & getEntry() const { return *Entry; }
733 CFGBlock & getExit() { return *Exit; }
734 const CFGBlock & getExit() const { return *Exit; }
736 CFGBlock * getIndirectGotoBlock() { return IndirectGotoBlock; }
737 const CFGBlock * getIndirectGotoBlock() const { return IndirectGotoBlock; }
739 typedef std::vector<const CFGBlock*>::const_iterator try_block_iterator;
740 try_block_iterator try_blocks_begin() const {
741 return TryDispatchBlocks.begin();
743 try_block_iterator try_blocks_end() const {
744 return TryDispatchBlocks.end();
747 void addTryDispatchBlock(const CFGBlock *block) {
748 TryDispatchBlocks.push_back(block);
751 //===--------------------------------------------------------------------===//
752 // Member templates useful for various batch operations over CFGs.
753 //===--------------------------------------------------------------------===//
755 template <typename CALLBACK>
756 void VisitBlockStmts(CALLBACK& O) const {
757 for (const_iterator I=begin(), E=end(); I != E; ++I)
758 for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
760 if (CFGStmt stmt = BI->getAs<CFGStmt>())
761 O(const_cast<Stmt*>(stmt.getStmt()));
765 //===--------------------------------------------------------------------===//
766 // CFG Introspection.
767 //===--------------------------------------------------------------------===//
769 struct BlkExprNumTy {
771 explicit BlkExprNumTy(signed idx) : Idx(idx) {}
772 explicit BlkExprNumTy() : Idx(-1) {}
773 operator bool() const { return Idx >= 0; }
774 operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
777 bool isBlkExpr(const Stmt *S) { return getBlkExprNum(S); }
778 bool isBlkExpr(const Stmt *S) const {
779 return const_cast<CFG*>(this)->isBlkExpr(S);
781 BlkExprNumTy getBlkExprNum(const Stmt *S);
782 unsigned getNumBlkExprs();
784 /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
786 unsigned getNumBlockIDs() const { return NumBlockIDs; }
788 /// size - Return the total number of CFGBlocks within the CFG
789 /// This is simply a renaming of the getNumBlockIDs(). This is necessary
790 /// because the dominator implementation needs such an interface.
791 unsigned size() const { return NumBlockIDs; }
793 //===--------------------------------------------------------------------===//
794 // CFG Debugging: Pretty-Printing and Visualization.
795 //===--------------------------------------------------------------------===//
797 void viewCFG(const LangOptions &LO) const;
798 void print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const;
799 void dump(const LangOptions &LO, bool ShowColors) const;
801 //===--------------------------------------------------------------------===//
802 // Internal: constructors and data.
803 //===--------------------------------------------------------------------===//
805 CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
806 BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
810 llvm::BumpPtrAllocator& getAllocator() {
811 return BlkBVC.getAllocator();
814 BumpVectorContext &getBumpVectorContext() {
821 CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch
822 // for indirect gotos
823 unsigned NumBlockIDs;
825 // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
826 // It represents a map from Expr* to integers to record the set of
827 // block-level expressions and their "statement number" in the CFG.
830 BumpVectorContext BlkBVC;
832 CFGBlockListTy Blocks;
834 /// C++ 'try' statements are modeled with an indirect dispatch block.
835 /// This is the collection of such blocks present in the CFG.
836 std::vector<const CFGBlock *> TryDispatchBlocks;
839 } // end namespace clang
841 //===----------------------------------------------------------------------===//
842 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
843 //===----------------------------------------------------------------------===//
847 /// Implement simplify_type for CFGTerminator, so that we can dyn_cast from
848 /// CFGTerminator to a specific Stmt class.
849 template <> struct simplify_type<const ::clang::CFGTerminator> {
850 typedef const ::clang::Stmt *SimpleType;
851 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
852 return Val.getStmt();
856 template <> struct simplify_type< ::clang::CFGTerminator> {
857 typedef ::clang::Stmt *SimpleType;
858 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
859 return const_cast<SimpleType>(Val.getStmt());
863 // Traits for: CFGBlock
865 template <> struct GraphTraits< ::clang::CFGBlock *> {
866 typedef ::clang::CFGBlock NodeType;
867 typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
869 static NodeType* getEntryNode(::clang::CFGBlock *BB)
872 static inline ChildIteratorType child_begin(NodeType* N)
873 { return N->succ_begin(); }
875 static inline ChildIteratorType child_end(NodeType* N)
876 { return N->succ_end(); }
879 template <> struct GraphTraits< const ::clang::CFGBlock *> {
880 typedef const ::clang::CFGBlock NodeType;
881 typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
883 static NodeType* getEntryNode(const clang::CFGBlock *BB)
886 static inline ChildIteratorType child_begin(NodeType* N)
887 { return N->succ_begin(); }
889 static inline ChildIteratorType child_end(NodeType* N)
890 { return N->succ_end(); }
893 template <> struct GraphTraits<Inverse< ::clang::CFGBlock*> > {
894 typedef ::clang::CFGBlock NodeType;
895 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
897 static NodeType *getEntryNode(Inverse< ::clang::CFGBlock*> G)
900 static inline ChildIteratorType child_begin(NodeType* N)
901 { return N->pred_begin(); }
903 static inline ChildIteratorType child_end(NodeType* N)
904 { return N->pred_end(); }
907 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
908 typedef const ::clang::CFGBlock NodeType;
909 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
911 static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
914 static inline ChildIteratorType child_begin(NodeType* N)
915 { return N->pred_begin(); }
917 static inline ChildIteratorType child_end(NodeType* N)
918 { return N->pred_end(); }
923 template <> struct GraphTraits< ::clang::CFG* >
924 : public GraphTraits< ::clang::CFGBlock *> {
926 typedef ::clang::CFG::graph_iterator nodes_iterator;
928 static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
929 static nodes_iterator nodes_begin(::clang::CFG* F) { return F->nodes_begin();}
930 static nodes_iterator nodes_end(::clang::CFG* F) { return F->nodes_end(); }
931 static unsigned size(::clang::CFG* F) { return F->size(); }
934 template <> struct GraphTraits<const ::clang::CFG* >
935 : public GraphTraits<const ::clang::CFGBlock *> {
937 typedef ::clang::CFG::const_graph_iterator nodes_iterator;
939 static NodeType *getEntryNode( const ::clang::CFG* F) {
940 return &F->getEntry();
942 static nodes_iterator nodes_begin( const ::clang::CFG* F) {
943 return F->nodes_begin();
945 static nodes_iterator nodes_end( const ::clang::CFG* F) {
946 return F->nodes_end();
948 static unsigned size(const ::clang::CFG* F) {
953 template <> struct GraphTraits<Inverse< ::clang::CFG*> >
954 : public GraphTraits<Inverse< ::clang::CFGBlock*> > {
956 typedef ::clang::CFG::graph_iterator nodes_iterator;
958 static NodeType *getEntryNode( ::clang::CFG* F) { return &F->getExit(); }
959 static nodes_iterator nodes_begin( ::clang::CFG* F) {return F->nodes_begin();}
960 static nodes_iterator nodes_end( ::clang::CFG* F) { return F->nodes_end(); }
963 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
964 : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
966 typedef ::clang::CFG::const_graph_iterator nodes_iterator;
968 static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
969 static nodes_iterator nodes_begin(const ::clang::CFG* F) {
970 return F->nodes_begin();
972 static nodes_iterator nodes_end(const ::clang::CFG* F) {
973 return F->nodes_end();
976 } // end llvm namespace