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