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 Stmt *getStmt() const { return static_cast<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;
279 void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
280 reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E,
281 BumpVectorContext& C) {
282 return Impl.insert(I, Cnt, E, C);
285 CFGElement front() const { return Impl.back(); }
286 CFGElement back() const { return Impl.front(); }
288 iterator begin() { return Impl.rbegin(); }
289 iterator end() { return Impl.rend(); }
290 const_iterator begin() const { return Impl.rbegin(); }
291 const_iterator end() const { return Impl.rend(); }
292 reverse_iterator rbegin() { return Impl.begin(); }
293 reverse_iterator rend() { return Impl.end(); }
294 const_reverse_iterator rbegin() const { return Impl.begin(); }
295 const_reverse_iterator rend() const { return Impl.end(); }
297 CFGElement operator[](size_t i) const {
298 assert(i < Impl.size());
299 return Impl[Impl.size() - 1 - i];
302 size_t size() const { return Impl.size(); }
303 bool empty() const { return Impl.empty(); }
306 /// Stmts - The set of statements in the basic block.
307 ElementList Elements;
309 /// Label - An (optional) label that prefixes the executable
310 /// statements in the block. When this variable is non-NULL, it is
311 /// either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
314 /// Terminator - The terminator for a basic block that
315 /// indicates the type of control-flow that occurs between a block
316 /// and its successors.
317 CFGTerminator Terminator;
319 /// LoopTarget - Some blocks are used to represent the "loop edge" to
320 /// the start of a loop from within the loop body. This Stmt* will be
321 /// refer to the loop statement for such blocks (and be null otherwise).
322 const Stmt *LoopTarget;
324 /// BlockID - A numerical ID assigned to a CFGBlock during construction
328 /// Predecessors/Successors - Keep track of the predecessor / successor
330 typedef BumpVector<CFGBlock*> AdjacentBlocks;
331 AdjacentBlocks Preds;
332 AdjacentBlocks Succs;
335 explicit CFGBlock(unsigned blockid, BumpVectorContext &C)
336 : Elements(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
337 BlockID(blockid), Preds(C, 1), Succs(C, 1) {}
340 // Statement iterators
341 typedef ElementList::iterator iterator;
342 typedef ElementList::const_iterator const_iterator;
343 typedef ElementList::reverse_iterator reverse_iterator;
344 typedef ElementList::const_reverse_iterator const_reverse_iterator;
346 CFGElement front() const { return Elements.front(); }
347 CFGElement back() const { return Elements.back(); }
349 iterator begin() { return Elements.begin(); }
350 iterator end() { return Elements.end(); }
351 const_iterator begin() const { return Elements.begin(); }
352 const_iterator end() const { return Elements.end(); }
354 reverse_iterator rbegin() { return Elements.rbegin(); }
355 reverse_iterator rend() { return Elements.rend(); }
356 const_reverse_iterator rbegin() const { return Elements.rbegin(); }
357 const_reverse_iterator rend() const { return Elements.rend(); }
359 unsigned size() const { return Elements.size(); }
360 bool empty() const { return Elements.empty(); }
362 CFGElement operator[](size_t i) const { return Elements[i]; }
365 typedef AdjacentBlocks::iterator pred_iterator;
366 typedef AdjacentBlocks::const_iterator const_pred_iterator;
367 typedef AdjacentBlocks::reverse_iterator pred_reverse_iterator;
368 typedef AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator;
370 typedef AdjacentBlocks::iterator succ_iterator;
371 typedef AdjacentBlocks::const_iterator const_succ_iterator;
372 typedef AdjacentBlocks::reverse_iterator succ_reverse_iterator;
373 typedef AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator;
375 pred_iterator pred_begin() { return Preds.begin(); }
376 pred_iterator pred_end() { return Preds.end(); }
377 const_pred_iterator pred_begin() const { return Preds.begin(); }
378 const_pred_iterator pred_end() const { return Preds.end(); }
380 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); }
381 pred_reverse_iterator pred_rend() { return Preds.rend(); }
382 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
383 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
385 succ_iterator succ_begin() { return Succs.begin(); }
386 succ_iterator succ_end() { return Succs.end(); }
387 const_succ_iterator succ_begin() const { return Succs.begin(); }
388 const_succ_iterator succ_end() const { return Succs.end(); }
390 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); }
391 succ_reverse_iterator succ_rend() { return Succs.rend(); }
392 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
393 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
395 unsigned succ_size() const { return Succs.size(); }
396 bool succ_empty() const { return Succs.empty(); }
398 unsigned pred_size() const { return Preds.size(); }
399 bool pred_empty() const { return Preds.empty(); }
402 class FilterOptions {
405 IgnoreDefaultsWithCoveredEnums = 0;
408 unsigned IgnoreDefaultsWithCoveredEnums : 1;
411 static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
412 const CFGBlock *Dst);
414 template <typename IMPL, bool IsPred>
415 class FilteredCFGBlockIterator {
418 const FilterOptions F;
419 const CFGBlock *From;
421 explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
422 const CFGBlock *from,
423 const FilterOptions &f)
424 : I(i), E(e), F(f), From(from) {}
426 bool hasMore() const { return I != E; }
428 FilteredCFGBlockIterator &operator++() {
429 do { ++I; } while (hasMore() && Filter(*I));
433 const CFGBlock *operator*() const { return *I; }
435 bool Filter(const CFGBlock *To) {
436 return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
440 typedef FilteredCFGBlockIterator<const_pred_iterator, true>
441 filtered_pred_iterator;
443 typedef FilteredCFGBlockIterator<const_succ_iterator, false>
444 filtered_succ_iterator;
446 filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
447 return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
450 filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
451 return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
454 // Manipulation of block contents
456 void setTerminator(Stmt* Statement) { Terminator = Statement; }
457 void setLabel(Stmt* Statement) { Label = Statement; }
458 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
460 CFGTerminator getTerminator() { return Terminator; }
461 const CFGTerminator getTerminator() const { return Terminator; }
463 Stmt* getTerminatorCondition();
465 const Stmt* getTerminatorCondition() const {
466 return const_cast<CFGBlock*>(this)->getTerminatorCondition();
469 const Stmt *getLoopTarget() const { return LoopTarget; }
471 Stmt* getLabel() { return Label; }
472 const Stmt* getLabel() const { return Label; }
474 unsigned getBlockID() const { return BlockID; }
476 void dump(const CFG *cfg, const LangOptions &LO) const;
477 void print(raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const;
478 void printTerminator(raw_ostream &OS, const LangOptions &LO) const;
480 void addSuccessor(CFGBlock* Block, BumpVectorContext &C) {
482 Block->Preds.push_back(this, C);
483 Succs.push_back(Block, C);
486 void appendStmt(Stmt* statement, BumpVectorContext &C) {
487 Elements.push_back(CFGStmt(statement), C);
490 void appendInitializer(CXXCtorInitializer *initializer,
491 BumpVectorContext& C) {
492 Elements.push_back(CFGInitializer(initializer), C);
495 void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) {
496 Elements.push_back(CFGBaseDtor(BS), C);
499 void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C) {
500 Elements.push_back(CFGMemberDtor(FD), C);
503 void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C) {
504 Elements.push_back(CFGTemporaryDtor(E), C);
507 // Destructors must be inserted in reversed order. So insertion is in two
508 // steps. First we prepare space for some number of elements, then we insert
509 // the elements beginning at the last position in prepared space.
510 iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt,
511 BumpVectorContext& C) {
512 return iterator(Elements.insert(I.base(), Cnt, CFGElement(), C));
514 iterator insertAutomaticObjDtor(iterator I, VarDecl* VD, Stmt* S) {
515 *I = CFGAutomaticObjDtor(VD, S);
520 /// CFG - Represents a source-level, intra-procedural CFG that represents the
521 /// control-flow of a Stmt. The Stmt can represent an entire function body,
522 /// or a single expression. A CFG will always contain one empty block that
523 /// represents the Exit point of the CFG. A CFG will also contain a designated
524 /// Entry block. The CFG solely represents control-flow; it consists of
525 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
526 /// was constructed from.
529 //===--------------------------------------------------------------------===//
530 // CFG Construction & Manipulation.
531 //===--------------------------------------------------------------------===//
534 llvm::BitVector alwaysAddMask;
536 typedef llvm::DenseMap<const Stmt *, const CFGBlock*> ForcedBlkExprs;
537 ForcedBlkExprs **forcedBlkExprs;
539 bool PruneTriviallyFalseEdges;
541 bool AddInitializers;
542 bool AddImplicitDtors;
544 bool alwaysAdd(const Stmt *stmt) const {
545 return alwaysAddMask[stmt->getStmtClass()];
548 BuildOptions &setAlwaysAdd(Stmt::StmtClass stmtClass, bool val = true) {
549 alwaysAddMask[stmtClass] = val;
553 BuildOptions &setAllAlwaysAdd() {
559 : alwaysAddMask(Stmt::lastStmtConstant, false)
560 ,forcedBlkExprs(0), PruneTriviallyFalseEdges(true)
562 ,AddInitializers(false)
563 ,AddImplicitDtors(false) {}
566 /// buildCFG - Builds a CFG from an AST. The responsibility to free the
567 /// constructed CFG belongs to the caller.
568 static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C,
569 const BuildOptions &BO);
571 /// createBlock - Create a new block in the CFG. The CFG owns the block;
572 /// the caller should not directly free it.
573 CFGBlock* createBlock();
575 /// setEntry - Set the entry block of the CFG. This is typically used
576 /// only during CFG construction. Most CFG clients expect that the
577 /// entry block has no predecessors and contains no statements.
578 void setEntry(CFGBlock *B) { Entry = B; }
580 /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
581 /// This is typically used only during CFG construction.
582 void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; }
584 //===--------------------------------------------------------------------===//
586 //===--------------------------------------------------------------------===//
588 typedef BumpVector<CFGBlock*> CFGBlockListTy;
589 typedef CFGBlockListTy::iterator iterator;
590 typedef CFGBlockListTy::const_iterator const_iterator;
591 typedef std::reverse_iterator<iterator> reverse_iterator;
592 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
594 CFGBlock& front() { return *Blocks.front(); }
595 CFGBlock& back() { return *Blocks.back(); }
597 iterator begin() { return Blocks.begin(); }
598 iterator end() { return Blocks.end(); }
599 const_iterator begin() const { return Blocks.begin(); }
600 const_iterator end() const { return Blocks.end(); }
602 reverse_iterator rbegin() { return Blocks.rbegin(); }
603 reverse_iterator rend() { return Blocks.rend(); }
604 const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
605 const_reverse_iterator rend() const { return Blocks.rend(); }
607 CFGBlock& getEntry() { return *Entry; }
608 const CFGBlock& getEntry() const { return *Entry; }
609 CFGBlock& getExit() { return *Exit; }
610 const CFGBlock& getExit() const { return *Exit; }
612 CFGBlock* getIndirectGotoBlock() { return IndirectGotoBlock; }
613 const CFGBlock* getIndirectGotoBlock() const { return IndirectGotoBlock; }
615 //===--------------------------------------------------------------------===//
616 // Member templates useful for various batch operations over CFGs.
617 //===--------------------------------------------------------------------===//
619 template <typename CALLBACK>
620 void VisitBlockStmts(CALLBACK& O) const {
621 for (const_iterator I=begin(), E=end(); I != E; ++I)
622 for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
624 if (const CFGStmt *stmt = BI->getAs<CFGStmt>())
629 //===--------------------------------------------------------------------===//
630 // CFG Introspection.
631 //===--------------------------------------------------------------------===//
633 struct BlkExprNumTy {
635 explicit BlkExprNumTy(signed idx) : Idx(idx) {}
636 explicit BlkExprNumTy() : Idx(-1) {}
637 operator bool() const { return Idx >= 0; }
638 operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
641 bool isBlkExpr(const Stmt* S) { return getBlkExprNum(S); }
642 bool isBlkExpr(const Stmt *S) const {
643 return const_cast<CFG*>(this)->isBlkExpr(S);
645 BlkExprNumTy getBlkExprNum(const Stmt* S);
646 unsigned getNumBlkExprs();
648 /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
650 unsigned getNumBlockIDs() const { return NumBlockIDs; }
652 //===--------------------------------------------------------------------===//
653 // CFG Debugging: Pretty-Printing and Visualization.
654 //===--------------------------------------------------------------------===//
656 void viewCFG(const LangOptions &LO) const;
657 void print(raw_ostream& OS, const LangOptions &LO) const;
658 void dump(const LangOptions &LO) const;
660 //===--------------------------------------------------------------------===//
661 // Internal: constructors and data.
662 //===--------------------------------------------------------------------===//
664 CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
665 BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
669 llvm::BumpPtrAllocator& getAllocator() {
670 return BlkBVC.getAllocator();
673 BumpVectorContext &getBumpVectorContext() {
680 CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch
681 // for indirect gotos
682 unsigned NumBlockIDs;
684 // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
685 // It represents a map from Expr* to integers to record the set of
686 // block-level expressions and their "statement number" in the CFG.
689 BumpVectorContext BlkBVC;
691 CFGBlockListTy Blocks;
694 } // end namespace clang
696 //===----------------------------------------------------------------------===//
697 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
698 //===----------------------------------------------------------------------===//
702 /// Implement simplify_type for CFGTerminator, so that we can dyn_cast from
703 /// CFGTerminator to a specific Stmt class.
704 template <> struct simplify_type<const ::clang::CFGTerminator> {
705 typedef const ::clang::Stmt *SimpleType;
706 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
707 return Val.getStmt();
711 template <> struct simplify_type< ::clang::CFGTerminator> {
712 typedef ::clang::Stmt *SimpleType;
713 static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
714 return const_cast<SimpleType>(Val.getStmt());
718 // Traits for: CFGBlock
720 template <> struct GraphTraits< ::clang::CFGBlock* > {
721 typedef ::clang::CFGBlock NodeType;
722 typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
724 static NodeType* getEntryNode(::clang::CFGBlock* BB)
727 static inline ChildIteratorType child_begin(NodeType* N)
728 { return N->succ_begin(); }
730 static inline ChildIteratorType child_end(NodeType* N)
731 { return N->succ_end(); }
734 template <> struct GraphTraits< const ::clang::CFGBlock* > {
735 typedef const ::clang::CFGBlock NodeType;
736 typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
738 static NodeType* getEntryNode(const clang::CFGBlock* BB)
741 static inline ChildIteratorType child_begin(NodeType* N)
742 { return N->succ_begin(); }
744 static inline ChildIteratorType child_end(NodeType* N)
745 { return N->succ_end(); }
748 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
749 typedef const ::clang::CFGBlock NodeType;
750 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
752 static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
755 static inline ChildIteratorType child_begin(NodeType* N)
756 { return N->pred_begin(); }
758 static inline ChildIteratorType child_end(NodeType* N)
759 { return N->pred_end(); }
764 template <> struct GraphTraits< ::clang::CFG* >
765 : public GraphTraits< ::clang::CFGBlock* > {
767 typedef ::clang::CFG::iterator nodes_iterator;
769 static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
770 static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); }
771 static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); }
774 template <> struct GraphTraits<const ::clang::CFG* >
775 : public GraphTraits<const ::clang::CFGBlock* > {
777 typedef ::clang::CFG::const_iterator nodes_iterator;
779 static NodeType *getEntryNode( const ::clang::CFG* F) {
780 return &F->getEntry();
782 static nodes_iterator nodes_begin( const ::clang::CFG* F) {
785 static nodes_iterator nodes_end( const ::clang::CFG* F) {
790 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
791 : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
793 typedef ::clang::CFG::const_iterator nodes_iterator;
795 static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
796 static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();}
797 static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); }
799 } // end llvm namespace