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