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