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