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