Ted Kremenek | fddd518 | 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 | 0bc735f | 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 | fddd518 | 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 | bd04878 | 2009-07-22 21:45:16 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Support/SaveAndRestore.h" |
Ted Kremenek | e41611a | 2009-07-16 18:13:04 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/CFG.h" |
Mike Stump | b978a44 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | c310e93 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 19 | #include "clang/AST/PrettyPrinter.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 20 | #include "llvm/Support/GraphWriter.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Allocator.h" |
| 22 | #include "llvm/Support/Format.h" |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 0ba497b | 2009-10-20 23:46:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | 83c01da | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | namespace { |
| 30 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 31 | static SourceLocation GetEndLoc(Decl* D) { |
Ted Kremenek | c7eb903 | 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 | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 35 | |
| 36 | return D->getLocation(); |
Ted Kremenek | c7eb903 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 37 | } |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 39 | class AddStmtChoice { |
| 40 | public: |
Ted Kremenek | 5ba290a | 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 | 792bea9 | 2010-03-03 16:28:47 +0000 | [diff] [blame] | 46 | AddStmtChoice(Kind kind) : k(kind) {} |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 47 | |
Benjamin Kramer | 792bea9 | 2010-03-03 16:28:47 +0000 | [diff] [blame] | 48 | bool alwaysAdd() const { return (unsigned)k & 0x1; } |
Ted Kremenek | 431ac2d | 2010-04-11 17:02:04 +0000 | [diff] [blame] | 49 | bool asLValue() const { return k >= AsLValueNotAlwaysAdd; } |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 51 | private: |
Benjamin Kramer | 792bea9 | 2010-03-03 16:28:47 +0000 | [diff] [blame] | 52 | Kind k; |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 53 | }; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 54 | |
Marcin Swiderski | f1308c7 | 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, |
Marcin Swiderski | 35387a0 | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 65 | /// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 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 |
Marcin Swiderski | 35387a0 | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 81 | /// scope on reaching the beginning of currently iterated scope. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 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 | } |
Marcin Swiderski | 35387a0 | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 125 | const_iterator operator++(int) { |
| 126 | const_iterator P = *this; |
| 127 | ++*this; |
| 128 | return P; |
| 129 | } |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 130 | |
| 131 | bool operator==(const const_iterator& rhs) const { |
| 132 | return Scope == rhs.Scope && VarIter == rhs.VarIter; |
| 133 | } |
| 134 | bool operator!=(const const_iterator& rhs) const { |
| 135 | return !(*this == rhs); |
| 136 | } |
Marcin Swiderski | 35387a0 | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 137 | |
| 138 | operator bool() const { |
| 139 | return *this != const_iterator(); |
| 140 | } |
| 141 | |
| 142 | int distance(const_iterator L); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | friend class const_iterator; |
| 146 | |
| 147 | private: |
| 148 | /// Automatic variables in order of declaration. |
| 149 | AutomaticVarsTy Vars; |
| 150 | /// Iterator to variable in previous scope that was declared just before |
| 151 | /// begin of this scope. |
| 152 | const_iterator Prev; |
| 153 | |
| 154 | public: |
| 155 | /// Constructs empty scope linked to previous scope in specified place. |
| 156 | LocalScope(const_iterator P) |
| 157 | : Vars() |
| 158 | , Prev(P) {} |
| 159 | |
| 160 | /// Begin of scope in direction of CFG building (backwards). |
| 161 | const_iterator begin() const { return const_iterator(*this, Vars.size()); } |
Marcin Swiderski | 35387a0 | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 162 | |
| 163 | void addVar(VarDecl* VD) { |
| 164 | Vars.push_back(VD); |
| 165 | } |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
Marcin Swiderski | 35387a0 | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 168 | /// distance - Calculates distance from this to L. L must be reachable from this |
| 169 | /// (with use of ++ operator). Cost of calculating the distance is linear w.r.t. |
| 170 | /// number of scopes between this and L. |
| 171 | int LocalScope::const_iterator::distance(LocalScope::const_iterator L) { |
| 172 | int D = 0; |
| 173 | const_iterator F = *this; |
| 174 | while (F.Scope != L.Scope) { |
| 175 | assert (F != const_iterator() |
| 176 | && "L iterator is not reachable from F iterator."); |
| 177 | D += F.VarIter; |
| 178 | F = F.Scope->Prev; |
| 179 | } |
| 180 | D += F.VarIter - L.VarIter; |
| 181 | return D; |
| 182 | } |
| 183 | |
| 184 | /// BlockScopePosPair - Structure for specifying position in CFG during its |
| 185 | /// build process. It consists of CFGBlock that specifies position in CFG graph |
| 186 | /// and LocalScope::const_iterator that specifies position in LocalScope graph. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 187 | struct BlockScopePosPair { |
| 188 | BlockScopePosPair() {} |
| 189 | BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S) |
| 190 | : Block(B), ScopePos(S) {} |
| 191 | |
| 192 | CFGBlock* Block; |
| 193 | LocalScope::const_iterator ScopePos; |
| 194 | }; |
| 195 | |
Ted Kremenek | a34ea07 | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 196 | /// CFGBuilder - This class implements CFG construction from an AST. |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 197 | /// The builder is stateful: an instance of the builder should be used to only |
| 198 | /// construct a single CFG. |
| 199 | /// |
| 200 | /// Example usage: |
| 201 | /// |
| 202 | /// CFGBuilder builder; |
| 203 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 204 | /// |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 205 | /// CFG construction is done via a recursive walk of an AST. We actually parse |
| 206 | /// the AST in reverse order so that the successor of a basic block is |
| 207 | /// constructed prior to its predecessor. This allows us to nicely capture |
| 208 | /// implicit fall-throughs without extra basic blocks. |
Ted Kremenek | c310e93 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 209 | /// |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 210 | class CFGBuilder { |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 211 | typedef BlockScopePosPair JumpTarget; |
| 212 | typedef BlockScopePosPair JumpSource; |
| 213 | |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 214 | ASTContext *Context; |
Ted Kremenek | 0ba497b | 2009-10-20 23:46:25 +0000 | [diff] [blame] | 215 | llvm::OwningPtr<CFG> cfg; |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 217 | CFGBlock* Block; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 218 | CFGBlock* Succ; |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 219 | JumpTarget ContinueJumpTarget; |
| 220 | JumpTarget BreakJumpTarget; |
Ted Kremenek | b5c13b0 | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 221 | CFGBlock* SwitchTerminatedBlock; |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 222 | CFGBlock* DefaultCaseBlock; |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 223 | CFGBlock* TryTerminatedBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 224 | |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 225 | // Current position in local scope. |
| 226 | LocalScope::const_iterator ScopePos; |
| 227 | |
| 228 | // LabelMap records the mapping from Label expressions to their jump targets. |
| 229 | typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy; |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 230 | LabelMapTy LabelMap; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 231 | |
| 232 | // A list of blocks that end with a "goto" that must be backpatched to their |
| 233 | // resolved targets upon completion of CFG construction. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 234 | typedef std::vector<JumpSource> BackpatchBlocksTy; |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 235 | BackpatchBlocksTy BackpatchBlocks; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 237 | // A list of labels whose address has been taken (for indirect gotos). |
| 238 | typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy; |
| 239 | LabelSetTy AddressTakenLabels; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 240 | |
Zhongxing Xu | 49b4ef3 | 2010-09-16 03:28:18 +0000 | [diff] [blame] | 241 | bool badCFG; |
| 242 | CFG::BuildOptions BuildOpts; |
| 243 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 244 | public: |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 245 | explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG |
| 246 | Block(NULL), Succ(NULL), |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 247 | SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL), |
Zhongxing Xu | 49b4ef3 | 2010-09-16 03:28:18 +0000 | [diff] [blame] | 248 | TryTerminatedBlock(NULL), badCFG(false) {} |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 250 | // buildCFG - Used by external clients to construct the CFG. |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 251 | CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, |
Ted Kremenek | 6c52c78 | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 252 | CFG::BuildOptions BO); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 254 | private: |
| 255 | // Visitors to walk an AST and construct the CFG. |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 256 | CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc); |
| 257 | CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc); |
| 258 | CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 259 | CFGBlock *VisitBreakStmt(BreakStmt *B); |
Ted Kremenek | 7ea2136 | 2010-04-11 17:01:59 +0000 | [diff] [blame] | 260 | CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S); |
| 261 | CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T); |
| 262 | CFGBlock *VisitCXXTryStmt(CXXTryStmt *S); |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 263 | CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E, |
| 264 | AddStmtChoice asc); |
Zhongxing Xu | 81bc7d0 | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 265 | CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc); |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 266 | CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E, |
| 267 | AddStmtChoice asc); |
Zhongxing Xu | 81bc7d0 | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 268 | CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C, |
| 269 | AddStmtChoice asc); |
Zhongxing Xu | c5354a2 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 270 | CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 271 | CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 272 | CFGBlock *VisitCaseStmt(CaseStmt *C); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 273 | CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc); |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 274 | CFGBlock *VisitCompoundStmt(CompoundStmt *C); |
Ted Kremenek | 7ea2136 | 2010-04-11 17:01:59 +0000 | [diff] [blame] | 275 | CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc); |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 276 | CFGBlock *VisitContinueStmt(ContinueStmt *C); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 277 | CFGBlock *VisitDeclStmt(DeclStmt *DS); |
| 278 | CFGBlock *VisitDeclSubExpr(Decl* D); |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 279 | CFGBlock *VisitDefaultStmt(DefaultStmt *D); |
| 280 | CFGBlock *VisitDoStmt(DoStmt *D); |
| 281 | CFGBlock *VisitForStmt(ForStmt *F); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 282 | CFGBlock *VisitGotoStmt(GotoStmt* G); |
| 283 | CFGBlock *VisitIfStmt(IfStmt *I); |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 284 | CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 285 | CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I); |
| 286 | CFGBlock *VisitLabelStmt(LabelStmt *L); |
Ted Kremenek | 115c1b9 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 287 | CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 288 | CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 289 | CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 290 | CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 291 | CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| 292 | CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| 293 | CFGBlock *VisitReturnStmt(ReturnStmt* R); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 294 | CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc); |
| 295 | CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 296 | CFGBlock *VisitSwitchStmt(SwitchStmt *S); |
| 297 | CFGBlock *VisitWhileStmt(WhileStmt *W); |
Mike Stump | cd7bf23 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 299 | CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd); |
| 300 | CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 301 | CFGBlock *VisitChildren(Stmt* S); |
Mike Stump | cd7bf23 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 303 | // NYS == Not Yet Supported |
| 304 | CFGBlock* NYS() { |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 305 | badCFG = true; |
| 306 | return Block; |
| 307 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 308 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 309 | void autoCreateBlock() { if (!Block) Block = createBlock(); } |
| 310 | CFGBlock *createBlock(bool add_successor = true); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 311 | |
Zhongxing Xu | df11989 | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 312 | CFGBlock *addStmt(Stmt *S) { |
| 313 | return Visit(S, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 314 | } |
Marcin Swiderski | 82bc3fd | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 315 | CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I); |
Zhongxing Xu | 6a16a30 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 316 | void addAutomaticObjDtors(LocalScope::const_iterator B, |
| 317 | LocalScope::const_iterator E, Stmt* S); |
Marcin Swiderski | 7c625d8 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 318 | void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 319 | |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 320 | // Local scopes creation. |
| 321 | LocalScope* createOrReuseLocalScope(LocalScope* Scope); |
| 322 | |
Zhongxing Xu | 02acdfa | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 323 | void addLocalScopeForStmt(Stmt* S); |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 324 | LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL); |
| 325 | LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL); |
| 326 | |
| 327 | void addLocalScopeAndDtors(Stmt* S); |
| 328 | |
| 329 | // Interface to CFGBlock - adding CFGElements. |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 330 | void AppendStmt(CFGBlock *B, Stmt *S, |
| 331 | AddStmtChoice asc = AddStmtChoice::AlwaysAdd) { |
| 332 | B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue()); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 333 | } |
Marcin Swiderski | 82bc3fd | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 334 | void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) { |
| 335 | B->appendInitializer(I, cfg->getBumpVectorContext()); |
| 336 | } |
Marcin Swiderski | 7c625d8 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 337 | void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) { |
| 338 | B->appendBaseDtor(BS, cfg->getBumpVectorContext()); |
| 339 | } |
| 340 | void appendMemberDtor(CFGBlock *B, FieldDecl *FD) { |
| 341 | B->appendMemberDtor(FD, cfg->getBumpVectorContext()); |
| 342 | } |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 343 | |
Marcin Swiderski | 53de134 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 344 | void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I, |
| 345 | LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S); |
| 346 | void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B, |
| 347 | LocalScope::const_iterator E, Stmt* S); |
| 348 | void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk, |
| 349 | LocalScope::const_iterator B, LocalScope::const_iterator E); |
| 350 | |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 351 | void AddSuccessor(CFGBlock *B, CFGBlock *S) { |
| 352 | B->addSuccessor(S, cfg->getBumpVectorContext()); |
| 353 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | fadc9ea | 2009-07-24 06:55:42 +0000 | [diff] [blame] | 355 | /// TryResult - a class representing a variant over the values |
| 356 | /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool, |
| 357 | /// and is used by the CFGBuilder to decide if a branch condition |
| 358 | /// can be decided up front during CFG construction. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 359 | class TryResult { |
| 360 | int X; |
| 361 | public: |
| 362 | TryResult(bool b) : X(b ? 1 : 0) {} |
| 363 | TryResult() : X(-1) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 365 | bool isTrue() const { return X == 1; } |
| 366 | bool isFalse() const { return X == 0; } |
| 367 | bool isKnown() const { return X >= 0; } |
| 368 | void negate() { |
| 369 | assert(isKnown()); |
| 370 | X ^= 0x1; |
| 371 | } |
| 372 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 374 | /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1 |
| 375 | /// if we can evaluate to a known value, otherwise return -1. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 376 | TryResult TryEvaluateBool(Expr *S) { |
Ted Kremenek | 6c52c78 | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 377 | if (!BuildOpts.PruneTriviallyFalseEdges) |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 378 | return TryResult(); |
| 379 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 380 | Expr::EvalResult Result; |
Douglas Gregor | 9983cc1 | 2009-08-24 21:39:56 +0000 | [diff] [blame] | 381 | if (!S->isTypeDependent() && !S->isValueDependent() && |
| 382 | S->Evaluate(Result, *Context) && Result.Val.isInt()) |
Ted Kremenek | fadc9ea | 2009-07-24 06:55:42 +0000 | [diff] [blame] | 383 | return Result.Val.getInt().getBoolValue(); |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 384 | |
| 385 | return TryResult(); |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 386 | } |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 387 | }; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 388 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 389 | // FIXME: Add support for dependent-sized array types in C++? |
| 390 | // Does it even make sense to build a CFG for an uninstantiated template? |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 391 | static VariableArrayType* FindVA(Type* t) { |
| 392 | while (ArrayType* vt = dyn_cast<ArrayType>(t)) { |
| 393 | if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt)) |
| 394 | if (vat->getSizeExpr()) |
| 395 | return vat; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 396 | |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 397 | t = vt->getElementType().getTypePtr(); |
| 398 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 399 | |
Ted Kremenek | 610a09e | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 400 | return 0; |
| 401 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 402 | |
| 403 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an |
| 404 | /// arbitrary statement. Examples include a single expression or a function |
| 405 | /// body (compound statement). The ownership of the returned CFG is |
| 406 | /// transferred to the caller. If CFG construction fails, this method returns |
| 407 | /// NULL. |
Mike Stump | b978a44 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 408 | CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C, |
Ted Kremenek | 6c52c78 | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 409 | CFG::BuildOptions BO) { |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 410 | |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 411 | Context = C; |
Ted Kremenek | 0ba497b | 2009-10-20 23:46:25 +0000 | [diff] [blame] | 412 | assert(cfg.get()); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 413 | if (!Statement) |
| 414 | return NULL; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 6c52c78 | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 416 | BuildOpts = BO; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 417 | |
| 418 | // Create an empty block that will serve as the exit block for the CFG. Since |
| 419 | // this is the first block added to the CFG, it will be implicitly registered |
| 420 | // as the exit block. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 421 | Succ = createBlock(); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 422 | assert(Succ == &cfg->getExit()); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 423 | Block = NULL; // the EXIT block is empty. Create all other blocks lazily. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 424 | |
Marcin Swiderski | 7c625d8 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 425 | if (BuildOpts.AddImplicitDtors) |
| 426 | if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D)) |
| 427 | addImplicitDtorsForDestructor(DD); |
| 428 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 429 | // Visit the statements and create the CFG. |
Zhongxing Xu | 1b3b7cb | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 430 | CFGBlock *B = addStmt(Statement); |
| 431 | |
| 432 | if (badCFG) |
| 433 | return NULL; |
| 434 | |
Marcin Swiderski | 82bc3fd | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 435 | // For C++ constructor add initializers to CFG. |
| 436 | if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) { |
| 437 | for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(), |
| 438 | E = CD->init_rend(); I != E; ++I) { |
| 439 | B = addInitializer(*I); |
| 440 | if (badCFG) |
| 441 | return NULL; |
| 442 | } |
| 443 | } |
| 444 | |
Zhongxing Xu | 1b3b7cb | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 445 | if (B) |
| 446 | Succ = B; |
Mike Stump | b978a44 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 447 | |
Zhongxing Xu | 1b3b7cb | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 448 | // Backpatch the gotos whose label -> block mappings we didn't know when we |
| 449 | // encountered them. |
| 450 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 451 | E = BackpatchBlocks.end(); I != E; ++I ) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 452 | |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 453 | CFGBlock* B = I->Block; |
Zhongxing Xu | 1b3b7cb | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 454 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 455 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 456 | |
Zhongxing Xu | 1b3b7cb | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 457 | // If there is no target for the goto, then we are looking at an |
| 458 | // incomplete AST. Handle this by not registering a successor. |
| 459 | if (LI == LabelMap.end()) continue; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 460 | |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 461 | JumpTarget JT = LI->second; |
Marcin Swiderski | fcb72ac | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 462 | prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 463 | AddSuccessor(B, JT.Block); |
Zhongxing Xu | 1b3b7cb | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // Add successors to the Indirect Goto Dispatch block (if we have one). |
| 467 | if (CFGBlock* B = cfg->getIndirectGotoBlock()) |
| 468 | for (LabelSetTy::iterator I = AddressTakenLabels.begin(), |
| 469 | E = AddressTakenLabels.end(); I != E; ++I ) { |
| 470 | |
| 471 | // Lookup the target block. |
| 472 | LabelMapTy::iterator LI = LabelMap.find(*I); |
| 473 | |
| 474 | // If there is no target block that contains label, then we are looking |
| 475 | // at an incomplete AST. Handle this by not registering a successor. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 476 | if (LI == LabelMap.end()) continue; |
Zhongxing Xu | 1b3b7cb | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 477 | |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 478 | AddSuccessor(B, LI->second.Block); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 479 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 480 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 481 | // Create an empty entry block that has no predecessors. |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 482 | cfg->setEntry(createBlock()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 483 | |
Zhongxing Xu | 1b3b7cb | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 484 | return cfg.take(); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 485 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 487 | /// createBlock - Used to lazily create blocks that are connected |
| 488 | /// to the current (global) succcessor. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 489 | CFGBlock* CFGBuilder::createBlock(bool add_successor) { |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 490 | CFGBlock* B = cfg->createBlock(); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 491 | if (add_successor && Succ) |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 492 | AddSuccessor(B, Succ); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 493 | return B; |
| 494 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 495 | |
Marcin Swiderski | 82bc3fd | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 496 | /// addInitializer - Add C++ base or member initializer element to CFG. |
| 497 | CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) { |
| 498 | if (!BuildOpts.AddInitializers) |
| 499 | return Block; |
| 500 | |
| 501 | autoCreateBlock(); |
| 502 | appendInitializer(Block, I); |
| 503 | |
| 504 | if (Expr *Init = I->getInit()) { |
| 505 | AddStmtChoice::Kind K = AddStmtChoice::NotAlwaysAdd; |
| 506 | if (FieldDecl *FD = I->getMember()) |
| 507 | if (FD->getType()->isReferenceType()) |
| 508 | K = AddStmtChoice::AsLValueNotAlwaysAdd; |
| 509 | |
| 510 | return Visit(Init, AddStmtChoice(K)); |
| 511 | } |
| 512 | |
| 513 | return Block; |
| 514 | } |
| 515 | |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 516 | /// addAutomaticObjDtors - Add to current block automatic objects destructors |
| 517 | /// for objects in range of local scope positions. Use S as trigger statement |
| 518 | /// for destructors. |
Zhongxing Xu | 6a16a30 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 519 | void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B, |
| 520 | LocalScope::const_iterator E, Stmt* S) { |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 521 | if (!BuildOpts.AddImplicitDtors) |
Zhongxing Xu | 6a16a30 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 522 | return; |
| 523 | |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 524 | if (B == E) |
Zhongxing Xu | 6a16a30 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 525 | return; |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 526 | |
| 527 | autoCreateBlock(); |
| 528 | appendAutomaticObjDtors(Block, B, E, S); |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Marcin Swiderski | 7c625d8 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 531 | /// addImplicitDtorsForDestructor - Add implicit destructors generated for |
| 532 | /// base and member objects in destructor. |
| 533 | void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) { |
| 534 | assert (BuildOpts.AddImplicitDtors |
| 535 | && "Can be called only when dtors should be added"); |
| 536 | const CXXRecordDecl *RD = DD->getParent(); |
| 537 | |
| 538 | // At the end destroy virtual base objects. |
| 539 | for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(), |
| 540 | VE = RD->vbases_end(); VI != VE; ++VI) { |
| 541 | const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl(); |
| 542 | if (!CD->hasTrivialDestructor()) { |
| 543 | autoCreateBlock(); |
| 544 | appendBaseDtor(Block, VI); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // Before virtual bases destroy direct base objects. |
| 549 | for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), |
| 550 | BE = RD->bases_end(); BI != BE; ++BI) { |
| 551 | if (!BI->isVirtual()) { |
| 552 | const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl(); |
| 553 | if (!CD->hasTrivialDestructor()) { |
| 554 | autoCreateBlock(); |
| 555 | appendBaseDtor(Block, BI); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // First destroy member objects. |
| 561 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 562 | FE = RD->field_end(); FI != FE; ++FI) { |
Marcin Swiderski | 8c5e5d6 | 2010-10-25 07:05:54 +0000 | [diff] [blame] | 563 | // Check for constant size array. Set type to array element type. |
| 564 | QualType QT = FI->getType(); |
| 565 | if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) { |
| 566 | if (AT->getSize() == 0) |
| 567 | continue; |
| 568 | QT = AT->getElementType(); |
| 569 | } |
| 570 | |
| 571 | if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl()) |
Marcin Swiderski | 7c625d8 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 572 | if (!CD->hasTrivialDestructor()) { |
| 573 | autoCreateBlock(); |
| 574 | appendMemberDtor(Block, *FI); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 579 | /// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either |
| 580 | /// way return valid LocalScope object. |
| 581 | LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) { |
| 582 | if (!Scope) { |
| 583 | Scope = cfg->getAllocator().Allocate<LocalScope>(); |
| 584 | new (Scope) LocalScope(ScopePos); |
| 585 | } |
| 586 | return Scope; |
| 587 | } |
| 588 | |
| 589 | /// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement |
Zhongxing Xu | 02acdfa | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 590 | /// that should create implicit scope (e.g. if/else substatements). |
| 591 | void CFGBuilder::addLocalScopeForStmt(Stmt* S) { |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 592 | if (!BuildOpts.AddImplicitDtors) |
Zhongxing Xu | 02acdfa | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 593 | return; |
| 594 | |
| 595 | LocalScope *Scope = 0; |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 596 | |
| 597 | // For compound statement we will be creating explicit scope. |
| 598 | if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) { |
| 599 | for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end() |
| 600 | ; BI != BE; ++BI) { |
| 601 | Stmt* SI = *BI; |
| 602 | if (LabelStmt* LS = dyn_cast<LabelStmt>(SI)) |
| 603 | SI = LS->getSubStmt(); |
| 604 | if (DeclStmt* DS = dyn_cast<DeclStmt>(SI)) |
| 605 | Scope = addLocalScopeForDeclStmt(DS, Scope); |
| 606 | } |
Zhongxing Xu | 02acdfa | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 607 | return; |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // For any other statement scope will be implicit and as such will be |
| 611 | // interesting only for DeclStmt. |
| 612 | if (LabelStmt* LS = dyn_cast<LabelStmt>(S)) |
| 613 | S = LS->getSubStmt(); |
| 614 | if (DeclStmt* DS = dyn_cast<DeclStmt>(S)) |
Zhongxing Xu | b6edff5 | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 615 | addLocalScopeForDeclStmt(DS); |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will |
| 619 | /// reuse Scope if not NULL. |
| 620 | LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS, |
Zhongxing Xu | b6edff5 | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 621 | LocalScope* Scope) { |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 622 | if (!BuildOpts.AddImplicitDtors) |
| 623 | return Scope; |
| 624 | |
| 625 | for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end() |
| 626 | ; DI != DE; ++DI) { |
| 627 | if (VarDecl* VD = dyn_cast<VarDecl>(*DI)) |
| 628 | Scope = addLocalScopeForVarDecl(VD, Scope); |
| 629 | } |
| 630 | return Scope; |
| 631 | } |
| 632 | |
| 633 | /// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will |
| 634 | /// create add scope for automatic objects and temporary objects bound to |
| 635 | /// const reference. Will reuse Scope if not NULL. |
| 636 | LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD, |
Zhongxing Xu | b6edff5 | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 637 | LocalScope* Scope) { |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 638 | if (!BuildOpts.AddImplicitDtors) |
| 639 | return Scope; |
| 640 | |
| 641 | // Check if variable is local. |
| 642 | switch (VD->getStorageClass()) { |
| 643 | case SC_None: |
| 644 | case SC_Auto: |
| 645 | case SC_Register: |
| 646 | break; |
| 647 | default: return Scope; |
| 648 | } |
| 649 | |
| 650 | // Check for const references bound to temporary. Set type to pointee. |
| 651 | QualType QT = VD->getType(); |
| 652 | if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) { |
| 653 | QT = RT->getPointeeType(); |
| 654 | if (!QT.isConstQualified()) |
| 655 | return Scope; |
| 656 | if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue()) |
| 657 | return Scope; |
| 658 | } |
| 659 | |
Marcin Swiderski | b1c5287 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 660 | // Check for constant size array. Set type to array element type. |
| 661 | if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) { |
| 662 | if (AT->getSize() == 0) |
| 663 | return Scope; |
| 664 | QT = AT->getElementType(); |
| 665 | } |
Zhongxing Xu | 4e493e0 | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 666 | |
Marcin Swiderski | b1c5287 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 667 | // Check if type is a C++ class with non-trivial destructor. |
Zhongxing Xu | 4e493e0 | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 668 | if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl()) |
| 669 | if (!CD->hasTrivialDestructor()) { |
| 670 | // Add the variable to scope |
| 671 | Scope = createOrReuseLocalScope(Scope); |
| 672 | Scope->addVar(VD); |
| 673 | ScopePos = Scope->begin(); |
| 674 | } |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 675 | return Scope; |
| 676 | } |
| 677 | |
| 678 | /// addLocalScopeAndDtors - For given statement add local scope for it and |
| 679 | /// add destructors that will cleanup the scope. Will reuse Scope if not NULL. |
| 680 | void CFGBuilder::addLocalScopeAndDtors(Stmt* S) { |
| 681 | if (!BuildOpts.AddImplicitDtors) |
| 682 | return; |
| 683 | |
| 684 | LocalScope::const_iterator scopeBeginPos = ScopePos; |
Zhongxing Xu | 02acdfa | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 685 | addLocalScopeForStmt(S); |
Marcin Swiderski | 239a7c4 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 686 | addAutomaticObjDtors(ScopePos, scopeBeginPos, S); |
| 687 | } |
| 688 | |
Marcin Swiderski | 53de134 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 689 | /// insertAutomaticObjDtors - Insert destructor CFGElements for variables with |
| 690 | /// automatic storage duration to CFGBlock's elements vector. Insertion will be |
| 691 | /// performed in place specified with iterator. |
| 692 | void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I, |
| 693 | LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) { |
| 694 | BumpVectorContext& C = cfg->getBumpVectorContext(); |
| 695 | I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C); |
| 696 | while (B != E) |
| 697 | I = Blk->insertAutomaticObjDtor(I, *B++, S); |
| 698 | } |
| 699 | |
| 700 | /// appendAutomaticObjDtors - Append destructor CFGElements for variables with |
| 701 | /// automatic storage duration to CFGBlock's elements vector. Elements will be |
| 702 | /// appended to physical end of the vector which happens to be logical |
| 703 | /// beginning. |
| 704 | void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk, |
| 705 | LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) { |
| 706 | insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S); |
| 707 | } |
| 708 | |
| 709 | /// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for |
| 710 | /// variables with automatic storage duration to CFGBlock's elements vector. |
| 711 | /// Elements will be prepended to physical beginning of the vector which |
| 712 | /// happens to be logical end. Use blocks terminator as statement that specifies |
| 713 | /// destructors call site. |
| 714 | void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk, |
| 715 | LocalScope::const_iterator B, LocalScope::const_iterator E) { |
| 716 | insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator()); |
| 717 | } |
| 718 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 719 | /// Visit - Walk the subtree of a statement and add extra |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 720 | /// blocks for ternary operators, &&, and ||. We also process "," and |
| 721 | /// DeclStmts (which may contain nested control-flow). |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 722 | CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) { |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 723 | tryAgain: |
Ted Kremenek | f42e337 | 2010-04-30 22:25:53 +0000 | [diff] [blame] | 724 | if (!S) { |
| 725 | badCFG = true; |
| 726 | return 0; |
| 727 | } |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 728 | switch (S->getStmtClass()) { |
| 729 | default: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 730 | return VisitStmt(S, asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 731 | |
| 732 | case Stmt::AddrLabelExprClass: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 733 | return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 734 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 735 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 736 | return VisitBinaryOperator(cast<BinaryOperator>(S), asc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 738 | case Stmt::BlockExprClass: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 739 | return VisitBlockExpr(cast<BlockExpr>(S), asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 740 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 741 | case Stmt::BreakStmtClass: |
| 742 | return VisitBreakStmt(cast<BreakStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 744 | case Stmt::CallExprClass: |
Ted Kremenek | a427f1d | 2010-08-31 18:47:34 +0000 | [diff] [blame] | 745 | case Stmt::CXXOperatorCallExprClass: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 746 | return VisitCallExpr(cast<CallExpr>(S), asc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 748 | case Stmt::CaseStmtClass: |
| 749 | return VisitCaseStmt(cast<CaseStmt>(S)); |
| 750 | |
| 751 | case Stmt::ChooseExprClass: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 752 | return VisitChooseExpr(cast<ChooseExpr>(S), asc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 754 | case Stmt::CompoundStmtClass: |
| 755 | return VisitCompoundStmt(cast<CompoundStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 756 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 757 | case Stmt::ConditionalOperatorClass: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 758 | return VisitConditionalOperator(cast<ConditionalOperator>(S), asc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 760 | case Stmt::ContinueStmtClass: |
| 761 | return VisitContinueStmt(cast<ContinueStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
Ted Kremenek | 021c8af | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 763 | case Stmt::CXXCatchStmtClass: |
| 764 | return VisitCXXCatchStmt(cast<CXXCatchStmt>(S)); |
| 765 | |
Ted Kremenek | 47e331e | 2010-08-28 00:19:02 +0000 | [diff] [blame] | 766 | case Stmt::CXXExprWithTemporariesClass: { |
| 767 | // FIXME: Handle temporaries. For now, just visit the subexpression |
| 768 | // so we don't artificially create extra blocks. |
Ted Kremenek | a427f1d | 2010-08-31 18:47:34 +0000 | [diff] [blame] | 769 | return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc); |
Ted Kremenek | 47e331e | 2010-08-28 00:19:02 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 772 | case Stmt::CXXBindTemporaryExprClass: |
| 773 | return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc); |
| 774 | |
Zhongxing Xu | 81bc7d0 | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 775 | case Stmt::CXXConstructExprClass: |
| 776 | return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc); |
| 777 | |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 778 | case Stmt::CXXFunctionalCastExprClass: |
| 779 | return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc); |
| 780 | |
Zhongxing Xu | 81bc7d0 | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 781 | case Stmt::CXXTemporaryObjectExprClass: |
| 782 | return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc); |
| 783 | |
Zhongxing Xu | c5354a2 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 784 | case Stmt::CXXMemberCallExprClass: |
| 785 | return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc); |
| 786 | |
Ted Kremenek | 021c8af | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 787 | case Stmt::CXXThrowExprClass: |
| 788 | return VisitCXXThrowExpr(cast<CXXThrowExpr>(S)); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 789 | |
Ted Kremenek | 021c8af | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 790 | case Stmt::CXXTryStmtClass: |
| 791 | return VisitCXXTryStmt(cast<CXXTryStmt>(S)); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 792 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 793 | case Stmt::DeclStmtClass: |
| 794 | return VisitDeclStmt(cast<DeclStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 796 | case Stmt::DefaultStmtClass: |
| 797 | return VisitDefaultStmt(cast<DefaultStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 798 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 799 | case Stmt::DoStmtClass: |
| 800 | return VisitDoStmt(cast<DoStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 802 | case Stmt::ForStmtClass: |
| 803 | return VisitForStmt(cast<ForStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 805 | case Stmt::GotoStmtClass: |
| 806 | return VisitGotoStmt(cast<GotoStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 808 | case Stmt::IfStmtClass: |
| 809 | return VisitIfStmt(cast<IfStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 811 | case Stmt::ImplicitCastExprClass: |
| 812 | return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc); |
| 813 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 814 | case Stmt::IndirectGotoStmtClass: |
| 815 | return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 817 | case Stmt::LabelStmtClass: |
| 818 | return VisitLabelStmt(cast<LabelStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | |
Ted Kremenek | 115c1b9 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 820 | case Stmt::MemberExprClass: |
| 821 | return VisitMemberExpr(cast<MemberExpr>(S), asc); |
| 822 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 823 | case Stmt::ObjCAtCatchStmtClass: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S)); |
| 825 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 826 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 827 | return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 829 | case Stmt::ObjCAtThrowStmtClass: |
| 830 | return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 832 | case Stmt::ObjCAtTryStmtClass: |
| 833 | return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 835 | case Stmt::ObjCForCollectionStmtClass: |
| 836 | return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 838 | case Stmt::ParenExprClass: |
| 839 | S = cast<ParenExpr>(S)->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | goto tryAgain; |
| 841 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 842 | case Stmt::NullStmtClass: |
| 843 | return Block; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 845 | case Stmt::ReturnStmtClass: |
| 846 | return VisitReturnStmt(cast<ReturnStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 848 | case Stmt::SizeOfAlignOfExprClass: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 849 | return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 851 | case Stmt::StmtExprClass: |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 852 | return VisitStmtExpr(cast<StmtExpr>(S), asc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 854 | case Stmt::SwitchStmtClass: |
| 855 | return VisitSwitchStmt(cast<SwitchStmt>(S)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 857 | case Stmt::WhileStmtClass: |
| 858 | return VisitWhileStmt(cast<WhileStmt>(S)); |
| 859 | } |
| 860 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 862 | CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) { |
| 863 | if (asc.alwaysAdd()) { |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 864 | autoCreateBlock(); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 865 | AppendStmt(Block, S, asc); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 868 | return VisitChildren(S); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 869 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 870 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 871 | /// VisitChildren - Visit the children of a Stmt. |
| 872 | CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) { |
| 873 | CFGBlock *B = Block; |
Mike Stump | 54cc43f | 2009-02-26 08:00:25 +0000 | [diff] [blame] | 874 | for (Stmt::child_iterator I = Terminator->child_begin(), |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 875 | E = Terminator->child_end(); I != E; ++I) { |
| 876 | if (*I) B = Visit(*I); |
| 877 | } |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 878 | return B; |
| 879 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 881 | CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, |
| 882 | AddStmtChoice asc) { |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 883 | AddressTakenLabels.insert(A->getLabel()); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 884 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 885 | if (asc.alwaysAdd()) { |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 886 | autoCreateBlock(); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 887 | AppendStmt(Block, A, asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 888 | } |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 889 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 890 | return Block; |
| 891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 893 | CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, |
| 894 | AddStmtChoice asc) { |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 895 | if (B->isLogicalOp()) { // && or || |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 896 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 897 | AppendStmt(ConfluenceBlock, B, asc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 899 | if (badCFG) |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 900 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 902 | // create the block evaluating the LHS |
| 903 | CFGBlock* LHSBlock = createBlock(false); |
| 904 | LHSBlock->setTerminator(B); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 906 | // create the block evaluating the RHS |
| 907 | Succ = ConfluenceBlock; |
| 908 | Block = NULL; |
| 909 | CFGBlock* RHSBlock = addStmt(B->getRHS()); |
Ted Kremenek | 862b24f | 2010-04-29 01:10:26 +0000 | [diff] [blame] | 910 | |
| 911 | if (RHSBlock) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 912 | if (badCFG) |
Ted Kremenek | 862b24f | 2010-04-29 01:10:26 +0000 | [diff] [blame] | 913 | return 0; |
| 914 | } |
| 915 | else { |
| 916 | // Create an empty block for cases where the RHS doesn't require |
| 917 | // any explicit statements in the CFG. |
| 918 | RHSBlock = createBlock(); |
| 919 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 921 | // See if this is a known constant. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 922 | TryResult KnownVal = TryEvaluateBool(B->getLHS()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 923 | if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr)) |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 924 | KnownVal.negate(); |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 925 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 926 | // Now link the LHSBlock with RHSBlock. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 927 | if (B->getOpcode() == BO_LOr) { |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 928 | AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock); |
| 929 | AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 931 | assert(B->getOpcode() == BO_LAnd); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 932 | AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock); |
| 933 | AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 934 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 936 | // Generate the blocks for evaluating the LHS. |
| 937 | Block = LHSBlock; |
| 938 | return addStmt(B->getLHS()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 940 | else if (B->getOpcode() == BO_Comma) { // , |
Ted Kremenek | 6dc534e | 2009-07-17 22:57:50 +0000 | [diff] [blame] | 941 | autoCreateBlock(); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 942 | AppendStmt(Block, B, asc); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 943 | addStmt(B->getRHS()); |
| 944 | return addStmt(B->getLHS()); |
| 945 | } |
Zhongxing Xu | fc61d94 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 946 | else if (B->isAssignmentOp()) { |
| 947 | if (asc.alwaysAdd()) { |
| 948 | autoCreateBlock(); |
| 949 | AppendStmt(Block, B, asc); |
| 950 | } |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 951 | |
Marcin Swiderski | e166719 | 2010-10-24 08:21:40 +0000 | [diff] [blame] | 952 | Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd); |
| 953 | return Visit(B->getRHS()); |
Zhongxing Xu | fc61d94 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Marcin Swiderski | e166719 | 2010-10-24 08:21:40 +0000 | [diff] [blame] | 956 | if (asc.alwaysAdd()) { |
| 957 | autoCreateBlock(); |
| 958 | AppendStmt(Block, B, asc); |
| 959 | } |
| 960 | |
Zhongxing Xu | a1898dd | 2010-10-27 03:23:10 +0000 | [diff] [blame] | 961 | CFGBlock *RBlock = Visit(B->getRHS()); |
| 962 | CFGBlock *LBlock = Visit(B->getLHS()); |
| 963 | // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr |
| 964 | // containing a DoStmt, and the LHS doesn't create a new block, then we should |
| 965 | // return RBlock. Otherwise we'll incorrectly return NULL. |
| 966 | return (LBlock ? LBlock : RBlock); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 969 | CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) { |
| 970 | if (asc.alwaysAdd()) { |
Ted Kremenek | 721903e | 2009-11-25 01:34:30 +0000 | [diff] [blame] | 971 | autoCreateBlock(); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 972 | AppendStmt(Block, E, asc); |
Ted Kremenek | 721903e | 2009-11-25 01:34:30 +0000 | [diff] [blame] | 973 | } |
| 974 | return Block; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 977 | CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) { |
| 978 | // "break" is a control-flow statement. Thus we stop processing the current |
| 979 | // block. |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 980 | if (badCFG) |
| 981 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 983 | // Now create a new block that ends with the break statement. |
| 984 | Block = createBlock(false); |
| 985 | Block->setTerminator(B); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 986 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 987 | // If there is no target for the break, then we are looking at an incomplete |
| 988 | // AST. This means that the CFG cannot be constructed. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 989 | if (BreakJumpTarget.Block) { |
Marcin Swiderski | fcb72ac | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 990 | addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 991 | AddSuccessor(Block, BreakJumpTarget.Block); |
| 992 | } else |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 993 | badCFG = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
| 995 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 996 | return Block; |
| 997 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 999 | static bool CanThrow(Expr *E) { |
| 1000 | QualType Ty = E->getType(); |
| 1001 | if (Ty->isFunctionPointerType()) |
| 1002 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
| 1003 | else if (Ty->isBlockPointerType()) |
| 1004 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1005 | |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1006 | const FunctionType *FT = Ty->getAs<FunctionType>(); |
| 1007 | if (FT) { |
| 1008 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) |
| 1009 | if (Proto->hasEmptyExceptionSpec()) |
| 1010 | return false; |
| 1011 | } |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1015 | CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1016 | // If this is a call to a no-return function, this stops the block here. |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1017 | bool NoReturn = false; |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1018 | if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) { |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1019 | NoReturn = true; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1020 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1021 | |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1022 | bool AddEHEdge = false; |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1023 | |
| 1024 | // Languages without exceptions are assumed to not throw. |
| 1025 | if (Context->getLangOptions().Exceptions) { |
Ted Kremenek | 6c52c78 | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 1026 | if (BuildOpts.AddEHEdges) |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1027 | AddEHEdge = true; |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | if (FunctionDecl *FD = C->getDirectCallee()) { |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1031 | if (FD->hasAttr<NoReturnAttr>()) |
| 1032 | NoReturn = true; |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1033 | if (FD->hasAttr<NoThrowAttr>()) |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1034 | AddEHEdge = false; |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1035 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1036 | |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1037 | if (!CanThrow(C->getCallee())) |
| 1038 | AddEHEdge = false; |
| 1039 | |
Zhongxing Xu | fc61d94 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 1040 | if (!NoReturn && !AddEHEdge) { |
| 1041 | if (asc.asLValue()) |
| 1042 | return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue); |
| 1043 | else |
| 1044 | return VisitStmt(C, AddStmtChoice::AlwaysAdd); |
| 1045 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1047 | if (Block) { |
| 1048 | Succ = Block; |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1049 | if (badCFG) |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1053 | Block = createBlock(!NoReturn); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1054 | AppendStmt(Block, C, asc); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1055 | |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1056 | if (NoReturn) { |
| 1057 | // Wire this to the exit block directly. |
| 1058 | AddSuccessor(Block, &cfg->getExit()); |
| 1059 | } |
Mike Stump | 4c45aa1 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1060 | if (AddEHEdge) { |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1061 | // Add exceptional edges. |
| 1062 | if (TryTerminatedBlock) |
| 1063 | AddSuccessor(Block, TryTerminatedBlock); |
| 1064 | else |
| 1065 | AddSuccessor(Block, &cfg->getExit()); |
| 1066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1068 | return VisitChildren(C); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1069 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1070 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1071 | CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C, |
| 1072 | AddStmtChoice asc) { |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1073 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1074 | AppendStmt(ConfluenceBlock, C, asc); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1075 | if (badCFG) |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1076 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Ted Kremenek | 115c1b9 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 1078 | asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue |
| 1079 | : AddStmtChoice::AlwaysAdd; |
| 1080 | |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1081 | Succ = ConfluenceBlock; |
| 1082 | Block = NULL; |
Zhongxing Xu | df11989 | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 1083 | CFGBlock* LHSBlock = Visit(C->getLHS(), asc); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1084 | if (badCFG) |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1085 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1087 | Succ = ConfluenceBlock; |
| 1088 | Block = NULL; |
Zhongxing Xu | df11989 | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 1089 | CFGBlock* RHSBlock = Visit(C->getRHS(), asc); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1090 | if (badCFG) |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1091 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1093 | Block = createBlock(false); |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1094 | // See if this is a known constant. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1095 | const TryResult& KnownVal = TryEvaluateBool(C->getCond()); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1096 | AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock); |
| 1097 | AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock); |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1098 | Block->setTerminator(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | return addStmt(C->getCond()); |
Ted Kremenek | 3fc8ef5 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
| 1102 | |
| 1103 | CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { |
Marcin Swiderski | fcb72ac | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 1104 | addLocalScopeAndDtors(C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | CFGBlock* LastBlock = Block; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1106 | |
| 1107 | for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); |
| 1108 | I != E; ++I ) { |
Ted Kremenek | 334c195 | 2010-08-17 21:00:06 +0000 | [diff] [blame] | 1109 | // If we hit a segment of code just containing ';' (NullStmts), we can |
| 1110 | // get a null block back. In such cases, just use the LastBlock |
| 1111 | if (CFGBlock *newBlock = addStmt(*I)) |
| 1112 | LastBlock = newBlock; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Ted Kremenek | e8d6d2b | 2009-08-27 23:16:26 +0000 | [diff] [blame] | 1114 | if (badCFG) |
| 1115 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | } |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1117 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1118 | return LastBlock; |
| 1119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1121 | CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C, |
| 1122 | AddStmtChoice asc) { |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1123 | // Create the confluence block that will "merge" the results of the ternary |
| 1124 | // expression. |
| 1125 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1126 | AppendStmt(ConfluenceBlock, C, asc); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1127 | if (badCFG) |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1128 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Ted Kremenek | 115c1b9 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 1130 | asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue |
| 1131 | : AddStmtChoice::AlwaysAdd; |
| 1132 | |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1133 | // Create a block for the LHS expression if there is an LHS expression. A |
| 1134 | // GCC extension allows LHS to be NULL, causing the condition to be the |
| 1135 | // value that is returned instead. |
| 1136 | // e.g: x ?: y is shorthand for: x ? x : y; |
| 1137 | Succ = ConfluenceBlock; |
| 1138 | Block = NULL; |
| 1139 | CFGBlock* LHSBlock = NULL; |
| 1140 | if (C->getLHS()) { |
Zhongxing Xu | df11989 | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 1141 | LHSBlock = Visit(C->getLHS(), asc); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1142 | if (badCFG) |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1143 | return 0; |
| 1144 | Block = NULL; |
| 1145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1147 | // Create the block for the RHS expression. |
| 1148 | Succ = ConfluenceBlock; |
Zhongxing Xu | df11989 | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 1149 | CFGBlock* RHSBlock = Visit(C->getRHS(), asc); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1150 | if (badCFG) |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1151 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1153 | // Create the block that will contain the condition. |
| 1154 | Block = createBlock(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1156 | // See if this is a known constant. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1157 | const TryResult& KnownVal = TryEvaluateBool(C->getCond()); |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 1158 | if (LHSBlock) { |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1159 | AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock); |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 1160 | } else { |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1161 | if (KnownVal.isFalse()) { |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 1162 | // If we know the condition is false, add NULL as the successor for |
| 1163 | // the block containing the condition. In this case, the confluence |
| 1164 | // block will have just one predecessor. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1165 | AddSuccessor(Block, 0); |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1166 | assert(ConfluenceBlock->pred_size() == 1); |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 1167 | } else { |
| 1168 | // If we have no LHS expression, add the ConfluenceBlock as a direct |
| 1169 | // successor for the block containing the condition. Moreover, we need to |
| 1170 | // reverse the order of the predecessors in the ConfluenceBlock because |
| 1171 | // the RHSBlock will have been added to the succcessors already, and we |
| 1172 | // want the first predecessor to the the block containing the expression |
| 1173 | // for the case when the ternary expression evaluates to true. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1174 | AddSuccessor(Block, ConfluenceBlock); |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1175 | assert(ConfluenceBlock->pred_size() == 2); |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 1176 | std::reverse(ConfluenceBlock->pred_begin(), |
| 1177 | ConfluenceBlock->pred_end()); |
| 1178 | } |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1181 | AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock); |
Ted Kremenek | f34bb2e | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1182 | Block->setTerminator(C); |
| 1183 | return addStmt(C->getCond()); |
| 1184 | } |
| 1185 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1186 | CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) { |
| 1187 | autoCreateBlock(); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1188 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1189 | if (DS->isSingleDecl()) { |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1190 | AppendStmt(Block, DS); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1191 | return VisitDeclSubExpr(DS->getSingleDecl()); |
Ted Kremenek | d34066c | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 1192 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1194 | CFGBlock *B = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1196 | // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy. |
| 1197 | typedef llvm::SmallVector<Decl*,10> BufTy; |
| 1198 | BufTy Buf(DS->decl_begin(), DS->decl_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1200 | for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) { |
| 1201 | // Get the alignment of the new DeclStmt, padding out to >=8 bytes. |
| 1202 | unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8 |
| 1203 | ? 8 : llvm::AlignOf<DeclStmt>::Alignment; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1205 | // Allocate the DeclStmt using the BumpPtrAllocator. It will get |
| 1206 | // automatically freed with the CFG. |
| 1207 | DeclGroupRef DG(*I); |
| 1208 | Decl *D = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1210 | DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1212 | // Append the fake DeclStmt to block. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1213 | AppendStmt(Block, DSNew); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1214 | B = VisitDeclSubExpr(D); |
| 1215 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
| 1217 | return B; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1220 | /// VisitDeclSubExpr - Utility method to add block-level expressions for |
| 1221 | /// initializers in Decls. |
| 1222 | CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) { |
| 1223 | assert(Block); |
Ted Kremenek | d34066c | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1225 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1227 | if (!VD) |
| 1228 | return Block; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1230 | Expr *Init = VD->getInit(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1232 | if (Init) { |
Ted Kremenek | 5ba290a | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 1233 | AddStmtChoice::Kind k = |
| 1234 | VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd |
| 1235 | : AddStmtChoice::NotAlwaysAdd; |
| 1236 | Visit(Init, AddStmtChoice(k)); |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1239 | // If the type of VD is a VLA, then we must process its size expressions. |
| 1240 | for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0; |
| 1241 | VA = FindVA(VA->getElementType().getTypePtr())) |
| 1242 | Block = addStmt(VA->getSizeExpr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | |
Marcin Swiderski | fcb72ac | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 1244 | // Remove variable from local scope. |
| 1245 | if (ScopePos && VD == *ScopePos) |
| 1246 | ++ScopePos; |
| 1247 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1248 | return Block; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1252 | // We may see an if statement in the middle of a basic block, or it may be the |
| 1253 | // first statement we are processing. In either case, we create a new basic |
| 1254 | // block. First, we create the blocks for the then...else statements, and |
| 1255 | // then we create the block containing the if statement. If we were in the |
Ted Kremenek | 6c24972 | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 1256 | // middle of a block, we stop processing that block. That block is then the |
| 1257 | // implicit successor for the "then" and "else" clauses. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1258 | |
Marcin Swiderski | 04e046c | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 1259 | // Save local scope position because in case of condition variable ScopePos |
| 1260 | // won't be restored when traversing AST. |
| 1261 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 1262 | |
| 1263 | // Create local scope for possible condition variable. |
| 1264 | // Store scope position. Add implicit destructor. |
| 1265 | if (VarDecl* VD = I->getConditionVariable()) { |
| 1266 | LocalScope::const_iterator BeginScopePos = ScopePos; |
| 1267 | addLocalScopeForVarDecl(VD); |
| 1268 | addAutomaticObjDtors(ScopePos, BeginScopePos, I); |
| 1269 | } |
| 1270 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1271 | // The block we were proccessing is now finished. Make it the successor |
| 1272 | // block. |
| 1273 | if (Block) { |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1274 | Succ = Block; |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1275 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1276 | return 0; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1277 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1278 | |
Ted Kremenek | b6f1d78 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 1279 | // Process the false branch. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1280 | CFGBlock* ElseBlock = Succ; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1281 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1282 | if (Stmt* Else = I->getElse()) { |
| 1283 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1284 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1285 | // NULL out Block so that the recursive call to Visit will |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1286 | // create a new basic block. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1287 | Block = NULL; |
Marcin Swiderski | 04e046c | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 1288 | |
| 1289 | // If branch is not a compound statement create implicit scope |
| 1290 | // and add destructors. |
| 1291 | if (!isa<CompoundStmt>(Else)) |
| 1292 | addLocalScopeAndDtors(Else); |
| 1293 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1294 | ElseBlock = addStmt(Else); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1295 | |
Ted Kremenek | b6f7b72 | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 1296 | if (!ElseBlock) // Can occur when the Else body has all NullStmts. |
| 1297 | ElseBlock = sv.get(); |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1298 | else if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1299 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1300 | return 0; |
| 1301 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1302 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1303 | |
Ted Kremenek | b6f1d78 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 1304 | // Process the true branch. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1305 | CFGBlock* ThenBlock; |
| 1306 | { |
| 1307 | Stmt* Then = I->getThen(); |
Ted Kremenek | 6db0ad3 | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 1308 | assert(Then); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1309 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1310 | Block = NULL; |
Marcin Swiderski | 04e046c | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 1311 | |
| 1312 | // If branch is not a compound statement create implicit scope |
| 1313 | // and add destructors. |
| 1314 | if (!isa<CompoundStmt>(Then)) |
| 1315 | addLocalScopeAndDtors(Then); |
| 1316 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1317 | ThenBlock = addStmt(Then); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1318 | |
Ted Kremenek | dbdf794 | 2009-04-01 03:52:47 +0000 | [diff] [blame] | 1319 | if (!ThenBlock) { |
| 1320 | // We can reach here if the "then" body has all NullStmts. |
| 1321 | // Create an empty block so we can distinguish between true and false |
| 1322 | // branches in path-sensitive analyses. |
| 1323 | ThenBlock = createBlock(false); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1324 | AddSuccessor(ThenBlock, sv.get()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1325 | } else if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1326 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1327 | return 0; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1328 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1331 | // Now create a new block containing the if statement. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1332 | Block = createBlock(false); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1333 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1334 | // Set the terminator of the new block to the If statement. |
| 1335 | Block->setTerminator(I); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1336 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1337 | // See if this is a known constant. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1338 | const TryResult &KnownVal = TryEvaluateBool(I->getCond()); |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1339 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1340 | // Now add the successors. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1341 | AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock); |
| 1342 | AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1343 | |
| 1344 | // Add the condition as the last statement in the new block. This may create |
| 1345 | // new blocks as the condition may contain control-flow. Any newly created |
| 1346 | // blocks will be pointed to be "Block". |
Ted Kremenek | 61dfbec | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 1347 | Block = addStmt(I->getCond()); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1348 | |
Ted Kremenek | 61dfbec | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 1349 | // Finally, if the IfStmt contains a condition variable, add both the IfStmt |
| 1350 | // and the condition variable initialization to the CFG. |
| 1351 | if (VarDecl *VD = I->getConditionVariable()) { |
| 1352 | if (Expr *Init = VD->getInit()) { |
| 1353 | autoCreateBlock(); |
| 1354 | AppendStmt(Block, I, AddStmtChoice::AlwaysAdd); |
| 1355 | addStmt(Init); |
| 1356 | } |
| 1357 | } |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1358 | |
Ted Kremenek | 61dfbec | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 1359 | return Block; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1360 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1361 | |
| 1362 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1363 | CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { |
Ted Kremenek | 6c24972 | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 1364 | // If we were in the middle of a block we stop processing that block. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1365 | // |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1366 | // NOTE: If a "return" appears in the middle of a block, this means that the |
| 1367 | // code afterwards is DEAD (unreachable). We still keep a basic block |
| 1368 | // for that code; a simple "mark-and-sweep" from the entry block will be |
| 1369 | // able to report such dead blocks. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1370 | |
| 1371 | // Create the new block. |
| 1372 | Block = createBlock(false); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1373 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1374 | // The Exit block is the only successor. |
Marcin Swiderski | fcb72ac | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 1375 | addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1376 | AddSuccessor(Block, &cfg->getExit()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1377 | |
| 1378 | // Add the return statement to the block. This may create new blocks if R |
| 1379 | // contains control-flow (short-circuit operations). |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1380 | return VisitStmt(R, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { |
| 1384 | // Get the block of the labeled statement. Add it to our map. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1385 | addStmt(L->getSubStmt()); |
Ted Kremenek | 2677ea8 | 2008-03-15 07:45:02 +0000 | [diff] [blame] | 1386 | CFGBlock* LabelBlock = Block; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1387 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1388 | if (!LabelBlock) // This can happen when the body is empty, i.e. |
| 1389 | LabelBlock = createBlock(); // scopes that only contains NullStmts. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1390 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1391 | assert(LabelMap.find(L) == LabelMap.end() && "label already in map"); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1392 | LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1393 | |
| 1394 | // Labels partition blocks, so this is the end of the basic block we were |
| 1395 | // processing (L is the block's label). Because this is label (and we have |
| 1396 | // already processed the substatement) there is no extra control-flow to worry |
| 1397 | // about. |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1398 | LabelBlock->setLabel(L); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1399 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1400 | return 0; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1401 | |
| 1402 | // We set Block to NULL to allow lazy creation of a new block (if necessary); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1403 | Block = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1404 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1405 | // This block is now the implicit successor of other blocks. |
| 1406 | Succ = LabelBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1407 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1408 | return LabelBlock; |
| 1409 | } |
| 1410 | |
| 1411 | CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1412 | // Goto is a control-flow statement. Thus we stop processing the current |
| 1413 | // block and create a new one. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1414 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1415 | Block = createBlock(false); |
| 1416 | Block->setTerminator(G); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1417 | |
| 1418 | // If we already know the mapping to the label block add the successor now. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1419 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1420 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1421 | if (I == LabelMap.end()) |
| 1422 | // We will need to backpatch this block later. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1423 | BackpatchBlocks.push_back(JumpSource(Block, ScopePos)); |
| 1424 | else { |
| 1425 | JumpTarget JT = I->second; |
Marcin Swiderski | fcb72ac | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 1426 | addAutomaticObjDtors(ScopePos, JT.ScopePos, G); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1427 | AddSuccessor(Block, JT.Block); |
| 1428 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1429 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1430 | return Block; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1434 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1435 | |
Marcin Swiderski | 47575f1 | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 1436 | // Save local scope position because in case of condition variable ScopePos |
| 1437 | // won't be restored when traversing AST. |
| 1438 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 1439 | |
| 1440 | // Create local scope for init statement and possible condition variable. |
| 1441 | // Add destructor for init statement and condition variable. |
| 1442 | // Store scope position for continue statement. |
| 1443 | if (Stmt* Init = F->getInit()) |
| 1444 | addLocalScopeForStmt(Init); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1445 | LocalScope::const_iterator LoopBeginScopePos = ScopePos; |
| 1446 | |
Marcin Swiderski | 47575f1 | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 1447 | if (VarDecl* VD = F->getConditionVariable()) |
| 1448 | addLocalScopeForVarDecl(VD); |
| 1449 | LocalScope::const_iterator ContinueScopePos = ScopePos; |
| 1450 | |
| 1451 | addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F); |
| 1452 | |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 1453 | // "for" is a control-flow statement. Thus we stop processing the current |
| 1454 | // block. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1455 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1456 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1457 | return 0; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1458 | LoopSuccessor = Block; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1459 | } else |
| 1460 | LoopSuccessor = Succ; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1461 | |
Ted Kremenek | 3f64a0e | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 1462 | // Save the current value for the break targets. |
| 1463 | // All breaks should go to the code following the loop. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1464 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
Marcin Swiderski | 47575f1 | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 1465 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Ted Kremenek | 3f64a0e | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 1466 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1467 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1468 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1469 | // evaluate the condition. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1470 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1471 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1472 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1473 | // Set the terminator for the "exit" condition block. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1474 | ExitConditionBlock->setTerminator(F); |
| 1475 | |
| 1476 | // Now add the actual condition to the condition block. Because the condition |
| 1477 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1478 | if (Stmt* C = F->getCond()) { |
| 1479 | Block = ExitConditionBlock; |
| 1480 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 8f3b834 | 2010-09-15 07:01:20 +0000 | [diff] [blame] | 1481 | assert(Block == EntryConditionBlock || |
| 1482 | (Block == 0 && EntryConditionBlock == Succ)); |
Ted Kremenek | 58b87fe | 2009-12-24 01:49:06 +0000 | [diff] [blame] | 1483 | |
| 1484 | // If this block contains a condition variable, add both the condition |
| 1485 | // variable and initializer to the CFG. |
| 1486 | if (VarDecl *VD = F->getConditionVariable()) { |
| 1487 | if (Expr *Init = VD->getInit()) { |
| 1488 | autoCreateBlock(); |
| 1489 | AppendStmt(Block, F, AddStmtChoice::AlwaysAdd); |
| 1490 | EntryConditionBlock = addStmt(Init); |
| 1491 | assert(Block == EntryConditionBlock); |
| 1492 | } |
| 1493 | } |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1494 | |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1495 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1496 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1497 | return 0; |
| 1498 | } |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1499 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1500 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1501 | // The condition block is the implicit successor for the loop body as well as |
| 1502 | // any code above the loop. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1503 | Succ = EntryConditionBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1504 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1505 | // See if this is a known constant. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1506 | TryResult KnownVal(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1508 | if (F->getCond()) |
| 1509 | KnownVal = TryEvaluateBool(F->getCond()); |
| 1510 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1511 | // Now create the loop body. |
| 1512 | { |
Ted Kremenek | 6db0ad3 | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 1513 | assert(F->getBody()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1514 | |
Ted Kremenek | 3f64a0e | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 1515 | // Save the current values for Block, Succ, and continue targets. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1516 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 1517 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1518 | |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1519 | // Create a new block to contain the (bottom) of the loop body. |
| 1520 | Block = NULL; |
Marcin Swiderski | 47575f1 | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 1521 | |
| 1522 | // Loop body should end with destructor of Condition variable (if any). |
| 1523 | addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1524 | |
Ted Kremenek | e933450 | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 1525 | if (Stmt* I = F->getInc()) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1526 | // Generate increment code in its own basic block. This is the target of |
| 1527 | // continue statements. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1528 | Succ = addStmt(I); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1529 | } else { |
| 1530 | // No increment code. Create a special, empty, block that is used as the |
| 1531 | // target block for "looping back" to the start of the loop. |
Ted Kremenek | 3575f84 | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1532 | assert(Succ == EntryConditionBlock); |
Marcin Swiderski | 47575f1 | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 1533 | Succ = Block ? Block : createBlock(); |
Ted Kremenek | e933450 | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 1534 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1535 | |
Ted Kremenek | 3575f84 | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1536 | // Finish up the increment (or empty) block if it hasn't been already. |
| 1537 | if (Block) { |
| 1538 | assert(Block == Succ); |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1539 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1540 | return 0; |
Ted Kremenek | 3575f84 | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1541 | Block = 0; |
| 1542 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1543 | |
Marcin Swiderski | 47575f1 | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 1544 | ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1545 | |
Ted Kremenek | 3575f84 | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1546 | // The starting block for the loop increment is the block that should |
| 1547 | // represent the 'loop target' for looping back to the start of the loop. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1548 | ContinueJumpTarget.Block->setLoopTarget(F); |
Ted Kremenek | 3575f84 | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 1549 | |
Marcin Swiderski | 47575f1 | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 1550 | // If body is not a compound statement create implicit scope |
| 1551 | // and add destructors. |
| 1552 | if (!isa<CompoundStmt>(F->getBody())) |
| 1553 | addLocalScopeAndDtors(F->getBody()); |
| 1554 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1555 | // Now populate the body block, and in the process create new blocks as we |
| 1556 | // walk the body of the loop. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1557 | CFGBlock* BodyBlock = addStmt(F->getBody()); |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1558 | |
| 1559 | if (!BodyBlock) |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1560 | BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);" |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1561 | else if (badCFG) |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1562 | return 0; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1563 | |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1564 | // This new body block is a successor to our "exit" condition block. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1565 | AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1566 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1567 | |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1568 | // Link up the condition block with the code that follows the loop. (the |
| 1569 | // false branch). |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1570 | AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1571 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1572 | // If the loop contains initialization, create a new block for those |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1573 | // statements. This block can also contain statements that precede the loop. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1574 | if (Stmt* I = F->getInit()) { |
| 1575 | Block = createBlock(); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1576 | return addStmt(I); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1577 | } else { |
| 1578 | // There is no loop initialization. We are thus basically a while loop. |
| 1579 | // NULL out Block to force lazy block construction. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1580 | Block = NULL; |
Ted Kremenek | 5482713 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1581 | Succ = EntryConditionBlock; |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1582 | return EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
Ted Kremenek | 115c1b9 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 1586 | CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) { |
| 1587 | if (asc.alwaysAdd()) { |
| 1588 | autoCreateBlock(); |
| 1589 | AppendStmt(Block, M, asc); |
| 1590 | } |
| 1591 | return Visit(M->getBase(), |
| 1592 | M->isArrow() ? AddStmtChoice::NotAlwaysAdd |
| 1593 | : AddStmtChoice::AsLValueNotAlwaysAdd); |
| 1594 | } |
| 1595 | |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1596 | CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) { |
| 1597 | // Objective-C fast enumeration 'for' statements: |
| 1598 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC |
| 1599 | // |
| 1600 | // for ( Type newVariable in collection_expression ) { statements } |
| 1601 | // |
| 1602 | // becomes: |
| 1603 | // |
| 1604 | // prologue: |
| 1605 | // 1. collection_expression |
| 1606 | // T. jump to loop_entry |
| 1607 | // loop_entry: |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1608 | // 1. side-effects of element expression |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1609 | // 1. ObjCForCollectionStmt [performs binding to newVariable] |
| 1610 | // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil] |
| 1611 | // TB: |
| 1612 | // statements |
| 1613 | // T. jump to loop_entry |
| 1614 | // FB: |
| 1615 | // what comes after |
| 1616 | // |
| 1617 | // and |
| 1618 | // |
| 1619 | // Type existingItem; |
| 1620 | // for ( existingItem in expression ) { statements } |
| 1621 | // |
| 1622 | // becomes: |
| 1623 | // |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1624 | // the same with newVariable replaced with existingItem; the binding works |
| 1625 | // the same except that for one ObjCForCollectionStmt::getElement() returns |
| 1626 | // a DeclStmt and the other returns a DeclRefExpr. |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1627 | // |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1628 | |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1629 | CFGBlock* LoopSuccessor = 0; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1630 | |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1631 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1632 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1633 | return 0; |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1634 | LoopSuccessor = Block; |
| 1635 | Block = 0; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1636 | } else |
| 1637 | LoopSuccessor = Succ; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1638 | |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1639 | // Build the condition blocks. |
| 1640 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1641 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1642 | |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1643 | // Set the terminator for the "exit" condition block. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1644 | ExitConditionBlock->setTerminator(S); |
| 1645 | |
| 1646 | // The last statement in the block should be the ObjCForCollectionStmt, which |
| 1647 | // performs the actual binding to 'element' and determines if there are any |
| 1648 | // more items in the collection. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1649 | AppendStmt(ExitConditionBlock, S); |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1650 | Block = ExitConditionBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1651 | |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1652 | // Walk the 'element' expression to see if there are any side-effects. We |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1653 | // generate new blocks as necesary. We DON'T add the statement by default to |
| 1654 | // the CFG unless it contains control-flow. |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1655 | EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1656 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1657 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1658 | return 0; |
| 1659 | Block = 0; |
| 1660 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1661 | |
| 1662 | // The condition block is the implicit successor for the loop body as well as |
| 1663 | // any code above the loop. |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1664 | Succ = EntryConditionBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1665 | |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1666 | // Now create the true branch. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1667 | { |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1668 | // Save the current values for Succ, continue and break targets. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1669 | SaveAndRestore<CFGBlock*> save_Succ(Succ); |
| 1670 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
| 1671 | save_break(BreakJumpTarget); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1672 | |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1673 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
| 1674 | ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1675 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1676 | CFGBlock* BodyBlock = addStmt(S->getBody()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1677 | |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1678 | if (!BodyBlock) |
| 1679 | BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;" |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1680 | else if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1681 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1682 | return 0; |
| 1683 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1684 | |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1685 | // This new body block is a successor to our "exit" condition block. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1686 | AddSuccessor(ExitConditionBlock, BodyBlock); |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1687 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1688 | |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1689 | // Link up the condition block with the code that follows the loop. |
| 1690 | // (the false branch). |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1691 | AddSuccessor(ExitConditionBlock, LoopSuccessor); |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1692 | |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1693 | // Now create a prologue block to contain the collection expression. |
Ted Kremenek | 4cb3a85 | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1694 | Block = createBlock(); |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1695 | return addStmt(S->getCollection()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1698 | CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) { |
| 1699 | // FIXME: Add locking 'primitives' to CFG for @synchronized. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1700 | |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1701 | // Inline the body. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1702 | CFGBlock *SyncBlock = addStmt(S->getSynchBody()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1703 | |
Ted Kremenek | da5348e | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1704 | // The sync body starts its own basic block. This makes it a little easier |
| 1705 | // for diagnostic clients. |
| 1706 | if (SyncBlock) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1707 | if (badCFG) |
Ted Kremenek | da5348e | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1708 | return 0; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1709 | |
Ted Kremenek | da5348e | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1710 | Block = 0; |
Ted Kremenek | fadebba | 2010-05-13 16:38:08 +0000 | [diff] [blame] | 1711 | Succ = SyncBlock; |
Ted Kremenek | da5348e | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1712 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1713 | |
Ted Kremenek | 4beaa9f | 2010-09-10 03:05:33 +0000 | [diff] [blame] | 1714 | // Add the @synchronized to the CFG. |
| 1715 | autoCreateBlock(); |
| 1716 | AppendStmt(Block, S, AddStmtChoice::AlwaysAdd); |
| 1717 | |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1718 | // Inline the sync expression. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1719 | return addStmt(S->getSynchExpr()); |
Ted Kremenek | b3b0b36 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1720 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1721 | |
Ted Kremenek | e31c0d2 | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 1722 | CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) { |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1723 | // FIXME |
Ted Kremenek | 90658ec | 2009-04-07 04:26:02 +0000 | [diff] [blame] | 1724 | return NYS(); |
Ted Kremenek | e31c0d2 | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 1725 | } |
Ted Kremenek | 514de5a | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1726 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1727 | CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1728 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1729 | |
Marcin Swiderski | 05adedc | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 1730 | // Save local scope position because in case of condition variable ScopePos |
| 1731 | // won't be restored when traversing AST. |
| 1732 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 1733 | |
| 1734 | // Create local scope for possible condition variable. |
| 1735 | // Store scope position for continue statement. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1736 | LocalScope::const_iterator LoopBeginScopePos = ScopePos; |
Marcin Swiderski | 05adedc | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 1737 | if (VarDecl* VD = W->getConditionVariable()) { |
| 1738 | addLocalScopeForVarDecl(VD); |
| 1739 | addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W); |
| 1740 | } |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1741 | |
Mike Stump | fefb9f7 | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 1742 | // "while" is a control-flow statement. Thus we stop processing the current |
| 1743 | // block. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1744 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1745 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1746 | return 0; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1747 | LoopSuccessor = Block; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1748 | } else |
| 1749 | LoopSuccessor = Succ; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1750 | |
| 1751 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1752 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1753 | // evaluate the condition. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1754 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1755 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1756 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1757 | // Set the terminator for the "exit" condition block. |
| 1758 | ExitConditionBlock->setTerminator(W); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1759 | |
| 1760 | // Now add the actual condition to the condition block. Because the condition |
| 1761 | // itself may contain control-flow, new blocks may be created. Thus we update |
| 1762 | // "Succ" after adding the condition. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1763 | if (Stmt* C = W->getCond()) { |
| 1764 | Block = ExitConditionBlock; |
| 1765 | EntryConditionBlock = addStmt(C); |
Zhongxing Xu | a1898dd | 2010-10-27 03:23:10 +0000 | [diff] [blame] | 1766 | // The condition might finish the current 'Block'. |
| 1767 | Block = EntryConditionBlock; |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1768 | |
Ted Kremenek | 4ec010a | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 1769 | // If this block contains a condition variable, add both the condition |
| 1770 | // variable and initializer to the CFG. |
| 1771 | if (VarDecl *VD = W->getConditionVariable()) { |
| 1772 | if (Expr *Init = VD->getInit()) { |
| 1773 | autoCreateBlock(); |
| 1774 | AppendStmt(Block, W, AddStmtChoice::AlwaysAdd); |
| 1775 | EntryConditionBlock = addStmt(Init); |
| 1776 | assert(Block == EntryConditionBlock); |
| 1777 | } |
| 1778 | } |
| 1779 | |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1780 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1781 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1782 | return 0; |
| 1783 | } |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1784 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1785 | |
| 1786 | // The condition block is the implicit successor for the loop body as well as |
| 1787 | // any code above the loop. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1788 | Succ = EntryConditionBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1789 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1790 | // See if this is a known constant. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1791 | const TryResult& KnownVal = TryEvaluateBool(W->getCond()); |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1792 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1793 | // Process the loop body. |
| 1794 | { |
Ted Kremenek | f6e8541 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1795 | assert(W->getBody()); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1796 | |
| 1797 | // Save the current values for Block, Succ, and continue and break targets |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1798 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 1799 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
| 1800 | save_break(BreakJumpTarget); |
Ted Kremenek | f6e8541 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1801 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1802 | // Create an empty block to represent the transition block for looping back |
| 1803 | // to the head of the loop. |
Ted Kremenek | f6e8541 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1804 | Block = 0; |
| 1805 | assert(Succ == EntryConditionBlock); |
| 1806 | Succ = createBlock(); |
| 1807 | Succ->setLoopTarget(W); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1808 | ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1809 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1810 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 05adedc | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 1811 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1812 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1813 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 1814 | Block = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1815 | |
Marcin Swiderski | 05adedc | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 1816 | // Loop body should end with destructor of Condition variable (if any). |
| 1817 | addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W); |
| 1818 | |
| 1819 | // If body is not a compound statement create implicit scope |
| 1820 | // and add destructors. |
| 1821 | if (!isa<CompoundStmt>(W->getBody())) |
| 1822 | addLocalScopeAndDtors(W->getBody()); |
| 1823 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1824 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1825 | CFGBlock* BodyBlock = addStmt(W->getBody()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1826 | |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1827 | if (!BodyBlock) |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1828 | BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;" |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1829 | else if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1830 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1831 | return 0; |
| 1832 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1833 | |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1834 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1835 | AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1836 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1837 | |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1838 | // Link up the condition block with the code that follows the loop. (the |
| 1839 | // false branch). |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1840 | AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1841 | |
| 1842 | // There can be no more statements in the condition block since we loop back |
| 1843 | // to this block. NULL out Block to force lazy creation of another block. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1844 | Block = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1845 | |
Ted Kremenek | 4ec010a | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 1846 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | 5482713 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1847 | Succ = EntryConditionBlock; |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1848 | return EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | |
| 1851 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1852 | CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { |
| 1853 | // FIXME: For now we pretend that @catch and the code it contains does not |
| 1854 | // exit. |
| 1855 | return Block; |
| 1856 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1857 | |
Ted Kremenek | 2fda504 | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1858 | CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { |
| 1859 | // FIXME: This isn't complete. We basically treat @throw like a return |
| 1860 | // statement. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1861 | |
Ted Kremenek | 6c24972 | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 1862 | // If we were in the middle of a block we stop processing that block. |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1863 | if (badCFG) |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1864 | return 0; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1865 | |
Ted Kremenek | 2fda504 | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1866 | // Create the new block. |
| 1867 | Block = createBlock(false); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1868 | |
Ted Kremenek | 2fda504 | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1869 | // The Exit block is the only successor. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1870 | AddSuccessor(Block, &cfg->getExit()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1871 | |
| 1872 | // Add the statement to the block. This may create new blocks if S contains |
| 1873 | // control-flow (short-circuit operations). |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1874 | return VisitStmt(S, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 2fda504 | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1875 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1876 | |
Mike Stump | 0979d80 | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1877 | CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) { |
Ted Kremenek | 6c24972 | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 1878 | // If we were in the middle of a block we stop processing that block. |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1879 | if (badCFG) |
Mike Stump | 0979d80 | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1880 | return 0; |
| 1881 | |
| 1882 | // Create the new block. |
| 1883 | Block = createBlock(false); |
| 1884 | |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1885 | if (TryTerminatedBlock) |
| 1886 | // The current try statement is the only successor. |
| 1887 | AddSuccessor(Block, TryTerminatedBlock); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1888 | else |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 1889 | // otherwise the Exit block is the only successor. |
| 1890 | AddSuccessor(Block, &cfg->getExit()); |
Mike Stump | 0979d80 | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1891 | |
| 1892 | // Add the statement to the block. This may create new blocks if S contains |
| 1893 | // control-flow (short-circuit operations). |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1894 | return VisitStmt(T, AddStmtChoice::AlwaysAdd); |
Mike Stump | 0979d80 | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1897 | CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) { |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1898 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1899 | |
Mike Stump | 8f9893a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 1900 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 1901 | // current block. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1902 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1903 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1904 | return 0; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1905 | LoopSuccessor = Block; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1906 | } else |
| 1907 | LoopSuccessor = Succ; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1910 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1911 | // evaluate the condition. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1912 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1913 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1914 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1915 | // Set the terminator for the "exit" condition block. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1916 | ExitConditionBlock->setTerminator(D); |
| 1917 | |
| 1918 | // Now add the actual condition to the condition block. Because the condition |
| 1919 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1920 | if (Stmt* C = D->getCond()) { |
| 1921 | Block = ExitConditionBlock; |
| 1922 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1923 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1924 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1925 | return 0; |
| 1926 | } |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1927 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1928 | |
Ted Kremenek | 5482713 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1929 | // The condition block is the implicit successor for the loop body. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1930 | Succ = EntryConditionBlock; |
| 1931 | |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1932 | // See if this is a known constant. |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1933 | const TryResult &KnownVal = TryEvaluateBool(D->getCond()); |
Mike Stump | 00998a0 | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1934 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1935 | // Process the loop body. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1936 | CFGBlock* BodyBlock = NULL; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1937 | { |
Ted Kremenek | 6db0ad3 | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 1938 | assert(D->getBody()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1939 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1940 | // Save the current values for Block, Succ, and continue and break targets |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1941 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 1942 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
| 1943 | save_break(BreakJumpTarget); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1944 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1945 | // All continues within this loop should go to the condition block |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1946 | ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1947 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1948 | // All breaks should go to the code following the loop. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1949 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1950 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1951 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 1952 | Block = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1953 | |
Marcin Swiderski | 05adedc | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 1954 | // If body is not a compound statement create implicit scope |
| 1955 | // and add destructors. |
| 1956 | if (!isa<CompoundStmt>(D->getBody())) |
| 1957 | addLocalScopeAndDtors(D->getBody()); |
| 1958 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1959 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1960 | BodyBlock = addStmt(D->getBody()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1961 | |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1962 | if (!BodyBlock) |
Ted Kremenek | a9d996d | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 1963 | BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1964 | else if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1965 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1966 | return 0; |
| 1967 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1968 | |
Ted Kremenek | d173dc7 | 2010-08-17 20:59:56 +0000 | [diff] [blame] | 1969 | if (!KnownVal.isFalse()) { |
| 1970 | // Add an intermediate block between the BodyBlock and the |
| 1971 | // ExitConditionBlock to represent the "loop back" transition. Create an |
| 1972 | // empty block to represent the transition block for looping back to the |
| 1973 | // head of the loop. |
| 1974 | // FIXME: Can we do this more efficiently without adding another block? |
| 1975 | Block = NULL; |
| 1976 | Succ = BodyBlock; |
| 1977 | CFGBlock *LoopBackBlock = createBlock(); |
| 1978 | LoopBackBlock->setLoopTarget(D); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1979 | |
Ted Kremenek | d173dc7 | 2010-08-17 20:59:56 +0000 | [diff] [blame] | 1980 | // Add the loop body entry as a successor to the condition. |
| 1981 | AddSuccessor(ExitConditionBlock, LoopBackBlock); |
| 1982 | } |
| 1983 | else |
| 1984 | AddSuccessor(ExitConditionBlock, NULL); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1985 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1986 | |
Ted Kremenek | 941fde8 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 1987 | // Link up the condition block with the code that follows the loop. |
| 1988 | // (the false branch). |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1989 | AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1990 | |
| 1991 | // There can be no more statements in the body block(s) since we loop back to |
| 1992 | // the body. NULL out Block to force lazy creation of another block. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1993 | Block = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1994 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1995 | // Return the loop body, which is the dominating block for the loop. |
Ted Kremenek | 5482713 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1996 | Succ = BodyBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1997 | return BodyBlock; |
| 1998 | } |
| 1999 | |
| 2000 | CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { |
| 2001 | // "continue" is a control-flow statement. Thus we stop processing the |
| 2002 | // current block. |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2003 | if (badCFG) |
| 2004 | return 0; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2005 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2006 | // Now create a new block that ends with the continue statement. |
| 2007 | Block = createBlock(false); |
| 2008 | Block->setTerminator(C); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2009 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2010 | // If there is no target for the continue, then we are looking at an |
Ted Kremenek | 235c5ed | 2009-04-07 18:53:24 +0000 | [diff] [blame] | 2011 | // incomplete AST. This means the CFG cannot be constructed. |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2012 | if (ContinueJumpTarget.Block) { |
Marcin Swiderski | fcb72ac | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 2013 | addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2014 | AddSuccessor(Block, ContinueJumpTarget.Block); |
| 2015 | } else |
Ted Kremenek | 235c5ed | 2009-04-07 18:53:24 +0000 | [diff] [blame] | 2016 | badCFG = true; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2017 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2018 | return Block; |
| 2019 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2020 | |
Ted Kremenek | 13fc08a | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 2021 | CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2022 | AddStmtChoice asc) { |
Ted Kremenek | 13fc08a | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 2023 | |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2024 | if (asc.alwaysAdd()) { |
Ted Kremenek | 13fc08a | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 2025 | autoCreateBlock(); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2026 | AppendStmt(Block, E); |
Ted Kremenek | 13fc08a | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 2027 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2028 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2029 | // VLA types have expressions that must be evaluated. |
| 2030 | if (E->isArgumentType()) { |
| 2031 | for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr()); |
| 2032 | VA != 0; VA = FindVA(VA->getElementType().getTypePtr())) |
| 2033 | addStmt(VA->getSizeExpr()); |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2034 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2036 | return Block; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2037 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2039 | /// VisitStmtExpr - Utility method to handle (nested) statement |
| 2040 | /// expressions (a GCC extension). |
Ted Kremenek | 852274d | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2041 | CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) { |
| 2042 | if (asc.alwaysAdd()) { |
Ted Kremenek | 13fc08a | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 2043 | autoCreateBlock(); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2044 | AppendStmt(Block, SE); |
Ted Kremenek | 13fc08a | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 2045 | } |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2046 | return VisitCompoundStmt(SE->getSubStmt()); |
| 2047 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2048 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2049 | CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2050 | // "switch" is a control-flow statement. Thus we stop processing the current |
| 2051 | // block. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2052 | CFGBlock* SwitchSuccessor = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2053 | |
Marcin Swiderski | 8ae6058 | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 2054 | // Save local scope position because in case of condition variable ScopePos |
| 2055 | // won't be restored when traversing AST. |
| 2056 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 2057 | |
| 2058 | // Create local scope for possible condition variable. |
| 2059 | // Store scope position. Add implicit destructor. |
| 2060 | if (VarDecl* VD = Terminator->getConditionVariable()) { |
| 2061 | LocalScope::const_iterator SwitchBeginScopePos = ScopePos; |
| 2062 | addLocalScopeForVarDecl(VD); |
| 2063 | addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator); |
| 2064 | } |
| 2065 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2066 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2067 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2068 | return 0; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2069 | SwitchSuccessor = Block; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2070 | } else SwitchSuccessor = Succ; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2071 | |
| 2072 | // Save the current "switch" context. |
| 2073 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 2074 | save_default(DefaultCaseBlock); |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2075 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 2076 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2077 | // Set the "default" case to be the block after the switch statement. If the |
| 2078 | // switch statement contains a "default:", this value will be overwritten with |
| 2079 | // the block for that code. |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 2080 | DefaultCaseBlock = SwitchSuccessor; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2081 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2082 | // Create a new block that will contain the switch statement. |
| 2083 | SwitchTerminatedBlock = createBlock(false); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2084 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2085 | // Now process the switch body. The code after the switch is the implicit |
| 2086 | // successor. |
| 2087 | Succ = SwitchSuccessor; |
Marcin Swiderski | f1308c7 | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2088 | BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2089 | |
| 2090 | // When visiting the body, the case statements should automatically get linked |
| 2091 | // up to the switch. We also don't keep a pointer to the body, since all |
| 2092 | // control-flow from the switch goes to case/default statements. |
Ted Kremenek | 6db0ad3 | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 2093 | assert(Terminator->getBody() && "switch must contain a non-NULL body"); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2094 | Block = NULL; |
Marcin Swiderski | 8ae6058 | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 2095 | |
| 2096 | // If body is not a compound statement create implicit scope |
| 2097 | // and add destructors. |
| 2098 | if (!isa<CompoundStmt>(Terminator->getBody())) |
| 2099 | addLocalScopeAndDtors(Terminator->getBody()); |
| 2100 | |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2101 | addStmt(Terminator->getBody()); |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2102 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2103 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2104 | return 0; |
| 2105 | } |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2106 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2107 | // If we have no "default:" case, the default transition is to the code |
| 2108 | // following the switch body. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2109 | AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2110 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2111 | // Add the terminator and condition in the switch block. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2112 | SwitchTerminatedBlock->setTerminator(Terminator); |
Ted Kremenek | 6db0ad3 | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 2113 | assert(Terminator->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2114 | Block = SwitchTerminatedBlock; |
Ted Kremenek | 6b501eb | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 2115 | Block = addStmt(Terminator->getCond()); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2116 | |
Ted Kremenek | 6b501eb | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 2117 | // Finally, if the SwitchStmt contains a condition variable, add both the |
| 2118 | // SwitchStmt and the condition variable initialization to the CFG. |
| 2119 | if (VarDecl *VD = Terminator->getConditionVariable()) { |
| 2120 | if (Expr *Init = VD->getInit()) { |
| 2121 | autoCreateBlock(); |
| 2122 | AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd); |
| 2123 | addStmt(Init); |
| 2124 | } |
| 2125 | } |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2126 | |
Ted Kremenek | 6b501eb | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 2127 | return Block; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2130 | CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2131 | // CaseStmts are essentially labels, so they are the first statement in a |
| 2132 | // block. |
Ted Kremenek | 0fc67e2 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 2133 | CFGBlock *TopBlock = 0, *LastBlock = 0; |
| 2134 | |
| 2135 | if (Stmt *Sub = CS->getSubStmt()) { |
| 2136 | // For deeply nested chains of CaseStmts, instead of doing a recursion |
| 2137 | // (which can blow out the stack), manually unroll and create blocks |
| 2138 | // along the way. |
| 2139 | while (isa<CaseStmt>(Sub)) { |
| 2140 | CFGBlock *CurrentBlock = createBlock(false); |
| 2141 | CurrentBlock->setLabel(CS); |
Ted Kremenek | 29ccaa1 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 2142 | |
Ted Kremenek | 0fc67e2 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 2143 | if (TopBlock) |
| 2144 | AddSuccessor(LastBlock, CurrentBlock); |
| 2145 | else |
| 2146 | TopBlock = CurrentBlock; |
| 2147 | |
| 2148 | AddSuccessor(SwitchTerminatedBlock, CurrentBlock); |
| 2149 | LastBlock = CurrentBlock; |
| 2150 | |
| 2151 | CS = cast<CaseStmt>(Sub); |
| 2152 | Sub = CS->getSubStmt(); |
| 2153 | } |
| 2154 | |
| 2155 | addStmt(Sub); |
| 2156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | |
Ted Kremenek | 29ccaa1 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 2158 | CFGBlock* CaseBlock = Block; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2159 | if (!CaseBlock) |
| 2160 | CaseBlock = createBlock(); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2161 | |
| 2162 | // Cases statements partition blocks, so this is the top of the basic block we |
| 2163 | // were processing (the "case XXX:" is the label). |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2164 | CaseBlock->setLabel(CS); |
| 2165 | |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2166 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2167 | return 0; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2168 | |
| 2169 | // Add this block to the list of successors for the block with the switch |
| 2170 | // statement. |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2171 | assert(SwitchTerminatedBlock); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2172 | AddSuccessor(SwitchTerminatedBlock, CaseBlock); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2173 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2174 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 2175 | Block = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2176 | |
Ted Kremenek | 0fc67e2 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 2177 | if (TopBlock) { |
| 2178 | AddSuccessor(LastBlock, CaseBlock); |
| 2179 | Succ = TopBlock; |
| 2180 | } |
| 2181 | else { |
| 2182 | // This block is now the implicit successor of other blocks. |
| 2183 | Succ = CaseBlock; |
| 2184 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2185 | |
Ted Kremenek | 0fc67e2 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 2186 | return Succ; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2187 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2188 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2189 | CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) { |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2190 | if (Terminator->getSubStmt()) |
| 2191 | addStmt(Terminator->getSubStmt()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 2193 | DefaultCaseBlock = Block; |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2194 | |
| 2195 | if (!DefaultCaseBlock) |
| 2196 | DefaultCaseBlock = createBlock(); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2197 | |
| 2198 | // Default statements partition blocks, so this is the top of the basic block |
| 2199 | // we were processing (the "default:" is the label). |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2200 | DefaultCaseBlock->setLabel(Terminator); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2202 | if (badCFG) |
Ted Kremenek | 4e8df2e | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2203 | return 0; |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 2204 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2205 | // Unlike case statements, we don't add the default block to the successors |
| 2206 | // for the switch statement immediately. This is done when we finish |
| 2207 | // processing the switch statement. This allows for the default case |
| 2208 | // (including a fall-through to the code after the switch statement) to always |
| 2209 | // be the last successor of a switch-terminated block. |
| 2210 | |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 2211 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 2212 | Block = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2213 | |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 2214 | // This block is now the implicit successor of other blocks. |
| 2215 | Succ = DefaultCaseBlock; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2216 | |
| 2217 | return DefaultCaseBlock; |
Ted Kremenek | 295222c | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 2218 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2219 | |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2220 | CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) { |
| 2221 | // "try"/"catch" is a control-flow statement. Thus we stop processing the |
| 2222 | // current block. |
| 2223 | CFGBlock* TrySuccessor = NULL; |
| 2224 | |
| 2225 | if (Block) { |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2226 | if (badCFG) |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2227 | return 0; |
| 2228 | TrySuccessor = Block; |
| 2229 | } else TrySuccessor = Succ; |
| 2230 | |
Mike Stump | a1f9363 | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 2231 | CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock; |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2232 | |
| 2233 | // Create a new block that will contain the try statement. |
Mike Stump | f00cca5 | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 2234 | CFGBlock *NewTryTerminatedBlock = createBlock(false); |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2235 | // Add the terminator in the try block. |
Mike Stump | f00cca5 | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 2236 | NewTryTerminatedBlock->setTerminator(Terminator); |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2237 | |
Mike Stump | a1f9363 | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 2238 | bool HasCatchAll = false; |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2239 | for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) { |
| 2240 | // The code after the try is the implicit successor. |
| 2241 | Succ = TrySuccessor; |
| 2242 | CXXCatchStmt *CS = Terminator->getHandler(h); |
Mike Stump | a1f9363 | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 2243 | if (CS->getExceptionDecl() == 0) { |
| 2244 | HasCatchAll = true; |
| 2245 | } |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2246 | Block = NULL; |
| 2247 | CFGBlock *CatchBlock = VisitCXXCatchStmt(CS); |
| 2248 | if (CatchBlock == 0) |
| 2249 | return 0; |
| 2250 | // Add this block to the list of successors for the block with the try |
| 2251 | // statement. |
Mike Stump | f00cca5 | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 2252 | AddSuccessor(NewTryTerminatedBlock, CatchBlock); |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2253 | } |
Mike Stump | a1f9363 | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 2254 | if (!HasCatchAll) { |
| 2255 | if (PrevTryTerminatedBlock) |
Mike Stump | f00cca5 | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 2256 | AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock); |
Mike Stump | a1f9363 | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 2257 | else |
Mike Stump | f00cca5 | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 2258 | AddSuccessor(NewTryTerminatedBlock, &cfg->getExit()); |
Mike Stump | a1f9363 | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 2259 | } |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2260 | |
| 2261 | // The code after the try is the implicit successor. |
| 2262 | Succ = TrySuccessor; |
| 2263 | |
Mike Stump | f00cca5 | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 2264 | // Save the current "try" context. |
| 2265 | SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock); |
| 2266 | TryTerminatedBlock = NewTryTerminatedBlock; |
| 2267 | |
Ted Kremenek | 6db0ad3 | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 2268 | assert(Terminator->getTryBlock() && "try must contain a non-NULL body"); |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2269 | Block = NULL; |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2270 | Block = addStmt(Terminator->getTryBlock()); |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2271 | return Block; |
| 2272 | } |
| 2273 | |
| 2274 | CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) { |
| 2275 | // CXXCatchStmt are treated like labels, so they are the first statement in a |
| 2276 | // block. |
| 2277 | |
Marcin Swiderski | 0e97bcb | 2010-10-01 01:46:52 +0000 | [diff] [blame] | 2278 | // Save local scope position because in case of exception variable ScopePos |
| 2279 | // won't be restored when traversing AST. |
| 2280 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 2281 | |
| 2282 | // Create local scope for possible exception variable. |
| 2283 | // Store scope position. Add implicit destructor. |
| 2284 | if (VarDecl* VD = CS->getExceptionDecl()) { |
| 2285 | LocalScope::const_iterator BeginScopePos = ScopePos; |
| 2286 | addLocalScopeForVarDecl(VD); |
| 2287 | addAutomaticObjDtors(ScopePos, BeginScopePos, CS); |
| 2288 | } |
| 2289 | |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2290 | if (CS->getHandlerBlock()) |
| 2291 | addStmt(CS->getHandlerBlock()); |
| 2292 | |
| 2293 | CFGBlock* CatchBlock = Block; |
| 2294 | if (!CatchBlock) |
| 2295 | CatchBlock = createBlock(); |
| 2296 | |
| 2297 | CatchBlock->setLabel(CS); |
| 2298 | |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2299 | if (badCFG) |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2300 | return 0; |
| 2301 | |
| 2302 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 2303 | Block = NULL; |
| 2304 | |
| 2305 | return CatchBlock; |
| 2306 | } |
| 2307 | |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 2308 | CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E, |
| 2309 | AddStmtChoice asc) { |
| 2310 | if (asc.alwaysAdd()) { |
| 2311 | autoCreateBlock(); |
| 2312 | AppendStmt(Block, E, asc); |
| 2313 | |
| 2314 | // We do not want to propagate the AlwaysAdd property. |
| 2315 | asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd |
| 2316 | : AddStmtChoice::NotAlwaysAdd); |
| 2317 | } |
| 2318 | return Visit(E->getSubExpr(), asc); |
| 2319 | } |
| 2320 | |
Zhongxing Xu | 81bc7d0 | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 2321 | CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C, |
| 2322 | AddStmtChoice asc) { |
| 2323 | AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue |
| 2324 | : AddStmtChoice::AlwaysAdd; |
| 2325 | autoCreateBlock(); |
| 2326 | AppendStmt(Block, C, AddStmtChoice(K)); |
| 2327 | return VisitChildren(C); |
| 2328 | } |
| 2329 | |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 2330 | CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E, |
| 2331 | AddStmtChoice asc) { |
| 2332 | if (asc.alwaysAdd()) { |
| 2333 | autoCreateBlock(); |
| 2334 | AppendStmt(Block, E, asc); |
| 2335 | // We do not want to propagate the AlwaysAdd property. |
| 2336 | asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd |
| 2337 | : AddStmtChoice::NotAlwaysAdd); |
| 2338 | } |
| 2339 | return Visit(E->getSubExpr(), asc); |
| 2340 | } |
| 2341 | |
Zhongxing Xu | 81bc7d0 | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 2342 | CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C, |
| 2343 | AddStmtChoice asc) { |
| 2344 | AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue |
| 2345 | : AddStmtChoice::AlwaysAdd; |
| 2346 | autoCreateBlock(); |
| 2347 | AppendStmt(Block, C, AddStmtChoice(K)); |
| 2348 | return VisitChildren(C); |
| 2349 | } |
| 2350 | |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2351 | CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C, |
Zhongxing Xu | c5354a2 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 2352 | AddStmtChoice asc) { |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2353 | AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue |
Zhongxing Xu | 21f6d6e | 2010-04-14 05:50:04 +0000 | [diff] [blame] | 2354 | : AddStmtChoice::AlwaysAdd; |
Zhongxing Xu | c5354a2 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 2355 | autoCreateBlock(); |
Zhongxing Xu | 21f6d6e | 2010-04-14 05:50:04 +0000 | [diff] [blame] | 2356 | AppendStmt(Block, C, AddStmtChoice(K)); |
Zhongxing Xu | c5354a2 | 2010-04-13 09:38:01 +0000 | [diff] [blame] | 2357 | return VisitChildren(C); |
| 2358 | } |
| 2359 | |
Zhongxing Xu | a725ed4 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 2360 | CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E, |
| 2361 | AddStmtChoice asc) { |
| 2362 | if (asc.alwaysAdd()) { |
| 2363 | autoCreateBlock(); |
| 2364 | AppendStmt(Block, E, asc); |
| 2365 | // We do not want to propagate the AlwaysAdd property. |
| 2366 | asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd |
| 2367 | : AddStmtChoice::NotAlwaysAdd); |
| 2368 | } |
| 2369 | return Visit(E->getSubExpr(), asc); |
| 2370 | } |
| 2371 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 2372 | CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2373 | // Lazily create the indirect-goto dispatch block if there isn't one already. |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 2374 | CFGBlock* IBlock = cfg->getIndirectGotoBlock(); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2375 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 2376 | if (!IBlock) { |
| 2377 | IBlock = createBlock(false); |
| 2378 | cfg->setIndirectGotoBlock(IBlock); |
| 2379 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2380 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 2381 | // IndirectGoto is a control-flow statement. Thus we stop processing the |
| 2382 | // current block and create a new one. |
Zhongxing Xu | d438b3d | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2383 | if (badCFG) |
Ted Kremenek | 4f88063 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2384 | return 0; |
| 2385 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 2386 | Block = createBlock(false); |
| 2387 | Block->setTerminator(I); |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2388 | AddSuccessor(Block, IBlock); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 2389 | return addStmt(I->getTarget()); |
| 2390 | } |
| 2391 | |
Ted Kremenek | befef2f | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 2392 | } // end anonymous namespace |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2393 | |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2394 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has |
| 2395 | /// no successors or predecessors. If this is the first block created in the |
| 2396 | /// CFG, it is automatically set to be the Entry and Exit of the CFG. |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 2397 | CFGBlock* CFG::createBlock() { |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2398 | bool first_block = begin() == end(); |
| 2399 | |
| 2400 | // Create the block. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2401 | CFGBlock *Mem = getAllocator().Allocate<CFGBlock>(); |
| 2402 | new (Mem) CFGBlock(NumBlockIDs++, BlkBVC); |
| 2403 | Blocks.push_back(Mem, BlkBVC); |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2404 | |
| 2405 | // If this is the first block, set it as the Entry and Exit. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2406 | if (first_block) |
| 2407 | Entry = Exit = &back(); |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2408 | |
| 2409 | // Return the block. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2410 | return &back(); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2413 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 2414 | /// CFG is returned to the caller. |
Mike Stump | b978a44 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 2415 | CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C, |
Ted Kremenek | 6c52c78 | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 2416 | BuildOptions BO) { |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2417 | CFGBuilder Builder; |
Ted Kremenek | 6c52c78 | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 2418 | return Builder.buildCFG(D, Statement, C, BO); |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2421 | //===----------------------------------------------------------------------===// |
| 2422 | // CFG: Queries for BlkExprs. |
| 2423 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2424 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2425 | namespace { |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 2426 | typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy; |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Ted Kremenek | 8a69366 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 2429 | static void FindSubExprAssignments(Stmt *S, |
| 2430 | llvm::SmallPtrSet<Expr*,50>& Set) { |
| 2431 | if (!S) |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2432 | return; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2433 | |
Ted Kremenek | 8a69366 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 2434 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) { |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2435 | Stmt *child = *I; |
Ted Kremenek | 8a69366 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 2436 | if (!child) |
| 2437 | continue; |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2438 | |
Ted Kremenek | 8a69366 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 2439 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(child)) |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2440 | if (B->isAssignmentOp()) Set.insert(B); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2441 | |
Ted Kremenek | 8a69366 | 2009-12-23 23:37:10 +0000 | [diff] [blame] | 2442 | FindSubExprAssignments(child, Set); |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2446 | static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) { |
| 2447 | BlkExprMapTy* M = new BlkExprMapTy(); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2448 | |
| 2449 | // Look for assignments that are used as subexpressions. These are the only |
| 2450 | // assignments that we want to *possibly* register as a block-level |
| 2451 | // expression. Basically, if an assignment occurs both in a subexpression and |
| 2452 | // at the block-level, it is a block-level expression. |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2453 | llvm::SmallPtrSet<Expr*,50> SubExprAssignments; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2454 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2455 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2456 | for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 2457 | if (CFGStmt S = BI->getAs<CFGStmt>()) |
| 2458 | FindSubExprAssignments(S, SubExprAssignments); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 2459 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2460 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2461 | |
| 2462 | // Iterate over the statements again on identify the Expr* and Stmt* at the |
| 2463 | // block-level that are block-level expressions. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2464 | |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 2465 | for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) { |
| 2466 | CFGStmt CS = BI->getAs<CFGStmt>(); |
| 2467 | if (!CS.isValid()) |
| 2468 | continue; |
| 2469 | if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2470 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2471 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) { |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2472 | // Assignment expressions that are not nested within another |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2473 | // expression are really "statements" whose value is never used by |
| 2474 | // another expression. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2475 | if (B->isAssignmentOp() && !SubExprAssignments.count(Exp)) |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2476 | continue; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2477 | } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) { |
| 2478 | // Special handling for statement expressions. The last statement in |
| 2479 | // the statement expression is also a block-level expr. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2480 | const CompoundStmt* C = Terminator->getSubStmt(); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 2481 | if (!C->body_empty()) { |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2482 | unsigned x = M->size(); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 2483 | (*M)[C->body_back()] = x; |
| 2484 | } |
| 2485 | } |
Ted Kremenek | e2dcd78 | 2008-01-25 23:22:27 +0000 | [diff] [blame] | 2486 | |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2487 | unsigned x = M->size(); |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2488 | (*M)[Exp] = x; |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 2489 | } |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 2490 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2491 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2492 | // Look at terminators. The condition is a block-level expression. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2493 | |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2494 | Stmt* S = (*I)->getTerminatorCondition(); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2495 | |
Ted Kremenek | 390e48b | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2496 | if (S && M->find(S) == M->end()) { |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2497 | unsigned x = M->size(); |
Ted Kremenek | 390e48b | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2498 | (*M)[S] = x; |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2499 | } |
| 2500 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2501 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2502 | return M; |
| 2503 | } |
| 2504 | |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 2505 | CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) { |
| 2506 | assert(S != NULL); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2507 | if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2508 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2509 | BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 2510 | BlkExprMapTy::iterator I = M->find(S); |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2511 | return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | unsigned CFG::getNumBlkExprs() { |
| 2515 | if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap)) |
| 2516 | return M->size(); |
| 2517 | else { |
| 2518 | // We assume callers interested in the number of BlkExprs will want |
| 2519 | // the map constructed if it doesn't already exist. |
| 2520 | BlkExprMap = (void*) PopulateBlkExprMap(*this); |
| 2521 | return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size(); |
| 2522 | } |
| 2523 | } |
| 2524 | |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 2525 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ee7f84d | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 2526 | // Filtered walking of the CFG. |
| 2527 | //===----------------------------------------------------------------------===// |
| 2528 | |
| 2529 | bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F, |
Ted Kremenek | be39a56 | 2010-09-09 02:57:48 +0000 | [diff] [blame] | 2530 | const CFGBlock *From, const CFGBlock *To) { |
Ted Kremenek | ee7f84d | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 2531 | |
| 2532 | if (F.IgnoreDefaultsWithCoveredEnums) { |
| 2533 | // If the 'To' has no label or is labeled but the label isn't a |
| 2534 | // CaseStmt then filter this edge. |
| 2535 | if (const SwitchStmt *S = |
Marcin Swiderski | 4ba72a0 | 2010-10-29 05:21:47 +0000 | [diff] [blame] | 2536 | dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) { |
Ted Kremenek | ee7f84d | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 2537 | if (S->isAllEnumCasesCovered()) { |
Ted Kremenek | be39a56 | 2010-09-09 02:57:48 +0000 | [diff] [blame] | 2538 | const Stmt *L = To->getLabel(); |
| 2539 | if (!L || !isa<CaseStmt>(L)) |
| 2540 | return true; |
Ted Kremenek | ee7f84d | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | return false; |
| 2546 | } |
| 2547 | |
| 2548 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 2549 | // Cleanup: CFG dstor. |
| 2550 | //===----------------------------------------------------------------------===// |
| 2551 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 2552 | CFG::~CFG() { |
| 2553 | delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap); |
| 2554 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2555 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2556 | //===----------------------------------------------------------------------===// |
| 2557 | // CFG pretty printing |
| 2558 | //===----------------------------------------------------------------------===// |
| 2559 | |
Ted Kremenek | e8ee26b | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 2560 | namespace { |
| 2561 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 2562 | class StmtPrinterHelper : public PrinterHelper { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2563 | typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2564 | typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2565 | StmtMapTy StmtMap; |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2566 | DeclMapTy DeclMap; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2567 | signed CurrentBlock; |
| 2568 | unsigned CurrentStmt; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2569 | const LangOptions &LangOpts; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2570 | public: |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2571 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2572 | StmtPrinterHelper(const CFG* cfg, const LangOptions &LO) |
| 2573 | : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2574 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { |
| 2575 | unsigned j = 1; |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2576 | for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ; |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2577 | BI != BEnd; ++BI, ++j ) { |
| 2578 | if (CFGStmt SE = BI->getAs<CFGStmt>()) { |
| 2579 | std::pair<unsigned, unsigned> P((*I)->getBlockID(), j); |
| 2580 | StmtMap[SE] = P; |
| 2581 | |
| 2582 | if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) { |
| 2583 | DeclMap[DS->getSingleDecl()] = P; |
| 2584 | |
| 2585 | } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) { |
| 2586 | if (VarDecl* VD = IS->getConditionVariable()) |
| 2587 | DeclMap[VD] = P; |
| 2588 | |
| 2589 | } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) { |
| 2590 | if (VarDecl* VD = FS->getConditionVariable()) |
| 2591 | DeclMap[VD] = P; |
| 2592 | |
| 2593 | } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) { |
| 2594 | if (VarDecl* VD = WS->getConditionVariable()) |
| 2595 | DeclMap[VD] = P; |
| 2596 | |
| 2597 | } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) { |
| 2598 | if (VarDecl* VD = SS->getConditionVariable()) |
| 2599 | DeclMap[VD] = P; |
| 2600 | |
| 2601 | } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) { |
| 2602 | if (VarDecl* VD = CS->getExceptionDecl()) |
| 2603 | DeclMap[VD] = P; |
| 2604 | } |
| 2605 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2606 | } |
Zhongxing Xu | b36cd3e | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 2607 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2608 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2609 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2610 | virtual ~StmtPrinterHelper() {} |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2611 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2612 | const LangOptions &getLangOpts() const { return LangOpts; } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2613 | void setBlockID(signed i) { CurrentBlock = i; } |
| 2614 | void setStmtID(unsigned i) { CurrentStmt = i; } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2615 | |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2616 | virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) { |
| 2617 | StmtMapTy::iterator I = StmtMap.find(S); |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2618 | |
| 2619 | if (I == StmtMap.end()) |
| 2620 | return false; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2621 | |
| 2622 | if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2623 | && I->second.second == CurrentStmt) { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2624 | return false; |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2625 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2626 | |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2627 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2628 | return true; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2629 | } |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2630 | |
| 2631 | bool handleDecl(Decl* D, llvm::raw_ostream& OS) { |
| 2632 | DeclMapTy::iterator I = DeclMap.find(D); |
| 2633 | |
| 2634 | if (I == DeclMap.end()) |
| 2635 | return false; |
| 2636 | |
| 2637 | if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock |
| 2638 | && I->second.second == CurrentStmt) { |
| 2639 | return false; |
| 2640 | } |
| 2641 | |
| 2642 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
| 2643 | return true; |
| 2644 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2645 | }; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2646 | } // end anonymous namespace |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2648 | |
| 2649 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 2650 | class CFGBlockTerminatorPrint |
Ted Kremenek | 6fa9b88 | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 2651 | : public StmtVisitor<CFGBlockTerminatorPrint,void> { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2652 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2653 | llvm::raw_ostream& OS; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2654 | StmtPrinterHelper* Helper; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2655 | PrintingPolicy Policy; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2656 | public: |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2657 | CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper, |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2658 | const PrintingPolicy &Policy) |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2659 | : OS(os), Helper(helper), Policy(Policy) {} |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2660 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2661 | void VisitIfStmt(IfStmt* I) { |
| 2662 | OS << "if "; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2663 | I->getCond()->printPretty(OS,Helper,Policy); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2664 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2665 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2666 | // Default case. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2667 | void VisitStmt(Stmt* Terminator) { |
| 2668 | Terminator->printPretty(OS, Helper, Policy); |
| 2669 | } |
| 2670 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2671 | void VisitForStmt(ForStmt* F) { |
| 2672 | OS << "for (" ; |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2673 | if (F->getInit()) |
| 2674 | OS << "..."; |
Ted Kremenek | 535bb20 | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 2675 | OS << "; "; |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2676 | if (Stmt* C = F->getCond()) |
| 2677 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 535bb20 | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 2678 | OS << "; "; |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2679 | if (F->getInc()) |
| 2680 | OS << "..."; |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2681 | OS << ")"; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2682 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2683 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2684 | void VisitWhileStmt(WhileStmt* W) { |
| 2685 | OS << "while " ; |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2686 | if (Stmt* C = W->getCond()) |
| 2687 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2688 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2689 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2690 | void VisitDoStmt(DoStmt* D) { |
| 2691 | OS << "do ... while "; |
Ted Kremenek | 3fa1e4b | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 2692 | if (Stmt* C = D->getCond()) |
| 2693 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 2694 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2695 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2696 | void VisitSwitchStmt(SwitchStmt* Terminator) { |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 2697 | OS << "switch "; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2698 | Terminator->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 2699 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2700 | |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2701 | void VisitCXXTryStmt(CXXTryStmt* CS) { |
| 2702 | OS << "try ..."; |
| 2703 | } |
| 2704 | |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2705 | void VisitConditionalOperator(ConditionalOperator* C) { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2706 | C->getCond()->printPretty(OS, Helper, Policy); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2707 | OS << " ? ... : ..."; |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2708 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2709 | |
Ted Kremenek | aeddbf6 | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 2710 | void VisitChooseExpr(ChooseExpr* C) { |
| 2711 | OS << "__builtin_choose_expr( "; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2712 | C->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2713 | OS << " )"; |
Ted Kremenek | aeddbf6 | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 2714 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2715 | |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2716 | void VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
| 2717 | OS << "goto *"; |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2718 | I->getTarget()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2719 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2720 | |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2721 | void VisitBinaryOperator(BinaryOperator* B) { |
| 2722 | if (!B->isLogicalOp()) { |
| 2723 | VisitExpr(B); |
| 2724 | return; |
| 2725 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2726 | |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2727 | B->getLHS()->printPretty(OS, Helper, Policy); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2728 | |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2729 | switch (B->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2730 | case BO_LOr: |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2731 | OS << " || ..."; |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2732 | return; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2733 | case BO_LAnd: |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2734 | OS << " && ..."; |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2735 | return; |
| 2736 | default: |
| 2737 | assert(false && "Invalid logical operator."); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2738 | } |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 2739 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2740 | |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 2741 | void VisitExpr(Expr* E) { |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 2742 | E->printPretty(OS, Helper, Policy); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2743 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2744 | }; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2745 | } // end anonymous namespace |
| 2746 | |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2747 | static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper, |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2748 | const CFGElement &E) { |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2749 | if (CFGStmt CS = E.getAs<CFGStmt>()) { |
| 2750 | Stmt *S = CS; |
| 2751 | |
| 2752 | if (Helper) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2753 | |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2754 | // special printing for statement-expressions. |
| 2755 | if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) { |
| 2756 | CompoundStmt* Sub = SE->getSubStmt(); |
| 2757 | |
| 2758 | if (Sub->child_begin() != Sub->child_end()) { |
| 2759 | OS << "({ ... ; "; |
| 2760 | Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS); |
| 2761 | OS << " })\n"; |
| 2762 | return; |
| 2763 | } |
| 2764 | } |
| 2765 | // special printing for comma expressions. |
| 2766 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 2767 | if (B->getOpcode() == BO_Comma) { |
| 2768 | OS << "... , "; |
| 2769 | Helper->handledStmt(B->getRHS(),OS); |
| 2770 | OS << '\n'; |
| 2771 | return; |
| 2772 | } |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 2773 | } |
| 2774 | } |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2775 | S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts())); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2776 | |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2777 | if (isa<CXXOperatorCallExpr>(S)) { |
| 2778 | OS << " (OperatorCall)"; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2779 | } |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2780 | else if (isa<CXXBindTemporaryExpr>(S)) { |
| 2781 | OS << " (BindTemporary)"; |
| 2782 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2783 | |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2784 | // Expressions need a newline. |
| 2785 | if (isa<Expr>(S)) |
| 2786 | OS << '\n'; |
Ted Kremenek | 4e0cfa8 | 2010-08-31 18:47:37 +0000 | [diff] [blame] | 2787 | |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2788 | } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) { |
| 2789 | CXXBaseOrMemberInitializer* I = IE; |
| 2790 | if (I->isBaseInitializer()) |
| 2791 | OS << I->getBaseClass()->getAsCXXRecordDecl()->getName(); |
| 2792 | else OS << I->getMember()->getName(); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2793 | |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2794 | OS << "("; |
| 2795 | if (Expr* IE = I->getInit()) |
| 2796 | IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts())); |
| 2797 | OS << ")"; |
| 2798 | |
| 2799 | if (I->isBaseInitializer()) |
| 2800 | OS << " (Base initializer)\n"; |
| 2801 | else OS << " (Member initializer)\n"; |
| 2802 | |
| 2803 | } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){ |
| 2804 | VarDecl* VD = DE.getVarDecl(); |
| 2805 | Helper->handleDecl(VD, OS); |
| 2806 | |
Marcin Swiderski | b1c5287 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 2807 | const Type* T = VD->getType().getTypePtr(); |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2808 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) |
| 2809 | T = RT->getPointeeType().getTypePtr(); |
Marcin Swiderski | b1c5287 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 2810 | else if (const Type *ET = T->getArrayElementTypeNoTypeQual()) |
| 2811 | T = ET; |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2812 | |
| 2813 | OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()"; |
| 2814 | OS << " (Implicit destructor)\n"; |
Marcin Swiderski | 7c625d8 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 2815 | |
| 2816 | } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) { |
| 2817 | const CXXBaseSpecifier *BS = BE.getBaseSpecifier(); |
| 2818 | OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()"; |
Zhongxing Xu | 4e493e0 | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 2819 | OS << " (Base object destructor)\n"; |
Marcin Swiderski | 7c625d8 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 2820 | |
| 2821 | } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) { |
| 2822 | FieldDecl *FD = ME.getFieldDecl(); |
Marcin Swiderski | 8c5e5d6 | 2010-10-25 07:05:54 +0000 | [diff] [blame] | 2823 | |
| 2824 | const Type *T = FD->getType().getTypePtr(); |
| 2825 | if (const Type *ET = T->getArrayElementTypeNoTypeQual()) |
| 2826 | T = ET; |
| 2827 | |
Marcin Swiderski | 7c625d8 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 2828 | OS << "this->" << FD->getName(); |
Marcin Swiderski | 8c5e5d6 | 2010-10-25 07:05:54 +0000 | [diff] [blame] | 2829 | OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()"; |
Zhongxing Xu | 4e493e0 | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 2830 | OS << " (Member object destructor)\n"; |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2831 | } |
Zhongxing Xu | 81bc7d0 | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 2832 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2833 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2834 | static void print_block(llvm::raw_ostream& OS, const CFG* cfg, |
| 2835 | const CFGBlock& B, |
| 2836 | StmtPrinterHelper* Helper, bool print_edges) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2837 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2838 | if (Helper) Helper->setBlockID(B.getBlockID()); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2839 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2840 | // Print the header. |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2841 | OS << "\n [ B" << B.getBlockID(); |
| 2842 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2843 | if (&B == &cfg->getEntry()) |
| 2844 | OS << " (ENTRY) ]\n"; |
| 2845 | else if (&B == &cfg->getExit()) |
| 2846 | OS << " (EXIT) ]\n"; |
| 2847 | else if (&B == cfg->getIndirectGotoBlock()) |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2848 | OS << " (INDIRECT GOTO DISPATCH) ]\n"; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2849 | else |
| 2850 | OS << " ]\n"; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2851 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2852 | // Print the label of this block. |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2853 | if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2854 | |
| 2855 | if (print_edges) |
| 2856 | OS << " "; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2857 | |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2858 | if (LabelStmt* L = dyn_cast<LabelStmt>(Label)) |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2859 | OS << L->getName(); |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2860 | else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2861 | OS << "case "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2862 | C->getLHS()->printPretty(OS, Helper, |
| 2863 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2864 | if (C->getRHS()) { |
| 2865 | OS << " ... "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2866 | C->getRHS()->printPretty(OS, Helper, |
| 2867 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2868 | } |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2869 | } else if (isa<DefaultStmt>(Label)) |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2870 | OS << "default"; |
Mike Stump | 079bd72 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2871 | else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) { |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2872 | OS << "catch ("; |
Mike Stump | a1f9363 | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 2873 | if (CS->getExceptionDecl()) |
| 2874 | CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()), |
| 2875 | 0); |
| 2876 | else |
| 2877 | OS << "..."; |
Mike Stump | 5d1d202 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2878 | OS << ")"; |
| 2879 | |
| 2880 | } else |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2881 | assert(false && "Invalid label statement in CFGBlock."); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2882 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2883 | OS << ":\n"; |
| 2884 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2885 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2886 | // Iterate through the statements in the block and print them. |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2887 | unsigned j = 1; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2888 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2889 | for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; |
| 2890 | I != E ; ++I, ++j ) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2891 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2892 | // Print the statement # in the basic block and the statement itself. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2893 | if (print_edges) |
| 2894 | OS << " "; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2895 | |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2896 | OS << llvm::format("%3d", j) << ": "; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2897 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2898 | if (Helper) |
| 2899 | Helper->setStmtID(j); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2900 | |
Marcin Swiderski | 1cff132 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 2901 | print_elem(OS,Helper,*I); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2902 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2903 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2904 | // Print the terminator of this block. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2905 | if (B.getTerminator()) { |
| 2906 | if (print_edges) |
| 2907 | OS << " "; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2908 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2909 | OS << " T: "; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2910 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2911 | if (Helper) Helper->setBlockID(-1); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2912 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2913 | CFGBlockTerminatorPrint TPrinter(OS, Helper, |
| 2914 | PrintingPolicy(Helper->getLangOpts())); |
Marcin Swiderski | 4ba72a0 | 2010-10-29 05:21:47 +0000 | [diff] [blame] | 2915 | TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt())); |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2916 | OS << '\n'; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2917 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2918 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2919 | if (print_edges) { |
| 2920 | // Print the predecessors of this block. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2921 | OS << " Predecessors (" << B.pred_size() << "):"; |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2922 | unsigned i = 0; |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2923 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2924 | for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); |
| 2925 | I != E; ++I, ++i) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2926 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2927 | if (i == 8 || (i-8) == 0) |
| 2928 | OS << "\n "; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2929 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2930 | OS << " B" << (*I)->getBlockID(); |
| 2931 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2932 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2933 | OS << '\n'; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2934 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2935 | // Print the successors of this block. |
| 2936 | OS << " Successors (" << B.succ_size() << "):"; |
| 2937 | i = 0; |
| 2938 | |
| 2939 | for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); |
| 2940 | I != E; ++I, ++i) { |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2941 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2942 | if (i == 8 || (i-8) % 10 == 0) |
| 2943 | OS << "\n "; |
| 2944 | |
Mike Stump | e5af3ce | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 2945 | if (*I) |
| 2946 | OS << " B" << (*I)->getBlockID(); |
| 2947 | else |
| 2948 | OS << " NULL"; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2949 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2950 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2951 | OS << '\n'; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 2952 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2953 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2954 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2955 | |
| 2956 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2957 | void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2958 | |
| 2959 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2960 | void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const { |
| 2961 | StmtPrinterHelper Helper(this, LO); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2962 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2963 | // Print the entry block. |
| 2964 | print_block(OS, this, getEntry(), &Helper, true); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2965 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2966 | // Iterate through the CFGBlocks and print them one by one. |
| 2967 | for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 2968 | // Skip the entry block, because we already printed it. |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2969 | if (&(**I) == &getEntry() || &(**I) == &getExit()) |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2970 | continue; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2971 | |
Ted Kremenek | ee82d9b | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 2972 | print_block(OS, this, **I, &Helper, true); |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2973 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2974 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2975 | // Print the exit block. |
| 2976 | print_block(OS, this, getExit(), &Helper, true); |
Ted Kremenek | d017243 | 2008-11-24 20:50:24 +0000 | [diff] [blame] | 2977 | OS.flush(); |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2978 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2979 | |
| 2980 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2981 | void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const { |
| 2982 | print(llvm::errs(), cfg, LO); |
| 2983 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2984 | |
| 2985 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 2986 | /// Generally this will only be called from CFG::print. |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2987 | void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg, |
| 2988 | const LangOptions &LO) const { |
| 2989 | StmtPrinterHelper Helper(cfg, LO); |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2990 | print_block(OS, cfg, *this, &Helper, true); |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2991 | } |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2992 | |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2993 | /// printTerminator - A simple pretty printer of the terminator of a CFGBlock. |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2994 | void CFGBlock::printTerminator(llvm::raw_ostream &OS, |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2995 | const LangOptions &LO) const { |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2996 | CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO)); |
Marcin Swiderski | 4ba72a0 | 2010-10-29 05:21:47 +0000 | [diff] [blame] | 2997 | TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt())); |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Ted Kremenek | 390e48b | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 3000 | Stmt* CFGBlock::getTerminatorCondition() { |
Marcin Swiderski | 4ba72a0 | 2010-10-29 05:21:47 +0000 | [diff] [blame] | 3001 | Stmt *Terminator = this->Terminator; |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3002 | if (!Terminator) |
| 3003 | return NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3004 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3005 | Expr* E = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3006 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3007 | switch (Terminator->getStmtClass()) { |
| 3008 | default: |
| 3009 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3010 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3011 | case Stmt::ForStmtClass: |
| 3012 | E = cast<ForStmt>(Terminator)->getCond(); |
| 3013 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3014 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3015 | case Stmt::WhileStmtClass: |
| 3016 | E = cast<WhileStmt>(Terminator)->getCond(); |
| 3017 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3018 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3019 | case Stmt::DoStmtClass: |
| 3020 | E = cast<DoStmt>(Terminator)->getCond(); |
| 3021 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3022 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3023 | case Stmt::IfStmtClass: |
| 3024 | E = cast<IfStmt>(Terminator)->getCond(); |
| 3025 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3026 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3027 | case Stmt::ChooseExprClass: |
| 3028 | E = cast<ChooseExpr>(Terminator)->getCond(); |
| 3029 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3030 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3031 | case Stmt::IndirectGotoStmtClass: |
| 3032 | E = cast<IndirectGotoStmt>(Terminator)->getTarget(); |
| 3033 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3034 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3035 | case Stmt::SwitchStmtClass: |
| 3036 | E = cast<SwitchStmt>(Terminator)->getCond(); |
| 3037 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3038 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3039 | case Stmt::ConditionalOperatorClass: |
| 3040 | E = cast<ConditionalOperator>(Terminator)->getCond(); |
| 3041 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3042 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3043 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 3044 | E = cast<BinaryOperator>(Terminator)->getLHS(); |
Ted Kremenek | 390e48b | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 3045 | break; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3046 | |
Ted Kremenek | 390e48b | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 3047 | case Stmt::ObjCForCollectionStmtClass: |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3048 | return Terminator; |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3049 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3050 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3051 | return E ? E->IgnoreParens() : NULL; |
| 3052 | } |
| 3053 | |
Ted Kremenek | 9c2535a | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 3054 | bool CFGBlock::hasBinaryBranchTerminator() const { |
Marcin Swiderski | 4ba72a0 | 2010-10-29 05:21:47 +0000 | [diff] [blame] | 3055 | const Stmt *Terminator = this->Terminator; |
Ted Kremenek | 9c2535a | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 3056 | if (!Terminator) |
| 3057 | return false; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3058 | |
Ted Kremenek | 9c2535a | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 3059 | Expr* E = NULL; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3060 | |
Ted Kremenek | 9c2535a | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 3061 | switch (Terminator->getStmtClass()) { |
| 3062 | default: |
| 3063 | return false; |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3064 | |
| 3065 | case Stmt::ForStmtClass: |
Ted Kremenek | 9c2535a | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 3066 | case Stmt::WhileStmtClass: |
| 3067 | case Stmt::DoStmtClass: |
| 3068 | case Stmt::IfStmtClass: |
| 3069 | case Stmt::ChooseExprClass: |
| 3070 | case Stmt::ConditionalOperatorClass: |
| 3071 | case Stmt::BinaryOperatorClass: |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3072 | return true; |
Ted Kremenek | 9c2535a | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 3073 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3074 | |
Ted Kremenek | 9c2535a | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 3075 | return E ? E->IgnoreParens() : NULL; |
| 3076 | } |
| 3077 | |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 3078 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 3079 | //===----------------------------------------------------------------------===// |
| 3080 | // CFG Graphviz Visualization |
| 3081 | //===----------------------------------------------------------------------===// |
| 3082 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 3083 | |
| 3084 | #ifndef NDEBUG |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3085 | static StmtPrinterHelper* GraphHelper; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 3086 | #endif |
| 3087 | |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 3088 | void CFG::viewCFG(const LangOptions &LO) const { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 3089 | #ifndef NDEBUG |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 3090 | StmtPrinterHelper H(this, LO); |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 3091 | GraphHelper = &H; |
| 3092 | llvm::ViewGraph(this,"CFG"); |
| 3093 | GraphHelper = NULL; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 3094 | #endif |
| 3095 | } |
| 3096 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 3097 | namespace llvm { |
| 3098 | template<> |
| 3099 | struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { |
Tobias Grosser | 006b0eb | 2009-11-30 14:16:05 +0000 | [diff] [blame] | 3100 | |
| 3101 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 3102 | |
| 3103 | static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) { |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 3104 | |
Hartmut Kaiser | bd250b4 | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 3105 | #ifndef NDEBUG |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 3106 | std::string OutSStr; |
| 3107 | llvm::raw_string_ostream Out(OutSStr); |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 3108 | print_block(Out,Graph, *Node, GraphHelper, false); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 3109 | std::string& OutStr = Out.str(); |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 3110 | |
| 3111 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 3112 | |
| 3113 | // Process string output to make it nicer... |
| 3114 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 3115 | if (OutStr[i] == '\n') { // Left justify |
| 3116 | OutStr[i] = '\\'; |
| 3117 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 3118 | } |
Mike Stump | 6d9828c | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3119 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 3120 | return OutStr; |
Hartmut Kaiser | bd250b4 | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 3121 | #else |
| 3122 | return ""; |
| 3123 | #endif |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 3124 | } |
| 3125 | }; |
| 3126 | } // end namespace llvm |