]> granicus.if.org Git - clang/blob - include/clang/Analysis/CFG.h
Teach CFGImplicitDtor::getDestructorDecl() about arrays of objects with destructors.
[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 #include <iterator>
26
27 namespace llvm {
28   class raw_ostream;
29 }
30
31 namespace clang {
32   class CXXDestructorDecl;
33   class Decl;
34   class Stmt;
35   class Expr;
36   class FieldDecl;
37   class VarDecl;
38   class CXXCtorInitializer;
39   class CXXBaseSpecifier;
40   class CXXBindTemporaryExpr;
41   class CFG;
42   class PrinterHelper;
43   class LangOptions;
44   class ASTContext;
45
46 /// CFGElement - Represents a top-level expression in a basic block.
47 class CFGElement {
48 public:
49   enum Kind {
50     // main kind
51     Invalid,
52     Statement,
53     Initializer,
54     // dtor kind
55     AutomaticObjectDtor,
56     BaseDtor,
57     MemberDtor,
58     TemporaryDtor,
59     DTOR_BEGIN = AutomaticObjectDtor,
60     DTOR_END = TemporaryDtor
61   };
62
63 protected:
64   // The int bits are used to mark the kind.
65   llvm::PointerIntPair<void *, 2> Data1;
66   llvm::PointerIntPair<void *, 2> Data2;
67
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) {}  
71
72 public:
73   CFGElement() {}
74
75   Kind getKind() const { 
76     unsigned x = Data2.getInt();
77     x <<= 2;
78     x |= Data1.getInt();
79     return (Kind) x;
80   }
81     
82   bool isValid() const { return getKind() != Invalid; }
83
84   operator bool() const { return isValid(); }
85   
86   template<class ElemTy> const ElemTy *getAs() const {
87     if (llvm::isa<ElemTy>(this))
88       return static_cast<const ElemTy*>(this);
89     return 0;
90   }
91
92   static bool classof(const CFGElement *E) { return true; }
93 };
94
95 class CFGStmt : public CFGElement {
96 public:
97   CFGStmt(Stmt *S) : CFGElement(Statement, S) {}
98
99   Stmt *getStmt() const { return static_cast<Stmt *>(Data1.getPointer()); }
100
101   static bool classof(const CFGElement *E) {
102     return E->getKind() == Statement;
103   }
104 };
105
106 /// CFGInitializer - Represents C++ base or member initializer from
107 /// constructor's initialization list.
108 class CFGInitializer : public CFGElement {
109 public:
110   CFGInitializer(CXXCtorInitializer *initializer)
111       : CFGElement(Initializer, initializer) {}
112
113   CXXCtorInitializer* getInitializer() const {
114     return static_cast<CXXCtorInitializer*>(Data1.getPointer());
115   }
116
117   static bool classof(const CFGElement *E) {
118     return E->getKind() == Initializer;
119   }
120 };
121
122 /// CFGImplicitDtor - Represents C++ object destructor implicitly generated
123 /// by compiler on various occasions.
124 class CFGImplicitDtor : public CFGElement {
125 protected:
126   CFGImplicitDtor(Kind kind, const void *data1, const void *data2 = 0) 
127     : CFGElement(kind, data1, data2) {
128     assert(kind >= DTOR_BEGIN && kind <= DTOR_END);    
129   }
130
131 public:
132   const CXXDestructorDecl *getDestructorDecl(ASTContext &astContext) const;
133   bool isNoReturn(ASTContext &astContext) const;
134
135   static bool classof(const CFGElement *E) {
136     Kind kind = E->getKind();
137     return kind >= DTOR_BEGIN && kind <= DTOR_END;
138   }
139 };
140
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 {
145 public:
146   CFGAutomaticObjDtor(const VarDecl *var, const Stmt *stmt)
147       : CFGImplicitDtor(AutomaticObjectDtor, var, stmt) {}
148
149   const VarDecl *getVarDecl() const {
150     return static_cast<VarDecl*>(Data1.getPointer());
151   }
152
153   // Get statement end of which triggered the destructor call.
154   const Stmt *getTriggerStmt() const {
155     return static_cast<Stmt*>(Data2.getPointer());
156   }
157
158   static bool classof(const CFGElement *elem) {
159     return elem->getKind() == AutomaticObjectDtor;
160   }
161 };
162
163 /// CFGBaseDtor - Represents C++ object destructor implicitly generated for
164 /// base object in destructor.
165 class CFGBaseDtor : public CFGImplicitDtor {
166 public:
167   CFGBaseDtor(const CXXBaseSpecifier *base)
168       : CFGImplicitDtor(BaseDtor, base) {}
169
170   const CXXBaseSpecifier *getBaseSpecifier() const {
171     return static_cast<const CXXBaseSpecifier*>(Data1.getPointer());
172   }
173
174   static bool classof(const CFGElement *E) {
175     return E->getKind() == BaseDtor;
176   }
177 };
178
179 /// CFGMemberDtor - Represents C++ object destructor implicitly generated for
180 /// member object in destructor.
181 class CFGMemberDtor : public CFGImplicitDtor {
182 public:
183   CFGMemberDtor(const FieldDecl *field)
184       : CFGImplicitDtor(MemberDtor, field, 0) {}
185
186   const FieldDecl *getFieldDecl() const {
187     return static_cast<const FieldDecl*>(Data1.getPointer());
188   }
189
190   static bool classof(const CFGElement *E) {
191     return E->getKind() == MemberDtor;
192   }
193 };
194
195 /// CFGTemporaryDtor - Represents C++ object destructor implicitly generated
196 /// at the end of full expression for temporary object.
197 class CFGTemporaryDtor : public CFGImplicitDtor {
198 public:
199   CFGTemporaryDtor(CXXBindTemporaryExpr *expr)
200       : CFGImplicitDtor(TemporaryDtor, expr, 0) {}
201
202   const CXXBindTemporaryExpr *getBindTemporaryExpr() const {
203     return static_cast<const CXXBindTemporaryExpr *>(Data1.getPointer());
204   }
205
206   static bool classof(const CFGElement *E) {
207     return E->getKind() == TemporaryDtor;
208   }
209 };
210
211 /// CFGTerminator - Represents CFGBlock terminator statement.
212 ///
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;
219 public:
220   CFGTerminator() {}
221   CFGTerminator(Stmt *S, bool TemporaryDtorsBranch = false)
222       : Data(S, TemporaryDtorsBranch) {}
223
224   Stmt *getStmt() { return Data.getPointer(); }
225   const Stmt *getStmt() const { return Data.getPointer(); }
226
227   bool isTemporaryDtorsBranch() const { return Data.getInt(); }
228
229   operator Stmt *() { return getStmt(); }
230   operator const Stmt *() const { return getStmt(); }
231
232   Stmt *operator->() { return getStmt(); }
233   const Stmt *operator->() const { return getStmt(); }
234
235   Stmt &operator*() { return *getStmt(); }
236   const Stmt &operator*() const { return *getStmt(); }
237
238   operator bool() const { return getStmt(); }
239 };
240
241 /// CFGBlock - Represents a single basic block in a source-level CFG.
242 ///  It consists of:
243 ///
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.
247 ///
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).
253 ///
254 /// Predecessors: the order in the set of predecessors is arbitrary.
255 ///
256 /// Successors: the order in the set of successors is NOT arbitrary.  We
257 ///  currently have the following orderings based on the terminator:
258 ///
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
264 ///
265 class CFGBlock {
266   class ElementList {
267     typedef BumpVector<CFGElement> ImplTy;
268     ImplTy Impl;
269   public:
270     ElementList(BumpVectorContext &C) : Impl(C, 4) {}
271     
272     typedef std::reverse_iterator<ImplTy::iterator>       iterator;
273     typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
274     typedef ImplTy::iterator                              reverse_iterator;
275     typedef ImplTy::const_iterator                        const_reverse_iterator;
276   
277     void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
278     reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E,
279         BumpVectorContext& C) {
280       return Impl.insert(I, Cnt, E, C);
281     }
282
283     CFGElement front() const { return Impl.back(); }
284     CFGElement back() const { return Impl.front(); }
285     
286     iterator begin() { return Impl.rbegin(); }
287     iterator end() { return Impl.rend(); }
288     const_iterator begin() const { return Impl.rbegin(); }
289     const_iterator end() const { return Impl.rend(); }
290     reverse_iterator rbegin() { return Impl.begin(); }
291     reverse_iterator rend() { return Impl.end(); }
292     const_reverse_iterator rbegin() const { return Impl.begin(); }
293     const_reverse_iterator rend() const { return Impl.end(); }
294
295    CFGElement operator[](size_t i) const  {
296      assert(i < Impl.size());
297      return Impl[Impl.size() - 1 - i];
298    }
299     
300     size_t size() const { return Impl.size(); }
301     bool empty() const { return Impl.empty(); }
302   };
303
304   /// Stmts - The set of statements in the basic block.
305   ElementList Elements;
306
307   /// Label - An (optional) label that prefixes the executable
308   ///  statements in the block.  When this variable is non-NULL, it is
309   ///  either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
310   Stmt *Label;
311
312   /// Terminator - The terminator for a basic block that
313   ///  indicates the type of control-flow that occurs between a block
314   ///  and its successors.
315   CFGTerminator Terminator;
316
317   /// LoopTarget - Some blocks are used to represent the "loop edge" to
318   ///  the start of a loop from within the loop body.  This Stmt* will be
319   ///  refer to the loop statement for such blocks (and be null otherwise).
320   const Stmt *LoopTarget;
321
322   /// BlockID - A numerical ID assigned to a CFGBlock during construction
323   ///   of the CFG.
324   unsigned BlockID;
325
326   /// Predecessors/Successors - Keep track of the predecessor / successor
327   /// CFG blocks.
328   typedef BumpVector<CFGBlock*> AdjacentBlocks;
329   AdjacentBlocks Preds;
330   AdjacentBlocks Succs;
331
332 public:
333   explicit CFGBlock(unsigned blockid, BumpVectorContext &C)
334     : Elements(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
335       BlockID(blockid), Preds(C, 1), Succs(C, 1) {}
336   ~CFGBlock() {}
337
338   // Statement iterators
339   typedef ElementList::iterator                      iterator;
340   typedef ElementList::const_iterator                const_iterator;
341   typedef ElementList::reverse_iterator              reverse_iterator;
342   typedef ElementList::const_reverse_iterator        const_reverse_iterator;
343
344   CFGElement                 front()       const { return Elements.front();   }
345   CFGElement                 back()        const { return Elements.back();    }
346
347   iterator                   begin()             { return Elements.begin();   }
348   iterator                   end()               { return Elements.end();     }
349   const_iterator             begin()       const { return Elements.begin();   }
350   const_iterator             end()         const { return Elements.end();     }
351
352   reverse_iterator           rbegin()            { return Elements.rbegin();  }
353   reverse_iterator           rend()              { return Elements.rend();    }
354   const_reverse_iterator     rbegin()      const { return Elements.rbegin();  }
355   const_reverse_iterator     rend()        const { return Elements.rend();    }
356
357   unsigned                   size()        const { return Elements.size();    }
358   bool                       empty()       const { return Elements.empty();   }
359
360   CFGElement operator[](size_t i) const  { return Elements[i]; }
361
362   // CFG iterators
363   typedef AdjacentBlocks::iterator                              pred_iterator;
364   typedef AdjacentBlocks::const_iterator                  const_pred_iterator;
365   typedef AdjacentBlocks::reverse_iterator              pred_reverse_iterator;
366   typedef AdjacentBlocks::const_reverse_iterator  const_pred_reverse_iterator;
367
368   typedef AdjacentBlocks::iterator                              succ_iterator;
369   typedef AdjacentBlocks::const_iterator                  const_succ_iterator;
370   typedef AdjacentBlocks::reverse_iterator              succ_reverse_iterator;
371   typedef AdjacentBlocks::const_reverse_iterator  const_succ_reverse_iterator;
372
373   pred_iterator                pred_begin()        { return Preds.begin();   }
374   pred_iterator                pred_end()          { return Preds.end();     }
375   const_pred_iterator          pred_begin()  const { return Preds.begin();   }
376   const_pred_iterator          pred_end()    const { return Preds.end();     }
377
378   pred_reverse_iterator        pred_rbegin()       { return Preds.rbegin();  }
379   pred_reverse_iterator        pred_rend()         { return Preds.rend();    }
380   const_pred_reverse_iterator  pred_rbegin() const { return Preds.rbegin();  }
381   const_pred_reverse_iterator  pred_rend()   const { return Preds.rend();    }
382
383   succ_iterator                succ_begin()        { return Succs.begin();   }
384   succ_iterator                succ_end()          { return Succs.end();     }
385   const_succ_iterator          succ_begin()  const { return Succs.begin();   }
386   const_succ_iterator          succ_end()    const { return Succs.end();     }
387
388   succ_reverse_iterator        succ_rbegin()       { return Succs.rbegin();  }
389   succ_reverse_iterator        succ_rend()         { return Succs.rend();    }
390   const_succ_reverse_iterator  succ_rbegin() const { return Succs.rbegin();  }
391   const_succ_reverse_iterator  succ_rend()   const { return Succs.rend();    }
392
393   unsigned                     succ_size()   const { return Succs.size();    }
394   bool                         succ_empty()  const { return Succs.empty();   }
395
396   unsigned                     pred_size()   const { return Preds.size();    }
397   bool                         pred_empty()  const { return Preds.empty();   }
398
399
400   class FilterOptions {
401   public:
402     FilterOptions() {
403       IgnoreDefaultsWithCoveredEnums = 0;
404     }
405
406     unsigned IgnoreDefaultsWithCoveredEnums : 1;
407   };
408
409   static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
410        const CFGBlock *Dst);
411
412   template <typename IMPL, bool IsPred>
413   class FilteredCFGBlockIterator {
414   private:
415     IMPL I, E;
416     const FilterOptions F;
417     const CFGBlock *From;
418    public:
419     explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
420               const CFGBlock *from,
421               const FilterOptions &f)
422       : I(i), E(e), F(f), From(from) {}
423
424     bool hasMore() const { return I != E; }
425
426     FilteredCFGBlockIterator &operator++() {
427       do { ++I; } while (hasMore() && Filter(*I));
428       return *this;
429     }
430
431     const CFGBlock *operator*() const { return *I; }
432   private:
433     bool Filter(const CFGBlock *To) {
434       return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
435     }
436   };
437
438   typedef FilteredCFGBlockIterator<const_pred_iterator, true>
439           filtered_pred_iterator;
440
441   typedef FilteredCFGBlockIterator<const_succ_iterator, false>
442           filtered_succ_iterator;
443
444   filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
445     return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
446   }
447
448   filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
449     return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
450   }
451
452   // Manipulation of block contents
453
454   void setTerminator(Stmt* Statement) { Terminator = Statement; }
455   void setLabel(Stmt* Statement) { Label = Statement; }
456   void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
457
458   CFGTerminator getTerminator() { return Terminator; }
459   const CFGTerminator getTerminator() const { return Terminator; }
460
461   Stmt* getTerminatorCondition();
462
463   const Stmt* getTerminatorCondition() const {
464     return const_cast<CFGBlock*>(this)->getTerminatorCondition();
465   }
466
467   const Stmt *getLoopTarget() const { return LoopTarget; }
468
469   bool hasBinaryBranchTerminator() const;
470
471   Stmt* getLabel() { return Label; }
472   const Stmt* getLabel() const { return Label; }
473
474   unsigned getBlockID() const { return BlockID; }
475
476   void dump(const CFG *cfg, const LangOptions &LO) const;
477   void print(llvm::raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const;
478   void printTerminator(llvm::raw_ostream &OS, const LangOptions &LO) const;
479   
480   void addSuccessor(CFGBlock* Block, BumpVectorContext &C) {
481     if (Block)
482       Block->Preds.push_back(this, C);
483     Succs.push_back(Block, C);
484   }
485   
486   void appendStmt(Stmt* statement, BumpVectorContext &C) {
487     Elements.push_back(CFGStmt(statement), C);
488   }
489
490   void appendInitializer(CXXCtorInitializer *initializer,
491                         BumpVectorContext& C) {
492     Elements.push_back(CFGInitializer(initializer), C);
493   }
494
495   void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) {
496     Elements.push_back(CFGBaseDtor(BS), C);
497   }
498
499   void appendMemberDtor(FieldDecl *FD, BumpVectorContext &C) {
500     Elements.push_back(CFGMemberDtor(FD), C);
501   }
502   
503   void appendTemporaryDtor(CXXBindTemporaryExpr *E, BumpVectorContext &C) {
504     Elements.push_back(CFGTemporaryDtor(E), C);
505   }
506
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));
513   }
514   iterator insertAutomaticObjDtor(iterator I, VarDecl* VD, Stmt* S) {
515     *I = CFGAutomaticObjDtor(VD, S);
516     return ++I;
517   }
518 };
519
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.
527 class CFG {
528 public:
529   //===--------------------------------------------------------------------===//
530   // CFG Construction & Manipulation.
531   //===--------------------------------------------------------------------===//
532
533   class BuildOptions {
534   public:
535     bool PruneTriviallyFalseEdges:1;
536     bool AddEHEdges:1;
537     bool AddInitializers:1;
538     bool AddImplicitDtors:1;
539
540     BuildOptions()
541         : PruneTriviallyFalseEdges(true)
542         , AddEHEdges(false)
543         , AddInitializers(false)
544         , AddImplicitDtors(false) {}
545   };
546
547   /// buildCFG - Builds a CFG from an AST.  The responsibility to free the
548   ///   constructed CFG belongs to the caller.
549   static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C,
550       BuildOptions BO = BuildOptions());
551
552   /// createBlock - Create a new block in the CFG.  The CFG owns the block;
553   ///  the caller should not directly free it.
554   CFGBlock* createBlock();
555
556   /// setEntry - Set the entry block of the CFG.  This is typically used
557   ///  only during CFG construction.  Most CFG clients expect that the
558   ///  entry block has no predecessors and contains no statements.
559   void setEntry(CFGBlock *B) { Entry = B; }
560
561   /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
562   ///  This is typically used only during CFG construction.
563   void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; }
564
565   //===--------------------------------------------------------------------===//
566   // Block Iterators
567   //===--------------------------------------------------------------------===//
568
569   typedef BumpVector<CFGBlock*>                    CFGBlockListTy;    
570   typedef CFGBlockListTy::iterator                 iterator;
571   typedef CFGBlockListTy::const_iterator           const_iterator;
572   typedef std::reverse_iterator<iterator>          reverse_iterator;
573   typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
574
575   CFGBlock&                 front()                { return *Blocks.front(); }
576   CFGBlock&                 back()                 { return *Blocks.back(); }
577
578   iterator                  begin()                { return Blocks.begin(); }
579   iterator                  end()                  { return Blocks.end(); }
580   const_iterator            begin()       const    { return Blocks.begin(); }
581   const_iterator            end()         const    { return Blocks.end(); }
582
583   reverse_iterator          rbegin()               { return Blocks.rbegin(); }
584   reverse_iterator          rend()                 { return Blocks.rend(); }
585   const_reverse_iterator    rbegin()      const    { return Blocks.rbegin(); }
586   const_reverse_iterator    rend()        const    { return Blocks.rend(); }
587
588   CFGBlock&                 getEntry()             { return *Entry; }
589   const CFGBlock&           getEntry()    const    { return *Entry; }
590   CFGBlock&                 getExit()              { return *Exit; }
591   const CFGBlock&           getExit()     const    { return *Exit; }
592
593   CFGBlock*        getIndirectGotoBlock() { return IndirectGotoBlock; }
594   const CFGBlock*  getIndirectGotoBlock() const { return IndirectGotoBlock; }
595
596   //===--------------------------------------------------------------------===//
597   // Member templates useful for various batch operations over CFGs.
598   //===--------------------------------------------------------------------===//
599
600   template <typename CALLBACK>
601   void VisitBlockStmts(CALLBACK& O) const {
602     for (const_iterator I=begin(), E=end(); I != E; ++I)
603       for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
604            BI != BE; ++BI) {
605         if (const CFGStmt *stmt = BI->getAs<CFGStmt>())
606           O(stmt->getStmt());
607       }
608   }
609
610   //===--------------------------------------------------------------------===//
611   // CFG Introspection.
612   //===--------------------------------------------------------------------===//
613
614   struct   BlkExprNumTy {
615     const signed Idx;
616     explicit BlkExprNumTy(signed idx) : Idx(idx) {}
617     explicit BlkExprNumTy() : Idx(-1) {}
618     operator bool() const { return Idx >= 0; }
619     operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
620   };
621
622   bool isBlkExpr(const Stmt* S) { return getBlkExprNum(S); }
623   bool isBlkExpr(const Stmt *S) const {
624     return const_cast<CFG*>(this)->isBlkExpr(S);
625   }
626   BlkExprNumTy  getBlkExprNum(const Stmt* S);
627   unsigned      getNumBlkExprs();
628
629   /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
630   /// start at 0).
631   unsigned getNumBlockIDs() const { return NumBlockIDs; }
632
633   //===--------------------------------------------------------------------===//
634   // CFG Debugging: Pretty-Printing and Visualization.
635   //===--------------------------------------------------------------------===//
636
637   void viewCFG(const LangOptions &LO) const;
638   void print(llvm::raw_ostream& OS, const LangOptions &LO) const;
639   void dump(const LangOptions &LO) const;
640
641   //===--------------------------------------------------------------------===//
642   // Internal: constructors and data.
643   //===--------------------------------------------------------------------===//
644
645   CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
646           BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
647
648   ~CFG();
649
650   llvm::BumpPtrAllocator& getAllocator() {
651     return BlkBVC.getAllocator();
652   }
653   
654   BumpVectorContext &getBumpVectorContext() {
655     return BlkBVC;
656   }
657
658 private:
659   CFGBlock* Entry;
660   CFGBlock* Exit;
661   CFGBlock* IndirectGotoBlock;  // Special block to contain collective dispatch
662                                 // for indirect gotos
663   unsigned  NumBlockIDs;
664
665   // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
666   //  It represents a map from Expr* to integers to record the set of
667   //  block-level expressions and their "statement number" in the CFG.
668   void*     BlkExprMap;
669   
670   BumpVectorContext BlkBVC;
671   
672   CFGBlockListTy Blocks;
673
674 };
675 } // end namespace clang
676
677 //===----------------------------------------------------------------------===//
678 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
679 //===----------------------------------------------------------------------===//
680
681 namespace llvm {
682
683 /// Implement simplify_type for CFGTerminator, so that we can dyn_cast from
684 /// CFGTerminator to a specific Stmt class.
685 template <> struct simplify_type<const ::clang::CFGTerminator> {
686   typedef const ::clang::Stmt *SimpleType;
687   static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
688     return Val.getStmt();
689   }
690 };
691
692 template <> struct simplify_type< ::clang::CFGTerminator> {
693   typedef ::clang::Stmt *SimpleType;
694   static SimpleType getSimplifiedValue(const ::clang::CFGTerminator &Val) {
695     return const_cast<SimpleType>(Val.getStmt());
696   }
697 };
698
699 // Traits for: CFGBlock
700
701 template <> struct GraphTraits< ::clang::CFGBlock* > {
702   typedef ::clang::CFGBlock NodeType;
703   typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
704
705   static NodeType* getEntryNode(::clang::CFGBlock* BB)
706   { return BB; }
707
708   static inline ChildIteratorType child_begin(NodeType* N)
709   { return N->succ_begin(); }
710
711   static inline ChildIteratorType child_end(NodeType* N)
712   { return N->succ_end(); }
713 };
714
715 template <> struct GraphTraits< const ::clang::CFGBlock* > {
716   typedef const ::clang::CFGBlock NodeType;
717   typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
718
719   static NodeType* getEntryNode(const clang::CFGBlock* BB)
720   { return BB; }
721
722   static inline ChildIteratorType child_begin(NodeType* N)
723   { return N->succ_begin(); }
724
725   static inline ChildIteratorType child_end(NodeType* N)
726   { return N->succ_end(); }
727 };
728
729 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
730   typedef const ::clang::CFGBlock NodeType;
731   typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
732
733   static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
734   { return G.Graph; }
735
736   static inline ChildIteratorType child_begin(NodeType* N)
737   { return N->pred_begin(); }
738
739   static inline ChildIteratorType child_end(NodeType* N)
740   { return N->pred_end(); }
741 };
742
743 // Traits for: CFG
744
745 template <> struct GraphTraits< ::clang::CFG* >
746     : public GraphTraits< ::clang::CFGBlock* >  {
747
748   typedef ::clang::CFG::iterator nodes_iterator;
749
750   static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
751   static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); }
752   static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); }
753 };
754
755 template <> struct GraphTraits<const ::clang::CFG* >
756     : public GraphTraits<const ::clang::CFGBlock* >  {
757
758   typedef ::clang::CFG::const_iterator nodes_iterator;
759
760   static NodeType *getEntryNode( const ::clang::CFG* F) {
761     return &F->getEntry();
762   }
763   static nodes_iterator nodes_begin( const ::clang::CFG* F) {
764     return F->begin();
765   }
766   static nodes_iterator nodes_end( const ::clang::CFG* F) {
767     return F->end();
768   }
769 };
770
771 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
772   : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
773
774   typedef ::clang::CFG::const_iterator nodes_iterator;
775
776   static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
777   static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();}
778   static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); }
779 };
780 } // end llvm namespace
781 #endif