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