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