]> granicus.if.org Git - clang/blob - include/clang/Analysis/CFG.h
Remove dead code.
[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 namespace clang {
30   class Decl;
31   class Stmt;
32   class Expr;
33   class CFG;
34   class PrinterHelper;
35   class LangOptions;
36   class ASTContext;
37
38 namespace {
39 // An element of the CFG for implicit descructor calls implied by the language
40 // rules.
41 class Dtor {
42   // Statement that introduces the variable.
43   Stmt *S;
44   // A token which ends the scope, return, goto, throw, }.
45   SourceLocation Loc;
46 public:
47   Dtor(Stmt *s, SourceLocation l) : S(s), Loc(l) {
48   }
49   SourceLocation getLoc() { return Loc; }
50   Stmt *getStmt() { return S; }
51 };
52 }
53
54 /// CFGElement - Represents a top-level expression in a basic block.
55 class CFGElement {
56   llvm::PointerIntPair<Stmt *, 2> Data;
57 public:
58   enum Type { StartScope, EndScope };
59   explicit CFGElement() {}
60   CFGElement(Stmt *S, bool lvalue) : Data(S, lvalue ? 1 : 0) {}
61   CFGElement(Stmt *S, Type t) : Data(S, t == StartScope ? 2 : 3) {}
62   // CFGElement(Dtor *S, Type t) : Data(reinterpret_cast<Stmt*>(S), 4) {}
63   Stmt *getStmt() const { return Data.getPointer(); }
64   bool asLValue() const { return Data.getInt() == 1; }
65   bool asStartScope() const { return Data.getInt() == 2; }
66   bool asEndScope() const { return Data.getInt() == 3; }
67   bool asDtor() const { return Data.getInt() == 4; }
68   operator Stmt*() const { return getStmt(); }
69   operator bool() const { return getStmt() != 0; }
70   operator Dtor*() const { return reinterpret_cast<Dtor*>(getStmt()); }
71 };
72
73 /// CFGBlock - Represents a single basic block in a source-level CFG.
74 ///  It consists of:
75 ///
76 ///  (1) A set of statements/expressions (which may contain subexpressions).
77 ///  (2) A "terminator" statement (not in the set of statements).
78 ///  (3) A list of successors and predecessors.
79 ///
80 /// Terminator: The terminator represents the type of control-flow that occurs
81 /// at the end of the basic block.  The terminator is a Stmt* referring to an
82 /// AST node that has control-flow: if-statements, breaks, loops, etc.
83 /// If the control-flow is conditional, the condition expression will appear
84 /// within the set of statements in the block (usually the last statement).
85 ///
86 /// Predecessors: the order in the set of predecessors is arbitrary.
87 ///
88 /// Successors: the order in the set of successors is NOT arbitrary.  We
89 ///  currently have the following orderings based on the terminator:
90 ///
91 ///     Terminator       Successor Ordering
92 ///  -----------------------------------------------------
93 ///       if            Then Block;  Else Block
94 ///     ? operator      LHS expression;  RHS expression
95 ///     &&, ||          expression that uses result of && or ||, RHS
96 ///
97 class CFGBlock {
98   class StatementList {
99     typedef BumpVector<CFGElement> ImplTy;
100     ImplTy Impl;
101   public:
102     StatementList(BumpVectorContext &C) : Impl(C, 4) {}
103     
104     typedef std::reverse_iterator<ImplTy::iterator>       iterator;
105     typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
106     typedef ImplTy::iterator                              reverse_iterator;
107     typedef ImplTy::const_iterator                        const_reverse_iterator;
108   
109     void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
110     CFGElement front() const { return Impl.back(); }
111     CFGElement back() const { return Impl.front(); }
112     
113     iterator begin() { return Impl.rbegin(); }
114     iterator end() { return Impl.rend(); }
115     const_iterator begin() const { return Impl.rbegin(); }
116     const_iterator end() const { return Impl.rend(); }
117     reverse_iterator rbegin() { return Impl.begin(); }
118     reverse_iterator rend() { return Impl.end(); }
119     const_reverse_iterator rbegin() const { return Impl.begin(); }
120     const_reverse_iterator rend() const { return Impl.end(); }
121
122    CFGElement operator[](size_t i) const  {
123      assert(i < Impl.size());
124      return Impl[Impl.size() - 1 - i];
125    }
126     
127     size_t size() const { return Impl.size(); }
128     bool empty() const { return Impl.empty(); }
129   };
130
131   /// Stmts - The set of statements in the basic block.
132   StatementList Stmts;
133
134   /// Label - An (optional) label that prefixes the executable
135   ///  statements in the block.  When this variable is non-NULL, it is
136   ///  either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
137   Stmt *Label;
138
139   /// Terminator - The terminator for a basic block that
140   ///  indicates the type of control-flow that occurs between a block
141   ///  and its successors.
142   Stmt *Terminator;
143
144   /// LoopTarget - Some blocks are used to represent the "loop edge" to
145   ///  the start of a loop from within the loop body.  This Stmt* will be
146   ///  refer to the loop statement for such blocks (and be null otherwise).
147   const Stmt *LoopTarget;
148
149   /// BlockID - A numerical ID assigned to a CFGBlock during construction
150   ///   of the CFG.
151   unsigned BlockID;
152
153   /// Predecessors/Successors - Keep track of the predecessor / successor
154   /// CFG blocks.
155   typedef BumpVector<CFGBlock*> AdjacentBlocks;
156   AdjacentBlocks Preds;
157   AdjacentBlocks Succs;
158
159 public:
160   explicit CFGBlock(unsigned blockid, BumpVectorContext &C)
161     : Stmts(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
162       BlockID(blockid), Preds(C, 1), Succs(C, 1) {}
163   ~CFGBlock() {}
164
165   // Statement iterators
166   typedef StatementList::iterator                      iterator;
167   typedef StatementList::const_iterator                const_iterator;
168   typedef StatementList::reverse_iterator              reverse_iterator;
169   typedef StatementList::const_reverse_iterator        const_reverse_iterator;
170
171   CFGElement                   front()       const { return Stmts.front();   }
172   CFGElement                   back()        const { return Stmts.back();    }
173
174   iterator                     begin()             { return Stmts.begin();   }
175   iterator                     end()               { return Stmts.end();     }
176   const_iterator               begin()       const { return Stmts.begin();   }
177   const_iterator               end()         const { return Stmts.end();     }
178
179   reverse_iterator             rbegin()            { return Stmts.rbegin();  }
180   reverse_iterator             rend()              { return Stmts.rend();    }
181   const_reverse_iterator       rbegin()      const { return Stmts.rbegin();  }
182   const_reverse_iterator       rend()        const { return Stmts.rend();    }
183
184   unsigned                     size()        const { return Stmts.size();    }
185   bool                         empty()       const { return Stmts.empty();   }
186
187   CFGElement operator[](size_t i) const  { return Stmts[i]; }
188
189   // CFG iterators
190   typedef AdjacentBlocks::iterator                              pred_iterator;
191   typedef AdjacentBlocks::const_iterator                  const_pred_iterator;
192   typedef AdjacentBlocks::reverse_iterator              pred_reverse_iterator;
193   typedef AdjacentBlocks::const_reverse_iterator  const_pred_reverse_iterator;
194
195   typedef AdjacentBlocks::iterator                              succ_iterator;
196   typedef AdjacentBlocks::const_iterator                  const_succ_iterator;
197   typedef AdjacentBlocks::reverse_iterator              succ_reverse_iterator;
198   typedef AdjacentBlocks::const_reverse_iterator  const_succ_reverse_iterator;
199
200   pred_iterator                pred_begin()        { return Preds.begin();   }
201   pred_iterator                pred_end()          { return Preds.end();     }
202   const_pred_iterator          pred_begin()  const { return Preds.begin();   }
203   const_pred_iterator          pred_end()    const { return Preds.end();     }
204
205   pred_reverse_iterator        pred_rbegin()       { return Preds.rbegin();  }
206   pred_reverse_iterator        pred_rend()         { return Preds.rend();    }
207   const_pred_reverse_iterator  pred_rbegin() const { return Preds.rbegin();  }
208   const_pred_reverse_iterator  pred_rend()   const { return Preds.rend();    }
209
210   succ_iterator                succ_begin()        { return Succs.begin();   }
211   succ_iterator                succ_end()          { return Succs.end();     }
212   const_succ_iterator          succ_begin()  const { return Succs.begin();   }
213   const_succ_iterator          succ_end()    const { return Succs.end();     }
214
215   succ_reverse_iterator        succ_rbegin()       { return Succs.rbegin();  }
216   succ_reverse_iterator        succ_rend()         { return Succs.rend();    }
217   const_succ_reverse_iterator  succ_rbegin() const { return Succs.rbegin();  }
218   const_succ_reverse_iterator  succ_rend()   const { return Succs.rend();    }
219
220   unsigned                     succ_size()   const { return Succs.size();    }
221   bool                         succ_empty()  const { return Succs.empty();   }
222
223   unsigned                     pred_size()   const { return Preds.size();    }
224   bool                         pred_empty()  const { return Preds.empty();   }
225
226   // Manipulation of block contents
227
228   void setTerminator(Stmt* Statement) { Terminator = Statement; }
229   void setLabel(Stmt* Statement) { Label = Statement; }
230   void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
231
232   Stmt* getTerminator() { return Terminator; }
233   const Stmt* getTerminator() const { return Terminator; }
234
235   Stmt* getTerminatorCondition();
236
237   const Stmt* getTerminatorCondition() const {
238     return const_cast<CFGBlock*>(this)->getTerminatorCondition();
239   }
240
241   const Stmt *getLoopTarget() const { return LoopTarget; }
242
243   bool hasBinaryBranchTerminator() const;
244
245   Stmt* getLabel() { return Label; }
246   const Stmt* getLabel() const { return Label; }
247
248   unsigned getBlockID() const { return BlockID; }
249
250   void dump(const CFG *cfg, const LangOptions &LO) const;
251   void print(llvm::raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const;
252   void printTerminator(llvm::raw_ostream &OS, const LangOptions &LO) const;
253   
254   void addSuccessor(CFGBlock* Block, BumpVectorContext &C) {
255     if (Block)
256       Block->Preds.push_back(this, C);
257     Succs.push_back(Block, C);
258   }
259   
260   void appendStmt(Stmt* Statement, BumpVectorContext &C, bool asLValue) {
261       Stmts.push_back(CFGElement(Statement, asLValue), C);
262   }  
263   void StartScope(Stmt* S, BumpVectorContext &C) {
264     Stmts.push_back(CFGElement(S, CFGElement::StartScope), C);
265   }
266   void EndScope(Stmt* S, BumpVectorContext &C) {
267     Stmts.push_back(CFGElement(S, CFGElement::EndScope), C);
268   }
269 };
270
271
272 /// CFG - Represents a source-level, intra-procedural CFG that represents the
273 ///  control-flow of a Stmt.  The Stmt can represent an entire function body,
274 ///  or a single expression.  A CFG will always contain one empty block that
275 ///  represents the Exit point of the CFG.  A CFG will also contain a designated
276 ///  Entry block.  The CFG solely represents control-flow; it consists of
277 ///  CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
278 ///  was constructed from.
279 class CFG {
280 public:
281   //===--------------------------------------------------------------------===//
282   // CFG Construction & Manipulation.
283   //===--------------------------------------------------------------------===//
284
285   /// buildCFG - Builds a CFG from an AST.  The responsibility to free the
286   ///   constructed CFG belongs to the caller.
287   static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C,
288                        bool AddEHEdges = false,
289                        bool AddScopes = false);
290
291   /// createBlock - Create a new block in the CFG.  The CFG owns the block;
292   ///  the caller should not directly free it.
293   CFGBlock* createBlock();
294
295   /// setEntry - Set the entry block of the CFG.  This is typically used
296   ///  only during CFG construction.  Most CFG clients expect that the
297   ///  entry block has no predecessors and contains no statements.
298   void setEntry(CFGBlock *B) { Entry = B; }
299
300   /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
301   ///  This is typically used only during CFG construction.
302   void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; }
303
304   //===--------------------------------------------------------------------===//
305   // Block Iterators
306   //===--------------------------------------------------------------------===//
307
308   typedef BumpVector<CFGBlock*>                    CFGBlockListTy;    
309   typedef CFGBlockListTy::iterator                 iterator;
310   typedef CFGBlockListTy::const_iterator           const_iterator;
311   typedef std::reverse_iterator<iterator>          reverse_iterator;
312   typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
313
314   CFGBlock&                 front()                { return *Blocks.front(); }
315   CFGBlock&                 back()                 { return *Blocks.back(); }
316
317   iterator                  begin()                { return Blocks.begin(); }
318   iterator                  end()                  { return Blocks.end(); }
319   const_iterator            begin()       const    { return Blocks.begin(); }
320   const_iterator            end()         const    { return Blocks.end(); }
321
322   reverse_iterator          rbegin()               { return Blocks.rbegin(); }
323   reverse_iterator          rend()                 { return Blocks.rend(); }
324   const_reverse_iterator    rbegin()      const    { return Blocks.rbegin(); }
325   const_reverse_iterator    rend()        const    { return Blocks.rend(); }
326
327   CFGBlock&                 getEntry()             { return *Entry; }
328   const CFGBlock&           getEntry()    const    { return *Entry; }
329   CFGBlock&                 getExit()              { return *Exit; }
330   const CFGBlock&           getExit()     const    { return *Exit; }
331
332   CFGBlock*        getIndirectGotoBlock() { return IndirectGotoBlock; }
333   const CFGBlock*  getIndirectGotoBlock() const { return IndirectGotoBlock; }
334
335   //===--------------------------------------------------------------------===//
336   // Member templates useful for various batch operations over CFGs.
337   //===--------------------------------------------------------------------===//
338
339   template <typename CALLBACK>
340   void VisitBlockStmts(CALLBACK& O) const {
341     for (const_iterator I=begin(), E=end(); I != E; ++I)
342       for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
343            BI != BE; ++BI)
344         O(*BI);
345   }
346
347   //===--------------------------------------------------------------------===//
348   // CFG Introspection.
349   //===--------------------------------------------------------------------===//
350
351   struct   BlkExprNumTy {
352     const signed Idx;
353     explicit BlkExprNumTy(signed idx) : Idx(idx) {}
354     explicit BlkExprNumTy() : Idx(-1) {}
355     operator bool() const { return Idx >= 0; }
356     operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
357   };
358
359   bool          isBlkExpr(const Stmt* S) { return getBlkExprNum(S); }
360   BlkExprNumTy  getBlkExprNum(const Stmt* S);
361   unsigned      getNumBlkExprs();
362
363   /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
364   /// start at 0).
365   unsigned getNumBlockIDs() const { return NumBlockIDs; }
366
367   //===--------------------------------------------------------------------===//
368   // CFG Debugging: Pretty-Printing and Visualization.
369   //===--------------------------------------------------------------------===//
370
371   void viewCFG(const LangOptions &LO) const;
372   void print(llvm::raw_ostream& OS, const LangOptions &LO) const;
373   void dump(const LangOptions &LO) const;
374
375   //===--------------------------------------------------------------------===//
376   // Internal: constructors and data.
377   //===--------------------------------------------------------------------===//
378
379   CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
380           BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
381
382   ~CFG();
383
384   llvm::BumpPtrAllocator& getAllocator() {
385     return BlkBVC.getAllocator();
386   }
387   
388   BumpVectorContext &getBumpVectorContext() {
389     return BlkBVC;
390   }
391
392 private:
393   CFGBlock* Entry;
394   CFGBlock* Exit;
395   CFGBlock* IndirectGotoBlock;  // Special block to contain collective dispatch
396                                 // for indirect gotos
397   unsigned  NumBlockIDs;
398
399   // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
400   //  It represents a map from Expr* to integers to record the set of
401   //  block-level expressions and their "statement number" in the CFG.
402   void*     BlkExprMap;
403   
404   BumpVectorContext BlkBVC;
405   
406   CFGBlockListTy Blocks;
407
408 };
409 } // end namespace clang
410
411 //===----------------------------------------------------------------------===//
412 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
413 //===----------------------------------------------------------------------===//
414
415 namespace llvm {
416
417 /// Implement simplify_type for CFGElement, so that we can dyn_cast from
418 /// CFGElement to a specific Stmt class.
419 template <> struct simplify_type<const ::clang::CFGElement> {
420   typedef ::clang::Stmt* SimpleType;
421   static SimpleType getSimplifiedValue(const ::clang::CFGElement &Val) {
422     return Val.getStmt();
423   }
424 };
425   
426 template <> struct simplify_type< ::clang::CFGElement> 
427   : public simplify_type<const ::clang::CFGElement> {};
428   
429 // Traits for: CFGBlock
430
431 template <> struct GraphTraits< ::clang::CFGBlock* > {
432   typedef ::clang::CFGBlock NodeType;
433   typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
434
435   static NodeType* getEntryNode(::clang::CFGBlock* BB)
436   { return BB; }
437
438   static inline ChildIteratorType child_begin(NodeType* N)
439   { return N->succ_begin(); }
440
441   static inline ChildIteratorType child_end(NodeType* N)
442   { return N->succ_end(); }
443 };
444
445 template <> struct GraphTraits< const ::clang::CFGBlock* > {
446   typedef const ::clang::CFGBlock NodeType;
447   typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
448
449   static NodeType* getEntryNode(const clang::CFGBlock* BB)
450   { return BB; }
451
452   static inline ChildIteratorType child_begin(NodeType* N)
453   { return N->succ_begin(); }
454
455   static inline ChildIteratorType child_end(NodeType* N)
456   { return N->succ_end(); }
457 };
458
459 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
460   typedef const ::clang::CFGBlock NodeType;
461   typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
462
463   static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
464   { return G.Graph; }
465
466   static inline ChildIteratorType child_begin(NodeType* N)
467   { return N->pred_begin(); }
468
469   static inline ChildIteratorType child_end(NodeType* N)
470   { return N->pred_end(); }
471 };
472
473 // Traits for: CFG
474
475 template <> struct GraphTraits< ::clang::CFG* >
476     : public GraphTraits< ::clang::CFGBlock* >  {
477
478   typedef ::clang::CFG::iterator nodes_iterator;
479
480   static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
481   static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); }
482   static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); }
483 };
484
485 template <> struct GraphTraits<const ::clang::CFG* >
486     : public GraphTraits<const ::clang::CFGBlock* >  {
487
488   typedef ::clang::CFG::const_iterator nodes_iterator;
489
490   static NodeType *getEntryNode( const ::clang::CFG* F) {
491     return &F->getEntry();
492   }
493   static nodes_iterator nodes_begin( const ::clang::CFG* F) {
494     return F->begin();
495   }
496   static nodes_iterator nodes_end( const ::clang::CFG* F) {
497     return F->end();
498   }
499 };
500
501 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
502   : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
503
504   typedef ::clang::CFG::const_iterator nodes_iterator;
505
506   static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
507   static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();}
508   static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); }
509 };
510 } // end llvm namespace
511 #endif