]> granicus.if.org Git - clang/blob - include/clang/Analysis/CFG.h
Added methods for inserting CFGAutomaticObjDtors to CFGBlocks,
[clang] / include / clang / Analysis / CFG.h
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
26 namespace llvm {
27   class raw_ostream;
28 }
29
30 namespace 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.
42 class CFGElement {
43 public:
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
58 protected:
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
68 public:
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
91 class CFGStmt : public CFGElement {
92 public:
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.
111 class CFGInitializer : public CFGElement {
112 public:
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.
129 class CFGImplicitDtor : public CFGElement {
130 protected:
131   CFGImplicitDtor(unsigned K, void* P, void* S)
132       : CFGElement(P, Dtor, S, K - DTOR_BEGIN) {}
133
134 public:
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.
145 class CFGAutomaticObjDtor: public CFGImplicitDtor {
146 public:
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
165 class CFGBaseDtor : public CFGImplicitDtor {
166 public:
167   static bool classof(const CFGElement *E) {
168     return E->getKind() == Dtor && E->getDtorKind() == BaseDtor;
169   }
170 };
171
172 class CFGMemberDtor : public CFGImplicitDtor {
173 public:
174   static bool classof(const CFGElement *E) {
175     return E->getKind() == Dtor && E->getDtorKind() == MemberDtor;
176   }
177
178 };
179
180 class CFGTemporaryDtor : public CFGImplicitDtor {
181 public:
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 ///
211 class 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
278 public:
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   // Destructors must be inserted in reversed order. So insertion is in two
437   // steps. First we prepare space for some number of elements, then we insert
438   // the elements beginning at the last position in prepared space.
439   iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt,
440       BumpVectorContext& C) {
441     return iterator(Elements.insert(I.base(), Cnt, CFGElement(), C));
442   }
443   iterator insertAutomaticObjDtor(iterator I, VarDecl* VD, Stmt* S) {
444     *I = CFGAutomaticObjDtor(VD, S);
445     return ++I;
446   }
447 };
448
449 /// CFG - Represents a source-level, intra-procedural CFG that represents the
450 ///  control-flow of a Stmt.  The Stmt can represent an entire function body,
451 ///  or a single expression.  A CFG will always contain one empty block that
452 ///  represents the Exit point of the CFG.  A CFG will also contain a designated
453 ///  Entry block.  The CFG solely represents control-flow; it consists of
454 ///  CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
455 ///  was constructed from.
456 class CFG {
457 public:
458   //===--------------------------------------------------------------------===//
459   // CFG Construction & Manipulation.
460   //===--------------------------------------------------------------------===//
461
462   class BuildOptions {
463   public:
464     bool PruneTriviallyFalseEdges:1;
465     bool AddEHEdges:1;
466     bool AddInitializers:1;
467     bool AddImplicitDtors:1;
468
469     BuildOptions()
470         : PruneTriviallyFalseEdges(true)
471         , AddEHEdges(false)
472         , AddInitializers(false)
473         , AddImplicitDtors(false) {}
474   };
475
476   /// buildCFG - Builds a CFG from an AST.  The responsibility to free the
477   ///   constructed CFG belongs to the caller.
478   static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C,
479       BuildOptions BO = BuildOptions());
480
481   /// createBlock - Create a new block in the CFG.  The CFG owns the block;
482   ///  the caller should not directly free it.
483   CFGBlock* createBlock();
484
485   /// setEntry - Set the entry block of the CFG.  This is typically used
486   ///  only during CFG construction.  Most CFG clients expect that the
487   ///  entry block has no predecessors and contains no statements.
488   void setEntry(CFGBlock *B) { Entry = B; }
489
490   /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
491   ///  This is typically used only during CFG construction.
492   void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; }
493
494   //===--------------------------------------------------------------------===//
495   // Block Iterators
496   //===--------------------------------------------------------------------===//
497
498   typedef BumpVector<CFGBlock*>                    CFGBlockListTy;    
499   typedef CFGBlockListTy::iterator                 iterator;
500   typedef CFGBlockListTy::const_iterator           const_iterator;
501   typedef std::reverse_iterator<iterator>          reverse_iterator;
502   typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
503
504   CFGBlock&                 front()                { return *Blocks.front(); }
505   CFGBlock&                 back()                 { return *Blocks.back(); }
506
507   iterator                  begin()                { return Blocks.begin(); }
508   iterator                  end()                  { return Blocks.end(); }
509   const_iterator            begin()       const    { return Blocks.begin(); }
510   const_iterator            end()         const    { return Blocks.end(); }
511
512   reverse_iterator          rbegin()               { return Blocks.rbegin(); }
513   reverse_iterator          rend()                 { return Blocks.rend(); }
514   const_reverse_iterator    rbegin()      const    { return Blocks.rbegin(); }
515   const_reverse_iterator    rend()        const    { return Blocks.rend(); }
516
517   CFGBlock&                 getEntry()             { return *Entry; }
518   const CFGBlock&           getEntry()    const    { return *Entry; }
519   CFGBlock&                 getExit()              { return *Exit; }
520   const CFGBlock&           getExit()     const    { return *Exit; }
521
522   CFGBlock*        getIndirectGotoBlock() { return IndirectGotoBlock; }
523   const CFGBlock*  getIndirectGotoBlock() const { return IndirectGotoBlock; }
524
525   //===--------------------------------------------------------------------===//
526   // Member templates useful for various batch operations over CFGs.
527   //===--------------------------------------------------------------------===//
528
529   template <typename CALLBACK>
530   void VisitBlockStmts(CALLBACK& O) const {
531     for (const_iterator I=begin(), E=end(); I != E; ++I)
532       for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
533            BI != BE; ++BI) {
534         if (CFGStmt S = BI->getAs<CFGStmt>())
535           O(S);
536       }
537   }
538
539   //===--------------------------------------------------------------------===//
540   // CFG Introspection.
541   //===--------------------------------------------------------------------===//
542
543   struct   BlkExprNumTy {
544     const signed Idx;
545     explicit BlkExprNumTy(signed idx) : Idx(idx) {}
546     explicit BlkExprNumTy() : Idx(-1) {}
547     operator bool() const { return Idx >= 0; }
548     operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
549   };
550
551   bool          isBlkExpr(const Stmt* S) { return getBlkExprNum(S); }
552   BlkExprNumTy  getBlkExprNum(const Stmt* S);
553   unsigned      getNumBlkExprs();
554
555   /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
556   /// start at 0).
557   unsigned getNumBlockIDs() const { return NumBlockIDs; }
558
559   //===--------------------------------------------------------------------===//
560   // CFG Debugging: Pretty-Printing and Visualization.
561   //===--------------------------------------------------------------------===//
562
563   void viewCFG(const LangOptions &LO) const;
564   void print(llvm::raw_ostream& OS, const LangOptions &LO) const;
565   void dump(const LangOptions &LO) const;
566
567   //===--------------------------------------------------------------------===//
568   // Internal: constructors and data.
569   //===--------------------------------------------------------------------===//
570
571   CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
572           BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
573
574   ~CFG();
575
576   llvm::BumpPtrAllocator& getAllocator() {
577     return BlkBVC.getAllocator();
578   }
579   
580   BumpVectorContext &getBumpVectorContext() {
581     return BlkBVC;
582   }
583
584 private:
585   CFGBlock* Entry;
586   CFGBlock* Exit;
587   CFGBlock* IndirectGotoBlock;  // Special block to contain collective dispatch
588                                 // for indirect gotos
589   unsigned  NumBlockIDs;
590
591   // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
592   //  It represents a map from Expr* to integers to record the set of
593   //  block-level expressions and their "statement number" in the CFG.
594   void*     BlkExprMap;
595   
596   BumpVectorContext BlkBVC;
597   
598   CFGBlockListTy Blocks;
599
600 };
601 } // end namespace clang
602
603 //===----------------------------------------------------------------------===//
604 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
605 //===----------------------------------------------------------------------===//
606
607 namespace llvm {
608
609 // Traits for: CFGBlock
610
611 template <> struct GraphTraits< ::clang::CFGBlock* > {
612   typedef ::clang::CFGBlock NodeType;
613   typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
614
615   static NodeType* getEntryNode(::clang::CFGBlock* BB)
616   { return BB; }
617
618   static inline ChildIteratorType child_begin(NodeType* N)
619   { return N->succ_begin(); }
620
621   static inline ChildIteratorType child_end(NodeType* N)
622   { return N->succ_end(); }
623 };
624
625 template <> struct GraphTraits< const ::clang::CFGBlock* > {
626   typedef const ::clang::CFGBlock NodeType;
627   typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
628
629   static NodeType* getEntryNode(const clang::CFGBlock* BB)
630   { return BB; }
631
632   static inline ChildIteratorType child_begin(NodeType* N)
633   { return N->succ_begin(); }
634
635   static inline ChildIteratorType child_end(NodeType* N)
636   { return N->succ_end(); }
637 };
638
639 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
640   typedef const ::clang::CFGBlock NodeType;
641   typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
642
643   static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
644   { return G.Graph; }
645
646   static inline ChildIteratorType child_begin(NodeType* N)
647   { return N->pred_begin(); }
648
649   static inline ChildIteratorType child_end(NodeType* N)
650   { return N->pred_end(); }
651 };
652
653 // Traits for: CFG
654
655 template <> struct GraphTraits< ::clang::CFG* >
656     : public GraphTraits< ::clang::CFGBlock* >  {
657
658   typedef ::clang::CFG::iterator nodes_iterator;
659
660   static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
661   static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); }
662   static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); }
663 };
664
665 template <> struct GraphTraits<const ::clang::CFG* >
666     : public GraphTraits<const ::clang::CFGBlock* >  {
667
668   typedef ::clang::CFG::const_iterator nodes_iterator;
669
670   static NodeType *getEntryNode( const ::clang::CFG* F) {
671     return &F->getEntry();
672   }
673   static nodes_iterator nodes_begin( const ::clang::CFG* F) {
674     return F->begin();
675   }
676   static nodes_iterator nodes_end( const ::clang::CFG* F) {
677     return F->end();
678   }
679 };
680
681 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
682   : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
683
684   typedef ::clang::CFG::const_iterator nodes_iterator;
685
686   static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
687   static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();}
688   static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); }
689 };
690 } // end llvm namespace
691 #endif