diff options
Diffstat (limited to 'contrib/llvm/tools/clang/include/clang/Analysis/CFG.h')
-rw-r--r-- | contrib/llvm/tools/clang/include/clang/Analysis/CFG.h | 154 |
1 files changed, 102 insertions, 52 deletions
diff --git a/contrib/llvm/tools/clang/include/clang/Analysis/CFG.h b/contrib/llvm/tools/clang/include/clang/Analysis/CFG.h index ca46459..f191c80 100644 --- a/contrib/llvm/tools/clang/include/clang/Analysis/CFG.h +++ b/contrib/llvm/tools/clang/include/clang/Analysis/CFG.h @@ -21,15 +21,13 @@ #include "llvm/Support/Casting.h" #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/BitVector.h" +#include "clang/AST/Stmt.h" #include "clang/Analysis/Support/BumpVector.h" #include "clang/Basic/SourceLocation.h" #include <cassert> #include <iterator> -namespace llvm { - class raw_ostream; -} - namespace clang { class CXXDestructorDecl; class Decl; @@ -98,7 +96,9 @@ class CFGStmt : public CFGElement { public: CFGStmt(Stmt *S) : CFGElement(Statement, S) {} - Stmt *getStmt() const { return static_cast<Stmt *>(Data1.getPointer()); } + const Stmt *getStmt() const { + return static_cast<const Stmt *>(Data1.getPointer()); + } static bool classof(const CFGElement *E) { return E->getKind() == Statement; @@ -280,7 +280,7 @@ class CFGBlock { void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); } reverse_iterator insert(reverse_iterator I, size_t Cnt, CFGElement E, - BumpVectorContext& C) { + BumpVectorContext &C) { return Impl.insert(I, Cnt, E, C); } @@ -333,10 +333,21 @@ class CFGBlock { AdjacentBlocks Preds; AdjacentBlocks Succs; + /// NoReturn - This bit is set when the basic block contains a function call + /// or implicit destructor that is attributed as 'noreturn'. In that case, + /// control cannot technically ever proceed past this block. All such blocks + /// will have a single immediate successor: the exit block. This allows them + /// to be easily reached from the exit block and using this bit quickly + /// recognized without scanning the contents of the block. + /// + /// Optimization Note: This bit could be profitably folded with Terminator's + /// storage if the memory usage of CFGBlock becomes an issue. + unsigned HasNoReturnElement : 1; + public: explicit CFGBlock(unsigned blockid, BumpVectorContext &C) : Elements(C), Label(NULL), Terminator(NULL), LoopTarget(NULL), - BlockID(blockid), Preds(C, 1), Succs(C, 1) {} + BlockID(blockid), Preds(C, 1), Succs(C, 1), HasNoReturnElement(false) {} ~CFGBlock() {} // Statement iterators @@ -455,42 +466,45 @@ public: // Manipulation of block contents - void setTerminator(Stmt* Statement) { Terminator = Statement; } - void setLabel(Stmt* Statement) { Label = Statement; } + void setTerminator(Stmt *Statement) { Terminator = Statement; } + void setLabel(Stmt *Statement) { Label = Statement; } void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; } + void setHasNoReturnElement() { HasNoReturnElement = true; } CFGTerminator getTerminator() { return Terminator; } const CFGTerminator getTerminator() const { return Terminator; } - Stmt* getTerminatorCondition(); + Stmt *getTerminatorCondition(); - const Stmt* getTerminatorCondition() const { + const Stmt *getTerminatorCondition() const { return const_cast<CFGBlock*>(this)->getTerminatorCondition(); } const Stmt *getLoopTarget() const { return LoopTarget; } - Stmt* getLabel() { return Label; } - const Stmt* getLabel() const { return Label; } + Stmt *getLabel() { return Label; } + const Stmt *getLabel() const { return Label; } + + bool hasNoReturnElement() const { return HasNoReturnElement; } unsigned getBlockID() const { return BlockID; } void dump(const CFG *cfg, const LangOptions &LO) const; - void print(llvm::raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const; - void printTerminator(llvm::raw_ostream &OS, const LangOptions &LO) const; + void print(raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const; + void printTerminator(raw_ostream &OS, const LangOptions &LO) const; - void addSuccessor(CFGBlock* Block, BumpVectorContext &C) { + void addSuccessor(CFGBlock *Block, BumpVectorContext &C) { if (Block) Block->Preds.push_back(this, C); Succs.push_back(Block, C); } - void appendStmt(Stmt* statement, BumpVectorContext &C) { + void appendStmt(Stmt *statement, BumpVectorContext &C) { Elements.push_back(CFGStmt(statement), C); } void appendInitializer(CXXCtorInitializer *initializer, - BumpVectorContext& C) { + BumpVectorContext &C) { Elements.push_back(CFGInitializer(initializer), C); } @@ -506,14 +520,18 @@ public: Elements.push_back(CFGTemporaryDtor(E), C); } + void appendAutomaticObjDtor(VarDecl *VD, Stmt *S, BumpVectorContext &C) { + Elements.push_back(CFGAutomaticObjDtor(VD, S), C); + } + // Destructors must be inserted in reversed order. So insertion is in two // steps. First we prepare space for some number of elements, then we insert // the elements beginning at the last position in prepared space. iterator beginAutomaticObjDtorsInsert(iterator I, size_t Cnt, - BumpVectorContext& C) { + BumpVectorContext &C) { return iterator(Elements.insert(I.base(), Cnt, CFGElement(), C)); } - iterator insertAutomaticObjDtor(iterator I, VarDecl* VD, Stmt* S) { + iterator insertAutomaticObjDtor(iterator I, VarDecl *VD, Stmt *S) { *I = CFGAutomaticObjDtor(VD, S); return ++I; } @@ -533,30 +551,46 @@ public: //===--------------------------------------------------------------------===// class BuildOptions { + llvm::BitVector alwaysAddMask; public: typedef llvm::DenseMap<const Stmt *, const CFGBlock*> ForcedBlkExprs; ForcedBlkExprs **forcedBlkExprs; - bool PruneTriviallyFalseEdges:1; - bool AddEHEdges:1; - bool AddInitializers:1; - bool AddImplicitDtors:1; + bool PruneTriviallyFalseEdges; + bool AddEHEdges; + bool AddInitializers; + bool AddImplicitDtors; + + bool alwaysAdd(const Stmt *stmt) const { + return alwaysAddMask[stmt->getStmtClass()]; + } + + BuildOptions &setAlwaysAdd(Stmt::StmtClass stmtClass, bool val = true) { + alwaysAddMask[stmtClass] = val; + return *this; + } + + BuildOptions &setAllAlwaysAdd() { + alwaysAddMask.set(); + return *this; + } BuildOptions() - : forcedBlkExprs(0), PruneTriviallyFalseEdges(true) - , AddEHEdges(false) - , AddInitializers(false) - , AddImplicitDtors(false) {} + : alwaysAddMask(Stmt::lastStmtConstant, false) + ,forcedBlkExprs(0), PruneTriviallyFalseEdges(true) + ,AddEHEdges(false) + ,AddInitializers(false) + ,AddImplicitDtors(false) {} }; /// buildCFG - Builds a CFG from an AST. The responsibility to free the /// constructed CFG belongs to the caller. - static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C, + static CFG* buildCFG(const Decl *D, Stmt *AST, ASTContext *C, const BuildOptions &BO); /// createBlock - Create a new block in the CFG. The CFG owns the block; /// the caller should not directly free it. - CFGBlock* createBlock(); + CFGBlock *createBlock(); /// setEntry - Set the entry block of the CFG. This is typically used /// only during CFG construction. Most CFG clients expect that the @@ -565,7 +599,7 @@ public: /// setIndirectGotoBlock - Set the block used for indirect goto jumps. /// This is typically used only during CFG construction. - void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; } + void setIndirectGotoBlock(CFGBlock *B) { IndirectGotoBlock = B; } //===--------------------------------------------------------------------===// // Block Iterators @@ -577,8 +611,8 @@ public: typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; - CFGBlock& front() { return *Blocks.front(); } - CFGBlock& back() { return *Blocks.back(); } + CFGBlock & front() { return *Blocks.front(); } + CFGBlock & back() { return *Blocks.back(); } iterator begin() { return Blocks.begin(); } iterator end() { return Blocks.end(); } @@ -590,13 +624,25 @@ public: const_reverse_iterator rbegin() const { return Blocks.rbegin(); } const_reverse_iterator rend() const { return Blocks.rend(); } - CFGBlock& getEntry() { return *Entry; } - const CFGBlock& getEntry() const { return *Entry; } - CFGBlock& getExit() { return *Exit; } - const CFGBlock& getExit() const { return *Exit; } + CFGBlock & getEntry() { return *Entry; } + const CFGBlock & getEntry() const { return *Entry; } + CFGBlock & getExit() { return *Exit; } + const CFGBlock & getExit() const { return *Exit; } - CFGBlock* getIndirectGotoBlock() { return IndirectGotoBlock; } - const CFGBlock* getIndirectGotoBlock() const { return IndirectGotoBlock; } + CFGBlock * getIndirectGotoBlock() { return IndirectGotoBlock; } + const CFGBlock * getIndirectGotoBlock() const { return IndirectGotoBlock; } + + typedef std::vector<const CFGBlock*>::const_iterator try_block_iterator; + try_block_iterator try_blocks_begin() const { + return TryDispatchBlocks.begin(); + } + try_block_iterator try_blocks_end() const { + return TryDispatchBlocks.end(); + } + + void addTryDispatchBlock(const CFGBlock *block) { + TryDispatchBlocks.push_back(block); + } //===--------------------------------------------------------------------===// // Member templates useful for various batch operations over CFGs. @@ -608,7 +654,7 @@ public: for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end(); BI != BE; ++BI) { if (const CFGStmt *stmt = BI->getAs<CFGStmt>()) - O(stmt->getStmt()); + O(const_cast<Stmt*>(stmt->getStmt())); } } @@ -624,11 +670,11 @@ public: operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; } }; - bool isBlkExpr(const Stmt* S) { return getBlkExprNum(S); } + bool isBlkExpr(const Stmt *S) { return getBlkExprNum(S); } bool isBlkExpr(const Stmt *S) const { return const_cast<CFG*>(this)->isBlkExpr(S); } - BlkExprNumTy getBlkExprNum(const Stmt* S); + BlkExprNumTy getBlkExprNum(const Stmt *S); unsigned getNumBlkExprs(); /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which @@ -640,7 +686,7 @@ public: //===--------------------------------------------------------------------===// void viewCFG(const LangOptions &LO) const; - void print(llvm::raw_ostream& OS, const LangOptions &LO) const; + void print(raw_ostream &OS, const LangOptions &LO) const; void dump(const LangOptions &LO) const; //===--------------------------------------------------------------------===// @@ -661,8 +707,8 @@ public: } private: - CFGBlock* Entry; - CFGBlock* Exit; + CFGBlock *Entry; + CFGBlock *Exit; CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch // for indirect gotos unsigned NumBlockIDs; @@ -670,11 +716,15 @@ private: // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h. // It represents a map from Expr* to integers to record the set of // block-level expressions and their "statement number" in the CFG. - void* BlkExprMap; + void * BlkExprMap; BumpVectorContext BlkBVC; CFGBlockListTy Blocks; + + /// C++ 'try' statements are modeled with an indirect dispatch block. + /// This is the collection of such blocks present in the CFG. + std::vector<const CFGBlock *> TryDispatchBlocks; }; } // end namespace clang @@ -703,11 +753,11 @@ template <> struct simplify_type< ::clang::CFGTerminator> { // Traits for: CFGBlock -template <> struct GraphTraits< ::clang::CFGBlock* > { +template <> struct GraphTraits< ::clang::CFGBlock *> { typedef ::clang::CFGBlock NodeType; typedef ::clang::CFGBlock::succ_iterator ChildIteratorType; - static NodeType* getEntryNode(::clang::CFGBlock* BB) + static NodeType* getEntryNode(::clang::CFGBlock *BB) { return BB; } static inline ChildIteratorType child_begin(NodeType* N) @@ -717,11 +767,11 @@ template <> struct GraphTraits< ::clang::CFGBlock* > { { return N->succ_end(); } }; -template <> struct GraphTraits< const ::clang::CFGBlock* > { +template <> struct GraphTraits< const ::clang::CFGBlock *> { typedef const ::clang::CFGBlock NodeType; typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType; - static NodeType* getEntryNode(const clang::CFGBlock* BB) + static NodeType* getEntryNode(const clang::CFGBlock *BB) { return BB; } static inline ChildIteratorType child_begin(NodeType* N) @@ -748,7 +798,7 @@ template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > { // Traits for: CFG template <> struct GraphTraits< ::clang::CFG* > - : public GraphTraits< ::clang::CFGBlock* > { + : public GraphTraits< ::clang::CFGBlock *> { typedef ::clang::CFG::iterator nodes_iterator; @@ -758,7 +808,7 @@ template <> struct GraphTraits< ::clang::CFG* > }; template <> struct GraphTraits<const ::clang::CFG* > - : public GraphTraits<const ::clang::CFGBlock* > { + : public GraphTraits<const ::clang::CFGBlock *> { typedef ::clang::CFG::const_iterator nodes_iterator; |