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