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