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