| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1 | //===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===// |
| 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. |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for statements. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTDiagnostic.h" |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/CharUnits.h" |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
| Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 20 | #include "clang/AST/EvaluatedExprVisitor.h" |
| Douglas Gregor | d0c22e0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
| Chris Lattner | 2ba5ca9 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
| Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtObjC.h" |
| John McCall | 2351cb9 | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
| Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "clang/Lex/Preprocessor.h" |
| 30 | #include "clang/Sema/Initialization.h" |
| 31 | #include "clang/Sema/Lookup.h" |
| 32 | #include "clang/Sema/Scope.h" |
| 33 | #include "clang/Sema/ScopeInfo.h" |
| Chris Lattner | 70a4e9b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/ArrayRef.h" |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/DenseMap.h" |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallPtrSet.h" |
| Douglas Gregor | 0a9f4e0 | 2012-05-16 16:11:17 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallString.h" |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallVector.h" |
| Erik Pilkington | ce26eac | 2016-04-26 20:55:48 +0000 | [diff] [blame] | 40 | |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 41 | using namespace clang; |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 42 | using namespace sema; |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 43 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 44 | StmtResult Sema::ActOnExprStmt(ExprResult FE) { |
| 45 | if (FE.isInvalid()) |
| 46 | return StmtError(); |
| 47 | |
| 48 | FE = ActOnFinishFullExpr(FE.get(), FE.get()->getExprLoc(), |
| 49 | /*DiscardedValue*/ true); |
| 50 | if (FE.isInvalid()) |
| Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 51 | return StmtError(); |
| 52 | |
| Chris Lattner | 903eb51 | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 53 | // C99 6.8.3p2: The expression in an expression statement is evaluated as a |
| 54 | // void expression for its side effects. Conversion to void allows any |
| 55 | // operand, even incomplete types. |
| Sebastian Redl | 52f03ba | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 56 | |
| Chris Lattner | 903eb51 | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 57 | // Same thing in for stmt first clause (when expr) and third clause. |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 58 | return StmtResult(FE.getAs<Stmt>()); |
| Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| John McCall | eaef89b | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 62 | StmtResult Sema::ActOnExprStmtError() { |
| 63 | DiscardCleanupsInEvaluationContext(); |
| 64 | return StmtError(); |
| 65 | } |
| 66 | |
| Argyrios Kyrtzidis | f7620e4 | 2011-04-27 05:04:02 +0000 | [diff] [blame] | 67 | StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, |
| Argyrios Kyrtzidis | 43ea78b | 2011-09-01 21:53:45 +0000 | [diff] [blame] | 68 | bool HasLeadingEmptyMacro) { |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 69 | return new (Context) NullStmt(SemiLoc, HasLeadingEmptyMacro); |
| Chris Lattner | 0f203a7 | 2007-05-28 01:45:28 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 72 | StmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, SourceLocation StartLoc, |
| 73 | SourceLocation EndLoc) { |
| Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 74 | DeclGroupRef DG = dg.get(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
| Chris Lattner | cbafe8d | 2009-04-12 20:13:14 +0000 | [diff] [blame] | 76 | // If we have an invalid decl, just return an error. |
| 77 | if (DG.isNull()) return StmtError(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 79 | return new (Context) DeclStmt(DG, StartLoc, EndLoc); |
| Steve Naroff | 2a8ad18 | 2007-05-29 22:59:26 +0000 | [diff] [blame] | 80 | } |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 81 | |
| Fariborz Jahanian | e774fa6 | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 82 | void Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { |
| Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 83 | DeclGroupRef DG = dg.get(); |
| Wei Pan | c4c76d1 | 2013-05-03 21:07:45 +0000 | [diff] [blame] | 84 | |
| Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 85 | // If we don't have a declaration, or we have an invalid declaration, |
| 86 | // just return. |
| 87 | if (DG.isNull() || !DG.isSingleDecl()) |
| 88 | return; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 89 | |
| Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 90 | Decl *decl = DG.getSingleDecl(); |
| 91 | if (!decl || decl->isInvalidDecl()) |
| 92 | return; |
| 93 | |
| 94 | // Only variable declarations are permitted. |
| 95 | VarDecl *var = dyn_cast<VarDecl>(decl); |
| 96 | if (!var) { |
| 97 | Diag(decl->getLocation(), diag::err_non_variable_decl_in_for); |
| 98 | decl->setInvalidDecl(); |
| 99 | return; |
| 100 | } |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 101 | |
| John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 102 | // foreach variables are never actually initialized in the way that |
| 103 | // the parser came up with. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 104 | var->setInit(nullptr); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 105 | |
| John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 106 | // In ARC, we don't need to retain the iteration variable of a fast |
| 107 | // enumeration loop. Rather than actually trying to catch that |
| 108 | // during declaration processing, we remove the consequences here. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 109 | if (getLangOpts().ObjCAutoRefCount) { |
| John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 110 | QualType type = var->getType(); |
| 111 | |
| 112 | // Only do this if we inferred the lifetime. Inferred lifetime |
| 113 | // will show up as a local qualifier because explicit lifetime |
| 114 | // should have shown up as an AttributedType instead. |
| 115 | if (type.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 116 | // Add 'const' and mark the variable as pseudo-strong. |
| 117 | var->setType(type.withConst()); |
| 118 | var->setARCPseudoStrong(true); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| Fariborz Jahanian | e774fa6 | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 123 | /// \brief Diagnose unused comparisons, both builtin and overloaded operators. |
| 124 | /// For '==' and '!=', suggest fixits for '=' or '|='. |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 125 | /// |
| 126 | /// Adding a cast to void (or other expression wrappers) will prevent the |
| 127 | /// warning from firing. |
| Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 128 | static bool DiagnoseUnusedComparison(Sema &S, const Expr *E) { |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 129 | SourceLocation Loc; |
| Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 130 | bool IsNotEqual, CanAssign, IsRelational; |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 131 | |
| 132 | if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
| Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 133 | if (!Op->isComparisonOp()) |
| Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 134 | return false; |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 135 | |
| Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 136 | IsRelational = Op->isRelationalOp(); |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 137 | Loc = Op->getOperatorLoc(); |
| Chandler Carruth | e89ca5f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 138 | IsNotEqual = Op->getOpcode() == BO_NE; |
| 139 | CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue(); |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 140 | } else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
| Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 141 | switch (Op->getOperator()) { |
| 142 | default: |
| Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 143 | return false; |
| Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 144 | case OO_EqualEqual: |
| 145 | case OO_ExclaimEqual: |
| 146 | IsRelational = false; |
| 147 | break; |
| 148 | case OO_Less: |
| 149 | case OO_Greater: |
| 150 | case OO_GreaterEqual: |
| 151 | case OO_LessEqual: |
| 152 | IsRelational = true; |
| 153 | break; |
| 154 | } |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 155 | |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 156 | Loc = Op->getOperatorLoc(); |
| Chandler Carruth | e89ca5f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 157 | IsNotEqual = Op->getOperator() == OO_ExclaimEqual; |
| 158 | CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue(); |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 159 | } else { |
| 160 | // Not a typo-prone comparison. |
| Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 161 | return false; |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Suppress warnings when the operator, suspicious as it may be, comes from |
| 165 | // a macro expansion. |
| Matt Beaumont-Gay | b1e71a7 | 2013-01-12 00:54:16 +0000 | [diff] [blame] | 166 | if (S.SourceMgr.isMacroBodyExpansion(Loc)) |
| Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 167 | return false; |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 168 | |
| Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 169 | S.Diag(Loc, diag::warn_unused_comparison) |
| Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 170 | << (unsigned)IsRelational << (unsigned)IsNotEqual << E->getSourceRange(); |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 171 | |
| Chandler Carruth | e89ca5f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 172 | // If the LHS is a plausible entity to assign to, provide a fixit hint to |
| 173 | // correct common typos. |
| Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 174 | if (!IsRelational && CanAssign) { |
| Chandler Carruth | e89ca5f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 175 | if (IsNotEqual) |
| 176 | S.Diag(Loc, diag::note_inequality_comparison_to_or_assign) |
| 177 | << FixItHint::CreateReplacement(Loc, "|="); |
| 178 | else |
| 179 | S.Diag(Loc, diag::note_equality_comparison_to_assign) |
| 180 | << FixItHint::CreateReplacement(Loc, "="); |
| 181 | } |
| Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 182 | |
| 183 | return true; |
| Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 186 | void Sema::DiagnoseUnusedExprResult(const Stmt *S) { |
| Argyrios Kyrtzidis | 9096341 | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 187 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
| 188 | return DiagnoseUnusedExprResult(Label->getSubStmt()); |
| 189 | |
| Anders Carlsson | 5c5f160 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 190 | const Expr *E = dyn_cast_or_null<Expr>(S); |
| Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 191 | if (!E) |
| 192 | return; |
| Aaron Ballman | 78ecb87 | 2014-10-16 20:13:28 +0000 | [diff] [blame] | 193 | |
| 194 | // If we are in an unevaluated expression context, then there can be no unused |
| 195 | // results because the results aren't expected to be used in the first place. |
| 196 | if (isUnevaluatedContext()) |
| 197 | return; |
| 198 | |
| Nico Weber | 0e63163 | 2015-10-27 19:47:40 +0000 | [diff] [blame] | 199 | SourceLocation ExprLoc = E->IgnoreParenImpCasts()->getExprLoc(); |
| Matt Beaumont-Gay | 1c417da | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 200 | // In most cases, we don't want to warn if the expression is written in a |
| 201 | // macro body, or if the macro comes from a system header. If the offending |
| 202 | // expression is a call to a function with the warn_unused_result attribute, |
| 203 | // we warn no matter the location. Because of the order in which the various |
| 204 | // checks need to happen, we factor out the macro-related test here. |
| 205 | bool ShouldSuppress = |
| 206 | SourceMgr.isMacroBodyExpansion(ExprLoc) || |
| 207 | SourceMgr.isInSystemMacro(ExprLoc); |
| Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 208 | |
| Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 209 | const Expr *WarnExpr; |
| Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 210 | SourceLocation Loc; |
| 211 | SourceRange R1, R2; |
| Matt Beaumont-Gay | 978cca9 | 2013-01-17 02:06:08 +0000 | [diff] [blame] | 212 | if (!E->isUnusedResultAWarning(WarnExpr, Loc, R1, R2, Context)) |
| Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 213 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
| Chris Lattner | 6dc7e57 | 2012-08-31 22:39:21 +0000 | [diff] [blame] | 215 | // If this is a GNU statement expression expanded from a macro, it is probably |
| 216 | // unused because it is a function-like macro that can be used as either an |
| 217 | // expression or statement. Don't warn, because it is almost certainly a |
| 218 | // false positive. |
| 219 | if (isa<StmtExpr>(E) && Loc.isMacroID()) |
| 220 | return; |
| 221 | |
| Nico Weber | 0e63163 | 2015-10-27 19:47:40 +0000 | [diff] [blame] | 222 | // Check if this is the UNREFERENCED_PARAMETER from the Microsoft headers. |
| 223 | // That macro is frequently used to suppress "unused parameter" warnings, |
| 224 | // but its implementation makes clang's -Wunused-value fire. Prevent this. |
| 225 | if (isa<ParenExpr>(E->IgnoreImpCasts()) && Loc.isMacroID()) { |
| 226 | SourceLocation SpellLoc = Loc; |
| 227 | if (findMacroSpelling(SpellLoc, "UNREFERENCED_PARAMETER")) |
| 228 | return; |
| 229 | } |
| 230 | |
| Chris Lattner | 2ba5ca9 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 231 | // Okay, we have an unused result. Depending on what the base expression is, |
| 232 | // we might want to make a more specific diagnostic. Check for one of these |
| 233 | // cases now. |
| 234 | unsigned DiagID = diag::warn_unused_expr; |
| John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 235 | if (const ExprWithCleanups *Temps = dyn_cast<ExprWithCleanups>(E)) |
| Douglas Gregor | 50dc219 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 236 | E = Temps->getSubExpr(); |
| Chandler Carruth | d05b352 | 2011-02-21 00:56:56 +0000 | [diff] [blame] | 237 | if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 238 | E = TempExpr->getSubExpr(); |
| John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 239 | |
| Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 240 | if (DiagnoseUnusedComparison(*this, E)) |
| 241 | return; |
| 242 | |
| Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 243 | E = WarnExpr; |
| Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 244 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
| John McCall | c493a73 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 245 | if (E->getType()->isVoidType()) |
| 246 | return; |
| 247 | |
| Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 248 | // If the callee has attribute pure, const, or warn_unused_result, warn with |
| Matt Beaumont-Gay | 1c417da | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 249 | // a more specific message to make it clear what is happening. If the call |
| 250 | // is written in a macro body, only warn if it has the warn_unused_result |
| 251 | // attribute. |
| Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 252 | if (const Decl *FD = CE->getCalleeDecl()) { |
| Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 253 | if (const Attr *A = isa<FunctionDecl>(FD) |
| 254 | ? cast<FunctionDecl>(FD)->getUnusedResultAttr() |
| 255 | : FD->getAttr<WarnUnusedResultAttr>()) { |
| 256 | Diag(Loc, diag::warn_unused_result) << A << R1 << R2; |
| Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 257 | return; |
| 258 | } |
| Matt Beaumont-Gay | 1c417da | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 259 | if (ShouldSuppress) |
| 260 | return; |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 261 | if (FD->hasAttr<PureAttr>()) { |
| Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 262 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; |
| 263 | return; |
| 264 | } |
| Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 265 | if (FD->hasAttr<ConstAttr>()) { |
| Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 266 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; |
| 267 | return; |
| 268 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 269 | } |
| Matt Beaumont-Gay | 1c417da | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 270 | } else if (ShouldSuppress) |
| 271 | return; |
| 272 | |
| 273 | if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 274 | if (getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 275 | Diag(Loc, diag::err_arc_unused_init_message) << R1; |
| 276 | return; |
| 277 | } |
| Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 278 | const ObjCMethodDecl *MD = ME->getMethodDecl(); |
| Fariborz Jahanian | ac6b4ef | 2014-07-18 22:59:10 +0000 | [diff] [blame] | 279 | if (MD) { |
| Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 280 | if (const auto *A = MD->getAttr<WarnUnusedResultAttr>()) { |
| 281 | Diag(Loc, diag::warn_unused_result) << A << R1 << R2; |
| Fariborz Jahanian | ac6b4ef | 2014-07-18 22:59:10 +0000 | [diff] [blame] | 282 | return; |
| 283 | } |
| Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 284 | } |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 285 | } else if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { |
| 286 | const Expr *Source = POE->getSyntacticForm(); |
| 287 | if (isa<ObjCSubscriptRefExpr>(Source)) |
| 288 | DiagID = diag::warn_unused_container_subscript_expr; |
| 289 | else |
| 290 | DiagID = diag::warn_unused_property_expr; |
| Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 291 | } else if (const CXXFunctionalCastExpr *FC |
| 292 | = dyn_cast<CXXFunctionalCastExpr>(E)) { |
| Daniel Jasper | 9c81a72 | 2017-03-27 16:29:41 +0000 | [diff] [blame] | 293 | const Expr *E = FC->getSubExpr(); |
| 294 | if (const CXXBindTemporaryExpr *TE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 295 | E = TE->getSubExpr(); |
| 296 | if (isa<CXXTemporaryObjectExpr>(E)) |
| Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 297 | return; |
| Daniel Jasper | 9c81a72 | 2017-03-27 16:29:41 +0000 | [diff] [blame] | 298 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
| 299 | if (const CXXRecordDecl *RD = CE->getType()->getAsCXXRecordDecl()) |
| 300 | if (!RD->getAttr<WarnUnusedAttr>()) |
| 301 | return; |
| Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 302 | } |
| John McCall | 2351cb9 | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 303 | // Diagnose "(void*) blah" as a typo for "(void) blah". |
| 304 | else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { |
| 305 | TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); |
| 306 | QualType T = TI->getType(); |
| 307 | |
| 308 | // We really do want to use the non-canonical type here. |
| 309 | if (T == Context.VoidPtrTy) { |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 310 | PointerTypeLoc TL = TI->getTypeLoc().castAs<PointerTypeLoc>(); |
| John McCall | 2351cb9 | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 311 | |
| 312 | Diag(Loc, diag::warn_unused_voidptr) |
| 313 | << FixItHint::CreateRemoval(TL.getStarLoc()); |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | |
| Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 318 | if (E->isGLValue() && E->getType().isVolatileQualified()) { |
| 319 | Diag(Loc, diag::warn_unused_volatile) << R1 << R2; |
| 320 | return; |
| 321 | } |
| 322 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 323 | DiagRuntimeBehavior(Loc, nullptr, PDiag(DiagID) << R1 << R2); |
| Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 326 | void Sema::ActOnStartOfCompoundStmt() { |
| 327 | PushCompoundScope(); |
| 328 | } |
| 329 | |
| 330 | void Sema::ActOnFinishOfCompoundStmt() { |
| 331 | PopCompoundScope(); |
| 332 | } |
| 333 | |
| 334 | sema::CompoundScopeInfo &Sema::getCurCompoundScope() const { |
| 335 | return getCurFunction()->CompoundScopes.back(); |
| 336 | } |
| 337 | |
| Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 338 | StmtResult Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
| 339 | ArrayRef<Stmt *> Elts, bool isStmtExpr) { |
| 340 | const unsigned NumElts = Elts.size(); |
| 341 | |
| Chris Lattner | d864daf | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 342 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 343 | // so, emit an extension diagnostic. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 344 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus) { |
| Chris Lattner | d864daf | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 345 | // Note that __extension__ can be around a decl. |
| 346 | unsigned i = 0; |
| 347 | // Skip over all declarations. |
| 348 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 349 | /*empty*/; |
| 350 | |
| 351 | // We found the end of the list or a statement. Scan for another declstmt. |
| 352 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 353 | /*empty*/; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
| Chris Lattner | d864daf | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 355 | if (i != NumElts) { |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 356 | Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); |
| Chris Lattner | d864daf | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 357 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 358 | } |
| 359 | } |
| Chris Lattner | cac27a5 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 360 | // Warn about unused expressions in statements. |
| 361 | for (unsigned i = 0; i != NumElts; ++i) { |
| Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 362 | // Ignore statements that are last in a statement expression. |
| 363 | if (isStmtExpr && i == NumElts - 1) |
| Chris Lattner | cac27a5 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 364 | continue; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | |
| Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 366 | DiagnoseUnusedExprResult(Elts[i]); |
| Chris Lattner | cac27a5 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 367 | } |
| Sebastian Redl | 52f03ba | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 368 | |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 369 | // Check for suspicious empty body (null statement) in `for' and `while' |
| 370 | // statements. Don't do anything for template instantiations, this just adds |
| 371 | // noise. |
| 372 | if (NumElts != 0 && !CurrentInstantiationScope && |
| 373 | getCurCompoundScope().HasEmptyLoopBodies) { |
| 374 | for (unsigned i = 0; i != NumElts - 1; ++i) |
| 375 | DiagnoseEmptyLoopBody(Elts[i], Elts[i + 1]); |
| 376 | } |
| 377 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 378 | return new (Context) CompoundStmt(Context, Elts, L, R); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 381 | StmtResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 382 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal, |
| 383 | SourceLocation DotDotDotLoc, Expr *RHSVal, |
| Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 384 | SourceLocation ColonLoc) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 385 | assert(LHSVal && "missing expression in case statement"); |
| Sebastian Redl | 1cbb5918 | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 386 | |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 387 | if (getCurFunction()->SwitchStack.empty()) { |
| Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 388 | Diag(CaseLoc, diag::err_case_not_in_switch); |
| Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 389 | return StmtError(); |
| Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 390 | } |
| Chris Lattner | 35e287b | 2007-06-03 01:44:43 +0000 | [diff] [blame] | 391 | |
| Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 392 | ExprResult LHS = |
| 393 | CorrectDelayedTyposInExpr(LHSVal, [this](class Expr *E) { |
| 394 | if (!getLangOpts().CPlusPlus11) |
| 395 | return VerifyIntegerConstantExpression(E); |
| 396 | if (Expr *CondExpr = |
| 397 | getCurFunction()->SwitchStack.back()->getCond()) { |
| 398 | QualType CondType = CondExpr->getType(); |
| 399 | llvm::APSInt TempVal; |
| 400 | return CheckConvertedConstantExpression(E, CondType, TempVal, |
| 401 | CCEK_CaseValue); |
| 402 | } |
| 403 | return ExprError(); |
| 404 | }); |
| 405 | if (LHS.isInvalid()) |
| 406 | return StmtError(); |
| 407 | LHSVal = LHS.get(); |
| 408 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 409 | if (!getLangOpts().CPlusPlus11) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 410 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
| 411 | // However, GCC allows any evaluatable integer expression. |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 412 | if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent()) { |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 413 | LHSVal = VerifyIntegerConstantExpression(LHSVal).get(); |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 414 | if (!LHSVal) |
| 415 | return StmtError(); |
| 416 | } |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 417 | |
| 418 | // GCC extension: The expression shall be an integer constant. |
| 419 | |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 420 | if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent()) { |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 421 | RHSVal = VerifyIntegerConstantExpression(RHSVal).get(); |
| Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 422 | // Recover from an error by just forgetting about it. |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| Ben Langmuir | 2e13dd6 | 2013-04-29 13:07:42 +0000 | [diff] [blame] | 425 | |
| Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 426 | LHS = ActOnFinishFullExpr(LHSVal, LHSVal->getExprLoc(), false, |
| Richard Smith | 5b555da | 2014-11-20 01:24:12 +0000 | [diff] [blame] | 427 | getLangOpts().CPlusPlus11); |
| 428 | if (LHS.isInvalid()) |
| 429 | return StmtError(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 430 | |
| Richard Smith | 5b555da | 2014-11-20 01:24:12 +0000 | [diff] [blame] | 431 | auto RHS = RHSVal ? ActOnFinishFullExpr(RHSVal, RHSVal->getExprLoc(), false, |
| 432 | getLangOpts().CPlusPlus11) |
| 433 | : ExprResult(); |
| 434 | if (RHS.isInvalid()) |
| 435 | return StmtError(); |
| 436 | |
| 437 | CaseStmt *CS = new (Context) |
| 438 | CaseStmt(LHS.get(), RHS.get(), CaseLoc, DotDotDotLoc, ColonLoc); |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 439 | getCurFunction()->SwitchStack.back()->addSwitchCase(CS); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 440 | return CS; |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 443 | /// ActOnCaseStmtBody - This installs a statement as the body of a case. |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 444 | void Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) { |
| Chandler Carruth | 2b949c2 | 2011-08-18 02:04:29 +0000 | [diff] [blame] | 445 | DiagnoseUnusedExprResult(SubStmt); |
| 446 | |
| Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 447 | CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); |
| Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 448 | CS->setSubStmt(SubStmt); |
| 449 | } |
| 450 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 451 | StmtResult |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 453 | Stmt *SubStmt, Scope *CurScope) { |
| Chandler Carruth | 2b949c2 | 2011-08-18 02:04:29 +0000 | [diff] [blame] | 454 | DiagnoseUnusedExprResult(SubStmt); |
| 455 | |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 456 | if (getCurFunction()->SwitchStack.empty()) { |
| Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 457 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 458 | return SubStmt; |
| Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 459 | } |
| Sebastian Redl | 1cbb5918 | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 460 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 461 | DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt); |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 462 | getCurFunction()->SwitchStack.back()->addSwitchCase(DS); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 463 | return DS; |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 466 | StmtResult |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 467 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, |
| 468 | SourceLocation ColonLoc, Stmt *SubStmt) { |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 469 | // If the label was multiply defined, reject it now. |
| 470 | if (TheDecl->getStmt()) { |
| 471 | Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName(); |
| 472 | Diag(TheDecl->getLocation(), diag::note_previous_definition); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 473 | return SubStmt; |
| Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 474 | } |
| Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 475 | |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 476 | // Otherwise, things are good. Fill in the declaration and return it. |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 477 | LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt); |
| 478 | TheDecl->setStmt(LS); |
| Abramo Bagnara | 598b943 | 2012-10-15 21:07:44 +0000 | [diff] [blame] | 479 | if (!TheDecl->isGnuLocal()) { |
| 480 | TheDecl->setLocStart(IdentLoc); |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 481 | if (!TheDecl->isMSAsmLabel()) { |
| 482 | // Don't update the location of MS ASM labels. These will result in |
| 483 | // a diagnostic, and changing the location here will mess that up. |
| 484 | TheDecl->setLocation(IdentLoc); |
| 485 | } |
| Abramo Bagnara | 598b943 | 2012-10-15 21:07:44 +0000 | [diff] [blame] | 486 | } |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 487 | return LS; |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 490 | StmtResult Sema::ActOnAttributedStmt(SourceLocation AttrLoc, |
| Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 491 | ArrayRef<const Attr*> Attrs, |
| Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 492 | Stmt *SubStmt) { |
| Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 493 | // Fill in the declaration and return it. |
| 494 | AttributedStmt *LS = AttributedStmt::Create(Context, AttrLoc, Attrs, SubStmt); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 495 | return LS; |
| Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| Richard Trieu | faca2d8 | 2016-02-18 23:58:40 +0000 | [diff] [blame] | 498 | namespace { |
| 499 | class CommaVisitor : public EvaluatedExprVisitor<CommaVisitor> { |
| 500 | typedef EvaluatedExprVisitor<CommaVisitor> Inherited; |
| 501 | Sema &SemaRef; |
| 502 | public: |
| 503 | CommaVisitor(Sema &SemaRef) : Inherited(SemaRef.Context), SemaRef(SemaRef) {} |
| 504 | void VisitBinaryOperator(BinaryOperator *E) { |
| 505 | if (E->getOpcode() == BO_Comma) |
| 506 | SemaRef.DiagnoseCommaOperator(E->getLHS(), E->getExprLoc()); |
| 507 | EvaluatedExprVisitor<CommaVisitor>::VisitBinaryOperator(E); |
| 508 | } |
| 509 | }; |
| 510 | } |
| 511 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 512 | StmtResult |
| Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 513 | Sema::ActOnIfStmt(SourceLocation IfLoc, bool IsConstexpr, Stmt *InitStmt, |
| 514 | ConditionResult Cond, |
| Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 515 | Stmt *thenStmt, SourceLocation ElseLoc, |
| 516 | Stmt *elseStmt) { |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 517 | if (Cond.isInvalid()) |
| 518 | Cond = ConditionResult( |
| 519 | *this, nullptr, |
| 520 | MakeFullExpr(new (Context) OpaqueValueExpr(SourceLocation(), |
| 521 | Context.BoolTy, VK_RValue), |
| 522 | IfLoc), |
| 523 | false); |
| Anders Carlsson | db83d77 | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 524 | |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 525 | Expr *CondExpr = Cond.get().second; |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 526 | if (!Diags.isIgnored(diag::warn_comma_operator, |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 527 | CondExpr->getExprLoc())) |
| 528 | CommaVisitor(*this).Visit(CondExpr); |
| 529 | |
| 530 | if (!elseStmt) |
| 531 | DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), thenStmt, |
| 532 | diag::warn_empty_if_body); |
| 533 | |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 534 | return BuildIfStmt(IfLoc, IsConstexpr, InitStmt, Cond, thenStmt, ElseLoc, |
| 535 | elseStmt); |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | StmtResult Sema::BuildIfStmt(SourceLocation IfLoc, bool IsConstexpr, |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 539 | Stmt *InitStmt, ConditionResult Cond, |
| 540 | Stmt *thenStmt, SourceLocation ElseLoc, |
| 541 | Stmt *elseStmt) { |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 542 | if (Cond.isInvalid()) |
| 543 | return StmtError(); |
| 544 | |
| Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 545 | if (IsConstexpr || isa<ObjCAvailabilityCheckExpr>(Cond.get().second)) |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 546 | getCurFunction()->setHasBranchProtectedScope(); |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 547 | |
| 548 | DiagnoseUnusedExprResult(thenStmt); |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 549 | DiagnoseUnusedExprResult(elseStmt); |
| 550 | |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 551 | return new (Context) |
| 552 | IfStmt(Context, IfLoc, IsConstexpr, InitStmt, Cond.get().first, |
| 553 | Cond.get().second, thenStmt, ElseLoc, elseStmt); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 554 | } |
| Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 555 | |
| Chris Lattner | 6799845 | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 556 | namespace { |
| 557 | struct CaseCompareFunctor { |
| 558 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 559 | const llvm::APSInt &RHS) { |
| 560 | return LHS.first < RHS; |
| 561 | } |
| Chris Lattner | 1463cca | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 562 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 563 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 564 | return LHS.first < RHS.first; |
| 565 | } |
| Chris Lattner | 6799845 | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 566 | bool operator()(const llvm::APSInt &LHS, |
| 567 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 568 | return LHS < RHS.first; |
| 569 | } |
| 570 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 571 | } |
| Chris Lattner | 6799845 | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 572 | |
| Chris Lattner | 4b2ff02 | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 573 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 574 | /// |
| 575 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 576 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 577 | if (lhs.first < rhs.first) |
| 578 | return true; |
| 579 | |
| 580 | if (lhs.first == rhs.first && |
| 581 | lhs.second->getCaseLoc().getRawEncoding() |
| 582 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 583 | return true; |
| 584 | return false; |
| 585 | } |
| 586 | |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 587 | /// CmpEnumVals - Comparison predicate for sorting enumeration values. |
| 588 | /// |
| 589 | static bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, |
| 590 | const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) |
| 591 | { |
| 592 | return lhs.first < rhs.first; |
| 593 | } |
| 594 | |
| 595 | /// EqEnumVals - Comparison preficate for uniqing enumeration values. |
| 596 | /// |
| 597 | static bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, |
| 598 | const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) |
| 599 | { |
| 600 | return lhs.first == rhs.first; |
| 601 | } |
| 602 | |
| Chris Lattner | a96d427 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 603 | /// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of |
| 604 | /// potentially integral-promoted expression @p expr. |
| Gabor Horvath | 64c3241 | 2017-08-09 08:57:09 +0000 | [diff] [blame] | 605 | static QualType GetTypeBeforeIntegralPromotion(const Expr *&E) { |
| 606 | if (const auto *CleanUps = dyn_cast<ExprWithCleanups>(E)) |
| 607 | E = CleanUps->getSubExpr(); |
| 608 | while (const auto *ImpCast = dyn_cast<ImplicitCastExpr>(E)) { |
| 609 | if (ImpCast->getCastKind() != CK_IntegralCast) break; |
| 610 | E = ImpCast->getSubExpr(); |
| Chris Lattner | a96d427 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 611 | } |
| Gabor Horvath | 64c3241 | 2017-08-09 08:57:09 +0000 | [diff] [blame] | 612 | return E->getType(); |
| Chris Lattner | a96d427 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 615 | ExprResult Sema::CheckSwitchCondition(SourceLocation SwitchLoc, Expr *Cond) { |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 616 | class SwitchConvertDiagnoser : public ICEConvertDiagnoser { |
| 617 | Expr *Cond; |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 618 | |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 619 | public: |
| 620 | SwitchConvertDiagnoser(Expr *Cond) |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 621 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/true, false, true), |
| 622 | Cond(Cond) {} |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 623 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 624 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 625 | QualType T) override { |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 626 | return S.Diag(Loc, diag::err_typecheck_statement_requires_integer) << T; |
| 627 | } |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 628 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 629 | SemaDiagnosticBuilder diagnoseIncomplete( |
| 630 | Sema &S, SourceLocation Loc, QualType T) override { |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 631 | return S.Diag(Loc, diag::err_switch_incomplete_class_type) |
| 632 | << T << Cond->getSourceRange(); |
| 633 | } |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 634 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 635 | SemaDiagnosticBuilder diagnoseExplicitConv( |
| 636 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 637 | return S.Diag(Loc, diag::err_switch_explicit_conversion) << T << ConvTy; |
| 638 | } |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 639 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 640 | SemaDiagnosticBuilder noteExplicitConv( |
| 641 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 642 | return S.Diag(Conv->getLocation(), diag::note_switch_conversion) |
| 643 | << ConvTy->isEnumeralType() << ConvTy; |
| 644 | } |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 645 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 646 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 647 | QualType T) override { |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 648 | return S.Diag(Loc, diag::err_switch_multiple_conversions) << T; |
| 649 | } |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 650 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 651 | SemaDiagnosticBuilder noteAmbiguous( |
| 652 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 653 | return S.Diag(Conv->getLocation(), diag::note_switch_conversion) |
| 654 | << ConvTy->isEnumeralType() << ConvTy; |
| 655 | } |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 656 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 657 | SemaDiagnosticBuilder diagnoseConversion( |
| 658 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 659 | llvm_unreachable("conversion functions are permitted"); |
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 660 | } |
| 661 | } SwitchDiagnoser(Cond); |
| 662 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 663 | ExprResult CondResult = |
| Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 664 | PerformContextualImplicitConversion(SwitchLoc, Cond, SwitchDiagnoser); |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 665 | if (CondResult.isInvalid()) |
| 666 | return ExprError(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 667 | |
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 668 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 669 | return UsualUnaryConversions(CondResult.get()); |
| 670 | } |
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 671 | |
| Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 672 | StmtResult Sema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, |
| 673 | Stmt *InitStmt, ConditionResult Cond) { |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 674 | if (Cond.isInvalid()) |
| Meador Inge | f0af05c | 2015-06-25 22:06:40 +0000 | [diff] [blame] | 675 | return StmtError(); |
| John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 676 | |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 677 | getCurFunction()->setHasBranchIntoScope(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 678 | |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 679 | SwitchStmt *SS = new (Context) |
| 680 | SwitchStmt(Context, InitStmt, Cond.get().first, Cond.get().second); |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 681 | getCurFunction()->SwitchStack.push_back(SS); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 682 | return SS; |
| Chris Lattner | 8fd2d01 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 685 | static void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) { |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 686 | Val = Val.extOrTrunc(BitWidth); |
| Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 687 | Val.setIsSigned(IsSigned); |
| 688 | } |
| 689 | |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 690 | /// Check the specified case value is in range for the given unpromoted switch |
| 691 | /// type. |
| 692 | static void checkCaseValue(Sema &S, SourceLocation Loc, const llvm::APSInt &Val, |
| 693 | unsigned UnpromotedWidth, bool UnpromotedSign) { |
| 694 | // If the case value was signed and negative and the switch expression is |
| 695 | // unsigned, don't bother to warn: this is implementation-defined behavior. |
| 696 | // FIXME: Introduce a second, default-ignored warning for this case? |
| 697 | if (UnpromotedWidth < Val.getBitWidth()) { |
| 698 | llvm::APSInt ConvVal(Val); |
| 699 | AdjustAPSInt(ConvVal, UnpromotedWidth, UnpromotedSign); |
| 700 | AdjustAPSInt(ConvVal, Val.getBitWidth(), Val.isSigned()); |
| 701 | // FIXME: Use different diagnostics for overflow in conversion to promoted |
| 702 | // type versus "switch expression cannot have this value". Use proper |
| 703 | // IntRange checking rather than just looking at the unpromoted type here. |
| 704 | if (ConvVal != Val) |
| 705 | S.Diag(Loc, diag::warn_case_value_overflow) << Val.toString(10) |
| 706 | << ConvVal.toString(10); |
| 707 | } |
| 708 | } |
| 709 | |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 710 | typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy; |
| 711 | |
| Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 712 | /// Returns true if we should emit a diagnostic about this case expression not |
| 713 | /// being a part of the enum used in the switch controlling expression. |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 714 | static bool ShouldDiagnoseSwitchCaseNotInEnum(const Sema &S, |
| Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 715 | const EnumDecl *ED, |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 716 | const Expr *CaseExpr, |
| 717 | EnumValsTy::iterator &EI, |
| 718 | EnumValsTy::iterator &EIEnd, |
| 719 | const llvm::APSInt &Val) { |
| Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 720 | if (!ED->isClosed()) |
| 721 | return false; |
| 722 | |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 723 | if (const DeclRefExpr *DRE = |
| 724 | dyn_cast<DeclRefExpr>(CaseExpr->IgnoreParenImpCasts())) { |
| Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 725 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 726 | QualType VarType = VD->getType(); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 727 | QualType EnumType = S.Context.getTypeDeclType(ED); |
| 728 | if (VD->hasGlobalStorage() && VarType.isConstQualified() && |
| 729 | S.Context.hasSameUnqualifiedType(EnumType, VarType)) |
| Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 730 | return false; |
| 731 | } |
| 732 | } |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 733 | |
| Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 734 | if (ED->hasAttr<FlagEnumAttr>()) |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 735 | return !S.IsValueInFlagEnum(ED, Val, false); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 736 | |
| Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 737 | while (EI != EIEnd && EI->first < Val) |
| 738 | EI++; |
| 739 | |
| 740 | if (EI != EIEnd && EI->first == Val) |
| 741 | return false; |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 742 | |
| Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 743 | return true; |
| 744 | } |
| 745 | |
| Gabor Horvath | 64c3241 | 2017-08-09 08:57:09 +0000 | [diff] [blame] | 746 | static void checkEnumTypesInSwitchStmt(Sema &S, const Expr *Cond, |
| 747 | const Expr *Case) { |
| 748 | QualType CondType = GetTypeBeforeIntegralPromotion(Cond); |
| 749 | QualType CaseType = Case->getType(); |
| 750 | |
| 751 | const EnumType *CondEnumType = CondType->getAs<EnumType>(); |
| 752 | const EnumType *CaseEnumType = CaseType->getAs<EnumType>(); |
| 753 | if (!CondEnumType || !CaseEnumType) |
| 754 | return; |
| 755 | |
| Gabor Horvath | b57e264 | 2017-08-09 12:34:58 +0000 | [diff] [blame] | 756 | // Ignore anonymous enums. |
| Richard Trieu | 285c936 | 2017-09-09 00:25:05 +0000 | [diff] [blame] | 757 | if (!CondEnumType->getDecl()->getIdentifier() && |
| 758 | !CondEnumType->getDecl()->getTypedefNameForAnonDecl()) |
| Gabor Horvath | b57e264 | 2017-08-09 12:34:58 +0000 | [diff] [blame] | 759 | return; |
| Richard Trieu | 285c936 | 2017-09-09 00:25:05 +0000 | [diff] [blame] | 760 | if (!CaseEnumType->getDecl()->getIdentifier() && |
| 761 | !CaseEnumType->getDecl()->getTypedefNameForAnonDecl()) |
| Gabor Horvath | b57e264 | 2017-08-09 12:34:58 +0000 | [diff] [blame] | 762 | return; |
| 763 | |
| Gabor Horvath | 64c3241 | 2017-08-09 08:57:09 +0000 | [diff] [blame] | 764 | if (S.Context.hasSameUnqualifiedType(CondType, CaseType)) |
| 765 | return; |
| 766 | |
| Gabor Horvath | 0284a20 | 2017-08-09 20:56:43 +0000 | [diff] [blame] | 767 | S.Diag(Case->getExprLoc(), diag::warn_comparison_of_mixed_enum_types_switch) |
| Gabor Horvath | 64c3241 | 2017-08-09 08:57:09 +0000 | [diff] [blame] | 768 | << CondType << CaseType << Cond->getSourceRange() |
| 769 | << Case->getSourceRange(); |
| 770 | } |
| 771 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 772 | StmtResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 773 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, |
| 774 | Stmt *BodyStmt) { |
| 775 | SwitchStmt *SS = cast<SwitchStmt>(Switch); |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 776 | assert(SS == getCurFunction()->SwitchStack.back() && |
| 777 | "switch stack missing push/pop!"); |
| Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 778 | |
| David Majnemer | 418ad3f | 2014-12-15 07:46:12 +0000 | [diff] [blame] | 779 | getCurFunction()->SwitchStack.pop_back(); |
| 780 | |
| Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 781 | if (!BodyStmt) return StmtError(); |
| Steve Naroff | 42a350a | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 782 | SS->setBody(BodyStmt, SwitchLoc); |
| Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 783 | |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 784 | Expr *CondExpr = SS->getCond(); |
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 785 | if (!CondExpr) return StmtError(); |
| 786 | |
| 787 | QualType CondType = CondExpr->getType(); |
| 788 | |
| Gabor Horvath | 64c3241 | 2017-08-09 08:57:09 +0000 | [diff] [blame] | 789 | const Expr *CondExprBeforePromotion = CondExpr; |
| Douglas Gregor | d0c22e0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 790 | QualType CondTypeBeforePromotion = |
| John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 791 | GetTypeBeforeIntegralPromotion(CondExprBeforePromotion); |
| Douglas Gregor | d0c22e0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 792 | |
| Chris Lattner | a96d427 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 793 | // C++ 6.4.2.p2: |
| 794 | // Integral promotions are performed (on the switch condition). |
| 795 | // |
| 796 | // A case value unrepresentable by the original switch condition |
| 797 | // type (before the promotion) doesn't make sense, even when it can |
| 798 | // be represented by the promoted type. Therefore we need to find |
| 799 | // the pre-promotion type of the switch condition. |
| Edward O'Callaghan | 93135aa | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 800 | if (!CondExpr->isTypeDependent()) { |
| Douglas Gregor | 5823da3 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 801 | // We have already converted the expression to an integral or enumeration |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 802 | // type, when we started the switch statement. If we don't have an |
| Douglas Gregor | 5823da3 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 803 | // appropriate type now, just return an error. |
| 804 | if (!CondType->isIntegralOrEnumerationType()) |
| Edward O'Callaghan | 93135aa | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 805 | return StmtError(); |
| Edward O'Callaghan | 93135aa | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 806 | |
| Chris Lattner | 4ebae65 | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 807 | if (CondExpr->isKnownToHaveBooleanValue()) { |
| Edward O'Callaghan | 93135aa | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 808 | // switch(bool_expr) {...} is often a programmer error, e.g. |
| 809 | // switch(n && mask) { ... } // Doh - should be "n & mask". |
| 810 | // One can always use an if statement instead of switch(bool_expr). |
| 811 | Diag(SwitchLoc, diag::warn_bool_switch_condition) |
| 812 | << CondExpr->getSourceRange(); |
| 813 | } |
| Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 814 | } |
| Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 815 | |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 816 | // Get the bitwidth of the switched-on value after promotions. We must |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 817 | // convert the integer case values to this width before comparison. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | bool HasDependentValue |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 819 | = CondExpr->isTypeDependent() || CondExpr->isValueDependent(); |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 820 | unsigned CondWidth = HasDependentValue ? 0 : Context.getIntWidth(CondType); |
| 821 | bool CondIsSigned = CondType->isSignedIntegerOrEnumerationType(); |
| 822 | |
| 823 | // Get the width and signedness that the condition might actually have, for |
| 824 | // warning purposes. |
| 825 | // FIXME: Grab an IntRange for the condition rather than using the unpromoted |
| 826 | // type. |
| 827 | unsigned CondWidthBeforePromotion |
| Chris Lattner | abcf38a | 2011-02-24 07:31:28 +0000 | [diff] [blame] | 828 | = HasDependentValue ? 0 : Context.getIntWidth(CondTypeBeforePromotion); |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 829 | bool CondIsSignedBeforePromotion |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 830 | = CondTypeBeforePromotion->isSignedIntegerOrEnumerationType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 832 | // Accumulate all of the case values in a vector so that we can sort them |
| 833 | // and detect duplicates. This vector contains the APInt for the case after |
| 834 | // it has been converted to the condition type. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 835 | typedef SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
| Chris Lattner | 6799845 | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 836 | CaseValsTy CaseVals; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 838 | // Keep track of any GNU case ranges we see. The APSInt is the low value. |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 839 | typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy; |
| 840 | CaseRangesTy CaseRanges; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 842 | DefaultStmt *TheDefaultStmt = nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
| Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 844 | bool CaseListIsErroneous = false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 846 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue; |
| Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 847 | SC = SC->getNextSwitchCase()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
| Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 849 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 850 | if (TheDefaultStmt) { |
| 851 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
| Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 852 | Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); |
| Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 853 | |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 854 | // FIXME: Remove the default statement from the switch block so that |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 855 | // we'll return a valid AST. This requires recursing down the AST and |
| 856 | // finding it, not something we are set up to do right now. For now, |
| 857 | // just lop the entire switch stmt out of the AST. |
| Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 858 | CaseListIsErroneous = true; |
| Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 859 | } |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 860 | TheDefaultStmt = DS; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 862 | } else { |
| 863 | CaseStmt *CS = cast<CaseStmt>(SC); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
| Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 865 | Expr *Lo = CS->getLHS(); |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 866 | |
| 867 | if (Lo->isTypeDependent() || Lo->isValueDependent()) { |
| 868 | HasDependentValue = true; |
| 869 | break; |
| 870 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | |
| Gabor Horvath | 64c3241 | 2017-08-09 08:57:09 +0000 | [diff] [blame] | 872 | checkEnumTypesInSwitchStmt(*this, CondExpr, Lo); |
| 873 | |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 874 | llvm::APSInt LoVal; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 876 | if (getLangOpts().CPlusPlus11) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 877 | // C++11 [stmt.switch]p2: the constant-expression shall be a converted |
| 878 | // constant expression of the promoted type of the switch condition. |
| 879 | ExprResult ConvLo = |
| 880 | CheckConvertedConstantExpression(Lo, CondType, LoVal, CCEK_CaseValue); |
| 881 | if (ConvLo.isInvalid()) { |
| 882 | CaseListIsErroneous = true; |
| 883 | continue; |
| 884 | } |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 885 | Lo = ConvLo.get(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 886 | } else { |
| 887 | // We already verified that the expression has a i-c-e value (C99 |
| 888 | // 6.8.4.2p3) - get that value now. |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 889 | LoVal = Lo->EvaluateKnownConstInt(Context); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 890 | |
| 891 | // If the LHS is not the same type as the condition, insert an implicit |
| 892 | // cast. |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 893 | Lo = DefaultLvalueConversion(Lo).get(); |
| 894 | Lo = ImpCastExprToType(Lo, CondType, CK_IntegralCast).get(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 897 | // Check the unconverted value is within the range of possible values of |
| 898 | // the switch expression. |
| 899 | checkCaseValue(*this, Lo->getLocStart(), LoVal, |
| 900 | CondWidthBeforePromotion, CondIsSignedBeforePromotion); |
| 901 | |
| 902 | // Convert the value to the same width/sign as the condition. |
| 903 | AdjustAPSInt(LoVal, CondWidth, CondIsSigned); |
| Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 904 | |
| Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 905 | CS->setLHS(Lo); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | |
| Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 907 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 908 | if (CS->getRHS()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | if (CS->getRHS()->isTypeDependent() || |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 910 | CS->getRHS()->isValueDependent()) { |
| 911 | HasDependentValue = true; |
| 912 | break; |
| 913 | } |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 914 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | } else |
| Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 916 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
| Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 919 | |
| 920 | if (!HasDependentValue) { |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 921 | // If we don't have a default statement, check whether the |
| 922 | // condition is constant. |
| 923 | llvm::APSInt ConstantCondValue; |
| 924 | bool HasConstantCond = false; |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 925 | if (!HasDependentValue && !TheDefaultStmt) { |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 926 | HasConstantCond = CondExpr->EvaluateAsInt(ConstantCondValue, Context, |
| 927 | Expr::SE_AllowSideEffects); |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 928 | assert(!HasConstantCond || |
| 929 | (ConstantCondValue.getBitWidth() == CondWidth && |
| 930 | ConstantCondValue.isSigned() == CondIsSigned)); |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 931 | } |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 932 | bool ShouldCheckConstantCond = HasConstantCond; |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 933 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 934 | // Sort all the scalar case values so we can easily detect duplicates. |
| 935 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
| 936 | |
| 937 | if (!CaseVals.empty()) { |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 938 | for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { |
| 939 | if (ShouldCheckConstantCond && |
| 940 | CaseVals[i].first == ConstantCondValue) |
| 941 | ShouldCheckConstantCond = false; |
| 942 | |
| 943 | if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) { |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 944 | // If we have a duplicate, report it. |
| Douglas Gregor | 9841df6 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 945 | // First, determine if either case value has a name |
| 946 | StringRef PrevString, CurrString; |
| 947 | Expr *PrevCase = CaseVals[i-1].second->getLHS()->IgnoreParenCasts(); |
| 948 | Expr *CurrCase = CaseVals[i].second->getLHS()->IgnoreParenCasts(); |
| 949 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(PrevCase)) { |
| 950 | PrevString = DeclRef->getDecl()->getName(); |
| 951 | } |
| 952 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(CurrCase)) { |
| 953 | CurrString = DeclRef->getDecl()->getName(); |
| 954 | } |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 955 | SmallString<16> CaseValStr; |
| Douglas Gregor | 0a9f4e0 | 2012-05-16 16:11:17 +0000 | [diff] [blame] | 956 | CaseVals[i-1].first.toString(CaseValStr); |
| Douglas Gregor | 9841df6 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 957 | |
| 958 | if (PrevString == CurrString) |
| 959 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 960 | diag::err_duplicate_case) << |
| Yaron Keren | 49c6369 | 2015-03-18 10:26:22 +0000 | [diff] [blame] | 961 | (PrevString.empty() ? StringRef(CaseValStr) : PrevString); |
| Douglas Gregor | 9841df6 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 962 | else |
| 963 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 964 | diag::err_duplicate_case_differing_expr) << |
| Yaron Keren | 49c6369 | 2015-03-18 10:26:22 +0000 | [diff] [blame] | 965 | (PrevString.empty() ? StringRef(CaseValStr) : PrevString) << |
| 966 | (CurrString.empty() ? StringRef(CaseValStr) : CurrString) << |
| Douglas Gregor | 9841df6 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 967 | CaseValStr; |
| 968 | |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 969 | Diag(CaseVals[i-1].second->getLHS()->getLocStart(), |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 970 | diag::note_duplicate_case_prev); |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 971 | // FIXME: We really want to remove the bogus case stmt from the |
| 972 | // substmt, but we have no way to do this right now. |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 973 | CaseListIsErroneous = true; |
| 974 | } |
| 975 | } |
| 976 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 978 | // Detect duplicate case ranges, which usually don't exist at all in |
| 979 | // the first place. |
| 980 | if (!CaseRanges.empty()) { |
| 981 | // Sort all the case ranges by their low value so we can easily detect |
| 982 | // overlaps between ranges. |
| 983 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 985 | // Scan the ranges, computing the high values and removing empty ranges. |
| 986 | std::vector<llvm::APSInt> HiVals; |
| 987 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 988 | llvm::APSInt &LoVal = CaseRanges[i].first; |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 989 | CaseStmt *CR = CaseRanges[i].second; |
| 990 | Expr *Hi = CR->getRHS(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 991 | llvm::APSInt HiVal; |
| 992 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 993 | if (getLangOpts().CPlusPlus11) { |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 994 | // C++11 [stmt.switch]p2: the constant-expression shall be a converted |
| 995 | // constant expression of the promoted type of the switch condition. |
| 996 | ExprResult ConvHi = |
| 997 | CheckConvertedConstantExpression(Hi, CondType, HiVal, |
| 998 | CCEK_CaseValue); |
| 999 | if (ConvHi.isInvalid()) { |
| 1000 | CaseListIsErroneous = true; |
| 1001 | continue; |
| 1002 | } |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1003 | Hi = ConvHi.get(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 1004 | } else { |
| 1005 | HiVal = Hi->EvaluateKnownConstInt(Context); |
| 1006 | |
| 1007 | // If the RHS is not the same type as the condition, insert an |
| 1008 | // implicit cast. |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1009 | Hi = DefaultLvalueConversion(Hi).get(); |
| 1010 | Hi = ImpCastExprToType(Hi, CondType, CK_IntegralCast).get(); |
| Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 1011 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1012 | |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 1013 | // Check the unconverted value is within the range of possible values of |
| 1014 | // the switch expression. |
| 1015 | checkCaseValue(*this, Hi->getLocStart(), HiVal, |
| 1016 | CondWidthBeforePromotion, CondIsSignedBeforePromotion); |
| 1017 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1018 | // Convert the value to the same width/sign as the condition. |
| Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 1019 | AdjustAPSInt(HiVal, CondWidth, CondIsSigned); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1021 | CR->setRHS(Hi); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1023 | // If the low value is bigger than the high value, the case is empty. |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1024 | if (LoVal > HiVal) { |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1025 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) |
| 1026 | << SourceRange(CR->getLHS()->getLocStart(), |
| Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 1027 | Hi->getLocEnd()); |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1028 | CaseRanges.erase(CaseRanges.begin()+i); |
| Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 1029 | --i; |
| 1030 | --e; |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1031 | continue; |
| 1032 | } |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1033 | |
| 1034 | if (ShouldCheckConstantCond && |
| 1035 | LoVal <= ConstantCondValue && |
| 1036 | ConstantCondValue <= HiVal) |
| 1037 | ShouldCheckConstantCond = false; |
| 1038 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1039 | HiVals.push_back(HiVal); |
| 1040 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1042 | // Rescan the ranges, looking for overlap with singleton values and other |
| 1043 | // ranges. Since the range list is sorted, we only need to compare case |
| 1044 | // ranges with their neighbors. |
| 1045 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
| 1046 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 1047 | llvm::APSInt &CRHi = HiVals[i]; |
| 1048 | CaseStmt *CR = CaseRanges[i].second; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1050 | // Check to see whether the case range overlaps with any |
| 1051 | // singleton cases. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1052 | CaseStmt *OverlapStmt = nullptr; |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1053 | llvm::APSInt OverlapVal(32); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1055 | // Find the smallest value >= the lower bound. If I is in the |
| 1056 | // case range, then we have overlap. |
| 1057 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 1058 | CaseVals.end(), CRLo, |
| 1059 | CaseCompareFunctor()); |
| 1060 | if (I != CaseVals.end() && I->first < CRHi) { |
| 1061 | OverlapVal = I->first; // Found overlap with scalar. |
| 1062 | OverlapStmt = I->second; |
| 1063 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1065 | // Find the smallest value bigger than the upper bound. |
| 1066 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 1067 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 1068 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 1069 | OverlapStmt = (I-1)->second; |
| 1070 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1072 | // Check to see if this case stmt overlaps with the subsequent |
| 1073 | // case range. |
| 1074 | if (i && CRLo <= HiVals[i-1]) { |
| 1075 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 1076 | OverlapStmt = CaseRanges[i-1].second; |
| 1077 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1079 | if (OverlapStmt) { |
| 1080 | // If we have a duplicate, report it. |
| 1081 | Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) |
| 1082 | << OverlapVal.toString(10); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | Diag(OverlapStmt->getLHS()->getLocStart(), |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1084 | diag::note_duplicate_case_prev); |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1085 | // FIXME: We really want to remove the bogus case stmt from the |
| 1086 | // substmt, but we have no way to do this right now. |
| Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1087 | CaseListIsErroneous = true; |
| 1088 | } |
| Chris Lattner | fcb920d | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 1089 | } |
| Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1090 | } |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1091 | |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1092 | // Complain if we have a constant condition and we didn't find a match. |
| 1093 | if (!CaseListIsErroneous && ShouldCheckConstantCond) { |
| 1094 | // TODO: it would be nice if we printed enums as enums, chars as |
| 1095 | // chars, etc. |
| 1096 | Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition) |
| 1097 | << ConstantCondValue.toString(10) |
| 1098 | << CondExpr->getSourceRange(); |
| 1099 | } |
| 1100 | |
| 1101 | // Check to see if switch is over an Enum and handles all of its |
| Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1102 | // values. We only issue a warning if there is not 'default:', but |
| 1103 | // we still do the analysis to preserve this information in the AST |
| 1104 | // (which can be used by flow-based analyes). |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1105 | // |
| Chris Lattner | 5167908 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1106 | const EnumType *ET = CondTypeBeforePromotion->getAs<EnumType>(); |
| Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1107 | |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1108 | // If switch has default case, then ignore it. |
| Alex Lorenz | 660195f | 2016-12-08 14:46:05 +0000 | [diff] [blame] | 1109 | if (!CaseListIsErroneous && !HasConstantCond && ET && |
| 1110 | ET->getDecl()->isCompleteDefinition()) { |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1111 | const EnumDecl *ED = ET->getDecl(); |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1112 | EnumValsTy EnumVals; |
| 1113 | |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1114 | // Gather all enum values, set their type and sort them, |
| 1115 | // allowing easier comparison with CaseVals. |
| Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1116 | for (auto *EDI : ED->enumerators()) { |
| Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 1117 | llvm::APSInt Val = EDI->getInitVal(); |
| 1118 | AdjustAPSInt(Val, CondWidth, CondIsSigned); |
| Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1119 | EnumVals.push_back(std::make_pair(Val, EDI)); |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1120 | } |
| 1121 | std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1122 | auto EI = EnumVals.begin(), EIEnd = |
| John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1123 | std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); |
| Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1124 | |
| 1125 | // See which case values aren't in enum. |
| David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1126 | for (CaseValsTy::const_iterator CI = CaseVals.begin(); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1127 | CI != CaseVals.end(); CI++) { |
| 1128 | Expr *CaseExpr = CI->second->getLHS(); |
| 1129 | if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd, |
| 1130 | CI->first)) |
| 1131 | Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum) |
| 1132 | << CondTypeBeforePromotion; |
| David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1133 | } |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1134 | |
| David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1135 | // See which of case ranges aren't in enum |
| 1136 | EI = EnumVals.begin(); |
| 1137 | for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1138 | RI != CaseRanges.end(); RI++) { |
| 1139 | Expr *CaseExpr = RI->second->getLHS(); |
| 1140 | if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd, |
| 1141 | RI->first)) |
| 1142 | Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum) |
| 1143 | << CondTypeBeforePromotion; |
| David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1144 | |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1145 | llvm::APSInt Hi = |
| David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1146 | RI->second->getRHS()->EvaluateKnownConstInt(Context); |
| 1147 | AdjustAPSInt(Hi, CondWidth, CondIsSigned); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1148 | |
| 1149 | CaseExpr = RI->second->getRHS(); |
| 1150 | if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd, |
| 1151 | Hi)) |
| 1152 | Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum) |
| 1153 | << CondTypeBeforePromotion; |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1154 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1155 | |
| Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1156 | // Check which enum vals aren't in switch |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1157 | auto CI = CaseVals.begin(); |
| 1158 | auto RI = CaseRanges.begin(); |
| Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1159 | bool hasCasesNotInSwitch = false; |
| 1160 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1161 | SmallVector<DeclarationName,8> UnhandledNames; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1162 | |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1163 | for (EI = EnumVals.begin(); EI != EIEnd; EI++){ |
| Chris Lattner | 5167908 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1164 | // Drop unneeded case values |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1165 | while (CI != CaseVals.end() && CI->first < EI->first) |
| 1166 | CI++; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1167 | |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1168 | if (CI != CaseVals.end() && CI->first == EI->first) |
| 1169 | continue; |
| 1170 | |
| Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1171 | // Drop unneeded case ranges |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1172 | for (; RI != CaseRanges.end(); RI++) { |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1173 | llvm::APSInt Hi = |
| 1174 | RI->second->getRHS()->EvaluateKnownConstInt(Context); |
| Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 1175 | AdjustAPSInt(Hi, CondWidth, CondIsSigned); |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1176 | if (EI->first <= Hi) |
| 1177 | break; |
| 1178 | } |
| 1179 | |
| Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1180 | if (RI == CaseRanges.end() || EI->first < RI->first) { |
| Ted Kremenek | 02627a2 | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1181 | hasCasesNotInSwitch = true; |
| David Blaikie | 645ae0c | 2012-01-21 18:12:07 +0000 | [diff] [blame] | 1182 | UnhandledNames.push_back(EI->second->getDeclName()); |
| Ted Kremenek | 02627a2 | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1183 | } |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1184 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1185 | |
| Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 1186 | if (TheDefaultStmt && UnhandledNames.empty() && ED->isClosedNonFlag()) |
| David Blaikie | 60ac638 | 2012-01-23 04:46:12 +0000 | [diff] [blame] | 1187 | Diag(TheDefaultStmt->getDefaultLoc(), diag::warn_unreachable_default); |
| David Blaikie | 645ae0c | 2012-01-21 18:12:07 +0000 | [diff] [blame] | 1188 | |
| Chris Lattner | 5167908 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1189 | // Produce a nice diagnostic if multiple values aren't handled. |
| Benjamin Kramer | 3a8650a | 2015-03-27 17:23:14 +0000 | [diff] [blame] | 1190 | if (!UnhandledNames.empty()) { |
| 1191 | DiagnosticBuilder DB = Diag(CondExpr->getExprLoc(), |
| 1192 | TheDefaultStmt ? diag::warn_def_missing_case |
| 1193 | : diag::warn_missing_case) |
| 1194 | << (int)UnhandledNames.size(); |
| 1195 | |
| 1196 | for (size_t I = 0, E = std::min(UnhandledNames.size(), (size_t)3); |
| 1197 | I != E; ++I) |
| 1198 | DB << UnhandledNames[I]; |
| Chris Lattner | 5167908 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1199 | } |
| Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1200 | |
| 1201 | if (!hasCasesNotInSwitch) |
| Ted Kremenek | 02627a2 | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1202 | SS->setAllEnumCasesCovered(); |
| Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1203 | } |
| Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1204 | } |
| Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1205 | |
| Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 1206 | if (BodyStmt) |
| 1207 | DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), BodyStmt, |
| 1208 | diag::warn_empty_switch_body); |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1209 | |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1210 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 1211 | // to patch it up. Instead, just return the whole substmt as broken. |
| Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1212 | if (CaseListIsErroneous) |
| Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 1213 | return StmtError(); |
| 1214 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1215 | return SS; |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1218 | void |
| 1219 | Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType, |
| 1220 | Expr *SrcExpr) { |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1221 | if (Diags.isIgnored(diag::warn_not_in_enum_assignment, SrcExpr->getExprLoc())) |
| Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1222 | return; |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1223 | |
| Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1224 | if (const EnumType *ET = DstType->getAs<EnumType>()) |
| Dmitri Gribenko | e6ac50a | 2013-12-05 23:06:53 +0000 | [diff] [blame] | 1225 | if (!Context.hasSameUnqualifiedType(SrcType, DstType) && |
| Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1226 | SrcType->isIntegerType()) { |
| 1227 | if (!SrcExpr->isTypeDependent() && !SrcExpr->isValueDependent() && |
| 1228 | SrcExpr->isIntegerConstantExpr(Context)) { |
| 1229 | // Get the bitwidth of the enum value before promotions. |
| Joey Gouly | 1ba2733 | 2013-06-06 13:48:00 +0000 | [diff] [blame] | 1230 | unsigned DstWidth = Context.getIntWidth(DstType); |
| Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1231 | bool DstIsSigned = DstType->isSignedIntegerOrEnumerationType(); |
| 1232 | |
| 1233 | llvm::APSInt RhsVal = SrcExpr->EvaluateKnownConstInt(Context); |
| Joey Gouly | 1ba2733 | 2013-06-06 13:48:00 +0000 | [diff] [blame] | 1234 | AdjustAPSInt(RhsVal, DstWidth, DstIsSigned); |
| Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1235 | const EnumDecl *ED = ET->getDecl(); |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1236 | |
| Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 1237 | if (!ED->isClosed()) |
| 1238 | return; |
| 1239 | |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1240 | if (ED->hasAttr<FlagEnumAttr>()) { |
| 1241 | if (!IsValueInFlagEnum(ED, RhsVal, true)) |
| 1242 | Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignment) |
| Dmitri Gribenko | e6ac50a | 2013-12-05 23:06:53 +0000 | [diff] [blame] | 1243 | << DstType.getUnqualifiedType(); |
| Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1244 | } else { |
| 1245 | typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl *>, 64> |
| 1246 | EnumValsTy; |
| 1247 | EnumValsTy EnumVals; |
| 1248 | |
| 1249 | // Gather all enum values, set their type and sort them, |
| 1250 | // allowing easier comparison with rhs constant. |
| 1251 | for (auto *EDI : ED->enumerators()) { |
| 1252 | llvm::APSInt Val = EDI->getInitVal(); |
| 1253 | AdjustAPSInt(Val, DstWidth, DstIsSigned); |
| 1254 | EnumVals.push_back(std::make_pair(Val, EDI)); |
| 1255 | } |
| 1256 | if (EnumVals.empty()) |
| 1257 | return; |
| 1258 | std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); |
| 1259 | EnumValsTy::iterator EIend = |
| 1260 | std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); |
| 1261 | |
| 1262 | // See which values aren't in the enum. |
| 1263 | EnumValsTy::const_iterator EI = EnumVals.begin(); |
| 1264 | while (EI != EIend && EI->first < RhsVal) |
| 1265 | EI++; |
| 1266 | if (EI == EIend || EI->first != RhsVal) { |
| 1267 | Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignment) |
| 1268 | << DstType.getUnqualifiedType(); |
| 1269 | } |
| Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1275 | StmtResult Sema::ActOnWhileStmt(SourceLocation WhileLoc, ConditionResult Cond, |
| 1276 | Stmt *Body) { |
| 1277 | if (Cond.isInvalid()) |
| Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1278 | return StmtError(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1279 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1280 | auto CondVal = Cond.get(); |
| 1281 | CheckBreakContinueBinding(CondVal.second); |
| 1282 | |
| 1283 | if (CondVal.second && |
| 1284 | !Diags.isIgnored(diag::warn_comma_operator, CondVal.second->getExprLoc())) |
| 1285 | CommaVisitor(*this).Visit(CondVal.second); |
| Richard Trieu | faca2d8 | 2016-02-18 23:58:40 +0000 | [diff] [blame] | 1286 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1287 | DiagnoseUnusedExprResult(Body); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1289 | if (isa<NullStmt>(Body)) |
| 1290 | getCurCompoundScope().setHasEmptyLoopBodies(); |
| 1291 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1292 | return new (Context) |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1293 | WhileStmt(Context, CondVal.first, CondVal.second, Body, WhileLoc); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1296 | StmtResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1297 | Sema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, |
| Chris Lattner | 815b70e | 2009-06-12 23:04:47 +0000 | [diff] [blame] | 1298 | SourceLocation WhileLoc, SourceLocation CondLParen, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1299 | Expr *Cond, SourceLocation CondRParen) { |
| 1300 | assert(Cond && "ActOnDoStmt(): missing expression"); |
| Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1301 | |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1302 | CheckBreakContinueBinding(Cond); |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1303 | ExprResult CondResult = CheckBooleanCondition(DoLoc, Cond); |
| Dmitri Gribenko | d4bc5ac | 2012-11-18 22:28:42 +0000 | [diff] [blame] | 1304 | if (CondResult.isInvalid()) |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 1305 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1306 | Cond = CondResult.get(); |
| Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 1307 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1308 | CondResult = ActOnFinishFullExpr(Cond, DoLoc); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1309 | if (CondResult.isInvalid()) |
| Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1310 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1311 | Cond = CondResult.get(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1312 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1313 | DiagnoseUnusedExprResult(Body); |
| Anders Carlsson | 5c5f160 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 1314 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1315 | return new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1318 | namespace { |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1319 | // Use SetVector since the diagnostic cares about the ordering of the Decl's. |
| 1320 | using DeclSetVector = |
| 1321 | llvm::SetVector<VarDecl *, llvm::SmallVector<VarDecl *, 8>, |
| 1322 | llvm::SmallPtrSet<VarDecl *, 8>>; |
| 1323 | |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1324 | // This visitor will traverse a conditional statement and store all |
| 1325 | // the evaluated decls into a vector. Simple is set to true if none |
| 1326 | // of the excluded constructs are used. |
| 1327 | class DeclExtractor : public EvaluatedExprVisitor<DeclExtractor> { |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1328 | DeclSetVector &Decls; |
| Craig Topper | fa159c1 | 2013-07-14 16:47:36 +0000 | [diff] [blame] | 1329 | SmallVectorImpl<SourceRange> &Ranges; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1330 | bool Simple; |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1331 | public: |
| 1332 | typedef EvaluatedExprVisitor<DeclExtractor> Inherited; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1333 | |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1334 | DeclExtractor(Sema &S, DeclSetVector &Decls, |
| Craig Topper | fa159c1 | 2013-07-14 16:47:36 +0000 | [diff] [blame] | 1335 | SmallVectorImpl<SourceRange> &Ranges) : |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1336 | Inherited(S.Context), |
| 1337 | Decls(Decls), |
| 1338 | Ranges(Ranges), |
| 1339 | Simple(true) {} |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1340 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1341 | bool isSimple() { return Simple; } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1342 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1343 | // Replaces the method in EvaluatedExprVisitor. |
| 1344 | void VisitMemberExpr(MemberExpr* E) { |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1345 | Simple = false; |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | // Any Stmt not whitelisted will cause the condition to be marked complex. |
| 1349 | void VisitStmt(Stmt *S) { |
| 1350 | Simple = false; |
| 1351 | } |
| 1352 | |
| 1353 | void VisitBinaryOperator(BinaryOperator *E) { |
| 1354 | Visit(E->getLHS()); |
| 1355 | Visit(E->getRHS()); |
| 1356 | } |
| 1357 | |
| 1358 | void VisitCastExpr(CastExpr *E) { |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1359 | Visit(E->getSubExpr()); |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1360 | } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1361 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1362 | void VisitUnaryOperator(UnaryOperator *E) { |
| 1363 | // Skip checking conditionals with derefernces. |
| 1364 | if (E->getOpcode() == UO_Deref) |
| 1365 | Simple = false; |
| 1366 | else |
| 1367 | Visit(E->getSubExpr()); |
| 1368 | } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1369 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1370 | void VisitConditionalOperator(ConditionalOperator *E) { |
| 1371 | Visit(E->getCond()); |
| 1372 | Visit(E->getTrueExpr()); |
| 1373 | Visit(E->getFalseExpr()); |
| 1374 | } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1375 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1376 | void VisitParenExpr(ParenExpr *E) { |
| 1377 | Visit(E->getSubExpr()); |
| 1378 | } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1379 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1380 | void VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
| 1381 | Visit(E->getOpaqueValue()->getSourceExpr()); |
| 1382 | Visit(E->getFalseExpr()); |
| 1383 | } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1384 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1385 | void VisitIntegerLiteral(IntegerLiteral *E) { } |
| 1386 | void VisitFloatingLiteral(FloatingLiteral *E) { } |
| 1387 | void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { } |
| 1388 | void VisitCharacterLiteral(CharacterLiteral *E) { } |
| 1389 | void VisitGNUNullExpr(GNUNullExpr *E) { } |
| 1390 | void VisitImaginaryLiteral(ImaginaryLiteral *E) { } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1391 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1392 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 1393 | VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); |
| 1394 | if (!VD) return; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1395 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1396 | Ranges.push_back(E->getSourceRange()); |
| 1397 | |
| 1398 | Decls.insert(VD); |
| 1399 | } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1400 | |
| 1401 | }; // end class DeclExtractor |
| 1402 | |
| Sanjay Patel | 69e7f6e | 2015-08-28 14:42:54 +0000 | [diff] [blame] | 1403 | // DeclMatcher checks to see if the decls are used in a non-evaluated |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1404 | // context. |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1405 | class DeclMatcher : public EvaluatedExprVisitor<DeclMatcher> { |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1406 | DeclSetVector &Decls; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1407 | bool FoundDecl; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1408 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1409 | public: |
| 1410 | typedef EvaluatedExprVisitor<DeclMatcher> Inherited; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1411 | |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1412 | DeclMatcher(Sema &S, DeclSetVector &Decls, Stmt *Statement) : |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1413 | Inherited(S.Context), Decls(Decls), FoundDecl(false) { |
| 1414 | if (!Statement) return; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1415 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1416 | Visit(Statement); |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1419 | void VisitReturnStmt(ReturnStmt *S) { |
| 1420 | FoundDecl = true; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1423 | void VisitBreakStmt(BreakStmt *S) { |
| 1424 | FoundDecl = true; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1427 | void VisitGotoStmt(GotoStmt *S) { |
| 1428 | FoundDecl = true; |
| 1429 | } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1430 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1431 | void VisitCastExpr(CastExpr *E) { |
| 1432 | if (E->getCastKind() == CK_LValueToRValue) |
| 1433 | CheckLValueToRValueCast(E->getSubExpr()); |
| 1434 | else |
| 1435 | Visit(E->getSubExpr()); |
| 1436 | } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1437 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1438 | void CheckLValueToRValueCast(Expr *E) { |
| 1439 | E = E->IgnoreParenImpCasts(); |
| 1440 | |
| 1441 | if (isa<DeclRefExpr>(E)) { |
| 1442 | return; |
| 1443 | } |
| 1444 | |
| 1445 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 1446 | Visit(CO->getCond()); |
| 1447 | CheckLValueToRValueCast(CO->getTrueExpr()); |
| 1448 | CheckLValueToRValueCast(CO->getFalseExpr()); |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | if (BinaryConditionalOperator *BCO = |
| 1453 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 1454 | CheckLValueToRValueCast(BCO->getOpaqueValue()->getSourceExpr()); |
| 1455 | CheckLValueToRValueCast(BCO->getFalseExpr()); |
| 1456 | return; |
| 1457 | } |
| 1458 | |
| 1459 | Visit(E); |
| 1460 | } |
| 1461 | |
| 1462 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 1463 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
| 1464 | if (Decls.count(VD)) |
| 1465 | FoundDecl = true; |
| 1466 | } |
| 1467 | |
| Steven Wu | 92910f6 | 2016-03-10 02:02:48 +0000 | [diff] [blame] | 1468 | void VisitPseudoObjectExpr(PseudoObjectExpr *POE) { |
| 1469 | // Only need to visit the semantics for POE. |
| 1470 | // SyntaticForm doesn't really use the Decal. |
| 1471 | for (auto *S : POE->semantics()) { |
| 1472 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(S)) |
| 1473 | // Look past the OVE into the expression it binds. |
| 1474 | Visit(OVE->getSourceExpr()); |
| 1475 | else |
| 1476 | Visit(S); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1480 | bool FoundDeclInUse() { return FoundDecl; } |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1481 | |
| 1482 | }; // end class DeclMatcher |
| 1483 | |
| 1484 | void CheckForLoopConditionalStatement(Sema &S, Expr *Second, |
| 1485 | Expr *Third, Stmt *Body) { |
| 1486 | // Condition is empty |
| 1487 | if (!Second) return; |
| 1488 | |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1489 | if (S.Diags.isIgnored(diag::warn_variables_not_in_loop_body, |
| 1490 | Second->getLocStart())) |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1491 | return; |
| 1492 | |
| 1493 | PartialDiagnostic PDiag = S.PDiag(diag::warn_variables_not_in_loop_body); |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1494 | DeclSetVector Decls; |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1495 | SmallVector<SourceRange, 10> Ranges; |
| Benjamin Kramer | d1d76b2 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 1496 | DeclExtractor DE(S, Decls, Ranges); |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1497 | DE.Visit(Second); |
| 1498 | |
| 1499 | // Don't analyze complex conditionals. |
| 1500 | if (!DE.isSimple()) return; |
| 1501 | |
| 1502 | // No decls found. |
| 1503 | if (Decls.size() == 0) return; |
| 1504 | |
| Richard Trieu | 0030f1d | 2012-05-04 03:01:54 +0000 | [diff] [blame] | 1505 | // Don't warn on volatile, static, or global variables. |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1506 | for (auto *VD : Decls) |
| 1507 | if (VD->getType().isVolatileQualified() || VD->hasGlobalStorage()) |
| 1508 | return; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1509 | |
| 1510 | if (DeclMatcher(S, Decls, Second).FoundDeclInUse() || |
| 1511 | DeclMatcher(S, Decls, Third).FoundDeclInUse() || |
| 1512 | DeclMatcher(S, Decls, Body).FoundDeclInUse()) |
| 1513 | return; |
| 1514 | |
| 1515 | // Load decl names into diagnostic. |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1516 | if (Decls.size() > 4) { |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1517 | PDiag << 0; |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1518 | } else { |
| 1519 | PDiag << (unsigned)Decls.size(); |
| 1520 | for (auto *VD : Decls) |
| 1521 | PDiag << VD->getDeclName(); |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| Richard Trieu | 5fb874a | 2017-06-02 04:24:46 +0000 | [diff] [blame] | 1524 | for (auto Range : Ranges) |
| 1525 | PDiag << Range; |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1526 | |
| 1527 | S.Diag(Ranges.begin()->getBegin(), PDiag); |
| 1528 | } |
| 1529 | |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1530 | // If Statement is an incemement or decrement, return true and sets the |
| 1531 | // variables Increment and DRE. |
| 1532 | bool ProcessIterationStmt(Sema &S, Stmt* Statement, bool &Increment, |
| 1533 | DeclRefExpr *&DRE) { |
| Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 1534 | if (auto Cleanups = dyn_cast<ExprWithCleanups>(Statement)) |
| 1535 | if (!Cleanups->cleanupsHaveSideEffects()) |
| 1536 | Statement = Cleanups->getSubExpr(); |
| 1537 | |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1538 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(Statement)) { |
| 1539 | switch (UO->getOpcode()) { |
| 1540 | default: return false; |
| 1541 | case UO_PostInc: |
| 1542 | case UO_PreInc: |
| 1543 | Increment = true; |
| 1544 | break; |
| 1545 | case UO_PostDec: |
| 1546 | case UO_PreDec: |
| 1547 | Increment = false; |
| 1548 | break; |
| 1549 | } |
| 1550 | DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr()); |
| 1551 | return DRE; |
| 1552 | } |
| 1553 | |
| 1554 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(Statement)) { |
| 1555 | FunctionDecl *FD = Call->getDirectCallee(); |
| 1556 | if (!FD || !FD->isOverloadedOperator()) return false; |
| 1557 | switch (FD->getOverloadedOperator()) { |
| 1558 | default: return false; |
| 1559 | case OO_PlusPlus: |
| 1560 | Increment = true; |
| 1561 | break; |
| 1562 | case OO_MinusMinus: |
| 1563 | Increment = false; |
| 1564 | break; |
| 1565 | } |
| 1566 | DRE = dyn_cast<DeclRefExpr>(Call->getArg(0)); |
| 1567 | return DRE; |
| 1568 | } |
| 1569 | |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1573 | // A visitor to determine if a continue or break statement is a |
| 1574 | // subexpression. |
| Eli Friedman | e91b2e6 | 2017-07-04 00:52:24 +0000 | [diff] [blame] | 1575 | class BreakContinueFinder : public ConstEvaluatedExprVisitor<BreakContinueFinder> { |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1576 | SourceLocation BreakLoc; |
| 1577 | SourceLocation ContinueLoc; |
| Eli Friedman | e91b2e6 | 2017-07-04 00:52:24 +0000 | [diff] [blame] | 1578 | bool InSwitch = false; |
| 1579 | |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1580 | public: |
| Eli Friedman | e91b2e6 | 2017-07-04 00:52:24 +0000 | [diff] [blame] | 1581 | BreakContinueFinder(Sema &S, const Stmt* Body) : |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1582 | Inherited(S.Context) { |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1583 | Visit(Body); |
| 1584 | } |
| 1585 | |
| Eli Friedman | e91b2e6 | 2017-07-04 00:52:24 +0000 | [diff] [blame] | 1586 | typedef ConstEvaluatedExprVisitor<BreakContinueFinder> Inherited; |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1587 | |
| Eli Friedman | e91b2e6 | 2017-07-04 00:52:24 +0000 | [diff] [blame] | 1588 | void VisitContinueStmt(const ContinueStmt* E) { |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1589 | ContinueLoc = E->getContinueLoc(); |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| Eli Friedman | e91b2e6 | 2017-07-04 00:52:24 +0000 | [diff] [blame] | 1592 | void VisitBreakStmt(const BreakStmt* E) { |
| 1593 | if (!InSwitch) |
| 1594 | BreakLoc = E->getBreakLoc(); |
| 1595 | } |
| 1596 | |
| 1597 | void VisitSwitchStmt(const SwitchStmt* S) { |
| 1598 | if (const Stmt *Init = S->getInit()) |
| 1599 | Visit(Init); |
| 1600 | if (const Stmt *CondVar = S->getConditionVariableDeclStmt()) |
| 1601 | Visit(CondVar); |
| 1602 | if (const Stmt *Cond = S->getCond()) |
| 1603 | Visit(Cond); |
| 1604 | |
| 1605 | // Don't return break statements from the body of a switch. |
| 1606 | InSwitch = true; |
| 1607 | if (const Stmt *Body = S->getBody()) |
| 1608 | Visit(Body); |
| 1609 | InSwitch = false; |
| 1610 | } |
| 1611 | |
| 1612 | void VisitForStmt(const ForStmt *S) { |
| 1613 | // Only visit the init statement of a for loop; the body |
| 1614 | // has a different break/continue scope. |
| 1615 | if (const Stmt *Init = S->getInit()) |
| 1616 | Visit(Init); |
| 1617 | } |
| 1618 | |
| 1619 | void VisitWhileStmt(const WhileStmt *) { |
| 1620 | // Do nothing; the children of a while loop have a different |
| 1621 | // break/continue scope. |
| 1622 | } |
| 1623 | |
| 1624 | void VisitDoStmt(const DoStmt *) { |
| 1625 | // Do nothing; the children of a while loop have a different |
| 1626 | // break/continue scope. |
| 1627 | } |
| 1628 | |
| 1629 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
| 1630 | // Only visit the initialization of a for loop; the body |
| 1631 | // has a different break/continue scope. |
| 1632 | if (const Stmt *Range = S->getRangeStmt()) |
| 1633 | Visit(Range); |
| 1634 | if (const Stmt *Begin = S->getBeginStmt()) |
| 1635 | Visit(Begin); |
| 1636 | if (const Stmt *End = S->getEndStmt()) |
| 1637 | Visit(End); |
| 1638 | } |
| 1639 | |
| 1640 | void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { |
| 1641 | // Only visit the initialization of a for loop; the body |
| 1642 | // has a different break/continue scope. |
| 1643 | if (const Stmt *Element = S->getElement()) |
| 1644 | Visit(Element); |
| 1645 | if (const Stmt *Collection = S->getCollection()) |
| 1646 | Visit(Collection); |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1647 | } |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1648 | |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1649 | bool ContinueFound() { return ContinueLoc.isValid(); } |
| 1650 | bool BreakFound() { return BreakLoc.isValid(); } |
| 1651 | SourceLocation GetContinueLoc() { return ContinueLoc; } |
| 1652 | SourceLocation GetBreakLoc() { return BreakLoc; } |
| 1653 | |
| 1654 | }; // end class BreakContinueFinder |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1655 | |
| 1656 | // Emit a warning when a loop increment/decrement appears twice per loop |
| 1657 | // iteration. The conditions which trigger this warning are: |
| 1658 | // 1) The last statement in the loop body and the third expression in the |
| 1659 | // for loop are both increment or both decrement of the same variable |
| 1660 | // 2) No continue statements in the loop body. |
| 1661 | void CheckForRedundantIteration(Sema &S, Expr *Third, Stmt *Body) { |
| 1662 | // Return when there is nothing to check. |
| 1663 | if (!Body || !Third) return; |
| 1664 | |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1665 | if (S.Diags.isIgnored(diag::warn_redundant_loop_iteration, |
| 1666 | Third->getLocStart())) |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1667 | return; |
| 1668 | |
| 1669 | // Get the last statement from the loop body. |
| 1670 | CompoundStmt *CS = dyn_cast<CompoundStmt>(Body); |
| 1671 | if (!CS || CS->body_empty()) return; |
| 1672 | Stmt *LastStmt = CS->body_back(); |
| 1673 | if (!LastStmt) return; |
| 1674 | |
| 1675 | bool LoopIncrement, LastIncrement; |
| 1676 | DeclRefExpr *LoopDRE, *LastDRE; |
| 1677 | |
| 1678 | if (!ProcessIterationStmt(S, Third, LoopIncrement, LoopDRE)) return; |
| 1679 | if (!ProcessIterationStmt(S, LastStmt, LastIncrement, LastDRE)) return; |
| 1680 | |
| 1681 | // Check that the two statements are both increments or both decrements |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1682 | // on the same variable. |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1683 | if (LoopIncrement != LastIncrement || |
| 1684 | LoopDRE->getDecl() != LastDRE->getDecl()) return; |
| 1685 | |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1686 | if (BreakContinueFinder(S, Body).ContinueFound()) return; |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1687 | |
| 1688 | S.Diag(LastDRE->getLocation(), diag::warn_redundant_loop_iteration) |
| 1689 | << LastDRE->getDecl() << LastIncrement; |
| 1690 | S.Diag(LoopDRE->getLocation(), diag::note_loop_iteration_here) |
| 1691 | << LoopIncrement; |
| 1692 | } |
| 1693 | |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1694 | } // end namespace |
| 1695 | |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1696 | |
| 1697 | void Sema::CheckBreakContinueBinding(Expr *E) { |
| 1698 | if (!E || getLangOpts().CPlusPlus) |
| 1699 | return; |
| 1700 | BreakContinueFinder BCFinder(*this, E); |
| 1701 | Scope *BreakParent = CurScope->getBreakParent(); |
| 1702 | if (BCFinder.BreakFound() && BreakParent) { |
| 1703 | if (BreakParent->getFlags() & Scope::SwitchScope) { |
| 1704 | Diag(BCFinder.GetBreakLoc(), diag::warn_break_binds_to_switch); |
| 1705 | } else { |
| 1706 | Diag(BCFinder.GetBreakLoc(), diag::warn_loop_ctrl_binds_to_inner) |
| 1707 | << "break"; |
| 1708 | } |
| 1709 | } else if (BCFinder.ContinueFound() && CurScope->getContinueParent()) { |
| 1710 | Diag(BCFinder.GetContinueLoc(), diag::warn_loop_ctrl_binds_to_inner) |
| 1711 | << "continue"; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1715 | StmtResult Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
| 1716 | Stmt *First, ConditionResult Second, |
| 1717 | FullExprArg third, SourceLocation RParenLoc, |
| 1718 | Stmt *Body) { |
| 1719 | if (Second.isInvalid()) |
| 1720 | return StmtError(); |
| 1721 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1722 | if (!getLangOpts().CPlusPlus) { |
| Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1723 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1724 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 1725 | // declare identifiers for objects having storage class 'auto' or |
| 1726 | // 'register'. |
| Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 1727 | for (auto *DI : DS->decls()) { |
| 1728 | VarDecl *VD = dyn_cast<VarDecl>(DI); |
| John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1729 | if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1730 | VD = nullptr; |
| 1731 | if (!VD) { |
| Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 1732 | Diag(DI->getLocation(), diag::err_non_local_variable_decl_in_for); |
| 1733 | DI->setInvalidDecl(); |
| Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1734 | } |
| Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1735 | } |
| Chris Lattner | 39f920f | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 1736 | } |
| Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 1737 | } |
| Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1738 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1739 | CheckBreakContinueBinding(Second.get().second); |
| Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1740 | CheckBreakContinueBinding(third.get()); |
| 1741 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1742 | if (!Second.get().first) |
| 1743 | CheckForLoopConditionalStatement(*this, Second.get().second, third.get(), |
| 1744 | Body); |
| Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1745 | CheckForRedundantIteration(*this, third.get(), Body); |
| Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1746 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1747 | if (Second.get().second && |
| Richard Trieu | faca2d8 | 2016-02-18 23:58:40 +0000 | [diff] [blame] | 1748 | !Diags.isIgnored(diag::warn_comma_operator, |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1749 | Second.get().second->getExprLoc())) |
| 1750 | CommaVisitor(*this).Visit(Second.get().second); |
| Richard Trieu | faca2d8 | 2016-02-18 23:58:40 +0000 | [diff] [blame] | 1751 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1752 | Expr *Third = third.release().getAs<Expr>(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1753 | |
| Anders Carlsson | 1682af5 | 2009-08-01 01:39:59 +0000 | [diff] [blame] | 1754 | DiagnoseUnusedExprResult(First); |
| 1755 | DiagnoseUnusedExprResult(Third); |
| Anders Carlsson | 5c5f160 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 1756 | DiagnoseUnusedExprResult(Body); |
| 1757 | |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1758 | if (isa<NullStmt>(Body)) |
| 1759 | getCurCompoundScope().setHasEmptyLoopBodies(); |
| 1760 | |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1761 | return new (Context) |
| 1762 | ForStmt(Context, First, Second.get().second, Second.get().first, Third, |
| 1763 | Body, ForLoc, LParenLoc, RParenLoc); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1766 | /// In an Objective C collection iteration statement: |
| 1767 | /// for (x in y) |
| 1768 | /// x can be an arbitrary l-value expression. Bind it up as a |
| 1769 | /// full-expression. |
| 1770 | StmtResult Sema::ActOnForEachLValueExpr(Expr *E) { |
| John McCall | bc15335 | 2012-03-30 05:43:39 +0000 | [diff] [blame] | 1771 | // Reduce placeholder expressions here. Note that this rejects the |
| 1772 | // use of pseudo-object l-values in this position. |
| 1773 | ExprResult result = CheckPlaceholderExpr(E); |
| 1774 | if (result.isInvalid()) return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1775 | E = result.get(); |
| John McCall | bc15335 | 2012-03-30 05:43:39 +0000 | [diff] [blame] | 1776 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1777 | ExprResult FullExpr = ActOnFinishFullExpr(E); |
| 1778 | if (FullExpr.isInvalid()) |
| 1779 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1780 | return StmtResult(static_cast<Stmt*>(FullExpr.get())); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1783 | ExprResult |
| Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1784 | Sema::CheckObjCForCollectionOperand(SourceLocation forLoc, Expr *collection) { |
| 1785 | if (!collection) |
| 1786 | return ExprError(); |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1787 | |
| Kaelyn Takata | 1586782 | 2014-11-21 18:48:04 +0000 | [diff] [blame] | 1788 | ExprResult result = CorrectDelayedTyposInExpr(collection); |
| 1789 | if (!result.isUsable()) |
| 1790 | return ExprError(); |
| 1791 | collection = result.get(); |
| 1792 | |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1793 | // Bail out early if we've got a type-dependent expression. |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1794 | if (collection->isTypeDependent()) return collection; |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1795 | |
| 1796 | // Perform normal l-value conversion. |
| Kaelyn Takata | 1586782 | 2014-11-21 18:48:04 +0000 | [diff] [blame] | 1797 | result = DefaultFunctionArrayLvalueConversion(collection); |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1798 | if (result.isInvalid()) |
| 1799 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1800 | collection = result.get(); |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1801 | |
| 1802 | // The operand needs to have object-pointer type. |
| 1803 | // TODO: should we do a contextual conversion? |
| 1804 | const ObjCObjectPointerType *pointerType = |
| 1805 | collection->getType()->getAs<ObjCObjectPointerType>(); |
| 1806 | if (!pointerType) |
| 1807 | return Diag(forLoc, diag::err_collection_expr_type) |
| 1808 | << collection->getType() << collection->getSourceRange(); |
| 1809 | |
| 1810 | // Check that the operand provides |
| 1811 | // - countByEnumeratingWithState:objects:count: |
| 1812 | const ObjCObjectType *objectType = pointerType->getObjectType(); |
| 1813 | ObjCInterfaceDecl *iface = objectType->getInterface(); |
| 1814 | |
| 1815 | // If we have a forward-declared type, we can't do this check. |
| Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1816 | // Under ARC, it is an error not to have a forward-declared class. |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1817 | if (iface && |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 1818 | (getLangOpts().ObjCAutoRefCount |
| 1819 | ? RequireCompleteType(forLoc, QualType(objectType, 0), |
| 1820 | diag::err_arc_collection_forward, collection) |
| 1821 | : !isCompleteType(forLoc, QualType(objectType, 0)))) { |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1822 | // Otherwise, if we have any useful type information, check that |
| 1823 | // the type declares the appropriate method. |
| 1824 | } else if (iface || !objectType->qual_empty()) { |
| 1825 | IdentifierInfo *selectorIdents[] = { |
| 1826 | &Context.Idents.get("countByEnumeratingWithState"), |
| 1827 | &Context.Idents.get("objects"), |
| 1828 | &Context.Idents.get("count") |
| 1829 | }; |
| 1830 | Selector selector = Context.Selectors.getSelector(3, &selectorIdents[0]); |
| 1831 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1832 | ObjCMethodDecl *method = nullptr; |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1833 | |
| 1834 | // If there's an interface, look in both the public and private APIs. |
| 1835 | if (iface) { |
| 1836 | method = iface->lookupInstanceMethod(selector); |
| Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 1837 | if (!method) method = iface->lookupPrivateMethod(selector); |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | // Also check protocol qualifiers. |
| 1841 | if (!method) |
| 1842 | method = LookupMethodInQualifiedType(selector, pointerType, |
| 1843 | /*instance*/ true); |
| 1844 | |
| 1845 | // If we didn't find it anywhere, give up. |
| 1846 | if (!method) { |
| 1847 | Diag(forLoc, diag::warn_collection_expr_type) |
| 1848 | << collection->getType() << selector << collection->getSourceRange(); |
| 1849 | } |
| 1850 | |
| 1851 | // TODO: check for an incompatible signature? |
| 1852 | } |
| 1853 | |
| 1854 | // Wrap up any cleanups in the expression. |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1855 | return collection; |
| John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1858 | StmtResult |
| Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1859 | Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, |
| Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1860 | Stmt *First, Expr *collection, |
| 1861 | SourceLocation RParenLoc) { |
| Akira Hatanaka | 3901377 | 2017-04-19 17:54:08 +0000 | [diff] [blame] | 1862 | getCurFunction()->setHasBranchProtectedScope(); |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1863 | |
| 1864 | ExprResult CollectionExprResult = |
| Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1865 | CheckObjCForCollectionOperand(ForLoc, collection); |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1866 | |
| Fariborz Jahanian | 9397767 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 1867 | if (First) { |
| 1868 | QualType FirstType; |
| 1869 | if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { |
| Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1870 | if (!DS->isSingleDecl()) |
| Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1871 | return StmtError(Diag((*DS->decl_begin())->getLocation(), |
| 1872 | diag::err_toomany_element_decls)); |
| 1873 | |
| Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1874 | VarDecl *D = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 1875 | if (!D || D->isInvalidDecl()) |
| 1876 | return StmtError(); |
| 1877 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1878 | FirstType = D->getType(); |
| Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1879 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 1880 | // declare identifiers for objects having storage class 'auto' or |
| 1881 | // 'register'. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1882 | if (!D->hasLocalStorage()) |
| 1883 | return StmtError(Diag(D->getLocation(), |
| Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1884 | diag::err_non_local_variable_decl_in_for)); |
| Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1885 | |
| 1886 | // If the type contained 'auto', deduce the 'auto' to 'id'. |
| 1887 | if (FirstType->getContainedAutoType()) { |
| Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1888 | OpaqueValueExpr OpaqueId(D->getLocation(), Context.getObjCIdType(), |
| 1889 | VK_RValue); |
| 1890 | Expr *DeducedInit = &OpaqueId; |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1891 | if (DeduceAutoType(D->getTypeSourceInfo(), DeducedInit, FirstType) == |
| 1892 | DAR_Failed) |
| Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1893 | DiagnoseAutoDeductionFailure(D, DeducedInit); |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1894 | if (FirstType.isNull()) { |
| Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1895 | D->setInvalidDecl(); |
| 1896 | return StmtError(); |
| 1897 | } |
| 1898 | |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1899 | D->setType(FirstType); |
| Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1900 | |
| Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1901 | if (!inTemplateInstantiation()) { |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1902 | SourceLocation Loc = |
| 1903 | D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1904 | Diag(Loc, diag::warn_auto_var_is_id) |
| 1905 | << D->getDeclName(); |
| 1906 | } |
| 1907 | } |
| 1908 | |
| Anders Carlsson | 1ec2ccd | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 1909 | } else { |
| Douglas Gregor | f68a508 | 2010-04-22 23:10:45 +0000 | [diff] [blame] | 1910 | Expr *FirstE = cast<Expr>(First); |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1911 | if (!FirstE->isTypeDependent() && !FirstE->isLValue()) |
| Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1912 | return StmtError(Diag(First->getLocStart(), |
| 1913 | diag::err_selector_element_not_lvalue) |
| 1914 | << First->getSourceRange()); |
| 1915 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | FirstType = static_cast<Expr*>(First)->getType(); |
| Fariborz Jahanian | 8bcf182 | 2013-10-10 21:58:04 +0000 | [diff] [blame] | 1917 | if (FirstType.isConstQualified()) |
| 1918 | Diag(ForLoc, diag::err_selector_element_const_type) |
| 1919 | << FirstType << First->getSourceRange(); |
| Anders Carlsson | 1ec2ccd | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 1920 | } |
| Douglas Gregor | f68a508 | 2010-04-22 23:10:45 +0000 | [diff] [blame] | 1921 | if (!FirstType->isDependentType() && |
| 1922 | !FirstType->isObjCObjectPointerType() && |
| Fariborz Jahanian | 2e4a46b | 2009-08-14 21:53:27 +0000 | [diff] [blame] | 1923 | !FirstType->isBlockPointerType()) |
| Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1924 | return StmtError(Diag(ForLoc, diag::err_selector_element_type) |
| 1925 | << FirstType << First->getSourceRange()); |
| Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1926 | } |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1927 | |
| Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1928 | if (CollectionExprResult.isInvalid()) |
| 1929 | return StmtError(); |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1930 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1931 | CollectionExprResult = ActOnFinishFullExpr(CollectionExprResult.get()); |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1932 | if (CollectionExprResult.isInvalid()) |
| 1933 | return StmtError(); |
| 1934 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1935 | return new (Context) ObjCForCollectionStmt(First, CollectionExprResult.get(), |
| 1936 | nullptr, ForLoc, RParenLoc); |
| Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1937 | } |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1938 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1939 | /// Finish building a variable declaration for a for-range statement. |
| 1940 | /// \return true if an error occurs. |
| 1941 | static bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init, |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1942 | SourceLocation Loc, int DiagID) { |
| Kaelyn Takata | fb8cf40 | 2015-05-07 00:11:02 +0000 | [diff] [blame] | 1943 | if (Decl->getType()->isUndeducedType()) { |
| 1944 | ExprResult Res = SemaRef.CorrectDelayedTyposInExpr(Init); |
| 1945 | if (!Res.isUsable()) { |
| 1946 | Decl->setInvalidDecl(); |
| 1947 | return true; |
| 1948 | } |
| 1949 | Init = Res.get(); |
| 1950 | } |
| 1951 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1952 | // Deduce the type for the iterator variable now rather than leaving it to |
| 1953 | // AddInitializerToDecl, so we can produce a more suitable diagnostic. |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1954 | QualType InitType; |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 1955 | if ((!isa<InitListExpr>(Init) && Init->getType()->isVoidType()) || |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1956 | SemaRef.DeduceAutoType(Decl->getTypeSourceInfo(), Init, InitType) == |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 1957 | Sema::DAR_Failed) |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1958 | SemaRef.Diag(Loc, DiagID) << Init->getType(); |
| 1959 | if (InitType.isNull()) { |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1960 | Decl->setInvalidDecl(); |
| 1961 | return true; |
| 1962 | } |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1963 | Decl->setType(InitType); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1964 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1965 | // In ARC, infer lifetime. |
| 1966 | // FIXME: ARC may want to turn this into 'const __unsafe_unretained' if |
| 1967 | // we're doing the equivalent of fast iteration. |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1968 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1969 | SemaRef.inferObjCARCLifetime(Decl)) |
| 1970 | Decl->setInvalidDecl(); |
| 1971 | |
| Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1972 | SemaRef.AddInitializerToDecl(Decl, Init, /*DirectInit=*/false); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1973 | SemaRef.FinalizeDeclaration(Decl); |
| Richard Smith | 0c502d2 | 2011-04-18 15:49:25 +0000 | [diff] [blame] | 1974 | SemaRef.CurContext->addHiddenDecl(Decl); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1975 | return false; |
| 1976 | } |
| 1977 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 1978 | namespace { |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1979 | // An enum to represent whether something is dealing with a call to begin() |
| 1980 | // or a call to end() in a range-based for loop. |
| 1981 | enum BeginEndFunction { |
| 1982 | BEF_begin, |
| 1983 | BEF_end |
| 1984 | }; |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 1985 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1986 | /// Produce a note indicating which begin/end function was implicitly called |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 1987 | /// by a C++11 for-range statement. This is often not obvious from the code, |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1988 | /// nor from the diagnostics produced when analysing the implicit expressions |
| 1989 | /// required in a for-range statement. |
| 1990 | void NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E, |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1991 | BeginEndFunction BEF) { |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1992 | CallExpr *CE = dyn_cast<CallExpr>(E); |
| 1993 | if (!CE) |
| 1994 | return; |
| 1995 | FunctionDecl *D = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); |
| 1996 | if (!D) |
| 1997 | return; |
| 1998 | SourceLocation Loc = D->getLocation(); |
| 1999 | |
| 2000 | std::string Description; |
| 2001 | bool IsTemplate = false; |
| 2002 | if (FunctionTemplateDecl *FunTmpl = D->getPrimaryTemplate()) { |
| 2003 | Description = SemaRef.getTemplateArgumentBindingsText( |
| 2004 | FunTmpl->getTemplateParameters(), *D->getTemplateSpecializationArgs()); |
| 2005 | IsTemplate = true; |
| 2006 | } |
| 2007 | |
| 2008 | SemaRef.Diag(Loc, diag::note_for_range_begin_end) |
| 2009 | << BEF << IsTemplate << Description << E->getType(); |
| 2010 | } |
| 2011 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2012 | /// Build a variable declaration for a for-range statement. |
| 2013 | VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, |
| 2014 | QualType Type, const char *Name) { |
| 2015 | DeclContext *DC = SemaRef.CurContext; |
| 2016 | IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); |
| 2017 | TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); |
| 2018 | VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2019 | TInfo, SC_None); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2020 | Decl->setImplicit(); |
| 2021 | return Decl; |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2024 | } |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2025 | |
| Fariborz Jahanian | 0021347 | 2012-07-06 19:04:04 +0000 | [diff] [blame] | 2026 | static bool ObjCEnumerationCollection(Expr *Collection) { |
| 2027 | return !Collection->isTypeDependent() |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2028 | && Collection->getType()->getAs<ObjCObjectPointerType>() != nullptr; |
| Fariborz Jahanian | 0021347 | 2012-07-06 19:04:04 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 2031 | /// ActOnCXXForRangeStmt - Check and build a C++11 for-range statement. |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2032 | /// |
| Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 2033 | /// C++11 [stmt.ranged]: |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2034 | /// A range-based for statement is equivalent to |
| 2035 | /// |
| 2036 | /// { |
| 2037 | /// auto && __range = range-init; |
| 2038 | /// for ( auto __begin = begin-expr, |
| 2039 | /// __end = end-expr; |
| 2040 | /// __begin != __end; |
| 2041 | /// ++__begin ) { |
| 2042 | /// for-range-declaration = *__begin; |
| 2043 | /// statement |
| 2044 | /// } |
| 2045 | /// } |
| 2046 | /// |
| 2047 | /// The body of the loop is not available yet, since it cannot be analysed until |
| 2048 | /// we have determined the type of the for-range-declaration. |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2049 | StmtResult Sema::ActOnCXXForRangeStmt(Scope *S, SourceLocation ForLoc, |
| 2050 | SourceLocation CoawaitLoc, Stmt *First, |
| 2051 | SourceLocation ColonLoc, Expr *Range, |
| 2052 | SourceLocation RParenLoc, |
| 2053 | BuildForRangeKind Kind) { |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2054 | if (!First) |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2055 | return StmtError(); |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 2056 | |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2057 | if (Range && ObjCEnumerationCollection(Range)) |
| Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 2058 | return ActOnObjCForCollectionStmt(ForLoc, First, Range, RParenLoc); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2059 | |
| 2060 | DeclStmt *DS = dyn_cast<DeclStmt>(First); |
| 2061 | assert(DS && "first part of for range not a decl stmt"); |
| 2062 | |
| 2063 | if (!DS->isSingleDecl()) { |
| 2064 | Diag(DS->getStartLoc(), diag::err_type_defined_in_for_range); |
| 2065 | return StmtError(); |
| 2066 | } |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2067 | |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2068 | Decl *LoopVar = DS->getSingleDecl(); |
| 2069 | if (LoopVar->isInvalidDecl() || !Range || |
| 2070 | DiagnoseUnexpandedParameterPack(Range, UPPC_Expression)) { |
| 2071 | LoopVar->setInvalidDecl(); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2072 | return StmtError(); |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2073 | } |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2074 | |
| Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 2075 | // Build the coroutine state immediately and not later during template |
| 2076 | // instantiation |
| 2077 | if (!CoawaitLoc.isInvalid()) { |
| 2078 | if (!ActOnCoroutineBodyStart(S, CoawaitLoc, "co_await")) |
| 2079 | return StmtError(); |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2082 | // Build auto && __range = range-init |
| 2083 | SourceLocation RangeLoc = Range->getLocStart(); |
| 2084 | VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc, |
| 2085 | Context.getAutoRRefDeductType(), |
| 2086 | "__range"); |
| 2087 | if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc, |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2088 | diag::err_for_range_deduction_failure)) { |
| 2089 | LoopVar->setInvalidDecl(); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2090 | return StmtError(); |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2091 | } |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2092 | |
| 2093 | // Claim the type doesn't contain auto: we've already done the checking. |
| 2094 | DeclGroupPtrTy RangeGroup = |
| Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2095 | BuildDeclaratorGroup(MutableArrayRef<Decl *>((Decl **)&RangeVar, 1)); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2096 | StmtResult RangeDecl = ActOnDeclStmt(RangeGroup, RangeLoc, RangeLoc); |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2097 | if (RangeDecl.isInvalid()) { |
| 2098 | LoopVar->setInvalidDecl(); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2099 | return StmtError(); |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2100 | } |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2101 | |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2102 | return BuildCXXForRangeStmt(ForLoc, CoawaitLoc, ColonLoc, RangeDecl.get(), |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 2103 | /*BeginStmt=*/nullptr, /*EndStmt=*/nullptr, |
| 2104 | /*Cond=*/nullptr, /*Inc=*/nullptr, |
| 2105 | DS, RParenLoc, Kind); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /// \brief Create the initialization, compare, and increment steps for |
| 2109 | /// the range-based for loop expression. |
| 2110 | /// This function does not handle array-based for loops, |
| 2111 | /// which are created in Sema::BuildCXXForRangeStmt. |
| 2112 | /// |
| 2113 | /// \returns a ForRangeStatus indicating success or what kind of error occurred. |
| 2114 | /// BeginExpr and EndExpr are set and FRS_Success is returned on success; |
| 2115 | /// CandidateSet and BEF are set and some non-success value is returned on |
| 2116 | /// failure. |
| Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 2117 | static Sema::ForRangeStatus |
| 2118 | BuildNonArrayForRange(Sema &SemaRef, Expr *BeginRange, Expr *EndRange, |
| 2119 | QualType RangeType, VarDecl *BeginVar, VarDecl *EndVar, |
| 2120 | SourceLocation ColonLoc, SourceLocation CoawaitLoc, |
| 2121 | OverloadCandidateSet *CandidateSet, ExprResult *BeginExpr, |
| 2122 | ExprResult *EndExpr, BeginEndFunction *BEF) { |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2123 | DeclarationNameInfo BeginNameInfo( |
| 2124 | &SemaRef.PP.getIdentifierTable().get("begin"), ColonLoc); |
| 2125 | DeclarationNameInfo EndNameInfo(&SemaRef.PP.getIdentifierTable().get("end"), |
| 2126 | ColonLoc); |
| 2127 | |
| 2128 | LookupResult BeginMemberLookup(SemaRef, BeginNameInfo, |
| 2129 | Sema::LookupMemberName); |
| 2130 | LookupResult EndMemberLookup(SemaRef, EndNameInfo, Sema::LookupMemberName); |
| 2131 | |
| 2132 | if (CXXRecordDecl *D = RangeType->getAsCXXRecordDecl()) { |
| 2133 | // - if _RangeT is a class type, the unqualified-ids begin and end are |
| 2134 | // looked up in the scope of class _RangeT as if by class member access |
| 2135 | // lookup (3.4.5), and if either (or both) finds at least one |
| 2136 | // declaration, begin-expr and end-expr are __range.begin() and |
| 2137 | // __range.end(), respectively; |
| 2138 | SemaRef.LookupQualifiedName(BeginMemberLookup, D); |
| 2139 | SemaRef.LookupQualifiedName(EndMemberLookup, D); |
| 2140 | |
| 2141 | if (BeginMemberLookup.empty() != EndMemberLookup.empty()) { |
| 2142 | SourceLocation RangeLoc = BeginVar->getLocation(); |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2143 | *BEF = BeginMemberLookup.empty() ? BEF_end : BEF_begin; |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2144 | |
| 2145 | SemaRef.Diag(RangeLoc, diag::err_for_range_member_begin_end_mismatch) |
| 2146 | << RangeLoc << BeginRange->getType() << *BEF; |
| 2147 | return Sema::FRS_DiagnosticIssued; |
| 2148 | } |
| 2149 | } else { |
| 2150 | // - otherwise, begin-expr and end-expr are begin(__range) and |
| 2151 | // end(__range), respectively, where begin and end are looked up with |
| 2152 | // argument-dependent lookup (3.4.2). For the purposes of this name |
| 2153 | // lookup, namespace std is an associated namespace. |
| 2154 | |
| 2155 | } |
| 2156 | |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2157 | *BEF = BEF_begin; |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2158 | Sema::ForRangeStatus RangeStatus = |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2159 | SemaRef.BuildForRangeBeginEndCall(ColonLoc, ColonLoc, BeginNameInfo, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2160 | BeginMemberLookup, CandidateSet, |
| 2161 | BeginRange, BeginExpr); |
| 2162 | |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2163 | if (RangeStatus != Sema::FRS_Success) { |
| 2164 | if (RangeStatus == Sema::FRS_DiagnosticIssued) |
| 2165 | SemaRef.Diag(BeginRange->getLocStart(), diag::note_in_for_range) |
| 2166 | << ColonLoc << BEF_begin << BeginRange->getType(); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2167 | return RangeStatus; |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2168 | } |
| Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 2169 | if (!CoawaitLoc.isInvalid()) { |
| 2170 | // FIXME: getCurScope() should not be used during template instantiation. |
| 2171 | // We should pick up the set of unqualified lookup results for operator |
| 2172 | // co_await during the initial parse. |
| 2173 | *BeginExpr = SemaRef.ActOnCoawaitExpr(SemaRef.getCurScope(), ColonLoc, |
| 2174 | BeginExpr->get()); |
| 2175 | if (BeginExpr->isInvalid()) |
| 2176 | return Sema::FRS_DiagnosticIssued; |
| 2177 | } |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2178 | if (FinishForRangeVarDecl(SemaRef, BeginVar, BeginExpr->get(), ColonLoc, |
| 2179 | diag::err_for_range_iter_deduction_failure)) { |
| 2180 | NoteForRangeBeginEndFunction(SemaRef, BeginExpr->get(), *BEF); |
| 2181 | return Sema::FRS_DiagnosticIssued; |
| 2182 | } |
| 2183 | |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2184 | *BEF = BEF_end; |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2185 | RangeStatus = |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2186 | SemaRef.BuildForRangeBeginEndCall(ColonLoc, ColonLoc, EndNameInfo, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2187 | EndMemberLookup, CandidateSet, |
| 2188 | EndRange, EndExpr); |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2189 | if (RangeStatus != Sema::FRS_Success) { |
| 2190 | if (RangeStatus == Sema::FRS_DiagnosticIssued) |
| 2191 | SemaRef.Diag(EndRange->getLocStart(), diag::note_in_for_range) |
| 2192 | << ColonLoc << BEF_end << EndRange->getType(); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2193 | return RangeStatus; |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2194 | } |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2195 | if (FinishForRangeVarDecl(SemaRef, EndVar, EndExpr->get(), ColonLoc, |
| 2196 | diag::err_for_range_iter_deduction_failure)) { |
| 2197 | NoteForRangeBeginEndFunction(SemaRef, EndExpr->get(), *BEF); |
| 2198 | return Sema::FRS_DiagnosticIssued; |
| 2199 | } |
| 2200 | return Sema::FRS_Success; |
| 2201 | } |
| 2202 | |
| 2203 | /// Speculatively attempt to dereference an invalid range expression. |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2204 | /// If the attempt fails, this function will return a valid, null StmtResult |
| 2205 | /// and emit no diagnostics. |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2206 | static StmtResult RebuildForRangeWithDereference(Sema &SemaRef, Scope *S, |
| 2207 | SourceLocation ForLoc, |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2208 | SourceLocation CoawaitLoc, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2209 | Stmt *LoopVarDecl, |
| 2210 | SourceLocation ColonLoc, |
| 2211 | Expr *Range, |
| 2212 | SourceLocation RangeLoc, |
| 2213 | SourceLocation RParenLoc) { |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2214 | // Determine whether we can rebuild the for-range statement with a |
| 2215 | // dereferenced range expression. |
| 2216 | ExprResult AdjustedRange; |
| 2217 | { |
| 2218 | Sema::SFINAETrap Trap(SemaRef); |
| 2219 | |
| 2220 | AdjustedRange = SemaRef.BuildUnaryOp(S, RangeLoc, UO_Deref, Range); |
| 2221 | if (AdjustedRange.isInvalid()) |
| 2222 | return StmtResult(); |
| 2223 | |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2224 | StmtResult SR = SemaRef.ActOnCXXForRangeStmt( |
| 2225 | S, ForLoc, CoawaitLoc, LoopVarDecl, ColonLoc, AdjustedRange.get(), |
| 2226 | RParenLoc, Sema::BFRK_Check); |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2227 | if (SR.isInvalid()) |
| 2228 | return StmtResult(); |
| 2229 | } |
| 2230 | |
| 2231 | // The attempt to dereference worked well enough that it could produce a valid |
| 2232 | // loop. Produce a fixit, and rebuild the loop with diagnostics enabled, in |
| 2233 | // case there are any other (non-fatal) problems with it. |
| 2234 | SemaRef.Diag(RangeLoc, diag::err_for_range_dereference) |
| 2235 | << Range->getType() << FixItHint::CreateInsertion(RangeLoc, "*"); |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2236 | return SemaRef.ActOnCXXForRangeStmt(S, ForLoc, CoawaitLoc, LoopVarDecl, |
| 2237 | ColonLoc, AdjustedRange.get(), RParenLoc, |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2238 | Sema::BFRK_Rebuild); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2241 | namespace { |
| 2242 | /// RAII object to automatically invalidate a declaration if an error occurs. |
| 2243 | struct InvalidateOnErrorScope { |
| 2244 | InvalidateOnErrorScope(Sema &SemaRef, Decl *D, bool Enabled) |
| 2245 | : Trap(SemaRef.Diags), D(D), Enabled(Enabled) {} |
| 2246 | ~InvalidateOnErrorScope() { |
| 2247 | if (Enabled && Trap.hasErrorOccurred()) |
| 2248 | D->setInvalidDecl(); |
| 2249 | } |
| 2250 | |
| 2251 | DiagnosticErrorTrap Trap; |
| 2252 | Decl *D; |
| 2253 | bool Enabled; |
| 2254 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2255 | } |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2256 | |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2257 | /// BuildCXXForRangeStmt - Build or instantiate a C++11 for-range statement. |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2258 | StmtResult |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2259 | Sema::BuildCXXForRangeStmt(SourceLocation ForLoc, SourceLocation CoawaitLoc, |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 2260 | SourceLocation ColonLoc, Stmt *RangeDecl, |
| 2261 | Stmt *Begin, Stmt *End, Expr *Cond, |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2262 | Expr *Inc, Stmt *LoopVarDecl, |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2263 | SourceLocation RParenLoc, BuildForRangeKind Kind) { |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2264 | // FIXME: This should not be used during template instantiation. We should |
| 2265 | // pick up the set of unqualified lookup results for the != and + operators |
| 2266 | // in the initial parse. |
| 2267 | // |
| 2268 | // Testcase (accepts-invalid): |
| 2269 | // template<typename T> void f() { for (auto x : T()) {} } |
| 2270 | // namespace N { struct X { X begin(); X end(); int operator*(); }; } |
| 2271 | // bool operator!=(N::X, N::X); void operator++(N::X); |
| 2272 | // void g() { f<N::X>(); } |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2273 | Scope *S = getCurScope(); |
| 2274 | |
| 2275 | DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl); |
| 2276 | VarDecl *RangeVar = cast<VarDecl>(RangeDS->getSingleDecl()); |
| 2277 | QualType RangeVarType = RangeVar->getType(); |
| 2278 | |
| 2279 | DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl); |
| 2280 | VarDecl *LoopVar = cast<VarDecl>(LoopVarDS->getSingleDecl()); |
| 2281 | |
| Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2282 | // If we hit any errors, mark the loop variable as invalid if its type |
| 2283 | // contains 'auto'. |
| 2284 | InvalidateOnErrorScope Invalidate(*this, LoopVar, |
| 2285 | LoopVar->getType()->isUndeducedType()); |
| 2286 | |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 2287 | StmtResult BeginDeclStmt = Begin; |
| 2288 | StmtResult EndDeclStmt = End; |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2289 | ExprResult NotEqExpr = Cond, IncrExpr = Inc; |
| 2290 | |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2291 | if (RangeVarType->isDependentType()) { |
| 2292 | // The range is implicitly used as a placeholder when it is dependent. |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 2293 | RangeVar->markUsed(Context); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2294 | |
| 2295 | // Deduce any 'auto's in the loop variable as 'DependentTy'. We'll fill |
| 2296 | // them in properly when we instantiate the loop. |
| Erik Pilkington | 21ff345 | 2017-06-12 16:11:06 +0000 | [diff] [blame] | 2297 | if (!LoopVar->isInvalidDecl() && Kind != BFRK_Check) { |
| 2298 | if (auto *DD = dyn_cast<DecompositionDecl>(LoopVar)) |
| 2299 | for (auto *Binding : DD->bindings()) |
| 2300 | Binding->setType(Context.DependentTy); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2301 | LoopVar->setType(SubstAutoType(LoopVar->getType(), Context.DependentTy)); |
| Erik Pilkington | 21ff345 | 2017-06-12 16:11:06 +0000 | [diff] [blame] | 2302 | } |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 2303 | } else if (!BeginDeclStmt.get()) { |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2304 | SourceLocation RangeLoc = RangeVar->getLocation(); |
| 2305 | |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2306 | const QualType RangeVarNonRefType = RangeVarType.getNonReferenceType(); |
| 2307 | |
| 2308 | ExprResult BeginRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, |
| 2309 | VK_LValue, ColonLoc); |
| 2310 | if (BeginRangeRef.isInvalid()) |
| 2311 | return StmtError(); |
| 2312 | |
| 2313 | ExprResult EndRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, |
| 2314 | VK_LValue, ColonLoc); |
| 2315 | if (EndRangeRef.isInvalid()) |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2316 | return StmtError(); |
| 2317 | |
| 2318 | QualType AutoType = Context.getAutoDeductType(); |
| 2319 | Expr *Range = RangeVar->getInit(); |
| 2320 | if (!Range) |
| 2321 | return StmtError(); |
| 2322 | QualType RangeType = Range->getType(); |
| 2323 | |
| 2324 | if (RequireCompleteType(RangeLoc, RangeType, |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2325 | diag::err_for_range_incomplete_type)) |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2326 | return StmtError(); |
| 2327 | |
| 2328 | // Build auto __begin = begin-expr, __end = end-expr. |
| 2329 | VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, |
| 2330 | "__begin"); |
| 2331 | VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, |
| 2332 | "__end"); |
| 2333 | |
| 2334 | // Build begin-expr and end-expr and attach to __begin and __end variables. |
| 2335 | ExprResult BeginExpr, EndExpr; |
| 2336 | if (const ArrayType *UnqAT = RangeType->getAsArrayTypeUnsafe()) { |
| 2337 | // - if _RangeT is an array type, begin-expr and end-expr are __range and |
| 2338 | // __range + __bound, respectively, where __bound is the array bound. If |
| 2339 | // _RangeT is an array of unknown size or an array of incomplete type, |
| 2340 | // the program is ill-formed; |
| 2341 | |
| 2342 | // begin-expr is __range. |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2343 | BeginExpr = BeginRangeRef; |
| Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 2344 | if (!CoawaitLoc.isInvalid()) { |
| 2345 | BeginExpr = ActOnCoawaitExpr(S, ColonLoc, BeginExpr.get()); |
| 2346 | if (BeginExpr.isInvalid()) |
| 2347 | return StmtError(); |
| 2348 | } |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2349 | if (FinishForRangeVarDecl(*this, BeginVar, BeginRangeRef.get(), ColonLoc, |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2350 | diag::err_for_range_iter_deduction_failure)) { |
| 2351 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2352 | return StmtError(); |
| 2353 | } |
| 2354 | |
| 2355 | // Find the array bound. |
| 2356 | ExprResult BoundExpr; |
| 2357 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(UnqAT)) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2358 | BoundExpr = IntegerLiteral::Create( |
| 2359 | Context, CAT->getSize(), Context.getPointerDiffType(), RangeLoc); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2360 | else if (const VariableArrayType *VAT = |
| Faisal Vali | 1ca2d96 | 2017-05-15 01:49:19 +0000 | [diff] [blame] | 2361 | dyn_cast<VariableArrayType>(UnqAT)) { |
| 2362 | // For a variably modified type we can't just use the expression within |
| 2363 | // the array bounds, since we don't want that to be re-evaluated here. |
| 2364 | // Rather, we need to determine what it was when the array was first |
| 2365 | // created - so we resort to using sizeof(vla)/sizeof(element). |
| 2366 | // For e.g. |
| 2367 | // void f(int b) { |
| 2368 | // int vla[b]; |
| 2369 | // b = -1; <-- This should not affect the num of iterations below |
| 2370 | // for (int &c : vla) { .. } |
| 2371 | // } |
| 2372 | |
| 2373 | // FIXME: This results in codegen generating IR that recalculates the |
| 2374 | // run-time number of elements (as opposed to just using the IR Value |
| 2375 | // that corresponds to the run-time value of each bound that was |
| 2376 | // generated when the array was created.) If this proves too embarassing |
| 2377 | // even for unoptimized IR, consider passing a magic-value/cookie to |
| 2378 | // codegen that then knows to simply use that initial llvm::Value (that |
| 2379 | // corresponds to the bound at time of array creation) within |
| 2380 | // getelementptr. But be prepared to pay the price of increasing a |
| 2381 | // customized form of coupling between the two components - which could |
| 2382 | // be hard to maintain as the codebase evolves. |
| 2383 | |
| 2384 | ExprResult SizeOfVLAExprR = ActOnUnaryExprOrTypeTraitExpr( |
| 2385 | EndVar->getLocation(), UETT_SizeOf, |
| 2386 | /*isType=*/true, |
| 2387 | CreateParsedType(VAT->desugar(), Context.getTrivialTypeSourceInfo( |
| 2388 | VAT->desugar(), RangeLoc)) |
| 2389 | .getAsOpaquePtr(), |
| 2390 | EndVar->getSourceRange()); |
| 2391 | if (SizeOfVLAExprR.isInvalid()) |
| 2392 | return StmtError(); |
| 2393 | |
| 2394 | ExprResult SizeOfEachElementExprR = ActOnUnaryExprOrTypeTraitExpr( |
| 2395 | EndVar->getLocation(), UETT_SizeOf, |
| 2396 | /*isType=*/true, |
| 2397 | CreateParsedType(VAT->desugar(), |
| 2398 | Context.getTrivialTypeSourceInfo( |
| 2399 | VAT->getElementType(), RangeLoc)) |
| 2400 | .getAsOpaquePtr(), |
| 2401 | EndVar->getSourceRange()); |
| 2402 | if (SizeOfEachElementExprR.isInvalid()) |
| 2403 | return StmtError(); |
| 2404 | |
| 2405 | BoundExpr = |
| 2406 | ActOnBinOp(S, EndVar->getLocation(), tok::slash, |
| 2407 | SizeOfVLAExprR.get(), SizeOfEachElementExprR.get()); |
| 2408 | if (BoundExpr.isInvalid()) |
| 2409 | return StmtError(); |
| 2410 | |
| 2411 | } else { |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2412 | // Can't be a DependentSizedArrayType or an IncompleteArrayType since |
| 2413 | // UnqAT is not incomplete and Range is not type-dependent. |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2414 | llvm_unreachable("Unexpected array type in for-range"); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | // end-expr is __range + __bound. |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2418 | EndExpr = ActOnBinOp(S, ColonLoc, tok::plus, EndRangeRef.get(), |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2419 | BoundExpr.get()); |
| 2420 | if (EndExpr.isInvalid()) |
| 2421 | return StmtError(); |
| 2422 | if (FinishForRangeVarDecl(*this, EndVar, EndExpr.get(), ColonLoc, |
| 2423 | diag::err_for_range_iter_deduction_failure)) { |
| 2424 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 2425 | return StmtError(); |
| 2426 | } |
| 2427 | } else { |
| Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 2428 | OverloadCandidateSet CandidateSet(RangeLoc, |
| 2429 | OverloadCandidateSet::CSK_Normal); |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2430 | BeginEndFunction BEFFailure; |
| Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 2431 | ForRangeStatus RangeStatus = BuildNonArrayForRange( |
| 2432 | *this, BeginRangeRef.get(), EndRangeRef.get(), RangeType, BeginVar, |
| 2433 | EndVar, ColonLoc, CoawaitLoc, &CandidateSet, &BeginExpr, &EndExpr, |
| 2434 | &BEFFailure); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2435 | |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2436 | if (Kind == BFRK_Build && RangeStatus == FRS_NoViableFunction && |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2437 | BEFFailure == BEF_begin) { |
| Richard Trieu | 0825469 | 2013-10-11 22:16:04 +0000 | [diff] [blame] | 2438 | // If the range is being built from an array parameter, emit a |
| 2439 | // a diagnostic that it is being treated as a pointer. |
| 2440 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Range)) { |
| 2441 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 2442 | QualType ArrayTy = PVD->getOriginalType(); |
| 2443 | QualType PointerTy = PVD->getType(); |
| 2444 | if (PointerTy->isPointerType() && ArrayTy->isArrayType()) { |
| 2445 | Diag(Range->getLocStart(), diag::err_range_on_array_parameter) |
| 2446 | << RangeLoc << PVD << ArrayTy << PointerTy; |
| 2447 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2448 | return StmtError(); |
| 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | // If building the range failed, try dereferencing the range expression |
| 2454 | // unless a diagnostic was issued or the end function is problematic. |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2455 | StmtResult SR = RebuildForRangeWithDereference(*this, S, ForLoc, |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2456 | CoawaitLoc, |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2457 | LoopVarDecl, ColonLoc, |
| 2458 | Range, RangeLoc, |
| 2459 | RParenLoc); |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2460 | if (SR.isInvalid() || SR.isUsable()) |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2461 | return SR; |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2464 | // Otherwise, emit diagnostics if we haven't already. |
| 2465 | if (RangeStatus == FRS_NoViableFunction) { |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2466 | Expr *Range = BEFFailure ? EndRangeRef.get() : BeginRangeRef.get(); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2467 | Diag(Range->getLocStart(), diag::err_for_range_invalid) |
| 2468 | << RangeLoc << Range->getType() << BEFFailure; |
| Nico Weber | a2a0eb9 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 2469 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Range); |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2470 | } |
| 2471 | // Return an error if no fix was discovered. |
| 2472 | if (RangeStatus != FRS_Success) |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2473 | return StmtError(); |
| 2474 | } |
| 2475 | |
| Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2476 | assert(!BeginExpr.isInvalid() && !EndExpr.isInvalid() && |
| 2477 | "invalid range expression in for loop"); |
| 2478 | |
| 2479 | // C++11 [dcl.spec.auto]p7: BeginType and EndType must be the same. |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 2480 | // C++1z removes this restriction. |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2481 | QualType BeginType = BeginVar->getType(), EndType = EndVar->getType(); |
| 2482 | if (!Context.hasSameType(BeginType, EndType)) { |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 2483 | Diag(RangeLoc, getLangOpts().CPlusPlus1z |
| 2484 | ? diag::warn_for_range_begin_end_types_differ |
| 2485 | : diag::ext_for_range_begin_end_types_differ) |
| 2486 | << BeginType << EndType; |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2487 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2488 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 2489 | } |
| 2490 | |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 2491 | BeginDeclStmt = |
| 2492 | ActOnDeclStmt(ConvertDeclToDeclGroup(BeginVar), ColonLoc, ColonLoc); |
| 2493 | EndDeclStmt = |
| 2494 | ActOnDeclStmt(ConvertDeclToDeclGroup(EndVar), ColonLoc, ColonLoc); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2495 | |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2496 | const QualType BeginRefNonRefType = BeginType.getNonReferenceType(); |
| 2497 | ExprResult BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2498 | VK_LValue, ColonLoc); |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2499 | if (BeginRef.isInvalid()) |
| 2500 | return StmtError(); |
| 2501 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2502 | ExprResult EndRef = BuildDeclRefExpr(EndVar, EndType.getNonReferenceType(), |
| 2503 | VK_LValue, ColonLoc); |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2504 | if (EndRef.isInvalid()) |
| 2505 | return StmtError(); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2506 | |
| 2507 | // Build and check __begin != __end expression. |
| 2508 | NotEqExpr = ActOnBinOp(S, ColonLoc, tok::exclaimequal, |
| 2509 | BeginRef.get(), EndRef.get()); |
| Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 2510 | if (!NotEqExpr.isInvalid()) |
| 2511 | NotEqExpr = CheckBooleanCondition(ColonLoc, NotEqExpr.get()); |
| 2512 | if (!NotEqExpr.isInvalid()) |
| 2513 | NotEqExpr = ActOnFinishFullExpr(NotEqExpr.get()); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2514 | if (NotEqExpr.isInvalid()) { |
| Sam Panzer | 22a3fe1 | 2012-09-06 21:50:08 +0000 | [diff] [blame] | 2515 | Diag(RangeLoc, diag::note_for_range_invalid_iterator) |
| 2516 | << RangeLoc << 0 << BeginRangeRef.get()->getType(); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2517 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2518 | if (!Context.hasSameType(BeginType, EndType)) |
| 2519 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 2520 | return StmtError(); |
| 2521 | } |
| 2522 | |
| 2523 | // Build and check ++__begin expression. |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2524 | BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
| 2525 | VK_LValue, ColonLoc); |
| 2526 | if (BeginRef.isInvalid()) |
| 2527 | return StmtError(); |
| 2528 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2529 | IncrExpr = ActOnUnaryOp(S, ColonLoc, tok::plusplus, BeginRef.get()); |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2530 | if (!IncrExpr.isInvalid() && CoawaitLoc.isValid()) |
| Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 2531 | // FIXME: getCurScope() should not be used during template instantiation. |
| 2532 | // We should pick up the set of unqualified lookup results for operator |
| 2533 | // co_await during the initial parse. |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2534 | IncrExpr = ActOnCoawaitExpr(S, CoawaitLoc, IncrExpr.get()); |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2535 | if (!IncrExpr.isInvalid()) |
| 2536 | IncrExpr = ActOnFinishFullExpr(IncrExpr.get()); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2537 | if (IncrExpr.isInvalid()) { |
| Sam Panzer | 22a3fe1 | 2012-09-06 21:50:08 +0000 | [diff] [blame] | 2538 | Diag(RangeLoc, diag::note_for_range_invalid_iterator) |
| 2539 | << RangeLoc << 2 << BeginRangeRef.get()->getType() ; |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2540 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2541 | return StmtError(); |
| 2542 | } |
| 2543 | |
| 2544 | // Build and check *__begin expression. |
| Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2545 | BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
| 2546 | VK_LValue, ColonLoc); |
| 2547 | if (BeginRef.isInvalid()) |
| 2548 | return StmtError(); |
| 2549 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2550 | ExprResult DerefExpr = ActOnUnaryOp(S, ColonLoc, tok::star, BeginRef.get()); |
| 2551 | if (DerefExpr.isInvalid()) { |
| Sam Panzer | 22a3fe1 | 2012-09-06 21:50:08 +0000 | [diff] [blame] | 2552 | Diag(RangeLoc, diag::note_for_range_invalid_iterator) |
| 2553 | << RangeLoc << 1 << BeginRangeRef.get()->getType(); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2554 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2555 | return StmtError(); |
| 2556 | } |
| 2557 | |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2558 | // Attach *__begin as initializer for VD. Don't touch it if we're just |
| 2559 | // trying to determine whether this would be a valid range. |
| 2560 | if (!LoopVar->isInvalidDecl() && Kind != BFRK_Check) { |
| Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2561 | AddInitializerToDecl(LoopVar, DerefExpr.get(), /*DirectInit=*/false); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2562 | if (LoopVar->isInvalidDecl()) |
| 2563 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2564 | } |
| 2565 | } |
| 2566 | |
| Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2567 | // Don't bother to actually allocate the result if we're just trying to |
| 2568 | // determine whether it would be valid. |
| 2569 | if (Kind == BFRK_Check) |
| 2570 | return StmtResult(); |
| 2571 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2572 | return new (Context) CXXForRangeStmt( |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 2573 | RangeDS, cast_or_null<DeclStmt>(BeginDeclStmt.get()), |
| 2574 | cast_or_null<DeclStmt>(EndDeclStmt.get()), NotEqExpr.get(), |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2575 | IncrExpr.get(), LoopVarDS, /*Body=*/nullptr, ForLoc, CoawaitLoc, |
| 2576 | ColonLoc, RParenLoc); |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 2579 | /// FinishObjCForCollectionStmt - Attach the body to a objective-C foreach |
| Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 2580 | /// statement. |
| 2581 | StmtResult Sema::FinishObjCForCollectionStmt(Stmt *S, Stmt *B) { |
| 2582 | if (!S || !B) |
| 2583 | return StmtError(); |
| 2584 | ObjCForCollectionStmt * ForStmt = cast<ObjCForCollectionStmt>(S); |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 2585 | |
| Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 2586 | ForStmt->setBody(B); |
| 2587 | return S; |
| 2588 | } |
| 2589 | |
| Richard Trieu | 3e1d483 | 2015-04-13 22:08:55 +0000 | [diff] [blame] | 2590 | // Warn when the loop variable is a const reference that creates a copy. |
| 2591 | // Suggest using the non-reference type for copies. If a copy can be prevented |
| 2592 | // suggest the const reference type that would do so. |
| 2593 | // For instance, given "for (const &Foo : Range)", suggest |
| 2594 | // "for (const Foo : Range)" to denote a copy is made for the loop. If |
| 2595 | // possible, also suggest "for (const &Bar : Range)" if this type prevents |
| 2596 | // the copy altogether. |
| 2597 | static void DiagnoseForRangeReferenceVariableCopies(Sema &SemaRef, |
| 2598 | const VarDecl *VD, |
| 2599 | QualType RangeInitType) { |
| 2600 | const Expr *InitExpr = VD->getInit(); |
| 2601 | if (!InitExpr) |
| 2602 | return; |
| 2603 | |
| 2604 | QualType VariableType = VD->getType(); |
| 2605 | |
| Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 2606 | if (auto Cleanups = dyn_cast<ExprWithCleanups>(InitExpr)) |
| 2607 | if (!Cleanups->cleanupsHaveSideEffects()) |
| 2608 | InitExpr = Cleanups->getSubExpr(); |
| 2609 | |
| Richard Trieu | 3e1d483 | 2015-04-13 22:08:55 +0000 | [diff] [blame] | 2610 | const MaterializeTemporaryExpr *MTE = |
| 2611 | dyn_cast<MaterializeTemporaryExpr>(InitExpr); |
| 2612 | |
| 2613 | // No copy made. |
| 2614 | if (!MTE) |
| 2615 | return; |
| 2616 | |
| 2617 | const Expr *E = MTE->GetTemporaryExpr()->IgnoreImpCasts(); |
| 2618 | |
| 2619 | // Searching for either UnaryOperator for dereference of a pointer or |
| 2620 | // CXXOperatorCallExpr for handling iterators. |
| 2621 | while (!isa<CXXOperatorCallExpr>(E) && !isa<UnaryOperator>(E)) { |
| 2622 | if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(E)) { |
| 2623 | E = CCE->getArg(0); |
| 2624 | } else if (const CXXMemberCallExpr *Call = dyn_cast<CXXMemberCallExpr>(E)) { |
| 2625 | const MemberExpr *ME = cast<MemberExpr>(Call->getCallee()); |
| 2626 | E = ME->getBase(); |
| 2627 | } else { |
| 2628 | const MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(E); |
| 2629 | E = MTE->GetTemporaryExpr(); |
| 2630 | } |
| 2631 | E = E->IgnoreImpCasts(); |
| 2632 | } |
| 2633 | |
| 2634 | bool ReturnsReference = false; |
| 2635 | if (isa<UnaryOperator>(E)) { |
| 2636 | ReturnsReference = true; |
| 2637 | } else { |
| 2638 | const CXXOperatorCallExpr *Call = cast<CXXOperatorCallExpr>(E); |
| 2639 | const FunctionDecl *FD = Call->getDirectCallee(); |
| 2640 | QualType ReturnType = FD->getReturnType(); |
| 2641 | ReturnsReference = ReturnType->isReferenceType(); |
| 2642 | } |
| 2643 | |
| 2644 | if (ReturnsReference) { |
| 2645 | // Loop variable creates a temporary. Suggest either to go with |
| 2646 | // non-reference loop variable to indiciate a copy is made, or |
| 2647 | // the correct time to bind a const reference. |
| 2648 | SemaRef.Diag(VD->getLocation(), diag::warn_for_range_const_reference_copy) |
| 2649 | << VD << VariableType << E->getType(); |
| 2650 | QualType NonReferenceType = VariableType.getNonReferenceType(); |
| 2651 | NonReferenceType.removeLocalConst(); |
| 2652 | QualType NewReferenceType = |
| 2653 | SemaRef.Context.getLValueReferenceType(E->getType().withConst()); |
| 2654 | SemaRef.Diag(VD->getLocStart(), diag::note_use_type_or_non_reference) |
| 2655 | << NonReferenceType << NewReferenceType << VD->getSourceRange(); |
| 2656 | } else { |
| 2657 | // The range always returns a copy, so a temporary is always created. |
| 2658 | // Suggest removing the reference from the loop variable. |
| 2659 | SemaRef.Diag(VD->getLocation(), diag::warn_for_range_variable_always_copy) |
| 2660 | << VD << RangeInitType; |
| 2661 | QualType NonReferenceType = VariableType.getNonReferenceType(); |
| 2662 | NonReferenceType.removeLocalConst(); |
| 2663 | SemaRef.Diag(VD->getLocStart(), diag::note_use_non_reference_type) |
| 2664 | << NonReferenceType << VD->getSourceRange(); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | // Warns when the loop variable can be changed to a reference type to |
| 2669 | // prevent a copy. For instance, if given "for (const Foo x : Range)" suggest |
| 2670 | // "for (const Foo &x : Range)" if this form does not make a copy. |
| 2671 | static void DiagnoseForRangeConstVariableCopies(Sema &SemaRef, |
| 2672 | const VarDecl *VD) { |
| 2673 | const Expr *InitExpr = VD->getInit(); |
| 2674 | if (!InitExpr) |
| 2675 | return; |
| 2676 | |
| 2677 | QualType VariableType = VD->getType(); |
| 2678 | |
| 2679 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(InitExpr)) { |
| 2680 | if (!CE->getConstructor()->isCopyConstructor()) |
| 2681 | return; |
| 2682 | } else if (const CastExpr *CE = dyn_cast<CastExpr>(InitExpr)) { |
| 2683 | if (CE->getCastKind() != CK_LValueToRValue) |
| 2684 | return; |
| 2685 | } else { |
| 2686 | return; |
| 2687 | } |
| 2688 | |
| 2689 | // TODO: Determine a maximum size that a POD type can be before a diagnostic |
| 2690 | // should be emitted. Also, only ignore POD types with trivial copy |
| 2691 | // constructors. |
| 2692 | if (VariableType.isPODType(SemaRef.Context)) |
| 2693 | return; |
| 2694 | |
| 2695 | // Suggest changing from a const variable to a const reference variable |
| 2696 | // if doing so will prevent a copy. |
| 2697 | SemaRef.Diag(VD->getLocation(), diag::warn_for_range_copy) |
| 2698 | << VD << VariableType << InitExpr->getType(); |
| 2699 | SemaRef.Diag(VD->getLocStart(), diag::note_use_reference_type) |
| 2700 | << SemaRef.Context.getLValueReferenceType(VariableType) |
| 2701 | << VD->getSourceRange(); |
| 2702 | } |
| 2703 | |
| 2704 | /// DiagnoseForRangeVariableCopies - Diagnose three cases and fixes for them. |
| 2705 | /// 1) for (const foo &x : foos) where foos only returns a copy. Suggest |
| 2706 | /// using "const foo x" to show that a copy is made |
| 2707 | /// 2) for (const bar &x : foos) where bar is a temporary intialized by bar. |
| 2708 | /// Suggest either "const bar x" to keep the copying or "const foo& x" to |
| 2709 | /// prevent the copy. |
| 2710 | /// 3) for (const foo x : foos) where x is constructed from a reference foo. |
| 2711 | /// Suggest "const foo &x" to prevent the copy. |
| 2712 | static void DiagnoseForRangeVariableCopies(Sema &SemaRef, |
| 2713 | const CXXForRangeStmt *ForStmt) { |
| 2714 | if (SemaRef.Diags.isIgnored(diag::warn_for_range_const_reference_copy, |
| 2715 | ForStmt->getLocStart()) && |
| 2716 | SemaRef.Diags.isIgnored(diag::warn_for_range_variable_always_copy, |
| 2717 | ForStmt->getLocStart()) && |
| 2718 | SemaRef.Diags.isIgnored(diag::warn_for_range_copy, |
| 2719 | ForStmt->getLocStart())) { |
| 2720 | return; |
| 2721 | } |
| 2722 | |
| 2723 | const VarDecl *VD = ForStmt->getLoopVariable(); |
| 2724 | if (!VD) |
| 2725 | return; |
| 2726 | |
| 2727 | QualType VariableType = VD->getType(); |
| 2728 | |
| 2729 | if (VariableType->isIncompleteType()) |
| 2730 | return; |
| 2731 | |
| 2732 | const Expr *InitExpr = VD->getInit(); |
| 2733 | if (!InitExpr) |
| 2734 | return; |
| 2735 | |
| 2736 | if (VariableType->isReferenceType()) { |
| 2737 | DiagnoseForRangeReferenceVariableCopies(SemaRef, VD, |
| 2738 | ForStmt->getRangeInit()->getType()); |
| 2739 | } else if (VariableType.isConstQualified()) { |
| 2740 | DiagnoseForRangeConstVariableCopies(SemaRef, VD); |
| 2741 | } |
| 2742 | } |
| 2743 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2744 | /// FinishCXXForRangeStmt - Attach the body to a C++0x for-range statement. |
| 2745 | /// This is a separate step from ActOnCXXForRangeStmt because analysis of the |
| 2746 | /// body cannot be performed until after the type of the range variable is |
| 2747 | /// determined. |
| 2748 | StmtResult Sema::FinishCXXForRangeStmt(Stmt *S, Stmt *B) { |
| 2749 | if (!S || !B) |
| 2750 | return StmtError(); |
| 2751 | |
| Fariborz Jahanian | 0021347 | 2012-07-06 19:04:04 +0000 | [diff] [blame] | 2752 | if (isa<ObjCForCollectionStmt>(S)) |
| 2753 | return FinishObjCForCollectionStmt(S, B); |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 2754 | |
| Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2755 | CXXForRangeStmt *ForStmt = cast<CXXForRangeStmt>(S); |
| 2756 | ForStmt->setBody(B); |
| 2757 | |
| 2758 | DiagnoseEmptyStmtBody(ForStmt->getRParenLoc(), B, |
| 2759 | diag::warn_empty_range_based_for_body); |
| 2760 | |
| Richard Trieu | 3e1d483 | 2015-04-13 22:08:55 +0000 | [diff] [blame] | 2761 | DiagnoseForRangeVariableCopies(*this, ForStmt); |
| 2762 | |
| Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2763 | return S; |
| 2764 | } |
| 2765 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2766 | StmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc, |
| 2767 | SourceLocation LabelLoc, |
| 2768 | LabelDecl *TheDecl) { |
| 2769 | getCurFunction()->setHasBranchIntoScope(); |
| Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 2770 | TheDecl->markUsed(Context); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2771 | return new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 2772 | } |
| Chris Lattner | 1c31050 | 2007-05-31 06:00:00 +0000 | [diff] [blame] | 2773 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2774 | StmtResult |
| Chris Lattner | 34d9a51 | 2009-04-19 01:04:21 +0000 | [diff] [blame] | 2775 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2776 | Expr *E) { |
| Eli Friedman | 8d7ff40 | 2009-03-26 00:18:06 +0000 | [diff] [blame] | 2777 | // Convert operand to void* |
| Douglas Gregor | 30776d4 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 2778 | if (!E->isTypeDependent()) { |
| 2779 | QualType ETy = E->getType(); |
| Chandler Carruth | 0021698 | 2010-01-31 10:26:25 +0000 | [diff] [blame] | 2780 | QualType DestTy = Context.getPointerType(Context.VoidTy.withConst()); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2781 | ExprResult ExprRes = E; |
| Douglas Gregor | 30776d4 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 2782 | AssignConvertType ConvTy = |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2783 | CheckSingleAssignmentConstraints(DestTy, ExprRes); |
| 2784 | if (ExprRes.isInvalid()) |
| 2785 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2786 | E = ExprRes.get(); |
| Chandler Carruth | 0021698 | 2010-01-31 10:26:25 +0000 | [diff] [blame] | 2787 | if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing)) |
| Douglas Gregor | 30776d4 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 2788 | return StmtError(); |
| 2789 | } |
| John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 2790 | |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2791 | ExprResult ExprRes = ActOnFinishFullExpr(E); |
| 2792 | if (ExprRes.isInvalid()) |
| 2793 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2794 | E = ExprRes.get(); |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2795 | |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 2796 | getCurFunction()->setHasIndirectGoto(); |
| John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 2797 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2798 | return new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
| Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 2801 | static void CheckJumpOutOfSEHFinally(Sema &S, SourceLocation Loc, |
| 2802 | const Scope &DestScope) { |
| 2803 | if (!S.CurrentSEHFinally.empty() && |
| 2804 | DestScope.Contains(*S.CurrentSEHFinally.back())) { |
| 2805 | S.Diag(Loc, diag::warn_jump_out_of_seh_finally); |
| 2806 | } |
| 2807 | } |
| 2808 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2809 | StmtResult |
| Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2810 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
| Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 2811 | Scope *S = CurScope->getContinueParent(); |
| 2812 | if (!S) { |
| 2813 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2814 | return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); |
| Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 2815 | } |
| Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 2816 | CheckJumpOutOfSEHFinally(*this, ContinueLoc, *S); |
| Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2817 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2818 | return new (Context) ContinueStmt(ContinueLoc); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2821 | StmtResult |
| Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2822 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
| Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 2823 | Scope *S = CurScope->getBreakParent(); |
| 2824 | if (!S) { |
| 2825 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2826 | return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); |
| Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 2827 | } |
| Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2828 | if (S->isOpenMPLoopScope()) |
| 2829 | return StmtError(Diag(BreakLoc, diag::err_omp_loop_cannot_use_stmt) |
| 2830 | << "break"); |
| Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 2831 | CheckJumpOutOfSEHFinally(*this, BreakLoc, *S); |
| Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2832 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2833 | return new (Context) BreakStmt(BreakLoc); |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2836 | /// \brief Determine whether the given expression is a candidate for |
| Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2837 | /// copy elision in either a return statement or a throw expression. |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2838 | /// |
| Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2839 | /// \param ReturnType If we're determining the copy elision candidate for |
| 2840 | /// a return statement, this is the return type of the function. If we're |
| 2841 | /// determining the copy elision candidate for a throw expression, this will |
| 2842 | /// be a NULL type. |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2843 | /// |
| Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2844 | /// \param E The expression being returned from the function or block, or |
| 2845 | /// being thrown. |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2846 | /// |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2847 | /// \param AllowParamOrMoveConstructible Whether we allow function parameters or |
| 2848 | /// id-expressions that could be moved out of the function to be considered NRVO |
| 2849 | /// candidates. C++ prohibits these for NRVO itself, but we re-use this logic to |
| 2850 | /// determine whether we should try to move as part of a return or throw (which |
| 2851 | /// does allow function parameters). |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2852 | /// |
| 2853 | /// \returns The NRVO candidate variable, if the return statement may use the |
| 2854 | /// NRVO, or NULL if there is no such candidate. |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2855 | VarDecl *Sema::getCopyElisionCandidate(QualType ReturnType, Expr *E, |
| 2856 | bool AllowParamOrMoveConstructible) { |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2857 | if (!getLangOpts().CPlusPlus) |
| 2858 | return nullptr; |
| 2859 | |
| 2860 | // - in a return statement in a function [where] ... |
| 2861 | // ... the expression is the name of a non-volatile automatic object ... |
| 2862 | DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()); |
| Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 2863 | if (!DR || DR->refersToEnclosingVariableOrCapture()) |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2864 | return nullptr; |
| 2865 | VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 2866 | if (!VD) |
| 2867 | return nullptr; |
| 2868 | |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2869 | if (isCopyElisionCandidate(ReturnType, VD, AllowParamOrMoveConstructible)) |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2870 | return VD; |
| 2871 | return nullptr; |
| 2872 | } |
| 2873 | |
| 2874 | bool Sema::isCopyElisionCandidate(QualType ReturnType, const VarDecl *VD, |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2875 | bool AllowParamOrMoveConstructible) { |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2876 | QualType VDType = VD->getType(); |
| Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2877 | // - in a return statement in a function with ... |
| 2878 | // ... a class return type ... |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2879 | if (!ReturnType.isNull() && !ReturnType->isDependentType()) { |
| Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2880 | if (!ReturnType->isRecordType()) |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2881 | return false; |
| Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2882 | // ... the same cv-unqualified type as the function return type ... |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2883 | // When considering moving this expression out, allow dissimilar types. |
| 2884 | if (!AllowParamOrMoveConstructible && !VDType->isDependentType() && |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2885 | !Context.hasSameUnqualifiedType(ReturnType, VDType)) |
| 2886 | return false; |
| Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2887 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2888 | |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2889 | // ...object (other than a function or catch-clause parameter)... |
| 2890 | if (VD->getKind() != Decl::Var && |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2891 | !(AllowParamOrMoveConstructible && VD->getKind() == Decl::ParmVar)) |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2892 | return false; |
| 2893 | if (VD->isExceptionVariable()) return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2894 | |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2895 | // ...automatic... |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2896 | if (!VD->hasLocalStorage()) return false; |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2897 | |
| Akira Hatanaka | 6697eff | 2017-02-15 05:15:28 +0000 | [diff] [blame] | 2898 | // Return false if VD is a __block variable. We don't want to implicitly move |
| 2899 | // out of a __block variable during a return because we cannot assume the |
| 2900 | // variable will no longer be used. |
| 2901 | if (VD->hasAttr<BlocksAttr>()) return false; |
| 2902 | |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2903 | if (AllowParamOrMoveConstructible) |
| 2904 | return true; |
| 2905 | |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2906 | // ...non-volatile... |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2907 | if (VD->getType().isVolatileQualified()) return false; |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2908 | |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2909 | // Variables with higher required alignment than their type's ABI |
| 2910 | // alignment cannot use NRVO. |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2911 | if (!VD->getType()->isDependentType() && VD->hasAttr<AlignedAttr>() && |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2912 | Context.getDeclAlign(VD) > Context.getTypeAlignInChars(VD->getType())) |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2913 | return false; |
| John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2914 | |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2915 | return true; |
| Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2918 | /// \brief Perform the initialization of a potentially-movable value, which |
| 2919 | /// is the result of return value. |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2920 | /// |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2921 | /// This routine implements C++14 [class.copy]p32, which attempts to treat |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2922 | /// returned lvalues as rvalues in certain cases (to prefer move construction), |
| 2923 | /// then falls back to treating them as lvalues if that failed. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2924 | ExprResult |
| Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2925 | Sema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity, |
| 2926 | const VarDecl *NRVOCandidate, |
| 2927 | QualType ResultType, |
| Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 2928 | Expr *Value, |
| 2929 | bool AllowNRVO) { |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2930 | // C++14 [class.copy]p32: |
| 2931 | // When the criteria for elision of a copy/move operation are met, but not for |
| 2932 | // an exception-declaration, and the object to be copied is designated by an |
| 2933 | // lvalue, or when the expression in a return statement is a (possibly |
| 2934 | // parenthesized) id-expression that names an object with automatic storage |
| 2935 | // duration declared in the body or parameter-declaration-clause of the |
| 2936 | // innermost enclosing function or lambda-expression, overload resolution to |
| 2937 | // select the constructor for the copy is first performed as if the object |
| 2938 | // were designated by an rvalue. |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2939 | ExprResult Res = ExprError(); |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2940 | |
| 2941 | if (AllowNRVO && !NRVOCandidate) |
| 2942 | NRVOCandidate = getCopyElisionCandidate(ResultType, Value, true); |
| 2943 | |
| 2944 | if (AllowNRVO && NRVOCandidate) { |
| 2945 | ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack, Value->getType(), |
| 2946 | CK_NoOp, Value, VK_XValue); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2947 | |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2948 | Expr *InitExpr = &AsRvalue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2949 | |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2950 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 2951 | Value->getLocStart(), Value->getLocStart()); |
| 2952 | |
| 2953 | InitializationSequence Seq(*this, Entity, Kind, InitExpr); |
| Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 2954 | if (Seq) { |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2955 | for (const InitializationSequence::Step &Step : Seq.steps()) { |
| 2956 | if (!(Step.Kind == |
| 2957 | InitializationSequence::SK_ConstructorInitialization || |
| 2958 | (Step.Kind == InitializationSequence::SK_UserConversion && |
| 2959 | isa<CXXConstructorDecl>(Step.Function.Function)))) |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2960 | continue; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2961 | |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2962 | CXXConstructorDecl *Constructor = |
| 2963 | cast<CXXConstructorDecl>(Step.Function.Function); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2964 | |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2965 | const RValueReferenceType *RRefType |
| Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2966 | = Constructor->getParamDecl(0)->getType() |
| 2967 | ->getAs<RValueReferenceType>(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2968 | |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2969 | // [...] If the first overload resolution fails or was not performed, or |
| 2970 | // if the type of the first parameter of the selected constructor is not |
| Nico Weber | 0a23404 | 2017-05-05 17:15:08 +0000 | [diff] [blame] | 2971 | // an rvalue reference to the object's type (possibly cv-qualified), |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2972 | // overload resolution is performed again, considering the object as an |
| 2973 | // lvalue. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2974 | if (!RRefType || |
| Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2975 | !Context.hasSameUnqualifiedType(RRefType->getPointeeType(), |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2976 | NRVOCandidate->getType())) |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2977 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2978 | |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2979 | // Promote "AsRvalue" to the heap, since we now need this |
| 2980 | // expression node to persist. |
| Erik Pilkington | fc235eb | 2016-06-30 23:09:13 +0000 | [diff] [blame] | 2981 | Value = ImplicitCastExpr::Create(Context, Value->getType(), CK_NoOp, |
| 2982 | Value, nullptr, VK_XValue); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2983 | |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2984 | // Complete type-checking the initialization of the return type |
| 2985 | // using the constructor we found. |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2986 | Res = Seq.Perform(*this, Entity, Kind, Value); |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2990 | |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2991 | // Either we didn't meet the criteria for treating an lvalue as an rvalue, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2992 | // above, or overload resolution failed. Either way, we need to try |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2993 | // (again) now with the return value expression as written. |
| 2994 | if (Res.isInvalid()) |
| Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2995 | Res = PerformCopyInitialization(Entity, SourceLocation(), Value); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2996 | |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2997 | return Res; |
| 2998 | } |
| 2999 | |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3000 | /// \brief Determine whether the declared return type of the specified function |
| 3001 | /// contains 'auto'. |
| 3002 | static bool hasDeducedReturnType(FunctionDecl *FD) { |
| 3003 | const FunctionProtoType *FPT = |
| 3004 | FD->getTypeSourceInfo()->getType()->castAs<FunctionProtoType>(); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3005 | return FPT->getReturnType()->isUndeducedType(); |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 3008 | /// ActOnCapScopeReturnStmt - Utility routine to type-check return statements |
| 3009 | /// for capturing scopes. |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 3010 | /// |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3011 | StmtResult |
| Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 3012 | Sema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
| 3013 | // If this is the first return we've seen, infer the return type. |
| Richard Smith | 9155be1 | 2013-05-12 03:09:35 +0000 | [diff] [blame] | 3014 | // [expr.prim.lambda]p4 in C++11; block literals follow the same rules. |
| Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 3015 | CapturingScopeInfo *CurCap = cast<CapturingScopeInfo>(getCurFunction()); |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 3016 | QualType FnRetType = CurCap->ReturnType; |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3017 | LambdaScopeInfo *CurLambda = dyn_cast<LambdaScopeInfo>(CurCap); |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3018 | bool HasDeducedReturnType = |
| 3019 | CurLambda && hasDeducedReturnType(CurLambda->CallOperator); |
| Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3020 | |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3021 | if (ExprEvalContexts.back().Context == |
| 3022 | ExpressionEvaluationContext::DiscardedStatement && |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3023 | (HasDeducedReturnType || CurCap->HasImplicitReturnType)) { |
| 3024 | if (RetValExp) { |
| 3025 | ExprResult ER = ActOnFinishFullExpr(RetValExp, ReturnLoc); |
| 3026 | if (ER.isInvalid()) |
| 3027 | return StmtError(); |
| 3028 | RetValExp = ER.get(); |
| 3029 | } |
| 3030 | return new (Context) ReturnStmt(ReturnLoc, RetValExp, nullptr); |
| 3031 | } |
| 3032 | |
| 3033 | if (HasDeducedReturnType) { |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3034 | // In C++1y, the return type may involve 'auto'. |
| 3035 | // FIXME: Blocks might have a return type of 'auto' explicitly specified. |
| 3036 | FunctionDecl *FD = CurLambda->CallOperator; |
| 3037 | if (CurCap->ReturnType.isNull()) |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3038 | CurCap->ReturnType = FD->getReturnType(); |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3039 | |
| 3040 | AutoType *AT = CurCap->ReturnType->getContainedAutoType(); |
| 3041 | assert(AT && "lost auto type from lambda return type"); |
| 3042 | if (DeduceFunctionTypeFromReturnExpr(FD, ReturnLoc, RetValExp, AT)) { |
| 3043 | FD->setInvalidDecl(); |
| 3044 | return StmtError(); |
| 3045 | } |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3046 | CurCap->ReturnType = FnRetType = FD->getReturnType(); |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3047 | } else if (CurCap->HasImplicitReturnType) { |
| 3048 | // For blocks/lambdas with implicit return types, we check each return |
| 3049 | // statement individually, and deduce the common return type when the block |
| 3050 | // or lambda is completed. |
| 3051 | // FIXME: Fold this into the 'auto' codepath above. |
| Douglas Gregor | 940a550 | 2012-02-09 18:40:39 +0000 | [diff] [blame] | 3052 | if (RetValExp && !isa<InitListExpr>(RetValExp)) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3053 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp); |
| 3054 | if (Result.isInvalid()) |
| 3055 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3056 | RetValExp = Result.get(); |
| Douglas Gregor | 0aa91e0 | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 3057 | |
| Richard Smith | 5a0e50c | 2014-12-19 22:10:51 +0000 | [diff] [blame] | 3058 | // DR1048: even prior to C++14, we should use the 'auto' deduction rules |
| 3059 | // when deducing a return type for a lambda-expression (or by extension |
| 3060 | // for a block). These rules differ from the stated C++11 rules only in |
| 3061 | // that they remove top-level cv-qualifiers. |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3062 | if (!CurContext->isDependentContext()) |
| Richard Smith | 5a0e50c | 2014-12-19 22:10:51 +0000 | [diff] [blame] | 3063 | FnRetType = RetValExp->getType().getUnqualifiedType(); |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3064 | else |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 3065 | FnRetType = CurCap->ReturnType = Context.DependentTy; |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3066 | } else { |
| Douglas Gregor | 940a550 | 2012-02-09 18:40:39 +0000 | [diff] [blame] | 3067 | if (RetValExp) { |
| 3068 | // C++11 [expr.lambda.prim]p4 bans inferring the result from an |
| 3069 | // initializer list, because it is not an expression (even |
| 3070 | // though we represent it as one). We still deduce 'void'. |
| 3071 | Diag(ReturnLoc, diag::err_lambda_return_init_list) |
| 3072 | << RetValExp->getSourceRange(); |
| 3073 | } |
| Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3074 | |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 3075 | FnRetType = Context.VoidTy; |
| Fariborz Jahanian | 5c12ca8 | 2011-12-03 23:53:56 +0000 | [diff] [blame] | 3076 | } |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 3077 | |
| 3078 | // Although we'll properly infer the type of the block once it's completed, |
| 3079 | // make sure we provide a return type now for better error recovery. |
| 3080 | if (CurCap->ReturnType.isNull()) |
| 3081 | CurCap->ReturnType = FnRetType; |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 3082 | } |
| Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 3083 | assert(!FnRetType.isNull()); |
| Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 3084 | |
| Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 3085 | if (BlockScopeInfo *CurBlock = dyn_cast<BlockScopeInfo>(CurCap)) { |
| Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 3086 | if (CurBlock->FunctionType->getAs<FunctionType>()->getNoReturnAttr()) { |
| 3087 | Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr); |
| 3088 | return StmtError(); |
| 3089 | } |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3090 | } else if (CapturedRegionScopeInfo *CurRegion = |
| 3091 | dyn_cast<CapturedRegionScopeInfo>(CurCap)) { |
| 3092 | Diag(ReturnLoc, diag::err_return_in_captured_stmt) << CurRegion->getRegionName(); |
| 3093 | return StmtError(); |
| Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 3094 | } else { |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3095 | assert(CurLambda && "unknown kind of captured scope"); |
| 3096 | if (CurLambda->CallOperator->getType()->getAs<FunctionType>() |
| 3097 | ->getNoReturnAttr()) { |
| Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 3098 | Diag(ReturnLoc, diag::err_noreturn_lambda_has_return_expr); |
| 3099 | return StmtError(); |
| 3100 | } |
| 3101 | } |
| Mike Stump | 56ed2ea | 2009-04-29 21:40:37 +0000 | [diff] [blame] | 3102 | |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 3103 | // Otherwise, verify that this result type matches the previous one. We are |
| 3104 | // pickier with blocks than for normal functions because we don't have GCC |
| 3105 | // compatibility to worry about here. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3106 | const VarDecl *NRVOCandidate = nullptr; |
| Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3107 | if (FnRetType->isDependentType()) { |
| John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 3108 | // Delay processing for now. TODO: there are lots of dependent |
| 3109 | // types we can conclusively prove aren't void. |
| 3110 | } else if (FnRetType->isVoidType()) { |
| Sebastian Redl | 74b173e | 2012-02-22 17:38:04 +0000 | [diff] [blame] | 3111 | if (RetValExp && !isa<InitListExpr>(RetValExp) && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3112 | !(getLangOpts().CPlusPlus && |
| John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 3113 | (RetValExp->isTypeDependent() || |
| 3114 | RetValExp->getType()->isVoidType()))) { |
| Fariborz Jahanian | 3ba24ba | 2012-03-21 16:45:13 +0000 | [diff] [blame] | 3115 | if (!getLangOpts().CPlusPlus && |
| 3116 | RetValExp->getType()->isVoidType()) |
| Fariborz Jahanian | 0740ed9 | 2012-03-21 20:28:39 +0000 | [diff] [blame] | 3117 | Diag(ReturnLoc, diag::ext_return_has_void_expr) << "literal" << 2; |
| Fariborz Jahanian | 3ba24ba | 2012-03-21 16:45:13 +0000 | [diff] [blame] | 3118 | else { |
| 3119 | Diag(ReturnLoc, diag::err_return_block_has_expr); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3120 | RetValExp = nullptr; |
| Fariborz Jahanian | 3ba24ba | 2012-03-21 16:45:13 +0000 | [diff] [blame] | 3121 | } |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 3122 | } |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3123 | } else if (!RetValExp) { |
| John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 3124 | return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); |
| 3125 | } else if (!RetValExp->isTypeDependent()) { |
| 3126 | // we have a non-void block with an expression, continue checking |
| Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 3127 | |
| John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 3128 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 3129 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 3130 | // function return. |
| Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 3131 | |
| John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 3132 | // In C++ the return statement is handled via a copy initialization. |
| 3133 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
| 3134 | NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); |
| 3135 | InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, |
| 3136 | FnRetType, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3137 | NRVOCandidate != nullptr); |
| John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 3138 | ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, |
| 3139 | FnRetType, RetValExp); |
| 3140 | if (Res.isInvalid()) { |
| 3141 | // FIXME: Cleanup temporaries here, anyway? |
| 3142 | return StmtError(); |
| Anders Carlsson | 6f923f8 | 2010-01-29 18:30:20 +0000 | [diff] [blame] | 3143 | } |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3144 | RetValExp = Res.get(); |
| Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 3145 | CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc); |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3146 | } else { |
| 3147 | NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 3148 | } |
| Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 3149 | |
| John McCall | 75f92b5 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 3150 | if (RetValExp) { |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3151 | ExprResult ER = ActOnFinishFullExpr(RetValExp, ReturnLoc); |
| 3152 | if (ER.isInvalid()) |
| 3153 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3154 | RetValExp = ER.get(); |
| John McCall | 75f92b5 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 3155 | } |
| John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 3156 | ReturnStmt *Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, |
| 3157 | NRVOCandidate); |
| John McCall | 75f92b5 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 3158 | |
| Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 3159 | // If we need to check for the named return value optimization, |
| 3160 | // or if we need to infer the return type, |
| 3161 | // save the return statement in our scope for later processing. |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3162 | if (CurCap->HasImplicitReturnType || NRVOCandidate) |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3163 | FunctionScopes.back()->Returns.push_back(Result); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3164 | |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 3165 | if (FunctionScopes.back()->FirstReturnLoc.isInvalid()) |
| 3166 | FunctionScopes.back()->FirstReturnLoc = ReturnLoc; |
| 3167 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3168 | return Result; |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 3169 | } |
| Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3170 | |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 3171 | namespace { |
| 3172 | /// \brief Marks all typedefs in all local classes in a type referenced. |
| 3173 | /// |
| 3174 | /// In a function like |
| 3175 | /// auto f() { |
| 3176 | /// struct S { typedef int a; }; |
| 3177 | /// return S(); |
| 3178 | /// } |
| 3179 | /// |
| 3180 | /// the local type escapes and could be referenced in some TUs but not in |
| 3181 | /// others. Pretend that all local typedefs are always referenced, to not warn |
| 3182 | /// on this. This isn't necessary if f has internal linkage, or the typedef |
| 3183 | /// is private. |
| 3184 | class LocalTypedefNameReferencer |
| 3185 | : public RecursiveASTVisitor<LocalTypedefNameReferencer> { |
| 3186 | public: |
| 3187 | LocalTypedefNameReferencer(Sema &S) : S(S) {} |
| 3188 | bool VisitRecordType(const RecordType *RT); |
| 3189 | private: |
| 3190 | Sema &S; |
| 3191 | }; |
| 3192 | bool LocalTypedefNameReferencer::VisitRecordType(const RecordType *RT) { |
| 3193 | auto *R = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 3194 | if (!R || !R->isLocalClass() || !R->isLocalClass()->isExternallyVisible() || |
| 3195 | R->isDependentType()) |
| 3196 | return true; |
| 3197 | for (auto *TmpD : R->decls()) |
| 3198 | if (auto *T = dyn_cast<TypedefNameDecl>(TmpD)) |
| 3199 | if (T->getAccess() != AS_private || R->hasFriends()) |
| 3200 | S.MarkAnyDeclReferenced(T->getLocation(), T, /*OdrUse=*/false); |
| 3201 | return true; |
| 3202 | } |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3203 | } |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 3204 | |
| Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 3205 | TypeLoc Sema::getReturnTypeLoc(FunctionDecl *FD) const { |
| Saleem Abdulrasool | e8aab74 | 2014-10-17 17:20:33 +0000 | [diff] [blame] | 3206 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 3207 | while (auto ATL = TL.getAs<AttributedTypeLoc>()) |
| 3208 | TL = ATL.getModifiedLoc().IgnoreParens(); |
| Saleem Abdulrasool | e8aab74 | 2014-10-17 17:20:33 +0000 | [diff] [blame] | 3209 | return TL.castAs<FunctionProtoTypeLoc>().getReturnLoc(); |
| Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3212 | /// Deduce the return type for a function from a returned expression, per |
| 3213 | /// C++1y [dcl.spec.auto]p6. |
| 3214 | bool Sema::DeduceFunctionTypeFromReturnExpr(FunctionDecl *FD, |
| 3215 | SourceLocation ReturnLoc, |
| 3216 | Expr *&RetExpr, |
| 3217 | AutoType *AT) { |
| Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 3218 | TypeLoc OrigResultType = getReturnTypeLoc(FD); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3219 | QualType Deduced; |
| Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3220 | |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3221 | if (RetExpr && isa<InitListExpr>(RetExpr)) { |
| 3222 | // If the deduction is for a return statement and the initializer is |
| 3223 | // a braced-init-list, the program is ill-formed. |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3224 | Diag(RetExpr->getExprLoc(), |
| 3225 | getCurLambda() ? diag::err_lambda_return_init_list |
| 3226 | : diag::err_auto_fn_return_init_list) |
| 3227 | << RetExpr->getSourceRange(); |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3228 | return true; |
| 3229 | } |
| 3230 | |
| 3231 | if (FD->isDependentContext()) { |
| 3232 | // C++1y [dcl.spec.auto]p12: |
| 3233 | // Return type deduction [...] occurs when the definition is |
| 3234 | // instantiated even if the function body contains a return |
| 3235 | // statement with a non-type-dependent operand. |
| 3236 | assert(AT->isDeduced() && "should have deduced to dependent type"); |
| 3237 | return false; |
| Douglas Gregor | 6032d5b | 2015-10-01 19:52:44 +0000 | [diff] [blame] | 3238 | } |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3239 | |
| Douglas Gregor | 6032d5b | 2015-10-01 19:52:44 +0000 | [diff] [blame] | 3240 | if (RetExpr) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3241 | // Otherwise, [...] deduce a value for U using the rules of template |
| 3242 | // argument deduction. |
| 3243 | DeduceAutoResult DAR = DeduceAutoType(OrigResultType, RetExpr, Deduced); |
| 3244 | |
| 3245 | if (DAR == DAR_Failed && !FD->isInvalidDecl()) |
| 3246 | Diag(RetExpr->getExprLoc(), diag::err_auto_fn_deduction_failure) |
| 3247 | << OrigResultType.getType() << RetExpr->getType(); |
| 3248 | |
| 3249 | if (DAR != DAR_Succeeded) |
| 3250 | return true; |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 3251 | |
| 3252 | // If a local type is part of the returned type, mark its fields as |
| 3253 | // referenced. |
| 3254 | LocalTypedefNameReferencer Referencer(*this); |
| 3255 | Referencer.TraverseType(RetExpr->getType()); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3256 | } else { |
| 3257 | // In the case of a return with no operand, the initializer is considered |
| 3258 | // to be void(). |
| 3259 | // |
| 3260 | // Deduction here can only succeed if the return type is exactly 'cv auto' |
| 3261 | // or 'decltype(auto)', so just check for that case directly. |
| 3262 | if (!OrigResultType.getType()->getAs<AutoType>()) { |
| 3263 | Diag(ReturnLoc, diag::err_auto_fn_return_void_but_not_auto) |
| 3264 | << OrigResultType.getType(); |
| 3265 | return true; |
| 3266 | } |
| 3267 | // We always deduce U = void in this case. |
| 3268 | Deduced = SubstAutoType(OrigResultType.getType(), Context.VoidTy); |
| 3269 | if (Deduced.isNull()) |
| 3270 | return true; |
| 3271 | } |
| 3272 | |
| 3273 | // If a function with a declared return type that contains a placeholder type |
| 3274 | // has multiple return statements, the return type is deduced for each return |
| 3275 | // statement. [...] if the type deduced is not the same in each deduction, |
| 3276 | // the program is ill-formed. |
| Argyrios Kyrtzidis | b4030df | 2016-01-30 01:51:20 +0000 | [diff] [blame] | 3277 | QualType DeducedT = AT->getDeducedType(); |
| 3278 | if (!DeducedT.isNull() && !FD->isInvalidDecl()) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3279 | AutoType *NewAT = Deduced->getContainedAutoType(); |
| Manman Ren | b4e8a1b | 2016-02-04 20:05:40 +0000 | [diff] [blame] | 3280 | // It is possible that NewAT->getDeducedType() is null. When that happens, |
| 3281 | // we should not crash, instead we ignore this deduction. |
| 3282 | if (NewAT->getDeducedType().isNull()) |
| 3283 | return false; |
| 3284 | |
| Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3285 | CanQualType OldDeducedType = Context.getCanonicalFunctionResultType( |
| Argyrios Kyrtzidis | b4030df | 2016-01-30 01:51:20 +0000 | [diff] [blame] | 3286 | DeducedT); |
| Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3287 | CanQualType NewDeducedType = Context.getCanonicalFunctionResultType( |
| 3288 | NewAT->getDeducedType()); |
| 3289 | if (!FD->isDependentContext() && OldDeducedType != NewDeducedType) { |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3290 | const LambdaScopeInfo *LambdaSI = getCurLambda(); |
| 3291 | if (LambdaSI && LambdaSI->HasImplicitReturnType) { |
| 3292 | Diag(ReturnLoc, diag::err_typecheck_missing_return_type_incompatible) |
| Argyrios Kyrtzidis | b4030df | 2016-01-30 01:51:20 +0000 | [diff] [blame] | 3293 | << NewAT->getDeducedType() << DeducedT |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3294 | << true /*IsLambda*/; |
| 3295 | } else { |
| 3296 | Diag(ReturnLoc, diag::err_auto_fn_different_deductions) |
| 3297 | << (AT->isDecltypeAuto() ? 1 : 0) |
| Argyrios Kyrtzidis | b4030df | 2016-01-30 01:51:20 +0000 | [diff] [blame] | 3298 | << NewAT->getDeducedType() << DeducedT; |
| Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3299 | } |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3300 | return true; |
| 3301 | } |
| 3302 | } else if (!FD->isInvalidDecl()) { |
| 3303 | // Update all declarations of the function to have the deduced return type. |
| 3304 | Context.adjustDeducedFunctionResultType(FD, Deduced); |
| 3305 | } |
| 3306 | |
| 3307 | return false; |
| 3308 | } |
| 3309 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3310 | StmtResult |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3311 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp, |
| 3312 | Scope *CurScope) { |
| 3313 | StmtResult R = BuildReturnStmt(ReturnLoc, RetValExp); |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3314 | if (R.isInvalid() || ExprEvalContexts.back().Context == |
| 3315 | ExpressionEvaluationContext::DiscardedStatement) |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3316 | return R; |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3317 | |
| 3318 | if (VarDecl *VD = |
| 3319 | const_cast<VarDecl*>(cast<ReturnStmt>(R.get())->getNRVOCandidate())) { |
| 3320 | CurScope->addNRVOCandidate(VD); |
| 3321 | } else { |
| 3322 | CurScope->setNoNRVO(); |
| 3323 | } |
| 3324 | |
| Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3325 | CheckJumpOutOfSEHFinally(*this, ReturnLoc, *CurScope->getFnParent()); |
| 3326 | |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3327 | return R; |
| 3328 | } |
| 3329 | |
| 3330 | StmtResult Sema::BuildReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
| Douglas Gregor | 4385d8b | 2011-05-20 15:32:55 +0000 | [diff] [blame] | 3331 | // Check for unexpanded parameter packs. |
| 3332 | if (RetValExp && DiagnoseUnexpandedParameterPack(RetValExp)) |
| 3333 | return StmtError(); |
| Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3334 | |
| Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 3335 | if (isa<CapturingScopeInfo>(getCurFunction())) |
| 3336 | return ActOnCapScopeReturnStmt(ReturnLoc, RetValExp); |
| Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3337 | |
| Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3338 | QualType FnRetType; |
| Eli Friedman | 410fc7a | 2012-03-30 01:13:43 +0000 | [diff] [blame] | 3339 | QualType RelatedRetType; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3340 | const AttrVec *Attrs = nullptr; |
| Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 3341 | bool isObjCMethod = false; |
| 3342 | |
| Mike Stump | d00bc1a | 2009-04-29 00:43:21 +0000 | [diff] [blame] | 3343 | if (const FunctionDecl *FD = getCurFunctionDecl()) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3344 | FnRetType = FD->getReturnType(); |
| Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 3345 | if (FD->hasAttrs()) |
| 3346 | Attrs = &FD->getAttrs(); |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3347 | if (FD->isNoReturn()) |
| Chris Lattner | 6e127a6 | 2009-05-31 19:32:13 +0000 | [diff] [blame] | 3348 | Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) |
| Eli Friedman | de95878 | 2012-01-05 00:49:17 +0000 | [diff] [blame] | 3349 | << FD->getDeclName(); |
| Richard Smith | 9bb192e | 2016-11-29 01:35:17 +0000 | [diff] [blame] | 3350 | if (FD->isMain() && RetValExp) |
| 3351 | if (isa<CXXBoolLiteralExpr>(RetValExp)) |
| 3352 | Diag(ReturnLoc, diag::warn_main_returns_bool_literal) |
| 3353 | << RetValExp->getSourceRange(); |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3354 | } else if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3355 | FnRetType = MD->getReturnType(); |
| Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 3356 | isObjCMethod = true; |
| 3357 | if (MD->hasAttrs()) |
| 3358 | Attrs = &MD->getAttrs(); |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3359 | if (MD->hasRelatedResultType() && MD->getClassInterface()) { |
| 3360 | // In the implementation of a method with a related return type, the |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3361 | // type used to type-check the validity of return statements within the |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3362 | // method body is a pointer to the type of the class being implemented. |
| Eli Friedman | 410fc7a | 2012-03-30 01:13:43 +0000 | [diff] [blame] | 3363 | RelatedRetType = Context.getObjCInterfaceType(MD->getClassInterface()); |
| 3364 | RelatedRetType = Context.getObjCObjectPointerType(RelatedRetType); |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3365 | } |
| 3366 | } else // If we don't have a function/method context, bail. |
| Steve Naroff | f3833d7 | 2009-03-03 00:45:38 +0000 | [diff] [blame] | 3367 | return StmtError(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3368 | |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3369 | // C++1z: discarded return statements are not considered when deducing a |
| 3370 | // return type. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3371 | if (ExprEvalContexts.back().Context == |
| 3372 | ExpressionEvaluationContext::DiscardedStatement && |
| Richard Smith | b130fe7 | 2016-06-23 19:16:49 +0000 | [diff] [blame] | 3373 | FnRetType->getContainedAutoType()) { |
| 3374 | if (RetValExp) { |
| 3375 | ExprResult ER = ActOnFinishFullExpr(RetValExp, ReturnLoc); |
| 3376 | if (ER.isInvalid()) |
| 3377 | return StmtError(); |
| 3378 | RetValExp = ER.get(); |
| 3379 | } |
| 3380 | return new (Context) ReturnStmt(ReturnLoc, RetValExp, nullptr); |
| 3381 | } |
| 3382 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3383 | // FIXME: Add a flag to the ScopeInfo to indicate whether we're performing |
| 3384 | // deduction. |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3385 | if (getLangOpts().CPlusPlus14) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3386 | if (AutoType *AT = FnRetType->getContainedAutoType()) { |
| 3387 | FunctionDecl *FD = cast<FunctionDecl>(CurContext); |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3388 | if (DeduceFunctionTypeFromReturnExpr(FD, ReturnLoc, RetValExp, AT)) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3389 | FD->setInvalidDecl(); |
| 3390 | return StmtError(); |
| 3391 | } else { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3392 | FnRetType = FD->getReturnType(); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3393 | } |
| 3394 | } |
| 3395 | } |
| 3396 | |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3397 | bool HasDependentReturnType = FnRetType->isDependentType(); |
| 3398 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3399 | ReturnStmt *Result = nullptr; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3400 | if (FnRetType->isVoidType()) { |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3401 | if (RetValExp) { |
| Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3402 | if (isa<InitListExpr>(RetValExp)) { |
| 3403 | // We simply never allow init lists as the return value of void |
| 3404 | // functions. This is compatible because this was never allowed before, |
| 3405 | // so there's no legacy code to deal with. |
| 3406 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 3407 | int FunctionKind = 0; |
| 3408 | if (isa<ObjCMethodDecl>(CurDecl)) |
| 3409 | FunctionKind = 1; |
| 3410 | else if (isa<CXXConstructorDecl>(CurDecl)) |
| 3411 | FunctionKind = 2; |
| 3412 | else if (isa<CXXDestructorDecl>(CurDecl)) |
| 3413 | FunctionKind = 3; |
| 3414 | |
| 3415 | Diag(ReturnLoc, diag::err_return_init_list) |
| 3416 | << CurDecl->getDeclName() << FunctionKind |
| 3417 | << RetValExp->getSourceRange(); |
| 3418 | |
| 3419 | // Drop the expression. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3420 | RetValExp = nullptr; |
| Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3421 | } else if (!RetValExp->isTypeDependent()) { |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3422 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 3423 | unsigned D = diag::ext_return_has_expr; |
| Fariborz Jahanian | a759848 | 2013-12-03 17:10:08 +0000 | [diff] [blame] | 3424 | if (RetValExp->getType()->isVoidType()) { |
| 3425 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 3426 | if (isa<CXXConstructorDecl>(CurDecl) || |
| 3427 | isa<CXXDestructorDecl>(CurDecl)) |
| 3428 | D = diag::err_ctor_dtor_returns_void; |
| 3429 | else |
| 3430 | D = diag::ext_return_has_void_expr; |
| 3431 | } |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3432 | else { |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3433 | ExprResult Result = RetValExp; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3434 | Result = IgnoredValueConversions(Result.get()); |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3435 | if (Result.isInvalid()) |
| 3436 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3437 | RetValExp = Result.get(); |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3438 | RetValExp = ImpCastExprToType(RetValExp, |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3439 | Context.VoidTy, CK_ToVoid).get(); |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3440 | } |
| Fariborz Jahanian | a759848 | 2013-12-03 17:10:08 +0000 | [diff] [blame] | 3441 | // return of void in constructor/destructor is illegal in C++. |
| 3442 | if (D == diag::err_ctor_dtor_returns_void) { |
| 3443 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 3444 | Diag(ReturnLoc, D) |
| 3445 | << CurDecl->getDeclName() << isa<CXXDestructorDecl>(CurDecl) |
| 3446 | << RetValExp->getSourceRange(); |
| 3447 | } |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3448 | // return (some void expression); is legal in C++. |
| Fariborz Jahanian | a759848 | 2013-12-03 17:10:08 +0000 | [diff] [blame] | 3449 | else if (D != diag::ext_return_has_void_expr || |
| Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 3450 | !getLangOpts().CPlusPlus) { |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3451 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| Chandler Carruth | 1406d6c | 2011-06-30 08:56:22 +0000 | [diff] [blame] | 3452 | |
| 3453 | int FunctionKind = 0; |
| 3454 | if (isa<ObjCMethodDecl>(CurDecl)) |
| 3455 | FunctionKind = 1; |
| 3456 | else if (isa<CXXConstructorDecl>(CurDecl)) |
| 3457 | FunctionKind = 2; |
| 3458 | else if (isa<CXXDestructorDecl>(CurDecl)) |
| 3459 | FunctionKind = 3; |
| 3460 | |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3461 | Diag(ReturnLoc, D) |
| Chandler Carruth | 1406d6c | 2011-06-30 08:56:22 +0000 | [diff] [blame] | 3462 | << CurDecl->getDeclName() << FunctionKind |
| Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3463 | << RetValExp->getSourceRange(); |
| 3464 | } |
| Chris Lattner | 0cb00d6 | 2008-12-18 02:03:48 +0000 | [diff] [blame] | 3465 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | |
| Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3467 | if (RetValExp) { |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3468 | ExprResult ER = ActOnFinishFullExpr(RetValExp, ReturnLoc); |
| 3469 | if (ER.isInvalid()) |
| 3470 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3471 | RetValExp = ER.get(); |
| Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3472 | } |
| Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 3473 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3474 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3475 | Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, nullptr); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3476 | } else if (!RetValExp && !HasDependentReturnType) { |
| David Majnemer | 2887ad3 | 2014-12-13 08:12:56 +0000 | [diff] [blame] | 3477 | FunctionDecl *FD = getCurFunctionDecl(); |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3478 | |
| David Majnemer | 2887ad3 | 2014-12-13 08:12:56 +0000 | [diff] [blame] | 3479 | unsigned DiagID; |
| 3480 | if (getLangOpts().CPlusPlus11 && FD && FD->isConstexpr()) { |
| 3481 | // C++11 [stmt.return]p2 |
| 3482 | DiagID = diag::err_constexpr_return_missing_expr; |
| 3483 | FD->setInvalidDecl(); |
| 3484 | } else if (getLangOpts().C99) { |
| 3485 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 3486 | DiagID = diag::ext_return_missing_expr; |
| 3487 | } else { |
| 3488 | // C90 6.6.6.4p4 |
| 3489 | DiagID = diag::warn_return_missing_expr; |
| 3490 | } |
| 3491 | |
| 3492 | if (FD) |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3493 | Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3494 | else |
| Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3495 | Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; |
| David Majnemer | 2887ad3 | 2014-12-13 08:12:56 +0000 | [diff] [blame] | 3496 | |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3497 | Result = new (Context) ReturnStmt(ReturnLoc); |
| 3498 | } else { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3499 | assert(RetValExp || HasDependentReturnType); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3500 | const VarDecl *NRVOCandidate = nullptr; |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3501 | |
| 3502 | QualType RetType = RelatedRetType.isNull() ? FnRetType : RelatedRetType; |
| 3503 | |
| 3504 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 3505 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 3506 | // function return. |
| 3507 | |
| 3508 | // In C++ the return statement is handled via a copy initialization, |
| 3509 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
| 3510 | if (RetValExp) |
| 3511 | NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3512 | if (!HasDependentReturnType && !RetValExp->isTypeDependent()) { |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3513 | // we have a non-void function with an expression, continue checking |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3514 | InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3515 | RetType, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3516 | NRVOCandidate != nullptr); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3517 | ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3518 | RetType, RetValExp); |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3519 | if (Res.isInvalid()) { |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3520 | // FIXME: Clean up temporaries here anyway? |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3521 | return StmtError(); |
| 3522 | } |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3523 | RetValExp = Res.getAs<Expr>(); |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3524 | |
| 3525 | // If we have a related result type, we need to implicitly |
| 3526 | // convert back to the formal result type. We can't pretend to |
| 3527 | // initialize the result again --- we might end double-retaining |
| Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3528 | // --- so instead we initialize a notional temporary. |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3529 | if (!RelatedRetType.isNull()) { |
| Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3530 | Entity = InitializedEntity::InitializeRelatedResult(getCurMethodDecl(), |
| 3531 | FnRetType); |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3532 | Res = PerformCopyInitialization(Entity, ReturnLoc, RetValExp); |
| 3533 | if (Res.isInvalid()) { |
| 3534 | // FIXME: Clean up temporaries here anyway? |
| 3535 | return StmtError(); |
| 3536 | } |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3537 | RetValExp = Res.getAs<Expr>(); |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 3540 | CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc, isObjCMethod, Attrs, |
| 3541 | getCurFunctionDecl()); |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3542 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3543 | |
| John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3544 | if (RetValExp) { |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3545 | ExprResult ER = ActOnFinishFullExpr(RetValExp, ReturnLoc); |
| 3546 | if (ER.isInvalid()) |
| 3547 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3548 | RetValExp = ER.get(); |
| John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3549 | } |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3550 | Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate); |
| Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3551 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3552 | |
| 3553 | // If we need to check for the named return value optimization, save the |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3554 | // return statement in our scope for later processing. |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3555 | if (Result->getNRVOCandidate()) |
| Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3556 | FunctionScopes.back()->Returns.push_back(Result); |
| Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 3557 | |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 3558 | if (FunctionScopes.back()->FirstReturnLoc.isInvalid()) |
| 3559 | FunctionScopes.back()->FirstReturnLoc = ReturnLoc; |
| 3560 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3561 | return Result; |
| Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3564 | StmtResult |
| Sebastian Redl | 481bf3f | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 3565 | Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3566 | SourceLocation RParen, Decl *Parm, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3567 | Stmt *Body) { |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3568 | VarDecl *Var = cast_or_null<VarDecl>(Parm); |
| Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3569 | if (Var && Var->isInvalidDecl()) |
| 3570 | return StmtError(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3571 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3572 | return new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body); |
| Fariborz Jahanian | 9e63b98 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3575 | StmtResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3576 | Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) { |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3577 | return new (Context) ObjCAtFinallyStmt(AtLoc, Body); |
| Fariborz Jahanian | 71234d8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 3578 | } |
| Fariborz Jahanian | f859ef2 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 3579 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3580 | StmtResult |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3582 | MultiStmtArg CatchStmts, Stmt *Finally) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3583 | if (!getLangOpts().ObjCExceptions) |
| Anders Carlsson | ce8dd3a | 2011-02-19 23:53:54 +0000 | [diff] [blame] | 3584 | Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@try"; |
| 3585 | |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3586 | getCurFunction()->setHasBranchProtectedScope(); |
| Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3587 | unsigned NumCatchStmts = CatchStmts.size(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3588 | return ObjCAtTryStmt::Create(Context, AtLoc, Try, CatchStmts.data(), |
| 3589 | NumCatchStmts, Finally); |
| Fariborz Jahanian | f859ef2 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
| John McCall | 0bd3e40 | 2012-05-08 21:41:25 +0000 | [diff] [blame] | 3592 | StmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw) { |
| Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3593 | if (Throw) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3594 | ExprResult Result = DefaultLvalueConversion(Throw); |
| 3595 | if (Result.isInvalid()) |
| 3596 | return StmtError(); |
| John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3597 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3598 | Result = ActOnFinishFullExpr(Result.get()); |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3599 | if (Result.isInvalid()) |
| 3600 | return StmtError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3601 | Throw = Result.get(); |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3602 | |
| Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3603 | QualType ThrowType = Throw->getType(); |
| 3604 | // Make sure the expression type is an ObjC pointer or "void *". |
| 3605 | if (!ThrowType->isDependentType() && |
| 3606 | !ThrowType->isObjCObjectPointerType()) { |
| 3607 | const PointerType *PT = ThrowType->getAs<PointerType>(); |
| 3608 | if (!PT || !PT->getPointeeType()->isVoidType()) |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 3609 | return StmtError(Diag(AtLoc, diag::err_objc_throw_expects_object) |
| Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3610 | << Throw->getType() << Throw->getSourceRange()); |
| 3611 | } |
| 3612 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3613 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3614 | return new (Context) ObjCAtThrowStmt(AtLoc, Throw); |
| Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3617 | StmtResult |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3618 | Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw, |
| Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3619 | Scope *CurScope) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3620 | if (!getLangOpts().ObjCExceptions) |
| Anders Carlsson | ce8dd3a | 2011-02-19 23:53:54 +0000 | [diff] [blame] | 3621 | Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@throw"; |
| 3622 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3623 | if (!Throw) { |
| Nico Weber | 9af63b2 | 2015-03-09 02:34:29 +0000 | [diff] [blame] | 3624 | // @throw without an expression designates a rethrow (which must occur |
| Steve Naroff | 5ee2c02 | 2009-02-11 20:05:44 +0000 | [diff] [blame] | 3625 | // in the context of an @catch clause). |
| 3626 | Scope *AtCatchParent = CurScope; |
| 3627 | while (AtCatchParent && !AtCatchParent->isAtCatchScope()) |
| 3628 | AtCatchParent = AtCatchParent->getParent(); |
| 3629 | if (!AtCatchParent) |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 3630 | return StmtError(Diag(AtLoc, diag::err_rethrow_used_outside_catch)); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3631 | } |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3632 | return BuildObjCAtThrowStmt(AtLoc, Throw); |
| Fariborz Jahanian | adfbbc3 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 3633 | } |
| Fariborz Jahanian | f859ef2 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 3634 | |
| John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3635 | ExprResult |
| 3636 | Sema::ActOnObjCAtSynchronizedOperand(SourceLocation atLoc, Expr *operand) { |
| 3637 | ExprResult result = DefaultLvalueConversion(operand); |
| 3638 | if (result.isInvalid()) |
| 3639 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3640 | operand = result.get(); |
| John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3641 | |
| 3642 | // Make sure the expression type is an ObjC pointer or "void *". |
| 3643 | QualType type = operand->getType(); |
| 3644 | if (!type->isDependentType() && |
| 3645 | !type->isObjCObjectPointerType()) { |
| 3646 | const PointerType *pointerType = type->getAs<PointerType>(); |
| Jordan Rose | 5790d52 | 2014-08-12 16:20:36 +0000 | [diff] [blame] | 3647 | if (!pointerType || !pointerType->getPointeeType()->isVoidType()) { |
| 3648 | if (getLangOpts().CPlusPlus) { |
| 3649 | if (RequireCompleteType(atLoc, type, |
| 3650 | diag::err_incomplete_receiver_type)) |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 3651 | return Diag(atLoc, diag::err_objc_synchronized_expects_object) |
| Jordan Rose | 5790d52 | 2014-08-12 16:20:36 +0000 | [diff] [blame] | 3652 | << type << operand->getSourceRange(); |
| 3653 | |
| 3654 | ExprResult result = PerformContextuallyConvertToObjCPointer(operand); |
| Richard Smith | e15a370 | 2016-10-06 23:12:58 +0000 | [diff] [blame] | 3655 | if (result.isInvalid()) |
| 3656 | return ExprError(); |
| Jordan Rose | 5790d52 | 2014-08-12 16:20:36 +0000 | [diff] [blame] | 3657 | if (!result.isUsable()) |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 3658 | return Diag(atLoc, diag::err_objc_synchronized_expects_object) |
| Jordan Rose | 5790d52 | 2014-08-12 16:20:36 +0000 | [diff] [blame] | 3659 | << type << operand->getSourceRange(); |
| 3660 | |
| 3661 | operand = result.get(); |
| 3662 | } else { |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 3663 | return Diag(atLoc, diag::err_objc_synchronized_expects_object) |
| Jordan Rose | 5790d52 | 2014-08-12 16:20:36 +0000 | [diff] [blame] | 3664 | << type << operand->getSourceRange(); |
| 3665 | } |
| 3666 | } |
| John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | // The operand to @synchronized is a full-expression. |
| Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3670 | return ActOnFinishFullExpr(operand); |
| John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3673 | StmtResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3674 | Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr, |
| 3675 | Stmt *SyncBody) { |
| John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3676 | // We can't jump into or indirect-jump out of a @synchronized block. |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3677 | getCurFunction()->setHasBranchProtectedScope(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3678 | return new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody); |
| Fariborz Jahanian | 48085b8 | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 3679 | } |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3680 | |
| 3681 | /// ActOnCXXCatchBlock - Takes an exception declaration and a handler block |
| 3682 | /// and creates a proper catch handler from them. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3683 | StmtResult |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3684 | Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3685 | Stmt *HandlerBlock) { |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3686 | // There's nothing to test that ActOnExceptionDecl didn't already test. |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3687 | return new (Context) |
| 3688 | CXXCatchStmt(CatchLoc, cast_or_null<VarDecl>(ExDecl), HandlerBlock); |
| Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3689 | } |
| Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3690 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3691 | StmtResult |
| 3692 | Sema::ActOnObjCAutoreleasePoolStmt(SourceLocation AtLoc, Stmt *Body) { |
| 3693 | getCurFunction()->setHasBranchProtectedScope(); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3694 | return new (Context) ObjCAutoreleasePoolStmt(AtLoc, Body); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
| Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3697 | namespace { |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3698 | class CatchHandlerType { |
| 3699 | QualType QT; |
| 3700 | unsigned IsPointer : 1; |
| Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 3701 | |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3702 | // This is a special constructor to be used only with DenseMapInfo's |
| 3703 | // getEmptyKey() and getTombstoneKey() functions. |
| 3704 | friend struct llvm::DenseMapInfo<CatchHandlerType>; |
| 3705 | enum Unique { ForDenseMap }; |
| 3706 | CatchHandlerType(QualType QT, Unique) : QT(QT), IsPointer(false) {} |
| 3707 | |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3708 | public: |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3709 | /// Used when creating a CatchHandlerType from a handler type; will determine |
| Eric Christopher | 2c4555a | 2015-06-19 01:52:53 +0000 | [diff] [blame] | 3710 | /// whether the type is a pointer or reference and will strip off the top |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3711 | /// level pointer and cv-qualifiers. |
| 3712 | CatchHandlerType(QualType Q) : QT(Q), IsPointer(false) { |
| 3713 | if (QT->isPointerType()) |
| 3714 | IsPointer = true; |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3715 | |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3716 | if (IsPointer || QT->isReferenceType()) |
| 3717 | QT = QT->getPointeeType(); |
| 3718 | QT = QT.getUnqualifiedType(); |
| 3719 | } |
| 3720 | |
| 3721 | /// Used when creating a CatchHandlerType from a base class type; pretends the |
| 3722 | /// type passed in had the pointer qualifier, does not need to get an |
| 3723 | /// unqualified type. |
| 3724 | CatchHandlerType(QualType QT, bool IsPointer) |
| 3725 | : QT(QT), IsPointer(IsPointer) {} |
| 3726 | |
| 3727 | QualType underlying() const { return QT; } |
| 3728 | bool isPointer() const { return IsPointer; } |
| 3729 | |
| 3730 | friend bool operator==(const CatchHandlerType &LHS, |
| 3731 | const CatchHandlerType &RHS) { |
| 3732 | // If the pointer qualification does not match, we can return early. |
| 3733 | if (LHS.IsPointer != RHS.IsPointer) |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3734 | return false; |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3735 | // Otherwise, check the underlying type without cv-qualifiers. |
| 3736 | return LHS.QT == RHS.QT; |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3737 | } |
| 3738 | }; |
| Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3739 | } // namespace |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3740 | |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3741 | namespace llvm { |
| 3742 | template <> struct DenseMapInfo<CatchHandlerType> { |
| 3743 | static CatchHandlerType getEmptyKey() { |
| 3744 | return CatchHandlerType(DenseMapInfo<QualType>::getEmptyKey(), |
| 3745 | CatchHandlerType::ForDenseMap); |
| 3746 | } |
| 3747 | |
| 3748 | static CatchHandlerType getTombstoneKey() { |
| 3749 | return CatchHandlerType(DenseMapInfo<QualType>::getTombstoneKey(), |
| 3750 | CatchHandlerType::ForDenseMap); |
| 3751 | } |
| 3752 | |
| 3753 | static unsigned getHashValue(const CatchHandlerType &Base) { |
| 3754 | return DenseMapInfo<QualType>::getHashValue(Base.underlying()); |
| 3755 | } |
| 3756 | |
| 3757 | static bool isEqual(const CatchHandlerType &LHS, |
| 3758 | const CatchHandlerType &RHS) { |
| 3759 | return LHS == RHS; |
| 3760 | } |
| 3761 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3762 | } |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3763 | |
| 3764 | namespace { |
| 3765 | class CatchTypePublicBases { |
| 3766 | ASTContext &Ctx; |
| 3767 | const llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> &TypesToCheck; |
| 3768 | const bool CheckAgainstPointer; |
| 3769 | |
| 3770 | CXXCatchStmt *FoundHandler; |
| 3771 | CanQualType FoundHandlerType; |
| 3772 | |
| 3773 | public: |
| 3774 | CatchTypePublicBases( |
| 3775 | ASTContext &Ctx, |
| 3776 | const llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> &T, bool C) |
| 3777 | : Ctx(Ctx), TypesToCheck(T), CheckAgainstPointer(C), |
| 3778 | FoundHandler(nullptr) {} |
| 3779 | |
| 3780 | CXXCatchStmt *getFoundHandler() const { return FoundHandler; } |
| 3781 | CanQualType getFoundHandlerType() const { return FoundHandlerType; } |
| 3782 | |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 3783 | bool operator()(const CXXBaseSpecifier *S, CXXBasePath &) { |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3784 | if (S->getAccessSpecifier() == AccessSpecifier::AS_public) { |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 3785 | CatchHandlerType Check(S->getType(), CheckAgainstPointer); |
| Benjamin Kramer | 536ffdf | 2016-02-13 15:49:17 +0000 | [diff] [blame] | 3786 | const auto &M = TypesToCheck; |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3787 | auto I = M.find(Check); |
| 3788 | if (I != M.end()) { |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 3789 | FoundHandler = I->second; |
| 3790 | FoundHandlerType = Ctx.getCanonicalType(S->getType()); |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3791 | return true; |
| 3792 | } |
| 3793 | } |
| 3794 | return false; |
| 3795 | } |
| 3796 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3797 | } |
| Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 3798 | |
| Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3799 | /// ActOnCXXTryBlock - Takes a try compound-statement and a number of |
| 3800 | /// handlers and creates a try statement from them. |
| Robert Wilhelm | cafda82 | 2013-08-22 09:20:03 +0000 | [diff] [blame] | 3801 | StmtResult Sema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock, |
| 3802 | ArrayRef<Stmt *> Handlers) { |
| Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 3803 | // Don't report an error if 'try' is used in system headers. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3804 | if (!getLangOpts().CXXExceptions && |
| Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 3805 | !getSourceManager().isInSystemHeader(TryLoc)) |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3806 | Diag(TryLoc, diag::err_exceptions_disabled) << "try"; |
| Anders Carlsson | 68b36af | 2011-02-19 19:26:44 +0000 | [diff] [blame] | 3807 | |
| Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 3808 | // Exceptions aren't allowed in CUDA device code. |
| 3809 | if (getLangOpts().CUDA) |
| Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 3810 | CUDADiagIfDeviceCode(TryLoc, diag::err_cuda_device_exceptions) |
| 3811 | << "try" << CurrentCUDATarget(); |
| Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 3812 | |
| Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3813 | if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope()) |
| 3814 | Diag(TryLoc, diag::err_omp_simd_region_cannot_use_stmt) << "try"; |
| 3815 | |
| Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3816 | sema::FunctionScopeInfo *FSI = getCurFunction(); |
| 3817 | |
| Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3818 | // C++ try is incompatible with SEH __try. |
| Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3819 | if (!getLangOpts().Borland && FSI->FirstSEHTryLoc.isValid()) { |
| Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3820 | Diag(TryLoc, diag::err_mixing_cxx_try_seh_try); |
| Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3821 | Diag(FSI->FirstSEHTryLoc, diag::note_conflicting_try_here) << "'__try'"; |
| Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
| Robert Wilhelm | cafda82 | 2013-08-22 09:20:03 +0000 | [diff] [blame] | 3824 | const unsigned NumHandlers = Handlers.size(); |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3825 | assert(!Handlers.empty() && |
| Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3826 | "The parser shouldn't call this if there are no handlers."); |
| Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3827 | |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3828 | llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> HandledTypes; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | for (unsigned i = 0; i < NumHandlers; ++i) { |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3830 | CXXCatchStmt *H = cast<CXXCatchStmt>(Handlers[i]); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3831 | |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3832 | // Diagnose when the handler is a catch-all handler, but it isn't the last |
| 3833 | // handler for the try block. [except.handle]p5. Also, skip exception |
| 3834 | // declarations that are invalid, since we can't usefully report on them. |
| 3835 | if (!H->getExceptionDecl()) { |
| 3836 | if (i < NumHandlers - 1) |
| 3837 | return StmtError(Diag(H->getLocStart(), diag::err_early_catch_all)); |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3838 | continue; |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3839 | } else if (H->getExceptionDecl()->isInvalidDecl()) |
| 3840 | continue; |
| 3841 | |
| 3842 | // Walk the type hierarchy to diagnose when this type has already been |
| 3843 | // handled (duplication), or cannot be handled (derivation inversion). We |
| 3844 | // ignore top-level cv-qualifiers, per [except.handle]p3 |
| Aaron Ballman | aa301de | 2015-04-08 00:13:33 +0000 | [diff] [blame] | 3845 | CatchHandlerType HandlerCHT = |
| 3846 | (QualType)Context.getCanonicalType(H->getCaughtType()); |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3847 | |
| 3848 | // We can ignore whether the type is a reference or a pointer; we need the |
| 3849 | // underlying declaration type in order to get at the underlying record |
| 3850 | // decl, if there is one. |
| 3851 | QualType Underlying = HandlerCHT.underlying(); |
| 3852 | if (auto *RD = Underlying->getAsCXXRecordDecl()) { |
| 3853 | if (!RD->hasDefinition()) |
| 3854 | continue; |
| 3855 | // Check that none of the public, unambiguous base classes are in the |
| 3856 | // map ([except.handle]p1). Give the base classes the same pointer |
| 3857 | // qualification as the original type we are basing off of. This allows |
| 3858 | // comparison against the handler type using the same top-level pointer |
| 3859 | // as the original type. |
| 3860 | CXXBasePaths Paths; |
| 3861 | Paths.setOrigin(RD); |
| 3862 | CatchTypePublicBases CTPB(Context, HandledTypes, HandlerCHT.isPointer()); |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 3863 | if (RD->lookupInBases(CTPB, Paths)) { |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3864 | const CXXCatchStmt *Problem = CTPB.getFoundHandler(); |
| 3865 | if (!Paths.isAmbiguous(CTPB.getFoundHandlerType())) { |
| 3866 | Diag(H->getExceptionDecl()->getTypeSpecStartLoc(), |
| 3867 | diag::warn_exception_caught_by_earlier_handler) |
| 3868 | << H->getCaughtType(); |
| 3869 | Diag(Problem->getExceptionDecl()->getTypeSpecStartLoc(), |
| 3870 | diag::note_previous_exception_handler) |
| 3871 | << Problem->getCaughtType(); |
| 3872 | } |
| 3873 | } |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3874 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | |
| Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3876 | // Add the type the list of ones we have handled; diagnose if we've already |
| 3877 | // handled it. |
| 3878 | auto R = HandledTypes.insert(std::make_pair(H->getCaughtType(), H)); |
| 3879 | if (!R.second) { |
| 3880 | const CXXCatchStmt *Problem = R.first->second; |
| 3881 | Diag(H->getExceptionDecl()->getTypeSpecStartLoc(), |
| 3882 | diag::warn_exception_caught_by_earlier_handler) |
| 3883 | << H->getCaughtType(); |
| 3884 | Diag(Problem->getExceptionDecl()->getTypeSpecStartLoc(), |
| 3885 | diag::note_previous_exception_handler) |
| 3886 | << Problem->getCaughtType(); |
| Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3887 | } |
| 3888 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3889 | |
| Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3890 | FSI->setHasCXXTry(TryLoc); |
| John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 3891 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3892 | return CXXTryStmt::Create(Context, TryLoc, TryBlock, Handlers); |
| Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3893 | } |
| John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3894 | |
| Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3895 | StmtResult Sema::ActOnSEHTryBlock(bool IsCXXTry, SourceLocation TryLoc, |
| 3896 | Stmt *TryBlock, Stmt *Handler) { |
| John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3897 | assert(TryBlock && Handler); |
| 3898 | |
| Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3899 | sema::FunctionScopeInfo *FSI = getCurFunction(); |
| 3900 | |
| Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3901 | // SEH __try is incompatible with C++ try. Borland appears to support this, |
| 3902 | // however. |
| Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3903 | if (!getLangOpts().Borland) { |
| 3904 | if (FSI->FirstCXXTryLoc.isValid()) { |
| 3905 | Diag(TryLoc, diag::err_mixing_cxx_try_seh_try); |
| 3906 | Diag(FSI->FirstCXXTryLoc, diag::note_conflicting_try_here) << "'try'"; |
| 3907 | } |
| Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3908 | } |
| John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3909 | |
| Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3910 | FSI->setHasSEHTry(TryLoc); |
| 3911 | |
| 3912 | // Reject __try in Obj-C methods, blocks, and captured decls, since we don't |
| 3913 | // track if they use SEH. |
| 3914 | DeclContext *DC = CurContext; |
| 3915 | while (DC && !DC->isFunctionOrMethod()) |
| 3916 | DC = DC->getParent(); |
| 3917 | FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DC); |
| 3918 | if (FD) |
| 3919 | FD->setUsesSEHTry(true); |
| 3920 | else |
| 3921 | Diag(TryLoc, diag::err_seh_try_outside_functions); |
| Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3922 | |
| Reid Kleckner | 8819a40 | 2015-07-10 00:16:25 +0000 | [diff] [blame] | 3923 | // Reject __try on unsupported targets. |
| 3924 | if (!Context.getTargetInfo().isSEHTrySupported()) |
| 3925 | Diag(TryLoc, diag::err_seh_try_unsupported); |
| 3926 | |
| Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3927 | return SEHTryStmt::Create(Context, IsCXXTry, TryLoc, TryBlock, Handler); |
| John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
| 3930 | StmtResult |
| 3931 | Sema::ActOnSEHExceptBlock(SourceLocation Loc, |
| 3932 | Expr *FilterExpr, |
| 3933 | Stmt *Block) { |
| 3934 | assert(FilterExpr && Block); |
| 3935 | |
| 3936 | if(!FilterExpr->getType()->isIntegerType()) { |
| Francois Pichet | fbf7e17 | 2011-06-02 00:47:27 +0000 | [diff] [blame] | 3937 | return StmtError(Diag(FilterExpr->getExprLoc(), |
| 3938 | diag::err_filter_expression_integral) |
| 3939 | << FilterExpr->getType()); |
| John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3942 | return SEHExceptStmt::Create(Context,Loc,FilterExpr,Block); |
| John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3945 | void Sema::ActOnStartSEHFinallyBlock() { |
| 3946 | CurrentSEHFinally.push_back(CurScope); |
| 3947 | } |
| 3948 | |
| Nico Weber | ce90329 | 2015-03-09 03:17:15 +0000 | [diff] [blame] | 3949 | void Sema::ActOnAbortSEHFinallyBlock() { |
| 3950 | CurrentSEHFinally.pop_back(); |
| 3951 | } |
| 3952 | |
| Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3953 | StmtResult Sema::ActOnFinishSEHFinallyBlock(SourceLocation Loc, Stmt *Block) { |
| John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3954 | assert(Block); |
| Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3955 | CurrentSEHFinally.pop_back(); |
| 3956 | return SEHFinallyStmt::Create(Context, Loc, Block); |
| John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3957 | } |
| Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3958 | |
| Nico Weber | c7d0596 | 2014-07-06 22:32:59 +0000 | [diff] [blame] | 3959 | StmtResult |
| 3960 | Sema::ActOnSEHLeaveStmt(SourceLocation Loc, Scope *CurScope) { |
| Nico Weber | eb61d4d | 2014-07-06 22:53:19 +0000 | [diff] [blame] | 3961 | Scope *SEHTryParent = CurScope; |
| 3962 | while (SEHTryParent && !SEHTryParent->isSEHTryScope()) |
| 3963 | SEHTryParent = SEHTryParent->getParent(); |
| 3964 | if (!SEHTryParent) |
| 3965 | return StmtError(Diag(Loc, diag::err_ms___leave_not_in___try)); |
| Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3966 | CheckJumpOutOfSEHFinally(*this, Loc, *SEHTryParent); |
| Nico Weber | eb61d4d | 2014-07-06 22:53:19 +0000 | [diff] [blame] | 3967 | |
| Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 3968 | return new (Context) SEHLeaveStmt(Loc); |
| Nico Weber | c7d0596 | 2014-07-06 22:32:59 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
| Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3971 | StmtResult Sema::BuildMSDependentExistsStmt(SourceLocation KeywordLoc, |
| 3972 | bool IsIfExists, |
| 3973 | NestedNameSpecifierLoc QualifierLoc, |
| 3974 | DeclarationNameInfo NameInfo, |
| 3975 | Stmt *Nested) |
| 3976 | { |
| 3977 | return new (Context) MSDependentExistsStmt(KeywordLoc, IsIfExists, |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3978 | QualifierLoc, NameInfo, |
| Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3979 | cast<CompoundStmt>(Nested)); |
| 3980 | } |
| 3981 | |
| 3982 | |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3983 | StmtResult Sema::ActOnMSDependentExistsStmt(SourceLocation KeywordLoc, |
| Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3984 | bool IsIfExists, |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3985 | CXXScopeSpec &SS, |
| Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3986 | UnqualifiedId &Name, |
| 3987 | Stmt *Nested) { |
| Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3988 | return BuildMSDependentExistsStmt(KeywordLoc, IsIfExists, |
| Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3989 | SS.getWithLocInContext(Context), |
| 3990 | GetNameFromUnqualifiedId(Name), |
| 3991 | Nested); |
| 3992 | } |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3993 | |
| 3994 | RecordDecl* |
| Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 3995 | Sema::CreateCapturedStmtRecordDecl(CapturedDecl *&CD, SourceLocation Loc, |
| 3996 | unsigned NumParams) { |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3997 | DeclContext *DC = CurContext; |
| 3998 | while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext())) |
| 3999 | DC = DC->getParent(); |
| 4000 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4001 | RecordDecl *RD = nullptr; |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4002 | if (getLangOpts().CPlusPlus) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4003 | RD = CXXRecordDecl::Create(Context, TTK_Struct, DC, Loc, Loc, |
| 4004 | /*Id=*/nullptr); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4005 | else |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4006 | RD = RecordDecl::Create(Context, TTK_Struct, DC, Loc, Loc, /*Id=*/nullptr); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4007 | |
| Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 4008 | RD->setCapturedRecord(); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4009 | DC->addDecl(RD); |
| 4010 | RD->setImplicit(); |
| 4011 | RD->startDefinition(); |
| 4012 | |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4013 | assert(NumParams > 0 && "CapturedStmt requires context parameter"); |
| Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4014 | CD = CapturedDecl::Create(Context, CurContext, NumParams); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4015 | DC->addDecl(CD); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4016 | return RD; |
| 4017 | } |
| 4018 | |
| 4019 | static void buildCapturedStmtCaptureList( |
| 4020 | SmallVectorImpl<CapturedStmt::Capture> &Captures, |
| 4021 | SmallVectorImpl<Expr *> &CaptureInits, |
| 4022 | ArrayRef<CapturingScopeInfo::Capture> Candidates) { |
| 4023 | |
| 4024 | typedef ArrayRef<CapturingScopeInfo::Capture>::const_iterator CaptureIter; |
| 4025 | for (CaptureIter Cap = Candidates.begin(); Cap != Candidates.end(); ++Cap) { |
| 4026 | |
| 4027 | if (Cap->isThisCapture()) { |
| 4028 | Captures.push_back(CapturedStmt::Capture(Cap->getLocation(), |
| 4029 | CapturedStmt::VCK_This)); |
| Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 4030 | CaptureInits.push_back(Cap->getInitExpr()); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4031 | continue; |
| Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 4032 | } else if (Cap->isVLATypeCapture()) { |
| 4033 | Captures.push_back( |
| 4034 | CapturedStmt::Capture(Cap->getLocation(), CapturedStmt::VCK_VLAType)); |
| 4035 | CaptureInits.push_back(nullptr); |
| 4036 | continue; |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4039 | Captures.push_back(CapturedStmt::Capture(Cap->getLocation(), |
| Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 4040 | Cap->isReferenceCapture() |
| 4041 | ? CapturedStmt::VCK_ByRef |
| 4042 | : CapturedStmt::VCK_ByCopy, |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4043 | Cap->getVariable())); |
| Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 4044 | CaptureInits.push_back(Cap->getInitExpr()); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope, |
| Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 4049 | CapturedRegionKind Kind, |
| 4050 | unsigned NumParams) { |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4051 | CapturedDecl *CD = nullptr; |
| Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4052 | RecordDecl *RD = CreateCapturedStmtRecordDecl(CD, Loc, NumParams); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4053 | |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4054 | // Build the context parameter |
| 4055 | DeclContext *DC = CapturedDecl::castToDeclContext(CD); |
| 4056 | IdentifierInfo *ParamName = &Context.Idents.get("__context"); |
| 4057 | QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD)); |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4058 | auto *Param = |
| 4059 | ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType, |
| 4060 | ImplicitParamDecl::CapturedContext); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4061 | DC->addDecl(Param); |
| 4062 | |
| 4063 | CD->setContextParam(0, Param); |
| 4064 | |
| 4065 | // Enter the capturing scope for this captured region. |
| 4066 | PushCapturedRegionScope(CurScope, CD, RD, Kind); |
| 4067 | |
| 4068 | if (CurScope) |
| 4069 | PushDeclContext(CurScope, CD); |
| 4070 | else |
| 4071 | CurContext = CD; |
| 4072 | |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4073 | PushExpressionEvaluationContext( |
| 4074 | ExpressionEvaluationContext::PotentiallyEvaluated); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope, |
| 4078 | CapturedRegionKind Kind, |
| 4079 | ArrayRef<CapturedParamNameType> Params) { |
| 4080 | CapturedDecl *CD = nullptr; |
| 4081 | RecordDecl *RD = CreateCapturedStmtRecordDecl(CD, Loc, Params.size()); |
| 4082 | |
| 4083 | // Build the context parameter |
| 4084 | DeclContext *DC = CapturedDecl::castToDeclContext(CD); |
| 4085 | bool ContextIsFound = false; |
| 4086 | unsigned ParamNum = 0; |
| 4087 | for (ArrayRef<CapturedParamNameType>::iterator I = Params.begin(), |
| 4088 | E = Params.end(); |
| 4089 | I != E; ++I, ++ParamNum) { |
| 4090 | if (I->second.isNull()) { |
| 4091 | assert(!ContextIsFound && |
| 4092 | "null type has been found already for '__context' parameter"); |
| 4093 | IdentifierInfo *ParamName = &Context.Idents.get("__context"); |
| 4094 | QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD)); |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4095 | auto *Param = |
| 4096 | ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType, |
| 4097 | ImplicitParamDecl::CapturedContext); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4098 | DC->addDecl(Param); |
| 4099 | CD->setContextParam(ParamNum, Param); |
| 4100 | ContextIsFound = true; |
| 4101 | } else { |
| 4102 | IdentifierInfo *ParamName = &Context.Idents.get(I->first); |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4103 | auto *Param = |
| 4104 | ImplicitParamDecl::Create(Context, DC, Loc, ParamName, I->second, |
| 4105 | ImplicitParamDecl::CapturedContext); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4106 | DC->addDecl(Param); |
| 4107 | CD->setParam(ParamNum, Param); |
| 4108 | } |
| 4109 | } |
| 4110 | assert(ContextIsFound && "no null type for '__context' parameter"); |
| Alexey Bataev | 301a2d9 | 2014-05-14 10:40:54 +0000 | [diff] [blame] | 4111 | if (!ContextIsFound) { |
| 4112 | // Add __context implicitly if it is not specified. |
| 4113 | IdentifierInfo *ParamName = &Context.Idents.get("__context"); |
| 4114 | QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD)); |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4115 | auto *Param = |
| 4116 | ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType, |
| 4117 | ImplicitParamDecl::CapturedContext); |
| Alexey Bataev | 301a2d9 | 2014-05-14 10:40:54 +0000 | [diff] [blame] | 4118 | DC->addDecl(Param); |
| 4119 | CD->setContextParam(ParamNum, Param); |
| 4120 | } |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4121 | // Enter the capturing scope for this captured region. |
| 4122 | PushCapturedRegionScope(CurScope, CD, RD, Kind); |
| 4123 | |
| 4124 | if (CurScope) |
| 4125 | PushDeclContext(CurScope, CD); |
| 4126 | else |
| 4127 | CurContext = CD; |
| 4128 | |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4129 | PushExpressionEvaluationContext( |
| 4130 | ExpressionEvaluationContext::PotentiallyEvaluated); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
| Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 4133 | void Sema::ActOnCapturedRegionError() { |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4134 | DiscardCleanupsInEvaluationContext(); |
| 4135 | PopExpressionEvaluationContext(); |
| 4136 | |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4137 | CapturedRegionScopeInfo *RSI = getCurCapturedRegion(); |
| 4138 | RecordDecl *Record = RSI->TheRecordDecl; |
| 4139 | Record->setInvalidDecl(); |
| 4140 | |
| Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 4141 | SmallVector<Decl*, 4> Fields(Record->fields()); |
| Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4142 | ActOnFields(/*Scope=*/nullptr, Record->getLocation(), Record, Fields, |
| 4143 | SourceLocation(), SourceLocation(), /*AttributeList=*/nullptr); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4144 | |
| Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 4145 | PopDeclContext(); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4146 | PopFunctionScopeInfo(); |
| 4147 | } |
| 4148 | |
| 4149 | StmtResult Sema::ActOnCapturedRegionEnd(Stmt *S) { |
| 4150 | CapturedRegionScopeInfo *RSI = getCurCapturedRegion(); |
| 4151 | |
| 4152 | SmallVector<CapturedStmt::Capture, 4> Captures; |
| 4153 | SmallVector<Expr *, 4> CaptureInits; |
| 4154 | buildCapturedStmtCaptureList(Captures, CaptureInits, RSI->Captures); |
| 4155 | |
| 4156 | CapturedDecl *CD = RSI->TheCapturedDecl; |
| 4157 | RecordDecl *RD = RSI->TheRecordDecl; |
| 4158 | |
| Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 4159 | CapturedStmt *Res = CapturedStmt::Create( |
| 4160 | getASTContext(), S, static_cast<CapturedRegionKind>(RSI->CapRegionKind), |
| 4161 | Captures, CaptureInits, CD, RD); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4162 | |
| 4163 | CD->setBody(Res->getCapturedStmt()); |
| 4164 | RD->completeDefinition(); |
| 4165 | |
| Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 4166 | DiscardCleanupsInEvaluationContext(); |
| 4167 | PopExpressionEvaluationContext(); |
| 4168 | |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4169 | PopDeclContext(); |
| 4170 | PopFunctionScopeInfo(); |
| 4171 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 4172 | return Res; |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4173 | } |