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