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