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