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