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