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