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