Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 1 | //===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===// |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 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 | 6796fbd | 2009-07-16 18:13:04 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/CFG.h" |
Benjamin Kramer | 1ea8e09 | 2012-07-04 17:04:04 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Ted Kremenek | 1a241d1 | 2011-02-23 05:11:46 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
| 20 | #include "clang/AST/PrettyPrinter.h" |
| 21 | #include "clang/AST/StmtVisitor.h" |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseMap.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 24 | #include <memory> |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Allocator.h" |
| 27 | #include "llvm/Support/Format.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 28 | #include "llvm/Support/GraphWriter.h" |
| 29 | #include "llvm/Support/SaveAndRestore.h" |
Ted Kremenek | e5ccf9a | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
| 33 | namespace { |
| 34 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 35 | static SourceLocation GetEndLoc(Decl *D) { |
| 36 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 37 | if (Expr *Ex = VD->getInit()) |
Ted Kremenek | 8889bb3 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 38 | return Ex->getSourceRange().getEnd(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 39 | return D->getLocation(); |
Ted Kremenek | 8889bb3 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 40 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 41 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 42 | /// Helper for tryNormalizeBinaryOperator. Attempts to extract an IntegerLiteral |
| 43 | /// or EnumConstantDecl from the given Expr. If it fails, returns nullptr. |
| 44 | const Expr *tryTransformToIntOrEnumConstant(const Expr *E) { |
| 45 | E = E->IgnoreParens(); |
| 46 | if (isa<IntegerLiteral>(E)) |
| 47 | return E; |
| 48 | if (auto *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 49 | return isa<EnumConstantDecl>(DR->getDecl()) ? DR : nullptr; |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
| 53 | /// Tries to interpret a binary operator into `Decl Op Expr` form, if Expr is |
| 54 | /// an integer literal or an enum constant. |
| 55 | /// |
| 56 | /// If this fails, at least one of the returned DeclRefExpr or Expr will be |
| 57 | /// null. |
| 58 | static std::tuple<const DeclRefExpr *, BinaryOperatorKind, const Expr *> |
| 59 | tryNormalizeBinaryOperator(const BinaryOperator *B) { |
| 60 | BinaryOperatorKind Op = B->getOpcode(); |
| 61 | |
| 62 | const Expr *MaybeDecl = B->getLHS(); |
| 63 | const Expr *Constant = tryTransformToIntOrEnumConstant(B->getRHS()); |
| 64 | // Expr looked like `0 == Foo` instead of `Foo == 0` |
| 65 | if (Constant == nullptr) { |
| 66 | // Flip the operator |
| 67 | if (Op == BO_GT) |
| 68 | Op = BO_LT; |
| 69 | else if (Op == BO_GE) |
| 70 | Op = BO_LE; |
| 71 | else if (Op == BO_LT) |
| 72 | Op = BO_GT; |
| 73 | else if (Op == BO_LE) |
| 74 | Op = BO_GE; |
| 75 | |
| 76 | MaybeDecl = B->getRHS(); |
| 77 | Constant = tryTransformToIntOrEnumConstant(B->getLHS()); |
| 78 | } |
| 79 | |
| 80 | auto *D = dyn_cast<DeclRefExpr>(MaybeDecl->IgnoreParenImpCasts()); |
| 81 | return std::make_tuple(D, Op, Constant); |
| 82 | } |
| 83 | |
| 84 | /// For an expression `x == Foo && x == Bar`, this determines whether the |
| 85 | /// `Foo` and `Bar` are either of the same enumeration type, or both integer |
| 86 | /// literals. |
| 87 | /// |
| 88 | /// It's an error to pass this arguments that are not either IntegerLiterals |
| 89 | /// or DeclRefExprs (that have decls of type EnumConstantDecl) |
| 90 | static bool areExprTypesCompatible(const Expr *E1, const Expr *E2) { |
| 91 | // User intent isn't clear if they're mixing int literals with enum |
| 92 | // constants. |
| 93 | if (isa<IntegerLiteral>(E1) != isa<IntegerLiteral>(E2)) |
| 94 | return false; |
| 95 | |
| 96 | // Integer literal comparisons, regardless of literal type, are acceptable. |
| 97 | if (isa<IntegerLiteral>(E1)) |
| 98 | return true; |
| 99 | |
| 100 | // IntegerLiterals are handled above and only EnumConstantDecls are expected |
| 101 | // beyond this point |
| 102 | assert(isa<DeclRefExpr>(E1) && isa<DeclRefExpr>(E2)); |
| 103 | auto *Decl1 = cast<DeclRefExpr>(E1)->getDecl(); |
| 104 | auto *Decl2 = cast<DeclRefExpr>(E2)->getDecl(); |
| 105 | |
| 106 | assert(isa<EnumConstantDecl>(Decl1) && isa<EnumConstantDecl>(Decl2)); |
| 107 | const DeclContext *DC1 = Decl1->getDeclContext(); |
| 108 | const DeclContext *DC2 = Decl2->getDeclContext(); |
| 109 | |
| 110 | assert(isa<EnumDecl>(DC1) && isa<EnumDecl>(DC2)); |
| 111 | return DC1 == DC2; |
| 112 | } |
| 113 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 114 | class CFGBuilder; |
| 115 | |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 116 | /// The CFG builder uses a recursive algorithm to build the CFG. When |
| 117 | /// we process an expression, sometimes we know that we must add the |
| 118 | /// subexpressions as block-level expressions. For example: |
| 119 | /// |
| 120 | /// exp1 || exp2 |
| 121 | /// |
| 122 | /// When processing the '||' expression, we know that exp1 and exp2 |
| 123 | /// need to be added as block-level expressions, even though they |
| 124 | /// might not normally need to be. AddStmtChoice records this |
| 125 | /// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then |
| 126 | /// the builder has an option not to add a subexpression as a |
| 127 | /// block-level expression. |
| 128 | /// |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 129 | class AddStmtChoice { |
| 130 | public: |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 131 | enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 }; |
Ted Kremenek | 5d2bb1b | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 132 | |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 133 | AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {} |
Ted Kremenek | 5d2bb1b | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 135 | bool alwaysAdd(CFGBuilder &builder, |
| 136 | const Stmt *stmt) const; |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 137 | |
| 138 | /// Return a copy of this object, except with the 'always-add' bit |
| 139 | /// set as specified. |
| 140 | AddStmtChoice withAlwaysAdd(bool alwaysAdd) const { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 141 | return AddStmtChoice(alwaysAdd ? AlwaysAdd : NotAlwaysAdd); |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 144 | private: |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 145 | Kind kind; |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 146 | }; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 147 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 148 | /// LocalScope - Node in tree of local scopes created for C++ implicit |
| 149 | /// destructor calls generation. It contains list of automatic variables |
| 150 | /// declared in the scope and link to position in previous scope this scope |
| 151 | /// began in. |
| 152 | /// |
| 153 | /// The process of creating local scopes is as follows: |
| 154 | /// - Init CFGBuilder::ScopePos with invalid position (equivalent for null), |
| 155 | /// - Before processing statements in scope (e.g. CompoundStmt) create |
| 156 | /// LocalScope object using CFGBuilder::ScopePos as link to previous scope |
| 157 | /// and set CFGBuilder::ScopePos to the end of new scope, |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 158 | /// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 159 | /// at this VarDecl, |
| 160 | /// - For every normal (without jump) end of scope add to CFGBlock destructors |
| 161 | /// for objects in the current scope, |
| 162 | /// - For every jump add to CFGBlock destructors for objects |
| 163 | /// between CFGBuilder::ScopePos and local scope position saved for jump |
| 164 | /// target. Thanks to C++ restrictions on goto jumps we can be sure that |
| 165 | /// jump target position will be on the path to root from CFGBuilder::ScopePos |
| 166 | /// (adding any variable that doesn't need constructor to be called to |
| 167 | /// LocalScope can break this assumption), |
| 168 | /// |
| 169 | class LocalScope { |
| 170 | public: |
Ted Kremenek | c7bfdcd | 2011-02-15 02:47:45 +0000 | [diff] [blame] | 171 | typedef BumpVector<VarDecl*> AutomaticVarsTy; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 172 | |
| 173 | /// const_iterator - Iterates local scope backwards and jumps to previous |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 174 | /// scope on reaching the beginning of currently iterated scope. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 175 | class const_iterator { |
| 176 | const LocalScope* Scope; |
| 177 | |
| 178 | /// VarIter is guaranteed to be greater then 0 for every valid iterator. |
| 179 | /// Invalid iterator (with null Scope) has VarIter equal to 0. |
| 180 | unsigned VarIter; |
| 181 | |
| 182 | public: |
| 183 | /// Create invalid iterator. Dereferencing invalid iterator is not allowed. |
| 184 | /// Incrementing invalid iterator is allowed and will result in invalid |
| 185 | /// iterator. |
| 186 | const_iterator() |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 187 | : Scope(nullptr), VarIter(0) {} |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 188 | |
| 189 | /// Create valid iterator. In case when S.Prev is an invalid iterator and |
| 190 | /// I is equal to 0, this will create invalid iterator. |
| 191 | const_iterator(const LocalScope& S, unsigned I) |
| 192 | : Scope(&S), VarIter(I) { |
| 193 | // Iterator to "end" of scope is not allowed. Handle it by going up |
| 194 | // in scopes tree possibly up to invalid iterator in the root. |
| 195 | if (VarIter == 0 && Scope) |
| 196 | *this = Scope->Prev; |
| 197 | } |
| 198 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 199 | VarDecl *const* operator->() const { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 200 | assert (Scope && "Dereferencing invalid iterator is not allowed"); |
| 201 | assert (VarIter != 0 && "Iterator has invalid value of VarIter member"); |
| 202 | return &Scope->Vars[VarIter - 1]; |
| 203 | } |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 204 | VarDecl *operator*() const { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 205 | return *this->operator->(); |
| 206 | } |
| 207 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 208 | const_iterator &operator++() { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 209 | if (!Scope) |
| 210 | return *this; |
| 211 | |
| 212 | assert (VarIter != 0 && "Iterator has invalid value of VarIter member"); |
| 213 | --VarIter; |
| 214 | if (VarIter == 0) |
| 215 | *this = Scope->Prev; |
| 216 | return *this; |
| 217 | } |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 218 | const_iterator operator++(int) { |
| 219 | const_iterator P = *this; |
| 220 | ++*this; |
| 221 | return P; |
| 222 | } |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 224 | bool operator==(const const_iterator &rhs) const { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 225 | return Scope == rhs.Scope && VarIter == rhs.VarIter; |
| 226 | } |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 227 | bool operator!=(const const_iterator &rhs) const { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 228 | return !(*this == rhs); |
| 229 | } |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 230 | |
Aaron Ballman | 6734766 | 2015-02-15 22:00:28 +0000 | [diff] [blame] | 231 | explicit operator bool() const { |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 232 | return *this != const_iterator(); |
| 233 | } |
| 234 | |
| 235 | int distance(const_iterator L); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | friend class const_iterator; |
| 239 | |
| 240 | private: |
Ted Kremenek | c7bfdcd | 2011-02-15 02:47:45 +0000 | [diff] [blame] | 241 | BumpVectorContext ctx; |
| 242 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 243 | /// Automatic variables in order of declaration. |
| 244 | AutomaticVarsTy Vars; |
| 245 | /// Iterator to variable in previous scope that was declared just before |
| 246 | /// begin of this scope. |
| 247 | const_iterator Prev; |
| 248 | |
| 249 | public: |
| 250 | /// Constructs empty scope linked to previous scope in specified place. |
David Blaikie | c1334cc | 2015-08-13 22:12:21 +0000 | [diff] [blame] | 251 | LocalScope(BumpVectorContext ctx, const_iterator P) |
| 252 | : ctx(std::move(ctx)), Vars(this->ctx, 4), Prev(P) {} |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 253 | |
| 254 | /// Begin of scope in direction of CFG building (backwards). |
| 255 | const_iterator begin() const { return const_iterator(*this, Vars.size()); } |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 257 | void addVar(VarDecl *VD) { |
Ted Kremenek | c7bfdcd | 2011-02-15 02:47:45 +0000 | [diff] [blame] | 258 | Vars.push_back(VD, ctx); |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 259 | } |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 262 | /// distance - Calculates distance from this to L. L must be reachable from this |
| 263 | /// (with use of ++ operator). Cost of calculating the distance is linear w.r.t. |
| 264 | /// number of scopes between this and L. |
| 265 | int LocalScope::const_iterator::distance(LocalScope::const_iterator L) { |
| 266 | int D = 0; |
| 267 | const_iterator F = *this; |
| 268 | while (F.Scope != L.Scope) { |
Ted Kremenek | 50aa2d4 | 2011-08-12 14:41:23 +0000 | [diff] [blame] | 269 | assert (F != const_iterator() |
| 270 | && "L iterator is not reachable from F iterator."); |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 271 | D += F.VarIter; |
| 272 | F = F.Scope->Prev; |
| 273 | } |
| 274 | D += F.VarIter - L.VarIter; |
| 275 | return D; |
| 276 | } |
| 277 | |
Jonathan Roelofs | 99bdd98 | 2015-05-19 18:51:56 +0000 | [diff] [blame] | 278 | /// Structure for specifying position in CFG during its build process. It |
| 279 | /// consists of CFGBlock that specifies position in CFG and |
| 280 | /// LocalScope::const_iterator that specifies position in LocalScope graph. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 281 | struct BlockScopePosPair { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 282 | BlockScopePosPair() : block(nullptr) {} |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 283 | BlockScopePosPair(CFGBlock *b, LocalScope::const_iterator scopePos) |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 284 | : block(b), scopePosition(scopePos) {} |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 286 | CFGBlock *block; |
| 287 | LocalScope::const_iterator scopePosition; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 290 | /// TryResult - a class representing a variant over the values |
| 291 | /// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool, |
| 292 | /// and is used by the CFGBuilder to decide if a branch condition |
| 293 | /// can be decided up front during CFG construction. |
| 294 | class TryResult { |
| 295 | int X; |
| 296 | public: |
| 297 | TryResult(bool b) : X(b ? 1 : 0) {} |
| 298 | TryResult() : X(-1) {} |
| 299 | |
| 300 | bool isTrue() const { return X == 1; } |
| 301 | bool isFalse() const { return X == 0; } |
| 302 | bool isKnown() const { return X >= 0; } |
| 303 | void negate() { |
| 304 | assert(isKnown()); |
| 305 | X ^= 0x1; |
| 306 | } |
| 307 | }; |
| 308 | |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 309 | TryResult bothKnownTrue(TryResult R1, TryResult R2) { |
| 310 | if (!R1.isKnown() || !R2.isKnown()) |
| 311 | return TryResult(); |
| 312 | return TryResult(R1.isTrue() && R2.isTrue()); |
| 313 | } |
| 314 | |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 315 | class reverse_children { |
| 316 | llvm::SmallVector<Stmt *, 12> childrenBuf; |
| 317 | ArrayRef<Stmt*> children; |
| 318 | public: |
| 319 | reverse_children(Stmt *S); |
| 320 | |
| 321 | typedef ArrayRef<Stmt*>::reverse_iterator iterator; |
| 322 | iterator begin() const { return children.rbegin(); } |
| 323 | iterator end() const { return children.rend(); } |
| 324 | }; |
| 325 | |
| 326 | |
| 327 | reverse_children::reverse_children(Stmt *S) { |
| 328 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 329 | children = CE->getRawSubExprs(); |
| 330 | return; |
| 331 | } |
| 332 | switch (S->getStmtClass()) { |
Ted Kremenek | 7d86b9c | 2013-02-05 22:03:14 +0000 | [diff] [blame] | 333 | // Note: Fill in this switch with more cases we want to optimize. |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 334 | case Stmt::InitListExprClass: { |
| 335 | InitListExpr *IE = cast<InitListExpr>(S); |
| 336 | children = llvm::makeArrayRef(reinterpret_cast<Stmt**>(IE->getInits()), |
| 337 | IE->getNumInits()); |
| 338 | return; |
| 339 | } |
| 340 | default: |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | // Default case for all other statements. |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 345 | for (Stmt *SubStmt : S->children()) |
| 346 | childrenBuf.push_back(SubStmt); |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 347 | |
| 348 | // This needs to be done *after* childrenBuf has been populated. |
| 349 | children = childrenBuf; |
| 350 | } |
| 351 | |
Ted Kremenek | be9b33b | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 352 | /// CFGBuilder - This class implements CFG construction from an AST. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 353 | /// The builder is stateful: an instance of the builder should be used to only |
| 354 | /// construct a single CFG. |
| 355 | /// |
| 356 | /// Example usage: |
| 357 | /// |
| 358 | /// CFGBuilder builder; |
Jonathan Roelofs | ab046c5 | 2015-07-27 16:05:36 +0000 | [diff] [blame] | 359 | /// std::unique_ptr<CFG> cfg = builder.buildCFG(decl, stmt1); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 360 | /// |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 361 | /// CFG construction is done via a recursive walk of an AST. We actually parse |
| 362 | /// the AST in reverse order so that the successor of a basic block is |
| 363 | /// constructed prior to its predecessor. This allows us to nicely capture |
| 364 | /// implicit fall-throughs without extra basic blocks. |
Ted Kremenek | 1b8ac85 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 365 | /// |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 366 | class CFGBuilder { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 367 | typedef BlockScopePosPair JumpTarget; |
| 368 | typedef BlockScopePosPair JumpSource; |
| 369 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 370 | ASTContext *Context; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 371 | std::unique_ptr<CFG> cfg; |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 373 | CFGBlock *Block; |
| 374 | CFGBlock *Succ; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 375 | JumpTarget ContinueJumpTarget; |
| 376 | JumpTarget BreakJumpTarget; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 377 | CFGBlock *SwitchTerminatedBlock; |
| 378 | CFGBlock *DefaultCaseBlock; |
| 379 | CFGBlock *TryTerminatedBlock; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 380 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 381 | // Current position in local scope. |
| 382 | LocalScope::const_iterator ScopePos; |
| 383 | |
| 384 | // LabelMap records the mapping from Label expressions to their jump targets. |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 385 | typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy; |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 386 | LabelMapTy LabelMap; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 387 | |
| 388 | // A list of blocks that end with a "goto" that must be backpatched to their |
| 389 | // resolved targets upon completion of CFG construction. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 390 | typedef std::vector<JumpSource> BackpatchBlocksTy; |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 391 | BackpatchBlocksTy BackpatchBlocks; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 392 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 393 | // A list of labels whose address has been taken (for indirect gotos). |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 394 | typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy; |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 395 | LabelSetTy AddressTakenLabels; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 396 | |
Zhongxing Xu | d38fb84 | 2010-09-16 03:28:18 +0000 | [diff] [blame] | 397 | bool badCFG; |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 398 | const CFG::BuildOptions &BuildOpts; |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 399 | |
| 400 | // State to track for building switch statements. |
| 401 | bool switchExclusivelyCovered; |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 402 | Expr::EvalResult *switchCond; |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 403 | |
| 404 | CFG::BuildOptions::ForcedBlkExprs::value_type *cachedEntry; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 405 | const Stmt *lastLookup; |
Zhongxing Xu | d38fb84 | 2010-09-16 03:28:18 +0000 | [diff] [blame] | 406 | |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 407 | // Caches boolean evaluations of expressions to avoid multiple re-evaluations |
| 408 | // during construction of branches for chained logical operators. |
NAKAMURA Takumi | e9ca55e | 2012-03-25 06:30:37 +0000 | [diff] [blame] | 409 | typedef llvm::DenseMap<Expr *, TryResult> CachedBoolEvalsTy; |
| 410 | CachedBoolEvalsTy CachedBoolEvals; |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 411 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 412 | public: |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 413 | explicit CFGBuilder(ASTContext *astContext, |
| 414 | const CFG::BuildOptions &buildOpts) |
| 415 | : Context(astContext), cfg(new CFG()), // crew a new CFG |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 416 | Block(nullptr), Succ(nullptr), |
| 417 | SwitchTerminatedBlock(nullptr), DefaultCaseBlock(nullptr), |
| 418 | TryTerminatedBlock(nullptr), badCFG(false), BuildOpts(buildOpts), |
| 419 | switchExclusivelyCovered(false), switchCond(nullptr), |
| 420 | cachedEntry(nullptr), lastLookup(nullptr) {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 421 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 422 | // buildCFG - Used by external clients to construct the CFG. |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 423 | std::unique_ptr<CFG> buildCFG(const Decl *D, Stmt *Statement); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 424 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 425 | bool alwaysAdd(const Stmt *stmt); |
| 426 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 427 | private: |
| 428 | // Visitors to walk an AST and construct the CFG. |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 429 | CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc); |
| 430 | CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 431 | CFGBlock *VisitBreakStmt(BreakStmt *B); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 432 | CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 433 | CFGBlock *VisitCaseStmt(CaseStmt *C); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 434 | CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 435 | CFGBlock *VisitCompoundStmt(CompoundStmt *C); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 436 | CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C, |
| 437 | AddStmtChoice asc); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 438 | CFGBlock *VisitContinueStmt(ContinueStmt *C); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 439 | CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E, |
| 440 | AddStmtChoice asc); |
| 441 | CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S); |
| 442 | CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc); |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 443 | CFGBlock *VisitCXXNewExpr(CXXNewExpr *DE, AddStmtChoice asc); |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 444 | CFGBlock *VisitCXXDeleteExpr(CXXDeleteExpr *DE, AddStmtChoice asc); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 445 | CFGBlock *VisitCXXForRangeStmt(CXXForRangeStmt *S); |
| 446 | CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E, |
| 447 | AddStmtChoice asc); |
| 448 | CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C, |
| 449 | AddStmtChoice asc); |
| 450 | CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T); |
| 451 | CFGBlock *VisitCXXTryStmt(CXXTryStmt *S); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 452 | CFGBlock *VisitDeclStmt(DeclStmt *DS); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 453 | CFGBlock *VisitDeclSubExpr(DeclStmt *DS); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 454 | CFGBlock *VisitDefaultStmt(DefaultStmt *D); |
| 455 | CFGBlock *VisitDoStmt(DoStmt *D); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 456 | CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E, AddStmtChoice asc); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 457 | CFGBlock *VisitForStmt(ForStmt *F); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 458 | CFGBlock *VisitGotoStmt(GotoStmt *G); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 459 | CFGBlock *VisitIfStmt(IfStmt *I); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 460 | CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 461 | CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I); |
| 462 | CFGBlock *VisitLabelStmt(LabelStmt *L); |
Devin Coughlin | b6029b7 | 2015-11-25 22:35:37 +0000 | [diff] [blame] | 463 | CFGBlock *VisitBlockExpr(BlockExpr *E, AddStmtChoice asc); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 464 | CFGBlock *VisitLambdaExpr(LambdaExpr *E, AddStmtChoice asc); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 465 | CFGBlock *VisitLogicalOperator(BinaryOperator *B); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 466 | std::pair<CFGBlock *, CFGBlock *> VisitLogicalOperator(BinaryOperator *B, |
| 467 | Stmt *Term, |
| 468 | CFGBlock *TrueBlock, |
| 469 | CFGBlock *FalseBlock); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 470 | CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 471 | CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 472 | CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 473 | CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 474 | CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 475 | CFGBlock *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 476 | CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 477 | CFGBlock *VisitPseudoObjectExpr(PseudoObjectExpr *E); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 478 | CFGBlock *VisitReturnStmt(ReturnStmt *R); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 479 | CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 480 | CFGBlock *VisitSwitchStmt(SwitchStmt *S); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 481 | CFGBlock *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E, |
| 482 | AddStmtChoice asc); |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 483 | CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 484 | CFGBlock *VisitWhileStmt(WhileStmt *W); |
Mike Stump | 48871a2 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 486 | CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd); |
| 487 | CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 488 | CFGBlock *VisitChildren(Stmt *S); |
Ted Kremenek | e249984 | 2012-04-12 20:03:44 +0000 | [diff] [blame] | 489 | CFGBlock *VisitNoRecurse(Expr *E, AddStmtChoice asc); |
Mike Stump | 48871a2 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 490 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 491 | /// When creating the CFG for temporary destructors, we want to mirror the |
| 492 | /// branch structure of the corresponding constructor calls. |
| 493 | /// Thus, while visiting a statement for temporary destructors, we keep a |
| 494 | /// context to keep track of the following information: |
| 495 | /// - whether a subexpression is executed unconditionally |
| 496 | /// - if a subexpression is executed conditionally, the first |
| 497 | /// CXXBindTemporaryExpr we encounter in that subexpression (which |
| 498 | /// corresponds to the last temporary destructor we have to call for this |
| 499 | /// subexpression) and the CFG block at that point (which will become the |
| 500 | /// successor block when inserting the decision point). |
| 501 | /// |
| 502 | /// That way, we can build the branch structure for temporary destructors as |
| 503 | /// follows: |
| 504 | /// 1. If a subexpression is executed unconditionally, we add the temporary |
| 505 | /// destructor calls to the current block. |
| 506 | /// 2. If a subexpression is executed conditionally, when we encounter a |
| 507 | /// CXXBindTemporaryExpr: |
| 508 | /// a) If it is the first temporary destructor call in the subexpression, |
| 509 | /// we remember the CXXBindTemporaryExpr and the current block in the |
| 510 | /// TempDtorContext; we start a new block, and insert the temporary |
| 511 | /// destructor call. |
| 512 | /// b) Otherwise, add the temporary destructor call to the current block. |
| 513 | /// 3. When we finished visiting a conditionally executed subexpression, |
| 514 | /// and we found at least one temporary constructor during the visitation |
| 515 | /// (2.a has executed), we insert a decision block that uses the |
| 516 | /// CXXBindTemporaryExpr as terminator, and branches to the current block |
| 517 | /// if the CXXBindTemporaryExpr was marked executed, and otherwise |
| 518 | /// branches to the stored successor. |
| 519 | struct TempDtorContext { |
NAKAMURA Takumi | 6b0fe34 | 2014-08-08 09:51:07 +0000 | [diff] [blame] | 520 | TempDtorContext() |
| 521 | : IsConditional(false), KnownExecuted(true), Succ(nullptr), |
| 522 | TerminatorExpr(nullptr) {} |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 523 | |
| 524 | TempDtorContext(TryResult KnownExecuted) |
NAKAMURA Takumi | 6b0fe34 | 2014-08-08 09:51:07 +0000 | [diff] [blame] | 525 | : IsConditional(true), KnownExecuted(KnownExecuted), Succ(nullptr), |
| 526 | TerminatorExpr(nullptr) {} |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 527 | |
| 528 | /// Returns whether we need to start a new branch for a temporary destructor |
Eric Christopher | 2c4555a | 2015-06-19 01:52:53 +0000 | [diff] [blame] | 529 | /// call. This is the case when the temporary destructor is |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 530 | /// conditionally executed, and it is the first one we encounter while |
| 531 | /// visiting a subexpression - other temporary destructors at the same level |
| 532 | /// will be added to the same block and are executed under the same |
| 533 | /// condition. |
| 534 | bool needsTempDtorBranch() const { |
| 535 | return IsConditional && !TerminatorExpr; |
| 536 | } |
| 537 | |
| 538 | /// Remember the successor S of a temporary destructor decision branch for |
| 539 | /// the corresponding CXXBindTemporaryExpr E. |
| 540 | void setDecisionPoint(CFGBlock *S, CXXBindTemporaryExpr *E) { |
| 541 | Succ = S; |
| 542 | TerminatorExpr = E; |
| 543 | } |
| 544 | |
NAKAMURA Takumi | 6b0fe34 | 2014-08-08 09:51:07 +0000 | [diff] [blame] | 545 | const bool IsConditional; |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 546 | const TryResult KnownExecuted; |
NAKAMURA Takumi | 6b0fe34 | 2014-08-08 09:51:07 +0000 | [diff] [blame] | 547 | CFGBlock *Succ; |
| 548 | CXXBindTemporaryExpr *TerminatorExpr; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 549 | }; |
| 550 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 551 | // Visitors to walk an AST and generate destructors of temporaries in |
| 552 | // full expression. |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 553 | CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary, |
| 554 | TempDtorContext &Context); |
| 555 | CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E, TempDtorContext &Context); |
| 556 | CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E, |
| 557 | TempDtorContext &Context); |
| 558 | CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors( |
| 559 | CXXBindTemporaryExpr *E, bool BindToTemporary, TempDtorContext &Context); |
| 560 | CFGBlock *VisitConditionalOperatorForTemporaryDtors( |
| 561 | AbstractConditionalOperator *E, bool BindToTemporary, |
| 562 | TempDtorContext &Context); |
| 563 | void InsertTempDtorDecisionBlock(const TempDtorContext &Context, |
| 564 | CFGBlock *FalseSucc = nullptr); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 566 | // NYS == Not Yet Supported |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 567 | CFGBlock *NYS() { |
Ted Kremenek | b64d183 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 568 | badCFG = true; |
| 569 | return Block; |
| 570 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 571 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 572 | void autoCreateBlock() { if (!Block) Block = createBlock(); } |
| 573 | CFGBlock *createBlock(bool add_successor = true); |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 574 | CFGBlock *createNoReturnBlock(); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 575 | |
Zhongxing Xu | ea9fcff | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 576 | CFGBlock *addStmt(Stmt *S) { |
| 577 | return Visit(S, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 578 | } |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 579 | CFGBlock *addInitializer(CXXCtorInitializer *I); |
Zhongxing Xu | 6d372f7 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 580 | void addAutomaticObjDtors(LocalScope::const_iterator B, |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 581 | LocalScope::const_iterator E, Stmt *S); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 582 | void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 583 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 584 | // Local scopes creation. |
| 585 | LocalScope* createOrReuseLocalScope(LocalScope* Scope); |
| 586 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 587 | void addLocalScopeForStmt(Stmt *S); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 588 | LocalScope* addLocalScopeForDeclStmt(DeclStmt *DS, |
| 589 | LocalScope* Scope = nullptr); |
| 590 | LocalScope* addLocalScopeForVarDecl(VarDecl *VD, LocalScope* Scope = nullptr); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 591 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 592 | void addLocalScopeAndDtors(Stmt *S); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 593 | |
| 594 | // Interface to CFGBlock - adding CFGElements. |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 595 | void appendStmt(CFGBlock *B, const Stmt *S) { |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 596 | if (alwaysAdd(S) && cachedEntry) |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 597 | cachedEntry->second = B; |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 598 | |
Jordy Rose | 1734737 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 599 | // All block-level expressions should have already been IgnoreParens()ed. |
| 600 | assert(!isa<Expr>(S) || cast<Expr>(S)->IgnoreParens() == S); |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 601 | B->appendStmt(const_cast<Stmt*>(S), cfg->getBumpVectorContext()); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 602 | } |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 603 | void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) { |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 604 | B->appendInitializer(I, cfg->getBumpVectorContext()); |
| 605 | } |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 606 | void appendNewAllocator(CFGBlock *B, CXXNewExpr *NE) { |
| 607 | B->appendNewAllocator(NE, cfg->getBumpVectorContext()); |
| 608 | } |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 609 | void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) { |
| 610 | B->appendBaseDtor(BS, cfg->getBumpVectorContext()); |
| 611 | } |
| 612 | void appendMemberDtor(CFGBlock *B, FieldDecl *FD) { |
| 613 | B->appendMemberDtor(FD, cfg->getBumpVectorContext()); |
| 614 | } |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 615 | void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) { |
| 616 | B->appendTemporaryDtor(E, cfg->getBumpVectorContext()); |
| 617 | } |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 618 | void appendAutomaticObjDtor(CFGBlock *B, VarDecl *VD, Stmt *S) { |
| 619 | B->appendAutomaticObjDtor(VD, S, cfg->getBumpVectorContext()); |
| 620 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 621 | |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 622 | void appendDeleteDtor(CFGBlock *B, CXXRecordDecl *RD, CXXDeleteExpr *DE) { |
| 623 | B->appendDeleteDtor(RD, DE, cfg->getBumpVectorContext()); |
| 624 | } |
| 625 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 626 | void prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk, |
Marcin Swiderski | 321a707 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 627 | LocalScope::const_iterator B, LocalScope::const_iterator E); |
| 628 | |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 629 | void addSuccessor(CFGBlock *B, CFGBlock *S, bool IsReachable = true) { |
| 630 | B->addSuccessor(CFGBlock::AdjacentBlock(S, IsReachable), |
| 631 | cfg->getBumpVectorContext()); |
| 632 | } |
| 633 | |
| 634 | /// Add a reachable successor to a block, with the alternate variant that is |
| 635 | /// unreachable. |
| 636 | void addSuccessor(CFGBlock *B, CFGBlock *ReachableBlock, CFGBlock *AltBlock) { |
| 637 | B->addSuccessor(CFGBlock::AdjacentBlock(ReachableBlock, AltBlock), |
| 638 | cfg->getBumpVectorContext()); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 639 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 641 | /// \brief Find a relational comparison with an expression evaluating to a |
| 642 | /// boolean and a constant other than 0 and 1. |
| 643 | /// e.g. if ((x < y) == 10) |
| 644 | TryResult checkIncorrectRelationalOperator(const BinaryOperator *B) { |
| 645 | const Expr *LHSExpr = B->getLHS()->IgnoreParens(); |
| 646 | const Expr *RHSExpr = B->getRHS()->IgnoreParens(); |
| 647 | |
| 648 | const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(LHSExpr); |
| 649 | const Expr *BoolExpr = RHSExpr; |
| 650 | bool IntFirst = true; |
| 651 | if (!IntLiteral) { |
| 652 | IntLiteral = dyn_cast<IntegerLiteral>(RHSExpr); |
| 653 | BoolExpr = LHSExpr; |
| 654 | IntFirst = false; |
| 655 | } |
| 656 | |
| 657 | if (!IntLiteral || !BoolExpr->isKnownToHaveBooleanValue()) |
| 658 | return TryResult(); |
| 659 | |
| 660 | llvm::APInt IntValue = IntLiteral->getValue(); |
| 661 | if ((IntValue == 1) || (IntValue == 0)) |
| 662 | return TryResult(); |
| 663 | |
| 664 | bool IntLarger = IntLiteral->getType()->isUnsignedIntegerType() || |
| 665 | !IntValue.isNegative(); |
| 666 | |
| 667 | BinaryOperatorKind Bok = B->getOpcode(); |
| 668 | if (Bok == BO_GT || Bok == BO_GE) { |
| 669 | // Always true for 10 > bool and bool > -1 |
| 670 | // Always false for -1 > bool and bool > 10 |
| 671 | return TryResult(IntFirst == IntLarger); |
| 672 | } else { |
| 673 | // Always true for -1 < bool and bool < 10 |
| 674 | // Always false for 10 < bool and bool < -1 |
| 675 | return TryResult(IntFirst != IntLarger); |
| 676 | } |
| 677 | } |
| 678 | |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 679 | /// Find an incorrect equality comparison. Either with an expression |
| 680 | /// evaluating to a boolean and a constant other than 0 and 1. |
| 681 | /// e.g. if (!x == 10) or a bitwise and/or operation that always evaluates to |
| 682 | /// true/false e.q. (x & 8) == 4. |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 683 | TryResult checkIncorrectEqualityOperator(const BinaryOperator *B) { |
| 684 | const Expr *LHSExpr = B->getLHS()->IgnoreParens(); |
| 685 | const Expr *RHSExpr = B->getRHS()->IgnoreParens(); |
| 686 | |
| 687 | const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(LHSExpr); |
| 688 | const Expr *BoolExpr = RHSExpr; |
| 689 | |
| 690 | if (!IntLiteral) { |
| 691 | IntLiteral = dyn_cast<IntegerLiteral>(RHSExpr); |
| 692 | BoolExpr = LHSExpr; |
| 693 | } |
| 694 | |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 695 | if (!IntLiteral) |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 696 | return TryResult(); |
| 697 | |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 698 | const BinaryOperator *BitOp = dyn_cast<BinaryOperator>(BoolExpr); |
| 699 | if (BitOp && (BitOp->getOpcode() == BO_And || |
| 700 | BitOp->getOpcode() == BO_Or)) { |
| 701 | const Expr *LHSExpr2 = BitOp->getLHS()->IgnoreParens(); |
| 702 | const Expr *RHSExpr2 = BitOp->getRHS()->IgnoreParens(); |
| 703 | |
| 704 | const IntegerLiteral *IntLiteral2 = dyn_cast<IntegerLiteral>(LHSExpr2); |
| 705 | |
| 706 | if (!IntLiteral2) |
| 707 | IntLiteral2 = dyn_cast<IntegerLiteral>(RHSExpr2); |
| 708 | |
| 709 | if (!IntLiteral2) |
| 710 | return TryResult(); |
| 711 | |
| 712 | llvm::APInt L1 = IntLiteral->getValue(); |
| 713 | llvm::APInt L2 = IntLiteral2->getValue(); |
| 714 | if ((BitOp->getOpcode() == BO_And && (L2 & L1) != L1) || |
| 715 | (BitOp->getOpcode() == BO_Or && (L2 | L1) != L1)) { |
| 716 | if (BuildOpts.Observer) |
| 717 | BuildOpts.Observer->compareBitwiseEquality(B, |
| 718 | B->getOpcode() != BO_EQ); |
| 719 | TryResult(B->getOpcode() != BO_EQ); |
| 720 | } |
| 721 | } else if (BoolExpr->isKnownToHaveBooleanValue()) { |
| 722 | llvm::APInt IntValue = IntLiteral->getValue(); |
| 723 | if ((IntValue == 1) || (IntValue == 0)) { |
| 724 | return TryResult(); |
| 725 | } |
| 726 | return TryResult(B->getOpcode() != BO_EQ); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 729 | return TryResult(); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | TryResult analyzeLogicOperatorCondition(BinaryOperatorKind Relation, |
| 733 | const llvm::APSInt &Value1, |
| 734 | const llvm::APSInt &Value2) { |
| 735 | assert(Value1.isSigned() == Value2.isSigned()); |
| 736 | switch (Relation) { |
| 737 | default: |
| 738 | return TryResult(); |
| 739 | case BO_EQ: |
| 740 | return TryResult(Value1 == Value2); |
| 741 | case BO_NE: |
| 742 | return TryResult(Value1 != Value2); |
| 743 | case BO_LT: |
| 744 | return TryResult(Value1 < Value2); |
| 745 | case BO_LE: |
| 746 | return TryResult(Value1 <= Value2); |
| 747 | case BO_GT: |
| 748 | return TryResult(Value1 > Value2); |
| 749 | case BO_GE: |
| 750 | return TryResult(Value1 >= Value2); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | /// \brief Find a pair of comparison expressions with or without parentheses |
| 755 | /// with a shared variable and constants and a logical operator between them |
| 756 | /// that always evaluates to either true or false. |
| 757 | /// e.g. if (x != 3 || x != 4) |
| 758 | TryResult checkIncorrectLogicOperator(const BinaryOperator *B) { |
| 759 | assert(B->isLogicalOp()); |
| 760 | const BinaryOperator *LHS = |
| 761 | dyn_cast<BinaryOperator>(B->getLHS()->IgnoreParens()); |
| 762 | const BinaryOperator *RHS = |
| 763 | dyn_cast<BinaryOperator>(B->getRHS()->IgnoreParens()); |
| 764 | if (!LHS || !RHS) |
| 765 | return TryResult(); |
| 766 | |
| 767 | if (!LHS->isComparisonOp() || !RHS->isComparisonOp()) |
| 768 | return TryResult(); |
| 769 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 770 | const DeclRefExpr *Decl1; |
| 771 | const Expr *Expr1; |
| 772 | BinaryOperatorKind BO1; |
| 773 | std::tie(Decl1, BO1, Expr1) = tryNormalizeBinaryOperator(LHS); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 774 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 775 | if (!Decl1 || !Expr1) |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 776 | return TryResult(); |
| 777 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 778 | const DeclRefExpr *Decl2; |
| 779 | const Expr *Expr2; |
| 780 | BinaryOperatorKind BO2; |
| 781 | std::tie(Decl2, BO2, Expr2) = tryNormalizeBinaryOperator(RHS); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 782 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 783 | if (!Decl2 || !Expr2) |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 784 | return TryResult(); |
| 785 | |
| 786 | // Check that it is the same variable on both sides. |
| 787 | if (Decl1->getDecl() != Decl2->getDecl()) |
| 788 | return TryResult(); |
| 789 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 790 | // Make sure the user's intent is clear (e.g. they're comparing against two |
| 791 | // int literals, or two things from the same enum) |
| 792 | if (!areExprTypesCompatible(Expr1, Expr2)) |
| 793 | return TryResult(); |
| 794 | |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 795 | llvm::APSInt L1, L2; |
| 796 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 797 | if (!Expr1->EvaluateAsInt(L1, *Context) || |
| 798 | !Expr2->EvaluateAsInt(L2, *Context)) |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 799 | return TryResult(); |
| 800 | |
| 801 | // Can't compare signed with unsigned or with different bit width. |
| 802 | if (L1.isSigned() != L2.isSigned() || L1.getBitWidth() != L2.getBitWidth()) |
| 803 | return TryResult(); |
| 804 | |
| 805 | // Values that will be used to determine if result of logical |
| 806 | // operator is always true/false |
| 807 | const llvm::APSInt Values[] = { |
| 808 | // Value less than both Value1 and Value2 |
| 809 | llvm::APSInt::getMinValue(L1.getBitWidth(), L1.isUnsigned()), |
| 810 | // L1 |
| 811 | L1, |
| 812 | // Value between Value1 and Value2 |
| 813 | ((L1 < L2) ? L1 : L2) + llvm::APSInt(llvm::APInt(L1.getBitWidth(), 1), |
| 814 | L1.isUnsigned()), |
| 815 | // L2 |
| 816 | L2, |
| 817 | // Value greater than both Value1 and Value2 |
| 818 | llvm::APSInt::getMaxValue(L1.getBitWidth(), L1.isUnsigned()), |
| 819 | }; |
| 820 | |
| 821 | // Check whether expression is always true/false by evaluating the following |
| 822 | // * variable x is less than the smallest literal. |
| 823 | // * variable x is equal to the smallest literal. |
| 824 | // * Variable x is between smallest and largest literal. |
| 825 | // * Variable x is equal to the largest literal. |
| 826 | // * Variable x is greater than largest literal. |
| 827 | bool AlwaysTrue = true, AlwaysFalse = true; |
Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 828 | for (const llvm::APSInt &Value : Values) { |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 829 | TryResult Res1, Res2; |
| 830 | Res1 = analyzeLogicOperatorCondition(BO1, Value, L1); |
| 831 | Res2 = analyzeLogicOperatorCondition(BO2, Value, L2); |
| 832 | |
| 833 | if (!Res1.isKnown() || !Res2.isKnown()) |
| 834 | return TryResult(); |
| 835 | |
| 836 | if (B->getOpcode() == BO_LAnd) { |
| 837 | AlwaysTrue &= (Res1.isTrue() && Res2.isTrue()); |
| 838 | AlwaysFalse &= !(Res1.isTrue() && Res2.isTrue()); |
| 839 | } else { |
| 840 | AlwaysTrue &= (Res1.isTrue() || Res2.isTrue()); |
| 841 | AlwaysFalse &= !(Res1.isTrue() || Res2.isTrue()); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | if (AlwaysTrue || AlwaysFalse) { |
| 846 | if (BuildOpts.Observer) |
| 847 | BuildOpts.Observer->compareAlwaysTrue(B, AlwaysTrue); |
| 848 | return TryResult(AlwaysTrue); |
| 849 | } |
| 850 | return TryResult(); |
| 851 | } |
| 852 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 853 | /// Try and evaluate an expression to an integer constant. |
| 854 | bool tryEvaluate(Expr *S, Expr::EvalResult &outResult) { |
| 855 | if (!BuildOpts.PruneTriviallyFalseEdges) |
| 856 | return false; |
| 857 | return !S->isTypeDependent() && |
Ted Kremenek | 352a708 | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 858 | !S->isValueDependent() && |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 859 | S->EvaluateAsRValue(outResult, *Context); |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 860 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 862 | /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1 |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 863 | /// if we can evaluate to a known value, otherwise return -1. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 864 | TryResult tryEvaluateBool(Expr *S) { |
Richard Smith | faa32a9 | 2011-10-14 20:22:00 +0000 | [diff] [blame] | 865 | if (!BuildOpts.PruneTriviallyFalseEdges || |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 866 | S->isTypeDependent() || S->isValueDependent()) |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 867 | return TryResult(); |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 868 | |
| 869 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(S)) { |
| 870 | if (Bop->isLogicalOp()) { |
| 871 | // Check the cache first. |
NAKAMURA Takumi | e9ca55e | 2012-03-25 06:30:37 +0000 | [diff] [blame] | 872 | CachedBoolEvalsTy::iterator I = CachedBoolEvals.find(S); |
| 873 | if (I != CachedBoolEvals.end()) |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 874 | return I->second; // already in map; |
NAKAMURA Takumi | f0434b0 | 2012-03-25 06:30:32 +0000 | [diff] [blame] | 875 | |
| 876 | // Retrieve result at first, or the map might be updated. |
| 877 | TryResult Result = evaluateAsBooleanConditionNoCache(S); |
| 878 | CachedBoolEvals[S] = Result; // update or insert |
| 879 | return Result; |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 880 | } |
Ted Kremenek | 64fea5f | 2012-08-24 07:42:09 +0000 | [diff] [blame] | 881 | else { |
| 882 | switch (Bop->getOpcode()) { |
| 883 | default: break; |
| 884 | // For 'x & 0' and 'x * 0', we can determine that |
| 885 | // the value is always false. |
| 886 | case BO_Mul: |
| 887 | case BO_And: { |
| 888 | // If either operand is zero, we know the value |
| 889 | // must be false. |
| 890 | llvm::APSInt IntVal; |
| 891 | if (Bop->getLHS()->EvaluateAsInt(IntVal, *Context)) { |
David Blaikie | 7a3cbb2 | 2015-03-09 02:02:07 +0000 | [diff] [blame] | 892 | if (!IntVal.getBoolValue()) { |
Ted Kremenek | 64fea5f | 2012-08-24 07:42:09 +0000 | [diff] [blame] | 893 | return TryResult(false); |
| 894 | } |
| 895 | } |
| 896 | if (Bop->getRHS()->EvaluateAsInt(IntVal, *Context)) { |
David Blaikie | 7a3cbb2 | 2015-03-09 02:02:07 +0000 | [diff] [blame] | 897 | if (!IntVal.getBoolValue()) { |
Ted Kremenek | 64fea5f | 2012-08-24 07:42:09 +0000 | [diff] [blame] | 898 | return TryResult(false); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | break; |
| 903 | } |
| 904 | } |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | return evaluateAsBooleanConditionNoCache(S); |
| 908 | } |
| 909 | |
| 910 | /// \brief Evaluate as boolean \param E without using the cache. |
| 911 | TryResult evaluateAsBooleanConditionNoCache(Expr *E) { |
| 912 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(E)) { |
| 913 | if (Bop->isLogicalOp()) { |
| 914 | TryResult LHS = tryEvaluateBool(Bop->getLHS()); |
| 915 | if (LHS.isKnown()) { |
| 916 | // We were able to evaluate the LHS, see if we can get away with not |
| 917 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
| 918 | if (LHS.isTrue() == (Bop->getOpcode() == BO_LOr)) |
| 919 | return LHS.isTrue(); |
| 920 | |
| 921 | TryResult RHS = tryEvaluateBool(Bop->getRHS()); |
| 922 | if (RHS.isKnown()) { |
| 923 | if (Bop->getOpcode() == BO_LOr) |
| 924 | return LHS.isTrue() || RHS.isTrue(); |
| 925 | else |
| 926 | return LHS.isTrue() && RHS.isTrue(); |
| 927 | } |
| 928 | } else { |
| 929 | TryResult RHS = tryEvaluateBool(Bop->getRHS()); |
| 930 | if (RHS.isKnown()) { |
| 931 | // We can't evaluate the LHS; however, sometimes the result |
| 932 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 933 | if (RHS.isTrue() == (Bop->getOpcode() == BO_LOr)) |
| 934 | return RHS.isTrue(); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 935 | } else { |
| 936 | TryResult BopRes = checkIncorrectLogicOperator(Bop); |
| 937 | if (BopRes.isKnown()) |
| 938 | return BopRes.isTrue(); |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | return TryResult(); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 943 | } else if (Bop->isEqualityOp()) { |
| 944 | TryResult BopRes = checkIncorrectEqualityOperator(Bop); |
| 945 | if (BopRes.isKnown()) |
| 946 | return BopRes.isTrue(); |
| 947 | } else if (Bop->isRelationalOp()) { |
| 948 | TryResult BopRes = checkIncorrectRelationalOperator(Bop); |
| 949 | if (BopRes.isKnown()) |
| 950 | return BopRes.isTrue(); |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
| 954 | bool Result; |
| 955 | if (E->EvaluateAsBooleanCondition(Result, *Context)) |
| 956 | return Result; |
| 957 | |
| 958 | return TryResult(); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 959 | } |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 960 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 961 | }; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 962 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 963 | inline bool AddStmtChoice::alwaysAdd(CFGBuilder &builder, |
| 964 | const Stmt *stmt) const { |
| 965 | return builder.alwaysAdd(stmt) || kind == AlwaysAdd; |
| 966 | } |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 967 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 968 | bool CFGBuilder::alwaysAdd(const Stmt *stmt) { |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 969 | bool shouldAdd = BuildOpts.alwaysAdd(stmt); |
| 970 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 971 | if (!BuildOpts.forcedBlkExprs) |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 972 | return shouldAdd; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 973 | |
| 974 | if (lastLookup == stmt) { |
| 975 | if (cachedEntry) { |
| 976 | assert(cachedEntry->first == stmt); |
| 977 | return true; |
| 978 | } |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 979 | return shouldAdd; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 980 | } |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 981 | |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 982 | lastLookup = stmt; |
| 983 | |
| 984 | // Perform the lookup! |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 985 | CFG::BuildOptions::ForcedBlkExprs *fb = *BuildOpts.forcedBlkExprs; |
| 986 | |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 987 | if (!fb) { |
| 988 | // No need to update 'cachedEntry', since it will always be null. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 989 | assert(!cachedEntry); |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 990 | return shouldAdd; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 991 | } |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 992 | |
| 993 | CFG::BuildOptions::ForcedBlkExprs::iterator itr = fb->find(stmt); |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 994 | if (itr == fb->end()) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 995 | cachedEntry = nullptr; |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 996 | return shouldAdd; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 999 | cachedEntry = &*itr; |
| 1000 | return true; |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1003 | // FIXME: Add support for dependent-sized array types in C++? |
| 1004 | // Does it even make sense to build a CFG for an uninstantiated template? |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1005 | static const VariableArrayType *FindVA(const Type *t) { |
| 1006 | while (const ArrayType *vt = dyn_cast<ArrayType>(t)) { |
| 1007 | if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt)) |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 1008 | if (vat->getSizeExpr()) |
| 1009 | return vat; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1010 | |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 1011 | t = vt->getElementType().getTypePtr(); |
| 1012 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1013 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1014 | return nullptr; |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 1015 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1016 | |
| 1017 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an |
| 1018 | /// arbitrary statement. Examples include a single expression or a function |
| 1019 | /// body (compound statement). The ownership of the returned CFG is |
| 1020 | /// transferred to the caller. If CFG construction fails, this method returns |
| 1021 | /// NULL. |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 1022 | std::unique_ptr<CFG> CFGBuilder::buildCFG(const Decl *D, Stmt *Statement) { |
Ted Kremenek | 8aed490 | 2009-10-20 23:46:25 +0000 | [diff] [blame] | 1023 | assert(cfg.get()); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1024 | if (!Statement) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1025 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1026 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1027 | // Create an empty block that will serve as the exit block for the CFG. Since |
| 1028 | // this is the first block added to the CFG, it will be implicitly registered |
| 1029 | // as the exit block. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1030 | Succ = createBlock(); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1031 | assert(Succ == &cfg->getExit()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1032 | Block = nullptr; // the EXIT block is empty. Create all other blocks lazily. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1033 | |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1034 | if (BuildOpts.AddImplicitDtors) |
| 1035 | if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D)) |
| 1036 | addImplicitDtorsForDestructor(DD); |
| 1037 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1038 | // Visit the statements and create the CFG. |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1039 | CFGBlock *B = addStmt(Statement); |
| 1040 | |
| 1041 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1042 | return nullptr; |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1043 | |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1044 | // For C++ constructor add initializers to CFG. |
| 1045 | if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 1046 | for (auto *I : llvm::reverse(CD->inits())) { |
| 1047 | B = addInitializer(I); |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1048 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1049 | return nullptr; |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1053 | if (B) |
| 1054 | Succ = B; |
Mike Stump | 6bf1c08 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 1055 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1056 | // Backpatch the gotos whose label -> block mappings we didn't know when we |
| 1057 | // encountered them. |
| 1058 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 1059 | E = BackpatchBlocks.end(); I != E; ++I ) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1060 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1061 | CFGBlock *B = I->block; |
Rafael Espindola | 210de57 | 2013-03-27 15:37:54 +0000 | [diff] [blame] | 1062 | const GotoStmt *G = cast<GotoStmt>(B->getTerminator()); |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1063 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1064 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1065 | // If there is no target for the goto, then we are looking at an |
| 1066 | // incomplete AST. Handle this by not registering a successor. |
| 1067 | if (LI == LabelMap.end()) continue; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1068 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1069 | JumpTarget JT = LI->second; |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 1070 | prependAutomaticObjDtorsWithTerminator(B, I->scopePosition, |
| 1071 | JT.scopePosition); |
| 1072 | addSuccessor(B, JT.block); |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | // Add successors to the Indirect Goto Dispatch block (if we have one). |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1076 | if (CFGBlock *B = cfg->getIndirectGotoBlock()) |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1077 | for (LabelSetTy::iterator I = AddressTakenLabels.begin(), |
| 1078 | E = AddressTakenLabels.end(); I != E; ++I ) { |
| 1079 | |
| 1080 | // Lookup the target block. |
| 1081 | LabelMapTy::iterator LI = LabelMap.find(*I); |
| 1082 | |
| 1083 | // If there is no target block that contains label, then we are looking |
| 1084 | // at an incomplete AST. Handle this by not registering a successor. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1085 | if (LI == LabelMap.end()) continue; |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1086 | |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 1087 | addSuccessor(B, LI->second.block); |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1088 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1089 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1090 | // Create an empty entry block that has no predecessors. |
Ted Kremenek | 5c50fd1 | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 1091 | cfg->setEntry(createBlock()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1092 | |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 1093 | return std::move(cfg); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1094 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1095 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1096 | /// createBlock - Used to lazily create blocks that are connected |
| 1097 | /// to the current (global) succcessor. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1098 | CFGBlock *CFGBuilder::createBlock(bool add_successor) { |
| 1099 | CFGBlock *B = cfg->createBlock(); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1100 | if (add_successor && Succ) |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 1101 | addSuccessor(B, Succ); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1102 | return B; |
| 1103 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1104 | |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 1105 | /// createNoReturnBlock - Used to create a block is a 'noreturn' point in the |
| 1106 | /// CFG. It is *not* connected to the current (global) successor, and instead |
| 1107 | /// directly tied to the exit block in order to be reachable. |
| 1108 | CFGBlock *CFGBuilder::createNoReturnBlock() { |
| 1109 | CFGBlock *B = createBlock(false); |
Chandler Carruth | 75d7823 | 2011-09-13 09:53:55 +0000 | [diff] [blame] | 1110 | B->setHasNoReturnElement(); |
Ted Kremenek | f353919 | 2014-02-27 00:24:05 +0000 | [diff] [blame] | 1111 | addSuccessor(B, &cfg->getExit(), Succ); |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 1112 | return B; |
| 1113 | } |
| 1114 | |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1115 | /// addInitializer - Add C++ base or member initializer element to CFG. |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1116 | CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) { |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1117 | if (!BuildOpts.AddInitializers) |
| 1118 | return Block; |
| 1119 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1120 | bool HasTemporaries = false; |
| 1121 | |
| 1122 | // Destructors of temporaries in initialization expression should be called |
| 1123 | // after initialization finishes. |
| 1124 | Expr *Init = I->getInit(); |
| 1125 | if (Init) { |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1126 | HasTemporaries = isa<ExprWithCleanups>(Init); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1127 | |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 1128 | if (BuildOpts.AddTemporaryDtors && HasTemporaries) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1129 | // Generate destructors for temporaries in initialization expression. |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 1130 | TempDtorContext Context; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 1131 | VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(), |
| 1132 | /*BindToTemporary=*/false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1136 | autoCreateBlock(); |
| 1137 | appendInitializer(Block, I); |
| 1138 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1139 | if (Init) { |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1140 | if (HasTemporaries) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1141 | // For expression with temporaries go directly to subexpression to omit |
| 1142 | // generating destructors for the second time. |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1143 | return Visit(cast<ExprWithCleanups>(Init)->getSubExpr()); |
| 1144 | } |
Enrico Pertoso | faed801 | 2015-06-03 10:12:40 +0000 | [diff] [blame] | 1145 | if (BuildOpts.AddCXXDefaultInitExprInCtors) { |
| 1146 | if (CXXDefaultInitExpr *Default = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 1147 | // In general, appending the expression wrapped by a CXXDefaultInitExpr |
| 1148 | // may cause the same Expr to appear more than once in the CFG. Doing it |
| 1149 | // here is safe because there's only one initializer per field. |
| 1150 | autoCreateBlock(); |
| 1151 | appendStmt(Block, Default); |
| 1152 | if (Stmt *Child = Default->getExpr()) |
| 1153 | if (CFGBlock *R = Visit(Child)) |
| 1154 | Block = R; |
| 1155 | return Block; |
| 1156 | } |
| 1157 | } |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1158 | return Visit(Init); |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1159 | } |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1160 | |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1161 | return Block; |
| 1162 | } |
| 1163 | |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1164 | /// \brief Retrieve the type of the temporary object whose lifetime was |
| 1165 | /// extended by a local reference with the given initializer. |
| 1166 | static QualType getReferenceInitTemporaryType(ASTContext &Context, |
| 1167 | const Expr *Init) { |
| 1168 | while (true) { |
| 1169 | // Skip parentheses. |
| 1170 | Init = Init->IgnoreParens(); |
| 1171 | |
| 1172 | // Skip through cleanups. |
| 1173 | if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init)) { |
| 1174 | Init = EWC->getSubExpr(); |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | // Skip through the temporary-materialization expression. |
| 1179 | if (const MaterializeTemporaryExpr *MTE |
| 1180 | = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 1181 | Init = MTE->GetTemporaryExpr(); |
| 1182 | continue; |
| 1183 | } |
| 1184 | |
| 1185 | // Skip derived-to-base and no-op casts. |
| 1186 | if (const CastExpr *CE = dyn_cast<CastExpr>(Init)) { |
| 1187 | if ((CE->getCastKind() == CK_DerivedToBase || |
| 1188 | CE->getCastKind() == CK_UncheckedDerivedToBase || |
| 1189 | CE->getCastKind() == CK_NoOp) && |
| 1190 | Init->getType()->isRecordType()) { |
| 1191 | Init = CE->getSubExpr(); |
| 1192 | continue; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | // Skip member accesses into rvalues. |
| 1197 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Init)) { |
| 1198 | if (!ME->isArrow() && ME->getBase()->isRValue()) { |
| 1199 | Init = ME->getBase(); |
| 1200 | continue; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | break; |
| 1205 | } |
| 1206 | |
| 1207 | return Init->getType(); |
| 1208 | } |
| 1209 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1210 | /// addAutomaticObjDtors - Add to current block automatic objects destructors |
| 1211 | /// for objects in range of local scope positions. Use S as trigger statement |
| 1212 | /// for destructors. |
Zhongxing Xu | 6d372f7 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 1213 | void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B, |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1214 | LocalScope::const_iterator E, Stmt *S) { |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1215 | if (!BuildOpts.AddImplicitDtors) |
Zhongxing Xu | 6d372f7 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 1216 | return; |
| 1217 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1218 | if (B == E) |
Zhongxing Xu | 6d372f7 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 1219 | return; |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1220 | |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1221 | // We need to append the destructors in reverse order, but any one of them |
| 1222 | // may be a no-return destructor which changes the CFG. As a result, buffer |
| 1223 | // this sequence up and replay them in reverse order when appending onto the |
| 1224 | // CFGBlock(s). |
| 1225 | SmallVector<VarDecl*, 10> Decls; |
| 1226 | Decls.reserve(B.distance(E)); |
| 1227 | for (LocalScope::const_iterator I = B; I != E; ++I) |
| 1228 | Decls.push_back(*I); |
| 1229 | |
| 1230 | for (SmallVectorImpl<VarDecl*>::reverse_iterator I = Decls.rbegin(), |
| 1231 | E = Decls.rend(); |
| 1232 | I != E; ++I) { |
| 1233 | // If this destructor is marked as a no-return destructor, we need to |
| 1234 | // create a new block for the destructor which does not have as a successor |
| 1235 | // anything built thus far: control won't flow out of this block. |
Ted Kremenek | 3d61773 | 2012-07-18 04:57:57 +0000 | [diff] [blame] | 1236 | QualType Ty = (*I)->getType(); |
| 1237 | if (Ty->isReferenceType()) { |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1238 | Ty = getReferenceInitTemporaryType(*Context, (*I)->getInit()); |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1239 | } |
Ted Kremenek | 3d61773 | 2012-07-18 04:57:57 +0000 | [diff] [blame] | 1240 | Ty = Context->getBaseElementType(Ty); |
| 1241 | |
Richard Trieu | 95a192a | 2015-05-28 00:14:02 +0000 | [diff] [blame] | 1242 | if (Ty->getAsCXXRecordDecl()->isAnyDestructorNoReturn()) |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 1243 | Block = createNoReturnBlock(); |
| 1244 | else |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1245 | autoCreateBlock(); |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1246 | |
| 1247 | appendAutomaticObjDtor(Block, *I, S); |
| 1248 | } |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1251 | /// addImplicitDtorsForDestructor - Add implicit destructors generated for |
| 1252 | /// base and member objects in destructor. |
| 1253 | void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) { |
| 1254 | assert (BuildOpts.AddImplicitDtors |
| 1255 | && "Can be called only when dtors should be added"); |
| 1256 | const CXXRecordDecl *RD = DD->getParent(); |
| 1257 | |
| 1258 | // At the end destroy virtual base objects. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 1259 | for (const auto &VI : RD->vbases()) { |
| 1260 | const CXXRecordDecl *CD = VI.getType()->getAsCXXRecordDecl(); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1261 | if (!CD->hasTrivialDestructor()) { |
| 1262 | autoCreateBlock(); |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 1263 | appendBaseDtor(Block, &VI); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | // Before virtual bases destroy direct base objects. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1268 | for (const auto &BI : RD->bases()) { |
| 1269 | if (!BI.isVirtual()) { |
| 1270 | const CXXRecordDecl *CD = BI.getType()->getAsCXXRecordDecl(); |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1271 | if (!CD->hasTrivialDestructor()) { |
| 1272 | autoCreateBlock(); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1273 | appendBaseDtor(Block, &BI); |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1274 | } |
| 1275 | } |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | // First destroy member objects. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1279 | for (auto *FI : RD->fields()) { |
Marcin Swiderski | 0176990 | 2010-10-25 07:05:54 +0000 | [diff] [blame] | 1280 | // Check for constant size array. Set type to array element type. |
| 1281 | QualType QT = FI->getType(); |
| 1282 | if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) { |
| 1283 | if (AT->getSize() == 0) |
| 1284 | continue; |
| 1285 | QT = AT->getElementType(); |
| 1286 | } |
| 1287 | |
| 1288 | if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl()) |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1289 | if (!CD->hasTrivialDestructor()) { |
| 1290 | autoCreateBlock(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1291 | appendMemberDtor(Block, FI); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1296 | /// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either |
| 1297 | /// way return valid LocalScope object. |
| 1298 | LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) { |
David Blaikie | c1334cc | 2015-08-13 22:12:21 +0000 | [diff] [blame] | 1299 | if (Scope) |
| 1300 | return Scope; |
| 1301 | llvm::BumpPtrAllocator &alloc = cfg->getAllocator(); |
| 1302 | return new (alloc.Allocate<LocalScope>()) |
| 1303 | LocalScope(BumpVectorContext(alloc), ScopePos); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | /// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement |
Zhongxing Xu | 81714f2 | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 1307 | /// that should create implicit scope (e.g. if/else substatements). |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1308 | void CFGBuilder::addLocalScopeForStmt(Stmt *S) { |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1309 | if (!BuildOpts.AddImplicitDtors) |
Zhongxing Xu | 81714f2 | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 1310 | return; |
| 1311 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1312 | LocalScope *Scope = nullptr; |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1313 | |
| 1314 | // For compound statement we will be creating explicit scope. |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1315 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) { |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 1316 | for (auto *BI : CS->body()) { |
| 1317 | Stmt *SI = BI->stripLabelLikeStatements(); |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1318 | if (DeclStmt *DS = dyn_cast<DeclStmt>(SI)) |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1319 | Scope = addLocalScopeForDeclStmt(DS, Scope); |
| 1320 | } |
Zhongxing Xu | 81714f2 | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 1321 | return; |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | // For any other statement scope will be implicit and as such will be |
| 1325 | // interesting only for DeclStmt. |
Chandler Carruth | a626d64 | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 1326 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->stripLabelLikeStatements())) |
Zhongxing Xu | 307701e | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 1327 | addLocalScopeForDeclStmt(DS); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will |
| 1331 | /// reuse Scope if not NULL. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1332 | LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt *DS, |
Zhongxing Xu | 307701e | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 1333 | LocalScope* Scope) { |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1334 | if (!BuildOpts.AddImplicitDtors) |
| 1335 | return Scope; |
| 1336 | |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 1337 | for (auto *DI : DS->decls()) |
| 1338 | if (VarDecl *VD = dyn_cast<VarDecl>(DI)) |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1339 | Scope = addLocalScopeForVarDecl(VD, Scope); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1340 | return Scope; |
| 1341 | } |
| 1342 | |
| 1343 | /// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will |
| 1344 | /// create add scope for automatic objects and temporary objects bound to |
| 1345 | /// const reference. Will reuse Scope if not NULL. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1346 | LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD, |
Zhongxing Xu | 307701e | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 1347 | LocalScope* Scope) { |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1348 | if (!BuildOpts.AddImplicitDtors) |
| 1349 | return Scope; |
| 1350 | |
| 1351 | // Check if variable is local. |
| 1352 | switch (VD->getStorageClass()) { |
| 1353 | case SC_None: |
| 1354 | case SC_Auto: |
| 1355 | case SC_Register: |
| 1356 | break; |
| 1357 | default: return Scope; |
| 1358 | } |
| 1359 | |
| 1360 | // Check for const references bound to temporary. Set type to pointee. |
| 1361 | QualType QT = VD->getType(); |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1362 | if (QT.getTypePtr()->isReferenceType()) { |
Richard Smith | 5a0ef78 | 2013-06-27 21:43:17 +0000 | [diff] [blame] | 1363 | // Attempt to determine whether this declaration lifetime-extends a |
| 1364 | // temporary. |
| 1365 | // |
| 1366 | // FIXME: This is incorrect. Non-reference declarations can lifetime-extend |
| 1367 | // temporaries, and a single declaration can extend multiple temporaries. |
| 1368 | // We should look at the storage duration on each nested |
| 1369 | // MaterializeTemporaryExpr instead. |
| 1370 | const Expr *Init = VD->getInit(); |
| 1371 | if (!Init) |
| 1372 | return Scope; |
| 1373 | if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init)) |
| 1374 | Init = EWC->getSubExpr(); |
| 1375 | if (!isa<MaterializeTemporaryExpr>(Init)) |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1376 | return Scope; |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1377 | |
Richard Smith | 5a0ef78 | 2013-06-27 21:43:17 +0000 | [diff] [blame] | 1378 | // Lifetime-extending a temporary. |
| 1379 | QT = getReferenceInitTemporaryType(*Context, Init); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Marcin Swiderski | 52e4bc1 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 1382 | // Check for constant size array. Set type to array element type. |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1383 | while (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) { |
Marcin Swiderski | 52e4bc1 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 1384 | if (AT->getSize() == 0) |
| 1385 | return Scope; |
| 1386 | QT = AT->getElementType(); |
| 1387 | } |
Zhongxing Xu | 614e17d | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 1388 | |
Marcin Swiderski | 52e4bc1 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 1389 | // Check if type is a C++ class with non-trivial destructor. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1390 | if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl()) |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1391 | if (!CD->hasTrivialDestructor()) { |
Zhongxing Xu | 614e17d | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 1392 | // Add the variable to scope |
| 1393 | Scope = createOrReuseLocalScope(Scope); |
| 1394 | Scope->addVar(VD); |
| 1395 | ScopePos = Scope->begin(); |
| 1396 | } |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1397 | return Scope; |
| 1398 | } |
| 1399 | |
| 1400 | /// addLocalScopeAndDtors - For given statement add local scope for it and |
| 1401 | /// add destructors that will cleanup the scope. Will reuse Scope if not NULL. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1402 | void CFGBuilder::addLocalScopeAndDtors(Stmt *S) { |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1403 | if (!BuildOpts.AddImplicitDtors) |
| 1404 | return; |
| 1405 | |
| 1406 | LocalScope::const_iterator scopeBeginPos = ScopePos; |
Zhongxing Xu | 81714f2 | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 1407 | addLocalScopeForStmt(S); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1408 | addAutomaticObjDtors(ScopePos, scopeBeginPos, S); |
| 1409 | } |
| 1410 | |
Marcin Swiderski | 321a707 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 1411 | /// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for |
| 1412 | /// variables with automatic storage duration to CFGBlock's elements vector. |
| 1413 | /// Elements will be prepended to physical beginning of the vector which |
| 1414 | /// happens to be logical end. Use blocks terminator as statement that specifies |
| 1415 | /// destructors call site. |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1416 | /// FIXME: This mechanism for adding automatic destructors doesn't handle |
| 1417 | /// no-return destructors properly. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1418 | void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk, |
Marcin Swiderski | 321a707 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 1419 | LocalScope::const_iterator B, LocalScope::const_iterator E) { |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1420 | BumpVectorContext &C = cfg->getBumpVectorContext(); |
| 1421 | CFGBlock::iterator InsertPos |
| 1422 | = Blk->beginAutomaticObjDtorsInsert(Blk->end(), B.distance(E), C); |
| 1423 | for (LocalScope::const_iterator I = B; I != E; ++I) |
| 1424 | InsertPos = Blk->insertAutomaticObjDtor(InsertPos, *I, |
| 1425 | Blk->getTerminator()); |
Marcin Swiderski | 321a707 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1428 | /// Visit - Walk the subtree of a statement and add extra |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1429 | /// blocks for ternary operators, &&, and ||. We also process "," and |
| 1430 | /// DeclStmts (which may contain nested control-flow). |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1431 | CFGBlock *CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) { |
Ted Kremenek | bc1416d | 2010-04-30 22:25:53 +0000 | [diff] [blame] | 1432 | if (!S) { |
| 1433 | badCFG = true; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1434 | return nullptr; |
Ted Kremenek | bc1416d | 2010-04-30 22:25:53 +0000 | [diff] [blame] | 1435 | } |
Jordy Rose | 1734737 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 1436 | |
| 1437 | if (Expr *E = dyn_cast<Expr>(S)) |
| 1438 | S = E->IgnoreParens(); |
| 1439 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1440 | switch (S->getStmtClass()) { |
| 1441 | default: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1442 | return VisitStmt(S, asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1443 | |
| 1444 | case Stmt::AddrLabelExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1445 | return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1446 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1447 | case Stmt::BinaryConditionalOperatorClass: |
| 1448 | return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc); |
| 1449 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1450 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1451 | return VisitBinaryOperator(cast<BinaryOperator>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1453 | case Stmt::BlockExprClass: |
Devin Coughlin | b6029b7 | 2015-11-25 22:35:37 +0000 | [diff] [blame] | 1454 | return VisitBlockExpr(cast<BlockExpr>(S), asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1455 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1456 | case Stmt::BreakStmtClass: |
| 1457 | return VisitBreakStmt(cast<BreakStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1459 | case Stmt::CallExprClass: |
Ted Kremenek | 128d04d | 2010-08-31 18:47:34 +0000 | [diff] [blame] | 1460 | case Stmt::CXXOperatorCallExprClass: |
John McCall | c67067f | 2011-05-11 07:19:11 +0000 | [diff] [blame] | 1461 | case Stmt::CXXMemberCallExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1462 | case Stmt::UserDefinedLiteralClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1463 | return VisitCallExpr(cast<CallExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1465 | case Stmt::CaseStmtClass: |
| 1466 | return VisitCaseStmt(cast<CaseStmt>(S)); |
| 1467 | |
| 1468 | case Stmt::ChooseExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1469 | return VisitChooseExpr(cast<ChooseExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1471 | case Stmt::CompoundStmtClass: |
| 1472 | return VisitCompoundStmt(cast<CompoundStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1474 | case Stmt::ConditionalOperatorClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1475 | return VisitConditionalOperator(cast<ConditionalOperator>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1477 | case Stmt::ContinueStmtClass: |
| 1478 | return VisitContinueStmt(cast<ContinueStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 1480 | case Stmt::CXXCatchStmtClass: |
| 1481 | return VisitCXXCatchStmt(cast<CXXCatchStmt>(S)); |
| 1482 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1483 | case Stmt::ExprWithCleanupsClass: |
| 1484 | return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc); |
Ted Kremenek | 82bfc86 | 2010-08-28 00:19:02 +0000 | [diff] [blame] | 1485 | |
Jordan Rose | e5d5393 | 2012-08-23 18:10:53 +0000 | [diff] [blame] | 1486 | case Stmt::CXXDefaultArgExprClass: |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1487 | case Stmt::CXXDefaultInitExprClass: |
Jordan Rose | e5d5393 | 2012-08-23 18:10:53 +0000 | [diff] [blame] | 1488 | // FIXME: The expression inside a CXXDefaultArgExpr is owned by the |
| 1489 | // called function's declaration, not by the caller. If we simply add |
| 1490 | // this expression to the CFG, we could end up with the same Expr |
| 1491 | // appearing multiple times. |
| 1492 | // PR13385 / <rdar://problem/12156507> |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1493 | // |
| 1494 | // It's likewise possible for multiple CXXDefaultInitExprs for the same |
| 1495 | // expression to be used in the same function (through aggregate |
| 1496 | // initialization). |
Jordan Rose | e5d5393 | 2012-08-23 18:10:53 +0000 | [diff] [blame] | 1497 | return VisitStmt(S, asc); |
| 1498 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 1499 | case Stmt::CXXBindTemporaryExprClass: |
| 1500 | return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc); |
| 1501 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 1502 | case Stmt::CXXConstructExprClass: |
| 1503 | return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc); |
| 1504 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 1505 | case Stmt::CXXNewExprClass: |
| 1506 | return VisitCXXNewExpr(cast<CXXNewExpr>(S), asc); |
| 1507 | |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 1508 | case Stmt::CXXDeleteExprClass: |
| 1509 | return VisitCXXDeleteExpr(cast<CXXDeleteExpr>(S), asc); |
| 1510 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 1511 | case Stmt::CXXFunctionalCastExprClass: |
| 1512 | return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc); |
| 1513 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 1514 | case Stmt::CXXTemporaryObjectExprClass: |
| 1515 | return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc); |
| 1516 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 1517 | case Stmt::CXXThrowExprClass: |
| 1518 | return VisitCXXThrowExpr(cast<CXXThrowExpr>(S)); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1519 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 1520 | case Stmt::CXXTryStmtClass: |
| 1521 | return VisitCXXTryStmt(cast<CXXTryStmt>(S)); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1522 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1523 | case Stmt::CXXForRangeStmtClass: |
| 1524 | return VisitCXXForRangeStmt(cast<CXXForRangeStmt>(S)); |
| 1525 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1526 | case Stmt::DeclStmtClass: |
| 1527 | return VisitDeclStmt(cast<DeclStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1529 | case Stmt::DefaultStmtClass: |
| 1530 | return VisitDefaultStmt(cast<DefaultStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1532 | case Stmt::DoStmtClass: |
| 1533 | return VisitDoStmt(cast<DoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1535 | case Stmt::ForStmtClass: |
| 1536 | return VisitForStmt(cast<ForStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1538 | case Stmt::GotoStmtClass: |
| 1539 | return VisitGotoStmt(cast<GotoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1541 | case Stmt::IfStmtClass: |
| 1542 | return VisitIfStmt(cast<IfStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1544 | case Stmt::ImplicitCastExprClass: |
| 1545 | return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 1546 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1547 | case Stmt::IndirectGotoStmtClass: |
| 1548 | return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1550 | case Stmt::LabelStmtClass: |
| 1551 | return VisitLabelStmt(cast<LabelStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Ted Kremenek | da76a94 | 2012-04-12 20:34:52 +0000 | [diff] [blame] | 1553 | case Stmt::LambdaExprClass: |
| 1554 | return VisitLambdaExpr(cast<LambdaExpr>(S), asc); |
| 1555 | |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 1556 | case Stmt::MemberExprClass: |
| 1557 | return VisitMemberExpr(cast<MemberExpr>(S), asc); |
| 1558 | |
Ted Kremenek | 0426823 | 2011-11-05 00:10:15 +0000 | [diff] [blame] | 1559 | case Stmt::NullStmtClass: |
| 1560 | return Block; |
| 1561 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1562 | case Stmt::ObjCAtCatchStmtClass: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S)); |
| 1564 | |
Ted Kremenek | 5022f1d | 2012-03-06 23:40:47 +0000 | [diff] [blame] | 1565 | case Stmt::ObjCAutoreleasePoolStmtClass: |
| 1566 | return VisitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(S)); |
| 1567 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1568 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 1569 | return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1571 | case Stmt::ObjCAtThrowStmtClass: |
| 1572 | return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1574 | case Stmt::ObjCAtTryStmtClass: |
| 1575 | return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1577 | case Stmt::ObjCForCollectionStmtClass: |
| 1578 | return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
Ted Kremenek | 0426823 | 2011-11-05 00:10:15 +0000 | [diff] [blame] | 1580 | case Stmt::OpaqueValueExprClass: |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1581 | return Block; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1583 | case Stmt::PseudoObjectExprClass: |
| 1584 | return VisitPseudoObjectExpr(cast<PseudoObjectExpr>(S)); |
| 1585 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1586 | case Stmt::ReturnStmtClass: |
| 1587 | return VisitReturnStmt(cast<ReturnStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1589 | case Stmt::UnaryExprOrTypeTraitExprClass: |
| 1590 | return VisitUnaryExprOrTypeTraitExpr(cast<UnaryExprOrTypeTraitExpr>(S), |
| 1591 | asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1593 | case Stmt::StmtExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1594 | return VisitStmtExpr(cast<StmtExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1596 | case Stmt::SwitchStmtClass: |
| 1597 | return VisitSwitchStmt(cast<SwitchStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1599 | case Stmt::UnaryOperatorClass: |
| 1600 | return VisitUnaryOperator(cast<UnaryOperator>(S), asc); |
| 1601 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1602 | case Stmt::WhileStmtClass: |
| 1603 | return VisitWhileStmt(cast<WhileStmt>(S)); |
| 1604 | } |
| 1605 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1607 | CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1608 | if (asc.alwaysAdd(*this, S)) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1609 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1610 | appendStmt(Block, S); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1611 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1613 | return VisitChildren(S); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1614 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1615 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1616 | /// VisitChildren - Visit the children of a Stmt. |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 1617 | CFGBlock *CFGBuilder::VisitChildren(Stmt *S) { |
| 1618 | CFGBlock *B = Block; |
Ted Kremenek | 828f631 | 2011-02-21 22:11:26 +0000 | [diff] [blame] | 1619 | |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 1620 | // Visit the children in their reverse order so that they appear in |
| 1621 | // left-to-right (natural) order in the CFG. |
| 1622 | reverse_children RChildren(S); |
| 1623 | for (reverse_children::iterator I = RChildren.begin(), E = RChildren.end(); |
| 1624 | I != E; ++I) { |
| 1625 | if (Stmt *Child = *I) |
| 1626 | if (CFGBlock *R = Visit(Child)) |
| 1627 | B = R; |
| 1628 | } |
| 1629 | return B; |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1630 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1632 | CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, |
| 1633 | AddStmtChoice asc) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1634 | AddressTakenLabels.insert(A->getLabel()); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1635 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1636 | if (asc.alwaysAdd(*this, A)) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1637 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1638 | appendStmt(Block, A); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1639 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1640 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1641 | return Block; |
| 1642 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1644 | CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U, |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1645 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1646 | if (asc.alwaysAdd(*this, U)) { |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1647 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1648 | appendStmt(Block, U); |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1651 | return Visit(U->getSubExpr(), AddStmtChoice()); |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1654 | CFGBlock *CFGBuilder::VisitLogicalOperator(BinaryOperator *B) { |
| 1655 | CFGBlock *ConfluenceBlock = Block ? Block : createBlock(); |
| 1656 | appendStmt(ConfluenceBlock, B); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1658 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1659 | return nullptr; |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1660 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1661 | return VisitLogicalOperator(B, nullptr, ConfluenceBlock, |
| 1662 | ConfluenceBlock).first; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | std::pair<CFGBlock*, CFGBlock*> |
| 1666 | CFGBuilder::VisitLogicalOperator(BinaryOperator *B, |
| 1667 | Stmt *Term, |
| 1668 | CFGBlock *TrueBlock, |
| 1669 | CFGBlock *FalseBlock) { |
| 1670 | |
| 1671 | // Introspect the RHS. If it is a nested logical operation, we recursively |
| 1672 | // build the CFG using this function. Otherwise, resort to default |
| 1673 | // CFG construction behavior. |
| 1674 | Expr *RHS = B->getRHS()->IgnoreParens(); |
| 1675 | CFGBlock *RHSBlock, *ExitBlock; |
| 1676 | |
| 1677 | do { |
| 1678 | if (BinaryOperator *B_RHS = dyn_cast<BinaryOperator>(RHS)) |
| 1679 | if (B_RHS->isLogicalOp()) { |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1680 | std::tie(RHSBlock, ExitBlock) = |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1681 | VisitLogicalOperator(B_RHS, Term, TrueBlock, FalseBlock); |
| 1682 | break; |
| 1683 | } |
| 1684 | |
| 1685 | // The RHS is not a nested logical operation. Don't push the terminator |
| 1686 | // down further, but instead visit RHS and construct the respective |
| 1687 | // pieces of the CFG, and link up the RHSBlock with the terminator |
| 1688 | // we have been provided. |
| 1689 | ExitBlock = RHSBlock = createBlock(false); |
| 1690 | |
| 1691 | if (!Term) { |
| 1692 | assert(TrueBlock == FalseBlock); |
| 1693 | addSuccessor(RHSBlock, TrueBlock); |
| 1694 | } |
| 1695 | else { |
| 1696 | RHSBlock->setTerminator(Term); |
| 1697 | TryResult KnownVal = tryEvaluateBool(RHS); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 1698 | if (!KnownVal.isKnown()) |
| 1699 | KnownVal = tryEvaluateBool(B); |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 1700 | addSuccessor(RHSBlock, TrueBlock, !KnownVal.isFalse()); |
| 1701 | addSuccessor(RHSBlock, FalseBlock, !KnownVal.isTrue()); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | Block = RHSBlock; |
| 1705 | RHSBlock = addStmt(RHS); |
| 1706 | } |
| 1707 | while (false); |
| 1708 | |
| 1709 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1710 | return std::make_pair(nullptr, nullptr); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1711 | |
| 1712 | // Generate the blocks for evaluating the LHS. |
| 1713 | Expr *LHS = B->getLHS()->IgnoreParens(); |
| 1714 | |
| 1715 | if (BinaryOperator *B_LHS = dyn_cast<BinaryOperator>(LHS)) |
| 1716 | if (B_LHS->isLogicalOp()) { |
| 1717 | if (B->getOpcode() == BO_LOr) |
| 1718 | FalseBlock = RHSBlock; |
| 1719 | else |
| 1720 | TrueBlock = RHSBlock; |
| 1721 | |
| 1722 | // For the LHS, treat 'B' as the terminator that we want to sink |
| 1723 | // into the nested branch. The RHS always gets the top-most |
| 1724 | // terminator. |
| 1725 | return VisitLogicalOperator(B_LHS, B, TrueBlock, FalseBlock); |
| 1726 | } |
| 1727 | |
| 1728 | // Create the block evaluating the LHS. |
| 1729 | // This contains the '&&' or '||' as the terminator. |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1730 | CFGBlock *LHSBlock = createBlock(false); |
| 1731 | LHSBlock->setTerminator(B); |
| 1732 | |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1733 | Block = LHSBlock; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1734 | CFGBlock *EntryLHSBlock = addStmt(LHS); |
| 1735 | |
| 1736 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1737 | return std::make_pair(nullptr, nullptr); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1738 | |
| 1739 | // See if this is a known constant. |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1740 | TryResult KnownVal = tryEvaluateBool(LHS); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1741 | |
| 1742 | // Now link the LHSBlock with RHSBlock. |
| 1743 | if (B->getOpcode() == BO_LOr) { |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 1744 | addSuccessor(LHSBlock, TrueBlock, !KnownVal.isFalse()); |
| 1745 | addSuccessor(LHSBlock, RHSBlock, !KnownVal.isTrue()); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1746 | } else { |
| 1747 | assert(B->getOpcode() == BO_LAnd); |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 1748 | addSuccessor(LHSBlock, RHSBlock, !KnownVal.isFalse()); |
| 1749 | addSuccessor(LHSBlock, FalseBlock, !KnownVal.isTrue()); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1752 | return std::make_pair(EntryLHSBlock, ExitBlock); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1755 | |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1756 | CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, |
| 1757 | AddStmtChoice asc) { |
| 1758 | // && or || |
| 1759 | if (B->isLogicalOp()) |
| 1760 | return VisitLogicalOperator(B); |
| 1761 | |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 1762 | if (B->getOpcode() == BO_Comma) { // , |
Ted Kremenek | fe9b768 | 2009-07-17 22:57:50 +0000 | [diff] [blame] | 1763 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1764 | appendStmt(Block, B); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1765 | addStmt(B->getRHS()); |
| 1766 | return addStmt(B->getLHS()); |
| 1767 | } |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 1768 | |
| 1769 | if (B->isAssignmentOp()) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1770 | if (asc.alwaysAdd(*this, B)) { |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 1771 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1772 | appendStmt(Block, B); |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 1773 | } |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1774 | Visit(B->getLHS()); |
Marcin Swiderski | 7723249 | 2010-10-24 08:21:40 +0000 | [diff] [blame] | 1775 | return Visit(B->getRHS()); |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 1776 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1778 | if (asc.alwaysAdd(*this, B)) { |
Marcin Swiderski | 7723249 | 2010-10-24 08:21:40 +0000 | [diff] [blame] | 1779 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1780 | appendStmt(Block, B); |
Marcin Swiderski | 7723249 | 2010-10-24 08:21:40 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Zhongxing Xu | d95ccd5 | 2010-10-27 03:23:10 +0000 | [diff] [blame] | 1783 | CFGBlock *RBlock = Visit(B->getRHS()); |
| 1784 | CFGBlock *LBlock = Visit(B->getLHS()); |
| 1785 | // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr |
| 1786 | // containing a DoStmt, and the LHS doesn't create a new block, then we should |
| 1787 | // return RBlock. Otherwise we'll incorrectly return NULL. |
| 1788 | return (LBlock ? LBlock : RBlock); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Ted Kremenek | e249984 | 2012-04-12 20:03:44 +0000 | [diff] [blame] | 1791 | CFGBlock *CFGBuilder::VisitNoRecurse(Expr *E, AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1792 | if (asc.alwaysAdd(*this, E)) { |
Ted Kremenek | 470bfa4 | 2009-11-25 01:34:30 +0000 | [diff] [blame] | 1793 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1794 | appendStmt(Block, E); |
Ted Kremenek | 470bfa4 | 2009-11-25 01:34:30 +0000 | [diff] [blame] | 1795 | } |
| 1796 | return Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1799 | CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) { |
| 1800 | // "break" is a control-flow statement. Thus we stop processing the current |
| 1801 | // block. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1802 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1803 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1805 | // Now create a new block that ends with the break statement. |
| 1806 | Block = createBlock(false); |
| 1807 | Block->setTerminator(B); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1809 | // If there is no target for the break, then we are looking at an incomplete |
| 1810 | // AST. This means that the CFG cannot be constructed. |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 1811 | if (BreakJumpTarget.block) { |
| 1812 | addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B); |
| 1813 | addSuccessor(Block, BreakJumpTarget.block); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1814 | } else |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1815 | badCFG = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
| 1817 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1818 | return Block; |
| 1819 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1821 | static bool CanThrow(Expr *E, ASTContext &Ctx) { |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1822 | QualType Ty = E->getType(); |
| 1823 | if (Ty->isFunctionPointerType()) |
| 1824 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
| 1825 | else if (Ty->isBlockPointerType()) |
| 1826 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1827 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1828 | const FunctionType *FT = Ty->getAs<FunctionType>(); |
| 1829 | if (FT) { |
| 1830 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 1831 | if (!isUnresolvedExceptionSpec(Proto->getExceptionSpecType()) && |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1832 | Proto->isNothrow(Ctx)) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1833 | return false; |
| 1834 | } |
| 1835 | return true; |
| 1836 | } |
| 1837 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1838 | CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { |
John McCall | c67067f | 2011-05-11 07:19:11 +0000 | [diff] [blame] | 1839 | // Compute the callee type. |
| 1840 | QualType calleeType = C->getCallee()->getType(); |
| 1841 | if (calleeType == Context->BoundMemberTy) { |
| 1842 | QualType boundType = Expr::findBoundMemberType(C->getCallee()); |
| 1843 | |
| 1844 | // We should only get a null bound type if processing a dependent |
| 1845 | // CFG. Recover by assuming nothing. |
| 1846 | if (!boundType.isNull()) calleeType = boundType; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1847 | } |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1848 | |
John McCall | c67067f | 2011-05-11 07:19:11 +0000 | [diff] [blame] | 1849 | // If this is a call to a no-return function, this stops the block here. |
| 1850 | bool NoReturn = getFunctionExtInfo(*calleeType).getNoReturn(); |
| 1851 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1852 | bool AddEHEdge = false; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1853 | |
| 1854 | // Languages without exceptions are assumed to not throw. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1855 | if (Context->getLangOpts().Exceptions) { |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 1856 | if (BuildOpts.AddEHEdges) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1857 | AddEHEdge = true; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 1860 | // If this is a call to a builtin function, it might not actually evaluate |
| 1861 | // its arguments. Don't add them to the CFG if this is the case. |
| 1862 | bool OmitArguments = false; |
| 1863 | |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1864 | if (FunctionDecl *FD = C->getDirectCallee()) { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1865 | if (FD->isNoReturn()) |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1866 | NoReturn = true; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1867 | if (FD->hasAttr<NoThrowAttr>()) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1868 | AddEHEdge = false; |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 1869 | if (FD->getBuiltinID() == Builtin::BI__builtin_object_size) |
| 1870 | OmitArguments = true; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1871 | } |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1872 | |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 1873 | if (!CanThrow(C->getCallee(), *Context)) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1874 | AddEHEdge = false; |
| 1875 | |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 1876 | if (OmitArguments) { |
| 1877 | assert(!NoReturn && "noreturn calls with unevaluated args not implemented"); |
| 1878 | assert(!AddEHEdge && "EH calls with unevaluated args not implemented"); |
| 1879 | autoCreateBlock(); |
| 1880 | appendStmt(Block, C); |
| 1881 | return Visit(C->getCallee()); |
| 1882 | } |
| 1883 | |
| 1884 | if (!NoReturn && !AddEHEdge) { |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 1885 | return VisitStmt(C, asc.withAlwaysAdd(true)); |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 1886 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1888 | if (Block) { |
| 1889 | Succ = Block; |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1890 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1891 | return nullptr; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1892 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 1894 | if (NoReturn) |
| 1895 | Block = createNoReturnBlock(); |
| 1896 | else |
| 1897 | Block = createBlock(); |
| 1898 | |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1899 | appendStmt(Block, C); |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1900 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 1901 | if (AddEHEdge) { |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1902 | // Add exceptional edges. |
| 1903 | if (TryTerminatedBlock) |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 1904 | addSuccessor(Block, TryTerminatedBlock); |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1905 | else |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 1906 | addSuccessor(Block, &cfg->getExit()); |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1907 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1908 | |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1909 | return VisitChildren(C); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1910 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1911 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1912 | CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C, |
| 1913 | AddStmtChoice asc) { |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1914 | CFGBlock *ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1915 | appendStmt(ConfluenceBlock, C); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1916 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1917 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 1919 | AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1920 | Succ = ConfluenceBlock; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1921 | Block = nullptr; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1922 | CFGBlock *LHSBlock = Visit(C->getLHS(), alwaysAdd); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1923 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1924 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1925 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1926 | Succ = ConfluenceBlock; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1927 | Block = nullptr; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1928 | CFGBlock *RHSBlock = Visit(C->getRHS(), alwaysAdd); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1929 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1930 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1932 | Block = createBlock(false); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1933 | // See if this is a known constant. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 1934 | const TryResult& KnownVal = tryEvaluateBool(C->getCond()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1935 | addSuccessor(Block, KnownVal.isFalse() ? nullptr : LHSBlock); |
| 1936 | addSuccessor(Block, KnownVal.isTrue() ? nullptr : RHSBlock); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1937 | Block->setTerminator(C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | return addStmt(C->getCond()); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 1939 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | |
| 1941 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1942 | CFGBlock *CFGBuilder::VisitCompoundStmt(CompoundStmt *C) { |
Matthias Gehre | 09a134e | 2015-11-14 00:36:50 +0000 | [diff] [blame] | 1943 | LocalScope::const_iterator scopeBeginPos = ScopePos; |
| 1944 | if (BuildOpts.AddImplicitDtors) { |
| 1945 | addLocalScopeForStmt(C); |
| 1946 | } |
| 1947 | if (!C->body_empty() && !isa<ReturnStmt>(*C->body_rbegin())) { |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 1948 | // If the body ends with a ReturnStmt, the dtors will be added in |
| 1949 | // VisitReturnStmt. |
Matthias Gehre | 09a134e | 2015-11-14 00:36:50 +0000 | [diff] [blame] | 1950 | addAutomaticObjDtors(ScopePos, scopeBeginPos, C); |
| 1951 | } |
| 1952 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1953 | CFGBlock *LastBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1954 | |
| 1955 | for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); |
| 1956 | I != E; ++I ) { |
Ted Kremenek | 4f2ab5a | 2010-08-17 21:00:06 +0000 | [diff] [blame] | 1957 | // If we hit a segment of code just containing ';' (NullStmts), we can |
| 1958 | // get a null block back. In such cases, just use the LastBlock |
| 1959 | if (CFGBlock *newBlock = addStmt(*I)) |
| 1960 | LastBlock = newBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | |
Ted Kremenek | ce499c2 | 2009-08-27 23:16:26 +0000 | [diff] [blame] | 1962 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1963 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | } |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 1965 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1966 | return LastBlock; |
| 1967 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1969 | CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C, |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1970 | AddStmtChoice asc) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1971 | const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1972 | const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : nullptr); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1973 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1974 | // Create the confluence block that will "merge" the results of the ternary |
| 1975 | // expression. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1976 | CFGBlock *ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1977 | appendStmt(ConfluenceBlock, C); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1978 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1979 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 1981 | AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 1982 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1983 | // Create a block for the LHS expression if there is an LHS expression. A |
| 1984 | // GCC extension allows LHS to be NULL, causing the condition to be the |
| 1985 | // value that is returned instead. |
| 1986 | // e.g: x ?: y is shorthand for: x ? x : y; |
| 1987 | Succ = ConfluenceBlock; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1988 | Block = nullptr; |
| 1989 | CFGBlock *LHSBlock = nullptr; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1990 | const Expr *trueExpr = C->getTrueExpr(); |
| 1991 | if (trueExpr != opaqueValue) { |
| 1992 | LHSBlock = Visit(C->getTrueExpr(), alwaysAdd); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 1993 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1994 | return nullptr; |
| 1995 | Block = nullptr; |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 1996 | } |
Ted Kremenek | d813801 | 2011-02-24 03:09:15 +0000 | [diff] [blame] | 1997 | else |
| 1998 | LHSBlock = ConfluenceBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2000 | // Create the block for the RHS expression. |
| 2001 | Succ = ConfluenceBlock; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2002 | CFGBlock *RHSBlock = Visit(C->getFalseExpr(), alwaysAdd); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2003 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2004 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 2006 | // If the condition is a logical '&&' or '||', build a more accurate CFG. |
| 2007 | if (BinaryOperator *Cond = |
| 2008 | dyn_cast<BinaryOperator>(C->getCond()->IgnoreParens())) |
| 2009 | if (Cond->isLogicalOp()) |
| 2010 | return VisitLogicalOperator(Cond, C, LHSBlock, RHSBlock).first; |
| 2011 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2012 | // Create the block that will contain the condition. |
| 2013 | Block = createBlock(false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2015 | // See if this is a known constant. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2016 | const TryResult& KnownVal = tryEvaluateBool(C->getCond()); |
Ted Kremenek | 5a09527 | 2014-03-04 21:53:26 +0000 | [diff] [blame] | 2017 | addSuccessor(Block, LHSBlock, !KnownVal.isFalse()); |
| 2018 | addSuccessor(Block, RHSBlock, !KnownVal.isTrue()); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2019 | Block->setTerminator(C); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2020 | Expr *condExpr = C->getCond(); |
John McCall | 68cc335 | 2011-02-19 03:13:26 +0000 | [diff] [blame] | 2021 | |
Ted Kremenek | d813801 | 2011-02-24 03:09:15 +0000 | [diff] [blame] | 2022 | if (opaqueValue) { |
| 2023 | // Run the condition expression if it's not trivially expressed in |
| 2024 | // terms of the opaque value (or if there is no opaque value). |
| 2025 | if (condExpr != opaqueValue) |
| 2026 | addStmt(condExpr); |
John McCall | 68cc335 | 2011-02-19 03:13:26 +0000 | [diff] [blame] | 2027 | |
Ted Kremenek | d813801 | 2011-02-24 03:09:15 +0000 | [diff] [blame] | 2028 | // Before that, run the common subexpression if there was one. |
| 2029 | // At least one of this or the above will be run. |
| 2030 | return addStmt(BCO->getCommon()); |
| 2031 | } |
| 2032 | |
| 2033 | return addStmt(condExpr); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2036 | CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) { |
Ted Kremenek | 6878c36 | 2011-05-10 18:42:15 +0000 | [diff] [blame] | 2037 | // Check if the Decl is for an __label__. If so, elide it from the |
| 2038 | // CFG entirely. |
| 2039 | if (isa<LabelDecl>(*DS->decl_begin())) |
| 2040 | return Block; |
| 2041 | |
Ted Kremenek | 3a60114 | 2011-05-24 20:41:31 +0000 | [diff] [blame] | 2042 | // This case also handles static_asserts. |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2043 | if (DS->isSingleDecl()) |
| 2044 | return VisitDeclSubExpr(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2046 | CFGBlock *B = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
Jordan Rose | 8c6c8a9 | 2012-07-20 18:50:48 +0000 | [diff] [blame] | 2048 | // Build an individual DeclStmt for each decl. |
| 2049 | for (DeclStmt::reverse_decl_iterator I = DS->decl_rbegin(), |
| 2050 | E = DS->decl_rend(); |
| 2051 | I != E; ++I) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2052 | // Get the alignment of the new DeclStmt, padding out to >=8 bytes. |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 2053 | unsigned A = alignof(DeclStmt) < 8 ? 8 : alignof(DeclStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2055 | // Allocate the DeclStmt using the BumpPtrAllocator. It will get |
| 2056 | // automatically freed with the CFG. |
| 2057 | DeclGroupRef DG(*I); |
| 2058 | Decl *D = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2060 | DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D)); |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 2061 | cfg->addSyntheticDeclStmt(DSNew, DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2063 | // Append the fake DeclStmt to block. |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2064 | B = VisitDeclSubExpr(DSNew); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2065 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | |
| 2067 | return B; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2068 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2070 | /// VisitDeclSubExpr - Utility method to add block-level expressions for |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2071 | /// DeclStmts and initializers in them. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2072 | CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt *DS) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2073 | assert(DS->isSingleDecl() && "Can handle single declarations only."); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2074 | VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2076 | if (!VD) { |
Jordan Rose | 5250b87 | 2013-06-03 22:59:41 +0000 | [diff] [blame] | 2077 | // Of everything that can be declared in a DeclStmt, only VarDecls impact |
| 2078 | // runtime semantics. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2079 | return Block; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2080 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2082 | bool HasTemporaries = false; |
| 2083 | |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 2084 | // Guard static initializers under a branch. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2085 | CFGBlock *blockAfterStaticInit = nullptr; |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2086 | |
| 2087 | if (BuildOpts.AddStaticInitBranches && VD->isStaticLocal()) { |
| 2088 | // For static variables, we need to create a branch to track |
| 2089 | // whether or not they are initialized. |
| 2090 | if (Block) { |
| 2091 | Succ = Block; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2092 | Block = nullptr; |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2093 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2094 | return nullptr; |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2095 | } |
| 2096 | blockAfterStaticInit = Succ; |
| 2097 | } |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 2098 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2099 | // Destructors of temporaries in initialization expression should be called |
| 2100 | // after initialization finishes. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2101 | Expr *Init = VD->getInit(); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2102 | if (Init) { |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 2103 | HasTemporaries = isa<ExprWithCleanups>(Init); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2104 | |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 2105 | if (BuildOpts.AddTemporaryDtors && HasTemporaries) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2106 | // Generate destructors for temporaries in initialization expression. |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 2107 | TempDtorContext Context; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 2108 | VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(), |
| 2109 | /*BindToTemporary=*/false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | autoCreateBlock(); |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 2114 | appendStmt(Block, DS); |
Ted Kremenek | 213d053 | 2012-03-22 05:57:43 +0000 | [diff] [blame] | 2115 | |
| 2116 | // Keep track of the last non-null block, as 'Block' can be nulled out |
| 2117 | // if the initializer expression is something like a 'while' in a |
| 2118 | // statement-expression. |
| 2119 | CFGBlock *LastBlock = Block; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2121 | if (Init) { |
Ted Kremenek | 213d053 | 2012-03-22 05:57:43 +0000 | [diff] [blame] | 2122 | if (HasTemporaries) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2123 | // For expression with temporaries go directly to subexpression to omit |
| 2124 | // generating destructors for the second time. |
Ted Kremenek | 213d053 | 2012-03-22 05:57:43 +0000 | [diff] [blame] | 2125 | ExprWithCleanups *EC = cast<ExprWithCleanups>(Init); |
| 2126 | if (CFGBlock *newBlock = Visit(EC->getSubExpr())) |
| 2127 | LastBlock = newBlock; |
| 2128 | } |
| 2129 | else { |
| 2130 | if (CFGBlock *newBlock = Visit(Init)) |
| 2131 | LastBlock = newBlock; |
| 2132 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2133 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2135 | // If the type of VD is a VLA, then we must process its size expressions. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2136 | for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2137 | VA != nullptr; VA = FindVA(VA->getElementType().getTypePtr())) { |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 2138 | if (CFGBlock *newBlock = addStmt(VA->getSizeExpr())) |
| 2139 | LastBlock = newBlock; |
| 2140 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | |
Marcin Swiderski | 667ffec | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 2142 | // Remove variable from local scope. |
| 2143 | if (ScopePos && VD == *ScopePos) |
| 2144 | ++ScopePos; |
| 2145 | |
Ted Kremenek | 338c3aa | 2013-03-29 00:09:28 +0000 | [diff] [blame] | 2146 | CFGBlock *B = LastBlock; |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2147 | if (blockAfterStaticInit) { |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 2148 | Succ = B; |
Ted Kremenek | 338c3aa | 2013-03-29 00:09:28 +0000 | [diff] [blame] | 2149 | Block = createBlock(false); |
| 2150 | Block->setTerminator(DS); |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2151 | addSuccessor(Block, blockAfterStaticInit); |
Ted Kremenek | 338c3aa | 2013-03-29 00:09:28 +0000 | [diff] [blame] | 2152 | addSuccessor(Block, B); |
| 2153 | B = Block; |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | return B; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2159 | CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2160 | // We may see an if statement in the middle of a basic block, or it may be the |
| 2161 | // first statement we are processing. In either case, we create a new basic |
| 2162 | // block. First, we create the blocks for the then...else statements, and |
| 2163 | // 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] | 2164 | // middle of a block, we stop processing that block. That block is then the |
| 2165 | // implicit successor for the "then" and "else" clauses. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2166 | |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2167 | // Save local scope position because in case of condition variable ScopePos |
| 2168 | // won't be restored when traversing AST. |
| 2169 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 2170 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 2171 | // Create local scope for C++17 if init-stmt if one exists. |
| 2172 | if (Stmt *Init = I->getInit()) { |
| 2173 | LocalScope::const_iterator BeginScopePos = ScopePos; |
| 2174 | addLocalScopeForStmt(Init); |
| 2175 | addAutomaticObjDtors(ScopePos, BeginScopePos, I); |
| 2176 | } |
| 2177 | |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2178 | // Create local scope for possible condition variable. |
| 2179 | // Store scope position. Add implicit destructor. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2180 | if (VarDecl *VD = I->getConditionVariable()) { |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2181 | LocalScope::const_iterator BeginScopePos = ScopePos; |
| 2182 | addLocalScopeForVarDecl(VD); |
| 2183 | addAutomaticObjDtors(ScopePos, BeginScopePos, I); |
| 2184 | } |
| 2185 | |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2186 | // The block we were processing is now finished. Make it the successor |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2187 | // block. |
| 2188 | if (Block) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2189 | Succ = Block; |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2190 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2191 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2192 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2193 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 2194 | // Process the false branch. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2195 | CFGBlock *ElseBlock = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2196 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2197 | if (Stmt *Else = I->getElse()) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2198 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2199 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2200 | // NULL out Block so that the recursive call to Visit will |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2201 | // create a new basic block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2202 | Block = nullptr; |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2203 | |
| 2204 | // If branch is not a compound statement create implicit scope |
| 2205 | // and add destructors. |
| 2206 | if (!isa<CompoundStmt>(Else)) |
| 2207 | addLocalScopeAndDtors(Else); |
| 2208 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2209 | ElseBlock = addStmt(Else); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2210 | |
Ted Kremenek | bbad8ce | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 2211 | if (!ElseBlock) // Can occur when the Else body has all NullStmts. |
| 2212 | ElseBlock = sv.get(); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2213 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2214 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2215 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2216 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2217 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2218 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 2219 | // Process the true branch. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2220 | CFGBlock *ThenBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2221 | { |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2222 | Stmt *Then = I->getThen(); |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 2223 | assert(Then); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2224 | SaveAndRestore<CFGBlock*> sv(Succ); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2225 | Block = nullptr; |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2226 | |
| 2227 | // If branch is not a compound statement create implicit scope |
| 2228 | // and add destructors. |
| 2229 | if (!isa<CompoundStmt>(Then)) |
| 2230 | addLocalScopeAndDtors(Then); |
| 2231 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2232 | ThenBlock = addStmt(Then); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2233 | |
Ted Kremenek | 1b37951 | 2009-04-01 03:52:47 +0000 | [diff] [blame] | 2234 | if (!ThenBlock) { |
| 2235 | // We can reach here if the "then" body has all NullStmts. |
| 2236 | // Create an empty block so we can distinguish between true and false |
| 2237 | // branches in path-sensitive analyses. |
| 2238 | ThenBlock = createBlock(false); |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2239 | addSuccessor(ThenBlock, sv.get()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2240 | } else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2241 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2242 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2243 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2246 | // Specially handle "if (expr1 || ...)" and "if (expr1 && ...)" by |
| 2247 | // having these handle the actual control-flow jump. Note that |
| 2248 | // if we introduce a condition variable, e.g. "if (int x = exp1 || exp2)" |
| 2249 | // we resort to the old control-flow behavior. This special handling |
| 2250 | // removes infeasible paths from the control-flow graph by having the |
| 2251 | // control-flow transfer of '&&' or '||' go directly into the then/else |
| 2252 | // blocks directly. |
| 2253 | if (!I->getConditionVariable()) |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 2254 | if (BinaryOperator *Cond = |
| 2255 | dyn_cast<BinaryOperator>(I->getCond()->IgnoreParens())) |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2256 | if (Cond->isLogicalOp()) |
| 2257 | return VisitLogicalOperator(Cond, I, ThenBlock, ElseBlock).first; |
| 2258 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2259 | // Now create a new block containing the if statement. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2260 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2261 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2262 | // Set the terminator of the new block to the If statement. |
| 2263 | Block->setTerminator(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2264 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2265 | // See if this is a known constant. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2266 | const TryResult &KnownVal = tryEvaluateBool(I->getCond()); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2267 | |
Ted Kremenek | f389861 | 2014-02-27 00:24:03 +0000 | [diff] [blame] | 2268 | // Add the successors. If we know that specific branches are |
| 2269 | // unreachable, inform addSuccessor() of that knowledge. |
| 2270 | addSuccessor(Block, ThenBlock, /* isReachable = */ !KnownVal.isFalse()); |
| 2271 | addSuccessor(Block, ElseBlock, /* isReachable = */ !KnownVal.isTrue()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2272 | |
| 2273 | // Add the condition as the last statement in the new block. This may create |
| 2274 | // new blocks as the condition may contain control-flow. Any newly created |
| 2275 | // blocks will be pointed to be "Block". |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 2276 | CFGBlock *LastBlock = addStmt(I->getCond()); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2277 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 2278 | // If the IfStmt contains a condition variable, add it and its |
Manuel Klimek | 75f34c1 | 2014-05-05 18:21:06 +0000 | [diff] [blame] | 2279 | // initializer to the CFG. |
| 2280 | if (const DeclStmt* DS = I->getConditionVariableDeclStmt()) { |
| 2281 | autoCreateBlock(); |
| 2282 | LastBlock = addStmt(const_cast<DeclStmt *>(DS)); |
Ted Kremenek | a7bcbde | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 2283 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2284 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 2285 | // Finally, if the IfStmt contains a C++17 init-stmt, add it to the CFG. |
| 2286 | if (Stmt *Init = I->getInit()) { |
| 2287 | autoCreateBlock(); |
| 2288 | LastBlock = addStmt(Init); |
| 2289 | } |
| 2290 | |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 2291 | return LastBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2292 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2293 | |
| 2294 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2295 | CFGBlock *CFGBuilder::VisitReturnStmt(ReturnStmt *R) { |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 2296 | // 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] | 2297 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2298 | // NOTE: If a "return" appears in the middle of a block, this means that the |
| 2299 | // code afterwards is DEAD (unreachable). We still keep a basic block |
| 2300 | // for that code; a simple "mark-and-sweep" from the entry block will be |
| 2301 | // able to report such dead blocks. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2302 | |
| 2303 | // Create the new block. |
| 2304 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2305 | |
Marcin Swiderski | 667ffec | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 2306 | addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R); |
Pavel Labath | 921e765 | 2013-09-06 08:12:48 +0000 | [diff] [blame] | 2307 | |
| 2308 | // If the one of the destructors does not return, we already have the Exit |
| 2309 | // block as a successor. |
| 2310 | if (!Block->hasNoReturnElement()) |
| 2311 | addSuccessor(Block, &cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2312 | |
| 2313 | // Add the return statement to the block. This may create new blocks if R |
| 2314 | // contains control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2315 | return VisitStmt(R, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2318 | CFGBlock *CFGBuilder::VisitLabelStmt(LabelStmt *L) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2319 | // Get the block of the labeled statement. Add it to our map. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2320 | addStmt(L->getSubStmt()); |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2321 | CFGBlock *LabelBlock = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2322 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2323 | if (!LabelBlock) // This can happen when the body is empty, i.e. |
| 2324 | LabelBlock = createBlock(); // scopes that only contains NullStmts. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2325 | |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2326 | assert(LabelMap.find(L->getDecl()) == LabelMap.end() && |
| 2327 | "label already in map"); |
| 2328 | LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2329 | |
| 2330 | // Labels partition blocks, so this is the end of the basic block we were |
| 2331 | // processing (L is the block's label). Because this is label (and we have |
| 2332 | // already processed the substatement) there is no extra control-flow to worry |
| 2333 | // about. |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2334 | LabelBlock->setLabel(L); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2335 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2336 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2337 | |
| 2338 | // We set Block to NULL to allow lazy creation of a new block (if necessary); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2339 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2340 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2341 | // This block is now the implicit successor of other blocks. |
| 2342 | Succ = LabelBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2343 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2344 | return LabelBlock; |
| 2345 | } |
| 2346 | |
Devin Coughlin | b6029b7 | 2015-11-25 22:35:37 +0000 | [diff] [blame] | 2347 | CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) { |
| 2348 | CFGBlock *LastBlock = VisitNoRecurse(E, asc); |
| 2349 | for (const BlockDecl::Capture &CI : E->getBlockDecl()->captures()) { |
| 2350 | if (Expr *CopyExpr = CI.getCopyExpr()) { |
| 2351 | CFGBlock *Tmp = Visit(CopyExpr); |
| 2352 | if (Tmp) |
| 2353 | LastBlock = Tmp; |
| 2354 | } |
| 2355 | } |
| 2356 | return LastBlock; |
| 2357 | } |
| 2358 | |
Ted Kremenek | da76a94 | 2012-04-12 20:34:52 +0000 | [diff] [blame] | 2359 | CFGBlock *CFGBuilder::VisitLambdaExpr(LambdaExpr *E, AddStmtChoice asc) { |
| 2360 | CFGBlock *LastBlock = VisitNoRecurse(E, asc); |
| 2361 | for (LambdaExpr::capture_init_iterator it = E->capture_init_begin(), |
| 2362 | et = E->capture_init_end(); it != et; ++it) { |
| 2363 | if (Expr *Init = *it) { |
| 2364 | CFGBlock *Tmp = Visit(Init); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2365 | if (Tmp) |
Ted Kremenek | da76a94 | 2012-04-12 20:34:52 +0000 | [diff] [blame] | 2366 | LastBlock = Tmp; |
| 2367 | } |
| 2368 | } |
| 2369 | return LastBlock; |
| 2370 | } |
| 2371 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2372 | CFGBlock *CFGBuilder::VisitGotoStmt(GotoStmt *G) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2373 | // Goto is a control-flow statement. Thus we stop processing the current |
| 2374 | // block and create a new one. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2375 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2376 | Block = createBlock(false); |
| 2377 | Block->setTerminator(G); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2378 | |
| 2379 | // 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] | 2380 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2381 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2382 | if (I == LabelMap.end()) |
| 2383 | // We will need to backpatch this block later. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2384 | BackpatchBlocks.push_back(JumpSource(Block, ScopePos)); |
| 2385 | else { |
| 2386 | JumpTarget JT = I->second; |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 2387 | addAutomaticObjDtors(ScopePos, JT.scopePosition, G); |
| 2388 | addSuccessor(Block, JT.block); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2389 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2390 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2391 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2394 | CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2395 | CFGBlock *LoopSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2396 | |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2397 | // Save local scope position because in case of condition variable ScopePos |
| 2398 | // won't be restored when traversing AST. |
| 2399 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 2400 | |
| 2401 | // Create local scope for init statement and possible condition variable. |
| 2402 | // Add destructor for init statement and condition variable. |
| 2403 | // Store scope position for continue statement. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2404 | if (Stmt *Init = F->getInit()) |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2405 | addLocalScopeForStmt(Init); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2406 | LocalScope::const_iterator LoopBeginScopePos = ScopePos; |
| 2407 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2408 | if (VarDecl *VD = F->getConditionVariable()) |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2409 | addLocalScopeForVarDecl(VD); |
| 2410 | LocalScope::const_iterator ContinueScopePos = ScopePos; |
| 2411 | |
| 2412 | addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F); |
| 2413 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 2414 | // "for" is a control-flow statement. Thus we stop processing the current |
| 2415 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2416 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2417 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2418 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2419 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2420 | } else |
| 2421 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2422 | |
Ted Kremenek | 304a953 | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 2423 | // Save the current value for the break targets. |
| 2424 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2425 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2426 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Ted Kremenek | 304a953 | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 2427 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2428 | CFGBlock *BodyBlock = nullptr, *TransitionBlock = nullptr; |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2429 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2430 | // Now create the loop body. |
| 2431 | { |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 2432 | assert(F->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2433 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2434 | // Save the current values for Block, Succ, continue and break targets. |
| 2435 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 2436 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2437 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2438 | // Create an empty block to represent the transition block for looping back |
| 2439 | // to the head of the loop. If we have increment code, it will |
| 2440 | // go in this block as well. |
| 2441 | Block = Succ = TransitionBlock = createBlock(false); |
| 2442 | TransitionBlock->setLoopTarget(F); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2443 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2444 | if (Stmt *I = F->getInc()) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2445 | // Generate increment code in its own basic block. This is the target of |
| 2446 | // continue statements. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2447 | Succ = addStmt(I); |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 2448 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2449 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 2450 | // Finish up the increment (or empty) block if it hasn't been already. |
| 2451 | if (Block) { |
| 2452 | assert(Block == Succ); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2453 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2454 | return nullptr; |
| 2455 | Block = nullptr; |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 2456 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2457 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2458 | // The starting block for the loop increment is the block that should |
| 2459 | // represent the 'loop target' for looping back to the start of the loop. |
| 2460 | ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos); |
| 2461 | ContinueJumpTarget.block->setLoopTarget(F); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2462 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2463 | // Loop body should end with destructor of Condition variable (if any). |
| 2464 | addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F); |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 2465 | |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2466 | // If body is not a compound statement create implicit scope |
| 2467 | // and add destructors. |
| 2468 | if (!isa<CompoundStmt>(F->getBody())) |
| 2469 | addLocalScopeAndDtors(F->getBody()); |
| 2470 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2471 | // Now populate the body block, and in the process create new blocks as we |
| 2472 | // walk the body of the loop. |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2473 | BodyBlock = addStmt(F->getBody()); |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 2474 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2475 | if (!BodyBlock) { |
| 2476 | // In the case of "for (...;...;...);" we can have a null BodyBlock. |
| 2477 | // Use the continue jump target as the proxy for the body. |
| 2478 | BodyBlock = ContinueJumpTarget.block; |
| 2479 | } |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2480 | else if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2481 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2482 | } |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2483 | |
| 2484 | // Because of short-circuit evaluation, the condition of the loop can span |
| 2485 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 2486 | // evaluate the condition. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2487 | CFGBlock *EntryConditionBlock = nullptr, *ExitConditionBlock = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2488 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2489 | do { |
| 2490 | Expr *C = F->getCond(); |
| 2491 | |
| 2492 | // Specially handle logical operators, which have a slightly |
| 2493 | // more optimal CFG representation. |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 2494 | if (BinaryOperator *Cond = |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2495 | dyn_cast_or_null<BinaryOperator>(C ? C->IgnoreParens() : nullptr)) |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2496 | if (Cond->isLogicalOp()) { |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 2497 | std::tie(EntryConditionBlock, ExitConditionBlock) = |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2498 | VisitLogicalOperator(Cond, F, BodyBlock, LoopSuccessor); |
| 2499 | break; |
| 2500 | } |
| 2501 | |
| 2502 | // The default case when not handling logical operators. |
| 2503 | EntryConditionBlock = ExitConditionBlock = createBlock(false); |
| 2504 | ExitConditionBlock->setTerminator(F); |
| 2505 | |
| 2506 | // See if this is a known constant. |
| 2507 | TryResult KnownVal(true); |
| 2508 | |
| 2509 | if (C) { |
| 2510 | // Now add the actual condition to the condition block. |
| 2511 | // Because the condition itself may contain control-flow, new blocks may |
| 2512 | // be created. Thus we update "Succ" after adding the condition. |
| 2513 | Block = ExitConditionBlock; |
| 2514 | EntryConditionBlock = addStmt(C); |
| 2515 | |
| 2516 | // If this block contains a condition variable, add both the condition |
| 2517 | // variable and initializer to the CFG. |
| 2518 | if (VarDecl *VD = F->getConditionVariable()) { |
| 2519 | if (Expr *Init = VD->getInit()) { |
| 2520 | autoCreateBlock(); |
| 2521 | appendStmt(Block, F->getConditionVariableDeclStmt()); |
| 2522 | EntryConditionBlock = addStmt(Init); |
| 2523 | assert(Block == EntryConditionBlock); |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | if (Block && badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2528 | return nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2529 | |
| 2530 | KnownVal = tryEvaluateBool(C); |
| 2531 | } |
| 2532 | |
| 2533 | // Add the loop body entry as a successor to the condition. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2534 | addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? nullptr : BodyBlock); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2535 | // Link up the condition block with the code that follows the loop. (the |
| 2536 | // false branch). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2537 | addSuccessor(ExitConditionBlock, |
| 2538 | KnownVal.isTrue() ? nullptr : LoopSuccessor); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2539 | |
| 2540 | } while (false); |
| 2541 | |
| 2542 | // Link up the loop-back block to the entry condition block. |
| 2543 | addSuccessor(TransitionBlock, EntryConditionBlock); |
| 2544 | |
| 2545 | // The condition block is the implicit successor for any code above the loop. |
| 2546 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2547 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2548 | // If the loop contains initialization, create a new block for those |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2549 | // statements. This block can also contain statements that precede the loop. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2550 | if (Stmt *I = F->getInit()) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2551 | Block = createBlock(); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2552 | return addStmt(I); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2553 | } |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 2554 | |
| 2555 | // There is no loop initialization. We are thus basically a while loop. |
| 2556 | // NULL out Block to force lazy block construction. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2557 | Block = nullptr; |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 2558 | Succ = EntryConditionBlock; |
| 2559 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 2562 | CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 2563 | if (asc.alwaysAdd(*this, M)) { |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 2564 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2565 | appendStmt(Block, M); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 2566 | } |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 2567 | return Visit(M->getBase()); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2570 | CFGBlock *CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2571 | // Objective-C fast enumeration 'for' statements: |
| 2572 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC |
| 2573 | // |
| 2574 | // for ( Type newVariable in collection_expression ) { statements } |
| 2575 | // |
| 2576 | // becomes: |
| 2577 | // |
| 2578 | // prologue: |
| 2579 | // 1. collection_expression |
| 2580 | // T. jump to loop_entry |
| 2581 | // loop_entry: |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2582 | // 1. side-effects of element expression |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2583 | // 1. ObjCForCollectionStmt [performs binding to newVariable] |
| 2584 | // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil] |
| 2585 | // TB: |
| 2586 | // statements |
| 2587 | // T. jump to loop_entry |
| 2588 | // FB: |
| 2589 | // what comes after |
| 2590 | // |
| 2591 | // and |
| 2592 | // |
| 2593 | // Type existingItem; |
| 2594 | // for ( existingItem in expression ) { statements } |
| 2595 | // |
| 2596 | // becomes: |
| 2597 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2598 | // the same with newVariable replaced with existingItem; the binding works |
| 2599 | // the same except that for one ObjCForCollectionStmt::getElement() returns |
| 2600 | // a DeclStmt and the other returns a DeclRefExpr. |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2601 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2602 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2603 | CFGBlock *LoopSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2604 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2605 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2606 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2607 | return nullptr; |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2608 | LoopSuccessor = Block; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2609 | Block = nullptr; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2610 | } else |
| 2611 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2612 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2613 | // Build the condition blocks. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2614 | CFGBlock *ExitConditionBlock = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2615 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2616 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2617 | ExitConditionBlock->setTerminator(S); |
| 2618 | |
| 2619 | // The last statement in the block should be the ObjCForCollectionStmt, which |
| 2620 | // performs the actual binding to 'element' and determines if there are any |
| 2621 | // more items in the collection. |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 2622 | appendStmt(ExitConditionBlock, S); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2623 | Block = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2624 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2625 | // Walk the 'element' expression to see if there are any side-effects. We |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2626 | // generate new blocks as necessary. We DON'T add the statement by default to |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2627 | // the CFG unless it contains control-flow. |
Ted Kremenek | c14efa7 | 2011-08-17 21:04:19 +0000 | [diff] [blame] | 2628 | CFGBlock *EntryConditionBlock = Visit(S->getElement(), |
| 2629 | AddStmtChoice::NotAlwaysAdd); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2630 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2631 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2632 | return nullptr; |
| 2633 | Block = nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2634 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2635 | |
| 2636 | // The condition block is the implicit successor for the loop body as well as |
| 2637 | // any code above the loop. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2638 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2639 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2640 | // Now create the true branch. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2641 | { |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2642 | // Save the current values for Succ, continue and break targets. |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 2643 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2644 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 2645 | save_break(BreakJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2646 | |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 2647 | // Add an intermediate block between the BodyBlock and the |
| 2648 | // EntryConditionBlock to represent the "loop back" transition, for looping |
| 2649 | // back to the head of the loop. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2650 | CFGBlock *LoopBackBlock = nullptr; |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 2651 | Succ = LoopBackBlock = createBlock(); |
| 2652 | LoopBackBlock->setLoopTarget(S); |
| 2653 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2654 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 2655 | ContinueJumpTarget = JumpTarget(Succ, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2656 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2657 | CFGBlock *BodyBlock = addStmt(S->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2658 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2659 | if (!BodyBlock) |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 2660 | BodyBlock = ContinueJumpTarget.block; // can happen for "for (X in Y) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2661 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2662 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2663 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2664 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2665 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2666 | // This new body block is a successor to our "exit" condition block. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2667 | addSuccessor(ExitConditionBlock, BodyBlock); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2668 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2669 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2670 | // Link up the condition block with the code that follows the loop. |
| 2671 | // (the false branch). |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2672 | addSuccessor(ExitConditionBlock, LoopSuccessor); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2673 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2674 | // Now create a prologue block to contain the collection expression. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 2675 | Block = createBlock(); |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2676 | return addStmt(S->getCollection()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Ted Kremenek | 5022f1d | 2012-03-06 23:40:47 +0000 | [diff] [blame] | 2679 | CFGBlock *CFGBuilder::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) { |
| 2680 | // Inline the body. |
| 2681 | return addStmt(S->getSubStmt()); |
| 2682 | // TODO: consider adding cleanups for the end of @autoreleasepool scope. |
| 2683 | } |
| 2684 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2685 | CFGBlock *CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 2686 | // FIXME: Add locking 'primitives' to CFG for @synchronized. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2687 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 2688 | // Inline the body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2689 | CFGBlock *SyncBlock = addStmt(S->getSynchBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2690 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 2691 | // The sync body starts its own basic block. This makes it a little easier |
| 2692 | // for diagnostic clients. |
| 2693 | if (SyncBlock) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2694 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2695 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2696 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2697 | Block = nullptr; |
Ted Kremenek | ecc31c9 | 2010-05-13 16:38:08 +0000 | [diff] [blame] | 2698 | Succ = SyncBlock; |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 2699 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2700 | |
Ted Kremenek | ed12f1b | 2010-09-10 03:05:33 +0000 | [diff] [blame] | 2701 | // Add the @synchronized to the CFG. |
| 2702 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2703 | appendStmt(Block, S); |
Ted Kremenek | ed12f1b | 2010-09-10 03:05:33 +0000 | [diff] [blame] | 2704 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 2705 | // Inline the sync expression. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2706 | return addStmt(S->getSynchExpr()); |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 2707 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2708 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2709 | CFGBlock *CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2710 | // FIXME |
Ted Kremenek | 89be652 | 2009-04-07 04:26:02 +0000 | [diff] [blame] | 2711 | return NYS(); |
Ted Kremenek | 89cc8ea | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 2712 | } |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 2713 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2714 | CFGBlock *CFGBuilder::VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 2715 | autoCreateBlock(); |
| 2716 | |
| 2717 | // Add the PseudoObject as the last thing. |
| 2718 | appendStmt(Block, E); |
| 2719 | |
| 2720 | CFGBlock *lastBlock = Block; |
| 2721 | |
| 2722 | // Before that, evaluate all of the semantics in order. In |
| 2723 | // CFG-land, that means appending them in reverse order. |
| 2724 | for (unsigned i = E->getNumSemanticExprs(); i != 0; ) { |
| 2725 | Expr *Semantic = E->getSemanticExpr(--i); |
| 2726 | |
| 2727 | // If the semantic is an opaque value, we're being asked to bind |
| 2728 | // it to its source expression. |
| 2729 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic)) |
| 2730 | Semantic = OVE->getSourceExpr(); |
| 2731 | |
| 2732 | if (CFGBlock *B = Visit(Semantic)) |
| 2733 | lastBlock = B; |
| 2734 | } |
| 2735 | |
| 2736 | return lastBlock; |
| 2737 | } |
| 2738 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2739 | CFGBlock *CFGBuilder::VisitWhileStmt(WhileStmt *W) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2740 | CFGBlock *LoopSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2741 | |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 2742 | // Save local scope position because in case of condition variable ScopePos |
| 2743 | // won't be restored when traversing AST. |
| 2744 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 2745 | |
| 2746 | // Create local scope for possible condition variable. |
| 2747 | // Store scope position for continue statement. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2748 | LocalScope::const_iterator LoopBeginScopePos = ScopePos; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2749 | if (VarDecl *VD = W->getConditionVariable()) { |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 2750 | addLocalScopeForVarDecl(VD); |
| 2751 | addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W); |
| 2752 | } |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2753 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 2754 | // "while" is a control-flow statement. Thus we stop processing the current |
| 2755 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2756 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2757 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2758 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2759 | LoopSuccessor = Block; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2760 | Block = nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2761 | } else { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2762 | LoopSuccessor = Succ; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2763 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2764 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2765 | CFGBlock *BodyBlock = nullptr, *TransitionBlock = nullptr; |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2766 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2767 | // Process the loop body. |
| 2768 | { |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 2769 | assert(W->getBody()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2770 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2771 | // Save the current values for Block, Succ, continue and break targets. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2772 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 2773 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2774 | save_break(BreakJumpTarget); |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 2775 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2776 | // Create an empty block to represent the transition block for looping back |
| 2777 | // to the head of the loop. |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2778 | Succ = TransitionBlock = createBlock(false); |
| 2779 | TransitionBlock->setLoopTarget(W); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2780 | ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2781 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2782 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 2783 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2784 | |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 2785 | // Loop body should end with destructor of Condition variable (if any). |
| 2786 | addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W); |
| 2787 | |
| 2788 | // If body is not a compound statement create implicit scope |
| 2789 | // and add destructors. |
| 2790 | if (!isa<CompoundStmt>(W->getBody())) |
| 2791 | addLocalScopeAndDtors(W->getBody()); |
| 2792 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2793 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2794 | BodyBlock = addStmt(W->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2795 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 2796 | if (!BodyBlock) |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 2797 | BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;" |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2798 | else if (Block && badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2799 | return nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | // Because of short-circuit evaluation, the condition of the loop can span |
| 2803 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 2804 | // evaluate the condition. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2805 | CFGBlock *EntryConditionBlock = nullptr, *ExitConditionBlock = nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2806 | |
| 2807 | do { |
| 2808 | Expr *C = W->getCond(); |
| 2809 | |
| 2810 | // Specially handle logical operators, which have a slightly |
| 2811 | // more optimal CFG representation. |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 2812 | if (BinaryOperator *Cond = dyn_cast<BinaryOperator>(C->IgnoreParens())) |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2813 | if (Cond->isLogicalOp()) { |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 2814 | std::tie(EntryConditionBlock, ExitConditionBlock) = |
| 2815 | VisitLogicalOperator(Cond, W, BodyBlock, LoopSuccessor); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2816 | break; |
| 2817 | } |
| 2818 | |
| 2819 | // The default case when not handling logical operators. |
Ted Kremenek | 451c4d5 | 2012-10-12 22:56:26 +0000 | [diff] [blame] | 2820 | ExitConditionBlock = createBlock(false); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2821 | ExitConditionBlock->setTerminator(W); |
| 2822 | |
| 2823 | // Now add the actual condition to the condition block. |
| 2824 | // Because the condition itself may contain control-flow, new blocks may |
| 2825 | // be created. Thus we update "Succ" after adding the condition. |
| 2826 | Block = ExitConditionBlock; |
| 2827 | Block = EntryConditionBlock = addStmt(C); |
| 2828 | |
| 2829 | // If this block contains a condition variable, add both the condition |
| 2830 | // variable and initializer to the CFG. |
| 2831 | if (VarDecl *VD = W->getConditionVariable()) { |
| 2832 | if (Expr *Init = VD->getInit()) { |
| 2833 | autoCreateBlock(); |
| 2834 | appendStmt(Block, W->getConditionVariableDeclStmt()); |
| 2835 | EntryConditionBlock = addStmt(Init); |
| 2836 | assert(Block == EntryConditionBlock); |
| 2837 | } |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2838 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2839 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2840 | if (Block && badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2841 | return nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2842 | |
| 2843 | // See if this is a known constant. |
| 2844 | const TryResult& KnownVal = tryEvaluateBool(C); |
| 2845 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 2846 | // Add the loop body entry as a successor to the condition. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2847 | addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? nullptr : BodyBlock); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2848 | // Link up the condition block with the code that follows the loop. (the |
| 2849 | // false branch). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2850 | addSuccessor(ExitConditionBlock, |
| 2851 | KnownVal.isTrue() ? nullptr : LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2852 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2853 | } while(false); |
| 2854 | |
| 2855 | // Link up the loop-back block to the entry condition block. |
| 2856 | addSuccessor(TransitionBlock, EntryConditionBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2857 | |
| 2858 | // There can be no more statements in the condition block since we loop back |
| 2859 | // to this block. NULL out Block to force lazy creation of another block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2860 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2861 | |
Ted Kremenek | 1ce53c4 | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 2862 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 2863 | Succ = EntryConditionBlock; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2864 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2865 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | |
| 2867 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2868 | CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2869 | // FIXME: For now we pretend that @catch and the code it contains does not |
| 2870 | // exit. |
| 2871 | return Block; |
| 2872 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2873 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2874 | CFGBlock *CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 2875 | // FIXME: This isn't complete. We basically treat @throw like a return |
| 2876 | // statement. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2877 | |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 2878 | // 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] | 2879 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2880 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2881 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 2882 | // Create the new block. |
| 2883 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2884 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 2885 | // The Exit block is the only successor. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2886 | addSuccessor(Block, &cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2887 | |
| 2888 | // Add the statement to the block. This may create new blocks if S contains |
| 2889 | // control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2890 | return VisitStmt(S, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 2891 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2892 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2893 | CFGBlock *CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr *T) { |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 2894 | // 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] | 2895 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2896 | return nullptr; |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 2897 | |
| 2898 | // Create the new block. |
| 2899 | Block = createBlock(false); |
| 2900 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2901 | if (TryTerminatedBlock) |
| 2902 | // The current try statement is the only successor. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2903 | addSuccessor(Block, TryTerminatedBlock); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2904 | else |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 2905 | // otherwise the Exit block is the only successor. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2906 | addSuccessor(Block, &cfg->getExit()); |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 2907 | |
| 2908 | // Add the statement to the block. This may create new blocks if S contains |
| 2909 | // control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2910 | return VisitStmt(T, AddStmtChoice::AlwaysAdd); |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2913 | CFGBlock *CFGBuilder::VisitDoStmt(DoStmt *D) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2914 | CFGBlock *LoopSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2915 | |
Mike Stump | 8d50b6a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 2916 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 2917 | // current block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2918 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2919 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2920 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2921 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2922 | } else |
| 2923 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2924 | |
| 2925 | // Because of short-circuit evaluation, the condition of the loop can span |
| 2926 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 2927 | // evaluate the condition. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2928 | CFGBlock *ExitConditionBlock = createBlock(false); |
| 2929 | CFGBlock *EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2930 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2931 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2932 | ExitConditionBlock->setTerminator(D); |
| 2933 | |
| 2934 | // Now add the actual condition to the condition block. Because the condition |
| 2935 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2936 | if (Stmt *C = D->getCond()) { |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2937 | Block = ExitConditionBlock; |
| 2938 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2939 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2940 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2941 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2942 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2943 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2944 | |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 2945 | // The condition block is the implicit successor for the loop body. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2946 | Succ = EntryConditionBlock; |
| 2947 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2948 | // See if this is a known constant. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2949 | const TryResult &KnownVal = tryEvaluateBool(D->getCond()); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2950 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2951 | // Process the loop body. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2952 | CFGBlock *BodyBlock = nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2953 | { |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 2954 | assert(D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2955 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2956 | // Save the current values for Block, Succ, and continue and break targets |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2957 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 2958 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
| 2959 | save_break(BreakJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2960 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2961 | // All continues within this loop should go to the condition block |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2962 | ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2963 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2964 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2965 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2966 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2967 | // NULL out Block to force lazy instantiation of blocks for the body. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2968 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2969 | |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 2970 | // If body is not a compound statement create implicit scope |
| 2971 | // and add destructors. |
| 2972 | if (!isa<CompoundStmt>(D->getBody())) |
| 2973 | addLocalScopeAndDtors(D->getBody()); |
| 2974 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2975 | // 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] | 2976 | BodyBlock = addStmt(D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2977 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 2978 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 2979 | BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2980 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2981 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2982 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2983 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2984 | |
Daniel Marjamaki | 042a3c5 | 2016-10-03 08:28:51 +0000 | [diff] [blame] | 2985 | // Add an intermediate block between the BodyBlock and the |
| 2986 | // ExitConditionBlock to represent the "loop back" transition. Create an |
| 2987 | // empty block to represent the transition block for looping back to the |
| 2988 | // head of the loop. |
| 2989 | // FIXME: Can we do this more efficiently without adding another block? |
| 2990 | Block = nullptr; |
| 2991 | Succ = BodyBlock; |
| 2992 | CFGBlock *LoopBackBlock = createBlock(); |
| 2993 | LoopBackBlock->setLoopTarget(D); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2994 | |
Daniel Marjamaki | 042a3c5 | 2016-10-03 08:28:51 +0000 | [diff] [blame] | 2995 | if (!KnownVal.isFalse()) |
Ted Kremenek | 110974d | 2010-08-17 20:59:56 +0000 | [diff] [blame] | 2996 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2997 | addSuccessor(ExitConditionBlock, LoopBackBlock); |
Ted Kremenek | 110974d | 2010-08-17 20:59:56 +0000 | [diff] [blame] | 2998 | else |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2999 | addSuccessor(ExitConditionBlock, nullptr); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3000 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3001 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 3002 | // Link up the condition block with the code that follows the loop. |
| 3003 | // (the false branch). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3004 | addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? nullptr : LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3005 | |
| 3006 | // There can be no more statements in the body block(s) since we loop back to |
| 3007 | // the body. NULL out Block to force lazy creation of another block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3008 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3009 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3010 | // Return the loop body, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 3011 | Succ = BodyBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3012 | return BodyBlock; |
| 3013 | } |
| 3014 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3015 | CFGBlock *CFGBuilder::VisitContinueStmt(ContinueStmt *C) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3016 | // "continue" is a control-flow statement. Thus we stop processing the |
| 3017 | // current block. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3018 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3019 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3020 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3021 | // Now create a new block that ends with the continue statement. |
| 3022 | Block = createBlock(false); |
| 3023 | Block->setTerminator(C); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3024 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3025 | // 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] | 3026 | // incomplete AST. This means the CFG cannot be constructed. |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 3027 | if (ContinueJumpTarget.block) { |
| 3028 | addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C); |
| 3029 | addSuccessor(Block, ContinueJumpTarget.block); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3030 | } else |
Ted Kremenek | 882cf06 | 2009-04-07 18:53:24 +0000 | [diff] [blame] | 3031 | badCFG = true; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3032 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3033 | return Block; |
| 3034 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3036 | CFGBlock *CFGBuilder::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E, |
| 3037 | AddStmtChoice asc) { |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3038 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 3039 | if (asc.alwaysAdd(*this, E)) { |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3040 | autoCreateBlock(); |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 3041 | appendStmt(Block, E); |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3042 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3044 | // VLA types have expressions that must be evaluated. |
Ted Kremenek | 9eb0b7d | 2011-04-14 01:50:50 +0000 | [diff] [blame] | 3045 | CFGBlock *lastBlock = Block; |
| 3046 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3047 | if (E->isArgumentType()) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3048 | for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3049 | VA != nullptr; VA = FindVA(VA->getElementType().getTypePtr())) |
Ted Kremenek | 9eb0b7d | 2011-04-14 01:50:50 +0000 | [diff] [blame] | 3050 | lastBlock = addStmt(VA->getSizeExpr()); |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 3051 | } |
Ted Kremenek | 9eb0b7d | 2011-04-14 01:50:50 +0000 | [diff] [blame] | 3052 | return lastBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3053 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3055 | /// VisitStmtExpr - Utility method to handle (nested) statement |
| 3056 | /// expressions (a GCC extension). |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3057 | CFGBlock *CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 3058 | if (asc.alwaysAdd(*this, SE)) { |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3059 | autoCreateBlock(); |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 3060 | appendStmt(Block, SE); |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3061 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3062 | return VisitCompoundStmt(SE->getSubStmt()); |
| 3063 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3064 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3065 | CFGBlock *CFGBuilder::VisitSwitchStmt(SwitchStmt *Terminator) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3066 | // "switch" is a control-flow statement. Thus we stop processing the current |
| 3067 | // block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3068 | CFGBlock *SwitchSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3069 | |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3070 | // Save local scope position because in case of condition variable ScopePos |
| 3071 | // won't be restored when traversing AST. |
| 3072 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 3073 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3074 | // Create local scope for C++17 switch init-stmt if one exists. |
| 3075 | if (Stmt *Init = Terminator->getInit()) { |
| 3076 | LocalScope::const_iterator BeginScopePos = ScopePos; |
| 3077 | addLocalScopeForStmt(Init); |
| 3078 | addAutomaticObjDtors(ScopePos, BeginScopePos, Terminator); |
| 3079 | } |
| 3080 | |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3081 | // Create local scope for possible condition variable. |
| 3082 | // Store scope position. Add implicit destructor. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3083 | if (VarDecl *VD = Terminator->getConditionVariable()) { |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3084 | LocalScope::const_iterator SwitchBeginScopePos = ScopePos; |
| 3085 | addLocalScopeForVarDecl(VD); |
| 3086 | addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator); |
| 3087 | } |
| 3088 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3089 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3090 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3091 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3092 | SwitchSuccessor = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3093 | } else SwitchSuccessor = Succ; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3094 | |
| 3095 | // Save the current "switch" context. |
| 3096 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3097 | save_default(DefaultCaseBlock); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3098 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3099 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3100 | // Set the "default" case to be the block after the switch statement. If the |
| 3101 | // switch statement contains a "default:", this value will be overwritten with |
| 3102 | // the block for that code. |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3103 | DefaultCaseBlock = SwitchSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3104 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3105 | // Create a new block that will contain the switch statement. |
| 3106 | SwitchTerminatedBlock = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3107 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3108 | // Now process the switch body. The code after the switch is the implicit |
| 3109 | // successor. |
| 3110 | Succ = SwitchSuccessor; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3111 | BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3112 | |
| 3113 | // When visiting the body, the case statements should automatically get linked |
| 3114 | // up to the switch. We also don't keep a pointer to the body, since all |
| 3115 | // control-flow from the switch goes to case/default statements. |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 3116 | assert(Terminator->getBody() && "switch must contain a non-NULL body"); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3117 | Block = nullptr; |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3118 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3119 | // For pruning unreachable case statements, save the current state |
| 3120 | // for tracking the condition value. |
| 3121 | SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered, |
| 3122 | false); |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3123 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3124 | // Determine if the switch condition can be explicitly evaluated. |
| 3125 | assert(Terminator->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3126 | Expr::EvalResult result; |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3127 | bool b = tryEvaluate(Terminator->getCond(), result); |
| 3128 | SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3129 | b ? &result : nullptr); |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3130 | |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3131 | // If body is not a compound statement create implicit scope |
| 3132 | // and add destructors. |
| 3133 | if (!isa<CompoundStmt>(Terminator->getBody())) |
| 3134 | addLocalScopeAndDtors(Terminator->getBody()); |
| 3135 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3136 | addStmt(Terminator->getBody()); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3137 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3138 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3139 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3140 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3141 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3142 | // If we have no "default:" case, the default transition is to the code |
Ted Kremenek | 35c70f6 | 2011-03-16 04:32:01 +0000 | [diff] [blame] | 3143 | // following the switch body. Moreover, take into account if all the |
| 3144 | // cases of a switch are covered (e.g., switching on an enum value). |
David Majnemer | f69ce86 | 2013-06-04 17:38:44 +0000 | [diff] [blame] | 3145 | // |
| 3146 | // Note: We add a successor to a switch that is considered covered yet has no |
| 3147 | // case statements if the enumeration has no enumerators. |
| 3148 | bool SwitchAlwaysHasSuccessor = false; |
| 3149 | SwitchAlwaysHasSuccessor |= switchExclusivelyCovered; |
| 3150 | SwitchAlwaysHasSuccessor |= Terminator->isAllEnumCasesCovered() && |
| 3151 | Terminator->getSwitchCaseList(); |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 3152 | addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock, |
| 3153 | !SwitchAlwaysHasSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3154 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3155 | // Add the terminator and condition in the switch block. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3156 | SwitchTerminatedBlock->setTerminator(Terminator); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3157 | Block = SwitchTerminatedBlock; |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3158 | CFGBlock *LastBlock = addStmt(Terminator->getCond()); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 3159 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3160 | // If the SwitchStmt contains a condition variable, add both the |
Ted Kremenek | 8b5dc12 | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 3161 | // SwitchStmt and the condition variable initialization to the CFG. |
| 3162 | if (VarDecl *VD = Terminator->getConditionVariable()) { |
| 3163 | if (Expr *Init = VD->getInit()) { |
| 3164 | autoCreateBlock(); |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 3165 | appendStmt(Block, Terminator->getConditionVariableDeclStmt()); |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3166 | LastBlock = addStmt(Init); |
Ted Kremenek | 8b5dc12 | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 3167 | } |
| 3168 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 3169 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3170 | // Finally, if the SwitchStmt contains a C++17 init-stmt, add it to the CFG. |
| 3171 | if (Stmt *Init = Terminator->getInit()) { |
| 3172 | autoCreateBlock(); |
| 3173 | LastBlock = addStmt(Init); |
| 3174 | } |
| 3175 | |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3176 | return LastBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3177 | } |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3178 | |
| 3179 | static bool shouldAddCase(bool &switchExclusivelyCovered, |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3180 | const Expr::EvalResult *switchCond, |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3181 | const CaseStmt *CS, |
| 3182 | ASTContext &Ctx) { |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3183 | if (!switchCond) |
| 3184 | return true; |
| 3185 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3186 | bool addCase = false; |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3187 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3188 | if (!switchExclusivelyCovered) { |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3189 | if (switchCond->Val.isInt()) { |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3190 | // Evaluate the LHS of the case value. |
Richard Smith | faa32a9 | 2011-10-14 20:22:00 +0000 | [diff] [blame] | 3191 | const llvm::APSInt &lhsInt = CS->getLHS()->EvaluateKnownConstInt(Ctx); |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3192 | const llvm::APSInt &condInt = switchCond->Val.getInt(); |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3193 | |
| 3194 | if (condInt == lhsInt) { |
| 3195 | addCase = true; |
| 3196 | switchExclusivelyCovered = true; |
| 3197 | } |
Devin Coughlin | eb538ab | 2015-09-22 20:31:19 +0000 | [diff] [blame] | 3198 | else if (condInt > lhsInt) { |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3199 | if (const Expr *RHS = CS->getRHS()) { |
| 3200 | // Evaluate the RHS of the case value. |
Richard Smith | faa32a9 | 2011-10-14 20:22:00 +0000 | [diff] [blame] | 3201 | const llvm::APSInt &V2 = RHS->EvaluateKnownConstInt(Ctx); |
Devin Coughlin | eb538ab | 2015-09-22 20:31:19 +0000 | [diff] [blame] | 3202 | if (V2 >= condInt) { |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3203 | addCase = true; |
| 3204 | switchExclusivelyCovered = true; |
| 3205 | } |
| 3206 | } |
| 3207 | } |
| 3208 | } |
| 3209 | else |
| 3210 | addCase = true; |
| 3211 | } |
| 3212 | return addCase; |
| 3213 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3214 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3215 | CFGBlock *CFGBuilder::VisitCaseStmt(CaseStmt *CS) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3216 | // CaseStmts are essentially labels, so they are the first statement in a |
| 3217 | // block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3218 | CFGBlock *TopBlock = nullptr, *LastBlock = nullptr; |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3219 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3220 | if (Stmt *Sub = CS->getSubStmt()) { |
| 3221 | // For deeply nested chains of CaseStmts, instead of doing a recursion |
| 3222 | // (which can blow out the stack), manually unroll and create blocks |
| 3223 | // along the way. |
| 3224 | while (isa<CaseStmt>(Sub)) { |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3225 | CFGBlock *currentBlock = createBlock(false); |
| 3226 | currentBlock->setLabel(CS); |
Ted Kremenek | 55e91e8 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 3227 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3228 | if (TopBlock) |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3229 | addSuccessor(LastBlock, currentBlock); |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3230 | else |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3231 | TopBlock = currentBlock; |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3232 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3233 | addSuccessor(SwitchTerminatedBlock, |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3234 | shouldAddCase(switchExclusivelyCovered, switchCond, |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3235 | CS, *Context) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3236 | ? currentBlock : nullptr); |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3237 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3238 | LastBlock = currentBlock; |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3239 | CS = cast<CaseStmt>(Sub); |
| 3240 | Sub = CS->getSubStmt(); |
| 3241 | } |
| 3242 | |
| 3243 | addStmt(Sub); |
| 3244 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3246 | CFGBlock *CaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3247 | if (!CaseBlock) |
| 3248 | CaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Cases statements partition blocks, so this is the top of the basic block we |
| 3251 | // were processing (the "case XXX:" is the label). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3252 | CaseBlock->setLabel(CS); |
| 3253 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3254 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3255 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3256 | |
| 3257 | // Add this block to the list of successors for the block with the switch |
| 3258 | // statement. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3259 | assert(SwitchTerminatedBlock); |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 3260 | addSuccessor(SwitchTerminatedBlock, CaseBlock, |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3261 | shouldAddCase(switchExclusivelyCovered, switchCond, |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 3262 | CS, *Context)); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3263 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3264 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3265 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3266 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3267 | if (TopBlock) { |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3268 | addSuccessor(LastBlock, CaseBlock); |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3269 | Succ = TopBlock; |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 3270 | } else { |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3271 | // This block is now the implicit successor of other blocks. |
| 3272 | Succ = CaseBlock; |
| 3273 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3274 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3275 | return Succ; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3276 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3277 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3278 | CFGBlock *CFGBuilder::VisitDefaultStmt(DefaultStmt *Terminator) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3279 | if (Terminator->getSubStmt()) |
| 3280 | addStmt(Terminator->getSubStmt()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3282 | DefaultCaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3283 | |
| 3284 | if (!DefaultCaseBlock) |
| 3285 | DefaultCaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3286 | |
| 3287 | // Default statements partition blocks, so this is the top of the basic block |
| 3288 | // we were processing (the "default:" is the label). |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3289 | DefaultCaseBlock->setLabel(Terminator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3291 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3292 | return nullptr; |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3293 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3294 | // Unlike case statements, we don't add the default block to the successors |
| 3295 | // for the switch statement immediately. This is done when we finish |
| 3296 | // processing the switch statement. This allows for the default case |
| 3297 | // (including a fall-through to the code after the switch statement) to always |
| 3298 | // be the last successor of a switch-terminated block. |
| 3299 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3300 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3301 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3302 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3303 | // This block is now the implicit successor of other blocks. |
| 3304 | Succ = DefaultCaseBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3305 | |
| 3306 | return DefaultCaseBlock; |
Ted Kremenek | 9682be1 | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 3307 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3308 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3309 | CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) { |
| 3310 | // "try"/"catch" is a control-flow statement. Thus we stop processing the |
| 3311 | // current block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3312 | CFGBlock *TrySuccessor = nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3313 | |
| 3314 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3315 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3316 | return nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3317 | TrySuccessor = Block; |
| 3318 | } else TrySuccessor = Succ; |
| 3319 | |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3320 | CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3321 | |
| 3322 | // Create a new block that will contain the try statement. |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 3323 | CFGBlock *NewTryTerminatedBlock = createBlock(false); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3324 | // Add the terminator in the try block. |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 3325 | NewTryTerminatedBlock->setTerminator(Terminator); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3326 | |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3327 | bool HasCatchAll = false; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3328 | for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) { |
| 3329 | // The code after the try is the implicit successor. |
| 3330 | Succ = TrySuccessor; |
| 3331 | CXXCatchStmt *CS = Terminator->getHandler(h); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3332 | if (CS->getExceptionDecl() == nullptr) { |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3333 | HasCatchAll = true; |
| 3334 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3335 | Block = nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3336 | CFGBlock *CatchBlock = VisitCXXCatchStmt(CS); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3337 | if (!CatchBlock) |
| 3338 | return nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3339 | // Add this block to the list of successors for the block with the try |
| 3340 | // statement. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3341 | addSuccessor(NewTryTerminatedBlock, CatchBlock); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3342 | } |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3343 | if (!HasCatchAll) { |
| 3344 | if (PrevTryTerminatedBlock) |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3345 | addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock); |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3346 | else |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3347 | addSuccessor(NewTryTerminatedBlock, &cfg->getExit()); |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3348 | } |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3349 | |
| 3350 | // The code after the try is the implicit successor. |
| 3351 | Succ = TrySuccessor; |
| 3352 | |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 3353 | // Save the current "try" context. |
Ted Kremenek | 6b9964d | 2011-08-23 23:05:07 +0000 | [diff] [blame] | 3354 | SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock, NewTryTerminatedBlock); |
| 3355 | cfg->addTryDispatchBlock(TryTerminatedBlock); |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 3356 | |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 3357 | assert(Terminator->getTryBlock() && "try must contain a non-NULL body"); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3358 | Block = nullptr; |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3359 | return addStmt(Terminator->getTryBlock()); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3362 | CFGBlock *CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt *CS) { |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3363 | // CXXCatchStmt are treated like labels, so they are the first statement in a |
| 3364 | // block. |
| 3365 | |
Marcin Swiderski | 3546b1a | 2010-10-01 01:46:52 +0000 | [diff] [blame] | 3366 | // Save local scope position because in case of exception variable ScopePos |
| 3367 | // won't be restored when traversing AST. |
| 3368 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 3369 | |
| 3370 | // Create local scope for possible exception variable. |
| 3371 | // Store scope position. Add implicit destructor. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3372 | if (VarDecl *VD = CS->getExceptionDecl()) { |
Marcin Swiderski | 3546b1a | 2010-10-01 01:46:52 +0000 | [diff] [blame] | 3373 | LocalScope::const_iterator BeginScopePos = ScopePos; |
| 3374 | addLocalScopeForVarDecl(VD); |
| 3375 | addAutomaticObjDtors(ScopePos, BeginScopePos, CS); |
| 3376 | } |
| 3377 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3378 | if (CS->getHandlerBlock()) |
| 3379 | addStmt(CS->getHandlerBlock()); |
| 3380 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3381 | CFGBlock *CatchBlock = Block; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3382 | if (!CatchBlock) |
| 3383 | CatchBlock = createBlock(); |
Ted Kremenek | 8fdb59f | 2012-03-10 01:34:17 +0000 | [diff] [blame] | 3384 | |
| 3385 | // CXXCatchStmt is more than just a label. They have semantic meaning |
| 3386 | // as well, as they implicitly "initialize" the catch variable. Add |
| 3387 | // it to the CFG as a CFGElement so that the control-flow of these |
| 3388 | // semantics gets captured. |
| 3389 | appendStmt(CatchBlock, CS); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3390 | |
Ted Kremenek | 8fdb59f | 2012-03-10 01:34:17 +0000 | [diff] [blame] | 3391 | // Also add the CXXCatchStmt as a label, to mirror handling of regular |
| 3392 | // labels. |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3393 | CatchBlock->setLabel(CS); |
| 3394 | |
Ted Kremenek | 8fdb59f | 2012-03-10 01:34:17 +0000 | [diff] [blame] | 3395 | // Bail out if the CFG is bad. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3396 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3397 | return nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3398 | |
| 3399 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3400 | Block = nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3401 | |
| 3402 | return CatchBlock; |
| 3403 | } |
| 3404 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3405 | CFGBlock *CFGBuilder::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3406 | // C++0x for-range statements are specified as [stmt.ranged]: |
| 3407 | // |
| 3408 | // { |
| 3409 | // auto && __range = range-init; |
| 3410 | // for ( auto __begin = begin-expr, |
| 3411 | // __end = end-expr; |
| 3412 | // __begin != __end; |
| 3413 | // ++__begin ) { |
| 3414 | // for-range-declaration = *__begin; |
| 3415 | // statement |
| 3416 | // } |
| 3417 | // } |
| 3418 | |
| 3419 | // Save local scope position before the addition of the implicit variables. |
| 3420 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 3421 | |
| 3422 | // Create local scopes and destructors for range, begin and end variables. |
| 3423 | if (Stmt *Range = S->getRangeStmt()) |
| 3424 | addLocalScopeForStmt(Range); |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 3425 | if (Stmt *Begin = S->getBeginStmt()) |
| 3426 | addLocalScopeForStmt(Begin); |
| 3427 | if (Stmt *End = S->getEndStmt()) |
| 3428 | addLocalScopeForStmt(End); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3429 | addAutomaticObjDtors(ScopePos, save_scope_pos.get(), S); |
| 3430 | |
| 3431 | LocalScope::const_iterator ContinueScopePos = ScopePos; |
| 3432 | |
| 3433 | // "for" is a control-flow statement. Thus we stop processing the current |
| 3434 | // block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3435 | CFGBlock *LoopSuccessor = nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3436 | if (Block) { |
| 3437 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3438 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3439 | LoopSuccessor = Block; |
| 3440 | } else |
| 3441 | LoopSuccessor = Succ; |
| 3442 | |
| 3443 | // Save the current value for the break targets. |
| 3444 | // All breaks should go to the code following the loop. |
| 3445 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
| 3446 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
| 3447 | |
| 3448 | // The block for the __begin != __end expression. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3449 | CFGBlock *ConditionBlock = createBlock(false); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3450 | ConditionBlock->setTerminator(S); |
| 3451 | |
| 3452 | // Now add the actual condition to the condition block. |
| 3453 | if (Expr *C = S->getCond()) { |
| 3454 | Block = ConditionBlock; |
| 3455 | CFGBlock *BeginConditionBlock = addStmt(C); |
| 3456 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3457 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3458 | assert(BeginConditionBlock == ConditionBlock && |
| 3459 | "condition block in for-range was unexpectedly complex"); |
| 3460 | (void)BeginConditionBlock; |
| 3461 | } |
| 3462 | |
| 3463 | // The condition block is the implicit successor for the loop body as well as |
| 3464 | // any code above the loop. |
| 3465 | Succ = ConditionBlock; |
| 3466 | |
| 3467 | // See if this is a known constant. |
| 3468 | TryResult KnownVal(true); |
| 3469 | |
| 3470 | if (S->getCond()) |
| 3471 | KnownVal = tryEvaluateBool(S->getCond()); |
| 3472 | |
| 3473 | // Now create the loop body. |
| 3474 | { |
| 3475 | assert(S->getBody()); |
| 3476 | |
| 3477 | // Save the current values for Block, Succ, and continue targets. |
| 3478 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 3479 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget); |
| 3480 | |
| 3481 | // Generate increment code in its own basic block. This is the target of |
| 3482 | // continue statements. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3483 | Block = nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3484 | Succ = addStmt(S->getInc()); |
Alexander Kornienko | ff2046a | 2016-07-08 10:50:51 +0000 | [diff] [blame] | 3485 | if (badCFG) |
| 3486 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3487 | ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos); |
| 3488 | |
| 3489 | // The starting block for the loop increment is the block that should |
| 3490 | // represent the 'loop target' for looping back to the start of the loop. |
| 3491 | ContinueJumpTarget.block->setLoopTarget(S); |
| 3492 | |
| 3493 | // Finish up the increment block and prepare to start the loop body. |
| 3494 | assert(Block); |
| 3495 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3496 | return nullptr; |
| 3497 | Block = nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3498 | |
| 3499 | // Add implicit scope and dtors for loop variable. |
| 3500 | addLocalScopeAndDtors(S->getLoopVarStmt()); |
| 3501 | |
| 3502 | // Populate a new block to contain the loop body and loop variable. |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3503 | addStmt(S->getBody()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3504 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3505 | return nullptr; |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3506 | CFGBlock *LoopVarStmtBlock = addStmt(S->getLoopVarStmt()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3507 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3508 | return nullptr; |
| 3509 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3510 | // This new body block is a successor to our condition block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3511 | addSuccessor(ConditionBlock, |
| 3512 | KnownVal.isFalse() ? nullptr : LoopVarStmtBlock); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | // Link up the condition block with the code that follows the loop (the |
| 3516 | // false branch). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3517 | addSuccessor(ConditionBlock, KnownVal.isTrue() ? nullptr : LoopSuccessor); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3518 | |
| 3519 | // Add the initialization statements. |
| 3520 | Block = createBlock(); |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 3521 | addStmt(S->getBeginStmt()); |
| 3522 | addStmt(S->getEndStmt()); |
Richard Smith | 0c502d2 | 2011-04-18 15:49:25 +0000 | [diff] [blame] | 3523 | return addStmt(S->getRangeStmt()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3526 | CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E, |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3527 | AddStmtChoice asc) { |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 3528 | if (BuildOpts.AddTemporaryDtors) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3529 | // If adding implicit destructors visit the full expression for adding |
| 3530 | // destructors of temporaries. |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3531 | TempDtorContext Context; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3532 | VisitForTemporaryDtors(E->getSubExpr(), false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3533 | |
| 3534 | // Full expression has to be added as CFGStmt so it will be sequenced |
| 3535 | // before destructors of it's temporaries. |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 3536 | asc = asc.withAlwaysAdd(true); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3537 | } |
| 3538 | return Visit(E->getSubExpr(), asc); |
| 3539 | } |
| 3540 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3541 | CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E, |
| 3542 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 3543 | if (asc.alwaysAdd(*this, E)) { |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3544 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 3545 | appendStmt(Block, E); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3546 | |
| 3547 | // We do not want to propagate the AlwaysAdd property. |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 3548 | asc = asc.withAlwaysAdd(false); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3549 | } |
| 3550 | return Visit(E->getSubExpr(), asc); |
| 3551 | } |
| 3552 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3553 | CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C, |
| 3554 | AddStmtChoice asc) { |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3555 | autoCreateBlock(); |
Zhongxing Xu | f0cb43f | 2012-01-11 02:39:07 +0000 | [diff] [blame] | 3556 | appendStmt(Block, C); |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 3557 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3558 | return VisitChildren(C); |
| 3559 | } |
| 3560 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 3561 | CFGBlock *CFGBuilder::VisitCXXNewExpr(CXXNewExpr *NE, |
| 3562 | AddStmtChoice asc) { |
| 3563 | |
| 3564 | autoCreateBlock(); |
| 3565 | appendStmt(Block, NE); |
Jordan Rose | 6f5f719 | 2014-01-14 17:29:12 +0000 | [diff] [blame] | 3566 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 3567 | if (NE->getInitializer()) |
Jordan Rose | 6f5f719 | 2014-01-14 17:29:12 +0000 | [diff] [blame] | 3568 | Block = Visit(NE->getInitializer()); |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 3569 | if (BuildOpts.AddCXXNewAllocator) |
| 3570 | appendNewAllocator(Block, NE); |
| 3571 | if (NE->isArray()) |
Jordan Rose | 6f5f719 | 2014-01-14 17:29:12 +0000 | [diff] [blame] | 3572 | Block = Visit(NE->getArraySize()); |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 3573 | for (CXXNewExpr::arg_iterator I = NE->placement_arg_begin(), |
| 3574 | E = NE->placement_arg_end(); I != E; ++I) |
Jordan Rose | 6f5f719 | 2014-01-14 17:29:12 +0000 | [diff] [blame] | 3575 | Block = Visit(*I); |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 3576 | return Block; |
| 3577 | } |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 3578 | |
| 3579 | CFGBlock *CFGBuilder::VisitCXXDeleteExpr(CXXDeleteExpr *DE, |
| 3580 | AddStmtChoice asc) { |
| 3581 | autoCreateBlock(); |
| 3582 | appendStmt(Block, DE); |
| 3583 | QualType DTy = DE->getDestroyedType(); |
Martin Bohme | f44cde8 | 2016-12-05 11:33:19 +0000 | [diff] [blame^] | 3584 | if (!DTy.isNull()) { |
| 3585 | DTy = DTy.getNonReferenceType(); |
| 3586 | CXXRecordDecl *RD = Context->getBaseElementType(DTy)->getAsCXXRecordDecl(); |
| 3587 | if (RD) { |
| 3588 | if (RD->isCompleteDefinition() && !RD->hasTrivialDestructor()) |
| 3589 | appendDeleteDtor(Block, RD, DE); |
| 3590 | } |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | return VisitChildren(DE); |
| 3594 | } |
| 3595 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3596 | CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E, |
| 3597 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 3598 | if (asc.alwaysAdd(*this, E)) { |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3599 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 3600 | appendStmt(Block, E); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3601 | // We do not want to propagate the AlwaysAdd property. |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 3602 | asc = asc.withAlwaysAdd(false); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3603 | } |
| 3604 | return Visit(E->getSubExpr(), asc); |
| 3605 | } |
| 3606 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3607 | CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C, |
| 3608 | AddStmtChoice asc) { |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3609 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 3610 | appendStmt(Block, C); |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3611 | return VisitChildren(C); |
| 3612 | } |
| 3613 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3614 | CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E, |
| 3615 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 3616 | if (asc.alwaysAdd(*this, E)) { |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3617 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 3618 | appendStmt(Block, E); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3619 | } |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 3620 | return Visit(E->getSubExpr(), AddStmtChoice()); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3623 | CFGBlock *CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt *I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3624 | // Lazily create the indirect-goto dispatch block if there isn't one already. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3625 | CFGBlock *IBlock = cfg->getIndirectGotoBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3626 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 3627 | if (!IBlock) { |
| 3628 | IBlock = createBlock(false); |
| 3629 | cfg->setIndirectGotoBlock(IBlock); |
| 3630 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3631 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 3632 | // IndirectGoto is a control-flow statement. Thus we stop processing the |
| 3633 | // current block and create a new one. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3634 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3635 | return nullptr; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3636 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 3637 | Block = createBlock(false); |
| 3638 | Block->setTerminator(I); |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3639 | addSuccessor(Block, IBlock); |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 3640 | return addStmt(I->getTarget()); |
| 3641 | } |
| 3642 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3643 | CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary, |
| 3644 | TempDtorContext &Context) { |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 3645 | assert(BuildOpts.AddImplicitDtors && BuildOpts.AddTemporaryDtors); |
| 3646 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3647 | tryAgain: |
| 3648 | if (!E) { |
| 3649 | badCFG = true; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3650 | return nullptr; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3651 | } |
| 3652 | switch (E->getStmtClass()) { |
| 3653 | default: |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3654 | return VisitChildrenForTemporaryDtors(E, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3655 | |
| 3656 | case Stmt::BinaryOperatorClass: |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3657 | return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E), |
| 3658 | Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3659 | |
| 3660 | case Stmt::CXXBindTemporaryExprClass: |
| 3661 | return VisitCXXBindTemporaryExprForTemporaryDtors( |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3662 | cast<CXXBindTemporaryExpr>(E), BindToTemporary, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3663 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3664 | case Stmt::BinaryConditionalOperatorClass: |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3665 | case Stmt::ConditionalOperatorClass: |
| 3666 | return VisitConditionalOperatorForTemporaryDtors( |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3667 | cast<AbstractConditionalOperator>(E), BindToTemporary, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3668 | |
| 3669 | case Stmt::ImplicitCastExprClass: |
| 3670 | // For implicit cast we want BindToTemporary to be passed further. |
| 3671 | E = cast<CastExpr>(E)->getSubExpr(); |
| 3672 | goto tryAgain; |
| 3673 | |
Manuel Klimek | b0042c4 | 2014-07-30 08:34:42 +0000 | [diff] [blame] | 3674 | case Stmt::CXXFunctionalCastExprClass: |
| 3675 | // For functional cast we want BindToTemporary to be passed further. |
| 3676 | E = cast<CXXFunctionalCastExpr>(E)->getSubExpr(); |
| 3677 | goto tryAgain; |
| 3678 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3679 | case Stmt::ParenExprClass: |
| 3680 | E = cast<ParenExpr>(E)->getSubExpr(); |
| 3681 | goto tryAgain; |
Richard Smith | 4137af2 | 2014-07-27 05:12:49 +0000 | [diff] [blame] | 3682 | |
Manuel Klimek | b0042c4 | 2014-07-30 08:34:42 +0000 | [diff] [blame] | 3683 | case Stmt::MaterializeTemporaryExprClass: { |
| 3684 | const MaterializeTemporaryExpr* MTE = cast<MaterializeTemporaryExpr>(E); |
| 3685 | BindToTemporary = (MTE->getStorageDuration() != SD_FullExpression); |
| 3686 | SmallVector<const Expr *, 2> CommaLHSs; |
| 3687 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 3688 | // Find the expression whose lifetime needs to be extended. |
| 3689 | E = const_cast<Expr *>( |
| 3690 | cast<MaterializeTemporaryExpr>(E) |
| 3691 | ->GetTemporaryExpr() |
| 3692 | ->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 3693 | // Visit the skipped comma operator left-hand sides for other temporaries. |
| 3694 | for (const Expr *CommaLHS : CommaLHSs) { |
| 3695 | VisitForTemporaryDtors(const_cast<Expr *>(CommaLHS), |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3696 | /*BindToTemporary=*/false, Context); |
Manuel Klimek | b0042c4 | 2014-07-30 08:34:42 +0000 | [diff] [blame] | 3697 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3698 | goto tryAgain; |
Manuel Klimek | b0042c4 | 2014-07-30 08:34:42 +0000 | [diff] [blame] | 3699 | } |
Richard Smith | 4137af2 | 2014-07-27 05:12:49 +0000 | [diff] [blame] | 3700 | |
| 3701 | case Stmt::BlockExprClass: |
| 3702 | // Don't recurse into blocks; their subexpressions don't get evaluated |
| 3703 | // here. |
| 3704 | return Block; |
| 3705 | |
| 3706 | case Stmt::LambdaExprClass: { |
| 3707 | // For lambda expressions, only recurse into the capture initializers, |
| 3708 | // and not the body. |
| 3709 | auto *LE = cast<LambdaExpr>(E); |
| 3710 | CFGBlock *B = Block; |
| 3711 | for (Expr *Init : LE->capture_inits()) { |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3712 | if (CFGBlock *R = VisitForTemporaryDtors( |
| 3713 | Init, /*BindToTemporary=*/false, Context)) |
Richard Smith | 4137af2 | 2014-07-27 05:12:49 +0000 | [diff] [blame] | 3714 | B = R; |
| 3715 | } |
| 3716 | return B; |
| 3717 | } |
| 3718 | |
| 3719 | case Stmt::CXXDefaultArgExprClass: |
| 3720 | E = cast<CXXDefaultArgExpr>(E)->getExpr(); |
| 3721 | goto tryAgain; |
| 3722 | |
| 3723 | case Stmt::CXXDefaultInitExprClass: |
| 3724 | E = cast<CXXDefaultInitExpr>(E)->getExpr(); |
| 3725 | goto tryAgain; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3726 | } |
| 3727 | } |
| 3728 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3729 | CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E, |
| 3730 | TempDtorContext &Context) { |
| 3731 | if (isa<LambdaExpr>(E)) { |
| 3732 | // Do not visit the children of lambdas; they have their own CFGs. |
| 3733 | return Block; |
| 3734 | } |
| 3735 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3736 | // When visiting children for destructors we want to visit them in reverse |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 3737 | // order that they will appear in the CFG. Because the CFG is built |
| 3738 | // bottom-up, this means we visit them in their natural order, which |
| 3739 | // reverses them in the CFG. |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3740 | CFGBlock *B = Block; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 3741 | for (Stmt *Child : E->children()) |
| 3742 | if (Child) |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3743 | if (CFGBlock *R = VisitForTemporaryDtors(Child, false, Context)) |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 3744 | B = R; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 3745 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3746 | return B; |
| 3747 | } |
| 3748 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3749 | CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors( |
| 3750 | BinaryOperator *E, TempDtorContext &Context) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3751 | if (E->isLogicalOp()) { |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3752 | VisitForTemporaryDtors(E->getLHS(), false, Context); |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 3753 | TryResult RHSExecuted = tryEvaluateBool(E->getLHS()); |
| 3754 | if (RHSExecuted.isKnown() && E->getOpcode() == BO_LOr) |
| 3755 | RHSExecuted.negate(); |
Manuel Klimek | 7c03013 | 2014-08-07 16:05:51 +0000 | [diff] [blame] | 3756 | |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 3757 | // We do not know at CFG-construction time whether the right-hand-side was |
| 3758 | // executed, thus we add a branch node that depends on the temporary |
| 3759 | // constructor call. |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3760 | TempDtorContext RHSContext( |
| 3761 | bothKnownTrue(Context.KnownExecuted, RHSExecuted)); |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 3762 | VisitForTemporaryDtors(E->getRHS(), false, RHSContext); |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3763 | InsertTempDtorDecisionBlock(RHSContext); |
Manuel Klimek | 7c03013 | 2014-08-07 16:05:51 +0000 | [diff] [blame] | 3764 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3765 | return Block; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 3768 | if (E->isAssignmentOp()) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3769 | // For assignment operator (=) LHS expression is visited |
| 3770 | // before RHS expression. For destructors visit them in reverse order. |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3771 | CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), false, Context); |
| 3772 | CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS(), false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3773 | return LHSBlock ? LHSBlock : RHSBlock; |
| 3774 | } |
| 3775 | |
| 3776 | // For any other binary operator RHS expression is visited before |
| 3777 | // LHS expression (order of children). For destructors visit them in reverse |
| 3778 | // order. |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3779 | CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS(), false, Context); |
| 3780 | CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3781 | return RHSBlock ? RHSBlock : LHSBlock; |
| 3782 | } |
| 3783 | |
| 3784 | CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors( |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3785 | CXXBindTemporaryExpr *E, bool BindToTemporary, TempDtorContext &Context) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3786 | // First add destructors for temporaries in subexpression. |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3787 | CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr(), false, Context); |
Zhongxing Xu | fee455f | 2010-11-14 15:23:50 +0000 | [diff] [blame] | 3788 | if (!BindToTemporary) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3789 | // If lifetime of temporary is not prolonged (by assigning to constant |
| 3790 | // reference) add destructor for it. |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 3791 | |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 3792 | const CXXDestructorDecl *Dtor = E->getTemporary()->getDestructor(); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3793 | |
Richard Trieu | 95a192a | 2015-05-28 00:14:02 +0000 | [diff] [blame] | 3794 | if (Dtor->getParent()->isAnyDestructorNoReturn()) { |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3795 | // If the destructor is marked as a no-return destructor, we need to |
| 3796 | // create a new block for the destructor which does not have as a |
| 3797 | // successor anything built thus far. Control won't flow out of this |
| 3798 | // block. |
| 3799 | if (B) Succ = B; |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 3800 | Block = createNoReturnBlock(); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3801 | } else if (Context.needsTempDtorBranch()) { |
| 3802 | // If we need to introduce a branch, we add a new block that we will hook |
| 3803 | // up to a decision block later. |
| 3804 | if (B) Succ = B; |
| 3805 | Block = createBlock(); |
Ted Kremenek | ff909f9 | 2014-03-08 02:22:25 +0000 | [diff] [blame] | 3806 | } else { |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 3807 | autoCreateBlock(); |
Ted Kremenek | ff909f9 | 2014-03-08 02:22:25 +0000 | [diff] [blame] | 3808 | } |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3809 | if (Context.needsTempDtorBranch()) { |
| 3810 | Context.setDecisionPoint(Succ, E); |
| 3811 | } |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 3812 | appendTemporaryDtor(Block, E); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3813 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3814 | B = Block; |
| 3815 | } |
| 3816 | return B; |
| 3817 | } |
| 3818 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3819 | void CFGBuilder::InsertTempDtorDecisionBlock(const TempDtorContext &Context, |
| 3820 | CFGBlock *FalseSucc) { |
| 3821 | if (!Context.TerminatorExpr) { |
| 3822 | // If no temporary was found, we do not need to insert a decision point. |
| 3823 | return; |
| 3824 | } |
| 3825 | assert(Context.TerminatorExpr); |
| 3826 | CFGBlock *Decision = createBlock(false); |
| 3827 | Decision->setTerminator(CFGTerminator(Context.TerminatorExpr, true)); |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3828 | addSuccessor(Decision, Block, !Context.KnownExecuted.isFalse()); |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 3829 | addSuccessor(Decision, FalseSucc ? FalseSucc : Context.Succ, |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3830 | !Context.KnownExecuted.isTrue()); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3831 | Block = Decision; |
| 3832 | } |
| 3833 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3834 | CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors( |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3835 | AbstractConditionalOperator *E, bool BindToTemporary, |
| 3836 | TempDtorContext &Context) { |
| 3837 | VisitForTemporaryDtors(E->getCond(), false, Context); |
| 3838 | CFGBlock *ConditionBlock = Block; |
| 3839 | CFGBlock *ConditionSucc = Succ; |
Manuel Klimek | 0ce9108 | 2014-08-07 14:25:43 +0000 | [diff] [blame] | 3840 | TryResult ConditionVal = tryEvaluateBool(E->getCond()); |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 3841 | TryResult NegatedVal = ConditionVal; |
| 3842 | if (NegatedVal.isKnown()) NegatedVal.negate(); |
Manuel Klimek | cadc603 | 2014-08-07 17:02:21 +0000 | [diff] [blame] | 3843 | |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3844 | TempDtorContext TrueContext( |
| 3845 | bothKnownTrue(Context.KnownExecuted, ConditionVal)); |
Manuel Klimek | cadc603 | 2014-08-07 17:02:21 +0000 | [diff] [blame] | 3846 | VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary, TrueContext); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3847 | CFGBlock *TrueBlock = Block; |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 3848 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3849 | Block = ConditionBlock; |
| 3850 | Succ = ConditionSucc; |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3851 | TempDtorContext FalseContext( |
| 3852 | bothKnownTrue(Context.KnownExecuted, NegatedVal)); |
Manuel Klimek | cadc603 | 2014-08-07 17:02:21 +0000 | [diff] [blame] | 3853 | VisitForTemporaryDtors(E->getFalseExpr(), BindToTemporary, FalseContext); |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 3854 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3855 | if (TrueContext.TerminatorExpr && FalseContext.TerminatorExpr) { |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3856 | InsertTempDtorDecisionBlock(FalseContext, TrueBlock); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3857 | } else if (TrueContext.TerminatorExpr) { |
| 3858 | Block = TrueBlock; |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3859 | InsertTempDtorDecisionBlock(TrueContext); |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 3860 | } else { |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3861 | InsertTempDtorDecisionBlock(FalseContext); |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 3862 | } |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3863 | return Block; |
| 3864 | } |
| 3865 | |
Ted Kremenek | 04cca64 | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 3866 | } // end anonymous namespace |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 3867 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3868 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has |
| 3869 | /// no successors or predecessors. If this is the first block created in the |
| 3870 | /// CFG, it is automatically set to be the Entry and Exit of the CFG. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3871 | CFGBlock *CFG::createBlock() { |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 3872 | bool first_block = begin() == end(); |
| 3873 | |
| 3874 | // Create the block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 3875 | CFGBlock *Mem = getAllocator().Allocate<CFGBlock>(); |
Anna Zaks | 02a1fc1 | 2011-12-05 21:33:11 +0000 | [diff] [blame] | 3876 | new (Mem) CFGBlock(NumBlockIDs++, BlkBVC, this); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 3877 | Blocks.push_back(Mem, BlkBVC); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 3878 | |
| 3879 | // 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] | 3880 | if (first_block) |
| 3881 | Entry = Exit = &back(); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 3882 | |
| 3883 | // Return the block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 3884 | return &back(); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 3887 | /// buildCFG - Constructs a CFG from an AST. |
| 3888 | std::unique_ptr<CFG> CFG::buildCFG(const Decl *D, Stmt *Statement, |
| 3889 | ASTContext *C, const BuildOptions &BO) { |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 3890 | CFGBuilder Builder(C, BO); |
| 3891 | return Builder.buildCFG(D, Statement); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Ted Kremenek | 8cfe207 | 2011-03-03 01:21:32 +0000 | [diff] [blame] | 3894 | const CXXDestructorDecl * |
| 3895 | CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const { |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3896 | switch (getKind()) { |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3897 | case CFGElement::Statement: |
| 3898 | case CFGElement::Initializer: |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 3899 | case CFGElement::NewAllocator: |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3900 | llvm_unreachable("getDestructorDecl should only be used with " |
| 3901 | "ImplicitDtors"); |
| 3902 | case CFGElement::AutomaticObjectDtor: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 3903 | const VarDecl *var = castAs<CFGAutomaticObjDtor>().getVarDecl(); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3904 | QualType ty = var->getType(); |
Devin Coughlin | 6eb1ca7 | 2016-08-02 21:07:23 +0000 | [diff] [blame] | 3905 | |
| 3906 | // FIXME: See CFGBuilder::addLocalScopeForVarDecl. |
| 3907 | // |
| 3908 | // Lifetime-extending constructs are handled here. This works for a single |
| 3909 | // temporary in an initializer expression. |
| 3910 | if (ty->isReferenceType()) { |
| 3911 | if (const Expr *Init = var->getInit()) { |
| 3912 | ty = getReferenceInitTemporaryType(astContext, Init); |
| 3913 | } |
| 3914 | } |
| 3915 | |
Ted Kremenek | e7d7888 | 2012-03-19 23:48:41 +0000 | [diff] [blame] | 3916 | while (const ArrayType *arrayType = astContext.getAsArrayType(ty)) { |
Ted Kremenek | 8cfe207 | 2011-03-03 01:21:32 +0000 | [diff] [blame] | 3917 | ty = arrayType->getElementType(); |
| 3918 | } |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3919 | const RecordType *recordType = ty->getAs<RecordType>(); |
| 3920 | const CXXRecordDecl *classDecl = |
Ted Kremenek | 1676a04 | 2011-03-03 01:01:03 +0000 | [diff] [blame] | 3921 | cast<CXXRecordDecl>(recordType->getDecl()); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3922 | return classDecl->getDestructor(); |
| 3923 | } |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 3924 | case CFGElement::DeleteDtor: { |
| 3925 | const CXXDeleteExpr *DE = castAs<CFGDeleteDtor>().getDeleteExpr(); |
| 3926 | QualType DTy = DE->getDestroyedType(); |
| 3927 | DTy = DTy.getNonReferenceType(); |
| 3928 | const CXXRecordDecl *classDecl = |
| 3929 | astContext.getBaseElementType(DTy)->getAsCXXRecordDecl(); |
| 3930 | return classDecl->getDestructor(); |
| 3931 | } |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3932 | case CFGElement::TemporaryDtor: { |
| 3933 | const CXXBindTemporaryExpr *bindExpr = |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 3934 | castAs<CFGTemporaryDtor>().getBindTemporaryExpr(); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3935 | const CXXTemporary *temp = bindExpr->getTemporary(); |
| 3936 | return temp->getDestructor(); |
| 3937 | } |
| 3938 | case CFGElement::BaseDtor: |
| 3939 | case CFGElement::MemberDtor: |
| 3940 | |
| 3941 | // Not yet supported. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3942 | return nullptr; |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3943 | } |
Ted Kremenek | 1676a04 | 2011-03-03 01:01:03 +0000 | [diff] [blame] | 3944 | llvm_unreachable("getKind() returned bogus value"); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Ted Kremenek | 8cfe207 | 2011-03-03 01:21:32 +0000 | [diff] [blame] | 3947 | bool CFGImplicitDtor::isNoReturn(ASTContext &astContext) const { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3948 | if (const CXXDestructorDecl *DD = getDestructorDecl(astContext)) |
| 3949 | return DD->isNoReturn(); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 3950 | return false; |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 3953 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 3954 | // CFGBlock operations. |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 3955 | //===----------------------------------------------------------------------===// |
| 3956 | |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 3957 | CFGBlock::AdjacentBlock::AdjacentBlock(CFGBlock *B, bool IsReachable) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3958 | : ReachableBlock(IsReachable ? B : nullptr), |
| 3959 | UnreachableBlock(!IsReachable ? B : nullptr, |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 3960 | B && IsReachable ? AB_Normal : AB_Unreachable) {} |
| 3961 | |
| 3962 | CFGBlock::AdjacentBlock::AdjacentBlock(CFGBlock *B, CFGBlock *AlternateBlock) |
| 3963 | : ReachableBlock(B), |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3964 | UnreachableBlock(B == AlternateBlock ? nullptr : AlternateBlock, |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 3965 | B == AlternateBlock ? AB_Alternate : AB_Normal) {} |
| 3966 | |
| 3967 | void CFGBlock::addSuccessor(AdjacentBlock Succ, |
| 3968 | BumpVectorContext &C) { |
| 3969 | if (CFGBlock *B = Succ.getReachableBlock()) |
David Blaikie | 9afd5da | 2014-03-04 23:39:18 +0000 | [diff] [blame] | 3970 | B->Preds.push_back(AdjacentBlock(this, Succ.isReachable()), C); |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 3971 | |
| 3972 | if (CFGBlock *UnreachableB = Succ.getPossiblyUnreachableBlock()) |
David Blaikie | 9afd5da | 2014-03-04 23:39:18 +0000 | [diff] [blame] | 3973 | UnreachableB->Preds.push_back(AdjacentBlock(this, false), C); |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 3974 | |
| 3975 | Succs.push_back(Succ, C); |
| 3976 | } |
| 3977 | |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 3978 | bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F, |
Ted Kremenek | f146cd1 | 2010-09-09 02:57:48 +0000 | [diff] [blame] | 3979 | const CFGBlock *From, const CFGBlock *To) { |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 3980 | |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 3981 | if (F.IgnoreNullPredecessors && !From) |
| 3982 | return true; |
| 3983 | |
| 3984 | if (To && From && F.IgnoreDefaultsWithCoveredEnums) { |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 3985 | // If the 'To' has no label or is labeled but the label isn't a |
| 3986 | // CaseStmt then filter this edge. |
| 3987 | if (const SwitchStmt *S = |
Ted Kremenek | 8979474 | 2011-03-07 22:04:39 +0000 | [diff] [blame] | 3988 | dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) { |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 3989 | if (S->isAllEnumCasesCovered()) { |
Ted Kremenek | 8979474 | 2011-03-07 22:04:39 +0000 | [diff] [blame] | 3990 | const Stmt *L = To->getLabel(); |
| 3991 | if (!L || !isa<CaseStmt>(L)) |
| 3992 | return true; |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 3993 | } |
| 3994 | } |
| 3995 | } |
| 3996 | |
| 3997 | return false; |
| 3998 | } |
| 3999 | |
| 4000 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4001 | // CFG pretty printing |
| 4002 | //===----------------------------------------------------------------------===// |
| 4003 | |
Ted Kremenek | 7e776b1 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 4004 | namespace { |
| 4005 | |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 4006 | class StmtPrinterHelper : public PrinterHelper { |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 4007 | typedef llvm::DenseMap<const Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; |
| 4008 | typedef llvm::DenseMap<const Decl*,std::pair<unsigned,unsigned> > DeclMapTy; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4009 | StmtMapTy StmtMap; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4010 | DeclMapTy DeclMap; |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 4011 | signed currentBlock; |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 4012 | unsigned currStmt; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4013 | const LangOptions &LangOpts; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4014 | public: |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4015 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4016 | StmtPrinterHelper(const CFG* cfg, const LangOptions &LO) |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 4017 | : currentBlock(0), currStmt(0), LangOpts(LO) |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 4018 | { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4019 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { |
| 4020 | unsigned j = 1; |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 4021 | for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4022 | BI != BEnd; ++BI, ++j ) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4023 | if (Optional<CFGStmt> SE = BI->getAs<CFGStmt>()) { |
| 4024 | const Stmt *stmt= SE->getStmt(); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4025 | std::pair<unsigned, unsigned> P((*I)->getBlockID(), j); |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 4026 | StmtMap[stmt] = P; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4027 | |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 4028 | switch (stmt->getStmtClass()) { |
| 4029 | case Stmt::DeclStmtClass: |
| 4030 | DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P; |
| 4031 | break; |
| 4032 | case Stmt::IfStmtClass: { |
| 4033 | const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable(); |
| 4034 | if (var) |
| 4035 | DeclMap[var] = P; |
| 4036 | break; |
| 4037 | } |
| 4038 | case Stmt::ForStmtClass: { |
| 4039 | const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable(); |
| 4040 | if (var) |
| 4041 | DeclMap[var] = P; |
| 4042 | break; |
| 4043 | } |
| 4044 | case Stmt::WhileStmtClass: { |
| 4045 | const VarDecl *var = |
| 4046 | cast<WhileStmt>(stmt)->getConditionVariable(); |
| 4047 | if (var) |
| 4048 | DeclMap[var] = P; |
| 4049 | break; |
| 4050 | } |
| 4051 | case Stmt::SwitchStmtClass: { |
| 4052 | const VarDecl *var = |
| 4053 | cast<SwitchStmt>(stmt)->getConditionVariable(); |
| 4054 | if (var) |
| 4055 | DeclMap[var] = P; |
| 4056 | break; |
| 4057 | } |
| 4058 | case Stmt::CXXCatchStmtClass: { |
| 4059 | const VarDecl *var = |
| 4060 | cast<CXXCatchStmt>(stmt)->getExceptionDecl(); |
| 4061 | if (var) |
| 4062 | DeclMap[var] = P; |
| 4063 | break; |
| 4064 | } |
| 4065 | default: |
| 4066 | break; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4067 | } |
| 4068 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4069 | } |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 4070 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4071 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4072 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 4073 | ~StmtPrinterHelper() override {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4074 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4075 | const LangOptions &getLangOpts() const { return LangOpts; } |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 4076 | void setBlockID(signed i) { currentBlock = i; } |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 4077 | void setStmtID(unsigned i) { currStmt = i; } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4078 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 4079 | bool handledStmt(Stmt *S, raw_ostream &OS) override { |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4080 | StmtMapTy::iterator I = StmtMap.find(S); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4081 | |
| 4082 | if (I == StmtMap.end()) |
| 4083 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4084 | |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 4085 | if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 4086 | && I->second.second == currStmt) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4087 | return false; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4088 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4089 | |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4090 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4091 | return true; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4092 | } |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4093 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4094 | bool handleDecl(const Decl *D, raw_ostream &OS) { |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4095 | DeclMapTy::iterator I = DeclMap.find(D); |
| 4096 | |
| 4097 | if (I == DeclMap.end()) |
| 4098 | return false; |
| 4099 | |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 4100 | if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 4101 | && I->second.second == currStmt) { |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4102 | return false; |
| 4103 | } |
| 4104 | |
| 4105 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
| 4106 | return true; |
| 4107 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4108 | }; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4109 | } // end anonymous namespace |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4110 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4111 | |
| 4112 | namespace { |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 4113 | class CFGBlockTerminatorPrint |
Ted Kremenek | 83ebcef | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 4114 | : public StmtVisitor<CFGBlockTerminatorPrint,void> { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4115 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4116 | raw_ostream &OS; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4117 | StmtPrinterHelper* Helper; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4118 | PrintingPolicy Policy; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4119 | public: |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4120 | CFGBlockTerminatorPrint(raw_ostream &os, StmtPrinterHelper* helper, |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4121 | const PrintingPolicy &Policy) |
Ted Kremenek | 5d0fb1e | 2013-12-11 23:44:05 +0000 | [diff] [blame] | 4122 | : OS(os), Helper(helper), Policy(Policy) { |
| 4123 | this->Policy.IncludeNewlines = false; |
| 4124 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4125 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4126 | void VisitIfStmt(IfStmt *I) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4127 | OS << "if "; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4128 | if (Stmt *C = I->getCond()) |
| 4129 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4130 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4131 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4132 | // Default case. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4133 | void VisitStmt(Stmt *Terminator) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4134 | Terminator->printPretty(OS, Helper, Policy); |
| 4135 | } |
| 4136 | |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 4137 | void VisitDeclStmt(DeclStmt *DS) { |
| 4138 | VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 4139 | OS << "static init " << VD->getName(); |
| 4140 | } |
| 4141 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4142 | void VisitForStmt(ForStmt *F) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4143 | OS << "for (" ; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4144 | if (F->getInit()) |
| 4145 | OS << "..."; |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 4146 | OS << "; "; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4147 | if (Stmt *C = F->getCond()) |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4148 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 4149 | OS << "; "; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4150 | if (F->getInc()) |
| 4151 | OS << "..."; |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4152 | OS << ")"; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4153 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4154 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4155 | void VisitWhileStmt(WhileStmt *W) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4156 | OS << "while " ; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4157 | if (Stmt *C = W->getCond()) |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4158 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4159 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4160 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4161 | void VisitDoStmt(DoStmt *D) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4162 | OS << "do ... while "; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4163 | if (Stmt *C = D->getCond()) |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4164 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 4165 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4166 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4167 | void VisitSwitchStmt(SwitchStmt *Terminator) { |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 4168 | OS << "switch "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4169 | Terminator->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 4170 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4171 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4172 | void VisitCXXTryStmt(CXXTryStmt *CS) { |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 4173 | OS << "try ..."; |
| 4174 | } |
| 4175 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4176 | void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) { |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4177 | if (Stmt *Cond = C->getCond()) |
| 4178 | Cond->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4179 | OS << " ? ... : ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4180 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4181 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4182 | void VisitChooseExpr(ChooseExpr *C) { |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 4183 | OS << "__builtin_choose_expr( "; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4184 | if (Stmt *Cond = C->getCond()) |
| 4185 | Cond->printPretty(OS, Helper, Policy); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4186 | OS << " )"; |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 4187 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4188 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4189 | void VisitIndirectGotoStmt(IndirectGotoStmt *I) { |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4190 | OS << "goto *"; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4191 | if (Stmt *T = I->getTarget()) |
| 4192 | T->printPretty(OS, Helper, Policy); |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4193 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4194 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4195 | void VisitBinaryOperator(BinaryOperator* B) { |
| 4196 | if (!B->isLogicalOp()) { |
| 4197 | VisitExpr(B); |
| 4198 | return; |
| 4199 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4200 | |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4201 | if (B->getLHS()) |
| 4202 | B->getLHS()->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4203 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4204 | switch (B->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4205 | case BO_LOr: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4206 | OS << " || ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4207 | return; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4208 | case BO_LAnd: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4209 | OS << " && ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4210 | return; |
| 4211 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4212 | llvm_unreachable("Invalid logical operator."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4213 | } |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4214 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4215 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4216 | void VisitExpr(Expr *E) { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4217 | E->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4218 | } |
Ted Kremenek | fcc1417 | 2014-03-08 02:22:29 +0000 | [diff] [blame] | 4219 | |
| 4220 | public: |
| 4221 | void print(CFGTerminator T) { |
| 4222 | if (T.isTemporaryDtorsBranch()) |
| 4223 | OS << "(Temp Dtor) "; |
| 4224 | Visit(T.getStmt()); |
| 4225 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4226 | }; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4227 | } // end anonymous namespace |
| 4228 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4229 | static void print_elem(raw_ostream &OS, StmtPrinterHelper &Helper, |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 4230 | const CFGElement &E) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4231 | if (Optional<CFGStmt> CS = E.getAs<CFGStmt>()) { |
| 4232 | const Stmt *S = CS->getStmt(); |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4233 | assert(S != nullptr && "Expecting non-null Stmt"); |
| 4234 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4235 | // special printing for statement-expressions. |
| 4236 | if (const StmtExpr *SE = dyn_cast<StmtExpr>(S)) { |
| 4237 | const CompoundStmt *Sub = SE->getSubStmt(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4238 | |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4239 | auto Children = Sub->children(); |
| 4240 | if (Children.begin() != Children.end()) { |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4241 | OS << "({ ... ; "; |
| 4242 | Helper.handledStmt(*SE->getSubStmt()->body_rbegin(),OS); |
| 4243 | OS << " })\n"; |
| 4244 | return; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4245 | } |
| 4246 | } |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4247 | // special printing for comma expressions. |
| 4248 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 4249 | if (B->getOpcode() == BO_Comma) { |
| 4250 | OS << "... , "; |
| 4251 | Helper.handledStmt(B->getRHS(),OS); |
| 4252 | OS << '\n'; |
| 4253 | return; |
| 4254 | } |
| 4255 | } |
| 4256 | S->printPretty(OS, &Helper, PrintingPolicy(Helper.getLangOpts())); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4257 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4258 | if (isa<CXXOperatorCallExpr>(S)) { |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 4259 | OS << " (OperatorCall)"; |
Ted Kremenek | 0ffba93 | 2011-12-21 19:32:38 +0000 | [diff] [blame] | 4260 | } |
| 4261 | else if (isa<CXXBindTemporaryExpr>(S)) { |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 4262 | OS << " (BindTemporary)"; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4263 | } |
Ted Kremenek | 1a7648b | 2011-12-21 19:39:59 +0000 | [diff] [blame] | 4264 | else if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(S)) { |
| 4265 | OS << " (CXXConstructExpr, " << CCE->getType().getAsString() << ")"; |
| 4266 | } |
Ted Kremenek | 0ffba93 | 2011-12-21 19:32:38 +0000 | [diff] [blame] | 4267 | else if (const CastExpr *CE = dyn_cast<CastExpr>(S)) { |
| 4268 | OS << " (" << CE->getStmtClassName() << ", " |
| 4269 | << CE->getCastKindName() |
| 4270 | << ", " << CE->getType().getAsString() |
| 4271 | << ")"; |
| 4272 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4273 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4274 | // Expressions need a newline. |
| 4275 | if (isa<Expr>(S)) |
| 4276 | OS << '\n'; |
Ted Kremenek | 0f5d8bc | 2010-08-31 18:47:37 +0000 | [diff] [blame] | 4277 | |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4278 | } else if (Optional<CFGInitializer> IE = E.getAs<CFGInitializer>()) { |
| 4279 | const CXXCtorInitializer *I = IE->getInitializer(); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4280 | if (I->isBaseInitializer()) |
| 4281 | OS << I->getBaseClass()->getAsCXXRecordDecl()->getName(); |
Jordan Rose | 69d0aed | 2013-10-22 23:19:47 +0000 | [diff] [blame] | 4282 | else if (I->isDelegatingInitializer()) |
| 4283 | OS << I->getTypeSourceInfo()->getType()->getAsCXXRecordDecl()->getName(); |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4284 | else OS << I->getAnyMember()->getName(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4285 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4286 | OS << "("; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4287 | if (Expr *IE = I->getInit()) |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4288 | IE->printPretty(OS, &Helper, PrintingPolicy(Helper.getLangOpts())); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4289 | OS << ")"; |
| 4290 | |
| 4291 | if (I->isBaseInitializer()) |
| 4292 | OS << " (Base initializer)\n"; |
Jordan Rose | 69d0aed | 2013-10-22 23:19:47 +0000 | [diff] [blame] | 4293 | else if (I->isDelegatingInitializer()) |
| 4294 | OS << " (Delegating initializer)\n"; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4295 | else OS << " (Member initializer)\n"; |
| 4296 | |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4297 | } else if (Optional<CFGAutomaticObjDtor> DE = |
| 4298 | E.getAs<CFGAutomaticObjDtor>()) { |
| 4299 | const VarDecl *VD = DE->getVarDecl(); |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4300 | Helper.handleDecl(VD, OS); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4301 | |
Marcin Swiderski | 52e4bc1 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 4302 | const Type* T = VD->getType().getTypePtr(); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4303 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) |
| 4304 | T = RT->getPointeeType().getTypePtr(); |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 4305 | T = T->getBaseElementTypeUnsafe(); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4306 | |
| 4307 | OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()"; |
| 4308 | OS << " (Implicit destructor)\n"; |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 4309 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 4310 | } else if (Optional<CFGNewAllocator> NE = E.getAs<CFGNewAllocator>()) { |
| 4311 | OS << "CFGNewAllocator("; |
| 4312 | if (const CXXNewExpr *AllocExpr = NE->getAllocatorExpr()) |
| 4313 | AllocExpr->getType().print(OS, PrintingPolicy(Helper.getLangOpts())); |
| 4314 | OS << ")\n"; |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 4315 | } else if (Optional<CFGDeleteDtor> DE = E.getAs<CFGDeleteDtor>()) { |
| 4316 | const CXXRecordDecl *RD = DE->getCXXRecordDecl(); |
| 4317 | if (!RD) |
| 4318 | return; |
| 4319 | CXXDeleteExpr *DelExpr = |
| 4320 | const_cast<CXXDeleteExpr*>(DE->getDeleteExpr()); |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4321 | Helper.handledStmt(cast<Stmt>(DelExpr->getArgument()), OS); |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 4322 | OS << "->~" << RD->getName().str() << "()"; |
| 4323 | OS << " (Implicit destructor)\n"; |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4324 | } else if (Optional<CFGBaseDtor> BE = E.getAs<CFGBaseDtor>()) { |
| 4325 | const CXXBaseSpecifier *BS = BE->getBaseSpecifier(); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 4326 | OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()"; |
Zhongxing Xu | 614e17d | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 4327 | OS << " (Base object destructor)\n"; |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 4328 | |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4329 | } else if (Optional<CFGMemberDtor> ME = E.getAs<CFGMemberDtor>()) { |
| 4330 | const FieldDecl *FD = ME->getFieldDecl(); |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 4331 | const Type *T = FD->getType()->getBaseElementTypeUnsafe(); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 4332 | OS << "this->" << FD->getName(); |
Marcin Swiderski | 0176990 | 2010-10-25 07:05:54 +0000 | [diff] [blame] | 4333 | OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()"; |
Zhongxing Xu | 614e17d | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 4334 | OS << " (Member object destructor)\n"; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4335 | |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4336 | } else if (Optional<CFGTemporaryDtor> TE = E.getAs<CFGTemporaryDtor>()) { |
| 4337 | const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr(); |
Pavel Labath | d527cf8 | 2013-09-02 09:09:15 +0000 | [diff] [blame] | 4338 | OS << "~"; |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4339 | BT->getType().print(OS, PrintingPolicy(Helper.getLangOpts())); |
Pavel Labath | d527cf8 | 2013-09-02 09:09:15 +0000 | [diff] [blame] | 4340 | OS << "() (Temporary object destructor)\n"; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4341 | } |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 4342 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4343 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4344 | static void print_block(raw_ostream &OS, const CFG* cfg, |
| 4345 | const CFGBlock &B, |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4346 | StmtPrinterHelper &Helper, bool print_edges, |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4347 | bool ShowColors) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4348 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4349 | Helper.setBlockID(B.getBlockID()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4350 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4351 | // Print the header. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4352 | if (ShowColors) |
| 4353 | OS.changeColor(raw_ostream::YELLOW, true); |
| 4354 | |
| 4355 | OS << "\n [B" << B.getBlockID(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4356 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4357 | if (&B == &cfg->getEntry()) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4358 | OS << " (ENTRY)]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4359 | else if (&B == &cfg->getExit()) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4360 | OS << " (EXIT)]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4361 | else if (&B == cfg->getIndirectGotoBlock()) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4362 | OS << " (INDIRECT GOTO DISPATCH)]\n"; |
Jordan Rose | 398fb00 | 2014-04-01 16:39:33 +0000 | [diff] [blame] | 4363 | else if (B.hasNoReturnElement()) |
| 4364 | OS << " (NORETURN)]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4365 | else |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4366 | OS << "]\n"; |
| 4367 | |
| 4368 | if (ShowColors) |
| 4369 | OS.resetColor(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4370 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4371 | // Print the label of this block. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4372 | if (Stmt *Label = const_cast<Stmt*>(B.getLabel())) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4373 | |
| 4374 | if (print_edges) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4375 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4376 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4377 | if (LabelStmt *L = dyn_cast<LabelStmt>(Label)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4378 | OS << L->getName(); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4379 | else if (CaseStmt *C = dyn_cast<CaseStmt>(Label)) { |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4380 | OS << "case "; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4381 | if (C->getLHS()) |
| 4382 | C->getLHS()->printPretty(OS, &Helper, |
| 4383 | PrintingPolicy(Helper.getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4384 | if (C->getRHS()) { |
| 4385 | OS << " ... "; |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4386 | C->getRHS()->printPretty(OS, &Helper, |
| 4387 | PrintingPolicy(Helper.getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4388 | } |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 4389 | } else if (isa<DefaultStmt>(Label)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4390 | OS << "default"; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 4391 | else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) { |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 4392 | OS << "catch ("; |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 4393 | if (CS->getExceptionDecl()) |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4394 | CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper.getLangOpts()), |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 4395 | 0); |
| 4396 | else |
| 4397 | OS << "..."; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 4398 | OS << ")"; |
| 4399 | |
| 4400 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4401 | llvm_unreachable("Invalid label statement in CFGBlock."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4402 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4403 | OS << ":\n"; |
| 4404 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4405 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4406 | // Iterate through the statements in the block and print them. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4407 | unsigned j = 1; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4408 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4409 | for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; |
| 4410 | I != E ; ++I, ++j ) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4411 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4412 | // Print the statement # in the basic block and the statement itself. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4413 | if (print_edges) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4414 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4415 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 4416 | OS << llvm::format("%3d", j) << ": "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4417 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4418 | Helper.setStmtID(j); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4419 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4420 | print_elem(OS, Helper, *I); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4421 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4422 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4423 | // Print the terminator of this block. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4424 | if (B.getTerminator()) { |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4425 | if (ShowColors) |
| 4426 | OS.changeColor(raw_ostream::GREEN); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4427 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4428 | OS << " T: "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4429 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4430 | Helper.setBlockID(-1); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4431 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4432 | PrintingPolicy PP(Helper.getLangOpts()); |
| 4433 | CFGBlockTerminatorPrint TPrinter(OS, &Helper, PP); |
Ted Kremenek | fcc1417 | 2014-03-08 02:22:29 +0000 | [diff] [blame] | 4434 | TPrinter.print(B.getTerminator()); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4435 | OS << '\n'; |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4436 | |
| 4437 | if (ShowColors) |
| 4438 | OS.resetColor(); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4439 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4440 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4441 | if (print_edges) { |
| 4442 | // Print the predecessors of this block. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4443 | if (!B.pred_empty()) { |
| 4444 | const raw_ostream::Colors Color = raw_ostream::BLUE; |
| 4445 | if (ShowColors) |
| 4446 | OS.changeColor(Color); |
| 4447 | OS << " Preds " ; |
| 4448 | if (ShowColors) |
| 4449 | OS.resetColor(); |
| 4450 | OS << '(' << B.pred_size() << "):"; |
| 4451 | unsigned i = 0; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4452 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4453 | if (ShowColors) |
| 4454 | OS.changeColor(Color); |
| 4455 | |
| 4456 | for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); |
| 4457 | I != E; ++I, ++i) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4458 | |
Will Dietz | df9a2bb | 2013-01-07 09:51:17 +0000 | [diff] [blame] | 4459 | if (i % 10 == 8) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4460 | OS << "\n "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4461 | |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4462 | CFGBlock *B = *I; |
| 4463 | bool Reachable = true; |
| 4464 | if (!B) { |
| 4465 | Reachable = false; |
| 4466 | B = I->getPossiblyUnreachableBlock(); |
| 4467 | } |
| 4468 | |
| 4469 | OS << " B" << B->getBlockID(); |
| 4470 | if (!Reachable) |
| 4471 | OS << "(Unreachable)"; |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | if (ShowColors) |
| 4475 | OS.resetColor(); |
| 4476 | |
| 4477 | OS << '\n'; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4478 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4479 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4480 | // Print the successors of this block. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4481 | if (!B.succ_empty()) { |
| 4482 | const raw_ostream::Colors Color = raw_ostream::MAGENTA; |
| 4483 | if (ShowColors) |
| 4484 | OS.changeColor(Color); |
| 4485 | OS << " Succs "; |
| 4486 | if (ShowColors) |
| 4487 | OS.resetColor(); |
| 4488 | OS << '(' << B.succ_size() << "):"; |
| 4489 | unsigned i = 0; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4490 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4491 | if (ShowColors) |
| 4492 | OS.changeColor(Color); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4493 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4494 | for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); |
| 4495 | I != E; ++I, ++i) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4496 | |
Will Dietz | df9a2bb | 2013-01-07 09:51:17 +0000 | [diff] [blame] | 4497 | if (i % 10 == 8) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4498 | OS << "\n "; |
| 4499 | |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 4500 | CFGBlock *B = *I; |
| 4501 | |
| 4502 | bool Reachable = true; |
| 4503 | if (!B) { |
| 4504 | Reachable = false; |
| 4505 | B = I->getPossiblyUnreachableBlock(); |
| 4506 | } |
| 4507 | |
| 4508 | if (B) { |
| 4509 | OS << " B" << B->getBlockID(); |
| 4510 | if (!Reachable) |
| 4511 | OS << "(Unreachable)"; |
| 4512 | } |
| 4513 | else { |
| 4514 | OS << " NULL"; |
| 4515 | } |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4516 | } |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 4517 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4518 | if (ShowColors) |
| 4519 | OS.resetColor(); |
| 4520 | OS << '\n'; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4521 | } |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4522 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4523 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4524 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4525 | |
| 4526 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4527 | void CFG::dump(const LangOptions &LO, bool ShowColors) const { |
| 4528 | print(llvm::errs(), LO, ShowColors); |
| 4529 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4530 | |
| 4531 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4532 | void CFG::print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const { |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4533 | StmtPrinterHelper Helper(this, LO); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4534 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4535 | // Print the entry block. |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4536 | print_block(OS, this, getEntry(), Helper, true, ShowColors); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4537 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4538 | // Iterate through the CFGBlocks and print them one by one. |
| 4539 | for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 4540 | // Skip the entry block, because we already printed it. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 4541 | if (&(**I) == &getEntry() || &(**I) == &getExit()) |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4542 | continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4543 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4544 | print_block(OS, this, **I, Helper, true, ShowColors); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4545 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4546 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4547 | // Print the exit block. |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4548 | print_block(OS, this, getExit(), Helper, true, ShowColors); |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4549 | OS << '\n'; |
Ted Kremenek | e03879b | 2008-11-24 20:50:24 +0000 | [diff] [blame] | 4550 | OS.flush(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4551 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4552 | |
| 4553 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4554 | void CFGBlock::dump(const CFG* cfg, const LangOptions &LO, |
| 4555 | bool ShowColors) const { |
| 4556 | print(llvm::errs(), cfg, LO, ShowColors); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4557 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4558 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 4559 | LLVM_DUMP_METHOD void CFGBlock::dump() const { |
Anna Zaks | a6fea13 | 2014-06-13 23:47:38 +0000 | [diff] [blame] | 4560 | dump(getParent(), LangOptions(), false); |
| 4561 | } |
| 4562 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4563 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 4564 | /// Generally this will only be called from CFG::print. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4565 | void CFGBlock::print(raw_ostream &OS, const CFG* cfg, |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4566 | const LangOptions &LO, bool ShowColors) const { |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4567 | StmtPrinterHelper Helper(cfg, LO); |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4568 | print_block(OS, cfg, *this, Helper, true, ShowColors); |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4569 | OS << '\n'; |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 4570 | } |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4571 | |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4572 | /// printTerminator - A simple pretty printer of the terminator of a CFGBlock. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4573 | void CFGBlock::printTerminator(raw_ostream &OS, |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4574 | const LangOptions &LO) const { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 4575 | CFGBlockTerminatorPrint TPrinter(OS, nullptr, PrintingPolicy(LO)); |
Ted Kremenek | fcc1417 | 2014-03-08 02:22:29 +0000 | [diff] [blame] | 4576 | TPrinter.print(getTerminator()); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 4579 | Stmt *CFGBlock::getTerminatorCondition(bool StripParens) { |
Marcin Swiderski | a7d84a7 | 2010-10-29 05:21:47 +0000 | [diff] [blame] | 4580 | Stmt *Terminator = this->Terminator; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4581 | if (!Terminator) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 4582 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4583 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 4584 | Expr *E = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4585 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4586 | switch (Terminator->getStmtClass()) { |
| 4587 | default: |
| 4588 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4589 | |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 4590 | case Stmt::CXXForRangeStmtClass: |
| 4591 | E = cast<CXXForRangeStmt>(Terminator)->getCond(); |
| 4592 | break; |
| 4593 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4594 | case Stmt::ForStmtClass: |
| 4595 | E = cast<ForStmt>(Terminator)->getCond(); |
| 4596 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4597 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4598 | case Stmt::WhileStmtClass: |
| 4599 | E = cast<WhileStmt>(Terminator)->getCond(); |
| 4600 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4601 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4602 | case Stmt::DoStmtClass: |
| 4603 | E = cast<DoStmt>(Terminator)->getCond(); |
| 4604 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4605 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4606 | case Stmt::IfStmtClass: |
| 4607 | E = cast<IfStmt>(Terminator)->getCond(); |
| 4608 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4609 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4610 | case Stmt::ChooseExprClass: |
| 4611 | E = cast<ChooseExpr>(Terminator)->getCond(); |
| 4612 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4613 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4614 | case Stmt::IndirectGotoStmtClass: |
| 4615 | E = cast<IndirectGotoStmt>(Terminator)->getTarget(); |
| 4616 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4617 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4618 | case Stmt::SwitchStmtClass: |
| 4619 | E = cast<SwitchStmt>(Terminator)->getCond(); |
| 4620 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4621 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4622 | case Stmt::BinaryConditionalOperatorClass: |
| 4623 | E = cast<BinaryConditionalOperator>(Terminator)->getCond(); |
| 4624 | break; |
| 4625 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4626 | case Stmt::ConditionalOperatorClass: |
| 4627 | E = cast<ConditionalOperator>(Terminator)->getCond(); |
| 4628 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4629 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4630 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 4631 | E = cast<BinaryOperator>(Terminator)->getLHS(); |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 4632 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4633 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 4634 | case Stmt::ObjCForCollectionStmtClass: |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4635 | return Terminator; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4636 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4637 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 4638 | if (!StripParens) |
| 4639 | return E; |
| 4640 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 4641 | return E ? E->IgnoreParens() : nullptr; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4644 | //===----------------------------------------------------------------------===// |
| 4645 | // CFG Graphviz Visualization |
| 4646 | //===----------------------------------------------------------------------===// |
| 4647 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4648 | |
| 4649 | #ifndef NDEBUG |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4650 | static StmtPrinterHelper* GraphHelper; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4651 | #endif |
| 4652 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4653 | void CFG::viewCFG(const LangOptions &LO) const { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4654 | #ifndef NDEBUG |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4655 | StmtPrinterHelper H(this, LO); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4656 | GraphHelper = &H; |
| 4657 | llvm::ViewGraph(this,"CFG"); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 4658 | GraphHelper = nullptr; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4659 | #endif |
| 4660 | } |
| 4661 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4662 | namespace llvm { |
| 4663 | template<> |
| 4664 | struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { |
Tobias Grosser | 9fc223a | 2009-11-30 14:16:05 +0000 | [diff] [blame] | 4665 | |
| 4666 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 4667 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4668 | static std::string getNodeLabel(const CFGBlock *Node, const CFG* Graph) { |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4669 | |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 4670 | #ifndef NDEBUG |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 4671 | std::string OutSStr; |
| 4672 | llvm::raw_string_ostream Out(OutSStr); |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4673 | print_block(Out,Graph, *Node, *GraphHelper, false, false); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 4674 | std::string& OutStr = Out.str(); |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4675 | |
| 4676 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 4677 | |
| 4678 | // Process string output to make it nicer... |
| 4679 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 4680 | if (OutStr[i] == '\n') { // Left justify |
| 4681 | OutStr[i] = '\\'; |
| 4682 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 4683 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4684 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4685 | return OutStr; |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 4686 | #else |
| 4687 | return ""; |
| 4688 | #endif |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4689 | } |
| 4690 | }; |
| 4691 | } // end namespace llvm |