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