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