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