Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1 | //===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | b1c170e | 2009-07-22 21:45:16 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Support/SaveAndRestore.h" |
Ted Kremenek | 6796fbd | 2009-07-16 18:13:04 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/CFG.h" |
Mike Stump | 6bf1c08 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | 1b8ac85 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 19 | #include "clang/AST/PrettyPrinter.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 20 | #include "llvm/Support/GraphWriter.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Allocator.h" |
| 22 | #include "llvm/Support/Format.h" |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 8aed490 | 2009-10-20 23:46:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | e5ccf9a | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | namespace { |
| 30 | |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 31 | static SourceLocation GetEndLoc(Decl* D) { |
Ted Kremenek | 8889bb3 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 32 | if (VarDecl* VD = dyn_cast<VarDecl>(D)) |
| 33 | if (Expr* Ex = VD->getInit()) |
| 34 | return Ex->getSourceRange().getEnd(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 35 | |
| 36 | return D->getLocation(); |
Ted Kremenek | 8889bb3 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 37 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 39 | class AddStmtChoice { |
| 40 | public: |
Ted Kremenek | 5d2bb1b | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 41 | enum Kind { NotAlwaysAdd = 0, |
| 42 | AlwaysAdd = 1, |
| 43 | AsLValueNotAlwaysAdd = 2, |
| 44 | AlwaysAddAsLValue = 3 }; |
| 45 | |
Benjamin Kramer | a3b1341 | 2010-03-03 16:28:47 +0000 | [diff] [blame] | 46 | AddStmtChoice(Kind kind) : k(kind) {} |
Ted Kremenek | 5d2bb1b | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 47 | |
Benjamin Kramer | a3b1341 | 2010-03-03 16:28:47 +0000 | [diff] [blame] | 48 | bool alwaysAdd() const { return (unsigned)k & 0x1; } |
Ted Kremenek | 66de603 | 2010-04-11 17:02:04 +0000 | [diff] [blame] | 49 | bool asLValue() const { return k >= AsLValueNotAlwaysAdd; } |
Ted Kremenek | 5d2bb1b | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 51 | private: |
Benjamin Kramer | a3b1341 | 2010-03-03 16:28:47 +0000 | [diff] [blame] | 52 | Kind k; |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 53 | }; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 54 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 55 | /// LocalScope - Node in tree of local scopes created for C++ implicit |
| 56 | /// destructor calls generation. It contains list of automatic variables |
| 57 | /// declared in the scope and link to position in previous scope this scope |
| 58 | /// began in. |
| 59 | /// |
| 60 | /// The process of creating local scopes is as follows: |
| 61 | /// - Init CFGBuilder::ScopePos with invalid position (equivalent for null), |
| 62 | /// - Before processing statements in scope (e.g. CompoundStmt) create |
| 63 | /// LocalScope object using CFGBuilder::ScopePos as link to previous scope |
| 64 | /// and set CFGBuilder::ScopePos to the end of new scope, |
| 65 | /// - On every occurance of VarDecl increase CFGBuilder::ScopePos if it points |
| 66 | /// at this VarDecl, |
| 67 | /// - For every normal (without jump) end of scope add to CFGBlock destructors |
| 68 | /// for objects in the current scope, |
| 69 | /// - For every jump add to CFGBlock destructors for objects |
| 70 | /// between CFGBuilder::ScopePos and local scope position saved for jump |
| 71 | /// target. Thanks to C++ restrictions on goto jumps we can be sure that |
| 72 | /// jump target position will be on the path to root from CFGBuilder::ScopePos |
| 73 | /// (adding any variable that doesn't need constructor to be called to |
| 74 | /// LocalScope can break this assumption), |
| 75 | /// |
| 76 | class LocalScope { |
| 77 | public: |
| 78 | typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy; |
| 79 | |
| 80 | /// const_iterator - Iterates local scope backwards and jumps to previous |
| 81 | /// scope on reaching the begining of currently iterated scope. |
| 82 | class const_iterator { |
| 83 | const LocalScope* Scope; |
| 84 | |
| 85 | /// VarIter is guaranteed to be greater then 0 for every valid iterator. |
| 86 | /// Invalid iterator (with null Scope) has VarIter equal to 0. |
| 87 | unsigned VarIter; |
| 88 | |
| 89 | public: |
| 90 | /// Create invalid iterator. Dereferencing invalid iterator is not allowed. |
| 91 | /// Incrementing invalid iterator is allowed and will result in invalid |
| 92 | /// iterator. |
| 93 | const_iterator() |
| 94 | : Scope(NULL), VarIter(0) {} |
| 95 | |
| 96 | /// Create valid iterator. In case when S.Prev is an invalid iterator and |
| 97 | /// I is equal to 0, this will create invalid iterator. |
| 98 | const_iterator(const LocalScope& S, unsigned I) |
| 99 | : Scope(&S), VarIter(I) { |
| 100 | // Iterator to "end" of scope is not allowed. Handle it by going up |
| 101 | // in scopes tree possibly up to invalid iterator in the root. |
| 102 | if (VarIter == 0 && Scope) |
| 103 | *this = Scope->Prev; |
| 104 | } |
| 105 | |
| 106 | VarDecl* const* operator->() const { |
| 107 | assert (Scope && "Dereferencing invalid iterator is not allowed"); |
| 108 | assert (VarIter != 0 && "Iterator has invalid value of VarIter member"); |
| 109 | return &Scope->Vars[VarIter - 1]; |
| 110 | } |
| 111 | VarDecl* operator*() const { |
| 112 | return *this->operator->(); |
| 113 | } |
| 114 | |
| 115 | const_iterator& operator++() { |
| 116 | if (!Scope) |
| 117 | return *this; |
| 118 | |
| 119 | assert (VarIter != 0 && "Iterator has invalid value of VarIter member"); |
| 120 | --VarIter; |
| 121 | if (VarIter == 0) |
| 122 | *this = Scope->Prev; |
| 123 | return *this; |
| 124 | } |
| 125 | |
| 126 | bool operator==(const const_iterator& rhs) const { |
| 127 | return Scope == rhs.Scope && VarIter == rhs.VarIter; |
| 128 | } |
| 129 | bool operator!=(const const_iterator& rhs) const { |
| 130 | return !(*this == rhs); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | friend class const_iterator; |
| 135 | |
| 136 | private: |
| 137 | /// Automatic variables in order of declaration. |
| 138 | AutomaticVarsTy Vars; |
| 139 | /// Iterator to variable in previous scope that was declared just before |
| 140 | /// begin of this scope. |
| 141 | const_iterator Prev; |
| 142 | |
| 143 | public: |
| 144 | /// Constructs empty scope linked to previous scope in specified place. |
| 145 | LocalScope(const_iterator P) |
| 146 | : Vars() |
| 147 | , Prev(P) {} |
| 148 | |
| 149 | /// Begin of scope in direction of CFG building (backwards). |
| 150 | const_iterator begin() const { return const_iterator(*this, Vars.size()); } |
| 151 | }; |
| 152 | |
| 153 | /// BlockScopePosPair - Structure for specifing position in CFG during its build |
| 154 | /// proces. It consists of CFGBlock that specifies position in CFG graph and |
| 155 | /// LocalScope::const_iterator that specifies position in LocalScope graph. |
| 156 | struct BlockScopePosPair { |
| 157 | BlockScopePosPair() {} |
| 158 | BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S) |
| 159 | : Block(B), ScopePos(S) {} |
| 160 | |
| 161 | CFGBlock* Block; |
| 162 | LocalScope::const_iterator ScopePos; |
| 163 | }; |
| 164 | |
Ted Kremenek | be9b33b | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 165 | /// CFGBuilder - This class implements CFG construction from an AST. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 166 | /// The builder is stateful: an instance of the builder should be used to only |
| 167 | /// construct a single CFG. |
| 168 | /// |
| 169 | /// Example usage: |
| 170 | /// |
| 171 | /// CFGBuilder builder; |
| 172 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 173 | /// |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 174 | /// CFG construction is done via a recursive walk of an AST. We actually parse |
| 175 | /// the AST in reverse order so that the successor of a basic block is |
| 176 | /// constructed prior to its predecessor. This allows us to nicely capture |
| 177 | /// implicit fall-throughs without extra basic blocks. |
Ted Kremenek | 1b8ac85 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 178 | /// |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 179 | class CFGBuilder { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 180 | typedef BlockScopePosPair JumpTarget; |
| 181 | typedef BlockScopePosPair JumpSource; |
| 182 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 183 | ASTContext *Context; |
Ted Kremenek | 8aed490 | 2009-10-20 23:46:25 +0000 | [diff] [blame] | 184 | llvm::OwningPtr<CFG> cfg; |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 186 | CFGBlock* Block; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 187 | CFGBlock* Succ; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 188 | JumpTarget ContinueJumpTarget; |
| 189 | JumpTarget BreakJumpTarget; |
Ted Kremenek | 879d8e1 | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 190 | CFGBlock* SwitchTerminatedBlock; |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 191 | CFGBlock* DefaultCaseBlock; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 192 | CFGBlock* TryTerminatedBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 193 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 194 | // Current position in local scope. |
| 195 | LocalScope::const_iterator ScopePos; |
| 196 | |
| 197 | // LabelMap records the mapping from Label expressions to their jump targets. |
| 198 | typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy; |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 199 | LabelMapTy LabelMap; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 200 | |
| 201 | // A list of blocks that end with a "goto" that must be backpatched to their |
| 202 | // resolved targets upon completion of CFG construction. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 203 | typedef std::vector<JumpSource> BackpatchBlocksTy; |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 204 | BackpatchBlocksTy BackpatchBlocks; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 206 | // A list of labels whose address has been taken (for indirect gotos). |
| 207 | typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy; |
| 208 | LabelSetTy AddressTakenLabels; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 209 | |
Zhongxing Xu | d38fb84 | 2010-09-16 03:28:18 +0000 | [diff] [blame] | 210 | bool badCFG; |
| 211 | CFG::BuildOptions BuildOpts; |
| 212 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 213 | public: |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 214 | explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG |
| 215 | Block(NULL), Succ(NULL), |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 216 | SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL), |
Zhongxing Xu | d38fb84 | 2010-09-16 03:28:18 +0000 | [diff] [blame] | 217 | TryTerminatedBlock(NULL), badCFG(false) {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 219 | // buildCFG - Used by external clients to construct the CFG. |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 220 | CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 221 | CFG::BuildOptions BO); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 223 | private: |
| 224 | // Visitors to walk an AST and construct the CFG. |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 225 | CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc); |
| 226 | CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc); |
| 227 | CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 228 | CFGBlock *VisitBreakStmt(BreakStmt *B); |
Ted Kremenek | d2ba1f9 | 2010-04-11 17:01:59 +0000 | [diff] [blame] | 229 | CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S); |
| 230 | CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T); |
| 231 | CFGBlock *VisitCXXTryStmt(CXXTryStmt *S); |
Zhongxing Xu | 7e61217 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 232 | CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 233 | CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 234 | CFGBlock *VisitCaseStmt(CaseStmt *C); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 235 | CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 236 | CFGBlock *VisitCompoundStmt(CompoundStmt *C); |
Ted Kremenek | d2ba1f9 | 2010-04-11 17:01:59 +0000 | [diff] [blame] | 237 | CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 238 | CFGBlock *VisitContinueStmt(ContinueStmt *C); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 239 | CFGBlock *VisitDeclStmt(DeclStmt *DS); |
| 240 | CFGBlock *VisitDeclSubExpr(Decl* D); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 241 | CFGBlock *VisitDefaultStmt(DefaultStmt *D); |
| 242 | CFGBlock *VisitDoStmt(DoStmt *D); |
| 243 | CFGBlock *VisitForStmt(ForStmt *F); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 244 | CFGBlock *VisitGotoStmt(GotoStmt* G); |
| 245 | CFGBlock *VisitIfStmt(IfStmt *I); |
| 246 | CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I); |
| 247 | CFGBlock *VisitLabelStmt(LabelStmt *L); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 248 | CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 249 | CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 250 | CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 251 | CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 252 | CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| 253 | CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| 254 | CFGBlock *VisitReturnStmt(ReturnStmt* R); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 255 | CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc); |
| 256 | CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 257 | CFGBlock *VisitSwitchStmt(SwitchStmt *S); |
| 258 | CFGBlock *VisitWhileStmt(WhileStmt *W); |
Mike Stump | 48871a2 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 260 | CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd); |
| 261 | CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 262 | CFGBlock *VisitChildren(Stmt* S); |
Mike Stump | 48871a2 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 264 | // NYS == Not Yet Supported |
| 265 | CFGBlock* NYS() { |
Ted Kremenek | b64d183 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 266 | badCFG = true; |
| 267 | return Block; |
| 268 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 270 | void autoCreateBlock() { if (!Block) Block = createBlock(); } |
| 271 | CFGBlock *createBlock(bool add_successor = true); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 272 | |
Zhongxing Xu | ea9fcff | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 273 | CFGBlock *addStmt(Stmt *S) { |
| 274 | return Visit(S, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 275 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 276 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 277 | void AppendStmt(CFGBlock *B, Stmt *S, |
| 278 | AddStmtChoice asc = AddStmtChoice::AlwaysAdd) { |
| 279 | B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue()); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 280 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 281 | |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 282 | void AddSuccessor(CFGBlock *B, CFGBlock *S) { |
| 283 | B->addSuccessor(S, cfg->getBumpVectorContext()); |
| 284 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | 963cc31 | 2009-07-24 06:55:42 +0000 | [diff] [blame] | 286 | /// TryResult - a class representing a variant over the values |
| 287 | /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool, |
| 288 | /// and is used by the CFGBuilder to decide if a branch condition |
| 289 | /// can be decided up front during CFG construction. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 290 | class TryResult { |
| 291 | int X; |
| 292 | public: |
| 293 | TryResult(bool b) : X(b ? 1 : 0) {} |
| 294 | TryResult() : X(-1) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 296 | bool isTrue() const { return X == 1; } |
| 297 | bool isFalse() const { return X == 0; } |
| 298 | bool isKnown() const { return X >= 0; } |
| 299 | void negate() { |
| 300 | assert(isKnown()); |
| 301 | X ^= 0x1; |
| 302 | } |
| 303 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 305 | /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1 |
| 306 | /// if we can evaluate to a known value, otherwise return -1. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 307 | TryResult TryEvaluateBool(Expr *S) { |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 308 | if (!BuildOpts.PruneTriviallyFalseEdges) |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 309 | return TryResult(); |
| 310 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 311 | Expr::EvalResult Result; |
Douglas Gregor | 4c95288 | 2009-08-24 21:39:56 +0000 | [diff] [blame] | 312 | if (!S->isTypeDependent() && !S->isValueDependent() && |
| 313 | S->Evaluate(Result, *Context) && Result.Val.isInt()) |
Ted Kremenek | 963cc31 | 2009-07-24 06:55:42 +0000 | [diff] [blame] | 314 | return Result.Val.getInt().getBoolValue(); |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 315 | |
| 316 | return TryResult(); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 317 | } |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 318 | }; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 319 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 320 | // FIXME: Add support for dependent-sized array types in C++? |
| 321 | // Does it even make sense to build a CFG for an uninstantiated template? |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 322 | static VariableArrayType* FindVA(Type* t) { |
| 323 | while (ArrayType* vt = dyn_cast<ArrayType>(t)) { |
| 324 | if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt)) |
| 325 | if (vat->getSizeExpr()) |
| 326 | return vat; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 327 | |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 328 | t = vt->getElementType().getTypePtr(); |
| 329 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 331 | return 0; |
| 332 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 333 | |
| 334 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an |
| 335 | /// arbitrary statement. Examples include a single expression or a function |
| 336 | /// body (compound statement). The ownership of the returned CFG is |
| 337 | /// transferred to the caller. If CFG construction fails, this method returns |
| 338 | /// NULL. |
Mike Stump | 6bf1c08 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 339 | CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C, |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 340 | CFG::BuildOptions BO) { |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 341 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 342 | Context = C; |
Ted Kremenek | 8aed490 | 2009-10-20 23:46:25 +0000 | [diff] [blame] | 343 | assert(cfg.get()); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 344 | if (!Statement) |
| 345 | return NULL; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 347 | BuildOpts = BO; |
| 348 | if (!C->getLangOptions().CPlusPlus) |
| 349 | BuildOpts.AddImplicitDtors = false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 350 | |
| 351 | // Create an empty block that will serve as the exit block for the CFG. Since |
| 352 | // this is the first block added to the CFG, it will be implicitly registered |
| 353 | // as the exit block. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 354 | Succ = createBlock(); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 355 | assert(Succ == &cfg->getExit()); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 356 | Block = NULL; // the EXIT block is empty. Create all other blocks lazily. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 358 | // Visit the statements and create the CFG. |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 359 | CFGBlock *B = addStmt(Statement); |
| 360 | |
| 361 | if (badCFG) |
| 362 | return NULL; |
| 363 | |
| 364 | if (B) |
| 365 | Succ = B; |
Mike Stump | 6bf1c08 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 366 | |
| 367 | if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) { |
| 368 | // FIXME: Add code for base initializers and member initializers. |
| 369 | (void)CD; |
| 370 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 371 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 372 | // Backpatch the gotos whose label -> block mappings we didn't know when we |
| 373 | // encountered them. |
| 374 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 375 | E = BackpatchBlocks.end(); I != E; ++I ) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 376 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 377 | CFGBlock* B = I->Block; |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 378 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 379 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 380 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 381 | // If there is no target for the goto, then we are looking at an |
| 382 | // incomplete AST. Handle this by not registering a successor. |
| 383 | if (LI == LabelMap.end()) continue; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 384 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 385 | JumpTarget JT = LI->second; |
| 386 | AddSuccessor(B, JT.Block); |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // Add successors to the Indirect Goto Dispatch block (if we have one). |
| 390 | if (CFGBlock* B = cfg->getIndirectGotoBlock()) |
| 391 | for (LabelSetTy::iterator I = AddressTakenLabels.begin(), |
| 392 | E = AddressTakenLabels.end(); I != E; ++I ) { |
| 393 | |
| 394 | // Lookup the target block. |
| 395 | LabelMapTy::iterator LI = LabelMap.find(*I); |
| 396 | |
| 397 | // If there is no target block that contains label, then we are looking |
| 398 | // at an incomplete AST. Handle this by not registering a successor. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 399 | if (LI == LabelMap.end()) continue; |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 400 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 401 | AddSuccessor(B, LI->second.Block); |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 402 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 403 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 404 | // Create an empty entry block that has no predecessors. |
Ted Kremenek | 5c50fd1 | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 405 | cfg->setEntry(createBlock()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 406 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 407 | return cfg.take(); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 408 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 410 | /// createBlock - Used to lazily create blocks that are connected |
| 411 | /// to the current (global) succcessor. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 412 | CFGBlock* CFGBuilder::createBlock(bool add_successor) { |
Ted Kremenek | 813dd67 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 413 | CFGBlock* B = cfg->createBlock(); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 414 | if (add_successor && Succ) |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 415 | AddSuccessor(B, Succ); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 416 | return B; |
| 417 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 419 | /// Visit - Walk the subtree of a statement and add extra |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 420 | /// blocks for ternary operators, &&, and ||. We also process "," and |
| 421 | /// DeclStmts (which may contain nested control-flow). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 422 | CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 423 | tryAgain: |
Ted Kremenek | bc1416d | 2010-04-30 22:25:53 +0000 | [diff] [blame] | 424 | if (!S) { |
| 425 | badCFG = true; |
| 426 | return 0; |
| 427 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 428 | switch (S->getStmtClass()) { |
| 429 | default: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 430 | return VisitStmt(S, asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 431 | |
| 432 | case Stmt::AddrLabelExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 433 | return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 435 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 436 | return VisitBinaryOperator(cast<BinaryOperator>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 438 | case Stmt::BlockExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 439 | return VisitBlockExpr(cast<BlockExpr>(S), asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 441 | case Stmt::BreakStmtClass: |
| 442 | return VisitBreakStmt(cast<BreakStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 444 | case Stmt::CallExprClass: |
Ted Kremenek | 128d04d | 2010-08-31 18:47:34 +0000 | [diff] [blame] | 445 | case Stmt::CXXOperatorCallExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 446 | return VisitCallExpr(cast<CallExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 448 | case Stmt::CaseStmtClass: |
| 449 | return VisitCaseStmt(cast<CaseStmt>(S)); |
| 450 | |
| 451 | case Stmt::ChooseExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 452 | return VisitChooseExpr(cast<ChooseExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 454 | case Stmt::CompoundStmtClass: |
| 455 | return VisitCompoundStmt(cast<CompoundStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 457 | case Stmt::ConditionalOperatorClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 458 | return VisitConditionalOperator(cast<ConditionalOperator>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 460 | case Stmt::ContinueStmtClass: |
| 461 | return VisitContinueStmt(cast<ContinueStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 463 | case Stmt::CXXCatchStmtClass: |
| 464 | return VisitCXXCatchStmt(cast<CXXCatchStmt>(S)); |
| 465 | |
Ted Kremenek | 82bfc86 | 2010-08-28 00:19:02 +0000 | [diff] [blame] | 466 | case Stmt::CXXExprWithTemporariesClass: { |
| 467 | // FIXME: Handle temporaries. For now, just visit the subexpression |
| 468 | // so we don't artificially create extra blocks. |
Ted Kremenek | 128d04d | 2010-08-31 18:47:34 +0000 | [diff] [blame] | 469 | return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc); |
Ted Kremenek | 82bfc86 | 2010-08-28 00:19:02 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Zhongxing Xu | 7e61217 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 472 | case Stmt::CXXMemberCallExprClass: |
| 473 | return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc); |
| 474 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 475 | case Stmt::CXXThrowExprClass: |
| 476 | return VisitCXXThrowExpr(cast<CXXThrowExpr>(S)); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 477 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 478 | case Stmt::CXXTryStmtClass: |
| 479 | return VisitCXXTryStmt(cast<CXXTryStmt>(S)); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 481 | case Stmt::DeclStmtClass: |
| 482 | return VisitDeclStmt(cast<DeclStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 484 | case Stmt::DefaultStmtClass: |
| 485 | return VisitDefaultStmt(cast<DefaultStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 487 | case Stmt::DoStmtClass: |
| 488 | return VisitDoStmt(cast<DoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 490 | case Stmt::ForStmtClass: |
| 491 | return VisitForStmt(cast<ForStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 493 | case Stmt::GotoStmtClass: |
| 494 | return VisitGotoStmt(cast<GotoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 496 | case Stmt::IfStmtClass: |
| 497 | return VisitIfStmt(cast<IfStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 499 | case Stmt::IndirectGotoStmtClass: |
| 500 | return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 502 | case Stmt::LabelStmtClass: |
| 503 | return VisitLabelStmt(cast<LabelStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 505 | case Stmt::MemberExprClass: |
| 506 | return VisitMemberExpr(cast<MemberExpr>(S), asc); |
| 507 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 508 | case Stmt::ObjCAtCatchStmtClass: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S)); |
| 510 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 511 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 512 | return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 514 | case Stmt::ObjCAtThrowStmtClass: |
| 515 | return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 517 | case Stmt::ObjCAtTryStmtClass: |
| 518 | return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 520 | case Stmt::ObjCForCollectionStmtClass: |
| 521 | return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 523 | case Stmt::ParenExprClass: |
| 524 | S = cast<ParenExpr>(S)->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | goto tryAgain; |
| 526 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 527 | case Stmt::NullStmtClass: |
| 528 | return Block; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 530 | case Stmt::ReturnStmtClass: |
| 531 | return VisitReturnStmt(cast<ReturnStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 533 | case Stmt::SizeOfAlignOfExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 534 | return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 536 | case Stmt::StmtExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 537 | return VisitStmtExpr(cast<StmtExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 539 | case Stmt::SwitchStmtClass: |
| 540 | return VisitSwitchStmt(cast<SwitchStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 542 | case Stmt::WhileStmtClass: |
| 543 | return VisitWhileStmt(cast<WhileStmt>(S)); |
| 544 | } |
| 545 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 547 | CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) { |
| 548 | if (asc.alwaysAdd()) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 549 | autoCreateBlock(); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 550 | AppendStmt(Block, S, asc); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 551 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 553 | return VisitChildren(S); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 554 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 555 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 556 | /// VisitChildren - Visit the children of a Stmt. |
| 557 | CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) { |
| 558 | CFGBlock *B = Block; |
Mike Stump | d8ba7a2 | 2009-02-26 08:00:25 +0000 | [diff] [blame] | 559 | for (Stmt::child_iterator I = Terminator->child_begin(), |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 560 | E = Terminator->child_end(); I != E; ++I) { |
| 561 | if (*I) B = Visit(*I); |
| 562 | } |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 563 | return B; |
| 564 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 566 | CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, |
| 567 | AddStmtChoice asc) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 568 | AddressTakenLabels.insert(A->getLabel()); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 569 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 570 | if (asc.alwaysAdd()) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 571 | autoCreateBlock(); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 572 | AppendStmt(Block, A, asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 573 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 574 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 575 | return Block; |
| 576 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 578 | CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, |
| 579 | AddStmtChoice asc) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 580 | if (B->isLogicalOp()) { // && or || |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 581 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 582 | AppendStmt(ConfluenceBlock, B, asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 584 | if (badCFG) |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 585 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 587 | // create the block evaluating the LHS |
| 588 | CFGBlock* LHSBlock = createBlock(false); |
| 589 | LHSBlock->setTerminator(B); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 591 | // create the block evaluating the RHS |
| 592 | Succ = ConfluenceBlock; |
| 593 | Block = NULL; |
| 594 | CFGBlock* RHSBlock = addStmt(B->getRHS()); |
Ted Kremenek | 989da5e | 2010-04-29 01:10:26 +0000 | [diff] [blame] | 595 | |
| 596 | if (RHSBlock) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 597 | if (badCFG) |
Ted Kremenek | 989da5e | 2010-04-29 01:10:26 +0000 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | else { |
| 601 | // Create an empty block for cases where the RHS doesn't require |
| 602 | // any explicit statements in the CFG. |
| 603 | RHSBlock = createBlock(); |
| 604 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 606 | // See if this is a known constant. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 607 | TryResult KnownVal = TryEvaluateBool(B->getLHS()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 608 | if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr)) |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 609 | KnownVal.negate(); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 610 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 611 | // Now link the LHSBlock with RHSBlock. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 612 | if (B->getOpcode() == BO_LOr) { |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 613 | AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock); |
| 614 | AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | } else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 616 | assert(B->getOpcode() == BO_LAnd); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 617 | AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock); |
| 618 | AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 619 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 621 | // Generate the blocks for evaluating the LHS. |
| 622 | Block = LHSBlock; |
| 623 | return addStmt(B->getLHS()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 625 | else if (B->getOpcode() == BO_Comma) { // , |
Ted Kremenek | fe9b768 | 2009-07-17 22:57:50 +0000 | [diff] [blame] | 626 | autoCreateBlock(); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 627 | AppendStmt(Block, B, asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 628 | addStmt(B->getRHS()); |
| 629 | return addStmt(B->getLHS()); |
| 630 | } |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 631 | else if (B->isAssignmentOp()) { |
| 632 | if (asc.alwaysAdd()) { |
| 633 | autoCreateBlock(); |
| 634 | AppendStmt(Block, B, asc); |
| 635 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 636 | |
Ted Kremenek | 8abff77 | 2010-09-14 01:13:32 +0000 | [diff] [blame] | 637 | // If visiting RHS causes us to finish 'Block' and the LHS doesn't |
| 638 | // create a new block, then we should return RBlock. Otherwise |
| 639 | // we'll incorrectly return NULL. |
| 640 | CFGBlock *RBlock = Visit(B->getRHS()); |
| 641 | CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd); |
| 642 | return LBlock ? LBlock : RBlock; |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 643 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 645 | return VisitStmt(B, asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 648 | CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) { |
| 649 | if (asc.alwaysAdd()) { |
Ted Kremenek | 470bfa4 | 2009-11-25 01:34:30 +0000 | [diff] [blame] | 650 | autoCreateBlock(); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 651 | AppendStmt(Block, E, asc); |
Ted Kremenek | 470bfa4 | 2009-11-25 01:34:30 +0000 | [diff] [blame] | 652 | } |
| 653 | return Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 656 | CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) { |
| 657 | // "break" is a control-flow statement. Thus we stop processing the current |
| 658 | // block. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 659 | if (badCFG) |
| 660 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 661 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 662 | // Now create a new block that ends with the break statement. |
| 663 | Block = createBlock(false); |
| 664 | Block->setTerminator(B); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 666 | // If there is no target for the break, then we are looking at an incomplete |
| 667 | // AST. This means that the CFG cannot be constructed. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 668 | if (BreakJumpTarget.Block) { |
| 669 | AddSuccessor(Block, BreakJumpTarget.Block); |
| 670 | } else |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 671 | badCFG = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | |
| 673 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 674 | return Block; |
| 675 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 677 | static bool CanThrow(Expr *E) { |
| 678 | QualType Ty = E->getType(); |
| 679 | if (Ty->isFunctionPointerType()) |
| 680 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
| 681 | else if (Ty->isBlockPointerType()) |
| 682 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 683 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 684 | const FunctionType *FT = Ty->getAs<FunctionType>(); |
| 685 | if (FT) { |
| 686 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) |
| 687 | if (Proto->hasEmptyExceptionSpec()) |
| 688 | return false; |
| 689 | } |
| 690 | return true; |
| 691 | } |
| 692 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 693 | CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 694 | // If this is a call to a no-return function, this stops the block here. |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 695 | bool NoReturn = false; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 696 | if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) { |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 697 | NoReturn = true; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 698 | } |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 699 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 700 | bool AddEHEdge = false; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 701 | |
| 702 | // Languages without exceptions are assumed to not throw. |
| 703 | if (Context->getLangOptions().Exceptions) { |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 704 | if (BuildOpts.AddEHEdges) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 705 | AddEHEdge = true; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | if (FunctionDecl *FD = C->getDirectCallee()) { |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 709 | if (FD->hasAttr<NoReturnAttr>()) |
| 710 | NoReturn = true; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 711 | if (FD->hasAttr<NoThrowAttr>()) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 712 | AddEHEdge = false; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 713 | } |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 714 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 715 | if (!CanThrow(C->getCallee())) |
| 716 | AddEHEdge = false; |
| 717 | |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 718 | if (!NoReturn && !AddEHEdge) { |
| 719 | if (asc.asLValue()) |
| 720 | return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue); |
| 721 | else |
| 722 | return VisitStmt(C, AddStmtChoice::AlwaysAdd); |
| 723 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 725 | if (Block) { |
| 726 | Succ = Block; |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 727 | if (badCFG) |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 728 | return 0; |
| 729 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 731 | Block = createBlock(!NoReturn); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 732 | AppendStmt(Block, C, asc); |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 733 | |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 734 | if (NoReturn) { |
| 735 | // Wire this to the exit block directly. |
| 736 | AddSuccessor(Block, &cfg->getExit()); |
| 737 | } |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 738 | if (AddEHEdge) { |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 739 | // Add exceptional edges. |
| 740 | if (TryTerminatedBlock) |
| 741 | AddSuccessor(Block, TryTerminatedBlock); |
| 742 | else |
| 743 | AddSuccessor(Block, &cfg->getExit()); |
| 744 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 746 | return VisitChildren(C); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 747 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 748 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 749 | CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C, |
| 750 | AddStmtChoice asc) { |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 751 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 752 | AppendStmt(ConfluenceBlock, C, asc); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 753 | if (badCFG) |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 754 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 756 | asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue |
| 757 | : AddStmtChoice::AlwaysAdd; |
| 758 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 759 | Succ = ConfluenceBlock; |
| 760 | Block = NULL; |
Zhongxing Xu | ea9fcff | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 761 | CFGBlock* LHSBlock = Visit(C->getLHS(), asc); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 762 | if (badCFG) |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 763 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 765 | Succ = ConfluenceBlock; |
| 766 | Block = NULL; |
Zhongxing Xu | ea9fcff | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 767 | CFGBlock* RHSBlock = Visit(C->getRHS(), asc); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 768 | if (badCFG) |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 769 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 771 | Block = createBlock(false); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 772 | // See if this is a known constant. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 773 | const TryResult& KnownVal = TryEvaluateBool(C->getCond()); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 774 | AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock); |
| 775 | AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 776 | Block->setTerminator(C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | return addStmt(C->getCond()); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 778 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | |
| 780 | |
| 781 | CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { |
| 782 | CFGBlock* LastBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 783 | |
| 784 | for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); |
| 785 | I != E; ++I ) { |
Ted Kremenek | 4f2ab5a | 2010-08-17 21:00:06 +0000 | [diff] [blame] | 786 | // If we hit a segment of code just containing ';' (NullStmts), we can |
| 787 | // get a null block back. In such cases, just use the LastBlock |
| 788 | if (CFGBlock *newBlock = addStmt(*I)) |
| 789 | LastBlock = newBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
Ted Kremenek | ce499c2 | 2009-08-27 23:16:26 +0000 | [diff] [blame] | 791 | if (badCFG) |
| 792 | return NULL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | } |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 794 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 795 | return LastBlock; |
| 796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 798 | CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C, |
| 799 | AddStmtChoice asc) { |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 800 | // Create the confluence block that will "merge" the results of the ternary |
| 801 | // expression. |
| 802 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 803 | AppendStmt(ConfluenceBlock, C, asc); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 804 | if (badCFG) |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 805 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 807 | asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue |
| 808 | : AddStmtChoice::AlwaysAdd; |
| 809 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 810 | // Create a block for the LHS expression if there is an LHS expression. A |
| 811 | // GCC extension allows LHS to be NULL, causing the condition to be the |
| 812 | // value that is returned instead. |
| 813 | // e.g: x ?: y is shorthand for: x ? x : y; |
| 814 | Succ = ConfluenceBlock; |
| 815 | Block = NULL; |
| 816 | CFGBlock* LHSBlock = NULL; |
| 817 | if (C->getLHS()) { |
Zhongxing Xu | ea9fcff | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 818 | LHSBlock = Visit(C->getLHS(), asc); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 819 | if (badCFG) |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 820 | return 0; |
| 821 | Block = NULL; |
| 822 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 824 | // Create the block for the RHS expression. |
| 825 | Succ = ConfluenceBlock; |
Zhongxing Xu | ea9fcff | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 826 | CFGBlock* RHSBlock = Visit(C->getRHS(), asc); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 827 | if (badCFG) |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 828 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 830 | // Create the block that will contain the condition. |
| 831 | Block = createBlock(false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 833 | // See if this is a known constant. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 834 | const TryResult& KnownVal = TryEvaluateBool(C->getCond()); |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 835 | if (LHSBlock) { |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 836 | AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock); |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 837 | } else { |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 838 | if (KnownVal.isFalse()) { |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 839 | // If we know the condition is false, add NULL as the successor for |
| 840 | // the block containing the condition. In this case, the confluence |
| 841 | // block will have just one predecessor. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 842 | AddSuccessor(Block, 0); |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 843 | assert(ConfluenceBlock->pred_size() == 1); |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 844 | } else { |
| 845 | // If we have no LHS expression, add the ConfluenceBlock as a direct |
| 846 | // successor for the block containing the condition. Moreover, we need to |
| 847 | // reverse the order of the predecessors in the ConfluenceBlock because |
| 848 | // the RHSBlock will have been added to the succcessors already, and we |
| 849 | // want the first predecessor to the the block containing the expression |
| 850 | // for the case when the ternary expression evaluates to true. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 851 | AddSuccessor(Block, ConfluenceBlock); |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 852 | assert(ConfluenceBlock->pred_size() == 2); |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 853 | std::reverse(ConfluenceBlock->pred_begin(), |
| 854 | ConfluenceBlock->pred_end()); |
| 855 | } |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 856 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 858 | AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 859 | Block->setTerminator(C); |
| 860 | return addStmt(C->getCond()); |
| 861 | } |
| 862 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 863 | CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) { |
| 864 | autoCreateBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 865 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 866 | if (DS->isSingleDecl()) { |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 867 | AppendStmt(Block, DS); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 868 | return VisitDeclSubExpr(DS->getSingleDecl()); |
Ted Kremenek | f699882 | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 869 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 871 | CFGBlock *B = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 873 | // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy. |
| 874 | typedef llvm::SmallVector<Decl*,10> BufTy; |
| 875 | BufTy Buf(DS->decl_begin(), DS->decl_end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 877 | for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) { |
| 878 | // Get the alignment of the new DeclStmt, padding out to >=8 bytes. |
| 879 | unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8 |
| 880 | ? 8 : llvm::AlignOf<DeclStmt>::Alignment; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 882 | // Allocate the DeclStmt using the BumpPtrAllocator. It will get |
| 883 | // automatically freed with the CFG. |
| 884 | DeclGroupRef DG(*I); |
| 885 | Decl *D = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 887 | DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 889 | // Append the fake DeclStmt to block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 890 | AppendStmt(Block, DSNew); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 891 | B = VisitDeclSubExpr(D); |
| 892 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
| 894 | return B; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 895 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 897 | /// VisitDeclSubExpr - Utility method to add block-level expressions for |
| 898 | /// initializers in Decls. |
| 899 | CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) { |
| 900 | assert(Block); |
Ted Kremenek | f699882 | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 901 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 902 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 904 | if (!VD) |
| 905 | return Block; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 907 | Expr *Init = VD->getInit(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 909 | if (Init) { |
Ted Kremenek | 5d2bb1b | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 910 | AddStmtChoice::Kind k = |
| 911 | VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd |
| 912 | : AddStmtChoice::NotAlwaysAdd; |
| 913 | Visit(Init, AddStmtChoice(k)); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 914 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 916 | // If the type of VD is a VLA, then we must process its size expressions. |
| 917 | for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0; |
| 918 | VA = FindVA(VA->getElementType().getTypePtr())) |
| 919 | Block = addStmt(VA->getSizeExpr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 921 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 925 | // We may see an if statement in the middle of a basic block, or it may be the |
| 926 | // first statement we are processing. In either case, we create a new basic |
| 927 | // block. First, we create the blocks for the then...else statements, and |
| 928 | // then we create the block containing the if statement. If we were in the |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 929 | // middle of a block, we stop processing that block. That block is then the |
| 930 | // implicit successor for the "then" and "else" clauses. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 931 | |
| 932 | // The block we were proccessing is now finished. Make it the successor |
| 933 | // block. |
| 934 | if (Block) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 935 | Succ = Block; |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 936 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 937 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 938 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 939 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 940 | // Process the false branch. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 941 | CFGBlock* ElseBlock = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 942 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 943 | if (Stmt* Else = I->getElse()) { |
| 944 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 945 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 946 | // NULL out Block so that the recursive call to Visit will |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 947 | // create a new basic block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 948 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 949 | ElseBlock = addStmt(Else); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 950 | |
Ted Kremenek | bbad8ce | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 951 | if (!ElseBlock) // Can occur when the Else body has all NullStmts. |
| 952 | ElseBlock = sv.get(); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 953 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 954 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 955 | return 0; |
| 956 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 957 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 958 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 959 | // Process the true branch. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 960 | CFGBlock* ThenBlock; |
| 961 | { |
| 962 | Stmt* Then = I->getThen(); |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 963 | assert(Then); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 964 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 965 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 966 | ThenBlock = addStmt(Then); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 967 | |
Ted Kremenek | 1b37951 | 2009-04-01 03:52:47 +0000 | [diff] [blame] | 968 | if (!ThenBlock) { |
| 969 | // We can reach here if the "then" body has all NullStmts. |
| 970 | // Create an empty block so we can distinguish between true and false |
| 971 | // branches in path-sensitive analyses. |
| 972 | ThenBlock = createBlock(false); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 973 | AddSuccessor(ThenBlock, sv.get()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 974 | } else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 975 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 976 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 977 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 980 | // Now create a new block containing the if statement. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 981 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 982 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 983 | // Set the terminator of the new block to the If statement. |
| 984 | Block->setTerminator(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 985 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 986 | // See if this is a known constant. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 987 | const TryResult &KnownVal = TryEvaluateBool(I->getCond()); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 988 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 989 | // Now add the successors. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 990 | AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock); |
| 991 | AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 992 | |
| 993 | // Add the condition as the last statement in the new block. This may create |
| 994 | // new blocks as the condition may contain control-flow. Any newly created |
| 995 | // blocks will be pointed to be "Block". |
Ted Kremenek | a7bcbde | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 996 | Block = addStmt(I->getCond()); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 997 | |
Ted Kremenek | a7bcbde | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 998 | // Finally, if the IfStmt contains a condition variable, add both the IfStmt |
| 999 | // and the condition variable initialization to the CFG. |
| 1000 | if (VarDecl *VD = I->getConditionVariable()) { |
| 1001 | if (Expr *Init = VD->getInit()) { |
| 1002 | autoCreateBlock(); |
| 1003 | AppendStmt(Block, I, AddStmtChoice::AlwaysAdd); |
| 1004 | addStmt(Init); |
| 1005 | } |
| 1006 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1007 | |
Ted Kremenek | a7bcbde | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 1008 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1009 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1010 | |
| 1011 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1012 | CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 1013 | // If we were in the middle of a block we stop processing that block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1014 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1015 | // NOTE: If a "return" appears in the middle of a block, this means that the |
| 1016 | // code afterwards is DEAD (unreachable). We still keep a basic block |
| 1017 | // for that code; a simple "mark-and-sweep" from the entry block will be |
| 1018 | // able to report such dead blocks. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1019 | |
| 1020 | // Create the new block. |
| 1021 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1022 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1023 | // The Exit block is the only successor. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1024 | AddSuccessor(Block, &cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1025 | |
| 1026 | // Add the return statement to the block. This may create new blocks if R |
| 1027 | // contains control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1028 | return VisitStmt(R, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { |
| 1032 | // Get the block of the labeled statement. Add it to our map. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1033 | addStmt(L->getSubStmt()); |
Ted Kremenek | cab47bd | 2008-03-15 07:45:02 +0000 | [diff] [blame] | 1034 | CFGBlock* LabelBlock = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1035 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1036 | if (!LabelBlock) // This can happen when the body is empty, i.e. |
| 1037 | LabelBlock = createBlock(); // scopes that only contains NullStmts. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1038 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1039 | assert(LabelMap.find(L) == LabelMap.end() && "label already in map"); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1040 | LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Labels partition blocks, so this is the end of the basic block we were |
| 1043 | // processing (L is the block's label). Because this is label (and we have |
| 1044 | // already processed the substatement) there is no extra control-flow to worry |
| 1045 | // about. |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1046 | LabelBlock->setLabel(L); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1047 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1048 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1049 | |
| 1050 | // We set Block to NULL to allow lazy creation of a new block (if necessary); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1051 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1053 | // This block is now the implicit successor of other blocks. |
| 1054 | Succ = LabelBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1055 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1056 | return LabelBlock; |
| 1057 | } |
| 1058 | |
| 1059 | CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1060 | // Goto is a control-flow statement. Thus we stop processing the current |
| 1061 | // block and create a new one. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1062 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1063 | Block = createBlock(false); |
| 1064 | Block->setTerminator(G); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1065 | |
| 1066 | // If we already know the mapping to the label block add the successor now. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1067 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1068 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1069 | if (I == LabelMap.end()) |
| 1070 | // We will need to backpatch this block later. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1071 | BackpatchBlocks.push_back(JumpSource(Block, ScopePos)); |
| 1072 | else { |
| 1073 | JumpTarget JT = I->second; |
| 1074 | AddSuccessor(Block, JT.Block); |
| 1075 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1076 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1077 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1081 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1082 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1083 | LocalScope::const_iterator LoopBeginScopePos = ScopePos; |
| 1084 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 1085 | // "for" is a control-flow statement. Thus we stop processing the current |
| 1086 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1087 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1088 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1089 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1090 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1091 | } else |
| 1092 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1093 | |
Ted Kremenek | 304a953 | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 1094 | // Save the current value for the break targets. |
| 1095 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1096 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
| 1097 | BreakJumpTarget = JumpTarget(LoopSuccessor, LoopBeginScopePos); |
Ted Kremenek | 304a953 | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 1098 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1099 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1100 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1101 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1102 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1103 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1104 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1105 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1106 | ExitConditionBlock->setTerminator(F); |
| 1107 | |
| 1108 | // Now add the actual condition to the condition block. Because the condition |
| 1109 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1110 | if (Stmt* C = F->getCond()) { |
| 1111 | Block = ExitConditionBlock; |
| 1112 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 7b31a61 | 2010-09-15 07:01:20 +0000 | [diff] [blame] | 1113 | assert(Block == EntryConditionBlock || |
| 1114 | (Block == 0 && EntryConditionBlock == Succ)); |
Ted Kremenek | ec92f94 | 2009-12-24 01:49:06 +0000 | [diff] [blame] | 1115 | |
| 1116 | // If this block contains a condition variable, add both the condition |
| 1117 | // variable and initializer to the CFG. |
| 1118 | if (VarDecl *VD = F->getConditionVariable()) { |
| 1119 | if (Expr *Init = VD->getInit()) { |
| 1120 | autoCreateBlock(); |
| 1121 | AppendStmt(Block, F, AddStmtChoice::AlwaysAdd); |
| 1122 | EntryConditionBlock = addStmt(Init); |
| 1123 | assert(Block == EntryConditionBlock); |
| 1124 | } |
| 1125 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1126 | |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1127 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1128 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1129 | return 0; |
| 1130 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1131 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1132 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1133 | // The condition block is the implicit successor for the loop body as well as |
| 1134 | // any code above the loop. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1135 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1136 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1137 | // See if this is a known constant. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1138 | TryResult KnownVal(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1140 | if (F->getCond()) |
| 1141 | KnownVal = TryEvaluateBool(F->getCond()); |
| 1142 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1143 | // Now create the loop body. |
| 1144 | { |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 1145 | assert(F->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1146 | |
Ted Kremenek | 304a953 | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 1147 | // Save the current values for Block, Succ, and continue targets. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1148 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 1149 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1150 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1151 | // Create a new block to contain the (bottom) of the loop body. |
| 1152 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1153 | |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 1154 | if (Stmt* I = F->getInc()) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1155 | // Generate increment code in its own basic block. This is the target of |
| 1156 | // continue statements. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1157 | Succ = addStmt(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1158 | } else { |
| 1159 | // No increment code. Create a special, empty, block that is used as the |
| 1160 | // target block for "looping back" to the start of the loop. |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1161 | assert(Succ == EntryConditionBlock); |
| 1162 | Succ = createBlock(); |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 1163 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1164 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1165 | // Finish up the increment (or empty) block if it hasn't been already. |
| 1166 | if (Block) { |
| 1167 | assert(Block == Succ); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1168 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1169 | return 0; |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1170 | Block = 0; |
| 1171 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1172 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1173 | ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1174 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1175 | // The starting block for the loop increment is the block that should |
| 1176 | // represent the 'loop target' for looping back to the start of the loop. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1177 | ContinueJumpTarget.Block->setLoopTarget(F); |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1178 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1179 | // Now populate the body block, and in the process create new blocks as we |
| 1180 | // walk the body of the loop. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1181 | CFGBlock* BodyBlock = addStmt(F->getBody()); |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1182 | |
| 1183 | if (!BodyBlock) |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1184 | BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);" |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1185 | else if (badCFG) |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1186 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1187 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1188 | // This new body block is a successor to our "exit" condition block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1189 | AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1190 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1191 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1192 | // Link up the condition block with the code that follows the loop. (the |
| 1193 | // false branch). |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1194 | AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1195 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1196 | // If the loop contains initialization, create a new block for those |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1197 | // statements. This block can also contain statements that precede the loop. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1198 | if (Stmt* I = F->getInit()) { |
| 1199 | Block = createBlock(); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1200 | return addStmt(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1201 | } else { |
| 1202 | // There is no loop initialization. We are thus basically a while loop. |
| 1203 | // NULL out Block to force lazy block construction. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1204 | Block = NULL; |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1205 | Succ = EntryConditionBlock; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1206 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 1210 | CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) { |
| 1211 | if (asc.alwaysAdd()) { |
| 1212 | autoCreateBlock(); |
| 1213 | AppendStmt(Block, M, asc); |
| 1214 | } |
| 1215 | return Visit(M->getBase(), |
| 1216 | M->isArrow() ? AddStmtChoice::NotAlwaysAdd |
| 1217 | : AddStmtChoice::AsLValueNotAlwaysAdd); |
| 1218 | } |
| 1219 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1220 | CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) { |
| 1221 | // Objective-C fast enumeration 'for' statements: |
| 1222 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC |
| 1223 | // |
| 1224 | // for ( Type newVariable in collection_expression ) { statements } |
| 1225 | // |
| 1226 | // becomes: |
| 1227 | // |
| 1228 | // prologue: |
| 1229 | // 1. collection_expression |
| 1230 | // T. jump to loop_entry |
| 1231 | // loop_entry: |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1232 | // 1. side-effects of element expression |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1233 | // 1. ObjCForCollectionStmt [performs binding to newVariable] |
| 1234 | // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil] |
| 1235 | // TB: |
| 1236 | // statements |
| 1237 | // T. jump to loop_entry |
| 1238 | // FB: |
| 1239 | // what comes after |
| 1240 | // |
| 1241 | // and |
| 1242 | // |
| 1243 | // Type existingItem; |
| 1244 | // for ( existingItem in expression ) { statements } |
| 1245 | // |
| 1246 | // becomes: |
| 1247 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1248 | // the same with newVariable replaced with existingItem; the binding works |
| 1249 | // the same except that for one ObjCForCollectionStmt::getElement() returns |
| 1250 | // a DeclStmt and the other returns a DeclRefExpr. |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1251 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1252 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1253 | CFGBlock* LoopSuccessor = 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1254 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1255 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1256 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1257 | return 0; |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1258 | LoopSuccessor = Block; |
| 1259 | Block = 0; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1260 | } else |
| 1261 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1262 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1263 | // Build the condition blocks. |
| 1264 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1265 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1266 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1267 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1268 | ExitConditionBlock->setTerminator(S); |
| 1269 | |
| 1270 | // The last statement in the block should be the ObjCForCollectionStmt, which |
| 1271 | // performs the actual binding to 'element' and determines if there are any |
| 1272 | // more items in the collection. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1273 | AppendStmt(ExitConditionBlock, S); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1274 | Block = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1275 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1276 | // Walk the 'element' expression to see if there are any side-effects. We |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1277 | // generate new blocks as necesary. We DON'T add the statement by default to |
| 1278 | // the CFG unless it contains control-flow. |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1279 | EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1280 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1281 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1282 | return 0; |
| 1283 | Block = 0; |
| 1284 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1285 | |
| 1286 | // The condition block is the implicit successor for the loop body as well as |
| 1287 | // any code above the loop. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1288 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1289 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1290 | // Now create the true branch. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1291 | { |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1292 | // Save the current values for Succ, continue and break targets. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1293 | SaveAndRestore<CFGBlock*> save_Succ(Succ); |
| 1294 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
| 1295 | save_break(BreakJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1296 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1297 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
| 1298 | ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1299 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1300 | CFGBlock* BodyBlock = addStmt(S->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1301 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1302 | if (!BodyBlock) |
| 1303 | BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1304 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1305 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1306 | return 0; |
| 1307 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1308 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1309 | // This new body block is a successor to our "exit" condition block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1310 | AddSuccessor(ExitConditionBlock, BodyBlock); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1311 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1312 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1313 | // Link up the condition block with the code that follows the loop. |
| 1314 | // (the false branch). |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1315 | AddSuccessor(ExitConditionBlock, LoopSuccessor); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1316 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1317 | // Now create a prologue block to contain the collection expression. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1318 | Block = createBlock(); |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1319 | return addStmt(S->getCollection()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1322 | CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) { |
| 1323 | // FIXME: Add locking 'primitives' to CFG for @synchronized. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1324 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1325 | // Inline the body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1326 | CFGBlock *SyncBlock = addStmt(S->getSynchBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1327 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1328 | // The sync body starts its own basic block. This makes it a little easier |
| 1329 | // for diagnostic clients. |
| 1330 | if (SyncBlock) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1331 | if (badCFG) |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1332 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1333 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1334 | Block = 0; |
Ted Kremenek | ecc31c9 | 2010-05-13 16:38:08 +0000 | [diff] [blame] | 1335 | Succ = SyncBlock; |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1336 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1337 | |
Ted Kremenek | ed12f1b | 2010-09-10 03:05:33 +0000 | [diff] [blame] | 1338 | // Add the @synchronized to the CFG. |
| 1339 | autoCreateBlock(); |
| 1340 | AppendStmt(Block, S, AddStmtChoice::AlwaysAdd); |
| 1341 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1342 | // Inline the sync expression. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1343 | return addStmt(S->getSynchExpr()); |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1344 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1345 | |
Ted Kremenek | 89cc8ea | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 1346 | CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1347 | // FIXME |
Ted Kremenek | 89be652 | 2009-04-07 04:26:02 +0000 | [diff] [blame] | 1348 | return NYS(); |
Ted Kremenek | 89cc8ea | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 1349 | } |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1350 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1351 | CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1352 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1353 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1354 | LocalScope::const_iterator LoopBeginScopePos = ScopePos; |
| 1355 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 1356 | // "while" is a control-flow statement. Thus we stop processing the current |
| 1357 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1358 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1359 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1360 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1361 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1362 | } else |
| 1363 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1364 | |
| 1365 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1366 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1367 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1368 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1369 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1370 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1371 | // Set the terminator for the "exit" condition block. |
| 1372 | ExitConditionBlock->setTerminator(W); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1373 | |
| 1374 | // Now add the actual condition to the condition block. Because the condition |
| 1375 | // itself may contain control-flow, new blocks may be created. Thus we update |
| 1376 | // "Succ" after adding the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1377 | if (Stmt* C = W->getCond()) { |
| 1378 | Block = ExitConditionBlock; |
| 1379 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1380 | assert(Block == EntryConditionBlock); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1381 | |
Ted Kremenek | 1ce53c4 | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 1382 | // If this block contains a condition variable, add both the condition |
| 1383 | // variable and initializer to the CFG. |
| 1384 | if (VarDecl *VD = W->getConditionVariable()) { |
| 1385 | if (Expr *Init = VD->getInit()) { |
| 1386 | autoCreateBlock(); |
| 1387 | AppendStmt(Block, W, AddStmtChoice::AlwaysAdd); |
| 1388 | EntryConditionBlock = addStmt(Init); |
| 1389 | assert(Block == EntryConditionBlock); |
| 1390 | } |
| 1391 | } |
| 1392 | |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1393 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1394 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1395 | return 0; |
| 1396 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1397 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1398 | |
| 1399 | // The condition block is the implicit successor for the loop body as well as |
| 1400 | // any code above the loop. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1401 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1402 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1403 | // See if this is a known constant. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1404 | const TryResult& KnownVal = TryEvaluateBool(W->getCond()); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1405 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1406 | // Process the loop body. |
| 1407 | { |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1408 | assert(W->getBody()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Save the current values for Block, Succ, and continue and break targets |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1411 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 1412 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
| 1413 | save_break(BreakJumpTarget); |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1414 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1415 | // Create an empty block to represent the transition block for looping back |
| 1416 | // to the head of the loop. |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1417 | Block = 0; |
| 1418 | assert(Succ == EntryConditionBlock); |
| 1419 | Succ = createBlock(); |
| 1420 | Succ->setLoopTarget(W); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1421 | ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1422 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1423 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1424 | BreakJumpTarget = JumpTarget(LoopSuccessor, LoopBeginScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1425 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1426 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 1427 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1428 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1429 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1430 | CFGBlock* BodyBlock = addStmt(W->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1431 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1432 | if (!BodyBlock) |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1433 | BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1434 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1435 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1436 | return 0; |
| 1437 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1438 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1439 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1440 | AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1441 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1442 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1443 | // Link up the condition block with the code that follows the loop. (the |
| 1444 | // false branch). |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1445 | AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1446 | |
| 1447 | // There can be no more statements in the condition block since we loop back |
| 1448 | // to this block. NULL out Block to force lazy creation of another block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1449 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1450 | |
Ted Kremenek | 1ce53c4 | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 1451 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1452 | Succ = EntryConditionBlock; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1453 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1454 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | |
| 1456 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1457 | CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { |
| 1458 | // FIXME: For now we pretend that @catch and the code it contains does not |
| 1459 | // exit. |
| 1460 | return Block; |
| 1461 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1462 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1463 | CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { |
| 1464 | // FIXME: This isn't complete. We basically treat @throw like a return |
| 1465 | // statement. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1466 | |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 1467 | // If we were in the middle of a block we stop processing that block. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1468 | if (badCFG) |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1469 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1470 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1471 | // Create the new block. |
| 1472 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1473 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1474 | // The Exit block is the only successor. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1475 | AddSuccessor(Block, &cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Add the statement to the block. This may create new blocks if S contains |
| 1478 | // control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1479 | return VisitStmt(S, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1480 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1481 | |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1482 | CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) { |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 1483 | // If we were in the middle of a block we stop processing that block. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1484 | if (badCFG) |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1485 | return 0; |
| 1486 | |
| 1487 | // Create the new block. |
| 1488 | Block = createBlock(false); |
| 1489 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1490 | if (TryTerminatedBlock) |
| 1491 | // The current try statement is the only successor. |
| 1492 | AddSuccessor(Block, TryTerminatedBlock); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1493 | else |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1494 | // otherwise the Exit block is the only successor. |
| 1495 | AddSuccessor(Block, &cfg->getExit()); |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1496 | |
| 1497 | // Add the statement to the block. This may create new blocks if S contains |
| 1498 | // control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1499 | return VisitStmt(T, AddStmtChoice::AlwaysAdd); |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1502 | CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1503 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1504 | |
Mike Stump | 8d50b6a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 1505 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 1506 | // current block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1507 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1508 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1509 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1510 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1511 | } else |
| 1512 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1513 | |
| 1514 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1515 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1516 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1517 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1518 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1519 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1520 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1521 | ExitConditionBlock->setTerminator(D); |
| 1522 | |
| 1523 | // Now add the actual condition to the condition block. Because the condition |
| 1524 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1525 | if (Stmt* C = D->getCond()) { |
| 1526 | Block = ExitConditionBlock; |
| 1527 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1528 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1529 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1530 | return 0; |
| 1531 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1532 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1533 | |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1534 | // The condition block is the implicit successor for the loop body. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1535 | Succ = EntryConditionBlock; |
| 1536 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1537 | // See if this is a known constant. |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1538 | const TryResult &KnownVal = TryEvaluateBool(D->getCond()); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1539 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1540 | // Process the loop body. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1541 | CFGBlock* BodyBlock = NULL; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1542 | { |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 1543 | assert(D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1544 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1545 | // Save the current values for Block, Succ, and continue and break targets |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1546 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 1547 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
| 1548 | save_break(BreakJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1549 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1550 | // All continues within this loop should go to the condition block |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1551 | ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1552 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1553 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1554 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1555 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1556 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 1557 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1558 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1559 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1560 | BodyBlock = addStmt(D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1561 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1562 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 1563 | BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1564 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1565 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1566 | return 0; |
| 1567 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1568 | |
Ted Kremenek | 110974d | 2010-08-17 20:59:56 +0000 | [diff] [blame] | 1569 | if (!KnownVal.isFalse()) { |
| 1570 | // Add an intermediate block between the BodyBlock and the |
| 1571 | // ExitConditionBlock to represent the "loop back" transition. Create an |
| 1572 | // empty block to represent the transition block for looping back to the |
| 1573 | // head of the loop. |
| 1574 | // FIXME: Can we do this more efficiently without adding another block? |
| 1575 | Block = NULL; |
| 1576 | Succ = BodyBlock; |
| 1577 | CFGBlock *LoopBackBlock = createBlock(); |
| 1578 | LoopBackBlock->setLoopTarget(D); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1579 | |
Ted Kremenek | 110974d | 2010-08-17 20:59:56 +0000 | [diff] [blame] | 1580 | // Add the loop body entry as a successor to the condition. |
| 1581 | AddSuccessor(ExitConditionBlock, LoopBackBlock); |
| 1582 | } |
| 1583 | else |
| 1584 | AddSuccessor(ExitConditionBlock, NULL); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1585 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1586 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1587 | // Link up the condition block with the code that follows the loop. |
| 1588 | // (the false branch). |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1589 | AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1590 | |
| 1591 | // There can be no more statements in the body block(s) since we loop back to |
| 1592 | // the body. NULL out Block to force lazy creation of another block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1593 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1594 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1595 | // Return the loop body, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1596 | Succ = BodyBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1597 | return BodyBlock; |
| 1598 | } |
| 1599 | |
| 1600 | CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { |
| 1601 | // "continue" is a control-flow statement. Thus we stop processing the |
| 1602 | // current block. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1603 | if (badCFG) |
| 1604 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1605 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1606 | // Now create a new block that ends with the continue statement. |
| 1607 | Block = createBlock(false); |
| 1608 | Block->setTerminator(C); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1609 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1610 | // If there is no target for the continue, then we are looking at an |
Ted Kremenek | 882cf06 | 2009-04-07 18:53:24 +0000 | [diff] [blame] | 1611 | // incomplete AST. This means the CFG cannot be constructed. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1612 | if (ContinueJumpTarget.Block) { |
| 1613 | AddSuccessor(Block, ContinueJumpTarget.Block); |
| 1614 | } else |
Ted Kremenek | 882cf06 | 2009-04-07 18:53:24 +0000 | [diff] [blame] | 1615 | badCFG = true; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1616 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1617 | return Block; |
| 1618 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1620 | CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1621 | AddStmtChoice asc) { |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1622 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1623 | if (asc.alwaysAdd()) { |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1624 | autoCreateBlock(); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1625 | AppendStmt(Block, E); |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1626 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1628 | // VLA types have expressions that must be evaluated. |
| 1629 | if (E->isArgumentType()) { |
| 1630 | for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr()); |
| 1631 | VA != 0; VA = FindVA(VA->getElementType().getTypePtr())) |
| 1632 | addStmt(VA->getSizeExpr()); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1633 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1635 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1636 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1638 | /// VisitStmtExpr - Utility method to handle (nested) statement |
| 1639 | /// expressions (a GCC extension). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1640 | CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) { |
| 1641 | if (asc.alwaysAdd()) { |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1642 | autoCreateBlock(); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1643 | AppendStmt(Block, SE); |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1644 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1645 | return VisitCompoundStmt(SE->getSubStmt()); |
| 1646 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1647 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1648 | CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1649 | // "switch" is a control-flow statement. Thus we stop processing the current |
| 1650 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1651 | CFGBlock* SwitchSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1652 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1653 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1654 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1655 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1656 | SwitchSuccessor = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1657 | } else SwitchSuccessor = Succ; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1658 | |
| 1659 | // Save the current "switch" context. |
| 1660 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1661 | save_default(DefaultCaseBlock); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1662 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1663 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1664 | // Set the "default" case to be the block after the switch statement. If the |
| 1665 | // switch statement contains a "default:", this value will be overwritten with |
| 1666 | // the block for that code. |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1667 | DefaultCaseBlock = SwitchSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1668 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1669 | // Create a new block that will contain the switch statement. |
| 1670 | SwitchTerminatedBlock = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1671 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1672 | // Now process the switch body. The code after the switch is the implicit |
| 1673 | // successor. |
| 1674 | Succ = SwitchSuccessor; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame^] | 1675 | BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1676 | |
| 1677 | // When visiting the body, the case statements should automatically get linked |
| 1678 | // up to the switch. We also don't keep a pointer to the body, since all |
| 1679 | // control-flow from the switch goes to case/default statements. |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 1680 | assert(Terminator->getBody() && "switch must contain a non-NULL body"); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1681 | Block = NULL; |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1682 | addStmt(Terminator->getBody()); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1683 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1684 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1685 | return 0; |
| 1686 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1687 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1688 | // If we have no "default:" case, the default transition is to the code |
| 1689 | // following the switch body. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1690 | AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1691 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1692 | // Add the terminator and condition in the switch block. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1693 | SwitchTerminatedBlock->setTerminator(Terminator); |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 1694 | assert(Terminator->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1695 | Block = SwitchTerminatedBlock; |
Ted Kremenek | 8b5dc12 | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 1696 | Block = addStmt(Terminator->getCond()); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1697 | |
Ted Kremenek | 8b5dc12 | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 1698 | // Finally, if the SwitchStmt contains a condition variable, add both the |
| 1699 | // SwitchStmt and the condition variable initialization to the CFG. |
| 1700 | if (VarDecl *VD = Terminator->getConditionVariable()) { |
| 1701 | if (Expr *Init = VD->getInit()) { |
| 1702 | autoCreateBlock(); |
| 1703 | AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd); |
| 1704 | addStmt(Init); |
| 1705 | } |
| 1706 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1707 | |
Ted Kremenek | 8b5dc12 | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 1708 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1711 | CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1712 | // CaseStmts are essentially labels, so they are the first statement in a |
| 1713 | // block. |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 1714 | CFGBlock *TopBlock = 0, *LastBlock = 0; |
| 1715 | |
| 1716 | if (Stmt *Sub = CS->getSubStmt()) { |
| 1717 | // For deeply nested chains of CaseStmts, instead of doing a recursion |
| 1718 | // (which can blow out the stack), manually unroll and create blocks |
| 1719 | // along the way. |
| 1720 | while (isa<CaseStmt>(Sub)) { |
| 1721 | CFGBlock *CurrentBlock = createBlock(false); |
| 1722 | CurrentBlock->setLabel(CS); |
Ted Kremenek | 55e91e8 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 1723 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 1724 | if (TopBlock) |
| 1725 | AddSuccessor(LastBlock, CurrentBlock); |
| 1726 | else |
| 1727 | TopBlock = CurrentBlock; |
| 1728 | |
| 1729 | AddSuccessor(SwitchTerminatedBlock, CurrentBlock); |
| 1730 | LastBlock = CurrentBlock; |
| 1731 | |
| 1732 | CS = cast<CaseStmt>(Sub); |
| 1733 | Sub = CS->getSubStmt(); |
| 1734 | } |
| 1735 | |
| 1736 | addStmt(Sub); |
| 1737 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | |
Ted Kremenek | 55e91e8 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 1739 | CFGBlock* CaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1740 | if (!CaseBlock) |
| 1741 | CaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1742 | |
| 1743 | // Cases statements partition blocks, so this is the top of the basic block we |
| 1744 | // were processing (the "case XXX:" is the label). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1745 | CaseBlock->setLabel(CS); |
| 1746 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1747 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1748 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1749 | |
| 1750 | // Add this block to the list of successors for the block with the switch |
| 1751 | // statement. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1752 | assert(SwitchTerminatedBlock); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1753 | AddSuccessor(SwitchTerminatedBlock, CaseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1754 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1755 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 1756 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1757 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 1758 | if (TopBlock) { |
| 1759 | AddSuccessor(LastBlock, CaseBlock); |
| 1760 | Succ = TopBlock; |
| 1761 | } |
| 1762 | else { |
| 1763 | // This block is now the implicit successor of other blocks. |
| 1764 | Succ = CaseBlock; |
| 1765 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1766 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 1767 | return Succ; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1768 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1769 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1770 | CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1771 | if (Terminator->getSubStmt()) |
| 1772 | addStmt(Terminator->getSubStmt()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1774 | DefaultCaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1775 | |
| 1776 | if (!DefaultCaseBlock) |
| 1777 | DefaultCaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1778 | |
| 1779 | // Default statements partition blocks, so this is the top of the basic block |
| 1780 | // we were processing (the "default:" is the label). |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1781 | DefaultCaseBlock->setLabel(Terminator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1783 | if (badCFG) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1784 | return 0; |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1785 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1786 | // Unlike case statements, we don't add the default block to the successors |
| 1787 | // for the switch statement immediately. This is done when we finish |
| 1788 | // processing the switch statement. This allows for the default case |
| 1789 | // (including a fall-through to the code after the switch statement) to always |
| 1790 | // be the last successor of a switch-terminated block. |
| 1791 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1792 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 1793 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1794 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1795 | // This block is now the implicit successor of other blocks. |
| 1796 | Succ = DefaultCaseBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1797 | |
| 1798 | return DefaultCaseBlock; |
Ted Kremenek | 9682be1 | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 1799 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1800 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1801 | CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) { |
| 1802 | // "try"/"catch" is a control-flow statement. Thus we stop processing the |
| 1803 | // current block. |
| 1804 | CFGBlock* TrySuccessor = NULL; |
| 1805 | |
| 1806 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1807 | if (badCFG) |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1808 | return 0; |
| 1809 | TrySuccessor = Block; |
| 1810 | } else TrySuccessor = Succ; |
| 1811 | |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 1812 | CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1813 | |
| 1814 | // Create a new block that will contain the try statement. |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 1815 | CFGBlock *NewTryTerminatedBlock = createBlock(false); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1816 | // Add the terminator in the try block. |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 1817 | NewTryTerminatedBlock->setTerminator(Terminator); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1818 | |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 1819 | bool HasCatchAll = false; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1820 | for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) { |
| 1821 | // The code after the try is the implicit successor. |
| 1822 | Succ = TrySuccessor; |
| 1823 | CXXCatchStmt *CS = Terminator->getHandler(h); |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 1824 | if (CS->getExceptionDecl() == 0) { |
| 1825 | HasCatchAll = true; |
| 1826 | } |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1827 | Block = NULL; |
| 1828 | CFGBlock *CatchBlock = VisitCXXCatchStmt(CS); |
| 1829 | if (CatchBlock == 0) |
| 1830 | return 0; |
| 1831 | // Add this block to the list of successors for the block with the try |
| 1832 | // statement. |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 1833 | AddSuccessor(NewTryTerminatedBlock, CatchBlock); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1834 | } |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 1835 | if (!HasCatchAll) { |
| 1836 | if (PrevTryTerminatedBlock) |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 1837 | AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock); |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 1838 | else |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 1839 | AddSuccessor(NewTryTerminatedBlock, &cfg->getExit()); |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 1840 | } |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1841 | |
| 1842 | // The code after the try is the implicit successor. |
| 1843 | Succ = TrySuccessor; |
| 1844 | |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 1845 | // Save the current "try" context. |
| 1846 | SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock); |
| 1847 | TryTerminatedBlock = NewTryTerminatedBlock; |
| 1848 | |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 1849 | assert(Terminator->getTryBlock() && "try must contain a non-NULL body"); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1850 | Block = NULL; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 1851 | Block = addStmt(Terminator->getTryBlock()); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1852 | return Block; |
| 1853 | } |
| 1854 | |
| 1855 | CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) { |
| 1856 | // CXXCatchStmt are treated like labels, so they are the first statement in a |
| 1857 | // block. |
| 1858 | |
| 1859 | if (CS->getHandlerBlock()) |
| 1860 | addStmt(CS->getHandlerBlock()); |
| 1861 | |
| 1862 | CFGBlock* CatchBlock = Block; |
| 1863 | if (!CatchBlock) |
| 1864 | CatchBlock = createBlock(); |
| 1865 | |
| 1866 | CatchBlock->setLabel(CS); |
| 1867 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1868 | if (badCFG) |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1869 | return 0; |
| 1870 | |
| 1871 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 1872 | Block = NULL; |
| 1873 | |
| 1874 | return CatchBlock; |
| 1875 | } |
| 1876 | |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1877 | CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C, |
Zhongxing Xu | 7e61217 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 1878 | AddStmtChoice asc) { |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1879 | AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue |
Zhongxing Xu | b5e94ac | 2010-04-14 05:50:04 +0000 | [diff] [blame] | 1880 | : AddStmtChoice::AlwaysAdd; |
Zhongxing Xu | 7e61217 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 1881 | autoCreateBlock(); |
Zhongxing Xu | b5e94ac | 2010-04-14 05:50:04 +0000 | [diff] [blame] | 1882 | AppendStmt(Block, C, AddStmtChoice(K)); |
Zhongxing Xu | 7e61217 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 1883 | return VisitChildren(C); |
| 1884 | } |
| 1885 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1886 | CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1887 | // Lazily create the indirect-goto dispatch block if there isn't one already. |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1888 | CFGBlock* IBlock = cfg->getIndirectGotoBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1889 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1890 | if (!IBlock) { |
| 1891 | IBlock = createBlock(false); |
| 1892 | cfg->setIndirectGotoBlock(IBlock); |
| 1893 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1894 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1895 | // IndirectGoto is a control-flow statement. Thus we stop processing the |
| 1896 | // current block and create a new one. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1897 | if (badCFG) |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1898 | return 0; |
| 1899 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1900 | Block = createBlock(false); |
| 1901 | Block->setTerminator(I); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1902 | AddSuccessor(Block, IBlock); |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1903 | return addStmt(I->getTarget()); |
| 1904 | } |
| 1905 | |
Ted Kremenek | 04cca64 | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 1906 | } // end anonymous namespace |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1907 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1908 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has |
| 1909 | /// no successors or predecessors. If this is the first block created in the |
| 1910 | /// CFG, it is automatically set to be the Entry and Exit of the CFG. |
Ted Kremenek | 813dd67 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 1911 | CFGBlock* CFG::createBlock() { |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1912 | bool first_block = begin() == end(); |
| 1913 | |
| 1914 | // Create the block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1915 | CFGBlock *Mem = getAllocator().Allocate<CFGBlock>(); |
| 1916 | new (Mem) CFGBlock(NumBlockIDs++, BlkBVC); |
| 1917 | Blocks.push_back(Mem, BlkBVC); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1918 | |
| 1919 | // If this is the first block, set it as the Entry and Exit. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1920 | if (first_block) |
| 1921 | Entry = Exit = &back(); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1922 | |
| 1923 | // Return the block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1924 | return &back(); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1927 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 1928 | /// CFG is returned to the caller. |
Mike Stump | 6bf1c08 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 1929 | CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C, |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 1930 | BuildOptions BO) { |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1931 | CFGBuilder Builder; |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 1932 | return Builder.buildCFG(D, Statement, C, BO); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1935 | //===----------------------------------------------------------------------===// |
| 1936 | // CFG: Queries for BlkExprs. |
| 1937 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1938 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1939 | namespace { |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1940 | typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy; |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Ted Kremenek | bff9844 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 1943 | static void FindSubExprAssignments(Stmt *S, |
| 1944 | llvm::SmallPtrSet<Expr*,50>& Set) { |
| 1945 | if (!S) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1946 | return; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1947 | |
Ted Kremenek | bff9844 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 1948 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) { |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1949 | Stmt *child = *I; |
Ted Kremenek | bff9844 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 1950 | if (!child) |
| 1951 | continue; |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1952 | |
Ted Kremenek | bff9844 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 1953 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(child)) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1954 | if (B->isAssignmentOp()) Set.insert(B); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1955 | |
Ted Kremenek | bff9844 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 1956 | FindSubExprAssignments(child, Set); |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1960 | static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) { |
| 1961 | BlkExprMapTy* M = new BlkExprMapTy(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Look for assignments that are used as subexpressions. These are the only |
| 1964 | // assignments that we want to *possibly* register as a block-level |
| 1965 | // expression. Basically, if an assignment occurs both in a subexpression and |
| 1966 | // at the block-level, it is a block-level expression. |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1967 | llvm::SmallPtrSet<Expr*,50> SubExprAssignments; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1968 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1969 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1970 | for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 1971 | if (CFGStmt S = BI->getAs<CFGStmt>()) |
| 1972 | FindSubExprAssignments(S, SubExprAssignments); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1973 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1974 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Iterate over the statements again on identify the Expr* and Stmt* at the |
| 1977 | // block-level that are block-level expressions. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1978 | |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 1979 | for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) { |
| 1980 | CFGStmt CS = BI->getAs<CFGStmt>(); |
| 1981 | if (!CS.isValid()) |
| 1982 | continue; |
| 1983 | if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1984 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1985 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) { |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1986 | // Assignment expressions that are not nested within another |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1987 | // expression are really "statements" whose value is never used by |
| 1988 | // another expression. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1989 | if (B->isAssignmentOp() && !SubExprAssignments.count(Exp)) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1990 | continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1991 | } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) { |
| 1992 | // Special handling for statement expressions. The last statement in |
| 1993 | // the statement expression is also a block-level expr. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1994 | const CompoundStmt* C = Terminator->getSubStmt(); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1995 | if (!C->body_empty()) { |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1996 | unsigned x = M->size(); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1997 | (*M)[C->body_back()] = x; |
| 1998 | } |
| 1999 | } |
Ted Kremenek | 0cb1ba2 | 2008-01-25 23:22:27 +0000 | [diff] [blame] | 2000 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2001 | unsigned x = M->size(); |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2002 | (*M)[Exp] = x; |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2003 | } |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 2004 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2005 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2006 | // Look at terminators. The condition is a block-level expression. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2007 | |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2008 | Stmt* S = (*I)->getTerminatorCondition(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2009 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2010 | if (S && M->find(S) == M->end()) { |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2011 | unsigned x = M->size(); |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2012 | (*M)[S] = x; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2013 | } |
| 2014 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2015 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2016 | return M; |
| 2017 | } |
| 2018 | |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 2019 | CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) { |
| 2020 | assert(S != NULL); |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2021 | if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2022 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2023 | BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 2024 | BlkExprMapTy::iterator I = M->find(S); |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2025 | return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second); |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | unsigned CFG::getNumBlkExprs() { |
| 2029 | if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap)) |
| 2030 | return M->size(); |
| 2031 | else { |
| 2032 | // We assume callers interested in the number of BlkExprs will want |
| 2033 | // the map constructed if it doesn't already exist. |
| 2034 | BlkExprMap = (void*) PopulateBlkExprMap(*this); |
| 2035 | return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size(); |
| 2036 | } |
| 2037 | } |
| 2038 | |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 2039 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 2040 | // Filtered walking of the CFG. |
| 2041 | //===----------------------------------------------------------------------===// |
| 2042 | |
| 2043 | bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F, |
Ted Kremenek | f146cd1 | 2010-09-09 02:57:48 +0000 | [diff] [blame] | 2044 | const CFGBlock *From, const CFGBlock *To) { |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 2045 | |
| 2046 | if (F.IgnoreDefaultsWithCoveredEnums) { |
| 2047 | // If the 'To' has no label or is labeled but the label isn't a |
| 2048 | // CaseStmt then filter this edge. |
| 2049 | if (const SwitchStmt *S = |
Ted Kremenek | f146cd1 | 2010-09-09 02:57:48 +0000 | [diff] [blame] | 2050 | dyn_cast_or_null<SwitchStmt>(From->getTerminator())) { |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 2051 | if (S->isAllEnumCasesCovered()) { |
Ted Kremenek | f146cd1 | 2010-09-09 02:57:48 +0000 | [diff] [blame] | 2052 | const Stmt *L = To->getLabel(); |
| 2053 | if (!L || !isa<CaseStmt>(L)) |
| 2054 | return true; |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | return false; |
| 2060 | } |
| 2061 | |
| 2062 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 2063 | // Cleanup: CFG dstor. |
| 2064 | //===----------------------------------------------------------------------===// |
| 2065 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2066 | CFG::~CFG() { |
| 2067 | delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap); |
| 2068 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2069 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2070 | //===----------------------------------------------------------------------===// |
| 2071 | // CFG pretty printing |
| 2072 | //===----------------------------------------------------------------------===// |
| 2073 | |
Ted Kremenek | 7e776b1 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 2074 | namespace { |
| 2075 | |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 2076 | class StmtPrinterHelper : public PrinterHelper { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2077 | typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2078 | typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2079 | StmtMapTy StmtMap; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2080 | DeclMapTy DeclMap; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2081 | signed CurrentBlock; |
| 2082 | unsigned CurrentStmt; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2083 | const LangOptions &LangOpts; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2084 | public: |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2085 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2086 | StmtPrinterHelper(const CFG* cfg, const LangOptions &LO) |
| 2087 | : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2088 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { |
| 2089 | unsigned j = 1; |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2090 | for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2091 | BI != BEnd; ++BI, ++j ) { |
| 2092 | if (CFGStmt SE = BI->getAs<CFGStmt>()) { |
| 2093 | std::pair<unsigned, unsigned> P((*I)->getBlockID(), j); |
| 2094 | StmtMap[SE] = P; |
| 2095 | |
| 2096 | if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) { |
| 2097 | DeclMap[DS->getSingleDecl()] = P; |
| 2098 | |
| 2099 | } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) { |
| 2100 | if (VarDecl* VD = IS->getConditionVariable()) |
| 2101 | DeclMap[VD] = P; |
| 2102 | |
| 2103 | } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) { |
| 2104 | if (VarDecl* VD = FS->getConditionVariable()) |
| 2105 | DeclMap[VD] = P; |
| 2106 | |
| 2107 | } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) { |
| 2108 | if (VarDecl* VD = WS->getConditionVariable()) |
| 2109 | DeclMap[VD] = P; |
| 2110 | |
| 2111 | } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) { |
| 2112 | if (VarDecl* VD = SS->getConditionVariable()) |
| 2113 | DeclMap[VD] = P; |
| 2114 | |
| 2115 | } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) { |
| 2116 | if (VarDecl* VD = CS->getExceptionDecl()) |
| 2117 | DeclMap[VD] = P; |
| 2118 | } |
| 2119 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2120 | } |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 2121 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2122 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2123 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2124 | virtual ~StmtPrinterHelper() {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2125 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2126 | const LangOptions &getLangOpts() const { return LangOpts; } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2127 | void setBlockID(signed i) { CurrentBlock = i; } |
| 2128 | void setStmtID(unsigned i) { CurrentStmt = i; } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2129 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2130 | virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) { |
| 2131 | StmtMapTy::iterator I = StmtMap.find(S); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2132 | |
| 2133 | if (I == StmtMap.end()) |
| 2134 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2135 | |
| 2136 | if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2137 | && I->second.second == CurrentStmt) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2138 | return false; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2139 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2140 | |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2141 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2142 | return true; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2143 | } |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2144 | |
| 2145 | bool handleDecl(Decl* D, llvm::raw_ostream& OS) { |
| 2146 | DeclMapTy::iterator I = DeclMap.find(D); |
| 2147 | |
| 2148 | if (I == DeclMap.end()) |
| 2149 | return false; |
| 2150 | |
| 2151 | if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock |
| 2152 | && I->second.second == CurrentStmt) { |
| 2153 | return false; |
| 2154 | } |
| 2155 | |
| 2156 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
| 2157 | return true; |
| 2158 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2159 | }; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2160 | } // end anonymous namespace |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2161 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2162 | |
| 2163 | namespace { |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 2164 | class CFGBlockTerminatorPrint |
Ted Kremenek | 83ebcef | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 2165 | : public StmtVisitor<CFGBlockTerminatorPrint,void> { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2166 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2167 | llvm::raw_ostream& OS; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2168 | StmtPrinterHelper* Helper; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2169 | PrintingPolicy Policy; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2170 | public: |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2171 | CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper, |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2172 | const PrintingPolicy &Policy) |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2173 | : OS(os), Helper(helper), Policy(Policy) {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2174 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2175 | void VisitIfStmt(IfStmt* I) { |
| 2176 | OS << "if "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2177 | I->getCond()->printPretty(OS,Helper,Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2178 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2179 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2180 | // Default case. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2181 | void VisitStmt(Stmt* Terminator) { |
| 2182 | Terminator->printPretty(OS, Helper, Policy); |
| 2183 | } |
| 2184 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2185 | void VisitForStmt(ForStmt* F) { |
| 2186 | OS << "for (" ; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2187 | if (F->getInit()) |
| 2188 | OS << "..."; |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 2189 | OS << "; "; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2190 | if (Stmt* C = F->getCond()) |
| 2191 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 2192 | OS << "; "; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2193 | if (F->getInc()) |
| 2194 | OS << "..."; |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2195 | OS << ")"; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2196 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2197 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2198 | void VisitWhileStmt(WhileStmt* W) { |
| 2199 | OS << "while " ; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2200 | if (Stmt* C = W->getCond()) |
| 2201 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2202 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2203 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2204 | void VisitDoStmt(DoStmt* D) { |
| 2205 | OS << "do ... while "; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2206 | if (Stmt* C = D->getCond()) |
| 2207 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 2208 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2209 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2210 | void VisitSwitchStmt(SwitchStmt* Terminator) { |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 2211 | OS << "switch "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2212 | Terminator->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 2213 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2214 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2215 | void VisitCXXTryStmt(CXXTryStmt* CS) { |
| 2216 | OS << "try ..."; |
| 2217 | } |
| 2218 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2219 | void VisitConditionalOperator(ConditionalOperator* C) { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2220 | C->getCond()->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2221 | OS << " ? ... : ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2222 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2223 | |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 2224 | void VisitChooseExpr(ChooseExpr* C) { |
| 2225 | OS << "__builtin_choose_expr( "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2226 | C->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2227 | OS << " )"; |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 2228 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2229 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2230 | void VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
| 2231 | OS << "goto *"; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2232 | I->getTarget()->printPretty(OS, Helper, Policy); |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2233 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2234 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2235 | void VisitBinaryOperator(BinaryOperator* B) { |
| 2236 | if (!B->isLogicalOp()) { |
| 2237 | VisitExpr(B); |
| 2238 | return; |
| 2239 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2240 | |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2241 | B->getLHS()->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2242 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2243 | switch (B->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2244 | case BO_LOr: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2245 | OS << " || ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2246 | return; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2247 | case BO_LAnd: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2248 | OS << " && ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2249 | return; |
| 2250 | default: |
| 2251 | assert(false && "Invalid logical operator."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2252 | } |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2253 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2254 | |
Ted Kremenek | 12687ff | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 2255 | void VisitExpr(Expr* E) { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2256 | E->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2257 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2258 | }; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2259 | } // end anonymous namespace |
| 2260 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2261 | static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper, |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2262 | const CFGElement &E) { |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2263 | if (CFGStmt CS = E.getAs<CFGStmt>()) { |
| 2264 | Stmt *S = CS; |
| 2265 | |
| 2266 | if (Helper) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2267 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2268 | // special printing for statement-expressions. |
| 2269 | if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) { |
| 2270 | CompoundStmt* Sub = SE->getSubStmt(); |
| 2271 | |
| 2272 | if (Sub->child_begin() != Sub->child_end()) { |
| 2273 | OS << "({ ... ; "; |
| 2274 | Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS); |
| 2275 | OS << " })\n"; |
| 2276 | return; |
| 2277 | } |
| 2278 | } |
| 2279 | // special printing for comma expressions. |
| 2280 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 2281 | if (B->getOpcode() == BO_Comma) { |
| 2282 | OS << "... , "; |
| 2283 | Helper->handledStmt(B->getRHS(),OS); |
| 2284 | OS << '\n'; |
| 2285 | return; |
| 2286 | } |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2287 | } |
| 2288 | } |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2289 | S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts())); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2290 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2291 | if (isa<CXXOperatorCallExpr>(S)) { |
| 2292 | OS << " (OperatorCall)"; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2293 | } |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2294 | else if (isa<CXXBindTemporaryExpr>(S)) { |
| 2295 | OS << " (BindTemporary)"; |
| 2296 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2297 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2298 | // Expressions need a newline. |
| 2299 | if (isa<Expr>(S)) |
| 2300 | OS << '\n'; |
Ted Kremenek | 0f5d8bc | 2010-08-31 18:47:37 +0000 | [diff] [blame] | 2301 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2302 | } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) { |
| 2303 | CXXBaseOrMemberInitializer* I = IE; |
| 2304 | if (I->isBaseInitializer()) |
| 2305 | OS << I->getBaseClass()->getAsCXXRecordDecl()->getName(); |
| 2306 | else OS << I->getMember()->getName(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2307 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2308 | OS << "("; |
| 2309 | if (Expr* IE = I->getInit()) |
| 2310 | IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts())); |
| 2311 | OS << ")"; |
| 2312 | |
| 2313 | if (I->isBaseInitializer()) |
| 2314 | OS << " (Base initializer)\n"; |
| 2315 | else OS << " (Member initializer)\n"; |
| 2316 | |
| 2317 | } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){ |
| 2318 | VarDecl* VD = DE.getVarDecl(); |
| 2319 | Helper->handleDecl(VD, OS); |
| 2320 | |
| 2321 | Type* T = VD->getType().getTypePtr(); |
| 2322 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) |
| 2323 | T = RT->getPointeeType().getTypePtr(); |
| 2324 | |
| 2325 | OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()"; |
| 2326 | OS << " (Implicit destructor)\n"; |
| 2327 | } |
| 2328 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2329 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2330 | static void print_block(llvm::raw_ostream& OS, const CFG* cfg, |
| 2331 | const CFGBlock& B, |
| 2332 | StmtPrinterHelper* Helper, bool print_edges) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2333 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2334 | if (Helper) Helper->setBlockID(B.getBlockID()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2335 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2336 | // Print the header. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2337 | OS << "\n [ B" << B.getBlockID(); |
| 2338 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2339 | if (&B == &cfg->getEntry()) |
| 2340 | OS << " (ENTRY) ]\n"; |
| 2341 | else if (&B == &cfg->getExit()) |
| 2342 | OS << " (EXIT) ]\n"; |
| 2343 | else if (&B == cfg->getIndirectGotoBlock()) |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2344 | OS << " (INDIRECT GOTO DISPATCH) ]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2345 | else |
| 2346 | OS << " ]\n"; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2347 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2348 | // Print the label of this block. |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2349 | if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2350 | |
| 2351 | if (print_edges) |
| 2352 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2353 | |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2354 | if (LabelStmt* L = dyn_cast<LabelStmt>(Label)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2355 | OS << L->getName(); |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2356 | else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2357 | OS << "case "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2358 | C->getLHS()->printPretty(OS, Helper, |
| 2359 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2360 | if (C->getRHS()) { |
| 2361 | OS << " ... "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2362 | C->getRHS()->printPretty(OS, Helper, |
| 2363 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2364 | } |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2365 | } else if (isa<DefaultStmt>(Label)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2366 | OS << "default"; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2367 | else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) { |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2368 | OS << "catch ("; |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 2369 | if (CS->getExceptionDecl()) |
| 2370 | CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()), |
| 2371 | 0); |
| 2372 | else |
| 2373 | OS << "..."; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2374 | OS << ")"; |
| 2375 | |
| 2376 | } else |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2377 | assert(false && "Invalid label statement in CFGBlock."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2378 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2379 | OS << ":\n"; |
| 2380 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2381 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2382 | // Iterate through the statements in the block and print them. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2383 | unsigned j = 1; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2384 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2385 | for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; |
| 2386 | I != E ; ++I, ++j ) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2387 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2388 | // Print the statement # in the basic block and the statement itself. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2389 | if (print_edges) |
| 2390 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2391 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2392 | OS << llvm::format("%3d", j) << ": "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2393 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2394 | if (Helper) |
| 2395 | Helper->setStmtID(j); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2396 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2397 | print_elem(OS,Helper,*I); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2398 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2399 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2400 | // Print the terminator of this block. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2401 | if (B.getTerminator()) { |
| 2402 | if (print_edges) |
| 2403 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2404 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2405 | OS << " T: "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2406 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2407 | if (Helper) Helper->setBlockID(-1); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2408 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2409 | CFGBlockTerminatorPrint TPrinter(OS, Helper, |
| 2410 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2411 | TPrinter.Visit(const_cast<Stmt*>(B.getTerminator())); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2412 | OS << '\n'; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2413 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2414 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2415 | if (print_edges) { |
| 2416 | // Print the predecessors of this block. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2417 | OS << " Predecessors (" << B.pred_size() << "):"; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2418 | unsigned i = 0; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2419 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2420 | for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); |
| 2421 | I != E; ++I, ++i) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2422 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2423 | if (i == 8 || (i-8) == 0) |
| 2424 | OS << "\n "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2425 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2426 | OS << " B" << (*I)->getBlockID(); |
| 2427 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2428 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2429 | OS << '\n'; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2430 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2431 | // Print the successors of this block. |
| 2432 | OS << " Successors (" << B.succ_size() << "):"; |
| 2433 | i = 0; |
| 2434 | |
| 2435 | for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); |
| 2436 | I != E; ++I, ++i) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2437 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2438 | if (i == 8 || (i-8) % 10 == 0) |
| 2439 | OS << "\n "; |
| 2440 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 2441 | if (*I) |
| 2442 | OS << " B" << (*I)->getBlockID(); |
| 2443 | else |
| 2444 | OS << " NULL"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2445 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2446 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2447 | OS << '\n'; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2448 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2449 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2450 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2451 | |
| 2452 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2453 | void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2454 | |
| 2455 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2456 | void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const { |
| 2457 | StmtPrinterHelper Helper(this, LO); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2458 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2459 | // Print the entry block. |
| 2460 | print_block(OS, this, getEntry(), &Helper, true); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2461 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2462 | // Iterate through the CFGBlocks and print them one by one. |
| 2463 | for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 2464 | // Skip the entry block, because we already printed it. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2465 | if (&(**I) == &getEntry() || &(**I) == &getExit()) |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2466 | continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2467 | |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2468 | print_block(OS, this, **I, &Helper, true); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2469 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2470 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2471 | // Print the exit block. |
| 2472 | print_block(OS, this, getExit(), &Helper, true); |
Ted Kremenek | e03879b | 2008-11-24 20:50:24 +0000 | [diff] [blame] | 2473 | OS.flush(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2474 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2475 | |
| 2476 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2477 | void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const { |
| 2478 | print(llvm::errs(), cfg, LO); |
| 2479 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2480 | |
| 2481 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 2482 | /// Generally this will only be called from CFG::print. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2483 | void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg, |
| 2484 | const LangOptions &LO) const { |
| 2485 | StmtPrinterHelper Helper(cfg, LO); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2486 | print_block(OS, cfg, *this, &Helper, true); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2487 | } |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2488 | |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2489 | /// printTerminator - A simple pretty printer of the terminator of a CFGBlock. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2490 | void CFGBlock::printTerminator(llvm::raw_ostream &OS, |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2491 | const LangOptions &LO) const { |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2492 | CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO)); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2493 | TPrinter.Visit(const_cast<Stmt*>(getTerminator())); |
| 2494 | } |
| 2495 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2496 | Stmt* CFGBlock::getTerminatorCondition() { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2497 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2498 | if (!Terminator) |
| 2499 | return NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2500 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2501 | Expr* E = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2502 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2503 | switch (Terminator->getStmtClass()) { |
| 2504 | default: |
| 2505 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2506 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2507 | case Stmt::ForStmtClass: |
| 2508 | E = cast<ForStmt>(Terminator)->getCond(); |
| 2509 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2510 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2511 | case Stmt::WhileStmtClass: |
| 2512 | E = cast<WhileStmt>(Terminator)->getCond(); |
| 2513 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2514 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2515 | case Stmt::DoStmtClass: |
| 2516 | E = cast<DoStmt>(Terminator)->getCond(); |
| 2517 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2518 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2519 | case Stmt::IfStmtClass: |
| 2520 | E = cast<IfStmt>(Terminator)->getCond(); |
| 2521 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2522 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2523 | case Stmt::ChooseExprClass: |
| 2524 | E = cast<ChooseExpr>(Terminator)->getCond(); |
| 2525 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2526 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2527 | case Stmt::IndirectGotoStmtClass: |
| 2528 | E = cast<IndirectGotoStmt>(Terminator)->getTarget(); |
| 2529 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2530 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2531 | case Stmt::SwitchStmtClass: |
| 2532 | E = cast<SwitchStmt>(Terminator)->getCond(); |
| 2533 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2534 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2535 | case Stmt::ConditionalOperatorClass: |
| 2536 | E = cast<ConditionalOperator>(Terminator)->getCond(); |
| 2537 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2538 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2539 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 2540 | E = cast<BinaryOperator>(Terminator)->getLHS(); |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2541 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2542 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2543 | case Stmt::ObjCForCollectionStmtClass: |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2544 | return Terminator; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2545 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2546 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2547 | return E ? E->IgnoreParens() : NULL; |
| 2548 | } |
| 2549 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2550 | bool CFGBlock::hasBinaryBranchTerminator() const { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2551 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2552 | if (!Terminator) |
| 2553 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2554 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2555 | Expr* E = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2556 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2557 | switch (Terminator->getStmtClass()) { |
| 2558 | default: |
| 2559 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2560 | |
| 2561 | case Stmt::ForStmtClass: |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2562 | case Stmt::WhileStmtClass: |
| 2563 | case Stmt::DoStmtClass: |
| 2564 | case Stmt::IfStmtClass: |
| 2565 | case Stmt::ChooseExprClass: |
| 2566 | case Stmt::ConditionalOperatorClass: |
| 2567 | case Stmt::BinaryOperatorClass: |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2568 | return true; |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2569 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2570 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2571 | return E ? E->IgnoreParens() : NULL; |
| 2572 | } |
| 2573 | |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2574 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2575 | //===----------------------------------------------------------------------===// |
| 2576 | // CFG Graphviz Visualization |
| 2577 | //===----------------------------------------------------------------------===// |
| 2578 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2579 | |
| 2580 | #ifndef NDEBUG |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2581 | static StmtPrinterHelper* GraphHelper; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2582 | #endif |
| 2583 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2584 | void CFG::viewCFG(const LangOptions &LO) const { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2585 | #ifndef NDEBUG |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2586 | StmtPrinterHelper H(this, LO); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2587 | GraphHelper = &H; |
| 2588 | llvm::ViewGraph(this,"CFG"); |
| 2589 | GraphHelper = NULL; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2590 | #endif |
| 2591 | } |
| 2592 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2593 | namespace llvm { |
| 2594 | template<> |
| 2595 | struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { |
Tobias Grosser | 9fc223a | 2009-11-30 14:16:05 +0000 | [diff] [blame] | 2596 | |
| 2597 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 2598 | |
| 2599 | static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) { |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2600 | |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 2601 | #ifndef NDEBUG |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2602 | std::string OutSStr; |
| 2603 | llvm::raw_string_ostream Out(OutSStr); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2604 | print_block(Out,Graph, *Node, GraphHelper, false); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2605 | std::string& OutStr = Out.str(); |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2606 | |
| 2607 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 2608 | |
| 2609 | // Process string output to make it nicer... |
| 2610 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 2611 | if (OutStr[i] == '\n') { // Left justify |
| 2612 | OutStr[i] = '\\'; |
| 2613 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 2614 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2615 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2616 | return OutStr; |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 2617 | #else |
| 2618 | return ""; |
| 2619 | #endif |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2620 | } |
| 2621 | }; |
| 2622 | } // end namespace llvm |