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