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