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