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