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