Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 1 | //===- CFG.cpp - Classes for representing and building CFGs ---------------===// |
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" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclBase.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclGroup.h" |
| 22 | #include "clang/AST/Expr.h" |
| 23 | #include "clang/AST/ExprCXX.h" |
| 24 | #include "clang/AST/OperationKinds.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 25 | #include "clang/AST/PrettyPrinter.h" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 26 | #include "clang/AST/Stmt.h" |
| 27 | #include "clang/AST/StmtCXX.h" |
| 28 | #include "clang/AST/StmtObjC.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 29 | #include "clang/AST/StmtVisitor.h" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 30 | #include "clang/AST/Type.h" |
| 31 | #include "clang/Analysis/Support/BumpVector.h" |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 32 | #include "clang/Basic/Builtins.h" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 33 | #include "clang/Basic/ExceptionSpecificationType.h" |
| 34 | #include "clang/Basic/LLVM.h" |
| 35 | #include "clang/Basic/LangOptions.h" |
| 36 | #include "clang/Basic/SourceLocation.h" |
| 37 | #include "clang/Basic/Specifiers.h" |
| 38 | #include "llvm/ADT/APInt.h" |
| 39 | #include "llvm/ADT/APSInt.h" |
| 40 | #include "llvm/ADT/ArrayRef.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/Optional.h" |
| 43 | #include "llvm/ADT/STLExtras.h" |
| 44 | #include "llvm/ADT/SetVector.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallVector.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Allocator.h" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Casting.h" |
| 49 | #include "llvm/Support/Compiler.h" |
| 50 | #include "llvm/Support/DOTGraphTraits.h" |
| 51 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Format.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 53 | #include "llvm/Support/GraphWriter.h" |
| 54 | #include "llvm/Support/SaveAndRestore.h" |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 55 | #include "llvm/Support/raw_ostream.h" |
| 56 | #include <cassert> |
| 57 | #include <memory> |
| 58 | #include <string> |
| 59 | #include <tuple> |
| 60 | #include <utility> |
| 61 | #include <vector> |
Ted Kremenek | e5ccf9a | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 63 | using namespace clang; |
| 64 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 65 | static SourceLocation GetEndLoc(Decl *D) { |
| 66 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 67 | if (Expr *Ex = VD->getInit()) |
Ted Kremenek | 8889bb3 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 68 | return Ex->getSourceRange().getEnd(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 69 | return D->getLocation(); |
Ted Kremenek | 8889bb3 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 70 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 71 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 72 | /// Helper for tryNormalizeBinaryOperator. Attempts to extract an IntegerLiteral |
| 73 | /// or EnumConstantDecl from the given Expr. If it fails, returns nullptr. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 74 | static const Expr *tryTransformToIntOrEnumConstant(const Expr *E) { |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 75 | E = E->IgnoreParens(); |
| 76 | if (isa<IntegerLiteral>(E)) |
| 77 | return E; |
| 78 | if (auto *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 79 | return isa<EnumConstantDecl>(DR->getDecl()) ? DR : nullptr; |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
| 83 | /// Tries to interpret a binary operator into `Decl Op Expr` form, if Expr is |
| 84 | /// an integer literal or an enum constant. |
| 85 | /// |
| 86 | /// If this fails, at least one of the returned DeclRefExpr or Expr will be |
| 87 | /// null. |
| 88 | static std::tuple<const DeclRefExpr *, BinaryOperatorKind, const Expr *> |
| 89 | tryNormalizeBinaryOperator(const BinaryOperator *B) { |
| 90 | BinaryOperatorKind Op = B->getOpcode(); |
| 91 | |
| 92 | const Expr *MaybeDecl = B->getLHS(); |
| 93 | const Expr *Constant = tryTransformToIntOrEnumConstant(B->getRHS()); |
| 94 | // Expr looked like `0 == Foo` instead of `Foo == 0` |
| 95 | if (Constant == nullptr) { |
| 96 | // Flip the operator |
| 97 | if (Op == BO_GT) |
| 98 | Op = BO_LT; |
| 99 | else if (Op == BO_GE) |
| 100 | Op = BO_LE; |
| 101 | else if (Op == BO_LT) |
| 102 | Op = BO_GT; |
| 103 | else if (Op == BO_LE) |
| 104 | Op = BO_GE; |
| 105 | |
| 106 | MaybeDecl = B->getRHS(); |
| 107 | Constant = tryTransformToIntOrEnumConstant(B->getLHS()); |
| 108 | } |
| 109 | |
| 110 | auto *D = dyn_cast<DeclRefExpr>(MaybeDecl->IgnoreParenImpCasts()); |
| 111 | return std::make_tuple(D, Op, Constant); |
| 112 | } |
| 113 | |
| 114 | /// For an expression `x == Foo && x == Bar`, this determines whether the |
| 115 | /// `Foo` and `Bar` are either of the same enumeration type, or both integer |
| 116 | /// literals. |
| 117 | /// |
| 118 | /// It's an error to pass this arguments that are not either IntegerLiterals |
| 119 | /// or DeclRefExprs (that have decls of type EnumConstantDecl) |
| 120 | static bool areExprTypesCompatible(const Expr *E1, const Expr *E2) { |
| 121 | // User intent isn't clear if they're mixing int literals with enum |
| 122 | // constants. |
| 123 | if (isa<IntegerLiteral>(E1) != isa<IntegerLiteral>(E2)) |
| 124 | return false; |
| 125 | |
| 126 | // Integer literal comparisons, regardless of literal type, are acceptable. |
| 127 | if (isa<IntegerLiteral>(E1)) |
| 128 | return true; |
| 129 | |
| 130 | // IntegerLiterals are handled above and only EnumConstantDecls are expected |
| 131 | // beyond this point |
| 132 | assert(isa<DeclRefExpr>(E1) && isa<DeclRefExpr>(E2)); |
| 133 | auto *Decl1 = cast<DeclRefExpr>(E1)->getDecl(); |
| 134 | auto *Decl2 = cast<DeclRefExpr>(E2)->getDecl(); |
| 135 | |
| 136 | assert(isa<EnumConstantDecl>(Decl1) && isa<EnumConstantDecl>(Decl2)); |
| 137 | const DeclContext *DC1 = Decl1->getDeclContext(); |
| 138 | const DeclContext *DC2 = Decl2->getDeclContext(); |
| 139 | |
| 140 | assert(isa<EnumDecl>(DC1) && isa<EnumDecl>(DC2)); |
| 141 | return DC1 == DC2; |
| 142 | } |
| 143 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 144 | namespace { |
| 145 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 146 | class CFGBuilder; |
| 147 | |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 148 | /// The CFG builder uses a recursive algorithm to build the CFG. When |
| 149 | /// we process an expression, sometimes we know that we must add the |
| 150 | /// subexpressions as block-level expressions. For example: |
| 151 | /// |
| 152 | /// exp1 || exp2 |
| 153 | /// |
| 154 | /// When processing the '||' expression, we know that exp1 and exp2 |
| 155 | /// need to be added as block-level expressions, even though they |
| 156 | /// might not normally need to be. AddStmtChoice records this |
| 157 | /// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then |
| 158 | /// the builder has an option not to add a subexpression as a |
| 159 | /// block-level expression. |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 160 | class AddStmtChoice { |
| 161 | public: |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 162 | enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 }; |
Ted Kremenek | 5d2bb1b | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 163 | |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 164 | AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {} |
Ted Kremenek | 5d2bb1b | 2010-03-02 21:43:54 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 166 | bool alwaysAdd(CFGBuilder &builder, |
| 167 | const Stmt *stmt) const; |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 168 | |
| 169 | /// Return a copy of this object, except with the 'always-add' bit |
| 170 | /// set as specified. |
| 171 | AddStmtChoice withAlwaysAdd(bool alwaysAdd) const { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 172 | return AddStmtChoice(alwaysAdd ? AlwaysAdd : NotAlwaysAdd); |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 175 | private: |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 176 | Kind kind; |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 177 | }; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 178 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 179 | /// LocalScope - Node in tree of local scopes created for C++ implicit |
| 180 | /// destructor calls generation. It contains list of automatic variables |
| 181 | /// declared in the scope and link to position in previous scope this scope |
| 182 | /// began in. |
| 183 | /// |
| 184 | /// The process of creating local scopes is as follows: |
| 185 | /// - Init CFGBuilder::ScopePos with invalid position (equivalent for null), |
| 186 | /// - Before processing statements in scope (e.g. CompoundStmt) create |
| 187 | /// LocalScope object using CFGBuilder::ScopePos as link to previous scope |
| 188 | /// and set CFGBuilder::ScopePos to the end of new scope, |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 189 | /// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 190 | /// at this VarDecl, |
| 191 | /// - For every normal (without jump) end of scope add to CFGBlock destructors |
| 192 | /// for objects in the current scope, |
| 193 | /// - For every jump add to CFGBlock destructors for objects |
| 194 | /// between CFGBuilder::ScopePos and local scope position saved for jump |
| 195 | /// target. Thanks to C++ restrictions on goto jumps we can be sure that |
| 196 | /// jump target position will be on the path to root from CFGBuilder::ScopePos |
| 197 | /// (adding any variable that doesn't need constructor to be called to |
| 198 | /// LocalScope can break this assumption), |
| 199 | /// |
| 200 | class LocalScope { |
| 201 | public: |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 202 | friend class const_iterator; |
| 203 | |
| 204 | using AutomaticVarsTy = BumpVector<VarDecl *>; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 205 | |
| 206 | /// const_iterator - Iterates local scope backwards and jumps to previous |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 207 | /// scope on reaching the beginning of currently iterated scope. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 208 | class const_iterator { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 209 | const LocalScope* Scope = nullptr; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 210 | |
| 211 | /// VarIter is guaranteed to be greater then 0 for every valid iterator. |
| 212 | /// Invalid iterator (with null Scope) has VarIter equal to 0. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 213 | unsigned VarIter = 0; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 214 | |
| 215 | public: |
| 216 | /// Create invalid iterator. Dereferencing invalid iterator is not allowed. |
| 217 | /// Incrementing invalid iterator is allowed and will result in invalid |
| 218 | /// iterator. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 219 | const_iterator() = default; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 220 | |
| 221 | /// Create valid iterator. In case when S.Prev is an invalid iterator and |
| 222 | /// I is equal to 0, this will create invalid iterator. |
| 223 | const_iterator(const LocalScope& S, unsigned I) |
| 224 | : Scope(&S), VarIter(I) { |
| 225 | // Iterator to "end" of scope is not allowed. Handle it by going up |
| 226 | // in scopes tree possibly up to invalid iterator in the root. |
| 227 | if (VarIter == 0 && Scope) |
| 228 | *this = Scope->Prev; |
| 229 | } |
| 230 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 231 | VarDecl *const* operator->() const { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 232 | assert(Scope && "Dereferencing invalid iterator is not allowed"); |
| 233 | assert(VarIter != 0 && "Iterator has invalid value of VarIter member"); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 234 | return &Scope->Vars[VarIter - 1]; |
| 235 | } |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 236 | VarDecl *operator*() const { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 237 | return *this->operator->(); |
| 238 | } |
| 239 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 240 | const_iterator &operator++() { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 241 | if (!Scope) |
| 242 | return *this; |
| 243 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 244 | assert(VarIter != 0 && "Iterator has invalid value of VarIter member"); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 245 | --VarIter; |
| 246 | if (VarIter == 0) |
| 247 | *this = Scope->Prev; |
| 248 | return *this; |
| 249 | } |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 250 | const_iterator operator++(int) { |
| 251 | const_iterator P = *this; |
| 252 | ++*this; |
| 253 | return P; |
| 254 | } |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 256 | bool operator==(const const_iterator &rhs) const { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 257 | return Scope == rhs.Scope && VarIter == rhs.VarIter; |
| 258 | } |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 259 | bool operator!=(const const_iterator &rhs) const { |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 260 | return !(*this == rhs); |
| 261 | } |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 262 | |
Aaron Ballman | 6734766 | 2015-02-15 22:00:28 +0000 | [diff] [blame] | 263 | explicit operator bool() const { |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 264 | return *this != const_iterator(); |
| 265 | } |
| 266 | |
| 267 | int distance(const_iterator L); |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 268 | const_iterator shared_parent(const_iterator L); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 271 | private: |
Ted Kremenek | c7bfdcd | 2011-02-15 02:47:45 +0000 | [diff] [blame] | 272 | BumpVectorContext ctx; |
| 273 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 274 | /// Automatic variables in order of declaration. |
| 275 | AutomaticVarsTy Vars; |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 276 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 277 | /// Iterator to variable in previous scope that was declared just before |
| 278 | /// begin of this scope. |
| 279 | const_iterator Prev; |
| 280 | |
| 281 | public: |
| 282 | /// Constructs empty scope linked to previous scope in specified place. |
David Blaikie | c1334cc | 2015-08-13 22:12:21 +0000 | [diff] [blame] | 283 | LocalScope(BumpVectorContext ctx, const_iterator P) |
| 284 | : ctx(std::move(ctx)), Vars(this->ctx, 4), Prev(P) {} |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 285 | |
| 286 | /// Begin of scope in direction of CFG building (backwards). |
| 287 | const_iterator begin() const { return const_iterator(*this, Vars.size()); } |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 289 | void addVar(VarDecl *VD) { |
Ted Kremenek | c7bfdcd | 2011-02-15 02:47:45 +0000 | [diff] [blame] | 290 | Vars.push_back(VD, ctx); |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 291 | } |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 294 | } // namespace |
| 295 | |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 296 | /// distance - Calculates distance from this to L. L must be reachable from this |
| 297 | /// (with use of ++ operator). Cost of calculating the distance is linear w.r.t. |
| 298 | /// number of scopes between this and L. |
| 299 | int LocalScope::const_iterator::distance(LocalScope::const_iterator L) { |
| 300 | int D = 0; |
| 301 | const_iterator F = *this; |
| 302 | while (F.Scope != L.Scope) { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 303 | assert(F != const_iterator() && |
| 304 | "L iterator is not reachable from F iterator."); |
Marcin Swiderski | e9862ce | 2010-09-30 22:42:32 +0000 | [diff] [blame] | 305 | D += F.VarIter; |
| 306 | F = F.Scope->Prev; |
| 307 | } |
| 308 | D += F.VarIter - L.VarIter; |
| 309 | return D; |
| 310 | } |
| 311 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 312 | /// Calculates the closest parent of this iterator |
| 313 | /// that is in a scope reachable through the parents of L. |
| 314 | /// I.e. when using 'goto' from this to L, the lifetime of all variables |
| 315 | /// between this and shared_parent(L) end. |
| 316 | LocalScope::const_iterator |
| 317 | LocalScope::const_iterator::shared_parent(LocalScope::const_iterator L) { |
| 318 | llvm::SmallPtrSet<const LocalScope *, 4> ScopesOfL; |
| 319 | while (true) { |
| 320 | ScopesOfL.insert(L.Scope); |
| 321 | if (L == const_iterator()) |
| 322 | break; |
| 323 | L = L.Scope->Prev; |
| 324 | } |
| 325 | |
| 326 | const_iterator F = *this; |
| 327 | while (true) { |
| 328 | if (ScopesOfL.count(F.Scope)) |
| 329 | return F; |
| 330 | assert(F != const_iterator() && |
| 331 | "L iterator is not reachable from F iterator."); |
| 332 | F = F.Scope->Prev; |
| 333 | } |
| 334 | } |
| 335 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 336 | namespace { |
| 337 | |
Jonathan Roelofs | 99bdd98 | 2015-05-19 18:51:56 +0000 | [diff] [blame] | 338 | /// Structure for specifying position in CFG during its build process. It |
| 339 | /// consists of CFGBlock that specifies position in CFG and |
| 340 | /// LocalScope::const_iterator that specifies position in LocalScope graph. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 341 | struct BlockScopePosPair { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 342 | CFGBlock *block = nullptr; |
| 343 | LocalScope::const_iterator scopePosition; |
| 344 | |
| 345 | BlockScopePosPair() = default; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 346 | BlockScopePosPair(CFGBlock *b, LocalScope::const_iterator scopePos) |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 347 | : block(b), scopePosition(scopePos) {} |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 350 | /// TryResult - a class representing a variant over the values |
| 351 | /// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool, |
| 352 | /// and is used by the CFGBuilder to decide if a branch condition |
| 353 | /// can be decided up front during CFG construction. |
| 354 | class TryResult { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 355 | int X = -1; |
| 356 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 357 | public: |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 358 | TryResult() = default; |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 359 | TryResult(bool b) : X(b ? 1 : 0) {} |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 360 | |
| 361 | bool isTrue() const { return X == 1; } |
| 362 | bool isFalse() const { return X == 0; } |
| 363 | bool isKnown() const { return X >= 0; } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 364 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 365 | void negate() { |
| 366 | assert(isKnown()); |
| 367 | X ^= 0x1; |
| 368 | } |
| 369 | }; |
| 370 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 371 | } // namespace |
| 372 | |
| 373 | static TryResult bothKnownTrue(TryResult R1, TryResult R2) { |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 374 | if (!R1.isKnown() || !R2.isKnown()) |
| 375 | return TryResult(); |
| 376 | return TryResult(R1.isTrue() && R2.isTrue()); |
| 377 | } |
| 378 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 379 | namespace { |
| 380 | |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 381 | class reverse_children { |
| 382 | llvm::SmallVector<Stmt *, 12> childrenBuf; |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 383 | ArrayRef<Stmt *> children; |
| 384 | |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 385 | public: |
| 386 | reverse_children(Stmt *S); |
| 387 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 388 | using iterator = ArrayRef<Stmt *>::reverse_iterator; |
| 389 | |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 390 | iterator begin() const { return children.rbegin(); } |
| 391 | iterator end() const { return children.rend(); } |
| 392 | }; |
| 393 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 394 | } // namespace |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 395 | |
| 396 | reverse_children::reverse_children(Stmt *S) { |
| 397 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 398 | children = CE->getRawSubExprs(); |
| 399 | return; |
| 400 | } |
| 401 | switch (S->getStmtClass()) { |
Ted Kremenek | 7d86b9c | 2013-02-05 22:03:14 +0000 | [diff] [blame] | 402 | // Note: Fill in this switch with more cases we want to optimize. |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 403 | case Stmt::InitListExprClass: { |
| 404 | InitListExpr *IE = cast<InitListExpr>(S); |
| 405 | children = llvm::makeArrayRef(reinterpret_cast<Stmt**>(IE->getInits()), |
| 406 | IE->getNumInits()); |
| 407 | return; |
| 408 | } |
| 409 | default: |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | // Default case for all other statements. |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 414 | for (Stmt *SubStmt : S->children()) |
| 415 | childrenBuf.push_back(SubStmt); |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 416 | |
| 417 | // This needs to be done *after* childrenBuf has been populated. |
| 418 | children = childrenBuf; |
| 419 | } |
| 420 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 421 | namespace { |
| 422 | |
Ted Kremenek | be9b33b | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 423 | /// CFGBuilder - This class implements CFG construction from an AST. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 424 | /// The builder is stateful: an instance of the builder should be used to only |
| 425 | /// construct a single CFG. |
| 426 | /// |
| 427 | /// Example usage: |
| 428 | /// |
| 429 | /// CFGBuilder builder; |
Jonathan Roelofs | ab046c5 | 2015-07-27 16:05:36 +0000 | [diff] [blame] | 430 | /// std::unique_ptr<CFG> cfg = builder.buildCFG(decl, stmt1); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 431 | /// |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 432 | /// CFG construction is done via a recursive walk of an AST. We actually parse |
| 433 | /// the AST in reverse order so that the successor of a basic block is |
| 434 | /// constructed prior to its predecessor. This allows us to nicely capture |
| 435 | /// implicit fall-throughs without extra basic blocks. |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 436 | class CFGBuilder { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 437 | using JumpTarget = BlockScopePosPair; |
| 438 | using JumpSource = BlockScopePosPair; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 439 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 440 | ASTContext *Context; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 441 | std::unique_ptr<CFG> cfg; |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 442 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 443 | // Current block. |
| 444 | CFGBlock *Block = nullptr; |
| 445 | |
| 446 | // Block after the current block. |
| 447 | CFGBlock *Succ = nullptr; |
| 448 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 449 | JumpTarget ContinueJumpTarget; |
| 450 | JumpTarget BreakJumpTarget; |
Nico Weber | 699670e | 2017-08-23 15:33:16 +0000 | [diff] [blame] | 451 | JumpTarget SEHLeaveJumpTarget; |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 452 | CFGBlock *SwitchTerminatedBlock = nullptr; |
| 453 | CFGBlock *DefaultCaseBlock = nullptr; |
Nico Weber | 699670e | 2017-08-23 15:33:16 +0000 | [diff] [blame] | 454 | |
| 455 | // This can point either to a try or a __try block. The frontend forbids |
| 456 | // mixing both kinds in one function, so having one for both is enough. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 457 | CFGBlock *TryTerminatedBlock = nullptr; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 458 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 459 | // Current position in local scope. |
| 460 | LocalScope::const_iterator ScopePos; |
| 461 | |
| 462 | // LabelMap records the mapping from Label expressions to their jump targets. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 463 | using LabelMapTy = llvm::DenseMap<LabelDecl *, JumpTarget>; |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 464 | LabelMapTy LabelMap; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 465 | |
| 466 | // A list of blocks that end with a "goto" that must be backpatched to their |
| 467 | // resolved targets upon completion of CFG construction. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 468 | using BackpatchBlocksTy = std::vector<JumpSource>; |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 469 | BackpatchBlocksTy BackpatchBlocks; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 470 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 471 | // A list of labels whose address has been taken (for indirect gotos). |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 472 | using LabelSetTy = llvm::SmallSetVector<LabelDecl *, 8>; |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 473 | LabelSetTy AddressTakenLabels; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 474 | |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 475 | // Information about the currently visited C++ object construction site. |
| 476 | // This is set in the construction trigger and read when the constructor |
| 477 | // itself is being visited. |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 478 | llvm::DenseMap<CXXConstructExpr *, const ConstructionContext *> |
Artem Dergachev | 5e2f6ba | 2018-02-23 22:49:25 +0000 | [diff] [blame] | 479 | ConstructionContextMap; |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 480 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 481 | bool badCFG = false; |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 482 | const CFG::BuildOptions &BuildOpts; |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 483 | |
| 484 | // State to track for building switch statements. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 485 | bool switchExclusivelyCovered = false; |
| 486 | Expr::EvalResult *switchCond = nullptr; |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 487 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 488 | CFG::BuildOptions::ForcedBlkExprs::value_type *cachedEntry = nullptr; |
| 489 | const Stmt *lastLookup = nullptr; |
Zhongxing Xu | d38fb84 | 2010-09-16 03:28:18 +0000 | [diff] [blame] | 490 | |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 491 | // Caches boolean evaluations of expressions to avoid multiple re-evaluations |
| 492 | // during construction of branches for chained logical operators. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 493 | using CachedBoolEvalsTy = llvm::DenseMap<Expr *, TryResult>; |
NAKAMURA Takumi | e9ca55e | 2012-03-25 06:30:37 +0000 | [diff] [blame] | 494 | CachedBoolEvalsTy CachedBoolEvals; |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 495 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 496 | public: |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 497 | explicit CFGBuilder(ASTContext *astContext, |
Nico Weber | 699670e | 2017-08-23 15:33:16 +0000 | [diff] [blame] | 498 | const CFG::BuildOptions &buildOpts) |
| 499 | : Context(astContext), cfg(new CFG()), // crew a new CFG |
Artem Dergachev | 5e2f6ba | 2018-02-23 22:49:25 +0000 | [diff] [blame] | 500 | ConstructionContextMap(), BuildOpts(buildOpts) {} |
| 501 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 503 | // buildCFG - Used by external clients to construct the CFG. |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 504 | std::unique_ptr<CFG> buildCFG(const Decl *D, Stmt *Statement); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 505 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 506 | bool alwaysAdd(const Stmt *stmt); |
| 507 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 508 | private: |
| 509 | // Visitors to walk an AST and construct the CFG. |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 510 | CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc); |
| 511 | CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 512 | CFGBlock *VisitBreakStmt(BreakStmt *B); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 513 | CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 514 | CFGBlock *VisitCaseStmt(CaseStmt *C); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 515 | CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 516 | CFGBlock *VisitCompoundStmt(CompoundStmt *C); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 517 | CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C, |
| 518 | AddStmtChoice asc); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 519 | CFGBlock *VisitContinueStmt(ContinueStmt *C); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 520 | CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E, |
| 521 | AddStmtChoice asc); |
| 522 | CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S); |
| 523 | CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc); |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 524 | CFGBlock *VisitCXXNewExpr(CXXNewExpr *DE, AddStmtChoice asc); |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 525 | CFGBlock *VisitCXXDeleteExpr(CXXDeleteExpr *DE, AddStmtChoice asc); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 526 | CFGBlock *VisitCXXForRangeStmt(CXXForRangeStmt *S); |
| 527 | CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E, |
| 528 | AddStmtChoice asc); |
| 529 | CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C, |
| 530 | AddStmtChoice asc); |
| 531 | CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T); |
| 532 | CFGBlock *VisitCXXTryStmt(CXXTryStmt *S); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 533 | CFGBlock *VisitDeclStmt(DeclStmt *DS); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 534 | CFGBlock *VisitDeclSubExpr(DeclStmt *DS); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 535 | CFGBlock *VisitDefaultStmt(DefaultStmt *D); |
| 536 | CFGBlock *VisitDoStmt(DoStmt *D); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 537 | CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E, AddStmtChoice asc); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 538 | CFGBlock *VisitForStmt(ForStmt *F); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 539 | CFGBlock *VisitGotoStmt(GotoStmt *G); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 540 | CFGBlock *VisitIfStmt(IfStmt *I); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 541 | CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 542 | CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I); |
| 543 | CFGBlock *VisitLabelStmt(LabelStmt *L); |
Devin Coughlin | b6029b7 | 2015-11-25 22:35:37 +0000 | [diff] [blame] | 544 | CFGBlock *VisitBlockExpr(BlockExpr *E, AddStmtChoice asc); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 545 | CFGBlock *VisitLambdaExpr(LambdaExpr *E, AddStmtChoice asc); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 546 | CFGBlock *VisitLogicalOperator(BinaryOperator *B); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 547 | std::pair<CFGBlock *, CFGBlock *> VisitLogicalOperator(BinaryOperator *B, |
| 548 | Stmt *Term, |
| 549 | CFGBlock *TrueBlock, |
| 550 | CFGBlock *FalseBlock); |
Artem Dergachev | f43ac4c | 2018-02-24 02:00:30 +0000 | [diff] [blame^] | 551 | CFGBlock *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *MTE, |
| 552 | AddStmtChoice asc); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 553 | CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 554 | CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 555 | CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 556 | CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 557 | CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 558 | CFGBlock *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 559 | CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 560 | CFGBlock *VisitPseudoObjectExpr(PseudoObjectExpr *E); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 561 | CFGBlock *VisitReturnStmt(ReturnStmt *R); |
Nico Weber | 699670e | 2017-08-23 15:33:16 +0000 | [diff] [blame] | 562 | CFGBlock *VisitSEHExceptStmt(SEHExceptStmt *S); |
| 563 | CFGBlock *VisitSEHFinallyStmt(SEHFinallyStmt *S); |
| 564 | CFGBlock *VisitSEHLeaveStmt(SEHLeaveStmt *S); |
| 565 | CFGBlock *VisitSEHTryStmt(SEHTryStmt *S); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 566 | CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 567 | CFGBlock *VisitSwitchStmt(SwitchStmt *S); |
Ted Kremenek | 6f40024 | 2012-07-14 05:04:01 +0000 | [diff] [blame] | 568 | CFGBlock *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E, |
| 569 | AddStmtChoice asc); |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 570 | CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 571 | CFGBlock *VisitWhileStmt(WhileStmt *W); |
Mike Stump | 48871a2 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 572 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 573 | CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd); |
| 574 | CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 575 | CFGBlock *VisitChildren(Stmt *S); |
Ted Kremenek | e249984 | 2012-04-12 20:03:44 +0000 | [diff] [blame] | 576 | CFGBlock *VisitNoRecurse(Expr *E, AddStmtChoice asc); |
Mike Stump | 48871a2 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 577 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 578 | /// When creating the CFG for temporary destructors, we want to mirror the |
| 579 | /// branch structure of the corresponding constructor calls. |
| 580 | /// Thus, while visiting a statement for temporary destructors, we keep a |
| 581 | /// context to keep track of the following information: |
| 582 | /// - whether a subexpression is executed unconditionally |
| 583 | /// - if a subexpression is executed conditionally, the first |
| 584 | /// CXXBindTemporaryExpr we encounter in that subexpression (which |
| 585 | /// corresponds to the last temporary destructor we have to call for this |
| 586 | /// subexpression) and the CFG block at that point (which will become the |
| 587 | /// successor block when inserting the decision point). |
| 588 | /// |
| 589 | /// That way, we can build the branch structure for temporary destructors as |
| 590 | /// follows: |
| 591 | /// 1. If a subexpression is executed unconditionally, we add the temporary |
| 592 | /// destructor calls to the current block. |
| 593 | /// 2. If a subexpression is executed conditionally, when we encounter a |
| 594 | /// CXXBindTemporaryExpr: |
| 595 | /// a) If it is the first temporary destructor call in the subexpression, |
| 596 | /// we remember the CXXBindTemporaryExpr and the current block in the |
| 597 | /// TempDtorContext; we start a new block, and insert the temporary |
| 598 | /// destructor call. |
| 599 | /// b) Otherwise, add the temporary destructor call to the current block. |
| 600 | /// 3. When we finished visiting a conditionally executed subexpression, |
| 601 | /// and we found at least one temporary constructor during the visitation |
| 602 | /// (2.a has executed), we insert a decision block that uses the |
| 603 | /// CXXBindTemporaryExpr as terminator, and branches to the current block |
| 604 | /// if the CXXBindTemporaryExpr was marked executed, and otherwise |
| 605 | /// branches to the stored successor. |
| 606 | struct TempDtorContext { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 607 | TempDtorContext() = default; |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 608 | TempDtorContext(TryResult KnownExecuted) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 609 | : IsConditional(true), KnownExecuted(KnownExecuted) {} |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 610 | |
| 611 | /// 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] | 612 | /// call. This is the case when the temporary destructor is |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 613 | /// conditionally executed, and it is the first one we encounter while |
| 614 | /// visiting a subexpression - other temporary destructors at the same level |
| 615 | /// will be added to the same block and are executed under the same |
| 616 | /// condition. |
| 617 | bool needsTempDtorBranch() const { |
| 618 | return IsConditional && !TerminatorExpr; |
| 619 | } |
| 620 | |
| 621 | /// Remember the successor S of a temporary destructor decision branch for |
| 622 | /// the corresponding CXXBindTemporaryExpr E. |
| 623 | void setDecisionPoint(CFGBlock *S, CXXBindTemporaryExpr *E) { |
| 624 | Succ = S; |
| 625 | TerminatorExpr = E; |
| 626 | } |
| 627 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 628 | const bool IsConditional = false; |
| 629 | const TryResult KnownExecuted = true; |
| 630 | CFGBlock *Succ = nullptr; |
| 631 | CXXBindTemporaryExpr *TerminatorExpr = nullptr; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 632 | }; |
| 633 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 634 | // Visitors to walk an AST and generate destructors of temporaries in |
| 635 | // full expression. |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 636 | CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary, |
| 637 | TempDtorContext &Context); |
| 638 | CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E, TempDtorContext &Context); |
| 639 | CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E, |
| 640 | TempDtorContext &Context); |
| 641 | CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors( |
| 642 | CXXBindTemporaryExpr *E, bool BindToTemporary, TempDtorContext &Context); |
| 643 | CFGBlock *VisitConditionalOperatorForTemporaryDtors( |
| 644 | AbstractConditionalOperator *E, bool BindToTemporary, |
| 645 | TempDtorContext &Context); |
| 646 | void InsertTempDtorDecisionBlock(const TempDtorContext &Context, |
| 647 | CFGBlock *FalseSucc = nullptr); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 648 | |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 649 | // NYS == Not Yet Supported |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 650 | CFGBlock *NYS() { |
Ted Kremenek | b64d183 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 651 | badCFG = true; |
| 652 | return Block; |
| 653 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 654 | |
Artem Dergachev | c1b07bd | 2018-02-23 23:38:41 +0000 | [diff] [blame] | 655 | // Remember to apply \p CC when constructing the CFG element for \p CE. |
| 656 | void consumeConstructionContext(const ConstructionContext *CC, |
| 657 | CXXConstructExpr *CE); |
| 658 | |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 659 | // Scan the child statement \p Child to find the constructor that might |
| 660 | // have been directly triggered by the current node, \p Trigger. If such |
| 661 | // constructor has been found, set current construction context to point |
| 662 | // to the trigger statement. The construction context will be unset once |
| 663 | // it is consumed when the CFG building procedure processes the |
| 664 | // construct-expression and adds the respective CFGConstructor element. |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 665 | void findConstructionContexts(const ConstructionContext *ContextSoFar, |
| 666 | Stmt *Child); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 667 | // Unset the construction context after consuming it. This is done immediately |
| 668 | // after adding the CFGConstructor element, so there's no need to |
| 669 | // do this manually in every Visit... function. |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 670 | void cleanupConstructionContext(CXXConstructExpr *CE); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 671 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 672 | void autoCreateBlock() { if (!Block) Block = createBlock(); } |
| 673 | CFGBlock *createBlock(bool add_successor = true); |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 674 | CFGBlock *createNoReturnBlock(); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 675 | |
Zhongxing Xu | ea9fcff | 2010-06-03 06:43:23 +0000 | [diff] [blame] | 676 | CFGBlock *addStmt(Stmt *S) { |
| 677 | return Visit(S, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 678 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 679 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 680 | CFGBlock *addInitializer(CXXCtorInitializer *I); |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 681 | void addLoopExit(const Stmt *LoopStmt); |
Zhongxing Xu | 6d372f7 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 682 | void addAutomaticObjDtors(LocalScope::const_iterator B, |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 683 | LocalScope::const_iterator E, Stmt *S); |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 684 | void addLifetimeEnds(LocalScope::const_iterator B, |
| 685 | LocalScope::const_iterator E, Stmt *S); |
| 686 | void addAutomaticObjHandling(LocalScope::const_iterator B, |
| 687 | LocalScope::const_iterator E, Stmt *S); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 688 | void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 689 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 690 | // Local scopes creation. |
| 691 | LocalScope* createOrReuseLocalScope(LocalScope* Scope); |
| 692 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 693 | void addLocalScopeForStmt(Stmt *S); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 694 | LocalScope* addLocalScopeForDeclStmt(DeclStmt *DS, |
| 695 | LocalScope* Scope = nullptr); |
| 696 | LocalScope* addLocalScopeForVarDecl(VarDecl *VD, LocalScope* Scope = nullptr); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 697 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 698 | void addLocalScopeAndDtors(Stmt *S); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 699 | |
| 700 | // Interface to CFGBlock - adding CFGElements. |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 701 | |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 702 | void appendStmt(CFGBlock *B, const Stmt *S) { |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 703 | if (alwaysAdd(S) && cachedEntry) |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 704 | cachedEntry->second = B; |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 705 | |
Jordy Rose | 1734737 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 706 | // All block-level expressions should have already been IgnoreParens()ed. |
| 707 | assert(!isa<Expr>(S) || cast<Expr>(S)->IgnoreParens() == S); |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 708 | B->appendStmt(const_cast<Stmt*>(S), cfg->getBumpVectorContext()); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 709 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 710 | |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 711 | void appendConstructor(CFGBlock *B, CXXConstructExpr *CE) { |
| 712 | if (BuildOpts.AddRichCXXConstructors) { |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 713 | if (const ConstructionContext *CC = ConstructionContextMap.lookup(CE)) { |
| 714 | B->appendConstructor(CE, CC, cfg->getBumpVectorContext()); |
| 715 | cleanupConstructionContext(CE); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 716 | return; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // No valid construction context found. Fall back to statement. |
| 721 | B->appendStmt(CE, cfg->getBumpVectorContext()); |
| 722 | } |
| 723 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 724 | void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) { |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 725 | B->appendInitializer(I, cfg->getBumpVectorContext()); |
| 726 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 727 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 728 | void appendNewAllocator(CFGBlock *B, CXXNewExpr *NE) { |
| 729 | B->appendNewAllocator(NE, cfg->getBumpVectorContext()); |
| 730 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 731 | |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 732 | void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) { |
| 733 | B->appendBaseDtor(BS, cfg->getBumpVectorContext()); |
| 734 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 735 | |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 736 | void appendMemberDtor(CFGBlock *B, FieldDecl *FD) { |
| 737 | B->appendMemberDtor(FD, cfg->getBumpVectorContext()); |
| 738 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 739 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 740 | void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) { |
| 741 | B->appendTemporaryDtor(E, cfg->getBumpVectorContext()); |
| 742 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 743 | |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 744 | void appendAutomaticObjDtor(CFGBlock *B, VarDecl *VD, Stmt *S) { |
| 745 | B->appendAutomaticObjDtor(VD, S, cfg->getBumpVectorContext()); |
| 746 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 747 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 748 | void appendLifetimeEnds(CFGBlock *B, VarDecl *VD, Stmt *S) { |
| 749 | B->appendLifetimeEnds(VD, S, cfg->getBumpVectorContext()); |
| 750 | } |
| 751 | |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 752 | void appendLoopExit(CFGBlock *B, const Stmt *LoopStmt) { |
| 753 | B->appendLoopExit(LoopStmt, cfg->getBumpVectorContext()); |
| 754 | } |
| 755 | |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 756 | void appendDeleteDtor(CFGBlock *B, CXXRecordDecl *RD, CXXDeleteExpr *DE) { |
| 757 | B->appendDeleteDtor(RD, DE, cfg->getBumpVectorContext()); |
| 758 | } |
| 759 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 760 | void prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk, |
Marcin Swiderski | 321a707 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 761 | LocalScope::const_iterator B, LocalScope::const_iterator E); |
| 762 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 763 | void prependAutomaticObjLifetimeWithTerminator(CFGBlock *Blk, |
| 764 | LocalScope::const_iterator B, |
| 765 | LocalScope::const_iterator E); |
| 766 | |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 767 | void addSuccessor(CFGBlock *B, CFGBlock *S, bool IsReachable = true) { |
| 768 | B->addSuccessor(CFGBlock::AdjacentBlock(S, IsReachable), |
| 769 | cfg->getBumpVectorContext()); |
| 770 | } |
| 771 | |
| 772 | /// Add a reachable successor to a block, with the alternate variant that is |
| 773 | /// unreachable. |
| 774 | void addSuccessor(CFGBlock *B, CFGBlock *ReachableBlock, CFGBlock *AltBlock) { |
| 775 | B->addSuccessor(CFGBlock::AdjacentBlock(ReachableBlock, AltBlock), |
| 776 | cfg->getBumpVectorContext()); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 777 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 779 | /// \brief Find a relational comparison with an expression evaluating to a |
| 780 | /// boolean and a constant other than 0 and 1. |
| 781 | /// e.g. if ((x < y) == 10) |
| 782 | TryResult checkIncorrectRelationalOperator(const BinaryOperator *B) { |
| 783 | const Expr *LHSExpr = B->getLHS()->IgnoreParens(); |
| 784 | const Expr *RHSExpr = B->getRHS()->IgnoreParens(); |
| 785 | |
| 786 | const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(LHSExpr); |
| 787 | const Expr *BoolExpr = RHSExpr; |
| 788 | bool IntFirst = true; |
| 789 | if (!IntLiteral) { |
| 790 | IntLiteral = dyn_cast<IntegerLiteral>(RHSExpr); |
| 791 | BoolExpr = LHSExpr; |
| 792 | IntFirst = false; |
| 793 | } |
| 794 | |
| 795 | if (!IntLiteral || !BoolExpr->isKnownToHaveBooleanValue()) |
| 796 | return TryResult(); |
| 797 | |
| 798 | llvm::APInt IntValue = IntLiteral->getValue(); |
| 799 | if ((IntValue == 1) || (IntValue == 0)) |
| 800 | return TryResult(); |
| 801 | |
| 802 | bool IntLarger = IntLiteral->getType()->isUnsignedIntegerType() || |
| 803 | !IntValue.isNegative(); |
| 804 | |
| 805 | BinaryOperatorKind Bok = B->getOpcode(); |
| 806 | if (Bok == BO_GT || Bok == BO_GE) { |
| 807 | // Always true for 10 > bool and bool > -1 |
| 808 | // Always false for -1 > bool and bool > 10 |
| 809 | return TryResult(IntFirst == IntLarger); |
| 810 | } else { |
| 811 | // Always true for -1 < bool and bool < 10 |
| 812 | // Always false for 10 < bool and bool < -1 |
| 813 | return TryResult(IntFirst != IntLarger); |
| 814 | } |
| 815 | } |
| 816 | |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 817 | /// Find an incorrect equality comparison. Either with an expression |
| 818 | /// evaluating to a boolean and a constant other than 0 and 1. |
| 819 | /// e.g. if (!x == 10) or a bitwise and/or operation that always evaluates to |
| 820 | /// true/false e.q. (x & 8) == 4. |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 821 | TryResult checkIncorrectEqualityOperator(const BinaryOperator *B) { |
| 822 | const Expr *LHSExpr = B->getLHS()->IgnoreParens(); |
| 823 | const Expr *RHSExpr = B->getRHS()->IgnoreParens(); |
| 824 | |
| 825 | const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(LHSExpr); |
| 826 | const Expr *BoolExpr = RHSExpr; |
| 827 | |
| 828 | if (!IntLiteral) { |
| 829 | IntLiteral = dyn_cast<IntegerLiteral>(RHSExpr); |
| 830 | BoolExpr = LHSExpr; |
| 831 | } |
| 832 | |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 833 | if (!IntLiteral) |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 834 | return TryResult(); |
| 835 | |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 836 | const BinaryOperator *BitOp = dyn_cast<BinaryOperator>(BoolExpr); |
| 837 | if (BitOp && (BitOp->getOpcode() == BO_And || |
| 838 | BitOp->getOpcode() == BO_Or)) { |
| 839 | const Expr *LHSExpr2 = BitOp->getLHS()->IgnoreParens(); |
| 840 | const Expr *RHSExpr2 = BitOp->getRHS()->IgnoreParens(); |
| 841 | |
| 842 | const IntegerLiteral *IntLiteral2 = dyn_cast<IntegerLiteral>(LHSExpr2); |
| 843 | |
| 844 | if (!IntLiteral2) |
| 845 | IntLiteral2 = dyn_cast<IntegerLiteral>(RHSExpr2); |
| 846 | |
| 847 | if (!IntLiteral2) |
| 848 | return TryResult(); |
| 849 | |
| 850 | llvm::APInt L1 = IntLiteral->getValue(); |
| 851 | llvm::APInt L2 = IntLiteral2->getValue(); |
| 852 | if ((BitOp->getOpcode() == BO_And && (L2 & L1) != L1) || |
| 853 | (BitOp->getOpcode() == BO_Or && (L2 | L1) != L1)) { |
| 854 | if (BuildOpts.Observer) |
| 855 | BuildOpts.Observer->compareBitwiseEquality(B, |
| 856 | B->getOpcode() != BO_EQ); |
| 857 | TryResult(B->getOpcode() != BO_EQ); |
| 858 | } |
| 859 | } else if (BoolExpr->isKnownToHaveBooleanValue()) { |
| 860 | llvm::APInt IntValue = IntLiteral->getValue(); |
| 861 | if ((IntValue == 1) || (IntValue == 0)) { |
| 862 | return TryResult(); |
| 863 | } |
| 864 | return TryResult(B->getOpcode() != BO_EQ); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Jordan Rose | 7afd71e | 2014-05-20 17:31:11 +0000 | [diff] [blame] | 867 | return TryResult(); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | TryResult analyzeLogicOperatorCondition(BinaryOperatorKind Relation, |
| 871 | const llvm::APSInt &Value1, |
| 872 | const llvm::APSInt &Value2) { |
| 873 | assert(Value1.isSigned() == Value2.isSigned()); |
| 874 | switch (Relation) { |
| 875 | default: |
| 876 | return TryResult(); |
| 877 | case BO_EQ: |
| 878 | return TryResult(Value1 == Value2); |
| 879 | case BO_NE: |
| 880 | return TryResult(Value1 != Value2); |
| 881 | case BO_LT: |
| 882 | return TryResult(Value1 < Value2); |
| 883 | case BO_LE: |
| 884 | return TryResult(Value1 <= Value2); |
| 885 | case BO_GT: |
| 886 | return TryResult(Value1 > Value2); |
| 887 | case BO_GE: |
| 888 | return TryResult(Value1 >= Value2); |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | /// \brief Find a pair of comparison expressions with or without parentheses |
| 893 | /// with a shared variable and constants and a logical operator between them |
| 894 | /// that always evaluates to either true or false. |
| 895 | /// e.g. if (x != 3 || x != 4) |
| 896 | TryResult checkIncorrectLogicOperator(const BinaryOperator *B) { |
| 897 | assert(B->isLogicalOp()); |
| 898 | const BinaryOperator *LHS = |
| 899 | dyn_cast<BinaryOperator>(B->getLHS()->IgnoreParens()); |
| 900 | const BinaryOperator *RHS = |
| 901 | dyn_cast<BinaryOperator>(B->getRHS()->IgnoreParens()); |
| 902 | if (!LHS || !RHS) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 903 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 904 | |
| 905 | if (!LHS->isComparisonOp() || !RHS->isComparisonOp()) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 906 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 907 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 908 | const DeclRefExpr *Decl1; |
| 909 | const Expr *Expr1; |
| 910 | BinaryOperatorKind BO1; |
| 911 | std::tie(Decl1, BO1, Expr1) = tryNormalizeBinaryOperator(LHS); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 912 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 913 | if (!Decl1 || !Expr1) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 914 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 915 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 916 | const DeclRefExpr *Decl2; |
| 917 | const Expr *Expr2; |
| 918 | BinaryOperatorKind BO2; |
| 919 | std::tie(Decl2, BO2, Expr2) = tryNormalizeBinaryOperator(RHS); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 920 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 921 | if (!Decl2 || !Expr2) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 922 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 923 | |
| 924 | // Check that it is the same variable on both sides. |
| 925 | if (Decl1->getDecl() != Decl2->getDecl()) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 926 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 927 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 928 | // Make sure the user's intent is clear (e.g. they're comparing against two |
| 929 | // int literals, or two things from the same enum) |
| 930 | if (!areExprTypesCompatible(Expr1, Expr2)) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 931 | return {}; |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 932 | |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 933 | llvm::APSInt L1, L2; |
| 934 | |
George Burgess IV | ced56e6 | 2015-10-01 18:47:52 +0000 | [diff] [blame] | 935 | if (!Expr1->EvaluateAsInt(L1, *Context) || |
| 936 | !Expr2->EvaluateAsInt(L2, *Context)) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 937 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 938 | |
| 939 | // Can't compare signed with unsigned or with different bit width. |
| 940 | if (L1.isSigned() != L2.isSigned() || L1.getBitWidth() != L2.getBitWidth()) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 941 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 942 | |
| 943 | // Values that will be used to determine if result of logical |
| 944 | // operator is always true/false |
| 945 | const llvm::APSInt Values[] = { |
| 946 | // Value less than both Value1 and Value2 |
| 947 | llvm::APSInt::getMinValue(L1.getBitWidth(), L1.isUnsigned()), |
| 948 | // L1 |
| 949 | L1, |
| 950 | // Value between Value1 and Value2 |
| 951 | ((L1 < L2) ? L1 : L2) + llvm::APSInt(llvm::APInt(L1.getBitWidth(), 1), |
| 952 | L1.isUnsigned()), |
| 953 | // L2 |
| 954 | L2, |
| 955 | // Value greater than both Value1 and Value2 |
| 956 | llvm::APSInt::getMaxValue(L1.getBitWidth(), L1.isUnsigned()), |
| 957 | }; |
| 958 | |
| 959 | // Check whether expression is always true/false by evaluating the following |
| 960 | // * variable x is less than the smallest literal. |
| 961 | // * variable x is equal to the smallest literal. |
| 962 | // * Variable x is between smallest and largest literal. |
| 963 | // * Variable x is equal to the largest literal. |
| 964 | // * Variable x is greater than largest literal. |
| 965 | bool AlwaysTrue = true, AlwaysFalse = true; |
Benjamin Kramer | 2e018ef | 2016-05-27 13:36:58 +0000 | [diff] [blame] | 966 | for (const llvm::APSInt &Value : Values) { |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 967 | TryResult Res1, Res2; |
| 968 | Res1 = analyzeLogicOperatorCondition(BO1, Value, L1); |
| 969 | Res2 = analyzeLogicOperatorCondition(BO2, Value, L2); |
| 970 | |
| 971 | if (!Res1.isKnown() || !Res2.isKnown()) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 972 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 973 | |
| 974 | if (B->getOpcode() == BO_LAnd) { |
| 975 | AlwaysTrue &= (Res1.isTrue() && Res2.isTrue()); |
| 976 | AlwaysFalse &= !(Res1.isTrue() && Res2.isTrue()); |
| 977 | } else { |
| 978 | AlwaysTrue &= (Res1.isTrue() || Res2.isTrue()); |
| 979 | AlwaysFalse &= !(Res1.isTrue() || Res2.isTrue()); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | if (AlwaysTrue || AlwaysFalse) { |
| 984 | if (BuildOpts.Observer) |
| 985 | BuildOpts.Observer->compareAlwaysTrue(B, AlwaysTrue); |
| 986 | return TryResult(AlwaysTrue); |
| 987 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 988 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 991 | /// Try and evaluate an expression to an integer constant. |
| 992 | bool tryEvaluate(Expr *S, Expr::EvalResult &outResult) { |
| 993 | if (!BuildOpts.PruneTriviallyFalseEdges) |
| 994 | return false; |
| 995 | return !S->isTypeDependent() && |
Ted Kremenek | 352a708 | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 996 | !S->isValueDependent() && |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 997 | S->EvaluateAsRValue(outResult, *Context); |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 998 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 1000 | /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1 |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1001 | /// if we can evaluate to a known value, otherwise return -1. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 1002 | TryResult tryEvaluateBool(Expr *S) { |
Richard Smith | faa32a9 | 2011-10-14 20:22:00 +0000 | [diff] [blame] | 1003 | if (!BuildOpts.PruneTriviallyFalseEdges || |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 1004 | S->isTypeDependent() || S->isValueDependent()) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 1005 | return {}; |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 1006 | |
| 1007 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(S)) { |
| 1008 | if (Bop->isLogicalOp()) { |
| 1009 | // Check the cache first. |
NAKAMURA Takumi | e9ca55e | 2012-03-25 06:30:37 +0000 | [diff] [blame] | 1010 | CachedBoolEvalsTy::iterator I = CachedBoolEvals.find(S); |
| 1011 | if (I != CachedBoolEvals.end()) |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 1012 | return I->second; // already in map; |
NAKAMURA Takumi | f0434b0 | 2012-03-25 06:30:32 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Retrieve result at first, or the map might be updated. |
| 1015 | TryResult Result = evaluateAsBooleanConditionNoCache(S); |
| 1016 | CachedBoolEvals[S] = Result; // update or insert |
| 1017 | return Result; |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 1018 | } |
Ted Kremenek | 64fea5f | 2012-08-24 07:42:09 +0000 | [diff] [blame] | 1019 | else { |
| 1020 | switch (Bop->getOpcode()) { |
| 1021 | default: break; |
| 1022 | // For 'x & 0' and 'x * 0', we can determine that |
| 1023 | // the value is always false. |
| 1024 | case BO_Mul: |
| 1025 | case BO_And: { |
| 1026 | // If either operand is zero, we know the value |
| 1027 | // must be false. |
| 1028 | llvm::APSInt IntVal; |
| 1029 | if (Bop->getLHS()->EvaluateAsInt(IntVal, *Context)) { |
David Blaikie | 7a3cbb2 | 2015-03-09 02:02:07 +0000 | [diff] [blame] | 1030 | if (!IntVal.getBoolValue()) { |
Ted Kremenek | 64fea5f | 2012-08-24 07:42:09 +0000 | [diff] [blame] | 1031 | return TryResult(false); |
| 1032 | } |
| 1033 | } |
| 1034 | if (Bop->getRHS()->EvaluateAsInt(IntVal, *Context)) { |
David Blaikie | 7a3cbb2 | 2015-03-09 02:02:07 +0000 | [diff] [blame] | 1035 | if (!IntVal.getBoolValue()) { |
Ted Kremenek | 64fea5f | 2012-08-24 07:42:09 +0000 | [diff] [blame] | 1036 | return TryResult(false); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | break; |
| 1041 | } |
| 1042 | } |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | return evaluateAsBooleanConditionNoCache(S); |
| 1046 | } |
| 1047 | |
| 1048 | /// \brief Evaluate as boolean \param E without using the cache. |
| 1049 | TryResult evaluateAsBooleanConditionNoCache(Expr *E) { |
| 1050 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(E)) { |
| 1051 | if (Bop->isLogicalOp()) { |
| 1052 | TryResult LHS = tryEvaluateBool(Bop->getLHS()); |
| 1053 | if (LHS.isKnown()) { |
| 1054 | // We were able to evaluate the LHS, see if we can get away with not |
| 1055 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
| 1056 | if (LHS.isTrue() == (Bop->getOpcode() == BO_LOr)) |
| 1057 | return LHS.isTrue(); |
| 1058 | |
| 1059 | TryResult RHS = tryEvaluateBool(Bop->getRHS()); |
| 1060 | if (RHS.isKnown()) { |
| 1061 | if (Bop->getOpcode() == BO_LOr) |
| 1062 | return LHS.isTrue() || RHS.isTrue(); |
| 1063 | else |
| 1064 | return LHS.isTrue() && RHS.isTrue(); |
| 1065 | } |
| 1066 | } else { |
| 1067 | TryResult RHS = tryEvaluateBool(Bop->getRHS()); |
| 1068 | if (RHS.isKnown()) { |
| 1069 | // We can't evaluate the LHS; however, sometimes the result |
| 1070 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 1071 | if (RHS.isTrue() == (Bop->getOpcode() == BO_LOr)) |
| 1072 | return RHS.isTrue(); |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | TryResult BopRes = checkIncorrectLogicOperator(Bop); |
| 1075 | if (BopRes.isKnown()) |
| 1076 | return BopRes.isTrue(); |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 1080 | return {}; |
Richard Trieu | f935b56 | 2014-04-05 05:17:01 +0000 | [diff] [blame] | 1081 | } else if (Bop->isEqualityOp()) { |
| 1082 | TryResult BopRes = checkIncorrectEqualityOperator(Bop); |
| 1083 | if (BopRes.isKnown()) |
| 1084 | return BopRes.isTrue(); |
| 1085 | } else if (Bop->isRelationalOp()) { |
| 1086 | TryResult BopRes = checkIncorrectRelationalOperator(Bop); |
| 1087 | if (BopRes.isKnown()) |
| 1088 | return BopRes.isTrue(); |
Argyrios Kyrtzidis | 5f172a3 | 2012-03-23 00:59:17 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | bool Result; |
| 1093 | if (E->EvaluateAsBooleanCondition(Result, *Context)) |
| 1094 | return Result; |
| 1095 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 1096 | return {}; |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 1097 | } |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1098 | |
| 1099 | bool hasTrivialDestructor(VarDecl *VD); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1100 | }; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1101 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 1102 | } // namespace |
| 1103 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1104 | inline bool AddStmtChoice::alwaysAdd(CFGBuilder &builder, |
| 1105 | const Stmt *stmt) const { |
| 1106 | return builder.alwaysAdd(stmt) || kind == AlwaysAdd; |
| 1107 | } |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 1108 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1109 | bool CFGBuilder::alwaysAdd(const Stmt *stmt) { |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 1110 | bool shouldAdd = BuildOpts.alwaysAdd(stmt); |
| 1111 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1112 | if (!BuildOpts.forcedBlkExprs) |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 1113 | return shouldAdd; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (lastLookup == stmt) { |
| 1116 | if (cachedEntry) { |
| 1117 | assert(cachedEntry->first == stmt); |
| 1118 | return true; |
| 1119 | } |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 1120 | return shouldAdd; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 1121 | } |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1122 | |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 1123 | lastLookup = stmt; |
| 1124 | |
| 1125 | // Perform the lookup! |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1126 | CFG::BuildOptions::ForcedBlkExprs *fb = *BuildOpts.forcedBlkExprs; |
| 1127 | |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 1128 | if (!fb) { |
| 1129 | // No need to update 'cachedEntry', since it will always be null. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1130 | assert(!cachedEntry); |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 1131 | return shouldAdd; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 1132 | } |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1133 | |
| 1134 | CFG::BuildOptions::ForcedBlkExprs::iterator itr = fb->find(stmt); |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 1135 | if (itr == fb->end()) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1136 | cachedEntry = nullptr; |
Ted Kremenek | 8b46c00 | 2011-07-19 14:18:43 +0000 | [diff] [blame] | 1137 | return shouldAdd; |
Ted Kremenek | dcc4c38 | 2011-03-23 21:33:21 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 1140 | cachedEntry = &*itr; |
| 1141 | return true; |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1144 | // FIXME: Add support for dependent-sized array types in C++? |
| 1145 | // 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] | 1146 | static const VariableArrayType *FindVA(const Type *t) { |
| 1147 | while (const ArrayType *vt = dyn_cast<ArrayType>(t)) { |
| 1148 | if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt)) |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 1149 | if (vat->getSizeExpr()) |
| 1150 | return vat; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1151 | |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 1152 | t = vt->getElementType().getTypePtr(); |
| 1153 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1154 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1155 | return nullptr; |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 1156 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1157 | |
Artem Dergachev | c1b07bd | 2018-02-23 23:38:41 +0000 | [diff] [blame] | 1158 | void CFGBuilder::consumeConstructionContext(const ConstructionContext *CC, CXXConstructExpr *CE) { |
| 1159 | if (const ConstructionContext *PreviousContext = |
| 1160 | ConstructionContextMap.lookup(CE)) { |
| 1161 | // We might have visited this child when we were finding construction |
| 1162 | // contexts within its parents. |
| 1163 | assert(PreviousContext->isStrictlyMoreSpecificThan(CC) && |
| 1164 | "Already within a different construction context!"); |
| 1165 | } else { |
| 1166 | ConstructionContextMap[CE] = CC; |
| 1167 | } |
| 1168 | } |
| 1169 | |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 1170 | void CFGBuilder::findConstructionContexts( |
| 1171 | const ConstructionContext *ContextSoFar, Stmt *Child) { |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 1172 | if (!BuildOpts.AddRichCXXConstructors) |
| 1173 | return; |
| 1174 | if (!Child) |
| 1175 | return; |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 1176 | if (auto *CE = dyn_cast<CXXConstructExpr>(Child)) { |
Artem Dergachev | c1b07bd | 2018-02-23 23:38:41 +0000 | [diff] [blame] | 1177 | consumeConstructionContext(ContextSoFar, CE); |
Artem Dergachev | 08225bb | 2018-02-10 02:46:14 +0000 | [diff] [blame] | 1178 | } else if (auto *Cleanups = dyn_cast<ExprWithCleanups>(Child)) { |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 1179 | findConstructionContexts(ContextSoFar, Cleanups->getSubExpr()); |
| 1180 | } else if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(Child)) { |
| 1181 | findConstructionContexts( |
| 1182 | ConstructionContext::create(cfg->getBumpVectorContext(), BTE, |
| 1183 | ContextSoFar), |
| 1184 | BTE->getSubExpr()); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 1188 | void CFGBuilder::cleanupConstructionContext(CXXConstructExpr *CE) { |
| 1189 | assert(BuildOpts.AddRichCXXConstructors && |
| 1190 | "We should not be managing construction contexts!"); |
| 1191 | assert(ConstructionContextMap.count(CE) && |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 1192 | "Cannot exit construction context without the context!"); |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 1193 | ConstructionContextMap.erase(CE); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1197 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an |
| 1198 | /// arbitrary statement. Examples include a single expression or a function |
| 1199 | /// body (compound statement). The ownership of the returned CFG is |
| 1200 | /// transferred to the caller. If CFG construction fails, this method returns |
| 1201 | /// NULL. |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 1202 | std::unique_ptr<CFG> CFGBuilder::buildCFG(const Decl *D, Stmt *Statement) { |
Ted Kremenek | 8aed490 | 2009-10-20 23:46:25 +0000 | [diff] [blame] | 1203 | assert(cfg.get()); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1204 | if (!Statement) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1205 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1206 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1207 | // Create an empty block that will serve as the exit block for the CFG. Since |
| 1208 | // this is the first block added to the CFG, it will be implicitly registered |
| 1209 | // as the exit block. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1210 | Succ = createBlock(); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 1211 | assert(Succ == &cfg->getExit()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1212 | Block = nullptr; // the EXIT block is empty. Create all other blocks lazily. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1213 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1214 | assert(!(BuildOpts.AddImplicitDtors && BuildOpts.AddLifetime) && |
| 1215 | "AddImplicitDtors and AddLifetime cannot be used at the same time"); |
| 1216 | |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1217 | if (BuildOpts.AddImplicitDtors) |
| 1218 | if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D)) |
| 1219 | addImplicitDtorsForDestructor(DD); |
| 1220 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1221 | // Visit the statements and create the CFG. |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1222 | CFGBlock *B = addStmt(Statement); |
| 1223 | |
| 1224 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1225 | return nullptr; |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1226 | |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1227 | // For C++ constructor add initializers to CFG. |
| 1228 | if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 1229 | for (auto *I : llvm::reverse(CD->inits())) { |
| 1230 | B = addInitializer(I); |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1231 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1232 | return nullptr; |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1236 | if (B) |
| 1237 | Succ = B; |
Mike Stump | 6bf1c08 | 2010-01-21 02:21:40 +0000 | [diff] [blame] | 1238 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1239 | // Backpatch the gotos whose label -> block mappings we didn't know when we |
| 1240 | // encountered them. |
| 1241 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 1242 | E = BackpatchBlocks.end(); I != E; ++I ) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1243 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1244 | CFGBlock *B = I->block; |
Rafael Espindola | 210de57 | 2013-03-27 15:37:54 +0000 | [diff] [blame] | 1245 | const GotoStmt *G = cast<GotoStmt>(B->getTerminator()); |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1246 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1247 | |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1248 | // If there is no target for the goto, then we are looking at an |
| 1249 | // incomplete AST. Handle this by not registering a successor. |
| 1250 | if (LI == LabelMap.end()) continue; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1251 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 1252 | JumpTarget JT = LI->second; |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1253 | prependAutomaticObjLifetimeWithTerminator(B, I->scopePosition, |
| 1254 | JT.scopePosition); |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 1255 | prependAutomaticObjDtorsWithTerminator(B, I->scopePosition, |
| 1256 | JT.scopePosition); |
| 1257 | addSuccessor(B, JT.block); |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | // Add successors to the Indirect Goto Dispatch block (if we have one). |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1261 | if (CFGBlock *B = cfg->getIndirectGotoBlock()) |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1262 | for (LabelSetTy::iterator I = AddressTakenLabels.begin(), |
| 1263 | E = AddressTakenLabels.end(); I != E; ++I ) { |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1264 | // Lookup the target block. |
| 1265 | LabelMapTy::iterator LI = LabelMap.find(*I); |
| 1266 | |
| 1267 | // If there is no target block that contains label, then we are looking |
| 1268 | // at an incomplete AST. Handle this by not registering a successor. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1269 | if (LI == LabelMap.end()) continue; |
Zhongxing Xu | b1e10aa | 2010-09-06 07:04:06 +0000 | [diff] [blame] | 1270 | |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 1271 | addSuccessor(B, LI->second.block); |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1272 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1273 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1274 | // Create an empty entry block that has no predecessors. |
Ted Kremenek | 5c50fd1 | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 1275 | cfg->setEntry(createBlock()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1276 | |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 1277 | if (BuildOpts.AddRichCXXConstructors) |
| 1278 | assert(ConstructionContextMap.empty() && |
| 1279 | "Not all construction contexts were cleaned up!"); |
| 1280 | |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 1281 | return std::move(cfg); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1282 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1283 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1284 | /// createBlock - Used to lazily create blocks that are connected |
| 1285 | /// to the current (global) succcessor. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1286 | CFGBlock *CFGBuilder::createBlock(bool add_successor) { |
| 1287 | CFGBlock *B = cfg->createBlock(); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1288 | if (add_successor && Succ) |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 1289 | addSuccessor(B, Succ); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1290 | return B; |
| 1291 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1292 | |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 1293 | /// createNoReturnBlock - Used to create a block is a 'noreturn' point in the |
| 1294 | /// CFG. It is *not* connected to the current (global) successor, and instead |
| 1295 | /// directly tied to the exit block in order to be reachable. |
| 1296 | CFGBlock *CFGBuilder::createNoReturnBlock() { |
| 1297 | CFGBlock *B = createBlock(false); |
Chandler Carruth | 75d7823 | 2011-09-13 09:53:55 +0000 | [diff] [blame] | 1298 | B->setHasNoReturnElement(); |
Ted Kremenek | f353919 | 2014-02-27 00:24:05 +0000 | [diff] [blame] | 1299 | addSuccessor(B, &cfg->getExit(), Succ); |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 1300 | return B; |
| 1301 | } |
| 1302 | |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1303 | /// addInitializer - Add C++ base or member initializer element to CFG. |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1304 | CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) { |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1305 | if (!BuildOpts.AddInitializers) |
| 1306 | return Block; |
| 1307 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1308 | bool HasTemporaries = false; |
| 1309 | |
| 1310 | // Destructors of temporaries in initialization expression should be called |
| 1311 | // after initialization finishes. |
| 1312 | Expr *Init = I->getInit(); |
| 1313 | if (Init) { |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1314 | HasTemporaries = isa<ExprWithCleanups>(Init); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1315 | |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 1316 | if (BuildOpts.AddTemporaryDtors && HasTemporaries) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1317 | // Generate destructors for temporaries in initialization expression. |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 1318 | TempDtorContext Context; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 1319 | VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(), |
| 1320 | /*BindToTemporary=*/false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1324 | autoCreateBlock(); |
| 1325 | appendInitializer(Block, I); |
| 1326 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1327 | if (Init) { |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 1328 | findConstructionContexts( |
| 1329 | ConstructionContext::create(cfg->getBumpVectorContext(), I), |
| 1330 | Init); |
Artem Dergachev | 5a281bb | 2018-02-10 02:18:04 +0000 | [diff] [blame] | 1331 | |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1332 | if (HasTemporaries) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1333 | // For expression with temporaries go directly to subexpression to omit |
| 1334 | // generating destructors for the second time. |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1335 | return Visit(cast<ExprWithCleanups>(Init)->getSubExpr()); |
| 1336 | } |
Enrico Pertoso | faed801 | 2015-06-03 10:12:40 +0000 | [diff] [blame] | 1337 | if (BuildOpts.AddCXXDefaultInitExprInCtors) { |
| 1338 | if (CXXDefaultInitExpr *Default = dyn_cast<CXXDefaultInitExpr>(Init)) { |
| 1339 | // In general, appending the expression wrapped by a CXXDefaultInitExpr |
| 1340 | // may cause the same Expr to appear more than once in the CFG. Doing it |
| 1341 | // here is safe because there's only one initializer per field. |
| 1342 | autoCreateBlock(); |
| 1343 | appendStmt(Block, Default); |
| 1344 | if (Stmt *Child = Default->getExpr()) |
| 1345 | if (CFGBlock *R = Visit(Child)) |
| 1346 | Block = R; |
| 1347 | return Block; |
| 1348 | } |
| 1349 | } |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1350 | return Visit(Init); |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1351 | } |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 1352 | |
Marcin Swiderski | 87b1bb6 | 2010-10-04 03:38:22 +0000 | [diff] [blame] | 1353 | return Block; |
| 1354 | } |
| 1355 | |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1356 | /// \brief Retrieve the type of the temporary object whose lifetime was |
| 1357 | /// extended by a local reference with the given initializer. |
| 1358 | static QualType getReferenceInitTemporaryType(ASTContext &Context, |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 1359 | const Expr *Init, |
| 1360 | bool *FoundMTE = nullptr) { |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1361 | while (true) { |
| 1362 | // Skip parentheses. |
| 1363 | Init = Init->IgnoreParens(); |
| 1364 | |
| 1365 | // Skip through cleanups. |
| 1366 | if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Init)) { |
| 1367 | Init = EWC->getSubExpr(); |
| 1368 | continue; |
| 1369 | } |
| 1370 | |
| 1371 | // Skip through the temporary-materialization expression. |
| 1372 | if (const MaterializeTemporaryExpr *MTE |
| 1373 | = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 1374 | Init = MTE->GetTemporaryExpr(); |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 1375 | if (FoundMTE) |
| 1376 | *FoundMTE = true; |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1377 | continue; |
| 1378 | } |
| 1379 | |
| 1380 | // Skip derived-to-base and no-op casts. |
| 1381 | if (const CastExpr *CE = dyn_cast<CastExpr>(Init)) { |
| 1382 | if ((CE->getCastKind() == CK_DerivedToBase || |
| 1383 | CE->getCastKind() == CK_UncheckedDerivedToBase || |
| 1384 | CE->getCastKind() == CK_NoOp) && |
| 1385 | Init->getType()->isRecordType()) { |
| 1386 | Init = CE->getSubExpr(); |
| 1387 | continue; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | // Skip member accesses into rvalues. |
| 1392 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Init)) { |
| 1393 | if (!ME->isArrow() && ME->getBase()->isRValue()) { |
| 1394 | Init = ME->getBase(); |
| 1395 | continue; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | break; |
| 1400 | } |
| 1401 | |
| 1402 | return Init->getType(); |
| 1403 | } |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1404 | |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 1405 | // TODO: Support adding LoopExit element to the CFG in case where the loop is |
| 1406 | // ended by ReturnStmt, GotoStmt or ThrowExpr. |
| 1407 | void CFGBuilder::addLoopExit(const Stmt *LoopStmt){ |
| 1408 | if(!BuildOpts.AddLoopExit) |
| 1409 | return; |
| 1410 | autoCreateBlock(); |
| 1411 | appendLoopExit(Block, LoopStmt); |
| 1412 | } |
| 1413 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1414 | void CFGBuilder::addAutomaticObjHandling(LocalScope::const_iterator B, |
| 1415 | LocalScope::const_iterator E, |
| 1416 | Stmt *S) { |
| 1417 | if (BuildOpts.AddImplicitDtors) |
| 1418 | addAutomaticObjDtors(B, E, S); |
| 1419 | if (BuildOpts.AddLifetime) |
| 1420 | addLifetimeEnds(B, E, S); |
| 1421 | } |
| 1422 | |
| 1423 | /// Add to current block automatic objects that leave the scope. |
| 1424 | void CFGBuilder::addLifetimeEnds(LocalScope::const_iterator B, |
| 1425 | LocalScope::const_iterator E, Stmt *S) { |
| 1426 | if (!BuildOpts.AddLifetime) |
| 1427 | return; |
| 1428 | |
| 1429 | if (B == E) |
| 1430 | return; |
| 1431 | |
| 1432 | // To go from B to E, one first goes up the scopes from B to P |
| 1433 | // then sideways in one scope from P to P' and then down |
| 1434 | // the scopes from P' to E. |
| 1435 | // The lifetime of all objects between B and P end. |
| 1436 | LocalScope::const_iterator P = B.shared_parent(E); |
| 1437 | int dist = B.distance(P); |
| 1438 | if (dist <= 0) |
| 1439 | return; |
| 1440 | |
| 1441 | // We need to perform the scope leaving in reverse order |
| 1442 | SmallVector<VarDecl *, 10> DeclsTrivial; |
| 1443 | SmallVector<VarDecl *, 10> DeclsNonTrivial; |
| 1444 | DeclsTrivial.reserve(dist); |
| 1445 | DeclsNonTrivial.reserve(dist); |
| 1446 | |
| 1447 | for (LocalScope::const_iterator I = B; I != P; ++I) |
| 1448 | if (hasTrivialDestructor(*I)) |
| 1449 | DeclsTrivial.push_back(*I); |
| 1450 | else |
| 1451 | DeclsNonTrivial.push_back(*I); |
| 1452 | |
| 1453 | autoCreateBlock(); |
| 1454 | // object with trivial destructor end their lifetime last (when storage |
| 1455 | // duration ends) |
| 1456 | for (SmallVectorImpl<VarDecl *>::reverse_iterator I = DeclsTrivial.rbegin(), |
| 1457 | E = DeclsTrivial.rend(); |
| 1458 | I != E; ++I) |
| 1459 | appendLifetimeEnds(Block, *I, S); |
| 1460 | |
| 1461 | for (SmallVectorImpl<VarDecl *>::reverse_iterator |
| 1462 | I = DeclsNonTrivial.rbegin(), |
| 1463 | E = DeclsNonTrivial.rend(); |
| 1464 | I != E; ++I) |
| 1465 | appendLifetimeEnds(Block, *I, S); |
| 1466 | } |
| 1467 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1468 | /// addAutomaticObjDtors - Add to current block automatic objects destructors |
| 1469 | /// for objects in range of local scope positions. Use S as trigger statement |
| 1470 | /// for destructors. |
Zhongxing Xu | 6d372f7 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 1471 | void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B, |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1472 | LocalScope::const_iterator E, Stmt *S) { |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1473 | if (!BuildOpts.AddImplicitDtors) |
Zhongxing Xu | 6d372f7 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 1474 | return; |
| 1475 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1476 | if (B == E) |
Zhongxing Xu | 6d372f7 | 2010-10-01 03:22:39 +0000 | [diff] [blame] | 1477 | return; |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1478 | |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1479 | // We need to append the destructors in reverse order, but any one of them |
| 1480 | // may be a no-return destructor which changes the CFG. As a result, buffer |
| 1481 | // this sequence up and replay them in reverse order when appending onto the |
| 1482 | // CFGBlock(s). |
| 1483 | SmallVector<VarDecl*, 10> Decls; |
| 1484 | Decls.reserve(B.distance(E)); |
| 1485 | for (LocalScope::const_iterator I = B; I != E; ++I) |
| 1486 | Decls.push_back(*I); |
| 1487 | |
| 1488 | for (SmallVectorImpl<VarDecl*>::reverse_iterator I = Decls.rbegin(), |
| 1489 | E = Decls.rend(); |
| 1490 | I != E; ++I) { |
| 1491 | // If this destructor is marked as a no-return destructor, we need to |
| 1492 | // create a new block for the destructor which does not have as a successor |
| 1493 | // anything built thus far: control won't flow out of this block. |
Ted Kremenek | 3d61773 | 2012-07-18 04:57:57 +0000 | [diff] [blame] | 1494 | QualType Ty = (*I)->getType(); |
| 1495 | if (Ty->isReferenceType()) { |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1496 | Ty = getReferenceInitTemporaryType(*Context, (*I)->getInit()); |
Douglas Gregor | 6c8f07f | 2011-11-15 15:29:30 +0000 | [diff] [blame] | 1497 | } |
Ted Kremenek | 3d61773 | 2012-07-18 04:57:57 +0000 | [diff] [blame] | 1498 | Ty = Context->getBaseElementType(Ty); |
| 1499 | |
Richard Trieu | 95a192a | 2015-05-28 00:14:02 +0000 | [diff] [blame] | 1500 | if (Ty->getAsCXXRecordDecl()->isAnyDestructorNoReturn()) |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 1501 | Block = createNoReturnBlock(); |
| 1502 | else |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1503 | autoCreateBlock(); |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1504 | |
| 1505 | appendAutomaticObjDtor(Block, *I, S); |
| 1506 | } |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1509 | /// addImplicitDtorsForDestructor - Add implicit destructors generated for |
| 1510 | /// base and member objects in destructor. |
| 1511 | void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 1512 | assert(BuildOpts.AddImplicitDtors && |
| 1513 | "Can be called only when dtors should be added"); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1514 | const CXXRecordDecl *RD = DD->getParent(); |
| 1515 | |
| 1516 | // At the end destroy virtual base objects. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 1517 | for (const auto &VI : RD->vbases()) { |
| 1518 | const CXXRecordDecl *CD = VI.getType()->getAsCXXRecordDecl(); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1519 | if (!CD->hasTrivialDestructor()) { |
| 1520 | autoCreateBlock(); |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 1521 | appendBaseDtor(Block, &VI); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | // Before virtual bases destroy direct base objects. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1526 | for (const auto &BI : RD->bases()) { |
| 1527 | if (!BI.isVirtual()) { |
| 1528 | const CXXRecordDecl *CD = BI.getType()->getAsCXXRecordDecl(); |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1529 | if (!CD->hasTrivialDestructor()) { |
| 1530 | autoCreateBlock(); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1531 | appendBaseDtor(Block, &BI); |
David Blaikie | 0f2ae78 | 2012-01-24 04:51:48 +0000 | [diff] [blame] | 1532 | } |
| 1533 | } |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | // First destroy member objects. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1537 | for (auto *FI : RD->fields()) { |
Marcin Swiderski | 0176990 | 2010-10-25 07:05:54 +0000 | [diff] [blame] | 1538 | // Check for constant size array. Set type to array element type. |
| 1539 | QualType QT = FI->getType(); |
| 1540 | if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) { |
| 1541 | if (AT->getSize() == 0) |
| 1542 | continue; |
| 1543 | QT = AT->getElementType(); |
| 1544 | } |
| 1545 | |
| 1546 | if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl()) |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1547 | if (!CD->hasTrivialDestructor()) { |
| 1548 | autoCreateBlock(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1549 | appendMemberDtor(Block, FI); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1554 | /// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either |
| 1555 | /// way return valid LocalScope object. |
| 1556 | LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) { |
David Blaikie | c1334cc | 2015-08-13 22:12:21 +0000 | [diff] [blame] | 1557 | if (Scope) |
| 1558 | return Scope; |
| 1559 | llvm::BumpPtrAllocator &alloc = cfg->getAllocator(); |
| 1560 | return new (alloc.Allocate<LocalScope>()) |
| 1561 | LocalScope(BumpVectorContext(alloc), ScopePos); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement |
Zhongxing Xu | 81714f2 | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 1565 | /// that should create implicit scope (e.g. if/else substatements). |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1566 | void CFGBuilder::addLocalScopeForStmt(Stmt *S) { |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1567 | if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime) |
Zhongxing Xu | 81714f2 | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 1568 | return; |
| 1569 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1570 | LocalScope *Scope = nullptr; |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1571 | |
| 1572 | // For compound statement we will be creating explicit scope. |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1573 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) { |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 1574 | for (auto *BI : CS->body()) { |
| 1575 | Stmt *SI = BI->stripLabelLikeStatements(); |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1576 | if (DeclStmt *DS = dyn_cast<DeclStmt>(SI)) |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1577 | Scope = addLocalScopeForDeclStmt(DS, Scope); |
| 1578 | } |
Zhongxing Xu | 81714f2 | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 1579 | return; |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | // For any other statement scope will be implicit and as such will be |
| 1583 | // interesting only for DeclStmt. |
Chandler Carruth | a626d64 | 2011-09-10 00:02:34 +0000 | [diff] [blame] | 1584 | if (DeclStmt *DS = dyn_cast<DeclStmt>(S->stripLabelLikeStatements())) |
Zhongxing Xu | 307701e | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 1585 | addLocalScopeForDeclStmt(DS); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will |
| 1589 | /// reuse Scope if not NULL. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1590 | LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt *DS, |
Zhongxing Xu | 307701e | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 1591 | LocalScope* Scope) { |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1592 | if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime) |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1593 | return Scope; |
| 1594 | |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 1595 | for (auto *DI : DS->decls()) |
| 1596 | if (VarDecl *VD = dyn_cast<VarDecl>(DI)) |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1597 | Scope = addLocalScopeForVarDecl(VD, Scope); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1598 | return Scope; |
| 1599 | } |
| 1600 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1601 | bool CFGBuilder::hasTrivialDestructor(VarDecl *VD) { |
| 1602 | // Check for const references bound to temporary. Set type to pointee. |
| 1603 | QualType QT = VD->getType(); |
| 1604 | if (QT.getTypePtr()->isReferenceType()) { |
| 1605 | // Attempt to determine whether this declaration lifetime-extends a |
| 1606 | // temporary. |
| 1607 | // |
| 1608 | // FIXME: This is incorrect. Non-reference declarations can lifetime-extend |
| 1609 | // temporaries, and a single declaration can extend multiple temporaries. |
| 1610 | // We should look at the storage duration on each nested |
| 1611 | // MaterializeTemporaryExpr instead. |
| 1612 | |
| 1613 | const Expr *Init = VD->getInit(); |
| 1614 | if (!Init) |
| 1615 | return true; |
| 1616 | |
| 1617 | // Lifetime-extending a temporary. |
| 1618 | bool FoundMTE = false; |
| 1619 | QT = getReferenceInitTemporaryType(*Context, Init, &FoundMTE); |
| 1620 | if (!FoundMTE) |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
| 1624 | // Check for constant size array. Set type to array element type. |
| 1625 | while (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) { |
| 1626 | if (AT->getSize() == 0) |
| 1627 | return true; |
| 1628 | QT = AT->getElementType(); |
| 1629 | } |
| 1630 | |
| 1631 | // Check if type is a C++ class with non-trivial destructor. |
| 1632 | if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl()) |
| 1633 | return !CD->hasDefinition() || CD->hasTrivialDestructor(); |
| 1634 | return true; |
| 1635 | } |
| 1636 | |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1637 | /// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will |
| 1638 | /// create add scope for automatic objects and temporary objects bound to |
| 1639 | /// const reference. Will reuse Scope if not NULL. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1640 | LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD, |
Zhongxing Xu | 307701e | 2010-10-01 03:09:09 +0000 | [diff] [blame] | 1641 | LocalScope* Scope) { |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1642 | assert(!(BuildOpts.AddImplicitDtors && BuildOpts.AddLifetime) && |
| 1643 | "AddImplicitDtors and AddLifetime cannot be used at the same time"); |
| 1644 | if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime) |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1645 | return Scope; |
| 1646 | |
| 1647 | // Check if variable is local. |
| 1648 | switch (VD->getStorageClass()) { |
| 1649 | case SC_None: |
| 1650 | case SC_Auto: |
| 1651 | case SC_Register: |
| 1652 | break; |
| 1653 | default: return Scope; |
| 1654 | } |
| 1655 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1656 | if (BuildOpts.AddImplicitDtors) { |
| 1657 | if (!hasTrivialDestructor(VD)) { |
Zhongxing Xu | 614e17d | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 1658 | // Add the variable to scope |
| 1659 | Scope = createOrReuseLocalScope(Scope); |
| 1660 | Scope->addVar(VD); |
| 1661 | ScopePos = Scope->begin(); |
| 1662 | } |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1663 | return Scope; |
| 1664 | } |
| 1665 | |
| 1666 | assert(BuildOpts.AddLifetime); |
| 1667 | // Add the variable to scope |
| 1668 | Scope = createOrReuseLocalScope(Scope); |
| 1669 | Scope->addVar(VD); |
| 1670 | ScopePos = Scope->begin(); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1671 | return Scope; |
| 1672 | } |
| 1673 | |
| 1674 | /// addLocalScopeAndDtors - For given statement add local scope for it and |
| 1675 | /// 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] | 1676 | void CFGBuilder::addLocalScopeAndDtors(Stmt *S) { |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1677 | LocalScope::const_iterator scopeBeginPos = ScopePos; |
Zhongxing Xu | 81714f2 | 2010-10-01 03:00:16 +0000 | [diff] [blame] | 1678 | addLocalScopeForStmt(S); |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1679 | addAutomaticObjHandling(ScopePos, scopeBeginPos, S); |
Marcin Swiderski | 5e41573 | 2010-09-30 23:05:00 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Marcin Swiderski | 321a707 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 1682 | /// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for |
| 1683 | /// variables with automatic storage duration to CFGBlock's elements vector. |
| 1684 | /// Elements will be prepended to physical beginning of the vector which |
| 1685 | /// happens to be logical end. Use blocks terminator as statement that specifies |
| 1686 | /// destructors call site. |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1687 | /// FIXME: This mechanism for adding automatic destructors doesn't handle |
| 1688 | /// no-return destructors properly. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1689 | void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock *Blk, |
Marcin Swiderski | 321a707 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 1690 | LocalScope::const_iterator B, LocalScope::const_iterator E) { |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1691 | if (!BuildOpts.AddImplicitDtors) |
| 1692 | return; |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 1693 | BumpVectorContext &C = cfg->getBumpVectorContext(); |
| 1694 | CFGBlock::iterator InsertPos |
| 1695 | = Blk->beginAutomaticObjDtorsInsert(Blk->end(), B.distance(E), C); |
| 1696 | for (LocalScope::const_iterator I = B; I != E; ++I) |
| 1697 | InsertPos = Blk->insertAutomaticObjDtor(InsertPos, *I, |
| 1698 | Blk->getTerminator()); |
Marcin Swiderski | 321a707 | 2010-09-30 22:54:37 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 1701 | /// prependAutomaticObjLifetimeWithTerminator - Prepend lifetime CFGElements for |
| 1702 | /// variables with automatic storage duration to CFGBlock's elements vector. |
| 1703 | /// Elements will be prepended to physical beginning of the vector which |
| 1704 | /// happens to be logical end. Use blocks terminator as statement that specifies |
| 1705 | /// where lifetime ends. |
| 1706 | void CFGBuilder::prependAutomaticObjLifetimeWithTerminator( |
| 1707 | CFGBlock *Blk, LocalScope::const_iterator B, LocalScope::const_iterator E) { |
| 1708 | if (!BuildOpts.AddLifetime) |
| 1709 | return; |
| 1710 | BumpVectorContext &C = cfg->getBumpVectorContext(); |
| 1711 | CFGBlock::iterator InsertPos = |
| 1712 | Blk->beginLifetimeEndsInsert(Blk->end(), B.distance(E), C); |
| 1713 | for (LocalScope::const_iterator I = B; I != E; ++I) |
| 1714 | InsertPos = Blk->insertLifetimeEnds(InsertPos, *I, Blk->getTerminator()); |
| 1715 | } |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 1716 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1717 | /// Visit - Walk the subtree of a statement and add extra |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1718 | /// blocks for ternary operators, &&, and ||. We also process "," and |
| 1719 | /// DeclStmts (which may contain nested control-flow). |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 1720 | CFGBlock *CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) { |
Ted Kremenek | bc1416d | 2010-04-30 22:25:53 +0000 | [diff] [blame] | 1721 | if (!S) { |
| 1722 | badCFG = true; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1723 | return nullptr; |
Ted Kremenek | bc1416d | 2010-04-30 22:25:53 +0000 | [diff] [blame] | 1724 | } |
Jordy Rose | 1734737 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 1725 | |
| 1726 | if (Expr *E = dyn_cast<Expr>(S)) |
| 1727 | S = E->IgnoreParens(); |
| 1728 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1729 | switch (S->getStmtClass()) { |
| 1730 | default: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1731 | return VisitStmt(S, asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1732 | |
| 1733 | case Stmt::AddrLabelExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1734 | return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1736 | case Stmt::BinaryConditionalOperatorClass: |
| 1737 | return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc); |
| 1738 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1739 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1740 | return VisitBinaryOperator(cast<BinaryOperator>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1742 | case Stmt::BlockExprClass: |
Devin Coughlin | b6029b7 | 2015-11-25 22:35:37 +0000 | [diff] [blame] | 1743 | return VisitBlockExpr(cast<BlockExpr>(S), asc); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1744 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1745 | case Stmt::BreakStmtClass: |
| 1746 | return VisitBreakStmt(cast<BreakStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1748 | case Stmt::CallExprClass: |
Ted Kremenek | 128d04d | 2010-08-31 18:47:34 +0000 | [diff] [blame] | 1749 | case Stmt::CXXOperatorCallExprClass: |
John McCall | c67067f | 2011-05-11 07:19:11 +0000 | [diff] [blame] | 1750 | case Stmt::CXXMemberCallExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1751 | case Stmt::UserDefinedLiteralClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1752 | return VisitCallExpr(cast<CallExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1754 | case Stmt::CaseStmtClass: |
| 1755 | return VisitCaseStmt(cast<CaseStmt>(S)); |
| 1756 | |
| 1757 | case Stmt::ChooseExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1758 | return VisitChooseExpr(cast<ChooseExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1760 | case Stmt::CompoundStmtClass: |
| 1761 | return VisitCompoundStmt(cast<CompoundStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1763 | case Stmt::ConditionalOperatorClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1764 | return VisitConditionalOperator(cast<ConditionalOperator>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1766 | case Stmt::ContinueStmtClass: |
| 1767 | return VisitContinueStmt(cast<ContinueStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1768 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 1769 | case Stmt::CXXCatchStmtClass: |
| 1770 | return VisitCXXCatchStmt(cast<CXXCatchStmt>(S)); |
| 1771 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1772 | case Stmt::ExprWithCleanupsClass: |
| 1773 | return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc); |
Ted Kremenek | 82bfc86 | 2010-08-28 00:19:02 +0000 | [diff] [blame] | 1774 | |
Jordan Rose | e5d5393 | 2012-08-23 18:10:53 +0000 | [diff] [blame] | 1775 | case Stmt::CXXDefaultArgExprClass: |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1776 | case Stmt::CXXDefaultInitExprClass: |
Jordan Rose | e5d5393 | 2012-08-23 18:10:53 +0000 | [diff] [blame] | 1777 | // FIXME: The expression inside a CXXDefaultArgExpr is owned by the |
| 1778 | // called function's declaration, not by the caller. If we simply add |
| 1779 | // this expression to the CFG, we could end up with the same Expr |
| 1780 | // appearing multiple times. |
| 1781 | // PR13385 / <rdar://problem/12156507> |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1782 | // |
| 1783 | // It's likewise possible for multiple CXXDefaultInitExprs for the same |
| 1784 | // expression to be used in the same function (through aggregate |
| 1785 | // initialization). |
Jordan Rose | e5d5393 | 2012-08-23 18:10:53 +0000 | [diff] [blame] | 1786 | return VisitStmt(S, asc); |
| 1787 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 1788 | case Stmt::CXXBindTemporaryExprClass: |
| 1789 | return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc); |
| 1790 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 1791 | case Stmt::CXXConstructExprClass: |
| 1792 | return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc); |
| 1793 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 1794 | case Stmt::CXXNewExprClass: |
| 1795 | return VisitCXXNewExpr(cast<CXXNewExpr>(S), asc); |
| 1796 | |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 1797 | case Stmt::CXXDeleteExprClass: |
| 1798 | return VisitCXXDeleteExpr(cast<CXXDeleteExpr>(S), asc); |
| 1799 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 1800 | case Stmt::CXXFunctionalCastExprClass: |
| 1801 | return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc); |
| 1802 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 1803 | case Stmt::CXXTemporaryObjectExprClass: |
| 1804 | return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc); |
| 1805 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 1806 | case Stmt::CXXThrowExprClass: |
| 1807 | return VisitCXXThrowExpr(cast<CXXThrowExpr>(S)); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1808 | |
Ted Kremenek | b27378c | 2010-01-19 20:40:33 +0000 | [diff] [blame] | 1809 | case Stmt::CXXTryStmtClass: |
| 1810 | return VisitCXXTryStmt(cast<CXXTryStmt>(S)); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 1811 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1812 | case Stmt::CXXForRangeStmtClass: |
| 1813 | return VisitCXXForRangeStmt(cast<CXXForRangeStmt>(S)); |
| 1814 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1815 | case Stmt::DeclStmtClass: |
| 1816 | return VisitDeclStmt(cast<DeclStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1818 | case Stmt::DefaultStmtClass: |
| 1819 | return VisitDefaultStmt(cast<DefaultStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1821 | case Stmt::DoStmtClass: |
| 1822 | return VisitDoStmt(cast<DoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1824 | case Stmt::ForStmtClass: |
| 1825 | return VisitForStmt(cast<ForStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1827 | case Stmt::GotoStmtClass: |
| 1828 | return VisitGotoStmt(cast<GotoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1830 | case Stmt::IfStmtClass: |
| 1831 | return VisitIfStmt(cast<IfStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1833 | case Stmt::ImplicitCastExprClass: |
| 1834 | return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 1835 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1836 | case Stmt::IndirectGotoStmtClass: |
| 1837 | return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1839 | case Stmt::LabelStmtClass: |
| 1840 | return VisitLabelStmt(cast<LabelStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Ted Kremenek | da76a94 | 2012-04-12 20:34:52 +0000 | [diff] [blame] | 1842 | case Stmt::LambdaExprClass: |
| 1843 | return VisitLambdaExpr(cast<LambdaExpr>(S), asc); |
| 1844 | |
Artem Dergachev | f43ac4c | 2018-02-24 02:00:30 +0000 | [diff] [blame^] | 1845 | case Stmt::MaterializeTemporaryExprClass: |
| 1846 | return VisitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(S), |
| 1847 | asc); |
| 1848 | |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 1849 | case Stmt::MemberExprClass: |
| 1850 | return VisitMemberExpr(cast<MemberExpr>(S), asc); |
| 1851 | |
Ted Kremenek | 0426823 | 2011-11-05 00:10:15 +0000 | [diff] [blame] | 1852 | case Stmt::NullStmtClass: |
| 1853 | return Block; |
| 1854 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1855 | case Stmt::ObjCAtCatchStmtClass: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S)); |
| 1857 | |
Ted Kremenek | 5022f1d | 2012-03-06 23:40:47 +0000 | [diff] [blame] | 1858 | case Stmt::ObjCAutoreleasePoolStmtClass: |
| 1859 | return VisitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(S)); |
| 1860 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1861 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 1862 | return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1864 | case Stmt::ObjCAtThrowStmtClass: |
| 1865 | return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1867 | case Stmt::ObjCAtTryStmtClass: |
| 1868 | return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1870 | case Stmt::ObjCForCollectionStmtClass: |
| 1871 | return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | |
Ted Kremenek | 0426823 | 2011-11-05 00:10:15 +0000 | [diff] [blame] | 1873 | case Stmt::OpaqueValueExprClass: |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1874 | return Block; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1876 | case Stmt::PseudoObjectExprClass: |
| 1877 | return VisitPseudoObjectExpr(cast<PseudoObjectExpr>(S)); |
| 1878 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1879 | case Stmt::ReturnStmtClass: |
| 1880 | return VisitReturnStmt(cast<ReturnStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | |
Nico Weber | 699670e | 2017-08-23 15:33:16 +0000 | [diff] [blame] | 1882 | case Stmt::SEHExceptStmtClass: |
| 1883 | return VisitSEHExceptStmt(cast<SEHExceptStmt>(S)); |
| 1884 | |
| 1885 | case Stmt::SEHFinallyStmtClass: |
| 1886 | return VisitSEHFinallyStmt(cast<SEHFinallyStmt>(S)); |
| 1887 | |
| 1888 | case Stmt::SEHLeaveStmtClass: |
| 1889 | return VisitSEHLeaveStmt(cast<SEHLeaveStmt>(S)); |
| 1890 | |
| 1891 | case Stmt::SEHTryStmtClass: |
| 1892 | return VisitSEHTryStmt(cast<SEHTryStmt>(S)); |
| 1893 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1894 | case Stmt::UnaryExprOrTypeTraitExprClass: |
| 1895 | return VisitUnaryExprOrTypeTraitExpr(cast<UnaryExprOrTypeTraitExpr>(S), |
| 1896 | asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1898 | case Stmt::StmtExprClass: |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1899 | return VisitStmtExpr(cast<StmtExpr>(S), asc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1901 | case Stmt::SwitchStmtClass: |
| 1902 | return VisitSwitchStmt(cast<SwitchStmt>(S)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1904 | case Stmt::UnaryOperatorClass: |
| 1905 | return VisitUnaryOperator(cast<UnaryOperator>(S), asc); |
| 1906 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1907 | case Stmt::WhileStmtClass: |
| 1908 | return VisitWhileStmt(cast<WhileStmt>(S)); |
| 1909 | } |
| 1910 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1912 | CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1913 | if (asc.alwaysAdd(*this, S)) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1914 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1915 | appendStmt(Block, S); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1916 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1918 | return VisitChildren(S); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1919 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1920 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1921 | /// VisitChildren - Visit the children of a Stmt. |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 1922 | CFGBlock *CFGBuilder::VisitChildren(Stmt *S) { |
| 1923 | CFGBlock *B = Block; |
Ted Kremenek | 828f631 | 2011-02-21 22:11:26 +0000 | [diff] [blame] | 1924 | |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 1925 | // Visit the children in their reverse order so that they appear in |
| 1926 | // left-to-right (natural) order in the CFG. |
| 1927 | reverse_children RChildren(S); |
| 1928 | for (reverse_children::iterator I = RChildren.begin(), E = RChildren.end(); |
| 1929 | I != E; ++I) { |
| 1930 | if (Stmt *Child = *I) |
| 1931 | if (CFGBlock *R = Visit(Child)) |
| 1932 | B = R; |
| 1933 | } |
| 1934 | return B; |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1935 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 1937 | CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, |
| 1938 | AddStmtChoice asc) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1939 | AddressTakenLabels.insert(A->getLabel()); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1940 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1941 | if (asc.alwaysAdd(*this, A)) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1942 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1943 | appendStmt(Block, A); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1944 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1945 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1946 | return Block; |
| 1947 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1949 | CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U, |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1950 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 1951 | if (asc.alwaysAdd(*this, U)) { |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1952 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 1953 | appendStmt(Block, U); |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 1956 | return Visit(U->getSubExpr(), AddStmtChoice()); |
Zhanyong Wan | 6dace61 | 2010-11-22 08:45:56 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1959 | CFGBlock *CFGBuilder::VisitLogicalOperator(BinaryOperator *B) { |
| 1960 | CFGBlock *ConfluenceBlock = Block ? Block : createBlock(); |
| 1961 | appendStmt(ConfluenceBlock, B); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1963 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1964 | return nullptr; |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 1965 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1966 | return VisitLogicalOperator(B, nullptr, ConfluenceBlock, |
| 1967 | ConfluenceBlock).first; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | std::pair<CFGBlock*, CFGBlock*> |
| 1971 | CFGBuilder::VisitLogicalOperator(BinaryOperator *B, |
| 1972 | Stmt *Term, |
| 1973 | CFGBlock *TrueBlock, |
| 1974 | CFGBlock *FalseBlock) { |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1975 | // Introspect the RHS. If it is a nested logical operation, we recursively |
| 1976 | // build the CFG using this function. Otherwise, resort to default |
| 1977 | // CFG construction behavior. |
| 1978 | Expr *RHS = B->getRHS()->IgnoreParens(); |
| 1979 | CFGBlock *RHSBlock, *ExitBlock; |
| 1980 | |
| 1981 | do { |
| 1982 | if (BinaryOperator *B_RHS = dyn_cast<BinaryOperator>(RHS)) |
| 1983 | if (B_RHS->isLogicalOp()) { |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1984 | std::tie(RHSBlock, ExitBlock) = |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 1985 | VisitLogicalOperator(B_RHS, Term, TrueBlock, FalseBlock); |
| 1986 | break; |
| 1987 | } |
| 1988 | |
| 1989 | // The RHS is not a nested logical operation. Don't push the terminator |
| 1990 | // down further, but instead visit RHS and construct the respective |
| 1991 | // pieces of the CFG, and link up the RHSBlock with the terminator |
| 1992 | // we have been provided. |
| 1993 | ExitBlock = RHSBlock = createBlock(false); |
| 1994 | |
Richard Trieu | 6a6af52 | 2017-01-04 00:46:30 +0000 | [diff] [blame] | 1995 | // Even though KnownVal is only used in the else branch of the next |
| 1996 | // conditional, tryEvaluateBool performs additional checking on the |
| 1997 | // Expr, so it should be called unconditionally. |
| 1998 | TryResult KnownVal = tryEvaluateBool(RHS); |
| 1999 | if (!KnownVal.isKnown()) |
| 2000 | KnownVal = tryEvaluateBool(B); |
| 2001 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2002 | if (!Term) { |
| 2003 | assert(TrueBlock == FalseBlock); |
| 2004 | addSuccessor(RHSBlock, TrueBlock); |
| 2005 | } |
| 2006 | else { |
| 2007 | RHSBlock->setTerminator(Term); |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 2008 | addSuccessor(RHSBlock, TrueBlock, !KnownVal.isFalse()); |
| 2009 | addSuccessor(RHSBlock, FalseBlock, !KnownVal.isTrue()); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | Block = RHSBlock; |
| 2013 | RHSBlock = addStmt(RHS); |
| 2014 | } |
| 2015 | while (false); |
| 2016 | |
| 2017 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2018 | return std::make_pair(nullptr, nullptr); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2019 | |
| 2020 | // Generate the blocks for evaluating the LHS. |
| 2021 | Expr *LHS = B->getLHS()->IgnoreParens(); |
| 2022 | |
| 2023 | if (BinaryOperator *B_LHS = dyn_cast<BinaryOperator>(LHS)) |
| 2024 | if (B_LHS->isLogicalOp()) { |
| 2025 | if (B->getOpcode() == BO_LOr) |
| 2026 | FalseBlock = RHSBlock; |
| 2027 | else |
| 2028 | TrueBlock = RHSBlock; |
| 2029 | |
| 2030 | // For the LHS, treat 'B' as the terminator that we want to sink |
| 2031 | // into the nested branch. The RHS always gets the top-most |
| 2032 | // terminator. |
| 2033 | return VisitLogicalOperator(B_LHS, B, TrueBlock, FalseBlock); |
| 2034 | } |
| 2035 | |
| 2036 | // Create the block evaluating the LHS. |
| 2037 | // This contains the '&&' or '||' as the terminator. |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 2038 | CFGBlock *LHSBlock = createBlock(false); |
| 2039 | LHSBlock->setTerminator(B); |
| 2040 | |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 2041 | Block = LHSBlock; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2042 | CFGBlock *EntryLHSBlock = addStmt(LHS); |
| 2043 | |
| 2044 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2045 | return std::make_pair(nullptr, nullptr); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 2046 | |
| 2047 | // See if this is a known constant. |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2048 | TryResult KnownVal = tryEvaluateBool(LHS); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Now link the LHSBlock with RHSBlock. |
| 2051 | if (B->getOpcode() == BO_LOr) { |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 2052 | addSuccessor(LHSBlock, TrueBlock, !KnownVal.isFalse()); |
| 2053 | addSuccessor(LHSBlock, RHSBlock, !KnownVal.isTrue()); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 2054 | } else { |
| 2055 | assert(B->getOpcode() == BO_LAnd); |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 2056 | addSuccessor(LHSBlock, RHSBlock, !KnownVal.isFalse()); |
| 2057 | addSuccessor(LHSBlock, FalseBlock, !KnownVal.isTrue()); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2060 | return std::make_pair(EntryLHSBlock, ExitBlock); |
Ted Kremenek | a16436f | 2012-07-14 05:04:06 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, |
| 2064 | AddStmtChoice asc) { |
| 2065 | // && or || |
| 2066 | if (B->isLogicalOp()) |
| 2067 | return VisitLogicalOperator(B); |
| 2068 | |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 2069 | if (B->getOpcode() == BO_Comma) { // , |
Ted Kremenek | fe9b768 | 2009-07-17 22:57:50 +0000 | [diff] [blame] | 2070 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2071 | appendStmt(Block, B); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2072 | addStmt(B->getRHS()); |
| 2073 | return addStmt(B->getLHS()); |
| 2074 | } |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 2075 | |
| 2076 | if (B->isAssignmentOp()) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 2077 | if (asc.alwaysAdd(*this, B)) { |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 2078 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2079 | appendStmt(Block, B); |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 2080 | } |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 2081 | Visit(B->getLHS()); |
Marcin Swiderski | 7723249 | 2010-10-24 08:21:40 +0000 | [diff] [blame] | 2082 | return Visit(B->getRHS()); |
Zhongxing Xu | 41cdf58 | 2010-06-03 06:23:18 +0000 | [diff] [blame] | 2083 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 2085 | if (asc.alwaysAdd(*this, B)) { |
Marcin Swiderski | 7723249 | 2010-10-24 08:21:40 +0000 | [diff] [blame] | 2086 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2087 | appendStmt(Block, B); |
Marcin Swiderski | 7723249 | 2010-10-24 08:21:40 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
Zhongxing Xu | d95ccd5 | 2010-10-27 03:23:10 +0000 | [diff] [blame] | 2090 | CFGBlock *RBlock = Visit(B->getRHS()); |
| 2091 | CFGBlock *LBlock = Visit(B->getLHS()); |
| 2092 | // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr |
| 2093 | // containing a DoStmt, and the LHS doesn't create a new block, then we should |
| 2094 | // return RBlock. Otherwise we'll incorrectly return NULL. |
| 2095 | return (LBlock ? LBlock : RBlock); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Ted Kremenek | e249984 | 2012-04-12 20:03:44 +0000 | [diff] [blame] | 2098 | CFGBlock *CFGBuilder::VisitNoRecurse(Expr *E, AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 2099 | if (asc.alwaysAdd(*this, E)) { |
Ted Kremenek | 470bfa4 | 2009-11-25 01:34:30 +0000 | [diff] [blame] | 2100 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2101 | appendStmt(Block, E); |
Ted Kremenek | 470bfa4 | 2009-11-25 01:34:30 +0000 | [diff] [blame] | 2102 | } |
| 2103 | return Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2106 | CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) { |
| 2107 | // "break" is a control-flow statement. Thus we stop processing the current |
| 2108 | // block. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2109 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2110 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2112 | // Now create a new block that ends with the break statement. |
| 2113 | Block = createBlock(false); |
| 2114 | Block->setTerminator(B); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2116 | // If there is no target for the break, then we are looking at an incomplete |
| 2117 | // AST. This means that the CFG cannot be constructed. |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 2118 | if (BreakJumpTarget.block) { |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 2119 | addAutomaticObjHandling(ScopePos, BreakJumpTarget.scopePosition, B); |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 2120 | addSuccessor(Block, BreakJumpTarget.block); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2121 | } else |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2122 | badCFG = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2124 | return Block; |
| 2125 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2127 | static bool CanThrow(Expr *E, ASTContext &Ctx) { |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2128 | QualType Ty = E->getType(); |
| 2129 | if (Ty->isFunctionPointerType()) |
| 2130 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
| 2131 | else if (Ty->isBlockPointerType()) |
| 2132 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2133 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2134 | const FunctionType *FT = Ty->getAs<FunctionType>(); |
| 2135 | if (FT) { |
| 2136 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2137 | if (!isUnresolvedExceptionSpec(Proto->getExceptionSpecType()) && |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2138 | Proto->isNothrow(Ctx)) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2139 | return false; |
| 2140 | } |
| 2141 | return true; |
| 2142 | } |
| 2143 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2144 | CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { |
John McCall | c67067f | 2011-05-11 07:19:11 +0000 | [diff] [blame] | 2145 | // Compute the callee type. |
| 2146 | QualType calleeType = C->getCallee()->getType(); |
| 2147 | if (calleeType == Context->BoundMemberTy) { |
| 2148 | QualType boundType = Expr::findBoundMemberType(C->getCallee()); |
| 2149 | |
| 2150 | // We should only get a null bound type if processing a dependent |
| 2151 | // CFG. Recover by assuming nothing. |
| 2152 | if (!boundType.isNull()) calleeType = boundType; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2153 | } |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 2154 | |
John McCall | c67067f | 2011-05-11 07:19:11 +0000 | [diff] [blame] | 2155 | // If this is a call to a no-return function, this stops the block here. |
| 2156 | bool NoReturn = getFunctionExtInfo(*calleeType).getNoReturn(); |
| 2157 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2158 | bool AddEHEdge = false; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Languages without exceptions are assumed to not throw. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2161 | if (Context->getLangOpts().Exceptions) { |
Ted Kremenek | e97b1eb | 2010-09-14 23:41:16 +0000 | [diff] [blame] | 2162 | if (BuildOpts.AddEHEdges) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2163 | AddEHEdge = true; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 2166 | // If this is a call to a builtin function, it might not actually evaluate |
| 2167 | // its arguments. Don't add them to the CFG if this is the case. |
| 2168 | bool OmitArguments = false; |
| 2169 | |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2170 | if (FunctionDecl *FD = C->getDirectCallee()) { |
Nico Weber | 758fbac | 2018-02-13 21:31:47 +0000 | [diff] [blame] | 2171 | if (FD->isNoReturn() || C->isBuiltinAssumeFalse(*Context)) |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 2172 | NoReturn = true; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2173 | if (FD->hasAttr<NoThrowAttr>()) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2174 | AddEHEdge = false; |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 2175 | if (FD->getBuiltinID() == Builtin::BI__builtin_object_size) |
| 2176 | OmitArguments = true; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2177 | } |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 2178 | |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2179 | if (!CanThrow(C->getCallee(), *Context)) |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2180 | AddEHEdge = false; |
| 2181 | |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 2182 | if (OmitArguments) { |
| 2183 | assert(!NoReturn && "noreturn calls with unevaluated args not implemented"); |
| 2184 | assert(!AddEHEdge && "EH calls with unevaluated args not implemented"); |
| 2185 | autoCreateBlock(); |
| 2186 | appendStmt(Block, C); |
| 2187 | return Visit(C->getCallee()); |
| 2188 | } |
| 2189 | |
| 2190 | if (!NoReturn && !AddEHEdge) { |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 2191 | return VisitStmt(C, asc.withAlwaysAdd(true)); |
Jordan Rose | 5374c07 | 2013-08-19 16:27:28 +0000 | [diff] [blame] | 2192 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2193 | |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2194 | if (Block) { |
| 2195 | Succ = Block; |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2196 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2197 | return nullptr; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2198 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 2200 | if (NoReturn) |
| 2201 | Block = createNoReturnBlock(); |
| 2202 | else |
| 2203 | Block = createBlock(); |
| 2204 | |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2205 | appendStmt(Block, C); |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 2206 | |
Mike Stump | 04c6851 | 2010-01-21 15:20:48 +0000 | [diff] [blame] | 2207 | if (AddEHEdge) { |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2208 | // Add exceptional edges. |
| 2209 | if (TryTerminatedBlock) |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2210 | addSuccessor(Block, TryTerminatedBlock); |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2211 | else |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2212 | addSuccessor(Block, &cfg->getExit()); |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2213 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | |
Mike Stump | 8c5d799 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 2215 | return VisitChildren(C); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2216 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2217 | |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2218 | CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C, |
| 2219 | AddStmtChoice asc) { |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2220 | CFGBlock *ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2221 | appendStmt(ConfluenceBlock, C); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2222 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2223 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 2225 | AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 2226 | Succ = ConfluenceBlock; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2227 | Block = nullptr; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2228 | CFGBlock *LHSBlock = Visit(C->getLHS(), alwaysAdd); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2229 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2230 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 2232 | Succ = ConfluenceBlock; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2233 | Block = nullptr; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2234 | CFGBlock *RHSBlock = Visit(C->getRHS(), alwaysAdd); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2235 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2236 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 2238 | Block = createBlock(false); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2239 | // See if this is a known constant. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2240 | const TryResult& KnownVal = tryEvaluateBool(C->getCond()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2241 | addSuccessor(Block, KnownVal.isFalse() ? nullptr : LHSBlock); |
| 2242 | addSuccessor(Block, KnownVal.isTrue() ? nullptr : RHSBlock); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 2243 | Block->setTerminator(C); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | return addStmt(C->getCond()); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 2245 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2247 | CFGBlock *CFGBuilder::VisitCompoundStmt(CompoundStmt *C) { |
Matthias Gehre | 09a134e | 2015-11-14 00:36:50 +0000 | [diff] [blame] | 2248 | LocalScope::const_iterator scopeBeginPos = ScopePos; |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 2249 | addLocalScopeForStmt(C); |
| 2250 | |
Matthias Gehre | 09a134e | 2015-11-14 00:36:50 +0000 | [diff] [blame] | 2251 | if (!C->body_empty() && !isa<ReturnStmt>(*C->body_rbegin())) { |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 2252 | // If the body ends with a ReturnStmt, the dtors will be added in |
| 2253 | // VisitReturnStmt. |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 2254 | addAutomaticObjHandling(ScopePos, scopeBeginPos, C); |
Matthias Gehre | 09a134e | 2015-11-14 00:36:50 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2257 | CFGBlock *LastBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2258 | |
| 2259 | for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); |
| 2260 | I != E; ++I ) { |
Ted Kremenek | 4f2ab5a | 2010-08-17 21:00:06 +0000 | [diff] [blame] | 2261 | // If we hit a segment of code just containing ';' (NullStmts), we can |
| 2262 | // get a null block back. In such cases, just use the LastBlock |
| 2263 | if (CFGBlock *newBlock = addStmt(*I)) |
| 2264 | LastBlock = newBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | |
Ted Kremenek | ce499c2 | 2009-08-27 23:16:26 +0000 | [diff] [blame] | 2266 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2267 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | } |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 2269 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2270 | return LastBlock; |
| 2271 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2273 | CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C, |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2274 | AddStmtChoice asc) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2275 | const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2276 | const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : nullptr); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2277 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2278 | // Create the confluence block that will "merge" the results of the ternary |
| 2279 | // expression. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2280 | CFGBlock *ConfluenceBlock = Block ? Block : createBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2281 | appendStmt(ConfluenceBlock, C); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2282 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2283 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 2285 | AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 2286 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2287 | // Create a block for the LHS expression if there is an LHS expression. A |
| 2288 | // GCC extension allows LHS to be NULL, causing the condition to be the |
| 2289 | // value that is returned instead. |
| 2290 | // e.g: x ?: y is shorthand for: x ? x : y; |
| 2291 | Succ = ConfluenceBlock; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2292 | Block = nullptr; |
| 2293 | CFGBlock *LHSBlock = nullptr; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2294 | const Expr *trueExpr = C->getTrueExpr(); |
| 2295 | if (trueExpr != opaqueValue) { |
| 2296 | LHSBlock = Visit(C->getTrueExpr(), alwaysAdd); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2297 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2298 | return nullptr; |
| 2299 | Block = nullptr; |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2300 | } |
Ted Kremenek | d813801 | 2011-02-24 03:09:15 +0000 | [diff] [blame] | 2301 | else |
| 2302 | LHSBlock = ConfluenceBlock; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2304 | // Create the block for the RHS expression. |
| 2305 | Succ = ConfluenceBlock; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2306 | CFGBlock *RHSBlock = Visit(C->getFalseExpr(), alwaysAdd); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2307 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2308 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2309 | |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 2310 | // If the condition is a logical '&&' or '||', build a more accurate CFG. |
| 2311 | if (BinaryOperator *Cond = |
| 2312 | dyn_cast<BinaryOperator>(C->getCond()->IgnoreParens())) |
| 2313 | if (Cond->isLogicalOp()) |
| 2314 | return VisitLogicalOperator(Cond, C, LHSBlock, RHSBlock).first; |
| 2315 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2316 | // Create the block that will contain the condition. |
| 2317 | Block = createBlock(false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2319 | // See if this is a known constant. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2320 | const TryResult& KnownVal = tryEvaluateBool(C->getCond()); |
Ted Kremenek | 5a09527 | 2014-03-04 21:53:26 +0000 | [diff] [blame] | 2321 | addSuccessor(Block, LHSBlock, !KnownVal.isFalse()); |
| 2322 | addSuccessor(Block, RHSBlock, !KnownVal.isTrue()); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2323 | Block->setTerminator(C); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2324 | Expr *condExpr = C->getCond(); |
John McCall | 68cc335 | 2011-02-19 03:13:26 +0000 | [diff] [blame] | 2325 | |
Ted Kremenek | d813801 | 2011-02-24 03:09:15 +0000 | [diff] [blame] | 2326 | if (opaqueValue) { |
| 2327 | // Run the condition expression if it's not trivially expressed in |
| 2328 | // terms of the opaque value (or if there is no opaque value). |
| 2329 | if (condExpr != opaqueValue) |
| 2330 | addStmt(condExpr); |
John McCall | 68cc335 | 2011-02-19 03:13:26 +0000 | [diff] [blame] | 2331 | |
Ted Kremenek | d813801 | 2011-02-24 03:09:15 +0000 | [diff] [blame] | 2332 | // Before that, run the common subexpression if there was one. |
| 2333 | // At least one of this or the above will be run. |
| 2334 | return addStmt(BCO->getCommon()); |
| 2335 | } |
| 2336 | |
| 2337 | return addStmt(condExpr); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2340 | CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) { |
Ted Kremenek | 6878c36 | 2011-05-10 18:42:15 +0000 | [diff] [blame] | 2341 | // Check if the Decl is for an __label__. If so, elide it from the |
| 2342 | // CFG entirely. |
| 2343 | if (isa<LabelDecl>(*DS->decl_begin())) |
| 2344 | return Block; |
| 2345 | |
Ted Kremenek | 3a60114 | 2011-05-24 20:41:31 +0000 | [diff] [blame] | 2346 | // This case also handles static_asserts. |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2347 | if (DS->isSingleDecl()) |
| 2348 | return VisitDeclSubExpr(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2349 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2350 | CFGBlock *B = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | |
Jordan Rose | 8c6c8a9 | 2012-07-20 18:50:48 +0000 | [diff] [blame] | 2352 | // Build an individual DeclStmt for each decl. |
| 2353 | for (DeclStmt::reverse_decl_iterator I = DS->decl_rbegin(), |
| 2354 | E = DS->decl_rend(); |
| 2355 | I != E; ++I) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2356 | // Get the alignment of the new DeclStmt, padding out to >=8 bytes. |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 2357 | unsigned A = alignof(DeclStmt) < 8 ? 8 : alignof(DeclStmt); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2358 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2359 | // Allocate the DeclStmt using the BumpPtrAllocator. It will get |
| 2360 | // automatically freed with the CFG. |
| 2361 | DeclGroupRef DG(*I); |
| 2362 | Decl *D = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2363 | void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2364 | DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D)); |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 2365 | cfg->addSyntheticDeclStmt(DSNew, DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2367 | // Append the fake DeclStmt to block. |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2368 | B = VisitDeclSubExpr(DSNew); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2369 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | |
| 2371 | return B; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2372 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2374 | /// VisitDeclSubExpr - Utility method to add block-level expressions for |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2375 | /// DeclStmts and initializers in them. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2376 | CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt *DS) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2377 | assert(DS->isSingleDecl() && "Can handle single declarations only."); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2378 | VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2380 | if (!VD) { |
Jordan Rose | 5250b87 | 2013-06-03 22:59:41 +0000 | [diff] [blame] | 2381 | // Of everything that can be declared in a DeclStmt, only VarDecls impact |
| 2382 | // runtime semantics. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2383 | return Block; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2384 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2386 | bool HasTemporaries = false; |
| 2387 | |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 2388 | // Guard static initializers under a branch. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2389 | CFGBlock *blockAfterStaticInit = nullptr; |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2390 | |
| 2391 | if (BuildOpts.AddStaticInitBranches && VD->isStaticLocal()) { |
| 2392 | // For static variables, we need to create a branch to track |
| 2393 | // whether or not they are initialized. |
| 2394 | if (Block) { |
| 2395 | Succ = Block; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2396 | Block = nullptr; |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2397 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2398 | return nullptr; |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2399 | } |
| 2400 | blockAfterStaticInit = Succ; |
| 2401 | } |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 2402 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2403 | // Destructors of temporaries in initialization expression should be called |
| 2404 | // after initialization finishes. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2405 | Expr *Init = VD->getInit(); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2406 | if (Init) { |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 2407 | HasTemporaries = isa<ExprWithCleanups>(Init); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2408 | |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 2409 | if (BuildOpts.AddTemporaryDtors && HasTemporaries) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2410 | // Generate destructors for temporaries in initialization expression. |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 2411 | TempDtorContext Context; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 2412 | VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(), |
| 2413 | /*BindToTemporary=*/false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | autoCreateBlock(); |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 2418 | appendStmt(Block, DS); |
Artem Dergachev | 5fc1033 | 2018-02-10 01:55:23 +0000 | [diff] [blame] | 2419 | |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 2420 | findConstructionContexts( |
| 2421 | ConstructionContext::create(cfg->getBumpVectorContext(), DS), |
| 2422 | Init); |
Artem Dergachev | 5fc1033 | 2018-02-10 01:55:23 +0000 | [diff] [blame] | 2423 | |
Ted Kremenek | 213d053 | 2012-03-22 05:57:43 +0000 | [diff] [blame] | 2424 | // Keep track of the last non-null block, as 'Block' can be nulled out |
| 2425 | // if the initializer expression is something like a 'while' in a |
| 2426 | // statement-expression. |
| 2427 | CFGBlock *LastBlock = Block; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2429 | if (Init) { |
Ted Kremenek | 213d053 | 2012-03-22 05:57:43 +0000 | [diff] [blame] | 2430 | if (HasTemporaries) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 2431 | // For expression with temporaries go directly to subexpression to omit |
| 2432 | // generating destructors for the second time. |
Ted Kremenek | 213d053 | 2012-03-22 05:57:43 +0000 | [diff] [blame] | 2433 | ExprWithCleanups *EC = cast<ExprWithCleanups>(Init); |
| 2434 | if (CFGBlock *newBlock = Visit(EC->getSubExpr())) |
| 2435 | LastBlock = newBlock; |
| 2436 | } |
| 2437 | else { |
| 2438 | if (CFGBlock *newBlock = Visit(Init)) |
| 2439 | LastBlock = newBlock; |
| 2440 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2441 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2442 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2443 | // 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] | 2444 | for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2445 | VA != nullptr; VA = FindVA(VA->getElementType().getTypePtr())) { |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 2446 | if (CFGBlock *newBlock = addStmt(VA->getSizeExpr())) |
| 2447 | LastBlock = newBlock; |
| 2448 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2449 | |
Marcin Swiderski | 667ffec | 2010-10-01 00:23:17 +0000 | [diff] [blame] | 2450 | // Remove variable from local scope. |
| 2451 | if (ScopePos && VD == *ScopePos) |
| 2452 | ++ScopePos; |
| 2453 | |
Ted Kremenek | 338c3aa | 2013-03-29 00:09:28 +0000 | [diff] [blame] | 2454 | CFGBlock *B = LastBlock; |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2455 | if (blockAfterStaticInit) { |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 2456 | Succ = B; |
Ted Kremenek | 338c3aa | 2013-03-29 00:09:28 +0000 | [diff] [blame] | 2457 | Block = createBlock(false); |
| 2458 | Block->setTerminator(DS); |
Ted Kremenek | f82d578 | 2013-03-29 00:42:56 +0000 | [diff] [blame] | 2459 | addSuccessor(Block, blockAfterStaticInit); |
Ted Kremenek | 338c3aa | 2013-03-29 00:09:28 +0000 | [diff] [blame] | 2460 | addSuccessor(Block, B); |
| 2461 | B = Block; |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | return B; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2467 | CFGBlock *CFGBuilder::VisitIfStmt(IfStmt *I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2468 | // We may see an if statement in the middle of a basic block, or it may be the |
| 2469 | // first statement we are processing. In either case, we create a new basic |
| 2470 | // block. First, we create the blocks for the then...else statements, and |
| 2471 | // 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] | 2472 | // middle of a block, we stop processing that block. That block is then the |
| 2473 | // implicit successor for the "then" and "else" clauses. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2474 | |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2475 | // Save local scope position because in case of condition variable ScopePos |
| 2476 | // won't be restored when traversing AST. |
| 2477 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 2478 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 2479 | // Create local scope for C++17 if init-stmt if one exists. |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2480 | if (Stmt *Init = I->getInit()) |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 2481 | addLocalScopeForStmt(Init); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 2482 | |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2483 | // Create local scope for possible condition variable. |
| 2484 | // Store scope position. Add implicit destructor. |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2485 | if (VarDecl *VD = I->getConditionVariable()) |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2486 | addLocalScopeForVarDecl(VD); |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2487 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 2488 | addAutomaticObjHandling(ScopePos, save_scope_pos.get(), I); |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2489 | |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2490 | // The block we were processing is now finished. Make it the successor |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2491 | // block. |
| 2492 | if (Block) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2493 | Succ = Block; |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2494 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2495 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2496 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2497 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 2498 | // Process the false branch. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2499 | CFGBlock *ElseBlock = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2500 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2501 | if (Stmt *Else = I->getElse()) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2502 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2503 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2504 | // NULL out Block so that the recursive call to Visit will |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2505 | // create a new basic block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2506 | Block = nullptr; |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2507 | |
| 2508 | // If branch is not a compound statement create implicit scope |
| 2509 | // and add destructors. |
| 2510 | if (!isa<CompoundStmt>(Else)) |
| 2511 | addLocalScopeAndDtors(Else); |
| 2512 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2513 | ElseBlock = addStmt(Else); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2514 | |
Ted Kremenek | bbad8ce | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 2515 | if (!ElseBlock) // Can occur when the Else body has all NullStmts. |
| 2516 | ElseBlock = sv.get(); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2517 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2518 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2519 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 2520 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2521 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2522 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 2523 | // Process the true branch. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2524 | CFGBlock *ThenBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2525 | { |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2526 | Stmt *Then = I->getThen(); |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 2527 | assert(Then); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2528 | SaveAndRestore<CFGBlock*> sv(Succ); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2529 | Block = nullptr; |
Marcin Swiderski | f883ade | 2010-10-01 00:52:17 +0000 | [diff] [blame] | 2530 | |
| 2531 | // If branch is not a compound statement create implicit scope |
| 2532 | // and add destructors. |
| 2533 | if (!isa<CompoundStmt>(Then)) |
| 2534 | addLocalScopeAndDtors(Then); |
| 2535 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2536 | ThenBlock = addStmt(Then); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2537 | |
Ted Kremenek | 1b37951 | 2009-04-01 03:52:47 +0000 | [diff] [blame] | 2538 | if (!ThenBlock) { |
| 2539 | // We can reach here if the "then" body has all NullStmts. |
| 2540 | // Create an empty block so we can distinguish between true and false |
| 2541 | // branches in path-sensitive analyses. |
| 2542 | ThenBlock = createBlock(false); |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 2543 | addSuccessor(ThenBlock, sv.get()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2544 | } else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2545 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2546 | return nullptr; |
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 | } |
| 2549 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2550 | // Specially handle "if (expr1 || ...)" and "if (expr1 && ...)" by |
| 2551 | // having these handle the actual control-flow jump. Note that |
| 2552 | // if we introduce a condition variable, e.g. "if (int x = exp1 || exp2)" |
| 2553 | // we resort to the old control-flow behavior. This special handling |
| 2554 | // removes infeasible paths from the control-flow graph by having the |
| 2555 | // control-flow transfer of '&&' or '||' go directly into the then/else |
| 2556 | // blocks directly. |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2557 | BinaryOperator *Cond = |
| 2558 | I->getConditionVariable() |
| 2559 | ? nullptr |
| 2560 | : dyn_cast<BinaryOperator>(I->getCond()->IgnoreParens()); |
| 2561 | CFGBlock *LastBlock; |
| 2562 | if (Cond && Cond->isLogicalOp()) |
| 2563 | LastBlock = VisitLogicalOperator(Cond, I, ThenBlock, ElseBlock).first; |
| 2564 | else { |
| 2565 | // Now create a new block containing the if statement. |
| 2566 | Block = createBlock(false); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2567 | |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2568 | // Set the terminator of the new block to the If statement. |
| 2569 | Block->setTerminator(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2570 | |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2571 | // See if this is a known constant. |
| 2572 | const TryResult &KnownVal = tryEvaluateBool(I->getCond()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2573 | |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2574 | // Add the successors. If we know that specific branches are |
| 2575 | // unreachable, inform addSuccessor() of that knowledge. |
| 2576 | addSuccessor(Block, ThenBlock, /* isReachable = */ !KnownVal.isFalse()); |
| 2577 | addSuccessor(Block, ElseBlock, /* isReachable = */ !KnownVal.isTrue()); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2578 | |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2579 | // Add the condition as the last statement in the new block. This may |
| 2580 | // create new blocks as the condition may contain control-flow. Any newly |
| 2581 | // created blocks will be pointed to be "Block". |
| 2582 | LastBlock = addStmt(I->getCond()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2583 | |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 2584 | // If the IfStmt contains a condition variable, add it and its |
| 2585 | // initializer to the CFG. |
| 2586 | if (const DeclStmt* DS = I->getConditionVariableDeclStmt()) { |
| 2587 | autoCreateBlock(); |
| 2588 | LastBlock = addStmt(const_cast<DeclStmt *>(DS)); |
| 2589 | } |
Ted Kremenek | a7bcbde | 2009-12-23 04:49:01 +0000 | [diff] [blame] | 2590 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 2591 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 2592 | // Finally, if the IfStmt contains a C++17 init-stmt, add it to the CFG. |
| 2593 | if (Stmt *Init = I->getInit()) { |
| 2594 | autoCreateBlock(); |
| 2595 | LastBlock = addStmt(Init); |
| 2596 | } |
| 2597 | |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 2598 | return LastBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2599 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2600 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2601 | CFGBlock *CFGBuilder::VisitReturnStmt(ReturnStmt *R) { |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 2602 | // 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] | 2603 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2604 | // NOTE: If a "return" appears in the middle of a block, this means that the |
| 2605 | // code afterwards is DEAD (unreachable). We still keep a basic block |
| 2606 | // for that code; a simple "mark-and-sweep" from the entry block will be |
| 2607 | // able to report such dead blocks. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2608 | |
| 2609 | // Create the new block. |
| 2610 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2611 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 2612 | addAutomaticObjHandling(ScopePos, LocalScope::const_iterator(), R); |
Pavel Labath | 921e765 | 2013-09-06 08:12:48 +0000 | [diff] [blame] | 2613 | |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 2614 | findConstructionContexts( |
| 2615 | ConstructionContext::create(cfg->getBumpVectorContext(), R), |
| 2616 | R->getRetValue()); |
Artem Dergachev | 9ac2e11 | 2018-02-12 22:36:36 +0000 | [diff] [blame] | 2617 | |
Pavel Labath | 921e765 | 2013-09-06 08:12:48 +0000 | [diff] [blame] | 2618 | // If the one of the destructors does not return, we already have the Exit |
| 2619 | // block as a successor. |
| 2620 | if (!Block->hasNoReturnElement()) |
| 2621 | addSuccessor(Block, &cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2622 | |
| 2623 | // Add the return statement to the block. This may create new blocks if R |
| 2624 | // contains control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 2625 | return VisitStmt(R, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Nico Weber | 699670e | 2017-08-23 15:33:16 +0000 | [diff] [blame] | 2628 | CFGBlock *CFGBuilder::VisitSEHExceptStmt(SEHExceptStmt *ES) { |
| 2629 | // SEHExceptStmt are treated like labels, so they are the first statement in a |
| 2630 | // block. |
| 2631 | |
| 2632 | // Save local scope position because in case of exception variable ScopePos |
| 2633 | // won't be restored when traversing AST. |
| 2634 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 2635 | |
| 2636 | addStmt(ES->getBlock()); |
| 2637 | CFGBlock *SEHExceptBlock = Block; |
| 2638 | if (!SEHExceptBlock) |
| 2639 | SEHExceptBlock = createBlock(); |
| 2640 | |
| 2641 | appendStmt(SEHExceptBlock, ES); |
| 2642 | |
| 2643 | // Also add the SEHExceptBlock as a label, like with regular labels. |
| 2644 | SEHExceptBlock->setLabel(ES); |
| 2645 | |
| 2646 | // Bail out if the CFG is bad. |
| 2647 | if (badCFG) |
| 2648 | return nullptr; |
| 2649 | |
| 2650 | // We set Block to NULL to allow lazy creation of a new block (if necessary). |
| 2651 | Block = nullptr; |
| 2652 | |
| 2653 | return SEHExceptBlock; |
| 2654 | } |
| 2655 | |
| 2656 | CFGBlock *CFGBuilder::VisitSEHFinallyStmt(SEHFinallyStmt *FS) { |
| 2657 | return VisitCompoundStmt(FS->getBlock()); |
| 2658 | } |
| 2659 | |
| 2660 | CFGBlock *CFGBuilder::VisitSEHLeaveStmt(SEHLeaveStmt *LS) { |
| 2661 | // "__leave" is a control-flow statement. Thus we stop processing the current |
| 2662 | // block. |
| 2663 | if (badCFG) |
| 2664 | return nullptr; |
| 2665 | |
| 2666 | // Now create a new block that ends with the __leave statement. |
| 2667 | Block = createBlock(false); |
| 2668 | Block->setTerminator(LS); |
| 2669 | |
| 2670 | // If there is no target for the __leave, then we are looking at an incomplete |
| 2671 | // AST. This means that the CFG cannot be constructed. |
| 2672 | if (SEHLeaveJumpTarget.block) { |
| 2673 | addAutomaticObjHandling(ScopePos, SEHLeaveJumpTarget.scopePosition, LS); |
| 2674 | addSuccessor(Block, SEHLeaveJumpTarget.block); |
| 2675 | } else |
| 2676 | badCFG = true; |
| 2677 | |
| 2678 | return Block; |
| 2679 | } |
| 2680 | |
| 2681 | CFGBlock *CFGBuilder::VisitSEHTryStmt(SEHTryStmt *Terminator) { |
| 2682 | // "__try"/"__except"/"__finally" is a control-flow statement. Thus we stop |
| 2683 | // processing the current block. |
| 2684 | CFGBlock *SEHTrySuccessor = nullptr; |
| 2685 | |
| 2686 | if (Block) { |
| 2687 | if (badCFG) |
| 2688 | return nullptr; |
| 2689 | SEHTrySuccessor = Block; |
| 2690 | } else SEHTrySuccessor = Succ; |
| 2691 | |
| 2692 | // FIXME: Implement __finally support. |
| 2693 | if (Terminator->getFinallyHandler()) |
| 2694 | return NYS(); |
| 2695 | |
| 2696 | CFGBlock *PrevSEHTryTerminatedBlock = TryTerminatedBlock; |
| 2697 | |
| 2698 | // Create a new block that will contain the __try statement. |
| 2699 | CFGBlock *NewTryTerminatedBlock = createBlock(false); |
| 2700 | |
| 2701 | // Add the terminator in the __try block. |
| 2702 | NewTryTerminatedBlock->setTerminator(Terminator); |
| 2703 | |
| 2704 | if (SEHExceptStmt *Except = Terminator->getExceptHandler()) { |
| 2705 | // The code after the try is the implicit successor if there's an __except. |
| 2706 | Succ = SEHTrySuccessor; |
| 2707 | Block = nullptr; |
| 2708 | CFGBlock *ExceptBlock = VisitSEHExceptStmt(Except); |
| 2709 | if (!ExceptBlock) |
| 2710 | return nullptr; |
| 2711 | // Add this block to the list of successors for the block with the try |
| 2712 | // statement. |
| 2713 | addSuccessor(NewTryTerminatedBlock, ExceptBlock); |
| 2714 | } |
| 2715 | if (PrevSEHTryTerminatedBlock) |
| 2716 | addSuccessor(NewTryTerminatedBlock, PrevSEHTryTerminatedBlock); |
| 2717 | else |
| 2718 | addSuccessor(NewTryTerminatedBlock, &cfg->getExit()); |
| 2719 | |
| 2720 | // The code after the try is the implicit successor. |
| 2721 | Succ = SEHTrySuccessor; |
| 2722 | |
| 2723 | // Save the current "__try" context. |
| 2724 | SaveAndRestore<CFGBlock *> save_try(TryTerminatedBlock, |
| 2725 | NewTryTerminatedBlock); |
| 2726 | cfg->addTryDispatchBlock(TryTerminatedBlock); |
| 2727 | |
| 2728 | // Save the current value for the __leave target. |
| 2729 | // All __leaves should go to the code following the __try |
| 2730 | // (FIXME: or if the __try has a __finally, to the __finally.) |
| 2731 | SaveAndRestore<JumpTarget> save_break(SEHLeaveJumpTarget); |
| 2732 | SEHLeaveJumpTarget = JumpTarget(SEHTrySuccessor, ScopePos); |
| 2733 | |
| 2734 | assert(Terminator->getTryBlock() && "__try must contain a non-NULL body"); |
| 2735 | Block = nullptr; |
| 2736 | return addStmt(Terminator->getTryBlock()); |
| 2737 | } |
| 2738 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2739 | CFGBlock *CFGBuilder::VisitLabelStmt(LabelStmt *L) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2740 | // Get the block of the labeled statement. Add it to our map. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2741 | addStmt(L->getSubStmt()); |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2742 | CFGBlock *LabelBlock = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2743 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2744 | if (!LabelBlock) // This can happen when the body is empty, i.e. |
| 2745 | LabelBlock = createBlock(); // scopes that only contains NullStmts. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2746 | |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2747 | assert(LabelMap.find(L->getDecl()) == LabelMap.end() && |
| 2748 | "label already in map"); |
| 2749 | LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2750 | |
| 2751 | // Labels partition blocks, so this is the end of the basic block we were |
| 2752 | // processing (L is the block's label). Because this is label (and we have |
| 2753 | // already processed the substatement) there is no extra control-flow to worry |
| 2754 | // about. |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 2755 | LabelBlock->setLabel(L); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2756 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2757 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2758 | |
| 2759 | // 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] | 2760 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2761 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2762 | // This block is now the implicit successor of other blocks. |
| 2763 | Succ = LabelBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2764 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2765 | return LabelBlock; |
| 2766 | } |
| 2767 | |
Devin Coughlin | b6029b7 | 2015-11-25 22:35:37 +0000 | [diff] [blame] | 2768 | CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) { |
| 2769 | CFGBlock *LastBlock = VisitNoRecurse(E, asc); |
| 2770 | for (const BlockDecl::Capture &CI : E->getBlockDecl()->captures()) { |
| 2771 | if (Expr *CopyExpr = CI.getCopyExpr()) { |
| 2772 | CFGBlock *Tmp = Visit(CopyExpr); |
| 2773 | if (Tmp) |
| 2774 | LastBlock = Tmp; |
| 2775 | } |
| 2776 | } |
| 2777 | return LastBlock; |
| 2778 | } |
| 2779 | |
Ted Kremenek | da76a94 | 2012-04-12 20:34:52 +0000 | [diff] [blame] | 2780 | CFGBlock *CFGBuilder::VisitLambdaExpr(LambdaExpr *E, AddStmtChoice asc) { |
| 2781 | CFGBlock *LastBlock = VisitNoRecurse(E, asc); |
| 2782 | for (LambdaExpr::capture_init_iterator it = E->capture_init_begin(), |
| 2783 | et = E->capture_init_end(); it != et; ++it) { |
| 2784 | if (Expr *Init = *it) { |
| 2785 | CFGBlock *Tmp = Visit(Init); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2786 | if (Tmp) |
Ted Kremenek | da76a94 | 2012-04-12 20:34:52 +0000 | [diff] [blame] | 2787 | LastBlock = Tmp; |
| 2788 | } |
| 2789 | } |
| 2790 | return LastBlock; |
| 2791 | } |
| 2792 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2793 | CFGBlock *CFGBuilder::VisitGotoStmt(GotoStmt *G) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2794 | // Goto is a control-flow statement. Thus we stop processing the current |
| 2795 | // block and create a new one. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2796 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2797 | Block = createBlock(false); |
| 2798 | Block->setTerminator(G); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2799 | |
| 2800 | // 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] | 2801 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2802 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2803 | if (I == LabelMap.end()) |
| 2804 | // We will need to backpatch this block later. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2805 | BackpatchBlocks.push_back(JumpSource(Block, ScopePos)); |
| 2806 | else { |
| 2807 | JumpTarget JT = I->second; |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 2808 | addAutomaticObjHandling(ScopePos, JT.scopePosition, G); |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 2809 | addSuccessor(Block, JT.block); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2810 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2811 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2812 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2815 | CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2816 | CFGBlock *LoopSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2817 | |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2818 | // Save local scope position because in case of condition variable ScopePos |
| 2819 | // won't be restored when traversing AST. |
| 2820 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 2821 | |
| 2822 | // Create local scope for init statement and possible condition variable. |
| 2823 | // Add destructor for init statement and condition variable. |
| 2824 | // Store scope position for continue statement. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2825 | if (Stmt *Init = F->getInit()) |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2826 | addLocalScopeForStmt(Init); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2827 | LocalScope::const_iterator LoopBeginScopePos = ScopePos; |
| 2828 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2829 | if (VarDecl *VD = F->getConditionVariable()) |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2830 | addLocalScopeForVarDecl(VD); |
| 2831 | LocalScope::const_iterator ContinueScopePos = ScopePos; |
| 2832 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 2833 | addAutomaticObjHandling(ScopePos, save_scope_pos.get(), F); |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2834 | |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 2835 | addLoopExit(F); |
| 2836 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 2837 | // "for" is a control-flow statement. Thus we stop processing the current |
| 2838 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2839 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2840 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2841 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2842 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2843 | } else |
| 2844 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2845 | |
Ted Kremenek | 304a953 | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 2846 | // Save the current value for the break targets. |
| 2847 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 2848 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2849 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Ted Kremenek | 304a953 | 2010-05-21 20:30:15 +0000 | [diff] [blame] | 2850 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2851 | CFGBlock *BodyBlock = nullptr, *TransitionBlock = nullptr; |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 2852 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2853 | // Now create the loop body. |
| 2854 | { |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 2855 | assert(F->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2856 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2857 | // Save the current values for Block, Succ, continue and break targets. |
| 2858 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 2859 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2860 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2861 | // Create an empty block to represent the transition block for looping back |
| 2862 | // to the head of the loop. If we have increment code, it will |
| 2863 | // go in this block as well. |
| 2864 | Block = Succ = TransitionBlock = createBlock(false); |
| 2865 | TransitionBlock->setLoopTarget(F); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2866 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2867 | if (Stmt *I = F->getInc()) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2868 | // Generate increment code in its own basic block. This is the target of |
| 2869 | // continue statements. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 2870 | Succ = addStmt(I); |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 2871 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2872 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 2873 | // Finish up the increment (or empty) block if it hasn't been already. |
| 2874 | if (Block) { |
| 2875 | assert(Block == Succ); |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2876 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2877 | return nullptr; |
| 2878 | Block = nullptr; |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 2879 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2880 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2881 | // The starting block for the loop increment is the block that should |
| 2882 | // represent the 'loop target' for looping back to the start of the loop. |
| 2883 | ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos); |
| 2884 | ContinueJumpTarget.block->setLoopTarget(F); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2885 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2886 | // Loop body should end with destructor of Condition variable (if any). |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 2887 | addAutomaticObjHandling(ScopePos, LoopBeginScopePos, F); |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 2888 | |
Marcin Swiderski | 6d5ee0c | 2010-10-01 01:38:14 +0000 | [diff] [blame] | 2889 | // If body is not a compound statement create implicit scope |
| 2890 | // and add destructors. |
| 2891 | if (!isa<CompoundStmt>(F->getBody())) |
| 2892 | addLocalScopeAndDtors(F->getBody()); |
| 2893 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2894 | // Now populate the body block, and in the process create new blocks as we |
| 2895 | // walk the body of the loop. |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2896 | BodyBlock = addStmt(F->getBody()); |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 2897 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2898 | if (!BodyBlock) { |
| 2899 | // In the case of "for (...;...;...);" we can have a null BodyBlock. |
| 2900 | // Use the continue jump target as the proxy for the body. |
| 2901 | BodyBlock = ContinueJumpTarget.block; |
| 2902 | } |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 2903 | else if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2904 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2905 | } |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2906 | |
| 2907 | // Because of short-circuit evaluation, the condition of the loop can span |
| 2908 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 2909 | // evaluate the condition. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2910 | CFGBlock *EntryConditionBlock = nullptr, *ExitConditionBlock = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2911 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2912 | do { |
| 2913 | Expr *C = F->getCond(); |
| 2914 | |
| 2915 | // Specially handle logical operators, which have a slightly |
| 2916 | // more optimal CFG representation. |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 2917 | if (BinaryOperator *Cond = |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2918 | dyn_cast_or_null<BinaryOperator>(C ? C->IgnoreParens() : nullptr)) |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2919 | if (Cond->isLogicalOp()) { |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 2920 | std::tie(EntryConditionBlock, ExitConditionBlock) = |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2921 | VisitLogicalOperator(Cond, F, BodyBlock, LoopSuccessor); |
| 2922 | break; |
| 2923 | } |
| 2924 | |
| 2925 | // The default case when not handling logical operators. |
| 2926 | EntryConditionBlock = ExitConditionBlock = createBlock(false); |
| 2927 | ExitConditionBlock->setTerminator(F); |
| 2928 | |
| 2929 | // See if this is a known constant. |
| 2930 | TryResult KnownVal(true); |
| 2931 | |
| 2932 | if (C) { |
| 2933 | // Now add the actual condition to the condition block. |
| 2934 | // Because the condition itself may contain control-flow, new blocks may |
| 2935 | // be created. Thus we update "Succ" after adding the condition. |
| 2936 | Block = ExitConditionBlock; |
| 2937 | EntryConditionBlock = addStmt(C); |
| 2938 | |
| 2939 | // If this block contains a condition variable, add both the condition |
| 2940 | // variable and initializer to the CFG. |
| 2941 | if (VarDecl *VD = F->getConditionVariable()) { |
| 2942 | if (Expr *Init = VD->getInit()) { |
| 2943 | autoCreateBlock(); |
| 2944 | appendStmt(Block, F->getConditionVariableDeclStmt()); |
| 2945 | EntryConditionBlock = addStmt(Init); |
| 2946 | assert(Block == EntryConditionBlock); |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | if (Block && badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2951 | return nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2952 | |
| 2953 | KnownVal = tryEvaluateBool(C); |
| 2954 | } |
| 2955 | |
| 2956 | // Add the loop body entry as a successor to the condition. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2957 | addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? nullptr : BodyBlock); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2958 | // Link up the condition block with the code that follows the loop. (the |
| 2959 | // false branch). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2960 | addSuccessor(ExitConditionBlock, |
| 2961 | KnownVal.isTrue() ? nullptr : LoopSuccessor); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 2962 | } while (false); |
| 2963 | |
| 2964 | // Link up the loop-back block to the entry condition block. |
| 2965 | addSuccessor(TransitionBlock, EntryConditionBlock); |
| 2966 | |
| 2967 | // The condition block is the implicit successor for any code above the loop. |
| 2968 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2969 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2970 | // If the loop contains initialization, create a new block for those |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2971 | // statements. This block can also contain statements that precede the loop. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 2972 | if (Stmt *I = F->getInit()) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2973 | Block = createBlock(); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 2974 | return addStmt(I); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2975 | } |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 2976 | |
| 2977 | // There is no loop initialization. We are thus basically a while loop. |
| 2978 | // NULL out Block to force lazy block construction. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 2979 | Block = nullptr; |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 2980 | Succ = EntryConditionBlock; |
| 2981 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Artem Dergachev | f43ac4c | 2018-02-24 02:00:30 +0000 | [diff] [blame^] | 2984 | CFGBlock * |
| 2985 | CFGBuilder::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *MTE, |
| 2986 | AddStmtChoice asc) { |
| 2987 | findConstructionContexts( |
| 2988 | ConstructionContext::create(cfg->getBumpVectorContext(), MTE), |
| 2989 | MTE->getTemporary()); |
| 2990 | |
| 2991 | return VisitStmt(MTE, asc); |
| 2992 | } |
| 2993 | |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 2994 | CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 2995 | if (asc.alwaysAdd(*this, M)) { |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 2996 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 2997 | appendStmt(Block, M); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 2998 | } |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 2999 | return Visit(M->getBase()); |
Ted Kremenek | 5868ec6 | 2010-04-11 17:02:10 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3002 | CFGBlock *CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 3003 | // Objective-C fast enumeration 'for' statements: |
| 3004 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC |
| 3005 | // |
| 3006 | // for ( Type newVariable in collection_expression ) { statements } |
| 3007 | // |
| 3008 | // becomes: |
| 3009 | // |
| 3010 | // prologue: |
| 3011 | // 1. collection_expression |
| 3012 | // T. jump to loop_entry |
| 3013 | // loop_entry: |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3014 | // 1. side-effects of element expression |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 3015 | // 1. ObjCForCollectionStmt [performs binding to newVariable] |
| 3016 | // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil] |
| 3017 | // TB: |
| 3018 | // statements |
| 3019 | // T. jump to loop_entry |
| 3020 | // FB: |
| 3021 | // what comes after |
| 3022 | // |
| 3023 | // and |
| 3024 | // |
| 3025 | // Type existingItem; |
| 3026 | // for ( existingItem in expression ) { statements } |
| 3027 | // |
| 3028 | // becomes: |
| 3029 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3030 | // the same with newVariable replaced with existingItem; the binding works |
| 3031 | // the same except that for one ObjCForCollectionStmt::getElement() returns |
| 3032 | // a DeclStmt and the other returns a DeclRefExpr. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3033 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3034 | CFGBlock *LoopSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3035 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 3036 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3037 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3038 | return nullptr; |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 3039 | LoopSuccessor = Block; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3040 | Block = nullptr; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3041 | } else |
| 3042 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3043 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3044 | // Build the condition blocks. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3045 | CFGBlock *ExitConditionBlock = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3046 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3047 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3048 | ExitConditionBlock->setTerminator(S); |
| 3049 | |
| 3050 | // The last statement in the block should be the ObjCForCollectionStmt, which |
| 3051 | // performs the actual binding to 'element' and determines if there are any |
| 3052 | // more items in the collection. |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 3053 | appendStmt(ExitConditionBlock, S); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3054 | Block = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3055 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3056 | // 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] | 3057 | // 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] | 3058 | // the CFG unless it contains control-flow. |
Ted Kremenek | c14efa7 | 2011-08-17 21:04:19 +0000 | [diff] [blame] | 3059 | CFGBlock *EntryConditionBlock = Visit(S->getElement(), |
| 3060 | AddStmtChoice::NotAlwaysAdd); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3061 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3062 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3063 | return nullptr; |
| 3064 | Block = nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3065 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3066 | |
| 3067 | // The condition block is the implicit successor for the loop body as well as |
| 3068 | // any code above the loop. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3069 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3070 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 3071 | // Now create the true branch. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3072 | { |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3073 | // Save the current values for Succ, continue and break targets. |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 3074 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3075 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 3076 | save_break(BreakJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3077 | |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 3078 | // Add an intermediate block between the BodyBlock and the |
| 3079 | // EntryConditionBlock to represent the "loop back" transition, for looping |
| 3080 | // back to the head of the loop. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3081 | CFGBlock *LoopBackBlock = nullptr; |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 3082 | Succ = LoopBackBlock = createBlock(); |
| 3083 | LoopBackBlock->setLoopTarget(S); |
| 3084 | |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3085 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 3086 | ContinueJumpTarget = JumpTarget(Succ, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3087 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3088 | CFGBlock *BodyBlock = addStmt(S->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3089 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3090 | if (!BodyBlock) |
Anna Zaks | 56b4975 | 2013-06-22 00:23:20 +0000 | [diff] [blame] | 3091 | BodyBlock = ContinueJumpTarget.block; // can happen for "for (X in Y) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3092 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3093 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3094 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3095 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3096 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3097 | // This new body block is a successor to our "exit" condition block. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3098 | addSuccessor(ExitConditionBlock, BodyBlock); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3099 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3100 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3101 | // Link up the condition block with the code that follows the loop. |
| 3102 | // (the false branch). |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3103 | addSuccessor(ExitConditionBlock, LoopSuccessor); |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3104 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 3105 | // Now create a prologue block to contain the collection expression. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 3106 | Block = createBlock(); |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 3107 | return addStmt(S->getCollection()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Ted Kremenek | 5022f1d | 2012-03-06 23:40:47 +0000 | [diff] [blame] | 3110 | CFGBlock *CFGBuilder::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) { |
| 3111 | // Inline the body. |
| 3112 | return addStmt(S->getSubStmt()); |
| 3113 | // TODO: consider adding cleanups for the end of @autoreleasepool scope. |
| 3114 | } |
| 3115 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3116 | CFGBlock *CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 3117 | // FIXME: Add locking 'primitives' to CFG for @synchronized. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3118 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 3119 | // Inline the body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3120 | CFGBlock *SyncBlock = addStmt(S->getSynchBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3121 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 3122 | // The sync body starts its own basic block. This makes it a little easier |
| 3123 | // for diagnostic clients. |
| 3124 | if (SyncBlock) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3125 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3126 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3127 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3128 | Block = nullptr; |
Ted Kremenek | ecc31c9 | 2010-05-13 16:38:08 +0000 | [diff] [blame] | 3129 | Succ = SyncBlock; |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 3130 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3131 | |
Ted Kremenek | ed12f1b | 2010-09-10 03:05:33 +0000 | [diff] [blame] | 3132 | // Add the @synchronized to the CFG. |
| 3133 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 3134 | appendStmt(Block, S); |
Ted Kremenek | ed12f1b | 2010-09-10 03:05:33 +0000 | [diff] [blame] | 3135 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 3136 | // Inline the sync expression. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3137 | return addStmt(S->getSynchExpr()); |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 3138 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3139 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3140 | CFGBlock *CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3141 | // FIXME |
Ted Kremenek | 89be652 | 2009-04-07 04:26:02 +0000 | [diff] [blame] | 3142 | return NYS(); |
Ted Kremenek | 89cc8ea | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 3143 | } |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 3144 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3145 | CFGBlock *CFGBuilder::VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 3146 | autoCreateBlock(); |
| 3147 | |
| 3148 | // Add the PseudoObject as the last thing. |
| 3149 | appendStmt(Block, E); |
| 3150 | |
| 3151 | CFGBlock *lastBlock = Block; |
| 3152 | |
| 3153 | // Before that, evaluate all of the semantics in order. In |
| 3154 | // CFG-land, that means appending them in reverse order. |
| 3155 | for (unsigned i = E->getNumSemanticExprs(); i != 0; ) { |
| 3156 | Expr *Semantic = E->getSemanticExpr(--i); |
| 3157 | |
| 3158 | // If the semantic is an opaque value, we're being asked to bind |
| 3159 | // it to its source expression. |
| 3160 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic)) |
| 3161 | Semantic = OVE->getSourceExpr(); |
| 3162 | |
| 3163 | if (CFGBlock *B = Visit(Semantic)) |
| 3164 | lastBlock = B; |
| 3165 | } |
| 3166 | |
| 3167 | return lastBlock; |
| 3168 | } |
| 3169 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3170 | CFGBlock *CFGBuilder::VisitWhileStmt(WhileStmt *W) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3171 | CFGBlock *LoopSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3172 | |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 3173 | // Save local scope position because in case of condition variable ScopePos |
| 3174 | // won't be restored when traversing AST. |
| 3175 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 3176 | |
| 3177 | // Create local scope for possible condition variable. |
| 3178 | // Store scope position for continue statement. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3179 | LocalScope::const_iterator LoopBeginScopePos = ScopePos; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3180 | if (VarDecl *VD = W->getConditionVariable()) { |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 3181 | addLocalScopeForVarDecl(VD); |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 3182 | addAutomaticObjHandling(ScopePos, LoopBeginScopePos, W); |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 3183 | } |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 3184 | addLoopExit(W); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3185 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 3186 | // "while" is a control-flow statement. Thus we stop processing the current |
| 3187 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3188 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3189 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3190 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3191 | LoopSuccessor = Block; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3192 | Block = nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3193 | } else { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3194 | LoopSuccessor = Succ; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3195 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3196 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3197 | CFGBlock *BodyBlock = nullptr, *TransitionBlock = nullptr; |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 3198 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3199 | // Process the loop body. |
| 3200 | { |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 3201 | assert(W->getBody()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3202 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3203 | // Save the current values for Block, Succ, continue and break targets. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3204 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 3205 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3206 | save_break(BreakJumpTarget); |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 3207 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3208 | // Create an empty block to represent the transition block for looping back |
| 3209 | // to the head of the loop. |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3210 | Succ = TransitionBlock = createBlock(false); |
| 3211 | TransitionBlock->setLoopTarget(W); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3212 | ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3213 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3214 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 3215 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3216 | |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 3217 | // Loop body should end with destructor of Condition variable (if any). |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 3218 | addAutomaticObjHandling(ScopePos, LoopBeginScopePos, W); |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 3219 | |
| 3220 | // If body is not a compound statement create implicit scope |
| 3221 | // and add destructors. |
| 3222 | if (!isa<CompoundStmt>(W->getBody())) |
| 3223 | addLocalScopeAndDtors(W->getBody()); |
| 3224 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3225 | // 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] | 3226 | BodyBlock = addStmt(W->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3227 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 3228 | if (!BodyBlock) |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 3229 | BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;" |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3230 | else if (Block && badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3231 | return nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | // Because of short-circuit evaluation, the condition of the loop can span |
| 3235 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 3236 | // evaluate the condition. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3237 | CFGBlock *EntryConditionBlock = nullptr, *ExitConditionBlock = nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3238 | |
| 3239 | do { |
| 3240 | Expr *C = W->getCond(); |
| 3241 | |
| 3242 | // Specially handle logical operators, which have a slightly |
| 3243 | // more optimal CFG representation. |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 3244 | if (BinaryOperator *Cond = dyn_cast<BinaryOperator>(C->IgnoreParens())) |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3245 | if (Cond->isLogicalOp()) { |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 3246 | std::tie(EntryConditionBlock, ExitConditionBlock) = |
| 3247 | VisitLogicalOperator(Cond, W, BodyBlock, LoopSuccessor); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3248 | break; |
| 3249 | } |
| 3250 | |
| 3251 | // The default case when not handling logical operators. |
Ted Kremenek | 451c4d5 | 2012-10-12 22:56:26 +0000 | [diff] [blame] | 3252 | ExitConditionBlock = createBlock(false); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3253 | ExitConditionBlock->setTerminator(W); |
| 3254 | |
| 3255 | // Now add the actual condition to the condition block. |
| 3256 | // Because the condition itself may contain control-flow, new blocks may |
| 3257 | // be created. Thus we update "Succ" after adding the condition. |
| 3258 | Block = ExitConditionBlock; |
| 3259 | Block = EntryConditionBlock = addStmt(C); |
| 3260 | |
| 3261 | // If this block contains a condition variable, add both the condition |
| 3262 | // variable and initializer to the CFG. |
| 3263 | if (VarDecl *VD = W->getConditionVariable()) { |
| 3264 | if (Expr *Init = VD->getInit()) { |
| 3265 | autoCreateBlock(); |
| 3266 | appendStmt(Block, W->getConditionVariableDeclStmt()); |
| 3267 | EntryConditionBlock = addStmt(Init); |
| 3268 | assert(Block == EntryConditionBlock); |
| 3269 | } |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3270 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3271 | |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3272 | if (Block && badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3273 | return nullptr; |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3274 | |
| 3275 | // See if this is a known constant. |
| 3276 | const TryResult& KnownVal = tryEvaluateBool(C); |
| 3277 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 3278 | // Add the loop body entry as a successor to the condition. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3279 | addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? nullptr : BodyBlock); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3280 | // Link up the condition block with the code that follows the loop. (the |
| 3281 | // false branch). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3282 | addSuccessor(ExitConditionBlock, |
| 3283 | KnownVal.isTrue() ? nullptr : LoopSuccessor); |
Ted Kremenek | b50e716 | 2012-07-14 05:04:10 +0000 | [diff] [blame] | 3284 | } while(false); |
| 3285 | |
| 3286 | // Link up the loop-back block to the entry condition block. |
| 3287 | addSuccessor(TransitionBlock, EntryConditionBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3288 | |
| 3289 | // There can be no more statements in the condition block since we loop back |
| 3290 | // 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] | 3291 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3292 | |
Ted Kremenek | 1ce53c4 | 2009-12-24 01:34:10 +0000 | [diff] [blame] | 3293 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 3294 | Succ = EntryConditionBlock; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3295 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3296 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3298 | CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3299 | // FIXME: For now we pretend that @catch and the code it contains does not |
| 3300 | // exit. |
| 3301 | return Block; |
| 3302 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3303 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3304 | CFGBlock *CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 3305 | // FIXME: This isn't complete. We basically treat @throw like a return |
| 3306 | // statement. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3307 | |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 3308 | // 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] | 3309 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3310 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3311 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 3312 | // Create the new block. |
| 3313 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3314 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 3315 | // The Exit block is the only successor. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3316 | addSuccessor(Block, &cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3317 | |
| 3318 | // Add the statement to the block. This may create new blocks if S contains |
| 3319 | // control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 3320 | return VisitStmt(S, AddStmtChoice::AlwaysAdd); |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 3321 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3322 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3323 | CFGBlock *CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr *T) { |
Ted Kremenek | 0868eea | 2009-09-24 18:45:41 +0000 | [diff] [blame] | 3324 | // 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] | 3325 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3326 | return nullptr; |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 3327 | |
| 3328 | // Create the new block. |
| 3329 | Block = createBlock(false); |
| 3330 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3331 | if (TryTerminatedBlock) |
| 3332 | // The current try statement is the only successor. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3333 | addSuccessor(Block, TryTerminatedBlock); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 3334 | else |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3335 | // otherwise the Exit block is the only successor. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3336 | addSuccessor(Block, &cfg->getExit()); |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 3337 | |
| 3338 | // Add the statement to the block. This may create new blocks if S contains |
| 3339 | // control-flow (short-circuit operations). |
Ted Kremenek | 4cad5fc | 2009-12-16 03:18:58 +0000 | [diff] [blame] | 3340 | return VisitStmt(T, AddStmtChoice::AlwaysAdd); |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3343 | CFGBlock *CFGBuilder::VisitDoStmt(DoStmt *D) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3344 | CFGBlock *LoopSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3345 | |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 3346 | addLoopExit(D); |
| 3347 | |
Mike Stump | 8d50b6a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 3348 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 3349 | // current block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3350 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3351 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3352 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3353 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3354 | } else |
| 3355 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3356 | |
| 3357 | // Because of short-circuit evaluation, the condition of the loop can span |
| 3358 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 3359 | // evaluate the condition. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3360 | CFGBlock *ExitConditionBlock = createBlock(false); |
| 3361 | CFGBlock *EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3362 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3363 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3364 | ExitConditionBlock->setTerminator(D); |
| 3365 | |
| 3366 | // Now add the actual condition to the condition block. Because the condition |
| 3367 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3368 | if (Stmt *C = D->getCond()) { |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3369 | Block = ExitConditionBlock; |
| 3370 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3371 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3372 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3373 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3374 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3375 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3376 | |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 3377 | // The condition block is the implicit successor for the loop body. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3378 | Succ = EntryConditionBlock; |
| 3379 | |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 3380 | // See if this is a known constant. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3381 | const TryResult &KnownVal = tryEvaluateBool(D->getCond()); |
Mike Stump | 773582d | 2009-07-23 23:25:26 +0000 | [diff] [blame] | 3382 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3383 | // Process the loop body. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3384 | CFGBlock *BodyBlock = nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3385 | { |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 3386 | assert(D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3387 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3388 | // Save the current values for Block, Succ, and continue and break targets |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3389 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 3390 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget), |
| 3391 | save_break(BreakJumpTarget); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3392 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3393 | // All continues within this loop should go to the condition block |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3394 | ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3395 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3396 | // All breaks should go to the code following the loop. |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3397 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3398 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3399 | // NULL out Block to force lazy instantiation of blocks for the body. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3400 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3401 | |
Marcin Swiderski | 1f4e15c | 2010-10-01 01:14:17 +0000 | [diff] [blame] | 3402 | // If body is not a compound statement create implicit scope |
| 3403 | // and add destructors. |
| 3404 | if (!isa<CompoundStmt>(D->getBody())) |
| 3405 | addLocalScopeAndDtors(D->getBody()); |
| 3406 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3407 | // 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] | 3408 | BodyBlock = addStmt(D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3409 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 3410 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 3411 | BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3412 | else if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3413 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3414 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3415 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3416 | |
Daniel Marjamaki | 042a3c5 | 2016-10-03 08:28:51 +0000 | [diff] [blame] | 3417 | // Add an intermediate block between the BodyBlock and the |
| 3418 | // ExitConditionBlock to represent the "loop back" transition. Create an |
| 3419 | // empty block to represent the transition block for looping back to the |
| 3420 | // head of the loop. |
| 3421 | // FIXME: Can we do this more efficiently without adding another block? |
| 3422 | Block = nullptr; |
| 3423 | Succ = BodyBlock; |
| 3424 | CFGBlock *LoopBackBlock = createBlock(); |
| 3425 | LoopBackBlock->setLoopTarget(D); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3426 | |
Daniel Marjamaki | 042a3c5 | 2016-10-03 08:28:51 +0000 | [diff] [blame] | 3427 | if (!KnownVal.isFalse()) |
Ted Kremenek | 110974d | 2010-08-17 20:59:56 +0000 | [diff] [blame] | 3428 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3429 | addSuccessor(ExitConditionBlock, LoopBackBlock); |
Ted Kremenek | 110974d | 2010-08-17 20:59:56 +0000 | [diff] [blame] | 3430 | else |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3431 | addSuccessor(ExitConditionBlock, nullptr); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3432 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3433 | |
Ted Kremenek | 3075428 | 2009-07-24 04:47:11 +0000 | [diff] [blame] | 3434 | // Link up the condition block with the code that follows the loop. |
| 3435 | // (the false branch). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3436 | addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? nullptr : LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3437 | |
| 3438 | // There can be no more statements in the body block(s) since we loop back to |
| 3439 | // the body. NULL out Block to force lazy creation of another block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3440 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3441 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3442 | // Return the loop body, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 3443 | Succ = BodyBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3444 | return BodyBlock; |
| 3445 | } |
| 3446 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3447 | CFGBlock *CFGBuilder::VisitContinueStmt(ContinueStmt *C) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3448 | // "continue" is a control-flow statement. Thus we stop processing the |
| 3449 | // current block. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3450 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3451 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3452 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3453 | // Now create a new block that ends with the continue statement. |
| 3454 | Block = createBlock(false); |
| 3455 | Block->setTerminator(C); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3456 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3457 | // 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] | 3458 | // incomplete AST. This means the CFG cannot be constructed. |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 3459 | if (ContinueJumpTarget.block) { |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 3460 | addAutomaticObjHandling(ScopePos, ContinueJumpTarget.scopePosition, C); |
Ted Kremenek | ef81e9e | 2011-01-07 19:37:16 +0000 | [diff] [blame] | 3461 | addSuccessor(Block, ContinueJumpTarget.block); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3462 | } else |
Ted Kremenek | 882cf06 | 2009-04-07 18:53:24 +0000 | [diff] [blame] | 3463 | badCFG = true; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3464 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3465 | return Block; |
| 3466 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3468 | CFGBlock *CFGBuilder::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E, |
| 3469 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 3470 | if (asc.alwaysAdd(*this, E)) { |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3471 | autoCreateBlock(); |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 3472 | appendStmt(Block, E); |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3475 | // VLA types have expressions that must be evaluated. |
Ted Kremenek | 9eb0b7d | 2011-04-14 01:50:50 +0000 | [diff] [blame] | 3476 | CFGBlock *lastBlock = Block; |
| 3477 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3478 | if (E->isArgumentType()) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3479 | for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr()); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3480 | VA != nullptr; VA = FindVA(VA->getElementType().getTypePtr())) |
Ted Kremenek | 9eb0b7d | 2011-04-14 01:50:50 +0000 | [diff] [blame] | 3481 | lastBlock = addStmt(VA->getSizeExpr()); |
Ted Kremenek | 84a1ca5 | 2011-08-06 00:30:00 +0000 | [diff] [blame] | 3482 | } |
Ted Kremenek | 9eb0b7d | 2011-04-14 01:50:50 +0000 | [diff] [blame] | 3483 | return lastBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3484 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3486 | /// VisitStmtExpr - Utility method to handle (nested) statement |
| 3487 | /// expressions (a GCC extension). |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3488 | CFGBlock *CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 3489 | if (asc.alwaysAdd(*this, SE)) { |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3490 | autoCreateBlock(); |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 3491 | appendStmt(Block, SE); |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 3492 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3493 | return VisitCompoundStmt(SE->getSubStmt()); |
| 3494 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3495 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3496 | CFGBlock *CFGBuilder::VisitSwitchStmt(SwitchStmt *Terminator) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3497 | // "switch" is a control-flow statement. Thus we stop processing the current |
| 3498 | // block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3499 | CFGBlock *SwitchSuccessor = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3500 | |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3501 | // Save local scope position because in case of condition variable ScopePos |
| 3502 | // won't be restored when traversing AST. |
| 3503 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 3504 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3505 | // Create local scope for C++17 switch init-stmt if one exists. |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 3506 | if (Stmt *Init = Terminator->getInit()) |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3507 | addLocalScopeForStmt(Init); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3508 | |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3509 | // Create local scope for possible condition variable. |
| 3510 | // Store scope position. Add implicit destructor. |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 3511 | if (VarDecl *VD = Terminator->getConditionVariable()) |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3512 | addLocalScopeForVarDecl(VD); |
Richard Smith | 509bbd1 | 2017-01-13 22:16:41 +0000 | [diff] [blame] | 3513 | |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 3514 | addAutomaticObjHandling(ScopePos, save_scope_pos.get(), Terminator); |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3515 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3516 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3517 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3518 | return nullptr; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3519 | SwitchSuccessor = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3520 | } else SwitchSuccessor = Succ; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3521 | |
| 3522 | // Save the current "switch" context. |
| 3523 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3524 | save_default(DefaultCaseBlock); |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3525 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3526 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3527 | // Set the "default" case to be the block after the switch statement. If the |
| 3528 | // switch statement contains a "default:", this value will be overwritten with |
| 3529 | // the block for that code. |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3530 | DefaultCaseBlock = SwitchSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3531 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3532 | // Create a new block that will contain the switch statement. |
| 3533 | SwitchTerminatedBlock = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3534 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3535 | // Now process the switch body. The code after the switch is the implicit |
| 3536 | // successor. |
| 3537 | Succ = SwitchSuccessor; |
Marcin Swiderski | 8b99b8a | 2010-09-25 11:05:21 +0000 | [diff] [blame] | 3538 | BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3539 | |
| 3540 | // When visiting the body, the case statements should automatically get linked |
| 3541 | // up to the switch. We also don't keep a pointer to the body, since all |
| 3542 | // control-flow from the switch goes to case/default statements. |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 3543 | assert(Terminator->getBody() && "switch must contain a non-NULL body"); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3544 | Block = nullptr; |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3545 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3546 | // For pruning unreachable case statements, save the current state |
| 3547 | // for tracking the condition value. |
| 3548 | SaveAndRestore<bool> save_switchExclusivelyCovered(switchExclusivelyCovered, |
| 3549 | false); |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3550 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3551 | // Determine if the switch condition can be explicitly evaluated. |
| 3552 | assert(Terminator->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3553 | Expr::EvalResult result; |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3554 | bool b = tryEvaluate(Terminator->getCond(), result); |
| 3555 | SaveAndRestore<Expr::EvalResult*> save_switchCond(switchCond, |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3556 | b ? &result : nullptr); |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3557 | |
Marcin Swiderski | e407a3b | 2010-10-01 01:24:41 +0000 | [diff] [blame] | 3558 | // If body is not a compound statement create implicit scope |
| 3559 | // and add destructors. |
| 3560 | if (!isa<CompoundStmt>(Terminator->getBody())) |
| 3561 | addLocalScopeAndDtors(Terminator->getBody()); |
| 3562 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3563 | addStmt(Terminator->getBody()); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3564 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3565 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3566 | return nullptr; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 3567 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3568 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3569 | // 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] | 3570 | // following the switch body. Moreover, take into account if all the |
| 3571 | // 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] | 3572 | // |
| 3573 | // Note: We add a successor to a switch that is considered covered yet has no |
| 3574 | // case statements if the enumeration has no enumerators. |
| 3575 | bool SwitchAlwaysHasSuccessor = false; |
| 3576 | SwitchAlwaysHasSuccessor |= switchExclusivelyCovered; |
| 3577 | SwitchAlwaysHasSuccessor |= Terminator->isAllEnumCasesCovered() && |
| 3578 | Terminator->getSwitchCaseList(); |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 3579 | addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock, |
| 3580 | !SwitchAlwaysHasSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3581 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 3582 | // Add the terminator and condition in the switch block. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3583 | SwitchTerminatedBlock->setTerminator(Terminator); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3584 | Block = SwitchTerminatedBlock; |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3585 | CFGBlock *LastBlock = addStmt(Terminator->getCond()); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 3586 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3587 | // If the SwitchStmt contains a condition variable, add both the |
Ted Kremenek | 8b5dc12 | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 3588 | // SwitchStmt and the condition variable initialization to the CFG. |
| 3589 | if (VarDecl *VD = Terminator->getConditionVariable()) { |
| 3590 | if (Expr *Init = VD->getInit()) { |
| 3591 | autoCreateBlock(); |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 3592 | appendStmt(Block, Terminator->getConditionVariableDeclStmt()); |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3593 | LastBlock = addStmt(Init); |
Ted Kremenek | 8b5dc12 | 2009-12-24 00:39:26 +0000 | [diff] [blame] | 3594 | } |
| 3595 | } |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 3596 | |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3597 | // Finally, if the SwitchStmt contains a C++17 init-stmt, add it to the CFG. |
| 3598 | if (Stmt *Init = Terminator->getInit()) { |
| 3599 | autoCreateBlock(); |
| 3600 | LastBlock = addStmt(Init); |
| 3601 | } |
| 3602 | |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3603 | return LastBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3604 | } |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3605 | |
| 3606 | static bool shouldAddCase(bool &switchExclusivelyCovered, |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3607 | const Expr::EvalResult *switchCond, |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3608 | const CaseStmt *CS, |
| 3609 | ASTContext &Ctx) { |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3610 | if (!switchCond) |
| 3611 | return true; |
| 3612 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3613 | bool addCase = false; |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3614 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3615 | if (!switchExclusivelyCovered) { |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3616 | if (switchCond->Val.isInt()) { |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3617 | // Evaluate the LHS of the case value. |
Richard Smith | faa32a9 | 2011-10-14 20:22:00 +0000 | [diff] [blame] | 3618 | const llvm::APSInt &lhsInt = CS->getLHS()->EvaluateKnownConstInt(Ctx); |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3619 | const llvm::APSInt &condInt = switchCond->Val.getInt(); |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3620 | |
| 3621 | if (condInt == lhsInt) { |
| 3622 | addCase = true; |
| 3623 | switchExclusivelyCovered = true; |
| 3624 | } |
Devin Coughlin | eb538ab | 2015-09-22 20:31:19 +0000 | [diff] [blame] | 3625 | else if (condInt > lhsInt) { |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3626 | if (const Expr *RHS = CS->getRHS()) { |
| 3627 | // Evaluate the RHS of the case value. |
Richard Smith | faa32a9 | 2011-10-14 20:22:00 +0000 | [diff] [blame] | 3628 | const llvm::APSInt &V2 = RHS->EvaluateKnownConstInt(Ctx); |
Devin Coughlin | eb538ab | 2015-09-22 20:31:19 +0000 | [diff] [blame] | 3629 | if (V2 >= condInt) { |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3630 | addCase = true; |
| 3631 | switchExclusivelyCovered = true; |
| 3632 | } |
| 3633 | } |
| 3634 | } |
| 3635 | } |
| 3636 | else |
| 3637 | addCase = true; |
| 3638 | } |
| 3639 | return addCase; |
| 3640 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3641 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3642 | CFGBlock *CFGBuilder::VisitCaseStmt(CaseStmt *CS) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3643 | // CaseStmts are essentially labels, so they are the first statement in a |
| 3644 | // block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3645 | CFGBlock *TopBlock = nullptr, *LastBlock = nullptr; |
Ted Kremenek | be52871 | 2011-03-04 01:03:41 +0000 | [diff] [blame] | 3646 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3647 | if (Stmt *Sub = CS->getSubStmt()) { |
| 3648 | // For deeply nested chains of CaseStmts, instead of doing a recursion |
| 3649 | // (which can blow out the stack), manually unroll and create blocks |
| 3650 | // along the way. |
| 3651 | while (isa<CaseStmt>(Sub)) { |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3652 | CFGBlock *currentBlock = createBlock(false); |
| 3653 | currentBlock->setLabel(CS); |
Ted Kremenek | 55e91e8 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 3654 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3655 | if (TopBlock) |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3656 | addSuccessor(LastBlock, currentBlock); |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3657 | else |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3658 | TopBlock = currentBlock; |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3659 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3660 | addSuccessor(SwitchTerminatedBlock, |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3661 | shouldAddCase(switchExclusivelyCovered, switchCond, |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3662 | CS, *Context) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3663 | ? currentBlock : nullptr); |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3664 | |
Ted Kremenek | eff9a7f | 2011-03-01 23:12:55 +0000 | [diff] [blame] | 3665 | LastBlock = currentBlock; |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3666 | CS = cast<CaseStmt>(Sub); |
| 3667 | Sub = CS->getSubStmt(); |
| 3668 | } |
| 3669 | |
| 3670 | addStmt(Sub); |
| 3671 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3673 | CFGBlock *CaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3674 | if (!CaseBlock) |
| 3675 | CaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3676 | |
| 3677 | // Cases statements partition blocks, so this is the top of the basic block we |
| 3678 | // were processing (the "case XXX:" is the label). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3679 | CaseBlock->setLabel(CS); |
| 3680 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3681 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3682 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3683 | |
| 3684 | // Add this block to the list of successors for the block with the switch |
| 3685 | // statement. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3686 | assert(SwitchTerminatedBlock); |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 3687 | addSuccessor(SwitchTerminatedBlock, CaseBlock, |
Ted Kremenek | 53e6538 | 2011-03-13 03:48:04 +0000 | [diff] [blame] | 3688 | shouldAddCase(switchExclusivelyCovered, switchCond, |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 3689 | CS, *Context)); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3690 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3691 | // 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] | 3692 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3693 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3694 | if (TopBlock) { |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3695 | addSuccessor(LastBlock, CaseBlock); |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3696 | Succ = TopBlock; |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 3697 | } else { |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3698 | // This block is now the implicit successor of other blocks. |
| 3699 | Succ = CaseBlock; |
| 3700 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3701 | |
Ted Kremenek | 60fa657 | 2010-08-04 23:54:30 +0000 | [diff] [blame] | 3702 | return Succ; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3703 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3704 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3705 | CFGBlock *CFGBuilder::VisitDefaultStmt(DefaultStmt *Terminator) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3706 | if (Terminator->getSubStmt()) |
| 3707 | addStmt(Terminator->getSubStmt()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3709 | DefaultCaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 3710 | |
| 3711 | if (!DefaultCaseBlock) |
| 3712 | DefaultCaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3713 | |
| 3714 | // Default statements partition blocks, so this is the top of the basic block |
| 3715 | // we were processing (the "default:" is the label). |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 3716 | DefaultCaseBlock->setLabel(Terminator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3717 | |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3718 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3719 | return nullptr; |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3720 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3721 | // Unlike case statements, we don't add the default block to the successors |
| 3722 | // for the switch statement immediately. This is done when we finish |
| 3723 | // processing the switch statement. This allows for the default case |
| 3724 | // (including a fall-through to the code after the switch statement) to always |
| 3725 | // be the last successor of a switch-terminated block. |
| 3726 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3727 | // 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] | 3728 | Block = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3729 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 3730 | // This block is now the implicit successor of other blocks. |
| 3731 | Succ = DefaultCaseBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 3732 | |
| 3733 | return DefaultCaseBlock; |
Ted Kremenek | 9682be1 | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 3734 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 3735 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3736 | CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) { |
| 3737 | // "try"/"catch" is a control-flow statement. Thus we stop processing the |
| 3738 | // current block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3739 | CFGBlock *TrySuccessor = nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3740 | |
| 3741 | if (Block) { |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3742 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3743 | return nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3744 | TrySuccessor = Block; |
| 3745 | } else TrySuccessor = Succ; |
| 3746 | |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3747 | CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3748 | |
| 3749 | // Create a new block that will contain the try statement. |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 3750 | CFGBlock *NewTryTerminatedBlock = createBlock(false); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3751 | // Add the terminator in the try block. |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 3752 | NewTryTerminatedBlock->setTerminator(Terminator); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3753 | |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3754 | bool HasCatchAll = false; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3755 | for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) { |
| 3756 | // The code after the try is the implicit successor. |
| 3757 | Succ = TrySuccessor; |
| 3758 | CXXCatchStmt *CS = Terminator->getHandler(h); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3759 | if (CS->getExceptionDecl() == nullptr) { |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3760 | HasCatchAll = true; |
| 3761 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3762 | Block = nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3763 | CFGBlock *CatchBlock = VisitCXXCatchStmt(CS); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3764 | if (!CatchBlock) |
| 3765 | return nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3766 | // Add this block to the list of successors for the block with the try |
| 3767 | // statement. |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3768 | addSuccessor(NewTryTerminatedBlock, CatchBlock); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3769 | } |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3770 | if (!HasCatchAll) { |
| 3771 | if (PrevTryTerminatedBlock) |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3772 | addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock); |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3773 | else |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 3774 | addSuccessor(NewTryTerminatedBlock, &cfg->getExit()); |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 3775 | } |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3776 | |
| 3777 | // The code after the try is the implicit successor. |
| 3778 | Succ = TrySuccessor; |
| 3779 | |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 3780 | // Save the current "try" context. |
Ted Kremenek | 6b9964d | 2011-08-23 23:05:07 +0000 | [diff] [blame] | 3781 | SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock, NewTryTerminatedBlock); |
| 3782 | cfg->addTryDispatchBlock(TryTerminatedBlock); |
Mike Stump | 845384a | 2010-01-20 01:30:58 +0000 | [diff] [blame] | 3783 | |
Ted Kremenek | 1362b8b | 2010-01-19 20:46:35 +0000 | [diff] [blame] | 3784 | assert(Terminator->getTryBlock() && "try must contain a non-NULL body"); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3785 | Block = nullptr; |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3786 | return addStmt(Terminator->getTryBlock()); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3789 | CFGBlock *CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt *CS) { |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3790 | // CXXCatchStmt are treated like labels, so they are the first statement in a |
| 3791 | // block. |
| 3792 | |
Marcin Swiderski | 3546b1a | 2010-10-01 01:46:52 +0000 | [diff] [blame] | 3793 | // Save local scope position because in case of exception variable ScopePos |
| 3794 | // won't be restored when traversing AST. |
| 3795 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 3796 | |
| 3797 | // Create local scope for possible exception variable. |
| 3798 | // Store scope position. Add implicit destructor. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3799 | if (VarDecl *VD = CS->getExceptionDecl()) { |
Marcin Swiderski | 3546b1a | 2010-10-01 01:46:52 +0000 | [diff] [blame] | 3800 | LocalScope::const_iterator BeginScopePos = ScopePos; |
| 3801 | addLocalScopeForVarDecl(VD); |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 3802 | addAutomaticObjHandling(ScopePos, BeginScopePos, CS); |
Marcin Swiderski | 3546b1a | 2010-10-01 01:46:52 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3805 | if (CS->getHandlerBlock()) |
| 3806 | addStmt(CS->getHandlerBlock()); |
| 3807 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3808 | CFGBlock *CatchBlock = Block; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3809 | if (!CatchBlock) |
| 3810 | CatchBlock = createBlock(); |
Ted Kremenek | 8fdb59f | 2012-03-10 01:34:17 +0000 | [diff] [blame] | 3811 | |
| 3812 | // CXXCatchStmt is more than just a label. They have semantic meaning |
| 3813 | // as well, as they implicitly "initialize" the catch variable. Add |
| 3814 | // it to the CFG as a CFGElement so that the control-flow of these |
| 3815 | // semantics gets captured. |
| 3816 | appendStmt(CatchBlock, CS); |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3817 | |
Ted Kremenek | 8fdb59f | 2012-03-10 01:34:17 +0000 | [diff] [blame] | 3818 | // Also add the CXXCatchStmt as a label, to mirror handling of regular |
| 3819 | // labels. |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3820 | CatchBlock->setLabel(CS); |
| 3821 | |
Ted Kremenek | 8fdb59f | 2012-03-10 01:34:17 +0000 | [diff] [blame] | 3822 | // Bail out if the CFG is bad. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 3823 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3824 | return nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3825 | |
| 3826 | // 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] | 3827 | Block = nullptr; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 3828 | |
| 3829 | return CatchBlock; |
| 3830 | } |
| 3831 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3832 | CFGBlock *CFGBuilder::VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3833 | // C++0x for-range statements are specified as [stmt.ranged]: |
| 3834 | // |
| 3835 | // { |
| 3836 | // auto && __range = range-init; |
| 3837 | // for ( auto __begin = begin-expr, |
| 3838 | // __end = end-expr; |
| 3839 | // __begin != __end; |
| 3840 | // ++__begin ) { |
| 3841 | // for-range-declaration = *__begin; |
| 3842 | // statement |
| 3843 | // } |
| 3844 | // } |
| 3845 | |
| 3846 | // Save local scope position before the addition of the implicit variables. |
| 3847 | SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos); |
| 3848 | |
| 3849 | // Create local scopes and destructors for range, begin and end variables. |
| 3850 | if (Stmt *Range = S->getRangeStmt()) |
| 3851 | addLocalScopeForStmt(Range); |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 3852 | if (Stmt *Begin = S->getBeginStmt()) |
| 3853 | addLocalScopeForStmt(Begin); |
| 3854 | if (Stmt *End = S->getEndStmt()) |
| 3855 | addLocalScopeForStmt(End); |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 3856 | addAutomaticObjHandling(ScopePos, save_scope_pos.get(), S); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3857 | |
| 3858 | LocalScope::const_iterator ContinueScopePos = ScopePos; |
| 3859 | |
| 3860 | // "for" is a control-flow statement. Thus we stop processing the current |
| 3861 | // block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3862 | CFGBlock *LoopSuccessor = nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3863 | if (Block) { |
| 3864 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3865 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3866 | LoopSuccessor = Block; |
| 3867 | } else |
| 3868 | LoopSuccessor = Succ; |
| 3869 | |
| 3870 | // Save the current value for the break targets. |
| 3871 | // All breaks should go to the code following the loop. |
| 3872 | SaveAndRestore<JumpTarget> save_break(BreakJumpTarget); |
| 3873 | BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos); |
| 3874 | |
| 3875 | // The block for the __begin != __end expression. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 3876 | CFGBlock *ConditionBlock = createBlock(false); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3877 | ConditionBlock->setTerminator(S); |
| 3878 | |
| 3879 | // Now add the actual condition to the condition block. |
| 3880 | if (Expr *C = S->getCond()) { |
| 3881 | Block = ConditionBlock; |
| 3882 | CFGBlock *BeginConditionBlock = addStmt(C); |
| 3883 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3884 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3885 | assert(BeginConditionBlock == ConditionBlock && |
| 3886 | "condition block in for-range was unexpectedly complex"); |
| 3887 | (void)BeginConditionBlock; |
| 3888 | } |
| 3889 | |
| 3890 | // The condition block is the implicit successor for the loop body as well as |
| 3891 | // any code above the loop. |
| 3892 | Succ = ConditionBlock; |
| 3893 | |
| 3894 | // See if this is a known constant. |
| 3895 | TryResult KnownVal(true); |
| 3896 | |
| 3897 | if (S->getCond()) |
| 3898 | KnownVal = tryEvaluateBool(S->getCond()); |
| 3899 | |
| 3900 | // Now create the loop body. |
| 3901 | { |
| 3902 | assert(S->getBody()); |
| 3903 | |
| 3904 | // Save the current values for Block, Succ, and continue targets. |
| 3905 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ); |
| 3906 | SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget); |
| 3907 | |
| 3908 | // Generate increment code in its own basic block. This is the target of |
| 3909 | // continue statements. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3910 | Block = nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3911 | Succ = addStmt(S->getInc()); |
Alexander Kornienko | ff2046a | 2016-07-08 10:50:51 +0000 | [diff] [blame] | 3912 | if (badCFG) |
| 3913 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3914 | ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos); |
| 3915 | |
| 3916 | // The starting block for the loop increment is the block that should |
| 3917 | // represent the 'loop target' for looping back to the start of the loop. |
| 3918 | ContinueJumpTarget.block->setLoopTarget(S); |
| 3919 | |
| 3920 | // Finish up the increment block and prepare to start the loop body. |
| 3921 | assert(Block); |
| 3922 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3923 | return nullptr; |
| 3924 | Block = nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3925 | |
| 3926 | // Add implicit scope and dtors for loop variable. |
| 3927 | addLocalScopeAndDtors(S->getLoopVarStmt()); |
| 3928 | |
| 3929 | // Populate a new block to contain the loop body and loop variable. |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3930 | addStmt(S->getBody()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3931 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3932 | return nullptr; |
Ted Kremenek | e6ee671 | 2012-11-13 00:12:13 +0000 | [diff] [blame] | 3933 | CFGBlock *LoopVarStmtBlock = addStmt(S->getLoopVarStmt()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3934 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3935 | return nullptr; |
| 3936 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3937 | // This new body block is a successor to our condition block. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3938 | addSuccessor(ConditionBlock, |
| 3939 | KnownVal.isFalse() ? nullptr : LoopVarStmtBlock); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
| 3942 | // Link up the condition block with the code that follows the loop (the |
| 3943 | // false branch). |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 3944 | addSuccessor(ConditionBlock, KnownVal.isTrue() ? nullptr : LoopSuccessor); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3945 | |
| 3946 | // Add the initialization statements. |
| 3947 | Block = createBlock(); |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 3948 | addStmt(S->getBeginStmt()); |
| 3949 | addStmt(S->getEndStmt()); |
Richard Smith | 0c502d2 | 2011-04-18 15:49:25 +0000 | [diff] [blame] | 3950 | return addStmt(S->getRangeStmt()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3953 | CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E, |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3954 | AddStmtChoice asc) { |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 3955 | if (BuildOpts.AddTemporaryDtors) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3956 | // If adding implicit destructors visit the full expression for adding |
| 3957 | // destructors of temporaries. |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 3958 | TempDtorContext Context; |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 3959 | VisitForTemporaryDtors(E->getSubExpr(), false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3960 | |
| 3961 | // Full expression has to be added as CFGStmt so it will be sequenced |
| 3962 | // before destructors of it's temporaries. |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 3963 | asc = asc.withAlwaysAdd(true); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 3964 | } |
| 3965 | return Visit(E->getSubExpr(), asc); |
| 3966 | } |
| 3967 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3968 | CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E, |
| 3969 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 3970 | if (asc.alwaysAdd(*this, E)) { |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3971 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 3972 | appendStmt(Block, E); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3973 | |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 3974 | findConstructionContexts( |
| 3975 | ConstructionContext::create(cfg->getBumpVectorContext(), E), |
| 3976 | E->getSubExpr()); |
Artem Dergachev | 1f68d9d | 2018-02-15 03:13:36 +0000 | [diff] [blame] | 3977 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3978 | // We do not want to propagate the AlwaysAdd property. |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 3979 | asc = asc.withAlwaysAdd(false); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 3980 | } |
| 3981 | return Visit(E->getSubExpr(), asc); |
| 3982 | } |
| 3983 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3984 | CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C, |
| 3985 | AddStmtChoice asc) { |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3986 | autoCreateBlock(); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 3987 | appendConstructor(Block, C); |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 3988 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 3989 | return VisitChildren(C); |
| 3990 | } |
| 3991 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 3992 | CFGBlock *CFGBuilder::VisitCXXNewExpr(CXXNewExpr *NE, |
| 3993 | AddStmtChoice asc) { |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 3994 | autoCreateBlock(); |
| 3995 | appendStmt(Block, NE); |
Jordan Rose | 6f5f719 | 2014-01-14 17:29:12 +0000 | [diff] [blame] | 3996 | |
Artem Dergachev | 783a457 | 2018-02-23 22:20:39 +0000 | [diff] [blame] | 3997 | findConstructionContexts( |
| 3998 | ConstructionContext::create(cfg->getBumpVectorContext(), NE), |
| 3999 | const_cast<CXXConstructExpr *>(NE->getConstructExpr())); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4000 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 4001 | if (NE->getInitializer()) |
Jordan Rose | 6f5f719 | 2014-01-14 17:29:12 +0000 | [diff] [blame] | 4002 | Block = Visit(NE->getInitializer()); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4003 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 4004 | if (BuildOpts.AddCXXNewAllocator) |
| 4005 | appendNewAllocator(Block, NE); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4006 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 4007 | if (NE->isArray()) |
Jordan Rose | 6f5f719 | 2014-01-14 17:29:12 +0000 | [diff] [blame] | 4008 | Block = Visit(NE->getArraySize()); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4009 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 4010 | for (CXXNewExpr::arg_iterator I = NE->placement_arg_begin(), |
| 4011 | E = NE->placement_arg_end(); I != E; ++I) |
Jordan Rose | 6f5f719 | 2014-01-14 17:29:12 +0000 | [diff] [blame] | 4012 | Block = Visit(*I); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4013 | |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 4014 | return Block; |
| 4015 | } |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 4016 | |
| 4017 | CFGBlock *CFGBuilder::VisitCXXDeleteExpr(CXXDeleteExpr *DE, |
| 4018 | AddStmtChoice asc) { |
| 4019 | autoCreateBlock(); |
| 4020 | appendStmt(Block, DE); |
| 4021 | QualType DTy = DE->getDestroyedType(); |
Martin Bohme | f44cde8 | 2016-12-05 11:33:19 +0000 | [diff] [blame] | 4022 | if (!DTy.isNull()) { |
| 4023 | DTy = DTy.getNonReferenceType(); |
| 4024 | CXXRecordDecl *RD = Context->getBaseElementType(DTy)->getAsCXXRecordDecl(); |
| 4025 | if (RD) { |
| 4026 | if (RD->isCompleteDefinition() && !RD->hasTrivialDestructor()) |
| 4027 | appendDeleteDtor(Block, RD, DE); |
| 4028 | } |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | return VisitChildren(DE); |
| 4032 | } |
| 4033 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 4034 | CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E, |
| 4035 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 4036 | if (asc.alwaysAdd(*this, E)) { |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 4037 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 4038 | appendStmt(Block, E); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 4039 | // We do not want to propagate the AlwaysAdd property. |
Zhanyong Wan | b5d11c1 | 2010-11-24 03:28:53 +0000 | [diff] [blame] | 4040 | asc = asc.withAlwaysAdd(false); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 4041 | } |
| 4042 | return Visit(E->getSubExpr(), asc); |
| 4043 | } |
| 4044 | |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 4045 | CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C, |
| 4046 | AddStmtChoice asc) { |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 4047 | autoCreateBlock(); |
Artem Dergachev | 1f68d9d | 2018-02-15 03:13:36 +0000 | [diff] [blame] | 4048 | appendConstructor(Block, C); |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 4049 | return VisitChildren(C); |
| 4050 | } |
| 4051 | |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 4052 | CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E, |
| 4053 | AddStmtChoice asc) { |
Ted Kremenek | 7c58d35 | 2011-03-10 01:14:11 +0000 | [diff] [blame] | 4054 | if (asc.alwaysAdd(*this, E)) { |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 4055 | autoCreateBlock(); |
Ted Kremenek | 2866bab | 2011-03-10 01:14:08 +0000 | [diff] [blame] | 4056 | appendStmt(Block, E); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 4057 | } |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 4058 | return Visit(E->getSubExpr(), AddStmtChoice()); |
Zhongxing Xu | e1dbeb2 | 2010-11-01 13:04:58 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4061 | CFGBlock *CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt *I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4062 | // 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] | 4063 | CFGBlock *IBlock = cfg->getIndirectGotoBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4064 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 4065 | if (!IBlock) { |
| 4066 | IBlock = createBlock(false); |
| 4067 | cfg->setIndirectGotoBlock(IBlock); |
| 4068 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4069 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 4070 | // IndirectGoto is a control-flow statement. Thus we stop processing the |
| 4071 | // current block and create a new one. |
Zhongxing Xu | 33dfc07 | 2010-09-06 07:32:31 +0000 | [diff] [blame] | 4072 | if (badCFG) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 4073 | return nullptr; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 4074 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 4075 | Block = createBlock(false); |
| 4076 | Block->setTerminator(I); |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 4077 | addSuccessor(Block, IBlock); |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 4078 | return addStmt(I->getTarget()); |
| 4079 | } |
| 4080 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4081 | CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary, |
| 4082 | TempDtorContext &Context) { |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 4083 | assert(BuildOpts.AddImplicitDtors && BuildOpts.AddTemporaryDtors); |
| 4084 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4085 | tryAgain: |
| 4086 | if (!E) { |
| 4087 | badCFG = true; |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 4088 | return nullptr; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4089 | } |
| 4090 | switch (E->getStmtClass()) { |
| 4091 | default: |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4092 | return VisitChildrenForTemporaryDtors(E, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4093 | |
| 4094 | case Stmt::BinaryOperatorClass: |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4095 | return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E), |
| 4096 | Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4097 | |
| 4098 | case Stmt::CXXBindTemporaryExprClass: |
| 4099 | return VisitCXXBindTemporaryExprForTemporaryDtors( |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4100 | cast<CXXBindTemporaryExpr>(E), BindToTemporary, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4101 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4102 | case Stmt::BinaryConditionalOperatorClass: |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4103 | case Stmt::ConditionalOperatorClass: |
| 4104 | return VisitConditionalOperatorForTemporaryDtors( |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4105 | cast<AbstractConditionalOperator>(E), BindToTemporary, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4106 | |
| 4107 | case Stmt::ImplicitCastExprClass: |
| 4108 | // For implicit cast we want BindToTemporary to be passed further. |
| 4109 | E = cast<CastExpr>(E)->getSubExpr(); |
| 4110 | goto tryAgain; |
| 4111 | |
Manuel Klimek | b0042c4 | 2014-07-30 08:34:42 +0000 | [diff] [blame] | 4112 | case Stmt::CXXFunctionalCastExprClass: |
| 4113 | // For functional cast we want BindToTemporary to be passed further. |
| 4114 | E = cast<CXXFunctionalCastExpr>(E)->getSubExpr(); |
| 4115 | goto tryAgain; |
| 4116 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4117 | case Stmt::ParenExprClass: |
| 4118 | E = cast<ParenExpr>(E)->getSubExpr(); |
| 4119 | goto tryAgain; |
Richard Smith | 4137af2 | 2014-07-27 05:12:49 +0000 | [diff] [blame] | 4120 | |
Manuel Klimek | b0042c4 | 2014-07-30 08:34:42 +0000 | [diff] [blame] | 4121 | case Stmt::MaterializeTemporaryExprClass: { |
| 4122 | const MaterializeTemporaryExpr* MTE = cast<MaterializeTemporaryExpr>(E); |
| 4123 | BindToTemporary = (MTE->getStorageDuration() != SD_FullExpression); |
| 4124 | SmallVector<const Expr *, 2> CommaLHSs; |
| 4125 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 4126 | // Find the expression whose lifetime needs to be extended. |
| 4127 | E = const_cast<Expr *>( |
| 4128 | cast<MaterializeTemporaryExpr>(E) |
| 4129 | ->GetTemporaryExpr() |
| 4130 | ->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 4131 | // Visit the skipped comma operator left-hand sides for other temporaries. |
| 4132 | for (const Expr *CommaLHS : CommaLHSs) { |
| 4133 | VisitForTemporaryDtors(const_cast<Expr *>(CommaLHS), |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4134 | /*BindToTemporary=*/false, Context); |
Manuel Klimek | b0042c4 | 2014-07-30 08:34:42 +0000 | [diff] [blame] | 4135 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4136 | goto tryAgain; |
Manuel Klimek | b0042c4 | 2014-07-30 08:34:42 +0000 | [diff] [blame] | 4137 | } |
Richard Smith | 4137af2 | 2014-07-27 05:12:49 +0000 | [diff] [blame] | 4138 | |
| 4139 | case Stmt::BlockExprClass: |
| 4140 | // Don't recurse into blocks; their subexpressions don't get evaluated |
| 4141 | // here. |
| 4142 | return Block; |
| 4143 | |
| 4144 | case Stmt::LambdaExprClass: { |
| 4145 | // For lambda expressions, only recurse into the capture initializers, |
| 4146 | // and not the body. |
| 4147 | auto *LE = cast<LambdaExpr>(E); |
| 4148 | CFGBlock *B = Block; |
| 4149 | for (Expr *Init : LE->capture_inits()) { |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4150 | if (CFGBlock *R = VisitForTemporaryDtors( |
| 4151 | Init, /*BindToTemporary=*/false, Context)) |
Richard Smith | 4137af2 | 2014-07-27 05:12:49 +0000 | [diff] [blame] | 4152 | B = R; |
| 4153 | } |
| 4154 | return B; |
| 4155 | } |
| 4156 | |
| 4157 | case Stmt::CXXDefaultArgExprClass: |
| 4158 | E = cast<CXXDefaultArgExpr>(E)->getExpr(); |
| 4159 | goto tryAgain; |
| 4160 | |
| 4161 | case Stmt::CXXDefaultInitExprClass: |
| 4162 | E = cast<CXXDefaultInitExpr>(E)->getExpr(); |
| 4163 | goto tryAgain; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4167 | CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E, |
| 4168 | TempDtorContext &Context) { |
| 4169 | if (isa<LambdaExpr>(E)) { |
| 4170 | // Do not visit the children of lambdas; they have their own CFGs. |
| 4171 | return Block; |
| 4172 | } |
| 4173 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4174 | // When visiting children for destructors we want to visit them in reverse |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 4175 | // order that they will appear in the CFG. Because the CFG is built |
| 4176 | // bottom-up, this means we visit them in their natural order, which |
| 4177 | // reverses them in the CFG. |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4178 | CFGBlock *B = Block; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 4179 | for (Stmt *Child : E->children()) |
| 4180 | if (Child) |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4181 | if (CFGBlock *R = VisitForTemporaryDtors(Child, false, Context)) |
Ted Kremenek | 8ae6787 | 2013-02-05 22:00:19 +0000 | [diff] [blame] | 4182 | B = R; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 4183 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4184 | return B; |
| 4185 | } |
| 4186 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4187 | CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors( |
| 4188 | BinaryOperator *E, TempDtorContext &Context) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4189 | if (E->isLogicalOp()) { |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4190 | VisitForTemporaryDtors(E->getLHS(), false, Context); |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 4191 | TryResult RHSExecuted = tryEvaluateBool(E->getLHS()); |
| 4192 | if (RHSExecuted.isKnown() && E->getOpcode() == BO_LOr) |
| 4193 | RHSExecuted.negate(); |
Manuel Klimek | 7c03013 | 2014-08-07 16:05:51 +0000 | [diff] [blame] | 4194 | |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 4195 | // We do not know at CFG-construction time whether the right-hand-side was |
| 4196 | // executed, thus we add a branch node that depends on the temporary |
| 4197 | // constructor call. |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4198 | TempDtorContext RHSContext( |
| 4199 | bothKnownTrue(Context.KnownExecuted, RHSExecuted)); |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 4200 | VisitForTemporaryDtors(E->getRHS(), false, RHSContext); |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4201 | InsertTempDtorDecisionBlock(RHSContext); |
Manuel Klimek | 7c03013 | 2014-08-07 16:05:51 +0000 | [diff] [blame] | 4202 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4203 | return Block; |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 4206 | if (E->isAssignmentOp()) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4207 | // For assignment operator (=) LHS expression is visited |
| 4208 | // before RHS expression. For destructors visit them in reverse order. |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4209 | CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), false, Context); |
| 4210 | CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS(), false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4211 | return LHSBlock ? LHSBlock : RHSBlock; |
| 4212 | } |
| 4213 | |
| 4214 | // For any other binary operator RHS expression is visited before |
| 4215 | // LHS expression (order of children). For destructors visit them in reverse |
| 4216 | // order. |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4217 | CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS(), false, Context); |
| 4218 | CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), false, Context); |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4219 | return RHSBlock ? RHSBlock : LHSBlock; |
| 4220 | } |
| 4221 | |
| 4222 | CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors( |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4223 | CXXBindTemporaryExpr *E, bool BindToTemporary, TempDtorContext &Context) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4224 | // First add destructors for temporaries in subexpression. |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4225 | CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr(), false, Context); |
Zhongxing Xu | fee455f | 2010-11-14 15:23:50 +0000 | [diff] [blame] | 4226 | if (!BindToTemporary) { |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4227 | // If lifetime of temporary is not prolonged (by assigning to constant |
| 4228 | // reference) add destructor for it. |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 4229 | |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 4230 | const CXXDestructorDecl *Dtor = E->getTemporary()->getDestructor(); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4231 | |
Richard Trieu | 95a192a | 2015-05-28 00:14:02 +0000 | [diff] [blame] | 4232 | if (Dtor->getParent()->isAnyDestructorNoReturn()) { |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4233 | // If the destructor is marked as a no-return destructor, we need to |
| 4234 | // create a new block for the destructor which does not have as a |
| 4235 | // successor anything built thus far. Control won't flow out of this |
| 4236 | // block. |
| 4237 | if (B) Succ = B; |
Chandler Carruth | a70991b | 2011-09-13 09:13:49 +0000 | [diff] [blame] | 4238 | Block = createNoReturnBlock(); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4239 | } else if (Context.needsTempDtorBranch()) { |
| 4240 | // If we need to introduce a branch, we add a new block that we will hook |
| 4241 | // up to a decision block later. |
| 4242 | if (B) Succ = B; |
| 4243 | Block = createBlock(); |
Ted Kremenek | ff909f9 | 2014-03-08 02:22:25 +0000 | [diff] [blame] | 4244 | } else { |
Chandler Carruth | ad74725 | 2011-09-13 06:09:01 +0000 | [diff] [blame] | 4245 | autoCreateBlock(); |
Ted Kremenek | ff909f9 | 2014-03-08 02:22:25 +0000 | [diff] [blame] | 4246 | } |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4247 | if (Context.needsTempDtorBranch()) { |
| 4248 | Context.setDecisionPoint(Succ, E); |
| 4249 | } |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 4250 | appendTemporaryDtor(Block, E); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4251 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4252 | B = Block; |
| 4253 | } |
| 4254 | return B; |
| 4255 | } |
| 4256 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4257 | void CFGBuilder::InsertTempDtorDecisionBlock(const TempDtorContext &Context, |
| 4258 | CFGBlock *FalseSucc) { |
| 4259 | if (!Context.TerminatorExpr) { |
| 4260 | // If no temporary was found, we do not need to insert a decision point. |
| 4261 | return; |
| 4262 | } |
| 4263 | assert(Context.TerminatorExpr); |
| 4264 | CFGBlock *Decision = createBlock(false); |
| 4265 | Decision->setTerminator(CFGTerminator(Context.TerminatorExpr, true)); |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4266 | addSuccessor(Decision, Block, !Context.KnownExecuted.isFalse()); |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 4267 | addSuccessor(Decision, FalseSucc ? FalseSucc : Context.Succ, |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4268 | !Context.KnownExecuted.isTrue()); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4269 | Block = Decision; |
| 4270 | } |
| 4271 | |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4272 | CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors( |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4273 | AbstractConditionalOperator *E, bool BindToTemporary, |
| 4274 | TempDtorContext &Context) { |
| 4275 | VisitForTemporaryDtors(E->getCond(), false, Context); |
| 4276 | CFGBlock *ConditionBlock = Block; |
| 4277 | CFGBlock *ConditionSucc = Succ; |
Manuel Klimek | 0ce9108 | 2014-08-07 14:25:43 +0000 | [diff] [blame] | 4278 | TryResult ConditionVal = tryEvaluateBool(E->getCond()); |
Manuel Klimek | edf925b9 | 2014-08-07 18:44:19 +0000 | [diff] [blame] | 4279 | TryResult NegatedVal = ConditionVal; |
| 4280 | if (NegatedVal.isKnown()) NegatedVal.negate(); |
Manuel Klimek | cadc603 | 2014-08-07 17:02:21 +0000 | [diff] [blame] | 4281 | |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4282 | TempDtorContext TrueContext( |
| 4283 | bothKnownTrue(Context.KnownExecuted, ConditionVal)); |
Manuel Klimek | cadc603 | 2014-08-07 17:02:21 +0000 | [diff] [blame] | 4284 | VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary, TrueContext); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4285 | CFGBlock *TrueBlock = Block; |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 4286 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4287 | Block = ConditionBlock; |
| 4288 | Succ = ConditionSucc; |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4289 | TempDtorContext FalseContext( |
| 4290 | bothKnownTrue(Context.KnownExecuted, NegatedVal)); |
Manuel Klimek | cadc603 | 2014-08-07 17:02:21 +0000 | [diff] [blame] | 4291 | VisitForTemporaryDtors(E->getFalseExpr(), BindToTemporary, FalseContext); |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 4292 | |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4293 | if (TrueContext.TerminatorExpr && FalseContext.TerminatorExpr) { |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4294 | InsertTempDtorDecisionBlock(FalseContext, TrueBlock); |
Manuel Klimek | b5616c9 | 2014-08-07 10:42:17 +0000 | [diff] [blame] | 4295 | } else if (TrueContext.TerminatorExpr) { |
| 4296 | Block = TrueBlock; |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4297 | InsertTempDtorDecisionBlock(TrueContext); |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 4298 | } else { |
Manuel Klimek | deb0262 | 2014-08-08 07:37:13 +0000 | [diff] [blame] | 4299 | InsertTempDtorDecisionBlock(FalseContext); |
Rui Ueyama | a89f9c8 | 2014-08-06 22:01:54 +0000 | [diff] [blame] | 4300 | } |
Marcin Swiderski | 3ab17ad | 2010-11-03 06:19:35 +0000 | [diff] [blame] | 4301 | return Block; |
| 4302 | } |
| 4303 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4304 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has |
| 4305 | /// no successors or predecessors. If this is the first block created in the |
| 4306 | /// 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] | 4307 | CFGBlock *CFG::createBlock() { |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 4308 | bool first_block = begin() == end(); |
| 4309 | |
| 4310 | // Create the block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 4311 | CFGBlock *Mem = getAllocator().Allocate<CFGBlock>(); |
Anna Zaks | 02a1fc1 | 2011-12-05 21:33:11 +0000 | [diff] [blame] | 4312 | new (Mem) CFGBlock(NumBlockIDs++, BlkBVC, this); |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 4313 | Blocks.push_back(Mem, BlkBVC); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 4314 | |
| 4315 | // 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] | 4316 | if (first_block) |
| 4317 | Entry = Exit = &back(); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 4318 | |
| 4319 | // Return the block. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 4320 | return &back(); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 4323 | /// buildCFG - Constructs a CFG from an AST. |
| 4324 | std::unique_ptr<CFG> CFG::buildCFG(const Decl *D, Stmt *Statement, |
| 4325 | ASTContext *C, const BuildOptions &BO) { |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 4326 | CFGBuilder Builder(C, BO); |
| 4327 | return Builder.buildCFG(D, Statement); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Ted Kremenek | 8cfe207 | 2011-03-03 01:21:32 +0000 | [diff] [blame] | 4330 | const CXXDestructorDecl * |
| 4331 | CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const { |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4332 | switch (getKind()) { |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4333 | case CFGElement::Initializer: |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 4334 | case CFGElement::NewAllocator: |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 4335 | case CFGElement::LoopExit: |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 4336 | case CFGElement::LifetimeEnds: |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4337 | case CFGElement::Statement: |
| 4338 | case CFGElement::Constructor: |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4339 | llvm_unreachable("getDestructorDecl should only be used with " |
| 4340 | "ImplicitDtors"); |
| 4341 | case CFGElement::AutomaticObjectDtor: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 4342 | const VarDecl *var = castAs<CFGAutomaticObjDtor>().getVarDecl(); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4343 | QualType ty = var->getType(); |
Devin Coughlin | 6eb1ca7 | 2016-08-02 21:07:23 +0000 | [diff] [blame] | 4344 | |
| 4345 | // FIXME: See CFGBuilder::addLocalScopeForVarDecl. |
| 4346 | // |
| 4347 | // Lifetime-extending constructs are handled here. This works for a single |
| 4348 | // temporary in an initializer expression. |
| 4349 | if (ty->isReferenceType()) { |
| 4350 | if (const Expr *Init = var->getInit()) { |
| 4351 | ty = getReferenceInitTemporaryType(astContext, Init); |
| 4352 | } |
| 4353 | } |
| 4354 | |
Ted Kremenek | e7d7888 | 2012-03-19 23:48:41 +0000 | [diff] [blame] | 4355 | while (const ArrayType *arrayType = astContext.getAsArrayType(ty)) { |
Ted Kremenek | 8cfe207 | 2011-03-03 01:21:32 +0000 | [diff] [blame] | 4356 | ty = arrayType->getElementType(); |
| 4357 | } |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4358 | const RecordType *recordType = ty->getAs<RecordType>(); |
| 4359 | const CXXRecordDecl *classDecl = |
Ted Kremenek | 1676a04 | 2011-03-03 01:01:03 +0000 | [diff] [blame] | 4360 | cast<CXXRecordDecl>(recordType->getDecl()); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4361 | return classDecl->getDestructor(); |
| 4362 | } |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 4363 | case CFGElement::DeleteDtor: { |
| 4364 | const CXXDeleteExpr *DE = castAs<CFGDeleteDtor>().getDeleteExpr(); |
| 4365 | QualType DTy = DE->getDestroyedType(); |
| 4366 | DTy = DTy.getNonReferenceType(); |
| 4367 | const CXXRecordDecl *classDecl = |
| 4368 | astContext.getBaseElementType(DTy)->getAsCXXRecordDecl(); |
| 4369 | return classDecl->getDestructor(); |
| 4370 | } |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4371 | case CFGElement::TemporaryDtor: { |
| 4372 | const CXXBindTemporaryExpr *bindExpr = |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 4373 | castAs<CFGTemporaryDtor>().getBindTemporaryExpr(); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4374 | const CXXTemporary *temp = bindExpr->getTemporary(); |
| 4375 | return temp->getDestructor(); |
| 4376 | } |
| 4377 | case CFGElement::BaseDtor: |
| 4378 | case CFGElement::MemberDtor: |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4379 | // Not yet supported. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 4380 | return nullptr; |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4381 | } |
Ted Kremenek | 1676a04 | 2011-03-03 01:01:03 +0000 | [diff] [blame] | 4382 | llvm_unreachable("getKind() returned bogus value"); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Ted Kremenek | 8cfe207 | 2011-03-03 01:21:32 +0000 | [diff] [blame] | 4385 | bool CFGImplicitDtor::isNoReturn(ASTContext &astContext) const { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4386 | if (const CXXDestructorDecl *DD = getDestructorDecl(astContext)) |
| 4387 | return DD->isNoReturn(); |
Ted Kremenek | e06a55c | 2011-03-02 20:32:29 +0000 | [diff] [blame] | 4388 | return false; |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 4391 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4392 | // CFGBlock operations. |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 4393 | //===----------------------------------------------------------------------===// |
| 4394 | |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4395 | CFGBlock::AdjacentBlock::AdjacentBlock(CFGBlock *B, bool IsReachable) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4396 | : ReachableBlock(IsReachable ? B : nullptr), |
| 4397 | UnreachableBlock(!IsReachable ? B : nullptr, |
| 4398 | B && IsReachable ? AB_Normal : AB_Unreachable) {} |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4399 | |
| 4400 | CFGBlock::AdjacentBlock::AdjacentBlock(CFGBlock *B, CFGBlock *AlternateBlock) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4401 | : ReachableBlock(B), |
| 4402 | UnreachableBlock(B == AlternateBlock ? nullptr : AlternateBlock, |
| 4403 | B == AlternateBlock ? AB_Alternate : AB_Normal) {} |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4404 | |
| 4405 | void CFGBlock::addSuccessor(AdjacentBlock Succ, |
| 4406 | BumpVectorContext &C) { |
| 4407 | if (CFGBlock *B = Succ.getReachableBlock()) |
David Blaikie | 9afd5da | 2014-03-04 23:39:18 +0000 | [diff] [blame] | 4408 | B->Preds.push_back(AdjacentBlock(this, Succ.isReachable()), C); |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4409 | |
| 4410 | if (CFGBlock *UnreachableB = Succ.getPossiblyUnreachableBlock()) |
David Blaikie | 9afd5da | 2014-03-04 23:39:18 +0000 | [diff] [blame] | 4411 | UnreachableB->Preds.push_back(AdjacentBlock(this, false), C); |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4412 | |
| 4413 | Succs.push_back(Succ, C); |
| 4414 | } |
| 4415 | |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 4416 | bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F, |
Ted Kremenek | f146cd1 | 2010-09-09 02:57:48 +0000 | [diff] [blame] | 4417 | const CFGBlock *From, const CFGBlock *To) { |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4418 | if (F.IgnoreNullPredecessors && !From) |
| 4419 | return true; |
| 4420 | |
| 4421 | if (To && From && F.IgnoreDefaultsWithCoveredEnums) { |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 4422 | // If the 'To' has no label or is labeled but the label isn't a |
| 4423 | // CaseStmt then filter this edge. |
| 4424 | if (const SwitchStmt *S = |
Ted Kremenek | 8979474 | 2011-03-07 22:04:39 +0000 | [diff] [blame] | 4425 | dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) { |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 4426 | if (S->isAllEnumCasesCovered()) { |
Ted Kremenek | 8979474 | 2011-03-07 22:04:39 +0000 | [diff] [blame] | 4427 | const Stmt *L = To->getLabel(); |
| 4428 | if (!L || !isa<CaseStmt>(L)) |
| 4429 | return true; |
Ted Kremenek | b037185 | 2010-09-09 00:06:04 +0000 | [diff] [blame] | 4430 | } |
| 4431 | } |
| 4432 | } |
| 4433 | |
| 4434 | return false; |
| 4435 | } |
| 4436 | |
| 4437 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4438 | // CFG pretty printing |
| 4439 | //===----------------------------------------------------------------------===// |
| 4440 | |
Ted Kremenek | 7e776b1 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 4441 | namespace { |
| 4442 | |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 4443 | class StmtPrinterHelper : public PrinterHelper { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4444 | using StmtMapTy = llvm::DenseMap<const Stmt *, std::pair<unsigned, unsigned>>; |
| 4445 | using DeclMapTy = llvm::DenseMap<const Decl *, std::pair<unsigned, unsigned>>; |
| 4446 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4447 | StmtMapTy StmtMap; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4448 | DeclMapTy DeclMap; |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4449 | signed currentBlock = 0; |
| 4450 | unsigned currStmt = 0; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4451 | const LangOptions &LangOpts; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4452 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4453 | public: |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4454 | StmtPrinterHelper(const CFG* cfg, const LangOptions &LO) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4455 | : LangOpts(LO) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4456 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { |
| 4457 | unsigned j = 1; |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 4458 | for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4459 | BI != BEnd; ++BI, ++j ) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4460 | if (Optional<CFGStmt> SE = BI->getAs<CFGStmt>()) { |
| 4461 | const Stmt *stmt= SE->getStmt(); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4462 | std::pair<unsigned, unsigned> P((*I)->getBlockID(), j); |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 4463 | StmtMap[stmt] = P; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4464 | |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 4465 | switch (stmt->getStmtClass()) { |
| 4466 | case Stmt::DeclStmtClass: |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4467 | DeclMap[cast<DeclStmt>(stmt)->getSingleDecl()] = P; |
| 4468 | break; |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 4469 | case Stmt::IfStmtClass: { |
| 4470 | const VarDecl *var = cast<IfStmt>(stmt)->getConditionVariable(); |
| 4471 | if (var) |
| 4472 | DeclMap[var] = P; |
| 4473 | break; |
| 4474 | } |
| 4475 | case Stmt::ForStmtClass: { |
| 4476 | const VarDecl *var = cast<ForStmt>(stmt)->getConditionVariable(); |
| 4477 | if (var) |
| 4478 | DeclMap[var] = P; |
| 4479 | break; |
| 4480 | } |
| 4481 | case Stmt::WhileStmtClass: { |
| 4482 | const VarDecl *var = |
| 4483 | cast<WhileStmt>(stmt)->getConditionVariable(); |
| 4484 | if (var) |
| 4485 | DeclMap[var] = P; |
| 4486 | break; |
| 4487 | } |
| 4488 | case Stmt::SwitchStmtClass: { |
| 4489 | const VarDecl *var = |
| 4490 | cast<SwitchStmt>(stmt)->getConditionVariable(); |
| 4491 | if (var) |
| 4492 | DeclMap[var] = P; |
| 4493 | break; |
| 4494 | } |
| 4495 | case Stmt::CXXCatchStmtClass: { |
| 4496 | const VarDecl *var = |
| 4497 | cast<CXXCatchStmt>(stmt)->getExceptionDecl(); |
| 4498 | if (var) |
| 4499 | DeclMap[var] = P; |
| 4500 | break; |
| 4501 | } |
| 4502 | default: |
| 4503 | break; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4504 | } |
| 4505 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4506 | } |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 4507 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4508 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4509 | |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4510 | ~StmtPrinterHelper() override = default; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4511 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4512 | const LangOptions &getLangOpts() const { return LangOpts; } |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 4513 | void setBlockID(signed i) { currentBlock = i; } |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 4514 | void setStmtID(unsigned i) { currStmt = i; } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4515 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 4516 | bool handledStmt(Stmt *S, raw_ostream &OS) override { |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4517 | StmtMapTy::iterator I = StmtMap.find(S); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4518 | |
| 4519 | if (I == StmtMap.end()) |
| 4520 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4521 | |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 4522 | if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 4523 | && I->second.second == currStmt) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4524 | return false; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4525 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4526 | |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4527 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4528 | return true; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4529 | } |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4530 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4531 | bool handleDecl(const Decl *D, raw_ostream &OS) { |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4532 | DeclMapTy::iterator I = DeclMap.find(D); |
| 4533 | |
| 4534 | if (I == DeclMap.end()) |
| 4535 | return false; |
| 4536 | |
Ted Kremenek | 3a9a2a5 | 2010-12-17 04:44:39 +0000 | [diff] [blame] | 4537 | if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock |
Ted Kremenek | d94854a | 2012-08-22 06:26:15 +0000 | [diff] [blame] | 4538 | && I->second.second == currStmt) { |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4539 | return false; |
| 4540 | } |
| 4541 | |
| 4542 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
| 4543 | return true; |
| 4544 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4545 | }; |
| 4546 | |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 4547 | class CFGBlockTerminatorPrint |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4548 | : public StmtVisitor<CFGBlockTerminatorPrint,void> { |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4549 | raw_ostream &OS; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4550 | StmtPrinterHelper* Helper; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4551 | PrintingPolicy Policy; |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4552 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4553 | public: |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4554 | CFGBlockTerminatorPrint(raw_ostream &os, StmtPrinterHelper* helper, |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4555 | const PrintingPolicy &Policy) |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4556 | : OS(os), Helper(helper), Policy(Policy) { |
Ted Kremenek | 5d0fb1e | 2013-12-11 23:44:05 +0000 | [diff] [blame] | 4557 | this->Policy.IncludeNewlines = false; |
| 4558 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4559 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4560 | void VisitIfStmt(IfStmt *I) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4561 | OS << "if "; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4562 | if (Stmt *C = I->getCond()) |
| 4563 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4564 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4565 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4566 | // Default case. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4567 | void VisitStmt(Stmt *Terminator) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4568 | Terminator->printPretty(OS, Helper, Policy); |
| 4569 | } |
| 4570 | |
Ted Kremenek | 0dd8fee | 2013-03-28 18:43:15 +0000 | [diff] [blame] | 4571 | void VisitDeclStmt(DeclStmt *DS) { |
| 4572 | VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 4573 | OS << "static init " << VD->getName(); |
| 4574 | } |
| 4575 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4576 | void VisitForStmt(ForStmt *F) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4577 | OS << "for (" ; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4578 | if (F->getInit()) |
| 4579 | OS << "..."; |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 4580 | OS << "; "; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4581 | if (Stmt *C = F->getCond()) |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4582 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 4583 | OS << "; "; |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4584 | if (F->getInc()) |
| 4585 | OS << "..."; |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4586 | OS << ")"; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4587 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4588 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4589 | void VisitWhileStmt(WhileStmt *W) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4590 | OS << "while " ; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4591 | if (Stmt *C = W->getCond()) |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4592 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4593 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4594 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4595 | void VisitDoStmt(DoStmt *D) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4596 | OS << "do ... while "; |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4597 | if (Stmt *C = D->getCond()) |
Ted Kremenek | 60983dc | 2010-01-19 20:52:05 +0000 | [diff] [blame] | 4598 | C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 4599 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4600 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4601 | void VisitSwitchStmt(SwitchStmt *Terminator) { |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 4602 | OS << "switch "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4603 | Terminator->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 4604 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4605 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4606 | void VisitCXXTryStmt(CXXTryStmt *CS) { |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 4607 | OS << "try ..."; |
| 4608 | } |
| 4609 | |
Nico Weber | 699670e | 2017-08-23 15:33:16 +0000 | [diff] [blame] | 4610 | void VisitSEHTryStmt(SEHTryStmt *CS) { |
| 4611 | OS << "__try ..."; |
| 4612 | } |
| 4613 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4614 | void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) { |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4615 | if (Stmt *Cond = C->getCond()) |
| 4616 | Cond->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4617 | OS << " ? ... : ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4618 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4619 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4620 | void VisitChooseExpr(ChooseExpr *C) { |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 4621 | OS << "__builtin_choose_expr( "; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4622 | if (Stmt *Cond = C->getCond()) |
| 4623 | Cond->printPretty(OS, Helper, Policy); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4624 | OS << " )"; |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 4625 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4626 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4627 | void VisitIndirectGotoStmt(IndirectGotoStmt *I) { |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4628 | OS << "goto *"; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4629 | if (Stmt *T = I->getTarget()) |
| 4630 | T->printPretty(OS, Helper, Policy); |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4631 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4632 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4633 | void VisitBinaryOperator(BinaryOperator* B) { |
| 4634 | if (!B->isLogicalOp()) { |
| 4635 | VisitExpr(B); |
| 4636 | return; |
| 4637 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4638 | |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4639 | if (B->getLHS()) |
| 4640 | B->getLHS()->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4641 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4642 | switch (B->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4643 | case BO_LOr: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4644 | OS << " || ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4645 | return; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4646 | case BO_LAnd: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4647 | OS << " && ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4648 | return; |
| 4649 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4650 | llvm_unreachable("Invalid logical operator."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4651 | } |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 4652 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4653 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4654 | void VisitExpr(Expr *E) { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 4655 | E->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4656 | } |
Ted Kremenek | fcc1417 | 2014-03-08 02:22:29 +0000 | [diff] [blame] | 4657 | |
| 4658 | public: |
| 4659 | void print(CFGTerminator T) { |
| 4660 | if (T.isTemporaryDtorsBranch()) |
| 4661 | OS << "(Temp Dtor) "; |
| 4662 | Visit(T.getStmt()); |
| 4663 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 4664 | }; |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 4665 | |
| 4666 | } // namespace |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4667 | |
Artem Dergachev | 5a281bb | 2018-02-10 02:18:04 +0000 | [diff] [blame] | 4668 | static void print_initializer(raw_ostream &OS, StmtPrinterHelper &Helper, |
| 4669 | const CXXCtorInitializer *I) { |
| 4670 | if (I->isBaseInitializer()) |
| 4671 | OS << I->getBaseClass()->getAsCXXRecordDecl()->getName(); |
| 4672 | else if (I->isDelegatingInitializer()) |
| 4673 | OS << I->getTypeSourceInfo()->getType()->getAsCXXRecordDecl()->getName(); |
| 4674 | else |
| 4675 | OS << I->getAnyMember()->getName(); |
| 4676 | OS << "("; |
| 4677 | if (Expr *IE = I->getInit()) |
| 4678 | IE->printPretty(OS, &Helper, PrintingPolicy(Helper.getLangOpts())); |
| 4679 | OS << ")"; |
| 4680 | |
| 4681 | if (I->isBaseInitializer()) |
| 4682 | OS << " (Base initializer)"; |
| 4683 | else if (I->isDelegatingInitializer()) |
| 4684 | OS << " (Delegating initializer)"; |
| 4685 | else |
| 4686 | OS << " (Member initializer)"; |
| 4687 | } |
| 4688 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4689 | static void print_elem(raw_ostream &OS, StmtPrinterHelper &Helper, |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 4690 | const CFGElement &E) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4691 | if (Optional<CFGStmt> CS = E.getAs<CFGStmt>()) { |
| 4692 | const Stmt *S = CS->getStmt(); |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4693 | assert(S != nullptr && "Expecting non-null Stmt"); |
| 4694 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4695 | // special printing for statement-expressions. |
| 4696 | if (const StmtExpr *SE = dyn_cast<StmtExpr>(S)) { |
| 4697 | const CompoundStmt *Sub = SE->getSubStmt(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4698 | |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 4699 | auto Children = Sub->children(); |
| 4700 | if (Children.begin() != Children.end()) { |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4701 | OS << "({ ... ; "; |
| 4702 | Helper.handledStmt(*SE->getSubStmt()->body_rbegin(),OS); |
| 4703 | OS << " })\n"; |
| 4704 | return; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 4705 | } |
| 4706 | } |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4707 | // special printing for comma expressions. |
| 4708 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 4709 | if (B->getOpcode() == BO_Comma) { |
| 4710 | OS << "... , "; |
| 4711 | Helper.handledStmt(B->getRHS(),OS); |
| 4712 | OS << '\n'; |
| 4713 | return; |
| 4714 | } |
| 4715 | } |
| 4716 | S->printPretty(OS, &Helper, PrintingPolicy(Helper.getLangOpts())); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4717 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4718 | if (isa<CXXOperatorCallExpr>(S)) { |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 4719 | OS << " (OperatorCall)"; |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4720 | } else if (isa<CXXBindTemporaryExpr>(S)) { |
Zhanyong Wan | 59f09c7 | 2010-11-22 19:32:14 +0000 | [diff] [blame] | 4721 | OS << " (BindTemporary)"; |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4722 | } else if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(S)) { |
| 4723 | OS << " (CXXConstructExpr, "; |
| 4724 | if (Optional<CFGConstructor> CE = E.getAs<CFGConstructor>()) { |
Artem Dergachev | f43ac4c | 2018-02-24 02:00:30 +0000 | [diff] [blame^] | 4725 | // TODO: Refactor into ConstructionContext::print(). |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4726 | if (const Stmt *S = CE->getTriggerStmt()) |
Artem Dergachev | f43ac4c | 2018-02-24 02:00:30 +0000 | [diff] [blame^] | 4727 | Helper.handledStmt(const_cast<Stmt *>(S), OS); |
Artem Dergachev | 5a281bb | 2018-02-10 02:18:04 +0000 | [diff] [blame] | 4728 | else if (const CXXCtorInitializer *I = CE->getTriggerInit()) |
| 4729 | print_initializer(OS, Helper, I); |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4730 | else |
| 4731 | llvm_unreachable("Unexpected trigger kind!"); |
| 4732 | OS << ", "; |
Artem Dergachev | f43ac4c | 2018-02-24 02:00:30 +0000 | [diff] [blame^] | 4733 | if (const Stmt *S = CE->getMaterializedTemporary()) { |
| 4734 | if (S != CE->getTriggerStmt()) { |
| 4735 | Helper.handledStmt(const_cast<Stmt *>(S), OS); |
| 4736 | OS << ", "; |
| 4737 | } |
| 4738 | } |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 4739 | } |
| 4740 | OS << CCE->getType().getAsString() << ")"; |
| 4741 | } else if (const CastExpr *CE = dyn_cast<CastExpr>(S)) { |
Ted Kremenek | 0ffba93 | 2011-12-21 19:32:38 +0000 | [diff] [blame] | 4742 | OS << " (" << CE->getStmtClassName() << ", " |
| 4743 | << CE->getCastKindName() |
| 4744 | << ", " << CE->getType().getAsString() |
| 4745 | << ")"; |
| 4746 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4747 | |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4748 | // Expressions need a newline. |
| 4749 | if (isa<Expr>(S)) |
| 4750 | OS << '\n'; |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4751 | } else if (Optional<CFGInitializer> IE = E.getAs<CFGInitializer>()) { |
Artem Dergachev | 5a281bb | 2018-02-10 02:18:04 +0000 | [diff] [blame] | 4752 | print_initializer(OS, Helper, IE->getInitializer()); |
| 4753 | OS << '\n'; |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4754 | } else if (Optional<CFGAutomaticObjDtor> DE = |
| 4755 | E.getAs<CFGAutomaticObjDtor>()) { |
| 4756 | const VarDecl *VD = DE->getVarDecl(); |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4757 | Helper.handleDecl(VD, OS); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4758 | |
Marcin Swiderski | 52e4bc1 | 2010-10-25 07:00:40 +0000 | [diff] [blame] | 4759 | const Type* T = VD->getType().getTypePtr(); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4760 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) |
| 4761 | T = RT->getPointeeType().getTypePtr(); |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 4762 | T = T->getBaseElementTypeUnsafe(); |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4763 | |
| 4764 | OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()"; |
| 4765 | OS << " (Implicit destructor)\n"; |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 4766 | } else if (Optional<CFGLifetimeEnds> DE = E.getAs<CFGLifetimeEnds>()) { |
| 4767 | const VarDecl *VD = DE->getVarDecl(); |
| 4768 | Helper.handleDecl(VD, OS); |
| 4769 | |
| 4770 | OS << " (Lifetime ends)\n"; |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 4771 | } else if (Optional<CFGLoopExit> LE = E.getAs<CFGLoopExit>()) { |
| 4772 | const Stmt *LoopStmt = LE->getLoopStmt(); |
| 4773 | OS << LoopStmt->getStmtClassName() << " (LoopExit)\n"; |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 4774 | } else if (Optional<CFGNewAllocator> NE = E.getAs<CFGNewAllocator>()) { |
| 4775 | OS << "CFGNewAllocator("; |
| 4776 | if (const CXXNewExpr *AllocExpr = NE->getAllocatorExpr()) |
| 4777 | AllocExpr->getType().print(OS, PrintingPolicy(Helper.getLangOpts())); |
| 4778 | OS << ")\n"; |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 4779 | } else if (Optional<CFGDeleteDtor> DE = E.getAs<CFGDeleteDtor>()) { |
| 4780 | const CXXRecordDecl *RD = DE->getCXXRecordDecl(); |
| 4781 | if (!RD) |
| 4782 | return; |
| 4783 | CXXDeleteExpr *DelExpr = |
| 4784 | const_cast<CXXDeleteExpr*>(DE->getDeleteExpr()); |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4785 | Helper.handledStmt(cast<Stmt>(DelExpr->getArgument()), OS); |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 4786 | OS << "->~" << RD->getName().str() << "()"; |
| 4787 | OS << " (Implicit destructor)\n"; |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4788 | } else if (Optional<CFGBaseDtor> BE = E.getAs<CFGBaseDtor>()) { |
| 4789 | const CXXBaseSpecifier *BS = BE->getBaseSpecifier(); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 4790 | OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()"; |
Zhongxing Xu | 614e17d | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 4791 | OS << " (Base object destructor)\n"; |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4792 | } else if (Optional<CFGMemberDtor> ME = E.getAs<CFGMemberDtor>()) { |
| 4793 | const FieldDecl *FD = ME->getFieldDecl(); |
Richard Smith | f676e45 | 2012-07-24 21:02:14 +0000 | [diff] [blame] | 4794 | const Type *T = FD->getType()->getBaseElementTypeUnsafe(); |
Marcin Swiderski | 20b8873 | 2010-10-05 05:37:00 +0000 | [diff] [blame] | 4795 | OS << "this->" << FD->getName(); |
Marcin Swiderski | 0176990 | 2010-10-25 07:05:54 +0000 | [diff] [blame] | 4796 | OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()"; |
Zhongxing Xu | 614e17d | 2010-10-05 08:38:06 +0000 | [diff] [blame] | 4797 | OS << " (Member object destructor)\n"; |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 4798 | } else if (Optional<CFGTemporaryDtor> TE = E.getAs<CFGTemporaryDtor>()) { |
| 4799 | const CXXBindTemporaryExpr *BT = TE->getBindTemporaryExpr(); |
Pavel Labath | d527cf8 | 2013-09-02 09:09:15 +0000 | [diff] [blame] | 4800 | OS << "~"; |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4801 | BT->getType().print(OS, PrintingPolicy(Helper.getLangOpts())); |
Pavel Labath | d527cf8 | 2013-09-02 09:09:15 +0000 | [diff] [blame] | 4802 | OS << "() (Temporary object destructor)\n"; |
Marcin Swiderski | c0ca731 | 2010-09-21 05:58:15 +0000 | [diff] [blame] | 4803 | } |
Zhongxing Xu | 0b51d4d | 2010-11-01 06:46:05 +0000 | [diff] [blame] | 4804 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4805 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4806 | static void print_block(raw_ostream &OS, const CFG* cfg, |
| 4807 | const CFGBlock &B, |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4808 | StmtPrinterHelper &Helper, bool print_edges, |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4809 | bool ShowColors) { |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4810 | Helper.setBlockID(B.getBlockID()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4811 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 4812 | // Print the header. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4813 | if (ShowColors) |
| 4814 | OS.changeColor(raw_ostream::YELLOW, true); |
| 4815 | |
| 4816 | OS << "\n [B" << B.getBlockID(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4817 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4818 | if (&B == &cfg->getEntry()) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4819 | OS << " (ENTRY)]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4820 | else if (&B == &cfg->getExit()) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4821 | OS << " (EXIT)]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4822 | else if (&B == cfg->getIndirectGotoBlock()) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4823 | OS << " (INDIRECT GOTO DISPATCH)]\n"; |
Jordan Rose | 398fb00 | 2014-04-01 16:39:33 +0000 | [diff] [blame] | 4824 | else if (B.hasNoReturnElement()) |
| 4825 | OS << " (NORETURN)]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4826 | else |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4827 | OS << "]\n"; |
| 4828 | |
| 4829 | if (ShowColors) |
| 4830 | OS.resetColor(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4831 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4832 | // Print the label of this block. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4833 | if (Stmt *Label = const_cast<Stmt*>(B.getLabel())) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4834 | if (print_edges) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4835 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4836 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4837 | if (LabelStmt *L = dyn_cast<LabelStmt>(Label)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4838 | OS << L->getName(); |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 4839 | else if (CaseStmt *C = dyn_cast<CaseStmt>(Label)) { |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4840 | OS << "case "; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 4841 | if (C->getLHS()) |
| 4842 | C->getLHS()->printPretty(OS, &Helper, |
| 4843 | PrintingPolicy(Helper.getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4844 | if (C->getRHS()) { |
| 4845 | OS << " ... "; |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4846 | C->getRHS()->printPretty(OS, &Helper, |
| 4847 | PrintingPolicy(Helper.getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4848 | } |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 4849 | } else if (isa<DefaultStmt>(Label)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4850 | OS << "default"; |
Mike Stump | 92244b0 | 2010-01-19 22:00:14 +0000 | [diff] [blame] | 4851 | else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) { |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 4852 | OS << "catch ("; |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 4853 | if (CS->getExceptionDecl()) |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4854 | CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper.getLangOpts()), |
Mike Stump | 0bdba6c | 2010-01-20 01:15:34 +0000 | [diff] [blame] | 4855 | 0); |
| 4856 | else |
| 4857 | OS << "..."; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 4858 | OS << ")"; |
Nico Weber | 699670e | 2017-08-23 15:33:16 +0000 | [diff] [blame] | 4859 | } else if (SEHExceptStmt *ES = dyn_cast<SEHExceptStmt>(Label)) { |
| 4860 | OS << "__except ("; |
| 4861 | ES->getFilterExpr()->printPretty(OS, &Helper, |
| 4862 | PrintingPolicy(Helper.getLangOpts()), 0); |
| 4863 | OS << ")"; |
Mike Stump | bbf5ba6 | 2010-01-19 02:20:09 +0000 | [diff] [blame] | 4864 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4865 | llvm_unreachable("Invalid label statement in CFGBlock."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4866 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4867 | OS << ":\n"; |
| 4868 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4869 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4870 | // Iterate through the statements in the block and print them. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4871 | unsigned j = 1; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4872 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4873 | for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; |
| 4874 | I != E ; ++I, ++j ) { |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4875 | // Print the statement # in the basic block and the statement itself. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4876 | if (print_edges) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4877 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4878 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 4879 | OS << llvm::format("%3d", j) << ": "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4880 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4881 | Helper.setStmtID(j); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4882 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4883 | print_elem(OS, Helper, *I); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4884 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4885 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4886 | // Print the terminator of this block. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4887 | if (B.getTerminator()) { |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4888 | if (ShowColors) |
| 4889 | OS.changeColor(raw_ostream::GREEN); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4890 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4891 | OS << " T: "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4892 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4893 | Helper.setBlockID(-1); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4894 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4895 | PrintingPolicy PP(Helper.getLangOpts()); |
| 4896 | CFGBlockTerminatorPrint TPrinter(OS, &Helper, PP); |
Ted Kremenek | fcc1417 | 2014-03-08 02:22:29 +0000 | [diff] [blame] | 4897 | TPrinter.print(B.getTerminator()); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 4898 | OS << '\n'; |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4899 | |
| 4900 | if (ShowColors) |
| 4901 | OS.resetColor(); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4902 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4903 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4904 | if (print_edges) { |
| 4905 | // Print the predecessors of this block. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4906 | if (!B.pred_empty()) { |
| 4907 | const raw_ostream::Colors Color = raw_ostream::BLUE; |
| 4908 | if (ShowColors) |
| 4909 | OS.changeColor(Color); |
| 4910 | OS << " Preds " ; |
| 4911 | if (ShowColors) |
| 4912 | OS.resetColor(); |
| 4913 | OS << '(' << B.pred_size() << "):"; |
| 4914 | unsigned i = 0; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4915 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4916 | if (ShowColors) |
| 4917 | OS.changeColor(Color); |
| 4918 | |
| 4919 | for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); |
| 4920 | I != E; ++I, ++i) { |
Will Dietz | df9a2bb | 2013-01-07 09:51:17 +0000 | [diff] [blame] | 4921 | if (i % 10 == 8) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4922 | OS << "\n "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4923 | |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 4924 | CFGBlock *B = *I; |
| 4925 | bool Reachable = true; |
| 4926 | if (!B) { |
| 4927 | Reachable = false; |
| 4928 | B = I->getPossiblyUnreachableBlock(); |
| 4929 | } |
| 4930 | |
| 4931 | OS << " B" << B->getBlockID(); |
| 4932 | if (!Reachable) |
| 4933 | OS << "(Unreachable)"; |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
| 4936 | if (ShowColors) |
| 4937 | OS.resetColor(); |
| 4938 | |
| 4939 | OS << '\n'; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 4940 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4941 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4942 | // Print the successors of this block. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4943 | if (!B.succ_empty()) { |
| 4944 | const raw_ostream::Colors Color = raw_ostream::MAGENTA; |
| 4945 | if (ShowColors) |
| 4946 | OS.changeColor(Color); |
| 4947 | OS << " Succs "; |
| 4948 | if (ShowColors) |
| 4949 | OS.resetColor(); |
| 4950 | OS << '(' << B.succ_size() << "):"; |
| 4951 | unsigned i = 0; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4952 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4953 | if (ShowColors) |
| 4954 | OS.changeColor(Color); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4955 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4956 | for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); |
| 4957 | I != E; ++I, ++i) { |
Will Dietz | df9a2bb | 2013-01-07 09:51:17 +0000 | [diff] [blame] | 4958 | if (i % 10 == 8) |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4959 | OS << "\n "; |
| 4960 | |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 4961 | CFGBlock *B = *I; |
| 4962 | |
| 4963 | bool Reachable = true; |
| 4964 | if (!B) { |
| 4965 | Reachable = false; |
| 4966 | B = I->getPossiblyUnreachableBlock(); |
| 4967 | } |
| 4968 | |
| 4969 | if (B) { |
| 4970 | OS << " B" << B->getBlockID(); |
| 4971 | if (!Reachable) |
| 4972 | OS << "(Unreachable)"; |
| 4973 | } |
| 4974 | else { |
| 4975 | OS << " NULL"; |
| 4976 | } |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4977 | } |
Ted Kremenek | 9238c5c | 2014-02-27 21:56:44 +0000 | [diff] [blame] | 4978 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4979 | if (ShowColors) |
| 4980 | OS.resetColor(); |
| 4981 | OS << '\n'; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4982 | } |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 4983 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4984 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4985 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4986 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 4987 | void CFG::dump(const LangOptions &LO, bool ShowColors) const { |
| 4988 | print(llvm::errs(), LO, ShowColors); |
| 4989 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4990 | |
| 4991 | /// 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] | 4992 | void CFG::print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const { |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 4993 | StmtPrinterHelper Helper(this, LO); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4994 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4995 | // Print the entry block. |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 4996 | print_block(OS, this, getEntry(), Helper, true, ShowColors); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 4997 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 4998 | // Iterate through the CFGBlocks and print them one by one. |
| 4999 | for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 5000 | // Skip the entry block, because we already printed it. |
Ted Kremenek | 289ae4f | 2009-10-12 20:55:07 +0000 | [diff] [blame] | 5001 | if (&(**I) == &getEntry() || &(**I) == &getExit()) |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5002 | continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5003 | |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 5004 | print_block(OS, this, **I, Helper, true, ShowColors); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5005 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5006 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5007 | // Print the exit block. |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 5008 | print_block(OS, this, getExit(), Helper, true, ShowColors); |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 5009 | OS << '\n'; |
Ted Kremenek | e03879b | 2008-11-24 20:50:24 +0000 | [diff] [blame] | 5010 | OS.flush(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5011 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5012 | |
| 5013 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 5014 | void CFGBlock::dump(const CFG* cfg, const LangOptions &LO, |
| 5015 | bool ShowColors) const { |
| 5016 | print(llvm::errs(), cfg, LO, ShowColors); |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 5017 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5018 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 5019 | LLVM_DUMP_METHOD void CFGBlock::dump() const { |
Anna Zaks | a6fea13 | 2014-06-13 23:47:38 +0000 | [diff] [blame] | 5020 | dump(getParent(), LangOptions(), false); |
| 5021 | } |
| 5022 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5023 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 5024 | /// Generally this will only be called from CFG::print. |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 5025 | void CFGBlock::print(raw_ostream &OS, const CFG* cfg, |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 5026 | const LangOptions &LO, bool ShowColors) const { |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 5027 | StmtPrinterHelper Helper(cfg, LO); |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 5028 | print_block(OS, cfg, *this, Helper, true, ShowColors); |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 5029 | OS << '\n'; |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 5030 | } |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 5031 | |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 5032 | /// printTerminator - A simple pretty printer of the terminator of a CFGBlock. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5033 | void CFGBlock::printTerminator(raw_ostream &OS, |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5034 | const LangOptions &LO) const { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 5035 | CFGBlockTerminatorPrint TPrinter(OS, nullptr, PrintingPolicy(LO)); |
Ted Kremenek | fcc1417 | 2014-03-08 02:22:29 +0000 | [diff] [blame] | 5036 | TPrinter.print(getTerminator()); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 5037 | } |
| 5038 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 5039 | Stmt *CFGBlock::getTerminatorCondition(bool StripParens) { |
Marcin Swiderski | a7d84a7 | 2010-10-29 05:21:47 +0000 | [diff] [blame] | 5040 | Stmt *Terminator = this->Terminator; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5041 | if (!Terminator) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 5042 | return nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5043 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 5044 | Expr *E = nullptr; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5045 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5046 | switch (Terminator->getStmtClass()) { |
| 5047 | default: |
| 5048 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5049 | |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 5050 | case Stmt::CXXForRangeStmtClass: |
| 5051 | E = cast<CXXForRangeStmt>(Terminator)->getCond(); |
| 5052 | break; |
| 5053 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5054 | case Stmt::ForStmtClass: |
| 5055 | E = cast<ForStmt>(Terminator)->getCond(); |
| 5056 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5057 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5058 | case Stmt::WhileStmtClass: |
| 5059 | E = cast<WhileStmt>(Terminator)->getCond(); |
| 5060 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5061 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5062 | case Stmt::DoStmtClass: |
| 5063 | E = cast<DoStmt>(Terminator)->getCond(); |
| 5064 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5065 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5066 | case Stmt::IfStmtClass: |
| 5067 | E = cast<IfStmt>(Terminator)->getCond(); |
| 5068 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5069 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5070 | case Stmt::ChooseExprClass: |
| 5071 | E = cast<ChooseExpr>(Terminator)->getCond(); |
| 5072 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5073 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5074 | case Stmt::IndirectGotoStmtClass: |
| 5075 | E = cast<IndirectGotoStmt>(Terminator)->getTarget(); |
| 5076 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5077 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5078 | case Stmt::SwitchStmtClass: |
| 5079 | E = cast<SwitchStmt>(Terminator)->getCond(); |
| 5080 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5081 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5082 | case Stmt::BinaryConditionalOperatorClass: |
| 5083 | E = cast<BinaryConditionalOperator>(Terminator)->getCond(); |
| 5084 | break; |
| 5085 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5086 | case Stmt::ConditionalOperatorClass: |
| 5087 | E = cast<ConditionalOperator>(Terminator)->getCond(); |
| 5088 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5089 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5090 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 5091 | E = cast<BinaryOperator>(Terminator)->getLHS(); |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 5092 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5093 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 5094 | case Stmt::ObjCForCollectionStmtClass: |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5095 | return Terminator; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5096 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5097 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 5098 | if (!StripParens) |
| 5099 | return E; |
| 5100 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 5101 | return E ? E->IgnoreParens() : nullptr; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 5104 | //===----------------------------------------------------------------------===// |
| 5105 | // CFG Graphviz Visualization |
| 5106 | //===----------------------------------------------------------------------===// |
| 5107 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5108 | #ifndef NDEBUG |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5109 | static StmtPrinterHelper* GraphHelper; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5110 | #endif |
| 5111 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 5112 | void CFG::viewCFG(const LangOptions &LO) const { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5113 | #ifndef NDEBUG |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 5114 | StmtPrinterHelper H(this, LO); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5115 | GraphHelper = &H; |
| 5116 | llvm::ViewGraph(this,"CFG"); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 5117 | GraphHelper = nullptr; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 5118 | #endif |
| 5119 | } |
| 5120 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 5121 | namespace llvm { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 5122 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 5123 | template<> |
| 5124 | struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 5125 | DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} |
Tobias Grosser | 9fc223a | 2009-11-30 14:16:05 +0000 | [diff] [blame] | 5126 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 5127 | static std::string getNodeLabel(const CFGBlock *Node, const CFG* Graph) { |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 5128 | #ifndef NDEBUG |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 5129 | std::string OutSStr; |
| 5130 | llvm::raw_string_ostream Out(OutSStr); |
Aaron Ballman | ff924b0 | 2013-11-18 20:11:50 +0000 | [diff] [blame] | 5131 | print_block(Out,Graph, *Node, *GraphHelper, false, false); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 5132 | std::string& OutStr = Out.str(); |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 5133 | |
| 5134 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 5135 | |
| 5136 | // Process string output to make it nicer... |
| 5137 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 5138 | if (OutStr[i] == '\n') { // Left justify |
| 5139 | OutStr[i] = '\\'; |
| 5140 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 5141 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 5142 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 5143 | return OutStr; |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 5144 | #else |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 5145 | return {}; |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 5146 | #endif |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 5147 | } |
| 5148 | }; |
Eugene Zelenko | 38c7052 | 2017-12-07 21:55:09 +0000 | [diff] [blame] | 5149 | |
| 5150 | } // namespace llvm |