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