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" |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTDiagnostic.h" |
John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 17 | #include "clang/AST/CharUnits.h" |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 20 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | d0c22e0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 2ba5ca9 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtObjC.h" |
John McCall | 2351cb9 | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Reid Kleckner | 9fe7f23 | 2015-07-07 00:36:30 +0000 | [diff] [blame] | 28 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "clang/Lex/Preprocessor.h" |
| 30 | #include "clang/Sema/Initialization.h" |
| 31 | #include "clang/Sema/Lookup.h" |
| 32 | #include "clang/Sema/Scope.h" |
| 33 | #include "clang/Sema/ScopeInfo.h" |
Chris Lattner | 70a4e9b | 2011-02-21 21:40:33 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/ArrayRef.h" |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/DenseMap.h" |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 0a9f4e0 | 2012-05-16 16:11:17 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallString.h" |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 40 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 41 | using namespace sema; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 42 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 43 | StmtResult Sema::ActOnExprStmt(ExprResult FE) { |
| 44 | if (FE.isInvalid()) |
| 45 | return StmtError(); |
| 46 | |
| 47 | FE = ActOnFinishFullExpr(FE.get(), FE.get()->getExprLoc(), |
| 48 | /*DiscardedValue*/ true); |
| 49 | if (FE.isInvalid()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 50 | return StmtError(); |
| 51 | |
Chris Lattner | 903eb51 | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 52 | // C99 6.8.3p2: The expression in an expression statement is evaluated as a |
| 53 | // void expression for its side effects. Conversion to void allows any |
| 54 | // operand, even incomplete types. |
Sebastian Redl | 52f03ba | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 903eb51 | 2008-07-25 23:18:17 +0000 | [diff] [blame] | 56 | // Same thing in for stmt first clause (when expr) and third clause. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 57 | return StmtResult(FE.getAs<Stmt>()); |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
John McCall | eaef89b | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 61 | StmtResult Sema::ActOnExprStmtError() { |
| 62 | DiscardCleanupsInEvaluationContext(); |
| 63 | return StmtError(); |
| 64 | } |
| 65 | |
Argyrios Kyrtzidis | f7620e4 | 2011-04-27 05:04:02 +0000 | [diff] [blame] | 66 | StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, |
Argyrios Kyrtzidis | 43ea78b | 2011-09-01 21:53:45 +0000 | [diff] [blame] | 67 | bool HasLeadingEmptyMacro) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 68 | return new (Context) NullStmt(SemiLoc, HasLeadingEmptyMacro); |
Chris Lattner | 0f203a7 | 2007-05-28 01:45:28 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 71 | StmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, SourceLocation StartLoc, |
| 72 | SourceLocation EndLoc) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 73 | DeclGroupRef DG = dg.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Chris Lattner | cbafe8d | 2009-04-12 20:13:14 +0000 | [diff] [blame] | 75 | // If we have an invalid decl, just return an error. |
| 76 | if (DG.isNull()) return StmtError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 78 | return new (Context) DeclStmt(DG, StartLoc, EndLoc); |
Steve Naroff | 2a8ad18 | 2007-05-29 22:59:26 +0000 | [diff] [blame] | 79 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 80 | |
Fariborz Jahanian | e774fa6 | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 81 | void Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { |
Serge Pavlov | 9ddb76e | 2013-08-27 13:15:56 +0000 | [diff] [blame] | 82 | DeclGroupRef DG = dg.get(); |
Wei Pan | c4c76d1 | 2013-05-03 21:07:45 +0000 | [diff] [blame] | 83 | |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 84 | // If we don't have a declaration, or we have an invalid declaration, |
| 85 | // just return. |
| 86 | if (DG.isNull() || !DG.isSingleDecl()) |
| 87 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 88 | |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 89 | Decl *decl = DG.getSingleDecl(); |
| 90 | if (!decl || decl->isInvalidDecl()) |
| 91 | return; |
| 92 | |
| 93 | // Only variable declarations are permitted. |
| 94 | VarDecl *var = dyn_cast<VarDecl>(decl); |
| 95 | if (!var) { |
| 96 | Diag(decl->getLocation(), diag::err_non_variable_decl_in_for); |
| 97 | decl->setInvalidDecl(); |
| 98 | return; |
| 99 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 100 | |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 101 | // foreach variables are never actually initialized in the way that |
| 102 | // the parser came up with. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 103 | var->setInit(nullptr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 104 | |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 105 | // In ARC, we don't need to retain the iteration variable of a fast |
| 106 | // enumeration loop. Rather than actually trying to catch that |
| 107 | // during declaration processing, we remove the consequences here. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 108 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 109 | QualType type = var->getType(); |
| 110 | |
| 111 | // Only do this if we inferred the lifetime. Inferred lifetime |
| 112 | // will show up as a local qualifier because explicit lifetime |
| 113 | // should have shown up as an AttributedType instead. |
| 114 | if (type.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 115 | // Add 'const' and mark the variable as pseudo-strong. |
| 116 | var->setType(type.withConst()); |
| 117 | var->setARCPseudoStrong(true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
Fariborz Jahanian | e774fa6 | 2009-11-19 22:12:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 122 | /// \brief Diagnose unused comparisons, both builtin and overloaded operators. |
| 123 | /// For '==' and '!=', suggest fixits for '=' or '|='. |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 124 | /// |
| 125 | /// Adding a cast to void (or other expression wrappers) will prevent the |
| 126 | /// warning from firing. |
Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 127 | static bool DiagnoseUnusedComparison(Sema &S, const Expr *E) { |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 128 | SourceLocation Loc; |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 129 | bool IsNotEqual, CanAssign, IsRelational; |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 130 | |
| 131 | if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 132 | if (!Op->isComparisonOp()) |
Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 133 | return false; |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 134 | |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 135 | IsRelational = Op->isRelationalOp(); |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 136 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | e89ca5f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 137 | IsNotEqual = Op->getOpcode() == BO_NE; |
| 138 | CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue(); |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 139 | } else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 140 | switch (Op->getOperator()) { |
| 141 | default: |
Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 142 | return false; |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 143 | case OO_EqualEqual: |
| 144 | case OO_ExclaimEqual: |
| 145 | IsRelational = false; |
| 146 | break; |
| 147 | case OO_Less: |
| 148 | case OO_Greater: |
| 149 | case OO_GreaterEqual: |
| 150 | case OO_LessEqual: |
| 151 | IsRelational = true; |
| 152 | break; |
| 153 | } |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 154 | |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 155 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | e89ca5f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 156 | IsNotEqual = Op->getOperator() == OO_ExclaimEqual; |
| 157 | CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue(); |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 158 | } else { |
| 159 | // Not a typo-prone comparison. |
Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 160 | return false; |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Suppress warnings when the operator, suspicious as it may be, comes from |
| 164 | // a macro expansion. |
Matt Beaumont-Gay | b1e71a7 | 2013-01-12 00:54:16 +0000 | [diff] [blame] | 165 | if (S.SourceMgr.isMacroBodyExpansion(Loc)) |
Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 166 | return false; |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 167 | |
Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 168 | S.Diag(Loc, diag::warn_unused_comparison) |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 169 | << (unsigned)IsRelational << (unsigned)IsNotEqual << E->getSourceRange(); |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 170 | |
Chandler Carruth | e89ca5f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 171 | // If the LHS is a plausible entity to assign to, provide a fixit hint to |
| 172 | // correct common typos. |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 173 | if (!IsRelational && CanAssign) { |
Chandler Carruth | e89ca5f | 2011-08-17 08:38:11 +0000 | [diff] [blame] | 174 | if (IsNotEqual) |
| 175 | S.Diag(Loc, diag::note_inequality_comparison_to_or_assign) |
| 176 | << FixItHint::CreateReplacement(Loc, "|="); |
| 177 | else |
| 178 | S.Diag(Loc, diag::note_equality_comparison_to_assign) |
| 179 | << FixItHint::CreateReplacement(Loc, "="); |
| 180 | } |
Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 181 | |
| 182 | return true; |
Chandler Carruth | ae51ecc | 2011-08-17 08:38:04 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 185 | void Sema::DiagnoseUnusedExprResult(const Stmt *S) { |
Argyrios Kyrtzidis | 9096341 | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 186 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
| 187 | return DiagnoseUnusedExprResult(Label->getSubStmt()); |
| 188 | |
Anders Carlsson | 5c5f160 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 189 | const Expr *E = dyn_cast_or_null<Expr>(S); |
Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 190 | if (!E) |
| 191 | return; |
Aaron Ballman | 78ecb87 | 2014-10-16 20:13:28 +0000 | [diff] [blame] | 192 | |
| 193 | // If we are in an unevaluated expression context, then there can be no unused |
| 194 | // results because the results aren't expected to be used in the first place. |
| 195 | if (isUnevaluatedContext()) |
| 196 | return; |
| 197 | |
Nico Weber | 0e63163 | 2015-10-27 19:47:40 +0000 | [diff] [blame] | 198 | SourceLocation ExprLoc = E->IgnoreParenImpCasts()->getExprLoc(); |
Matt Beaumont-Gay | 1c417da | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 199 | // In most cases, we don't want to warn if the expression is written in a |
| 200 | // macro body, or if the macro comes from a system header. If the offending |
| 201 | // expression is a call to a function with the warn_unused_result attribute, |
| 202 | // we warn no matter the location. Because of the order in which the various |
| 203 | // checks need to happen, we factor out the macro-related test here. |
| 204 | bool ShouldSuppress = |
| 205 | SourceMgr.isMacroBodyExpansion(ExprLoc) || |
| 206 | SourceMgr.isInSystemMacro(ExprLoc); |
Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 207 | |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 208 | const Expr *WarnExpr; |
Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 209 | SourceLocation Loc; |
| 210 | SourceRange R1, R2; |
Matt Beaumont-Gay | 978cca9 | 2013-01-17 02:06:08 +0000 | [diff] [blame] | 211 | if (!E->isUnusedResultAWarning(WarnExpr, Loc, R1, R2, Context)) |
Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 212 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 6dc7e57 | 2012-08-31 22:39:21 +0000 | [diff] [blame] | 214 | // If this is a GNU statement expression expanded from a macro, it is probably |
| 215 | // unused because it is a function-like macro that can be used as either an |
| 216 | // expression or statement. Don't warn, because it is almost certainly a |
| 217 | // false positive. |
| 218 | if (isa<StmtExpr>(E) && Loc.isMacroID()) |
| 219 | return; |
| 220 | |
Nico Weber | 0e63163 | 2015-10-27 19:47:40 +0000 | [diff] [blame] | 221 | // Check if this is the UNREFERENCED_PARAMETER from the Microsoft headers. |
| 222 | // That macro is frequently used to suppress "unused parameter" warnings, |
| 223 | // but its implementation makes clang's -Wunused-value fire. Prevent this. |
| 224 | if (isa<ParenExpr>(E->IgnoreImpCasts()) && Loc.isMacroID()) { |
| 225 | SourceLocation SpellLoc = Loc; |
| 226 | if (findMacroSpelling(SpellLoc, "UNREFERENCED_PARAMETER")) |
| 227 | return; |
| 228 | } |
| 229 | |
Chris Lattner | 2ba5ca9 | 2009-08-16 16:57:27 +0000 | [diff] [blame] | 230 | // Okay, we have an unused result. Depending on what the base expression is, |
| 231 | // we might want to make a more specific diagnostic. Check for one of these |
| 232 | // cases now. |
| 233 | unsigned DiagID = diag::warn_unused_expr; |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 234 | if (const ExprWithCleanups *Temps = dyn_cast<ExprWithCleanups>(E)) |
Douglas Gregor | 50dc219 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 235 | E = Temps->getSubExpr(); |
Chandler Carruth | d05b352 | 2011-02-21 00:56:56 +0000 | [diff] [blame] | 236 | if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 237 | E = TempExpr->getSubExpr(); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 238 | |
Chandler Carruth | e266939 | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 239 | if (DiagnoseUnusedComparison(*this, E)) |
| 240 | return; |
| 241 | |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 242 | E = WarnExpr; |
Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 243 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { |
John McCall | c493a73 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 244 | if (E->getType()->isVoidType()) |
| 245 | return; |
| 246 | |
Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 247 | // If the callee has attribute pure, const, or warn_unused_result, warn with |
Matt Beaumont-Gay | 1c417da | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 248 | // a more specific message to make it clear what is happening. If the call |
| 249 | // is written in a macro body, only warn if it has the warn_unused_result |
| 250 | // attribute. |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 251 | if (const Decl *FD = CE->getCalleeDecl()) { |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame^] | 252 | if (const Attr *A = isa<FunctionDecl>(FD) |
| 253 | ? cast<FunctionDecl>(FD)->getUnusedResultAttr() |
| 254 | : FD->getAttr<WarnUnusedResultAttr>()) { |
| 255 | Diag(Loc, diag::warn_unused_result) << A << R1 << R2; |
Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 256 | return; |
| 257 | } |
Matt Beaumont-Gay | 1c417da | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 258 | if (ShouldSuppress) |
| 259 | return; |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 260 | if (FD->hasAttr<PureAttr>()) { |
Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 261 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; |
| 262 | return; |
| 263 | } |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 264 | if (FD->hasAttr<ConstAttr>()) { |
Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 265 | Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; |
| 266 | return; |
| 267 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 268 | } |
Matt Beaumont-Gay | 1c417da | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 269 | } else if (ShouldSuppress) |
| 270 | return; |
| 271 | |
| 272 | if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 273 | if (getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 274 | Diag(Loc, diag::err_arc_unused_init_message) << R1; |
| 275 | return; |
| 276 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 277 | const ObjCMethodDecl *MD = ME->getMethodDecl(); |
Fariborz Jahanian | ac6b4ef | 2014-07-18 22:59:10 +0000 | [diff] [blame] | 278 | if (MD) { |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame^] | 279 | if (const auto *A = MD->getAttr<WarnUnusedResultAttr>()) { |
| 280 | Diag(Loc, diag::warn_unused_result) << A << R1 << R2; |
Fariborz Jahanian | ac6b4ef | 2014-07-18 22:59:10 +0000 | [diff] [blame] | 281 | return; |
| 282 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 283 | } |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 284 | } else if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { |
| 285 | const Expr *Source = POE->getSyntacticForm(); |
| 286 | if (isa<ObjCSubscriptRefExpr>(Source)) |
| 287 | DiagID = diag::warn_unused_container_subscript_expr; |
| 288 | else |
| 289 | DiagID = diag::warn_unused_property_expr; |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 290 | } else if (const CXXFunctionalCastExpr *FC |
| 291 | = dyn_cast<CXXFunctionalCastExpr>(E)) { |
| 292 | if (isa<CXXConstructExpr>(FC->getSubExpr()) || |
| 293 | isa<CXXTemporaryObjectExpr>(FC->getSubExpr())) |
| 294 | return; |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 295 | } |
John McCall | 2351cb9 | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 296 | // Diagnose "(void*) blah" as a typo for "(void) blah". |
| 297 | else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { |
| 298 | TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); |
| 299 | QualType T = TI->getType(); |
| 300 | |
| 301 | // We really do want to use the non-canonical type here. |
| 302 | if (T == Context.VoidPtrTy) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 303 | PointerTypeLoc TL = TI->getTypeLoc().castAs<PointerTypeLoc>(); |
John McCall | 2351cb9 | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 304 | |
| 305 | Diag(Loc, diag::warn_unused_voidptr) |
| 306 | << FixItHint::CreateRemoval(TL.getStarLoc()); |
| 307 | return; |
| 308 | } |
| 309 | } |
| 310 | |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 311 | if (E->isGLValue() && E->getType().isVolatileQualified()) { |
| 312 | Diag(Loc, diag::warn_unused_volatile) << R1 << R2; |
| 313 | return; |
| 314 | } |
| 315 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 316 | DiagRuntimeBehavior(Loc, nullptr, PDiag(DiagID) << R1 << R2); |
Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 319 | void Sema::ActOnStartOfCompoundStmt() { |
| 320 | PushCompoundScope(); |
| 321 | } |
| 322 | |
| 323 | void Sema::ActOnFinishOfCompoundStmt() { |
| 324 | PopCompoundScope(); |
| 325 | } |
| 326 | |
| 327 | sema::CompoundScopeInfo &Sema::getCurCompoundScope() const { |
| 328 | return getCurFunction()->CompoundScopes.back(); |
| 329 | } |
| 330 | |
Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 331 | StmtResult Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, |
| 332 | ArrayRef<Stmt *> Elts, bool isStmtExpr) { |
| 333 | const unsigned NumElts = Elts.size(); |
| 334 | |
Chris Lattner | d864daf | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 335 | // If we're in C89 mode, check that we don't have any decls after stmts. If |
| 336 | // so, emit an extension diagnostic. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 337 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus) { |
Chris Lattner | d864daf | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 338 | // Note that __extension__ can be around a decl. |
| 339 | unsigned i = 0; |
| 340 | // Skip over all declarations. |
| 341 | for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) |
| 342 | /*empty*/; |
| 343 | |
| 344 | // We found the end of the list or a statement. Scan for another declstmt. |
| 345 | for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) |
| 346 | /*empty*/; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Chris Lattner | d864daf | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 348 | if (i != NumElts) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 349 | Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); |
Chris Lattner | d864daf | 2007-08-27 04:29:41 +0000 | [diff] [blame] | 350 | Diag(D->getLocation(), diag::ext_mixed_decls_code); |
| 351 | } |
| 352 | } |
Chris Lattner | cac27a5 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 353 | // Warn about unused expressions in statements. |
| 354 | for (unsigned i = 0; i != NumElts; ++i) { |
Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 355 | // Ignore statements that are last in a statement expression. |
| 356 | if (isStmtExpr && i == NumElts - 1) |
Chris Lattner | cac27a5 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 357 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Anders Carlsson | 59a2ab9 | 2009-07-30 22:17:18 +0000 | [diff] [blame] | 359 | DiagnoseUnusedExprResult(Elts[i]); |
Chris Lattner | cac27a5 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 360 | } |
Sebastian Redl | 52f03ba | 2008-12-21 12:04:03 +0000 | [diff] [blame] | 361 | |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 362 | // Check for suspicious empty body (null statement) in `for' and `while' |
| 363 | // statements. Don't do anything for template instantiations, this just adds |
| 364 | // noise. |
| 365 | if (NumElts != 0 && !CurrentInstantiationScope && |
| 366 | getCurCompoundScope().HasEmptyLoopBodies) { |
| 367 | for (unsigned i = 0; i != NumElts - 1; ++i) |
| 368 | DiagnoseEmptyLoopBody(Elts[i], Elts[i + 1]); |
| 369 | } |
| 370 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 371 | return new (Context) CompoundStmt(Context, Elts, L, R); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 372 | } |
| 373 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 374 | StmtResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 375 | Sema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal, |
| 376 | SourceLocation DotDotDotLoc, Expr *RHSVal, |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 377 | SourceLocation ColonLoc) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 378 | assert(LHSVal && "missing expression in case statement"); |
Sebastian Redl | 1cbb5918 | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 379 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 380 | if (getCurFunction()->SwitchStack.empty()) { |
Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 381 | Diag(CaseLoc, diag::err_case_not_in_switch); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 382 | return StmtError(); |
Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame] | 383 | } |
Chris Lattner | 35e287b | 2007-06-03 01:44:43 +0000 | [diff] [blame] | 384 | |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 385 | ExprResult LHS = |
| 386 | CorrectDelayedTyposInExpr(LHSVal, [this](class Expr *E) { |
| 387 | if (!getLangOpts().CPlusPlus11) |
| 388 | return VerifyIntegerConstantExpression(E); |
| 389 | if (Expr *CondExpr = |
| 390 | getCurFunction()->SwitchStack.back()->getCond()) { |
| 391 | QualType CondType = CondExpr->getType(); |
| 392 | llvm::APSInt TempVal; |
| 393 | return CheckConvertedConstantExpression(E, CondType, TempVal, |
| 394 | CCEK_CaseValue); |
| 395 | } |
| 396 | return ExprError(); |
| 397 | }); |
| 398 | if (LHS.isInvalid()) |
| 399 | return StmtError(); |
| 400 | LHSVal = LHS.get(); |
| 401 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 402 | if (!getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 403 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
| 404 | // However, GCC allows any evaluatable integer expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 405 | if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 406 | LHSVal = VerifyIntegerConstantExpression(LHSVal).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 407 | if (!LHSVal) |
| 408 | return StmtError(); |
| 409 | } |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 410 | |
| 411 | // GCC extension: The expression shall be an integer constant. |
| 412 | |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 413 | if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 414 | RHSVal = VerifyIntegerConstantExpression(RHSVal).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 415 | // Recover from an error by just forgetting about it. |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
Ben Langmuir | 2e13dd6 | 2013-04-29 13:07:42 +0000 | [diff] [blame] | 418 | |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 419 | LHS = ActOnFinishFullExpr(LHSVal, LHSVal->getExprLoc(), false, |
Richard Smith | 5b555da | 2014-11-20 01:24:12 +0000 | [diff] [blame] | 420 | getLangOpts().CPlusPlus11); |
| 421 | if (LHS.isInvalid()) |
| 422 | return StmtError(); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 423 | |
Richard Smith | 5b555da | 2014-11-20 01:24:12 +0000 | [diff] [blame] | 424 | auto RHS = RHSVal ? ActOnFinishFullExpr(RHSVal, RHSVal->getExprLoc(), false, |
| 425 | getLangOpts().CPlusPlus11) |
| 426 | : ExprResult(); |
| 427 | if (RHS.isInvalid()) |
| 428 | return StmtError(); |
| 429 | |
| 430 | CaseStmt *CS = new (Context) |
| 431 | CaseStmt(LHS.get(), RHS.get(), CaseLoc, DotDotDotLoc, ColonLoc); |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 432 | getCurFunction()->SwitchStack.back()->addSwitchCase(CS); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 433 | return CS; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 436 | /// ActOnCaseStmtBody - This installs a statement as the body of a case. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 437 | void Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) { |
Chandler Carruth | 2b949c2 | 2011-08-18 02:04:29 +0000 | [diff] [blame] | 438 | DiagnoseUnusedExprResult(SubStmt); |
| 439 | |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 440 | CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); |
Chris Lattner | 34a2209 | 2009-03-04 04:23:07 +0000 | [diff] [blame] | 441 | CS->setSubStmt(SubStmt); |
| 442 | } |
| 443 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 444 | StmtResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 446 | Stmt *SubStmt, Scope *CurScope) { |
Chandler Carruth | 2b949c2 | 2011-08-18 02:04:29 +0000 | [diff] [blame] | 447 | DiagnoseUnusedExprResult(SubStmt); |
| 448 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 449 | if (getCurFunction()->SwitchStack.empty()) { |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 450 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 451 | return SubStmt; |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 452 | } |
Sebastian Redl | 1cbb5918 | 2008-12-28 16:13:43 +0000 | [diff] [blame] | 453 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 454 | DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt); |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 455 | getCurFunction()->SwitchStack.back()->addSwitchCase(DS); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 456 | return DS; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 457 | } |
| 458 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 459 | StmtResult |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 460 | Sema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, |
| 461 | SourceLocation ColonLoc, Stmt *SubStmt) { |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 462 | // If the label was multiply defined, reject it now. |
| 463 | if (TheDecl->getStmt()) { |
| 464 | Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName(); |
| 465 | Diag(TheDecl->getLocation(), diag::note_previous_definition); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 466 | return SubStmt; |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 467 | } |
Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 468 | |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 469 | // Otherwise, things are good. Fill in the declaration and return it. |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 470 | LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt); |
| 471 | TheDecl->setStmt(LS); |
Abramo Bagnara | 598b943 | 2012-10-15 21:07:44 +0000 | [diff] [blame] | 472 | if (!TheDecl->isGnuLocal()) { |
| 473 | TheDecl->setLocStart(IdentLoc); |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 474 | if (!TheDecl->isMSAsmLabel()) { |
| 475 | // Don't update the location of MS ASM labels. These will result in |
| 476 | // a diagnostic, and changing the location here will mess that up. |
| 477 | TheDecl->setLocation(IdentLoc); |
| 478 | } |
Abramo Bagnara | 598b943 | 2012-10-15 21:07:44 +0000 | [diff] [blame] | 479 | } |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 480 | return LS; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 483 | StmtResult Sema::ActOnAttributedStmt(SourceLocation AttrLoc, |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 484 | ArrayRef<const Attr*> Attrs, |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 485 | Stmt *SubStmt) { |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 486 | // Fill in the declaration and return it. |
| 487 | AttributedStmt *LS = AttributedStmt::Create(Context, AttrLoc, Attrs, SubStmt); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 488 | return LS; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Richard Trieu | faca2d8 | 2016-02-18 23:58:40 +0000 | [diff] [blame] | 491 | namespace { |
| 492 | class CommaVisitor : public EvaluatedExprVisitor<CommaVisitor> { |
| 493 | typedef EvaluatedExprVisitor<CommaVisitor> Inherited; |
| 494 | Sema &SemaRef; |
| 495 | public: |
| 496 | CommaVisitor(Sema &SemaRef) : Inherited(SemaRef.Context), SemaRef(SemaRef) {} |
| 497 | void VisitBinaryOperator(BinaryOperator *E) { |
| 498 | if (E->getOpcode() == BO_Comma) |
| 499 | SemaRef.DiagnoseCommaOperator(E->getLHS(), E->getExprLoc()); |
| 500 | EvaluatedExprVisitor<CommaVisitor>::VisitBinaryOperator(E); |
| 501 | } |
| 502 | }; |
| 503 | } |
| 504 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 505 | StmtResult |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 506 | Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar, |
Argyrios Kyrtzidis | de2bdf6 | 2010-11-20 02:04:01 +0000 | [diff] [blame] | 507 | Stmt *thenStmt, SourceLocation ElseLoc, |
| 508 | Stmt *elseStmt) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 509 | ExprResult CondResult(CondVal.release()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 511 | VarDecl *ConditionVar = nullptr; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 512 | if (CondVar) { |
| 513 | ConditionVar = cast<VarDecl>(CondVar); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 514 | CondResult = CheckConditionVariable(ConditionVar, IfLoc, true); |
Meador Inge | f0af05c | 2015-06-25 22:06:40 +0000 | [diff] [blame] | 515 | CondResult = ActOnFinishFullExpr(CondResult.get(), IfLoc); |
Douglas Gregor | 633caca | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 516 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 517 | Expr *ConditionExpr = CondResult.getAs<Expr>(); |
Olivier Goffart | 122993b | 2015-10-11 17:27:29 +0000 | [diff] [blame] | 518 | if (ConditionExpr) { |
Richard Trieu | faca2d8 | 2016-02-18 23:58:40 +0000 | [diff] [blame] | 519 | |
| 520 | if (!Diags.isIgnored(diag::warn_comma_operator, |
| 521 | ConditionExpr->getExprLoc())) |
| 522 | CommaVisitor(*this).Visit(ConditionExpr); |
| 523 | |
Olivier Goffart | 122993b | 2015-10-11 17:27:29 +0000 | [diff] [blame] | 524 | DiagnoseUnusedExprResult(thenStmt); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 525 | |
Olivier Goffart | 122993b | 2015-10-11 17:27:29 +0000 | [diff] [blame] | 526 | if (!elseStmt) { |
| 527 | DiagnoseEmptyStmtBody(ConditionExpr->getLocEnd(), thenStmt, |
| 528 | diag::warn_empty_if_body); |
| 529 | } |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 530 | |
Olivier Goffart | 122993b | 2015-10-11 17:27:29 +0000 | [diff] [blame] | 531 | DiagnoseUnusedExprResult(elseStmt); |
| 532 | } else { |
| 533 | // Create a dummy Expr for the condition for error recovery |
| 534 | ConditionExpr = new (Context) OpaqueValueExpr(SourceLocation(), |
| 535 | Context.BoolTy, VK_RValue); |
Anders Carlsson | db83d77 | 2007-10-10 20:50:11 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 538 | return new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr, |
| 539 | thenStmt, ElseLoc, elseStmt); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 540 | } |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 6799845 | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 542 | namespace { |
| 543 | struct CaseCompareFunctor { |
| 544 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 545 | const llvm::APSInt &RHS) { |
| 546 | return LHS.first < RHS; |
| 547 | } |
Chris Lattner | 1463cca | 2007-09-03 18:31:57 +0000 | [diff] [blame] | 548 | bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, |
| 549 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 550 | return LHS.first < RHS.first; |
| 551 | } |
Chris Lattner | 6799845 | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 552 | bool operator()(const llvm::APSInt &LHS, |
| 553 | const std::pair<llvm::APSInt, CaseStmt*> &RHS) { |
| 554 | return LHS < RHS.first; |
| 555 | } |
| 556 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 557 | } |
Chris Lattner | 6799845 | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 4b2ff02 | 2007-09-21 18:15:22 +0000 | [diff] [blame] | 559 | /// CmpCaseVals - Comparison predicate for sorting case values. |
| 560 | /// |
| 561 | static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, |
| 562 | const std::pair<llvm::APSInt, CaseStmt*>& rhs) { |
| 563 | if (lhs.first < rhs.first) |
| 564 | return true; |
| 565 | |
| 566 | if (lhs.first == rhs.first && |
| 567 | lhs.second->getCaseLoc().getRawEncoding() |
| 568 | < rhs.second->getCaseLoc().getRawEncoding()) |
| 569 | return true; |
| 570 | return false; |
| 571 | } |
| 572 | |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 573 | /// CmpEnumVals - Comparison predicate for sorting enumeration values. |
| 574 | /// |
| 575 | static bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, |
| 576 | const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) |
| 577 | { |
| 578 | return lhs.first < rhs.first; |
| 579 | } |
| 580 | |
| 581 | /// EqEnumVals - Comparison preficate for uniqing enumeration values. |
| 582 | /// |
| 583 | static bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, |
| 584 | const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) |
| 585 | { |
| 586 | return lhs.first == rhs.first; |
| 587 | } |
| 588 | |
Chris Lattner | a96d427 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 589 | /// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of |
| 590 | /// potentially integral-promoted expression @p expr. |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 591 | static QualType GetTypeBeforeIntegralPromotion(Expr *&expr) { |
| 592 | if (ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(expr)) |
| 593 | expr = cleanups->getSubExpr(); |
| 594 | while (ImplicitCastExpr *impcast = dyn_cast<ImplicitCastExpr>(expr)) { |
| 595 | if (impcast->getCastKind() != CK_IntegralCast) break; |
| 596 | expr = impcast->getSubExpr(); |
Chris Lattner | a96d427 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 597 | } |
| 598 | return expr->getType(); |
| 599 | } |
| 600 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 601 | StmtResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 602 | Sema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 603 | Decl *CondVar) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 604 | ExprResult CondResult; |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 605 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 606 | VarDecl *ConditionVar = nullptr; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 607 | if (CondVar) { |
| 608 | ConditionVar = cast<VarDecl>(CondVar); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 609 | CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false); |
| 610 | if (CondResult.isInvalid()) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 611 | return StmtError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 612 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 613 | Cond = CondResult.get(); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 614 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 615 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 616 | if (!Cond) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 617 | return StmtError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 619 | class SwitchConvertDiagnoser : public ICEConvertDiagnoser { |
| 620 | Expr *Cond; |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 621 | |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 622 | public: |
| 623 | SwitchConvertDiagnoser(Expr *Cond) |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 624 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/true, false, true), |
| 625 | Cond(Cond) {} |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 626 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 627 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 628 | QualType T) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 629 | return S.Diag(Loc, diag::err_typecheck_statement_requires_integer) << T; |
| 630 | } |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 631 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 632 | SemaDiagnosticBuilder diagnoseIncomplete( |
| 633 | Sema &S, SourceLocation Loc, QualType T) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 634 | return S.Diag(Loc, diag::err_switch_incomplete_class_type) |
| 635 | << T << Cond->getSourceRange(); |
| 636 | } |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 637 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 638 | SemaDiagnosticBuilder diagnoseExplicitConv( |
| 639 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 640 | return S.Diag(Loc, diag::err_switch_explicit_conversion) << T << ConvTy; |
| 641 | } |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 642 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 643 | SemaDiagnosticBuilder noteExplicitConv( |
| 644 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 645 | return S.Diag(Conv->getLocation(), diag::note_switch_conversion) |
| 646 | << ConvTy->isEnumeralType() << ConvTy; |
| 647 | } |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 648 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 649 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 650 | QualType T) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 651 | return S.Diag(Loc, diag::err_switch_multiple_conversions) << T; |
| 652 | } |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 653 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 654 | SemaDiagnosticBuilder noteAmbiguous( |
| 655 | Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override { |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 656 | return S.Diag(Conv->getLocation(), diag::note_switch_conversion) |
| 657 | << ConvTy->isEnumeralType() << ConvTy; |
| 658 | } |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 659 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 660 | SemaDiagnosticBuilder diagnoseConversion( |
| 661 | Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override { |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 662 | llvm_unreachable("conversion functions are permitted"); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 663 | } |
| 664 | } SwitchDiagnoser(Cond); |
| 665 | |
Richard Smith | ccc1181 | 2013-05-21 19:05:48 +0000 | [diff] [blame] | 666 | CondResult = |
| 667 | PerformContextualImplicitConversion(SwitchLoc, Cond, SwitchDiagnoser); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 668 | if (CondResult.isInvalid()) return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 669 | Cond = CondResult.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 670 | |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 671 | // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. |
| 672 | CondResult = UsualUnaryConversions(Cond); |
| 673 | if (CondResult.isInvalid()) return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 674 | Cond = CondResult.get(); |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 675 | |
Meador Inge | f0af05c | 2015-06-25 22:06:40 +0000 | [diff] [blame] | 676 | CondResult = ActOnFinishFullExpr(Cond, SwitchLoc); |
| 677 | if (CondResult.isInvalid()) |
| 678 | return StmtError(); |
| 679 | Cond = CondResult.get(); |
John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 680 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 681 | getCurFunction()->setHasBranchIntoScope(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 682 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 683 | SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond); |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 684 | getCurFunction()->SwitchStack.push_back(SS); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 685 | return SS; |
Chris Lattner | 8fd2d01 | 2010-01-24 01:50:29 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 688 | static void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) { |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 689 | Val = Val.extOrTrunc(BitWidth); |
Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 690 | Val.setIsSigned(IsSigned); |
| 691 | } |
| 692 | |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 693 | /// Check the specified case value is in range for the given unpromoted switch |
| 694 | /// type. |
| 695 | static void checkCaseValue(Sema &S, SourceLocation Loc, const llvm::APSInt &Val, |
| 696 | unsigned UnpromotedWidth, bool UnpromotedSign) { |
| 697 | // If the case value was signed and negative and the switch expression is |
| 698 | // unsigned, don't bother to warn: this is implementation-defined behavior. |
| 699 | // FIXME: Introduce a second, default-ignored warning for this case? |
| 700 | if (UnpromotedWidth < Val.getBitWidth()) { |
| 701 | llvm::APSInt ConvVal(Val); |
| 702 | AdjustAPSInt(ConvVal, UnpromotedWidth, UnpromotedSign); |
| 703 | AdjustAPSInt(ConvVal, Val.getBitWidth(), Val.isSigned()); |
| 704 | // FIXME: Use different diagnostics for overflow in conversion to promoted |
| 705 | // type versus "switch expression cannot have this value". Use proper |
| 706 | // IntRange checking rather than just looking at the unpromoted type here. |
| 707 | if (ConvVal != Val) |
| 708 | S.Diag(Loc, diag::warn_case_value_overflow) << Val.toString(10) |
| 709 | << ConvVal.toString(10); |
| 710 | } |
| 711 | } |
| 712 | |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 713 | typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> EnumValsTy; |
| 714 | |
Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 715 | /// Returns true if we should emit a diagnostic about this case expression not |
| 716 | /// being a part of the enum used in the switch controlling expression. |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 717 | static bool ShouldDiagnoseSwitchCaseNotInEnum(const Sema &S, |
Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 718 | const EnumDecl *ED, |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 719 | const Expr *CaseExpr, |
| 720 | EnumValsTy::iterator &EI, |
| 721 | EnumValsTy::iterator &EIEnd, |
| 722 | const llvm::APSInt &Val) { |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 723 | if (const DeclRefExpr *DRE = |
| 724 | dyn_cast<DeclRefExpr>(CaseExpr->IgnoreParenImpCasts())) { |
Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 725 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 726 | QualType VarType = VD->getType(); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 727 | QualType EnumType = S.Context.getTypeDeclType(ED); |
| 728 | if (VD->hasGlobalStorage() && VarType.isConstQualified() && |
| 729 | S.Context.hasSameUnqualifiedType(EnumType, VarType)) |
Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 730 | return false; |
| 731 | } |
| 732 | } |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 733 | |
Richard Smith | 332653c | 2015-09-04 01:03:03 +0000 | [diff] [blame] | 734 | if (ED->hasAttr<FlagEnumAttr>()) { |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 735 | return !S.IsValueInFlagEnum(ED, Val, false); |
| 736 | } else { |
| 737 | while (EI != EIEnd && EI->first < Val) |
| 738 | EI++; |
| 739 | |
| 740 | if (EI != EIEnd && EI->first == Val) |
| 741 | return false; |
| 742 | } |
| 743 | |
Dmitri Gribenko | 5868375 | 2013-12-05 22:52:07 +0000 | [diff] [blame] | 744 | return true; |
| 745 | } |
| 746 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 747 | StmtResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 748 | Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, |
| 749 | Stmt *BodyStmt) { |
| 750 | SwitchStmt *SS = cast<SwitchStmt>(Switch); |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 751 | assert(SS == getCurFunction()->SwitchStack.back() && |
| 752 | "switch stack missing push/pop!"); |
Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 753 | |
David Majnemer | 418ad3f | 2014-12-15 07:46:12 +0000 | [diff] [blame] | 754 | getCurFunction()->SwitchStack.pop_back(); |
| 755 | |
Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 756 | if (!BodyStmt) return StmtError(); |
Steve Naroff | 42a350a | 2007-09-01 21:08:38 +0000 | [diff] [blame] | 757 | SS->setBody(BodyStmt, SwitchLoc); |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 758 | |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 759 | Expr *CondExpr = SS->getCond(); |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 760 | if (!CondExpr) return StmtError(); |
| 761 | |
| 762 | QualType CondType = CondExpr->getType(); |
| 763 | |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 764 | Expr *CondExprBeforePromotion = CondExpr; |
Douglas Gregor | d0c22e0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 765 | QualType CondTypeBeforePromotion = |
John McCall | 5939b16 | 2011-08-06 07:30:58 +0000 | [diff] [blame] | 766 | GetTypeBeforeIntegralPromotion(CondExprBeforePromotion); |
Douglas Gregor | d0c22e0 | 2009-11-23 13:46:08 +0000 | [diff] [blame] | 767 | |
Chris Lattner | a96d427 | 2009-10-16 16:45:22 +0000 | [diff] [blame] | 768 | // C++ 6.4.2.p2: |
| 769 | // Integral promotions are performed (on the switch condition). |
| 770 | // |
| 771 | // A case value unrepresentable by the original switch condition |
| 772 | // type (before the promotion) doesn't make sense, even when it can |
| 773 | // be represented by the promoted type. Therefore we need to find |
| 774 | // the pre-promotion type of the switch condition. |
Edward O'Callaghan | 93135aa | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 775 | if (!CondExpr->isTypeDependent()) { |
Douglas Gregor | 5823da3 | 2010-06-29 23:25:20 +0000 | [diff] [blame] | 776 | // We have already converted the expression to an integral or enumeration |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 777 | // 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] | 778 | // appropriate type now, just return an error. |
| 779 | if (!CondType->isIntegralOrEnumerationType()) |
Edward O'Callaghan | 93135aa | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 780 | return StmtError(); |
Edward O'Callaghan | 93135aa | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 781 | |
Chris Lattner | 4ebae65 | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 782 | if (CondExpr->isKnownToHaveBooleanValue()) { |
Edward O'Callaghan | 93135aa | 2009-10-17 19:32:54 +0000 | [diff] [blame] | 783 | // switch(bool_expr) {...} is often a programmer error, e.g. |
| 784 | // switch(n && mask) { ... } // Doh - should be "n & mask". |
| 785 | // One can always use an if statement instead of switch(bool_expr). |
| 786 | Diag(SwitchLoc, diag::warn_bool_switch_condition) |
| 787 | << CondExpr->getSourceRange(); |
| 788 | } |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 789 | } |
Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 790 | |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 791 | // Get the bitwidth of the switched-on value after promotions. We must |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 792 | // convert the integer case values to this width before comparison. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | bool HasDependentValue |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 794 | = CondExpr->isTypeDependent() || CondExpr->isValueDependent(); |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 795 | unsigned CondWidth = HasDependentValue ? 0 : Context.getIntWidth(CondType); |
| 796 | bool CondIsSigned = CondType->isSignedIntegerOrEnumerationType(); |
| 797 | |
| 798 | // Get the width and signedness that the condition might actually have, for |
| 799 | // warning purposes. |
| 800 | // FIXME: Grab an IntRange for the condition rather than using the unpromoted |
| 801 | // type. |
| 802 | unsigned CondWidthBeforePromotion |
Chris Lattner | abcf38a | 2011-02-24 07:31:28 +0000 | [diff] [blame] | 803 | = HasDependentValue ? 0 : Context.getIntWidth(CondTypeBeforePromotion); |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 804 | bool CondIsSignedBeforePromotion |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 805 | = CondTypeBeforePromotion->isSignedIntegerOrEnumerationType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 807 | // Accumulate all of the case values in a vector so that we can sort them |
| 808 | // and detect duplicates. This vector contains the APInt for the case after |
| 809 | // it has been converted to the condition type. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 810 | typedef SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; |
Chris Lattner | 6799845 | 2007-08-23 18:29:20 +0000 | [diff] [blame] | 811 | CaseValsTy CaseVals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 813 | // 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] | 814 | typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy; |
| 815 | CaseRangesTy CaseRanges; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 817 | DefaultStmt *TheDefaultStmt = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | |
Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 819 | bool CaseListIsErroneous = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 821 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue; |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 822 | SC = SC->getNextSwitchCase()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 824 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 825 | if (TheDefaultStmt) { |
| 826 | Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 827 | Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); |
Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 828 | |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 829 | // FIXME: Remove the default statement from the switch block so that |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 830 | // we'll return a valid AST. This requires recursing down the AST and |
| 831 | // finding it, not something we are set up to do right now. For now, |
| 832 | // just lop the entire switch stmt out of the AST. |
Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 833 | CaseListIsErroneous = true; |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 834 | } |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 835 | TheDefaultStmt = DS; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 837 | } else { |
| 838 | CaseStmt *CS = cast<CaseStmt>(SC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 840 | Expr *Lo = CS->getLHS(); |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 841 | |
| 842 | if (Lo->isTypeDependent() || Lo->isValueDependent()) { |
| 843 | HasDependentValue = true; |
| 844 | break; |
| 845 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 847 | llvm::APSInt LoVal; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 848 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 849 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 850 | // C++11 [stmt.switch]p2: the constant-expression shall be a converted |
| 851 | // constant expression of the promoted type of the switch condition. |
| 852 | ExprResult ConvLo = |
| 853 | CheckConvertedConstantExpression(Lo, CondType, LoVal, CCEK_CaseValue); |
| 854 | if (ConvLo.isInvalid()) { |
| 855 | CaseListIsErroneous = true; |
| 856 | continue; |
| 857 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 858 | Lo = ConvLo.get(); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 859 | } else { |
| 860 | // We already verified that the expression has a i-c-e value (C99 |
| 861 | // 6.8.4.2p3) - get that value now. |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 862 | LoVal = Lo->EvaluateKnownConstInt(Context); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 863 | |
| 864 | // If the LHS is not the same type as the condition, insert an implicit |
| 865 | // cast. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 866 | Lo = DefaultLvalueConversion(Lo).get(); |
| 867 | Lo = ImpCastExprToType(Lo, CondType, CK_IntegralCast).get(); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 870 | // Check the unconverted value is within the range of possible values of |
| 871 | // the switch expression. |
| 872 | checkCaseValue(*this, Lo->getLocStart(), LoVal, |
| 873 | CondWidthBeforePromotion, CondIsSignedBeforePromotion); |
| 874 | |
| 875 | // Convert the value to the same width/sign as the condition. |
| 876 | AdjustAPSInt(LoVal, CondWidth, CondIsSigned); |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 877 | |
Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 878 | CS->setLHS(Lo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | |
Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 880 | // If this is a case range, remember it in CaseRanges, otherwise CaseVals. |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 881 | if (CS->getRHS()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | if (CS->getRHS()->isTypeDependent() || |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 883 | CS->getRHS()->isValueDependent()) { |
| 884 | HasDependentValue = true; |
| 885 | break; |
| 886 | } |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 887 | CaseRanges.push_back(std::make_pair(LoVal, CS)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | } else |
Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 889 | CaseVals.push_back(std::make_pair(LoVal, CS)); |
Chris Lattner | fc1c44a | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 892 | |
| 893 | if (!HasDependentValue) { |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 894 | // If we don't have a default statement, check whether the |
| 895 | // condition is constant. |
| 896 | llvm::APSInt ConstantCondValue; |
| 897 | bool HasConstantCond = false; |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 898 | if (!HasDependentValue && !TheDefaultStmt) { |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 899 | HasConstantCond = CondExpr->EvaluateAsInt(ConstantCondValue, Context, |
| 900 | Expr::SE_AllowSideEffects); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 901 | assert(!HasConstantCond || |
| 902 | (ConstantCondValue.getBitWidth() == CondWidth && |
| 903 | ConstantCondValue.isSigned() == CondIsSigned)); |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 904 | } |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 905 | bool ShouldCheckConstantCond = HasConstantCond; |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 906 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 907 | // Sort all the scalar case values so we can easily detect duplicates. |
| 908 | std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); |
| 909 | |
| 910 | if (!CaseVals.empty()) { |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 911 | for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { |
| 912 | if (ShouldCheckConstantCond && |
| 913 | CaseVals[i].first == ConstantCondValue) |
| 914 | ShouldCheckConstantCond = false; |
| 915 | |
| 916 | if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) { |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 917 | // If we have a duplicate, report it. |
Douglas Gregor | 9841df6 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 918 | // First, determine if either case value has a name |
| 919 | StringRef PrevString, CurrString; |
| 920 | Expr *PrevCase = CaseVals[i-1].second->getLHS()->IgnoreParenCasts(); |
| 921 | Expr *CurrCase = CaseVals[i].second->getLHS()->IgnoreParenCasts(); |
| 922 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(PrevCase)) { |
| 923 | PrevString = DeclRef->getDecl()->getName(); |
| 924 | } |
| 925 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(CurrCase)) { |
| 926 | CurrString = DeclRef->getDecl()->getName(); |
| 927 | } |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 928 | SmallString<16> CaseValStr; |
Douglas Gregor | 0a9f4e0 | 2012-05-16 16:11:17 +0000 | [diff] [blame] | 929 | CaseVals[i-1].first.toString(CaseValStr); |
Douglas Gregor | 9841df6 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 930 | |
| 931 | if (PrevString == CurrString) |
| 932 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 933 | diag::err_duplicate_case) << |
Yaron Keren | 49c6369 | 2015-03-18 10:26:22 +0000 | [diff] [blame] | 934 | (PrevString.empty() ? StringRef(CaseValStr) : PrevString); |
Douglas Gregor | 9841df6 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 935 | else |
| 936 | Diag(CaseVals[i].second->getLHS()->getLocStart(), |
| 937 | diag::err_duplicate_case_differing_expr) << |
Yaron Keren | 49c6369 | 2015-03-18 10:26:22 +0000 | [diff] [blame] | 938 | (PrevString.empty() ? StringRef(CaseValStr) : PrevString) << |
| 939 | (CurrString.empty() ? StringRef(CaseValStr) : CurrString) << |
Douglas Gregor | 9841df6 | 2012-05-16 05:32:58 +0000 | [diff] [blame] | 940 | CaseValStr; |
| 941 | |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 942 | Diag(CaseVals[i-1].second->getLHS()->getLocStart(), |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 943 | diag::note_duplicate_case_prev); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 944 | // FIXME: We really want to remove the bogus case stmt from the |
| 945 | // substmt, but we have no way to do this right now. |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 946 | CaseListIsErroneous = true; |
| 947 | } |
| 948 | } |
| 949 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 951 | // Detect duplicate case ranges, which usually don't exist at all in |
| 952 | // the first place. |
| 953 | if (!CaseRanges.empty()) { |
| 954 | // Sort all the case ranges by their low value so we can easily detect |
| 955 | // overlaps between ranges. |
| 956 | std::stable_sort(CaseRanges.begin(), CaseRanges.end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 958 | // Scan the ranges, computing the high values and removing empty ranges. |
| 959 | std::vector<llvm::APSInt> HiVals; |
| 960 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 961 | llvm::APSInt &LoVal = CaseRanges[i].first; |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 962 | CaseStmt *CR = CaseRanges[i].second; |
| 963 | Expr *Hi = CR->getRHS(); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 964 | llvm::APSInt HiVal; |
| 965 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 966 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 967 | // C++11 [stmt.switch]p2: the constant-expression shall be a converted |
| 968 | // constant expression of the promoted type of the switch condition. |
| 969 | ExprResult ConvHi = |
| 970 | CheckConvertedConstantExpression(Hi, CondType, HiVal, |
| 971 | CCEK_CaseValue); |
| 972 | if (ConvHi.isInvalid()) { |
| 973 | CaseListIsErroneous = true; |
| 974 | continue; |
| 975 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 976 | Hi = ConvHi.get(); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 977 | } else { |
| 978 | HiVal = Hi->EvaluateKnownConstInt(Context); |
| 979 | |
| 980 | // If the RHS is not the same type as the condition, insert an |
| 981 | // implicit cast. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 982 | Hi = DefaultLvalueConversion(Hi).get(); |
| 983 | Hi = ImpCastExprToType(Hi, CondType, CK_IntegralCast).get(); |
Richard Smith | f8379a0 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 984 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 986 | // Check the unconverted value is within the range of possible values of |
| 987 | // the switch expression. |
| 988 | checkCaseValue(*this, Hi->getLocStart(), HiVal, |
| 989 | CondWidthBeforePromotion, CondIsSignedBeforePromotion); |
| 990 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 991 | // Convert the value to the same width/sign as the condition. |
Richard Smith | 077d083 | 2014-08-04 00:40:48 +0000 | [diff] [blame] | 992 | AdjustAPSInt(HiVal, CondWidth, CondIsSigned); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 994 | CR->setRHS(Hi); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 996 | // 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] | 997 | if (LoVal > HiVal) { |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 998 | Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) |
| 999 | << SourceRange(CR->getLHS()->getLocStart(), |
Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 1000 | Hi->getLocEnd()); |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1001 | CaseRanges.erase(CaseRanges.begin()+i); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 1002 | --i; |
| 1003 | --e; |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1004 | continue; |
| 1005 | } |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1006 | |
| 1007 | if (ShouldCheckConstantCond && |
| 1008 | LoVal <= ConstantCondValue && |
| 1009 | ConstantCondValue <= HiVal) |
| 1010 | ShouldCheckConstantCond = false; |
| 1011 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1012 | HiVals.push_back(HiVal); |
| 1013 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1015 | // Rescan the ranges, looking for overlap with singleton values and other |
| 1016 | // ranges. Since the range list is sorted, we only need to compare case |
| 1017 | // ranges with their neighbors. |
| 1018 | for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { |
| 1019 | llvm::APSInt &CRLo = CaseRanges[i].first; |
| 1020 | llvm::APSInt &CRHi = HiVals[i]; |
| 1021 | CaseStmt *CR = CaseRanges[i].second; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1023 | // Check to see whether the case range overlaps with any |
| 1024 | // singleton cases. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1025 | CaseStmt *OverlapStmt = nullptr; |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1026 | llvm::APSInt OverlapVal(32); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1028 | // Find the smallest value >= the lower bound. If I is in the |
| 1029 | // case range, then we have overlap. |
| 1030 | CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), |
| 1031 | CaseVals.end(), CRLo, |
| 1032 | CaseCompareFunctor()); |
| 1033 | if (I != CaseVals.end() && I->first < CRHi) { |
| 1034 | OverlapVal = I->first; // Found overlap with scalar. |
| 1035 | OverlapStmt = I->second; |
| 1036 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1038 | // Find the smallest value bigger than the upper bound. |
| 1039 | I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); |
| 1040 | if (I != CaseVals.begin() && (I-1)->first >= CRLo) { |
| 1041 | OverlapVal = (I-1)->first; // Found overlap with scalar. |
| 1042 | OverlapStmt = (I-1)->second; |
| 1043 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1045 | // Check to see if this case stmt overlaps with the subsequent |
| 1046 | // case range. |
| 1047 | if (i && CRLo <= HiVals[i-1]) { |
| 1048 | OverlapVal = HiVals[i-1]; // Found overlap with range. |
| 1049 | OverlapStmt = CaseRanges[i-1].second; |
| 1050 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1052 | if (OverlapStmt) { |
| 1053 | // If we have a duplicate, report it. |
| 1054 | Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) |
| 1055 | << OverlapVal.toString(10); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | Diag(OverlapStmt->getLHS()->getLocStart(), |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1057 | diag::note_duplicate_case_prev); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1058 | // FIXME: We really want to remove the bogus case stmt from the |
| 1059 | // substmt, but we have no way to do this right now. |
Douglas Gregor | 2a2d00f | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 1060 | CaseListIsErroneous = true; |
| 1061 | } |
Chris Lattner | fcb920d | 2007-08-23 14:29:07 +0000 | [diff] [blame] | 1062 | } |
Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1063 | } |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1064 | |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1065 | // Complain if we have a constant condition and we didn't find a match. |
| 1066 | if (!CaseListIsErroneous && ShouldCheckConstantCond) { |
| 1067 | // TODO: it would be nice if we printed enums as enums, chars as |
| 1068 | // chars, etc. |
| 1069 | Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition) |
| 1070 | << ConstantCondValue.toString(10) |
| 1071 | << CondExpr->getSourceRange(); |
| 1072 | } |
| 1073 | |
| 1074 | // 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] | 1075 | // values. We only issue a warning if there is not 'default:', but |
| 1076 | // we still do the analysis to preserve this information in the AST |
| 1077 | // (which can be used by flow-based analyes). |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1078 | // |
Chris Lattner | 5167908 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1079 | const EnumType *ET = CondTypeBeforePromotion->getAs<EnumType>(); |
Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1080 | |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1081 | // If switch has default case, then ignore it. |
Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1082 | if (!CaseListIsErroneous && !HasConstantCond && ET) { |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1083 | const EnumDecl *ED = ET->getDecl(); |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1084 | EnumValsTy EnumVals; |
| 1085 | |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1086 | // Gather all enum values, set their type and sort them, |
| 1087 | // allowing easier comparison with CaseVals. |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1088 | for (auto *EDI : ED->enumerators()) { |
Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 1089 | llvm::APSInt Val = EDI->getInitVal(); |
| 1090 | AdjustAPSInt(Val, CondWidth, CondIsSigned); |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1091 | EnumVals.push_back(std::make_pair(Val, EDI)); |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1092 | } |
| 1093 | std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1094 | auto EI = EnumVals.begin(), EIEnd = |
John McCall | d3dfbd6 | 2010-05-18 03:19:21 +0000 | [diff] [blame] | 1095 | std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); |
Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1096 | |
| 1097 | // See which case values aren't in enum. |
David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1098 | for (CaseValsTy::const_iterator CI = CaseVals.begin(); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1099 | CI != CaseVals.end(); CI++) { |
| 1100 | Expr *CaseExpr = CI->second->getLHS(); |
| 1101 | if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd, |
| 1102 | CI->first)) |
| 1103 | Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum) |
| 1104 | << CondTypeBeforePromotion; |
David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1105 | } |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1106 | |
David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1107 | // See which of case ranges aren't in enum |
| 1108 | EI = EnumVals.begin(); |
| 1109 | for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1110 | RI != CaseRanges.end(); RI++) { |
| 1111 | Expr *CaseExpr = RI->second->getLHS(); |
| 1112 | if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd, |
| 1113 | RI->first)) |
| 1114 | Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum) |
| 1115 | << CondTypeBeforePromotion; |
David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1116 | |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1117 | llvm::APSInt Hi = |
David Blaikie | e476f97 | 2012-01-22 02:31:55 +0000 | [diff] [blame] | 1118 | RI->second->getRHS()->EvaluateKnownConstInt(Context); |
| 1119 | AdjustAPSInt(Hi, CondWidth, CondIsSigned); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1120 | |
| 1121 | CaseExpr = RI->second->getRHS(); |
| 1122 | if (ShouldDiagnoseSwitchCaseNotInEnum(*this, ED, CaseExpr, EI, EIEnd, |
| 1123 | Hi)) |
| 1124 | Diag(CaseExpr->getExprLoc(), diag::warn_not_in_enum) |
| 1125 | << CondTypeBeforePromotion; |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1126 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1127 | |
Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1128 | // Check which enum vals aren't in switch |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1129 | auto CI = CaseVals.begin(); |
| 1130 | auto RI = CaseRanges.begin(); |
Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1131 | bool hasCasesNotInSwitch = false; |
| 1132 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1133 | SmallVector<DeclarationName,8> UnhandledNames; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1134 | |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1135 | for (EI = EnumVals.begin(); EI != EIEnd; EI++){ |
Chris Lattner | 5167908 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1136 | // Drop unneeded case values |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1137 | while (CI != CaseVals.end() && CI->first < EI->first) |
| 1138 | CI++; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1140 | if (CI != CaseVals.end() && CI->first == EI->first) |
| 1141 | continue; |
| 1142 | |
Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1143 | // Drop unneeded case ranges |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1144 | for (; RI != CaseRanges.end(); RI++) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1145 | llvm::APSInt Hi = |
| 1146 | RI->second->getRHS()->EvaluateKnownConstInt(Context); |
Gabor Greif | 16e0286 | 2010-10-01 22:05:14 +0000 | [diff] [blame] | 1147 | AdjustAPSInt(Hi, CondWidth, CondIsSigned); |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1148 | if (EI->first <= Hi) |
| 1149 | break; |
| 1150 | } |
| 1151 | |
Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1152 | if (RI == CaseRanges.end() || EI->first < RI->first) { |
Ted Kremenek | 02627a2 | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1153 | hasCasesNotInSwitch = true; |
David Blaikie | 645ae0c | 2012-01-21 18:12:07 +0000 | [diff] [blame] | 1154 | UnhandledNames.push_back(EI->second->getDeclName()); |
Ted Kremenek | 02627a2 | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1155 | } |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1156 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1157 | |
David Blaikie | 60ac638 | 2012-01-23 04:46:12 +0000 | [diff] [blame] | 1158 | if (TheDefaultStmt && UnhandledNames.empty()) |
| 1159 | Diag(TheDefaultStmt->getDefaultLoc(), diag::warn_unreachable_default); |
David Blaikie | 645ae0c | 2012-01-21 18:12:07 +0000 | [diff] [blame] | 1160 | |
Chris Lattner | 5167908 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1161 | // Produce a nice diagnostic if multiple values aren't handled. |
Benjamin Kramer | 3a8650a | 2015-03-27 17:23:14 +0000 | [diff] [blame] | 1162 | if (!UnhandledNames.empty()) { |
| 1163 | DiagnosticBuilder DB = Diag(CondExpr->getExprLoc(), |
| 1164 | TheDefaultStmt ? diag::warn_def_missing_case |
| 1165 | : diag::warn_missing_case) |
| 1166 | << (int)UnhandledNames.size(); |
| 1167 | |
| 1168 | for (size_t I = 0, E = std::min(UnhandledNames.size(), (size_t)3); |
| 1169 | I != E; ++I) |
| 1170 | DB << UnhandledNames[I]; |
Chris Lattner | 5167908 | 2010-09-16 17:09:42 +0000 | [diff] [blame] | 1171 | } |
Ted Kremenek | c42f345 | 2010-09-09 00:05:53 +0000 | [diff] [blame] | 1172 | |
| 1173 | if (!hasCasesNotInSwitch) |
Ted Kremenek | 02627a2 | 2010-09-09 06:53:59 +0000 | [diff] [blame] | 1174 | SS->setAllEnumCasesCovered(); |
Douglas Gregor | bd683973 | 2010-02-08 22:24:16 +0000 | [diff] [blame] | 1175 | } |
Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1176 | } |
Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1177 | |
Serge Pavlov | 921c2ba | 2014-05-21 14:48:43 +0000 | [diff] [blame] | 1178 | if (BodyStmt) |
| 1179 | DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), BodyStmt, |
| 1180 | diag::warn_empty_switch_body); |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1181 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1182 | // FIXME: If the case list was broken is some way, we don't have a good system |
| 1183 | // to patch it up. Instead, just return the whole substmt as broken. |
Chris Lattner | 10cb5e5 | 2007-08-23 06:23:56 +0000 | [diff] [blame] | 1184 | if (CaseListIsErroneous) |
Sebastian Redl | 6a8002e | 2009-01-11 00:38:46 +0000 | [diff] [blame] | 1185 | return StmtError(); |
| 1186 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1187 | return SS; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1190 | void |
| 1191 | Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType, |
| 1192 | Expr *SrcExpr) { |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1193 | if (Diags.isIgnored(diag::warn_not_in_enum_assignment, SrcExpr->getExprLoc())) |
Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1194 | return; |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1195 | |
Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1196 | if (const EnumType *ET = DstType->getAs<EnumType>()) |
Dmitri Gribenko | e6ac50a | 2013-12-05 23:06:53 +0000 | [diff] [blame] | 1197 | if (!Context.hasSameUnqualifiedType(SrcType, DstType) && |
Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1198 | SrcType->isIntegerType()) { |
| 1199 | if (!SrcExpr->isTypeDependent() && !SrcExpr->isValueDependent() && |
| 1200 | SrcExpr->isIntegerConstantExpr(Context)) { |
| 1201 | // Get the bitwidth of the enum value before promotions. |
Joey Gouly | 1ba2733 | 2013-06-06 13:48:00 +0000 | [diff] [blame] | 1202 | unsigned DstWidth = Context.getIntWidth(DstType); |
Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1203 | bool DstIsSigned = DstType->isSignedIntegerOrEnumerationType(); |
| 1204 | |
| 1205 | llvm::APSInt RhsVal = SrcExpr->EvaluateKnownConstInt(Context); |
Joey Gouly | 1ba2733 | 2013-06-06 13:48:00 +0000 | [diff] [blame] | 1206 | AdjustAPSInt(RhsVal, DstWidth, DstIsSigned); |
Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1207 | const EnumDecl *ED = ET->getDecl(); |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1208 | |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1209 | if (ED->hasAttr<FlagEnumAttr>()) { |
| 1210 | if (!IsValueInFlagEnum(ED, RhsVal, true)) |
| 1211 | Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignment) |
Dmitri Gribenko | e6ac50a | 2013-12-05 23:06:53 +0000 | [diff] [blame] | 1212 | << DstType.getUnqualifiedType(); |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 1213 | } else { |
| 1214 | typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl *>, 64> |
| 1215 | EnumValsTy; |
| 1216 | EnumValsTy EnumVals; |
| 1217 | |
| 1218 | // Gather all enum values, set their type and sort them, |
| 1219 | // allowing easier comparison with rhs constant. |
| 1220 | for (auto *EDI : ED->enumerators()) { |
| 1221 | llvm::APSInt Val = EDI->getInitVal(); |
| 1222 | AdjustAPSInt(Val, DstWidth, DstIsSigned); |
| 1223 | EnumVals.push_back(std::make_pair(Val, EDI)); |
| 1224 | } |
| 1225 | if (EnumVals.empty()) |
| 1226 | return; |
| 1227 | std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); |
| 1228 | EnumValsTy::iterator EIend = |
| 1229 | std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); |
| 1230 | |
| 1231 | // See which values aren't in the enum. |
| 1232 | EnumValsTy::const_iterator EI = EnumVals.begin(); |
| 1233 | while (EI != EIend && EI->first < RhsVal) |
| 1234 | EI++; |
| 1235 | if (EI == EIend || EI->first != RhsVal) { |
| 1236 | Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignment) |
| 1237 | << DstType.getUnqualifiedType(); |
| 1238 | } |
Fariborz Jahanian | 268fec1 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1244 | StmtResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1245 | Sema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1246 | Decl *CondVar, Stmt *Body) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1247 | ExprResult CondResult(Cond.release()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1248 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1249 | VarDecl *ConditionVar = nullptr; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1250 | if (CondVar) { |
| 1251 | ConditionVar = cast<VarDecl>(CondVar); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1252 | CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true); |
Meador Inge | f0af05c | 2015-06-25 22:06:40 +0000 | [diff] [blame] | 1253 | CondResult = ActOnFinishFullExpr(CondResult.get(), WhileLoc); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1254 | if (CondResult.isInvalid()) |
| 1255 | return StmtError(); |
Douglas Gregor | 680f861 | 2009-11-24 21:15:44 +0000 | [diff] [blame] | 1256 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1257 | Expr *ConditionExpr = CondResult.get(); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1258 | if (!ConditionExpr) |
| 1259 | return StmtError(); |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1260 | CheckBreakContinueBinding(ConditionExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1261 | |
Richard Trieu | faca2d8 | 2016-02-18 23:58:40 +0000 | [diff] [blame] | 1262 | if (ConditionExpr && |
| 1263 | !Diags.isIgnored(diag::warn_comma_operator, ConditionExpr->getExprLoc())) |
| 1264 | CommaVisitor(*this).Visit(ConditionExpr); |
| 1265 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1266 | DiagnoseUnusedExprResult(Body); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1268 | if (isa<NullStmt>(Body)) |
| 1269 | getCurCompoundScope().setHasEmptyLoopBodies(); |
| 1270 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1271 | return new (Context) |
| 1272 | WhileStmt(Context, ConditionVar, ConditionExpr, Body, WhileLoc); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1275 | StmtResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1276 | Sema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, |
Chris Lattner | 815b70e | 2009-06-12 23:04:47 +0000 | [diff] [blame] | 1277 | SourceLocation WhileLoc, SourceLocation CondLParen, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1278 | Expr *Cond, SourceLocation CondRParen) { |
| 1279 | assert(Cond && "ActOnDoStmt(): missing expression"); |
Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1280 | |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1281 | CheckBreakContinueBinding(Cond); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1282 | ExprResult CondResult = CheckBooleanCondition(Cond, DoLoc); |
Dmitri Gribenko | d4bc5ac | 2012-11-18 22:28:42 +0000 | [diff] [blame] | 1283 | if (CondResult.isInvalid()) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 1284 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1285 | Cond = CondResult.get(); |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 1286 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1287 | CondResult = ActOnFinishFullExpr(Cond, DoLoc); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1288 | if (CondResult.isInvalid()) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1289 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1290 | Cond = CondResult.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1291 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1292 | DiagnoseUnusedExprResult(Body); |
Anders Carlsson | 5c5f160 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 1293 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1294 | return new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1297 | namespace { |
| 1298 | // This visitor will traverse a conditional statement and store all |
| 1299 | // the evaluated decls into a vector. Simple is set to true if none |
| 1300 | // of the excluded constructs are used. |
| 1301 | class DeclExtractor : public EvaluatedExprVisitor<DeclExtractor> { |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 1302 | llvm::SmallPtrSetImpl<VarDecl*> &Decls; |
Craig Topper | fa159c1 | 2013-07-14 16:47:36 +0000 | [diff] [blame] | 1303 | SmallVectorImpl<SourceRange> &Ranges; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1304 | bool Simple; |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1305 | public: |
| 1306 | typedef EvaluatedExprVisitor<DeclExtractor> Inherited; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1307 | |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 1308 | DeclExtractor(Sema &S, llvm::SmallPtrSetImpl<VarDecl*> &Decls, |
Craig Topper | fa159c1 | 2013-07-14 16:47:36 +0000 | [diff] [blame] | 1309 | SmallVectorImpl<SourceRange> &Ranges) : |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1310 | Inherited(S.Context), |
| 1311 | Decls(Decls), |
| 1312 | Ranges(Ranges), |
| 1313 | Simple(true) {} |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1314 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1315 | bool isSimple() { return Simple; } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1316 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1317 | // Replaces the method in EvaluatedExprVisitor. |
| 1318 | void VisitMemberExpr(MemberExpr* E) { |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1319 | Simple = false; |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | // Any Stmt not whitelisted will cause the condition to be marked complex. |
| 1323 | void VisitStmt(Stmt *S) { |
| 1324 | Simple = false; |
| 1325 | } |
| 1326 | |
| 1327 | void VisitBinaryOperator(BinaryOperator *E) { |
| 1328 | Visit(E->getLHS()); |
| 1329 | Visit(E->getRHS()); |
| 1330 | } |
| 1331 | |
| 1332 | void VisitCastExpr(CastExpr *E) { |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1333 | Visit(E->getSubExpr()); |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1334 | } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1335 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1336 | void VisitUnaryOperator(UnaryOperator *E) { |
| 1337 | // Skip checking conditionals with derefernces. |
| 1338 | if (E->getOpcode() == UO_Deref) |
| 1339 | Simple = false; |
| 1340 | else |
| 1341 | Visit(E->getSubExpr()); |
| 1342 | } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1343 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1344 | void VisitConditionalOperator(ConditionalOperator *E) { |
| 1345 | Visit(E->getCond()); |
| 1346 | Visit(E->getTrueExpr()); |
| 1347 | Visit(E->getFalseExpr()); |
| 1348 | } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1349 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1350 | void VisitParenExpr(ParenExpr *E) { |
| 1351 | Visit(E->getSubExpr()); |
| 1352 | } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1353 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1354 | void VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { |
| 1355 | Visit(E->getOpaqueValue()->getSourceExpr()); |
| 1356 | Visit(E->getFalseExpr()); |
| 1357 | } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1358 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1359 | void VisitIntegerLiteral(IntegerLiteral *E) { } |
| 1360 | void VisitFloatingLiteral(FloatingLiteral *E) { } |
| 1361 | void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { } |
| 1362 | void VisitCharacterLiteral(CharacterLiteral *E) { } |
| 1363 | void VisitGNUNullExpr(GNUNullExpr *E) { } |
| 1364 | void VisitImaginaryLiteral(ImaginaryLiteral *E) { } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1365 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1366 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 1367 | VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); |
| 1368 | if (!VD) return; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1369 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1370 | Ranges.push_back(E->getSourceRange()); |
| 1371 | |
| 1372 | Decls.insert(VD); |
| 1373 | } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1374 | |
| 1375 | }; // end class DeclExtractor |
| 1376 | |
Sanjay Patel | 69e7f6e | 2015-08-28 14:42:54 +0000 | [diff] [blame] | 1377 | // DeclMatcher checks to see if the decls are used in a non-evaluated |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1378 | // context. |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1379 | class DeclMatcher : public EvaluatedExprVisitor<DeclMatcher> { |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 1380 | llvm::SmallPtrSetImpl<VarDecl*> &Decls; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1381 | bool FoundDecl; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1382 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1383 | public: |
| 1384 | typedef EvaluatedExprVisitor<DeclMatcher> Inherited; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1385 | |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 1386 | DeclMatcher(Sema &S, llvm::SmallPtrSetImpl<VarDecl*> &Decls, |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1387 | Stmt *Statement) : |
| 1388 | Inherited(S.Context), Decls(Decls), FoundDecl(false) { |
| 1389 | if (!Statement) return; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1390 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1391 | Visit(Statement); |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1394 | void VisitReturnStmt(ReturnStmt *S) { |
| 1395 | FoundDecl = true; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1398 | void VisitBreakStmt(BreakStmt *S) { |
| 1399 | FoundDecl = true; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1402 | void VisitGotoStmt(GotoStmt *S) { |
| 1403 | FoundDecl = true; |
| 1404 | } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1405 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1406 | void VisitCastExpr(CastExpr *E) { |
| 1407 | if (E->getCastKind() == CK_LValueToRValue) |
| 1408 | CheckLValueToRValueCast(E->getSubExpr()); |
| 1409 | else |
| 1410 | Visit(E->getSubExpr()); |
| 1411 | } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1412 | |
Richard Trieu | 9d22880 | 2013-05-31 22:46:45 +0000 | [diff] [blame] | 1413 | void CheckLValueToRValueCast(Expr *E) { |
| 1414 | E = E->IgnoreParenImpCasts(); |
| 1415 | |
| 1416 | if (isa<DeclRefExpr>(E)) { |
| 1417 | return; |
| 1418 | } |
| 1419 | |
| 1420 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 1421 | Visit(CO->getCond()); |
| 1422 | CheckLValueToRValueCast(CO->getTrueExpr()); |
| 1423 | CheckLValueToRValueCast(CO->getFalseExpr()); |
| 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | if (BinaryConditionalOperator *BCO = |
| 1428 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 1429 | CheckLValueToRValueCast(BCO->getOpaqueValue()->getSourceExpr()); |
| 1430 | CheckLValueToRValueCast(BCO->getFalseExpr()); |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | Visit(E); |
| 1435 | } |
| 1436 | |
| 1437 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 1438 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
| 1439 | if (Decls.count(VD)) |
| 1440 | FoundDecl = true; |
| 1441 | } |
| 1442 | |
| 1443 | bool FoundDeclInUse() { return FoundDecl; } |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1444 | |
| 1445 | }; // end class DeclMatcher |
| 1446 | |
| 1447 | void CheckForLoopConditionalStatement(Sema &S, Expr *Second, |
| 1448 | Expr *Third, Stmt *Body) { |
| 1449 | // Condition is empty |
| 1450 | if (!Second) return; |
| 1451 | |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1452 | if (S.Diags.isIgnored(diag::warn_variables_not_in_loop_body, |
| 1453 | Second->getLocStart())) |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1454 | return; |
| 1455 | |
| 1456 | PartialDiagnostic PDiag = S.PDiag(diag::warn_variables_not_in_loop_body); |
| 1457 | llvm::SmallPtrSet<VarDecl*, 8> Decls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1458 | SmallVector<SourceRange, 10> Ranges; |
Benjamin Kramer | d1d76b2 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 1459 | DeclExtractor DE(S, Decls, Ranges); |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1460 | DE.Visit(Second); |
| 1461 | |
| 1462 | // Don't analyze complex conditionals. |
| 1463 | if (!DE.isSimple()) return; |
| 1464 | |
| 1465 | // No decls found. |
| 1466 | if (Decls.size() == 0) return; |
| 1467 | |
Richard Trieu | 0030f1d | 2012-05-04 03:01:54 +0000 | [diff] [blame] | 1468 | // Don't warn on volatile, static, or global variables. |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 1469 | for (llvm::SmallPtrSetImpl<VarDecl*>::iterator I = Decls.begin(), |
| 1470 | E = Decls.end(); |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1471 | I != E; ++I) |
Richard Trieu | 0030f1d | 2012-05-04 03:01:54 +0000 | [diff] [blame] | 1472 | if ((*I)->getType().isVolatileQualified() || |
| 1473 | (*I)->hasGlobalStorage()) return; |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1474 | |
| 1475 | if (DeclMatcher(S, Decls, Second).FoundDeclInUse() || |
| 1476 | DeclMatcher(S, Decls, Third).FoundDeclInUse() || |
| 1477 | DeclMatcher(S, Decls, Body).FoundDeclInUse()) |
| 1478 | return; |
| 1479 | |
| 1480 | // Load decl names into diagnostic. |
| 1481 | if (Decls.size() > 4) |
| 1482 | PDiag << 0; |
| 1483 | else { |
| 1484 | PDiag << Decls.size(); |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 1485 | for (llvm::SmallPtrSetImpl<VarDecl*>::iterator I = Decls.begin(), |
| 1486 | E = Decls.end(); |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1487 | I != E; ++I) |
| 1488 | PDiag << (*I)->getDeclName(); |
| 1489 | } |
| 1490 | |
| 1491 | // Load SourceRanges into diagnostic if there is room. |
| 1492 | // Otherwise, load the SourceRange of the conditional expression. |
| 1493 | if (Ranges.size() <= PartialDiagnostic::MaxArguments) |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 1494 | for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(), |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1495 | E = Ranges.end(); |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1496 | I != E; ++I) |
| 1497 | PDiag << *I; |
| 1498 | else |
| 1499 | PDiag << Second->getSourceRange(); |
| 1500 | |
| 1501 | S.Diag(Ranges.begin()->getBegin(), PDiag); |
| 1502 | } |
| 1503 | |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1504 | // If Statement is an incemement or decrement, return true and sets the |
| 1505 | // variables Increment and DRE. |
| 1506 | bool ProcessIterationStmt(Sema &S, Stmt* Statement, bool &Increment, |
| 1507 | DeclRefExpr *&DRE) { |
| 1508 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(Statement)) { |
| 1509 | switch (UO->getOpcode()) { |
| 1510 | default: return false; |
| 1511 | case UO_PostInc: |
| 1512 | case UO_PreInc: |
| 1513 | Increment = true; |
| 1514 | break; |
| 1515 | case UO_PostDec: |
| 1516 | case UO_PreDec: |
| 1517 | Increment = false; |
| 1518 | break; |
| 1519 | } |
| 1520 | DRE = dyn_cast<DeclRefExpr>(UO->getSubExpr()); |
| 1521 | return DRE; |
| 1522 | } |
| 1523 | |
| 1524 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(Statement)) { |
| 1525 | FunctionDecl *FD = Call->getDirectCallee(); |
| 1526 | if (!FD || !FD->isOverloadedOperator()) return false; |
| 1527 | switch (FD->getOverloadedOperator()) { |
| 1528 | default: return false; |
| 1529 | case OO_PlusPlus: |
| 1530 | Increment = true; |
| 1531 | break; |
| 1532 | case OO_MinusMinus: |
| 1533 | Increment = false; |
| 1534 | break; |
| 1535 | } |
| 1536 | DRE = dyn_cast<DeclRefExpr>(Call->getArg(0)); |
| 1537 | return DRE; |
| 1538 | } |
| 1539 | |
| 1540 | return false; |
| 1541 | } |
| 1542 | |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1543 | // A visitor to determine if a continue or break statement is a |
| 1544 | // subexpression. |
| 1545 | class BreakContinueFinder : public EvaluatedExprVisitor<BreakContinueFinder> { |
| 1546 | SourceLocation BreakLoc; |
| 1547 | SourceLocation ContinueLoc; |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1548 | public: |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1549 | BreakContinueFinder(Sema &S, Stmt* Body) : |
| 1550 | Inherited(S.Context) { |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1551 | Visit(Body); |
| 1552 | } |
| 1553 | |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1554 | typedef EvaluatedExprVisitor<BreakContinueFinder> Inherited; |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1555 | |
| 1556 | void VisitContinueStmt(ContinueStmt* E) { |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1557 | ContinueLoc = E->getContinueLoc(); |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1560 | void VisitBreakStmt(BreakStmt* E) { |
| 1561 | BreakLoc = E->getBreakLoc(); |
| 1562 | } |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1563 | |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1564 | bool ContinueFound() { return ContinueLoc.isValid(); } |
| 1565 | bool BreakFound() { return BreakLoc.isValid(); } |
| 1566 | SourceLocation GetContinueLoc() { return ContinueLoc; } |
| 1567 | SourceLocation GetBreakLoc() { return BreakLoc; } |
| 1568 | |
| 1569 | }; // end class BreakContinueFinder |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1570 | |
| 1571 | // Emit a warning when a loop increment/decrement appears twice per loop |
| 1572 | // iteration. The conditions which trigger this warning are: |
| 1573 | // 1) The last statement in the loop body and the third expression in the |
| 1574 | // for loop are both increment or both decrement of the same variable |
| 1575 | // 2) No continue statements in the loop body. |
| 1576 | void CheckForRedundantIteration(Sema &S, Expr *Third, Stmt *Body) { |
| 1577 | // Return when there is nothing to check. |
| 1578 | if (!Body || !Third) return; |
| 1579 | |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1580 | if (S.Diags.isIgnored(diag::warn_redundant_loop_iteration, |
| 1581 | Third->getLocStart())) |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1582 | return; |
| 1583 | |
| 1584 | // Get the last statement from the loop body. |
| 1585 | CompoundStmt *CS = dyn_cast<CompoundStmt>(Body); |
| 1586 | if (!CS || CS->body_empty()) return; |
| 1587 | Stmt *LastStmt = CS->body_back(); |
| 1588 | if (!LastStmt) return; |
| 1589 | |
| 1590 | bool LoopIncrement, LastIncrement; |
| 1591 | DeclRefExpr *LoopDRE, *LastDRE; |
| 1592 | |
| 1593 | if (!ProcessIterationStmt(S, Third, LoopIncrement, LoopDRE)) return; |
| 1594 | if (!ProcessIterationStmt(S, LastStmt, LastIncrement, LastDRE)) return; |
| 1595 | |
| 1596 | // Check that the two statements are both increments or both decrements |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1597 | // on the same variable. |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1598 | if (LoopIncrement != LastIncrement || |
| 1599 | LoopDRE->getDecl() != LastDRE->getDecl()) return; |
| 1600 | |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1601 | if (BreakContinueFinder(S, Body).ContinueFound()) return; |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1602 | |
| 1603 | S.Diag(LastDRE->getLocation(), diag::warn_redundant_loop_iteration) |
| 1604 | << LastDRE->getDecl() << LastIncrement; |
| 1605 | S.Diag(LoopDRE->getLocation(), diag::note_loop_iteration_here) |
| 1606 | << LoopIncrement; |
| 1607 | } |
| 1608 | |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1609 | } // end namespace |
| 1610 | |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1611 | |
| 1612 | void Sema::CheckBreakContinueBinding(Expr *E) { |
| 1613 | if (!E || getLangOpts().CPlusPlus) |
| 1614 | return; |
| 1615 | BreakContinueFinder BCFinder(*this, E); |
| 1616 | Scope *BreakParent = CurScope->getBreakParent(); |
| 1617 | if (BCFinder.BreakFound() && BreakParent) { |
| 1618 | if (BreakParent->getFlags() & Scope::SwitchScope) { |
| 1619 | Diag(BCFinder.GetBreakLoc(), diag::warn_break_binds_to_switch); |
| 1620 | } else { |
| 1621 | Diag(BCFinder.GetBreakLoc(), diag::warn_loop_ctrl_binds_to_inner) |
| 1622 | << "break"; |
| 1623 | } |
| 1624 | } else if (BCFinder.ContinueFound() && CurScope->getContinueParent()) { |
| 1625 | Diag(BCFinder.GetContinueLoc(), diag::warn_loop_ctrl_binds_to_inner) |
| 1626 | << "continue"; |
| 1627 | } |
| 1628 | } |
| 1629 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1630 | StmtResult |
Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1631 | Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1632 | Stmt *First, FullExprArg second, Decl *secondVar, |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1633 | FullExprArg third, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1634 | SourceLocation RParenLoc, Stmt *Body) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1635 | if (!getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1636 | if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1637 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 1638 | // declare identifiers for objects having storage class 'auto' or |
| 1639 | // 'register'. |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 1640 | for (auto *DI : DS->decls()) { |
| 1641 | VarDecl *VD = dyn_cast<VarDecl>(DI); |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1642 | if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1643 | VD = nullptr; |
| 1644 | if (!VD) { |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 1645 | Diag(DI->getLocation(), diag::err_non_local_variable_decl_in_for); |
| 1646 | DI->setInvalidDecl(); |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1647 | } |
Argyrios Kyrtzidis | 7620ee4 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1648 | } |
Chris Lattner | 39f920f | 2007-08-28 05:03:08 +0000 | [diff] [blame] | 1649 | } |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 1650 | } |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1651 | |
Serge Pavlov | 09f9924 | 2014-01-23 15:05:00 +0000 | [diff] [blame] | 1652 | CheckBreakContinueBinding(second.get()); |
| 1653 | CheckBreakContinueBinding(third.get()); |
| 1654 | |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1655 | CheckForLoopConditionalStatement(*this, second.get(), third.get(), Body); |
Richard Trieu | 4e7c962 | 2013-08-06 21:31:54 +0000 | [diff] [blame] | 1656 | CheckForRedundantIteration(*this, third.get(), Body); |
Richard Trieu | 451a5db | 2012-04-30 18:01:30 +0000 | [diff] [blame] | 1657 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1658 | ExprResult SecondResult(second.release()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1659 | VarDecl *ConditionVar = nullptr; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1660 | if (secondVar) { |
| 1661 | ConditionVar = cast<VarDecl>(secondVar); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1662 | SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true); |
Meador Inge | f0af05c | 2015-06-25 22:06:40 +0000 | [diff] [blame] | 1663 | SecondResult = ActOnFinishFullExpr(SecondResult.get(), ForLoc); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1664 | if (SecondResult.isInvalid()) |
| 1665 | return StmtError(); |
| 1666 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1667 | |
Richard Trieu | faca2d8 | 2016-02-18 23:58:40 +0000 | [diff] [blame] | 1668 | if (SecondResult.get() && |
| 1669 | !Diags.isIgnored(diag::warn_comma_operator, |
| 1670 | SecondResult.get()->getExprLoc())) |
| 1671 | CommaVisitor(*this).Visit(SecondResult.get()); |
| 1672 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1673 | Expr *Third = third.release().getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1674 | |
Anders Carlsson | 1682af5 | 2009-08-01 01:39:59 +0000 | [diff] [blame] | 1675 | DiagnoseUnusedExprResult(First); |
| 1676 | DiagnoseUnusedExprResult(Third); |
Anders Carlsson | 5c5f160 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 1677 | DiagnoseUnusedExprResult(Body); |
| 1678 | |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 1679 | if (isa<NullStmt>(Body)) |
| 1680 | getCurCompoundScope().setHasEmptyLoopBodies(); |
| 1681 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1682 | return new (Context) ForStmt(Context, First, SecondResult.get(), ConditionVar, |
| 1683 | Third, Body, ForLoc, LParenLoc, RParenLoc); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1686 | /// In an Objective C collection iteration statement: |
| 1687 | /// for (x in y) |
| 1688 | /// x can be an arbitrary l-value expression. Bind it up as a |
| 1689 | /// full-expression. |
| 1690 | StmtResult Sema::ActOnForEachLValueExpr(Expr *E) { |
John McCall | bc15335 | 2012-03-30 05:43:39 +0000 | [diff] [blame] | 1691 | // Reduce placeholder expressions here. Note that this rejects the |
| 1692 | // use of pseudo-object l-values in this position. |
| 1693 | ExprResult result = CheckPlaceholderExpr(E); |
| 1694 | if (result.isInvalid()) return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1695 | E = result.get(); |
John McCall | bc15335 | 2012-03-30 05:43:39 +0000 | [diff] [blame] | 1696 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1697 | ExprResult FullExpr = ActOnFinishFullExpr(E); |
| 1698 | if (FullExpr.isInvalid()) |
| 1699 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1700 | return StmtResult(static_cast<Stmt*>(FullExpr.get())); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1703 | ExprResult |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1704 | Sema::CheckObjCForCollectionOperand(SourceLocation forLoc, Expr *collection) { |
| 1705 | if (!collection) |
| 1706 | return ExprError(); |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1707 | |
Kaelyn Takata | 1586782 | 2014-11-21 18:48:04 +0000 | [diff] [blame] | 1708 | ExprResult result = CorrectDelayedTyposInExpr(collection); |
| 1709 | if (!result.isUsable()) |
| 1710 | return ExprError(); |
| 1711 | collection = result.get(); |
| 1712 | |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1713 | // Bail out early if we've got a type-dependent expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1714 | if (collection->isTypeDependent()) return collection; |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1715 | |
| 1716 | // Perform normal l-value conversion. |
Kaelyn Takata | 1586782 | 2014-11-21 18:48:04 +0000 | [diff] [blame] | 1717 | result = DefaultFunctionArrayLvalueConversion(collection); |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1718 | if (result.isInvalid()) |
| 1719 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1720 | collection = result.get(); |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1721 | |
| 1722 | // The operand needs to have object-pointer type. |
| 1723 | // TODO: should we do a contextual conversion? |
| 1724 | const ObjCObjectPointerType *pointerType = |
| 1725 | collection->getType()->getAs<ObjCObjectPointerType>(); |
| 1726 | if (!pointerType) |
| 1727 | return Diag(forLoc, diag::err_collection_expr_type) |
| 1728 | << collection->getType() << collection->getSourceRange(); |
| 1729 | |
| 1730 | // Check that the operand provides |
| 1731 | // - countByEnumeratingWithState:objects:count: |
| 1732 | const ObjCObjectType *objectType = pointerType->getObjectType(); |
| 1733 | ObjCInterfaceDecl *iface = objectType->getInterface(); |
| 1734 | |
| 1735 | // 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] | 1736 | // 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] | 1737 | if (iface && |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 1738 | (getLangOpts().ObjCAutoRefCount |
| 1739 | ? RequireCompleteType(forLoc, QualType(objectType, 0), |
| 1740 | diag::err_arc_collection_forward, collection) |
| 1741 | : !isCompleteType(forLoc, QualType(objectType, 0)))) { |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1742 | // Otherwise, if we have any useful type information, check that |
| 1743 | // the type declares the appropriate method. |
| 1744 | } else if (iface || !objectType->qual_empty()) { |
| 1745 | IdentifierInfo *selectorIdents[] = { |
| 1746 | &Context.Idents.get("countByEnumeratingWithState"), |
| 1747 | &Context.Idents.get("objects"), |
| 1748 | &Context.Idents.get("count") |
| 1749 | }; |
| 1750 | Selector selector = Context.Selectors.getSelector(3, &selectorIdents[0]); |
| 1751 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1752 | ObjCMethodDecl *method = nullptr; |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1753 | |
| 1754 | // If there's an interface, look in both the public and private APIs. |
| 1755 | if (iface) { |
| 1756 | method = iface->lookupInstanceMethod(selector); |
Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 1757 | if (!method) method = iface->lookupPrivateMethod(selector); |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | // Also check protocol qualifiers. |
| 1761 | if (!method) |
| 1762 | method = LookupMethodInQualifiedType(selector, pointerType, |
| 1763 | /*instance*/ true); |
| 1764 | |
| 1765 | // If we didn't find it anywhere, give up. |
| 1766 | if (!method) { |
| 1767 | Diag(forLoc, diag::warn_collection_expr_type) |
| 1768 | << collection->getType() << selector << collection->getSourceRange(); |
| 1769 | } |
| 1770 | |
| 1771 | // TODO: check for an incompatible signature? |
| 1772 | } |
| 1773 | |
| 1774 | // Wrap up any cleanups in the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1775 | return collection; |
John McCall | 5384823 | 2011-07-27 01:07:15 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1778 | StmtResult |
Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1779 | Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1780 | Stmt *First, Expr *collection, |
| 1781 | SourceLocation RParenLoc) { |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1782 | |
| 1783 | ExprResult CollectionExprResult = |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1784 | CheckObjCForCollectionOperand(ForLoc, collection); |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1785 | |
Fariborz Jahanian | 9397767 | 2008-01-10 20:33:58 +0000 | [diff] [blame] | 1786 | if (First) { |
| 1787 | QualType FirstType; |
| 1788 | if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { |
Chris Lattner | 529efc7 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1789 | if (!DS->isSingleDecl()) |
Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1790 | return StmtError(Diag((*DS->decl_begin())->getLocation(), |
| 1791 | diag::err_toomany_element_decls)); |
| 1792 | |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1793 | VarDecl *D = dyn_cast<VarDecl>(DS->getSingleDecl()); |
| 1794 | if (!D || D->isInvalidDecl()) |
| 1795 | return StmtError(); |
| 1796 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1797 | FirstType = D->getType(); |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1798 | // C99 6.8.5p3: The declaration part of a 'for' statement shall only |
| 1799 | // declare identifiers for objects having storage class 'auto' or |
| 1800 | // 'register'. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1801 | if (!D->hasLocalStorage()) |
| 1802 | return StmtError(Diag(D->getLocation(), |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1803 | diag::err_non_local_variable_decl_in_for)); |
Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1804 | |
| 1805 | // If the type contained 'auto', deduce the 'auto' to 'id'. |
| 1806 | if (FirstType->getContainedAutoType()) { |
Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1807 | OpaqueValueExpr OpaqueId(D->getLocation(), Context.getObjCIdType(), |
| 1808 | VK_RValue); |
| 1809 | Expr *DeducedInit = &OpaqueId; |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1810 | if (DeduceAutoType(D->getTypeSourceInfo(), DeducedInit, FirstType) == |
| 1811 | DAR_Failed) |
Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1812 | DiagnoseAutoDeductionFailure(D, DeducedInit); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1813 | if (FirstType.isNull()) { |
Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1814 | D->setInvalidDecl(); |
| 1815 | return StmtError(); |
| 1816 | } |
| 1817 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1818 | D->setType(FirstType); |
Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1819 | |
| 1820 | if (ActiveTemplateInstantiations.empty()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1821 | SourceLocation Loc = |
| 1822 | D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
Douglas Gregor | c430f45 | 2013-04-08 18:25:02 +0000 | [diff] [blame] | 1823 | Diag(Loc, diag::warn_auto_var_is_id) |
| 1824 | << D->getDeclName(); |
| 1825 | } |
| 1826 | } |
| 1827 | |
Anders Carlsson | 1ec2ccd | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 1828 | } else { |
Douglas Gregor | f68a508 | 2010-04-22 23:10:45 +0000 | [diff] [blame] | 1829 | Expr *FirstE = cast<Expr>(First); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1830 | if (!FirstE->isTypeDependent() && !FirstE->isLValue()) |
Sebastian Redl | fbfaafc | 2009-01-16 23:28:06 +0000 | [diff] [blame] | 1831 | return StmtError(Diag(First->getLocStart(), |
| 1832 | diag::err_selector_element_not_lvalue) |
| 1833 | << First->getSourceRange()); |
| 1834 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | FirstType = static_cast<Expr*>(First)->getType(); |
Fariborz Jahanian | 8bcf182 | 2013-10-10 21:58:04 +0000 | [diff] [blame] | 1836 | if (FirstType.isConstQualified()) |
| 1837 | Diag(ForLoc, diag::err_selector_element_const_type) |
| 1838 | << FirstType << First->getSourceRange(); |
Anders Carlsson | 1ec2ccd | 2008-08-25 18:16:36 +0000 | [diff] [blame] | 1839 | } |
Douglas Gregor | f68a508 | 2010-04-22 23:10:45 +0000 | [diff] [blame] | 1840 | if (!FirstType->isDependentType() && |
| 1841 | !FirstType->isObjCObjectPointerType() && |
Fariborz Jahanian | 2e4a46b | 2009-08-14 21:53:27 +0000 | [diff] [blame] | 1842 | !FirstType->isBlockPointerType()) |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1843 | return StmtError(Diag(ForLoc, diag::err_selector_element_type) |
| 1844 | << FirstType << First->getSourceRange()); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1845 | } |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1846 | |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 1847 | if (CollectionExprResult.isInvalid()) |
| 1848 | return StmtError(); |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1849 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1850 | CollectionExprResult = ActOnFinishFullExpr(CollectionExprResult.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1851 | if (CollectionExprResult.isInvalid()) |
| 1852 | return StmtError(); |
| 1853 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1854 | return new (Context) ObjCForCollectionStmt(First, CollectionExprResult.get(), |
| 1855 | nullptr, ForLoc, RParenLoc); |
Fariborz Jahanian | 732b8c2 | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 1856 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1857 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1858 | /// Finish building a variable declaration for a for-range statement. |
| 1859 | /// \return true if an error occurs. |
| 1860 | static bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init, |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1861 | SourceLocation Loc, int DiagID) { |
Kaelyn Takata | fb8cf40 | 2015-05-07 00:11:02 +0000 | [diff] [blame] | 1862 | if (Decl->getType()->isUndeducedType()) { |
| 1863 | ExprResult Res = SemaRef.CorrectDelayedTyposInExpr(Init); |
| 1864 | if (!Res.isUsable()) { |
| 1865 | Decl->setInvalidDecl(); |
| 1866 | return true; |
| 1867 | } |
| 1868 | Init = Res.get(); |
| 1869 | } |
| 1870 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1871 | // Deduce the type for the iterator variable now rather than leaving it to |
| 1872 | // AddInitializerToDecl, so we can produce a more suitable diagnostic. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1873 | QualType InitType; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 1874 | if ((!isa<InitListExpr>(Init) && Init->getType()->isVoidType()) || |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1875 | SemaRef.DeduceAutoType(Decl->getTypeSourceInfo(), Init, InitType) == |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 1876 | Sema::DAR_Failed) |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1877 | SemaRef.Diag(Loc, DiagID) << Init->getType(); |
| 1878 | if (InitType.isNull()) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1879 | Decl->setInvalidDecl(); |
| 1880 | return true; |
| 1881 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 1882 | Decl->setType(InitType); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1883 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1884 | // In ARC, infer lifetime. |
| 1885 | // FIXME: ARC may want to turn this into 'const __unsafe_unretained' if |
| 1886 | // we're doing the equivalent of fast iteration. |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1887 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1888 | SemaRef.inferObjCARCLifetime(Decl)) |
| 1889 | Decl->setInvalidDecl(); |
| 1890 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1891 | SemaRef.AddInitializerToDecl(Decl, Init, /*DirectInit=*/false, |
| 1892 | /*TypeMayContainAuto=*/false); |
| 1893 | SemaRef.FinalizeDeclaration(Decl); |
Richard Smith | 0c502d2 | 2011-04-18 15:49:25 +0000 | [diff] [blame] | 1894 | SemaRef.CurContext->addHiddenDecl(Decl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1895 | return false; |
| 1896 | } |
| 1897 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 1898 | namespace { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1899 | // An enum to represent whether something is dealing with a call to begin() |
| 1900 | // or a call to end() in a range-based for loop. |
| 1901 | enum BeginEndFunction { |
| 1902 | BEF_begin, |
| 1903 | BEF_end |
| 1904 | }; |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 1905 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1906 | /// Produce a note indicating which begin/end function was implicitly called |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 1907 | /// 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] | 1908 | /// nor from the diagnostics produced when analysing the implicit expressions |
| 1909 | /// required in a for-range statement. |
| 1910 | void NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E, |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1911 | BeginEndFunction BEF) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1912 | CallExpr *CE = dyn_cast<CallExpr>(E); |
| 1913 | if (!CE) |
| 1914 | return; |
| 1915 | FunctionDecl *D = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); |
| 1916 | if (!D) |
| 1917 | return; |
| 1918 | SourceLocation Loc = D->getLocation(); |
| 1919 | |
| 1920 | std::string Description; |
| 1921 | bool IsTemplate = false; |
| 1922 | if (FunctionTemplateDecl *FunTmpl = D->getPrimaryTemplate()) { |
| 1923 | Description = SemaRef.getTemplateArgumentBindingsText( |
| 1924 | FunTmpl->getTemplateParameters(), *D->getTemplateSpecializationArgs()); |
| 1925 | IsTemplate = true; |
| 1926 | } |
| 1927 | |
| 1928 | SemaRef.Diag(Loc, diag::note_for_range_begin_end) |
| 1929 | << BEF << IsTemplate << Description << E->getType(); |
| 1930 | } |
| 1931 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 1932 | /// Build a variable declaration for a for-range statement. |
| 1933 | VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, |
| 1934 | QualType Type, const char *Name) { |
| 1935 | DeclContext *DC = SemaRef.CurContext; |
| 1936 | IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); |
| 1937 | TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); |
| 1938 | VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1939 | TInfo, SC_None); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 1940 | Decl->setImplicit(); |
| 1941 | return Decl; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1944 | } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1945 | |
Fariborz Jahanian | 0021347 | 2012-07-06 19:04:04 +0000 | [diff] [blame] | 1946 | static bool ObjCEnumerationCollection(Expr *Collection) { |
| 1947 | return !Collection->isTypeDependent() |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1948 | && Collection->getType()->getAs<ObjCObjectPointerType>() != nullptr; |
Fariborz Jahanian | 0021347 | 2012-07-06 19:04:04 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 1951 | /// ActOnCXXForRangeStmt - Check and build a C++11 for-range statement. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1952 | /// |
Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 1953 | /// C++11 [stmt.ranged]: |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1954 | /// A range-based for statement is equivalent to |
| 1955 | /// |
| 1956 | /// { |
| 1957 | /// auto && __range = range-init; |
| 1958 | /// for ( auto __begin = begin-expr, |
| 1959 | /// __end = end-expr; |
| 1960 | /// __begin != __end; |
| 1961 | /// ++__begin ) { |
| 1962 | /// for-range-declaration = *__begin; |
| 1963 | /// statement |
| 1964 | /// } |
| 1965 | /// } |
| 1966 | /// |
| 1967 | /// The body of the loop is not available yet, since it cannot be analysed until |
| 1968 | /// we have determined the type of the for-range-declaration. |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1969 | StmtResult Sema::ActOnCXXForRangeStmt(Scope *S, SourceLocation ForLoc, |
| 1970 | SourceLocation CoawaitLoc, Stmt *First, |
| 1971 | SourceLocation ColonLoc, Expr *Range, |
| 1972 | SourceLocation RParenLoc, |
| 1973 | BuildForRangeKind Kind) { |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 1974 | if (!First) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1975 | return StmtError(); |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 1976 | |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 1977 | if (Range && ObjCEnumerationCollection(Range)) |
Sam Panzer | 2c4ca0f | 2012-08-16 21:47:25 +0000 | [diff] [blame] | 1978 | return ActOnObjCForCollectionStmt(ForLoc, First, Range, RParenLoc); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1979 | |
| 1980 | DeclStmt *DS = dyn_cast<DeclStmt>(First); |
| 1981 | assert(DS && "first part of for range not a decl stmt"); |
| 1982 | |
| 1983 | if (!DS->isSingleDecl()) { |
| 1984 | Diag(DS->getStartLoc(), diag::err_type_defined_in_for_range); |
| 1985 | return StmtError(); |
| 1986 | } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1987 | |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 1988 | Decl *LoopVar = DS->getSingleDecl(); |
| 1989 | if (LoopVar->isInvalidDecl() || !Range || |
| 1990 | DiagnoseUnexpandedParameterPack(Range, UPPC_Expression)) { |
| 1991 | LoopVar->setInvalidDecl(); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1992 | return StmtError(); |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 1993 | } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1994 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1995 | // Coroutines: 'for co_await' implicitly co_awaits its range. |
| 1996 | if (CoawaitLoc.isValid()) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1997 | ExprResult Coawait = ActOnCoawaitExpr(S, CoawaitLoc, Range); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1998 | if (Coawait.isInvalid()) return StmtError(); |
| 1999 | Range = Coawait.get(); |
| 2000 | } |
| 2001 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2002 | // Build auto && __range = range-init |
| 2003 | SourceLocation RangeLoc = Range->getLocStart(); |
| 2004 | VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc, |
| 2005 | Context.getAutoRRefDeductType(), |
| 2006 | "__range"); |
| 2007 | if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc, |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2008 | diag::err_for_range_deduction_failure)) { |
| 2009 | LoopVar->setInvalidDecl(); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2010 | return StmtError(); |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2011 | } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2012 | |
| 2013 | // Claim the type doesn't contain auto: we've already done the checking. |
| 2014 | DeclGroupPtrTy RangeGroup = |
Craig Topper | e3d2ecbe | 2014-06-28 23:22:33 +0000 | [diff] [blame] | 2015 | BuildDeclaratorGroup(MutableArrayRef<Decl *>((Decl **)&RangeVar, 1), |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2016 | /*TypeMayContainAuto=*/ false); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2017 | StmtResult RangeDecl = ActOnDeclStmt(RangeGroup, RangeLoc, RangeLoc); |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2018 | if (RangeDecl.isInvalid()) { |
| 2019 | LoopVar->setInvalidDecl(); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2020 | return StmtError(); |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2021 | } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2022 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2023 | return BuildCXXForRangeStmt(ForLoc, CoawaitLoc, ColonLoc, RangeDecl.get(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2024 | /*BeginEndDecl=*/nullptr, /*Cond=*/nullptr, |
| 2025 | /*Inc=*/nullptr, DS, RParenLoc, Kind); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | /// \brief Create the initialization, compare, and increment steps for |
| 2029 | /// the range-based for loop expression. |
| 2030 | /// This function does not handle array-based for loops, |
| 2031 | /// which are created in Sema::BuildCXXForRangeStmt. |
| 2032 | /// |
| 2033 | /// \returns a ForRangeStatus indicating success or what kind of error occurred. |
| 2034 | /// BeginExpr and EndExpr are set and FRS_Success is returned on success; |
| 2035 | /// CandidateSet and BEF are set and some non-success value is returned on |
| 2036 | /// failure. |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2037 | static Sema::ForRangeStatus BuildNonArrayForRange(Sema &SemaRef, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2038 | Expr *BeginRange, Expr *EndRange, |
| 2039 | QualType RangeType, |
| 2040 | VarDecl *BeginVar, |
| 2041 | VarDecl *EndVar, |
| 2042 | SourceLocation ColonLoc, |
| 2043 | OverloadCandidateSet *CandidateSet, |
| 2044 | ExprResult *BeginExpr, |
| 2045 | ExprResult *EndExpr, |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2046 | BeginEndFunction *BEF) { |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2047 | DeclarationNameInfo BeginNameInfo( |
| 2048 | &SemaRef.PP.getIdentifierTable().get("begin"), ColonLoc); |
| 2049 | DeclarationNameInfo EndNameInfo(&SemaRef.PP.getIdentifierTable().get("end"), |
| 2050 | ColonLoc); |
| 2051 | |
| 2052 | LookupResult BeginMemberLookup(SemaRef, BeginNameInfo, |
| 2053 | Sema::LookupMemberName); |
| 2054 | LookupResult EndMemberLookup(SemaRef, EndNameInfo, Sema::LookupMemberName); |
| 2055 | |
| 2056 | if (CXXRecordDecl *D = RangeType->getAsCXXRecordDecl()) { |
| 2057 | // - if _RangeT is a class type, the unqualified-ids begin and end are |
| 2058 | // looked up in the scope of class _RangeT as if by class member access |
| 2059 | // lookup (3.4.5), and if either (or both) finds at least one |
| 2060 | // declaration, begin-expr and end-expr are __range.begin() and |
| 2061 | // __range.end(), respectively; |
| 2062 | SemaRef.LookupQualifiedName(BeginMemberLookup, D); |
| 2063 | SemaRef.LookupQualifiedName(EndMemberLookup, D); |
| 2064 | |
| 2065 | if (BeginMemberLookup.empty() != EndMemberLookup.empty()) { |
| 2066 | SourceLocation RangeLoc = BeginVar->getLocation(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2067 | *BEF = BeginMemberLookup.empty() ? BEF_end : BEF_begin; |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2068 | |
| 2069 | SemaRef.Diag(RangeLoc, diag::err_for_range_member_begin_end_mismatch) |
| 2070 | << RangeLoc << BeginRange->getType() << *BEF; |
| 2071 | return Sema::FRS_DiagnosticIssued; |
| 2072 | } |
| 2073 | } else { |
| 2074 | // - otherwise, begin-expr and end-expr are begin(__range) and |
| 2075 | // end(__range), respectively, where begin and end are looked up with |
| 2076 | // argument-dependent lookup (3.4.2). For the purposes of this name |
| 2077 | // lookup, namespace std is an associated namespace. |
| 2078 | |
| 2079 | } |
| 2080 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2081 | *BEF = BEF_begin; |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2082 | Sema::ForRangeStatus RangeStatus = |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2083 | SemaRef.BuildForRangeBeginEndCall(ColonLoc, ColonLoc, BeginNameInfo, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2084 | BeginMemberLookup, CandidateSet, |
| 2085 | BeginRange, BeginExpr); |
| 2086 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2087 | if (RangeStatus != Sema::FRS_Success) { |
| 2088 | if (RangeStatus == Sema::FRS_DiagnosticIssued) |
| 2089 | SemaRef.Diag(BeginRange->getLocStart(), diag::note_in_for_range) |
| 2090 | << ColonLoc << BEF_begin << BeginRange->getType(); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2091 | return RangeStatus; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2092 | } |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2093 | if (FinishForRangeVarDecl(SemaRef, BeginVar, BeginExpr->get(), ColonLoc, |
| 2094 | diag::err_for_range_iter_deduction_failure)) { |
| 2095 | NoteForRangeBeginEndFunction(SemaRef, BeginExpr->get(), *BEF); |
| 2096 | return Sema::FRS_DiagnosticIssued; |
| 2097 | } |
| 2098 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2099 | *BEF = BEF_end; |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2100 | RangeStatus = |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2101 | SemaRef.BuildForRangeBeginEndCall(ColonLoc, ColonLoc, EndNameInfo, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2102 | EndMemberLookup, CandidateSet, |
| 2103 | EndRange, EndExpr); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2104 | if (RangeStatus != Sema::FRS_Success) { |
| 2105 | if (RangeStatus == Sema::FRS_DiagnosticIssued) |
| 2106 | SemaRef.Diag(EndRange->getLocStart(), diag::note_in_for_range) |
| 2107 | << ColonLoc << BEF_end << EndRange->getType(); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2108 | return RangeStatus; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2109 | } |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2110 | if (FinishForRangeVarDecl(SemaRef, EndVar, EndExpr->get(), ColonLoc, |
| 2111 | diag::err_for_range_iter_deduction_failure)) { |
| 2112 | NoteForRangeBeginEndFunction(SemaRef, EndExpr->get(), *BEF); |
| 2113 | return Sema::FRS_DiagnosticIssued; |
| 2114 | } |
| 2115 | return Sema::FRS_Success; |
| 2116 | } |
| 2117 | |
| 2118 | /// Speculatively attempt to dereference an invalid range expression. |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2119 | /// If the attempt fails, this function will return a valid, null StmtResult |
| 2120 | /// and emit no diagnostics. |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2121 | static StmtResult RebuildForRangeWithDereference(Sema &SemaRef, Scope *S, |
| 2122 | SourceLocation ForLoc, |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2123 | SourceLocation CoawaitLoc, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2124 | Stmt *LoopVarDecl, |
| 2125 | SourceLocation ColonLoc, |
| 2126 | Expr *Range, |
| 2127 | SourceLocation RangeLoc, |
| 2128 | SourceLocation RParenLoc) { |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2129 | // Determine whether we can rebuild the for-range statement with a |
| 2130 | // dereferenced range expression. |
| 2131 | ExprResult AdjustedRange; |
| 2132 | { |
| 2133 | Sema::SFINAETrap Trap(SemaRef); |
| 2134 | |
| 2135 | AdjustedRange = SemaRef.BuildUnaryOp(S, RangeLoc, UO_Deref, Range); |
| 2136 | if (AdjustedRange.isInvalid()) |
| 2137 | return StmtResult(); |
| 2138 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2139 | StmtResult SR = SemaRef.ActOnCXXForRangeStmt( |
| 2140 | S, ForLoc, CoawaitLoc, LoopVarDecl, ColonLoc, AdjustedRange.get(), |
| 2141 | RParenLoc, Sema::BFRK_Check); |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2142 | if (SR.isInvalid()) |
| 2143 | return StmtResult(); |
| 2144 | } |
| 2145 | |
| 2146 | // The attempt to dereference worked well enough that it could produce a valid |
| 2147 | // loop. Produce a fixit, and rebuild the loop with diagnostics enabled, in |
| 2148 | // case there are any other (non-fatal) problems with it. |
| 2149 | SemaRef.Diag(RangeLoc, diag::err_for_range_dereference) |
| 2150 | << Range->getType() << FixItHint::CreateInsertion(RangeLoc, "*"); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2151 | return SemaRef.ActOnCXXForRangeStmt(S, ForLoc, CoawaitLoc, LoopVarDecl, |
| 2152 | ColonLoc, AdjustedRange.get(), RParenLoc, |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2153 | Sema::BFRK_Rebuild); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2156 | namespace { |
| 2157 | /// RAII object to automatically invalidate a declaration if an error occurs. |
| 2158 | struct InvalidateOnErrorScope { |
| 2159 | InvalidateOnErrorScope(Sema &SemaRef, Decl *D, bool Enabled) |
| 2160 | : Trap(SemaRef.Diags), D(D), Enabled(Enabled) {} |
| 2161 | ~InvalidateOnErrorScope() { |
| 2162 | if (Enabled && Trap.hasErrorOccurred()) |
| 2163 | D->setInvalidDecl(); |
| 2164 | } |
| 2165 | |
| 2166 | DiagnosticErrorTrap Trap; |
| 2167 | Decl *D; |
| 2168 | bool Enabled; |
| 2169 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2170 | } |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2171 | |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2172 | /// BuildCXXForRangeStmt - Build or instantiate a C++11 for-range statement. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2173 | StmtResult |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2174 | Sema::BuildCXXForRangeStmt(SourceLocation ForLoc, SourceLocation CoawaitLoc, |
| 2175 | SourceLocation ColonLoc, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2176 | Stmt *RangeDecl, Stmt *BeginEnd, Expr *Cond, |
| 2177 | Expr *Inc, Stmt *LoopVarDecl, |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2178 | SourceLocation RParenLoc, BuildForRangeKind Kind) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2179 | // FIXME: This should not be used during template instantiation. We should |
| 2180 | // pick up the set of unqualified lookup results for the != and + operators |
| 2181 | // in the initial parse. |
| 2182 | // |
| 2183 | // Testcase (accepts-invalid): |
| 2184 | // template<typename T> void f() { for (auto x : T()) {} } |
| 2185 | // namespace N { struct X { X begin(); X end(); int operator*(); }; } |
| 2186 | // bool operator!=(N::X, N::X); void operator++(N::X); |
| 2187 | // void g() { f<N::X>(); } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2188 | Scope *S = getCurScope(); |
| 2189 | |
| 2190 | DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl); |
| 2191 | VarDecl *RangeVar = cast<VarDecl>(RangeDS->getSingleDecl()); |
| 2192 | QualType RangeVarType = RangeVar->getType(); |
| 2193 | |
| 2194 | DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl); |
| 2195 | VarDecl *LoopVar = cast<VarDecl>(LoopVarDS->getSingleDecl()); |
| 2196 | |
Richard Smith | 3249fed | 2013-08-21 01:40:36 +0000 | [diff] [blame] | 2197 | // If we hit any errors, mark the loop variable as invalid if its type |
| 2198 | // contains 'auto'. |
| 2199 | InvalidateOnErrorScope Invalidate(*this, LoopVar, |
| 2200 | LoopVar->getType()->isUndeducedType()); |
| 2201 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2202 | StmtResult BeginEndDecl = BeginEnd; |
| 2203 | ExprResult NotEqExpr = Cond, IncrExpr = Inc; |
| 2204 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2205 | if (RangeVarType->isDependentType()) { |
| 2206 | // The range is implicitly used as a placeholder when it is dependent. |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 2207 | RangeVar->markUsed(Context); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Deduce any 'auto's in the loop variable as 'DependentTy'. We'll fill |
| 2210 | // them in properly when we instantiate the loop. |
| 2211 | if (!LoopVar->isInvalidDecl() && Kind != BFRK_Check) |
| 2212 | LoopVar->setType(SubstAutoType(LoopVar->getType(), Context.DependentTy)); |
| 2213 | } else if (!BeginEndDecl.get()) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2214 | SourceLocation RangeLoc = RangeVar->getLocation(); |
| 2215 | |
Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2216 | const QualType RangeVarNonRefType = RangeVarType.getNonReferenceType(); |
| 2217 | |
| 2218 | ExprResult BeginRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, |
| 2219 | VK_LValue, ColonLoc); |
| 2220 | if (BeginRangeRef.isInvalid()) |
| 2221 | return StmtError(); |
| 2222 | |
| 2223 | ExprResult EndRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, |
| 2224 | VK_LValue, ColonLoc); |
| 2225 | if (EndRangeRef.isInvalid()) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2226 | return StmtError(); |
| 2227 | |
| 2228 | QualType AutoType = Context.getAutoDeductType(); |
| 2229 | Expr *Range = RangeVar->getInit(); |
| 2230 | if (!Range) |
| 2231 | return StmtError(); |
| 2232 | QualType RangeType = Range->getType(); |
| 2233 | |
| 2234 | if (RequireCompleteType(RangeLoc, RangeType, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2235 | diag::err_for_range_incomplete_type)) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2236 | return StmtError(); |
| 2237 | |
| 2238 | // Build auto __begin = begin-expr, __end = end-expr. |
| 2239 | VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, |
| 2240 | "__begin"); |
| 2241 | VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, |
| 2242 | "__end"); |
| 2243 | |
| 2244 | // Build begin-expr and end-expr and attach to __begin and __end variables. |
| 2245 | ExprResult BeginExpr, EndExpr; |
| 2246 | if (const ArrayType *UnqAT = RangeType->getAsArrayTypeUnsafe()) { |
| 2247 | // - if _RangeT is an array type, begin-expr and end-expr are __range and |
| 2248 | // __range + __bound, respectively, where __bound is the array bound. If |
| 2249 | // _RangeT is an array of unknown size or an array of incomplete type, |
| 2250 | // the program is ill-formed; |
| 2251 | |
| 2252 | // begin-expr is __range. |
Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2253 | BeginExpr = BeginRangeRef; |
| 2254 | if (FinishForRangeVarDecl(*this, BeginVar, BeginRangeRef.get(), ColonLoc, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2255 | diag::err_for_range_iter_deduction_failure)) { |
| 2256 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2257 | return StmtError(); |
| 2258 | } |
| 2259 | |
| 2260 | // Find the array bound. |
| 2261 | ExprResult BoundExpr; |
| 2262 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(UnqAT)) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2263 | BoundExpr = IntegerLiteral::Create( |
| 2264 | Context, CAT->getSize(), Context.getPointerDiffType(), RangeLoc); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2265 | else if (const VariableArrayType *VAT = |
| 2266 | dyn_cast<VariableArrayType>(UnqAT)) |
| 2267 | BoundExpr = VAT->getSizeExpr(); |
| 2268 | else { |
| 2269 | // Can't be a DependentSizedArrayType or an IncompleteArrayType since |
| 2270 | // UnqAT is not incomplete and Range is not type-dependent. |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2271 | llvm_unreachable("Unexpected array type in for-range"); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | // end-expr is __range + __bound. |
Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2275 | EndExpr = ActOnBinOp(S, ColonLoc, tok::plus, EndRangeRef.get(), |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2276 | BoundExpr.get()); |
| 2277 | if (EndExpr.isInvalid()) |
| 2278 | return StmtError(); |
| 2279 | if (FinishForRangeVarDecl(*this, EndVar, EndExpr.get(), ColonLoc, |
| 2280 | diag::err_for_range_iter_deduction_failure)) { |
| 2281 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 2282 | return StmtError(); |
| 2283 | } |
| 2284 | } else { |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 2285 | OverloadCandidateSet CandidateSet(RangeLoc, |
| 2286 | OverloadCandidateSet::CSK_Normal); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2287 | BeginEndFunction BEFFailure; |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2288 | ForRangeStatus RangeStatus = |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2289 | BuildNonArrayForRange(*this, BeginRangeRef.get(), |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2290 | EndRangeRef.get(), RangeType, |
| 2291 | BeginVar, EndVar, ColonLoc, &CandidateSet, |
| 2292 | &BeginExpr, &EndExpr, &BEFFailure); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2293 | |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2294 | if (Kind == BFRK_Build && RangeStatus == FRS_NoViableFunction && |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2295 | BEFFailure == BEF_begin) { |
Richard Trieu | 0825469 | 2013-10-11 22:16:04 +0000 | [diff] [blame] | 2296 | // If the range is being built from an array parameter, emit a |
| 2297 | // a diagnostic that it is being treated as a pointer. |
| 2298 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Range)) { |
| 2299 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 2300 | QualType ArrayTy = PVD->getOriginalType(); |
| 2301 | QualType PointerTy = PVD->getType(); |
| 2302 | if (PointerTy->isPointerType() && ArrayTy->isArrayType()) { |
| 2303 | Diag(Range->getLocStart(), diag::err_range_on_array_parameter) |
| 2304 | << RangeLoc << PVD << ArrayTy << PointerTy; |
| 2305 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2306 | return StmtError(); |
| 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | // If building the range failed, try dereferencing the range expression |
| 2312 | // unless a diagnostic was issued or the end function is problematic. |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2313 | StmtResult SR = RebuildForRangeWithDereference(*this, S, ForLoc, |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2314 | CoawaitLoc, |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2315 | LoopVarDecl, ColonLoc, |
| 2316 | Range, RangeLoc, |
| 2317 | RParenLoc); |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2318 | if (SR.isInvalid() || SR.isUsable()) |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2319 | return SR; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2322 | // Otherwise, emit diagnostics if we haven't already. |
| 2323 | if (RangeStatus == FRS_NoViableFunction) { |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2324 | Expr *Range = BEFFailure ? EndRangeRef.get() : BeginRangeRef.get(); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2325 | Diag(Range->getLocStart(), diag::err_for_range_invalid) |
| 2326 | << RangeLoc << Range->getType() << BEFFailure; |
Nico Weber | a2a0eb9 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 2327 | CandidateSet.NoteCandidates(*this, OCD_AllCandidates, Range); |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2328 | } |
| 2329 | // Return an error if no fix was discovered. |
| 2330 | if (RangeStatus != FRS_Success) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2331 | return StmtError(); |
| 2332 | } |
| 2333 | |
Sam Panzer | 0f38443 | 2012-08-21 00:52:01 +0000 | [diff] [blame] | 2334 | assert(!BeginExpr.isInvalid() && !EndExpr.isInvalid() && |
| 2335 | "invalid range expression in for loop"); |
| 2336 | |
| 2337 | // 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] | 2338 | QualType BeginType = BeginVar->getType(), EndType = EndVar->getType(); |
| 2339 | if (!Context.hasSameType(BeginType, EndType)) { |
| 2340 | Diag(RangeLoc, diag::err_for_range_begin_end_types_differ) |
| 2341 | << BeginType << EndType; |
| 2342 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2343 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 2344 | } |
| 2345 | |
| 2346 | Decl *BeginEndDecls[] = { BeginVar, EndVar }; |
| 2347 | // Claim the type doesn't contain auto: we've already done the checking. |
| 2348 | DeclGroupPtrTy BeginEndGroup = |
Craig Topper | e3d2ecbe | 2014-06-28 23:22:33 +0000 | [diff] [blame] | 2349 | BuildDeclaratorGroup(MutableArrayRef<Decl *>(BeginEndDecls, 2), |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2350 | /*TypeMayContainAuto=*/ false); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2351 | BeginEndDecl = ActOnDeclStmt(BeginEndGroup, ColonLoc, ColonLoc); |
| 2352 | |
Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2353 | const QualType BeginRefNonRefType = BeginType.getNonReferenceType(); |
| 2354 | ExprResult BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2355 | VK_LValue, ColonLoc); |
Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2356 | if (BeginRef.isInvalid()) |
| 2357 | return StmtError(); |
| 2358 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2359 | ExprResult EndRef = BuildDeclRefExpr(EndVar, EndType.getNonReferenceType(), |
| 2360 | VK_LValue, ColonLoc); |
Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2361 | if (EndRef.isInvalid()) |
| 2362 | return StmtError(); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2363 | |
| 2364 | // Build and check __begin != __end expression. |
| 2365 | NotEqExpr = ActOnBinOp(S, ColonLoc, tok::exclaimequal, |
| 2366 | BeginRef.get(), EndRef.get()); |
| 2367 | NotEqExpr = ActOnBooleanCondition(S, ColonLoc, NotEqExpr.get()); |
| 2368 | NotEqExpr = ActOnFinishFullExpr(NotEqExpr.get()); |
| 2369 | if (NotEqExpr.isInvalid()) { |
Sam Panzer | 22a3fe1 | 2012-09-06 21:50:08 +0000 | [diff] [blame] | 2370 | Diag(RangeLoc, diag::note_for_range_invalid_iterator) |
| 2371 | << RangeLoc << 0 << BeginRangeRef.get()->getType(); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2372 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2373 | if (!Context.hasSameType(BeginType, EndType)) |
| 2374 | NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); |
| 2375 | return StmtError(); |
| 2376 | } |
| 2377 | |
| 2378 | // Build and check ++__begin expression. |
Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2379 | BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
| 2380 | VK_LValue, ColonLoc); |
| 2381 | if (BeginRef.isInvalid()) |
| 2382 | return StmtError(); |
| 2383 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2384 | IncrExpr = ActOnUnaryOp(S, ColonLoc, tok::plusplus, BeginRef.get()); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2385 | if (!IncrExpr.isInvalid() && CoawaitLoc.isValid()) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2386 | IncrExpr = ActOnCoawaitExpr(S, CoawaitLoc, IncrExpr.get()); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2387 | if (!IncrExpr.isInvalid()) |
| 2388 | IncrExpr = ActOnFinishFullExpr(IncrExpr.get()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2389 | if (IncrExpr.isInvalid()) { |
Sam Panzer | 22a3fe1 | 2012-09-06 21:50:08 +0000 | [diff] [blame] | 2390 | Diag(RangeLoc, diag::note_for_range_invalid_iterator) |
| 2391 | << RangeLoc << 2 << BeginRangeRef.get()->getType() ; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2392 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2393 | return StmtError(); |
| 2394 | } |
| 2395 | |
| 2396 | // Build and check *__begin expression. |
Ted Kremenek | bed648e | 2011-10-10 22:36:28 +0000 | [diff] [blame] | 2397 | BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, |
| 2398 | VK_LValue, ColonLoc); |
| 2399 | if (BeginRef.isInvalid()) |
| 2400 | return StmtError(); |
| 2401 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2402 | ExprResult DerefExpr = ActOnUnaryOp(S, ColonLoc, tok::star, BeginRef.get()); |
| 2403 | if (DerefExpr.isInvalid()) { |
Sam Panzer | 22a3fe1 | 2012-09-06 21:50:08 +0000 | [diff] [blame] | 2404 | Diag(RangeLoc, diag::note_for_range_invalid_iterator) |
| 2405 | << RangeLoc << 1 << BeginRangeRef.get()->getType(); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2406 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2407 | return StmtError(); |
| 2408 | } |
| 2409 | |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2410 | // Attach *__begin as initializer for VD. Don't touch it if we're just |
| 2411 | // trying to determine whether this would be a valid range. |
| 2412 | if (!LoopVar->isInvalidDecl() && Kind != BFRK_Check) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2413 | AddInitializerToDecl(LoopVar, DerefExpr.get(), /*DirectInit=*/false, |
| 2414 | /*TypeMayContainAuto=*/true); |
| 2415 | if (LoopVar->isInvalidDecl()) |
| 2416 | NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); |
| 2417 | } |
| 2418 | } |
| 2419 | |
Richard Smith | a05b3b5 | 2012-09-20 21:52:32 +0000 | [diff] [blame] | 2420 | // Don't bother to actually allocate the result if we're just trying to |
| 2421 | // determine whether it would be valid. |
| 2422 | if (Kind == BFRK_Check) |
| 2423 | return StmtResult(); |
| 2424 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2425 | return new (Context) CXXForRangeStmt( |
| 2426 | RangeDS, cast_or_null<DeclStmt>(BeginEndDecl.get()), NotEqExpr.get(), |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2427 | IncrExpr.get(), LoopVarDS, /*Body=*/nullptr, ForLoc, CoawaitLoc, |
| 2428 | ColonLoc, RParenLoc); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 2431 | /// FinishObjCForCollectionStmt - Attach the body to a objective-C foreach |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 2432 | /// statement. |
| 2433 | StmtResult Sema::FinishObjCForCollectionStmt(Stmt *S, Stmt *B) { |
| 2434 | if (!S || !B) |
| 2435 | return StmtError(); |
| 2436 | ObjCForCollectionStmt * ForStmt = cast<ObjCForCollectionStmt>(S); |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 2437 | |
Fariborz Jahanian | 450bb6e | 2012-07-03 22:00:52 +0000 | [diff] [blame] | 2438 | ForStmt->setBody(B); |
| 2439 | return S; |
| 2440 | } |
| 2441 | |
Richard Trieu | 3e1d483 | 2015-04-13 22:08:55 +0000 | [diff] [blame] | 2442 | // Warn when the loop variable is a const reference that creates a copy. |
| 2443 | // Suggest using the non-reference type for copies. If a copy can be prevented |
| 2444 | // suggest the const reference type that would do so. |
| 2445 | // For instance, given "for (const &Foo : Range)", suggest |
| 2446 | // "for (const Foo : Range)" to denote a copy is made for the loop. If |
| 2447 | // possible, also suggest "for (const &Bar : Range)" if this type prevents |
| 2448 | // the copy altogether. |
| 2449 | static void DiagnoseForRangeReferenceVariableCopies(Sema &SemaRef, |
| 2450 | const VarDecl *VD, |
| 2451 | QualType RangeInitType) { |
| 2452 | const Expr *InitExpr = VD->getInit(); |
| 2453 | if (!InitExpr) |
| 2454 | return; |
| 2455 | |
| 2456 | QualType VariableType = VD->getType(); |
| 2457 | |
| 2458 | const MaterializeTemporaryExpr *MTE = |
| 2459 | dyn_cast<MaterializeTemporaryExpr>(InitExpr); |
| 2460 | |
| 2461 | // No copy made. |
| 2462 | if (!MTE) |
| 2463 | return; |
| 2464 | |
| 2465 | const Expr *E = MTE->GetTemporaryExpr()->IgnoreImpCasts(); |
| 2466 | |
| 2467 | // Searching for either UnaryOperator for dereference of a pointer or |
| 2468 | // CXXOperatorCallExpr for handling iterators. |
| 2469 | while (!isa<CXXOperatorCallExpr>(E) && !isa<UnaryOperator>(E)) { |
| 2470 | if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(E)) { |
| 2471 | E = CCE->getArg(0); |
| 2472 | } else if (const CXXMemberCallExpr *Call = dyn_cast<CXXMemberCallExpr>(E)) { |
| 2473 | const MemberExpr *ME = cast<MemberExpr>(Call->getCallee()); |
| 2474 | E = ME->getBase(); |
| 2475 | } else { |
| 2476 | const MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(E); |
| 2477 | E = MTE->GetTemporaryExpr(); |
| 2478 | } |
| 2479 | E = E->IgnoreImpCasts(); |
| 2480 | } |
| 2481 | |
| 2482 | bool ReturnsReference = false; |
| 2483 | if (isa<UnaryOperator>(E)) { |
| 2484 | ReturnsReference = true; |
| 2485 | } else { |
| 2486 | const CXXOperatorCallExpr *Call = cast<CXXOperatorCallExpr>(E); |
| 2487 | const FunctionDecl *FD = Call->getDirectCallee(); |
| 2488 | QualType ReturnType = FD->getReturnType(); |
| 2489 | ReturnsReference = ReturnType->isReferenceType(); |
| 2490 | } |
| 2491 | |
| 2492 | if (ReturnsReference) { |
| 2493 | // Loop variable creates a temporary. Suggest either to go with |
| 2494 | // non-reference loop variable to indiciate a copy is made, or |
| 2495 | // the correct time to bind a const reference. |
| 2496 | SemaRef.Diag(VD->getLocation(), diag::warn_for_range_const_reference_copy) |
| 2497 | << VD << VariableType << E->getType(); |
| 2498 | QualType NonReferenceType = VariableType.getNonReferenceType(); |
| 2499 | NonReferenceType.removeLocalConst(); |
| 2500 | QualType NewReferenceType = |
| 2501 | SemaRef.Context.getLValueReferenceType(E->getType().withConst()); |
| 2502 | SemaRef.Diag(VD->getLocStart(), diag::note_use_type_or_non_reference) |
| 2503 | << NonReferenceType << NewReferenceType << VD->getSourceRange(); |
| 2504 | } else { |
| 2505 | // The range always returns a copy, so a temporary is always created. |
| 2506 | // Suggest removing the reference from the loop variable. |
| 2507 | SemaRef.Diag(VD->getLocation(), diag::warn_for_range_variable_always_copy) |
| 2508 | << VD << RangeInitType; |
| 2509 | QualType NonReferenceType = VariableType.getNonReferenceType(); |
| 2510 | NonReferenceType.removeLocalConst(); |
| 2511 | SemaRef.Diag(VD->getLocStart(), diag::note_use_non_reference_type) |
| 2512 | << NonReferenceType << VD->getSourceRange(); |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | // Warns when the loop variable can be changed to a reference type to |
| 2517 | // prevent a copy. For instance, if given "for (const Foo x : Range)" suggest |
| 2518 | // "for (const Foo &x : Range)" if this form does not make a copy. |
| 2519 | static void DiagnoseForRangeConstVariableCopies(Sema &SemaRef, |
| 2520 | const VarDecl *VD) { |
| 2521 | const Expr *InitExpr = VD->getInit(); |
| 2522 | if (!InitExpr) |
| 2523 | return; |
| 2524 | |
| 2525 | QualType VariableType = VD->getType(); |
| 2526 | |
| 2527 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(InitExpr)) { |
| 2528 | if (!CE->getConstructor()->isCopyConstructor()) |
| 2529 | return; |
| 2530 | } else if (const CastExpr *CE = dyn_cast<CastExpr>(InitExpr)) { |
| 2531 | if (CE->getCastKind() != CK_LValueToRValue) |
| 2532 | return; |
| 2533 | } else { |
| 2534 | return; |
| 2535 | } |
| 2536 | |
| 2537 | // TODO: Determine a maximum size that a POD type can be before a diagnostic |
| 2538 | // should be emitted. Also, only ignore POD types with trivial copy |
| 2539 | // constructors. |
| 2540 | if (VariableType.isPODType(SemaRef.Context)) |
| 2541 | return; |
| 2542 | |
| 2543 | // Suggest changing from a const variable to a const reference variable |
| 2544 | // if doing so will prevent a copy. |
| 2545 | SemaRef.Diag(VD->getLocation(), diag::warn_for_range_copy) |
| 2546 | << VD << VariableType << InitExpr->getType(); |
| 2547 | SemaRef.Diag(VD->getLocStart(), diag::note_use_reference_type) |
| 2548 | << SemaRef.Context.getLValueReferenceType(VariableType) |
| 2549 | << VD->getSourceRange(); |
| 2550 | } |
| 2551 | |
| 2552 | /// DiagnoseForRangeVariableCopies - Diagnose three cases and fixes for them. |
| 2553 | /// 1) for (const foo &x : foos) where foos only returns a copy. Suggest |
| 2554 | /// using "const foo x" to show that a copy is made |
| 2555 | /// 2) for (const bar &x : foos) where bar is a temporary intialized by bar. |
| 2556 | /// Suggest either "const bar x" to keep the copying or "const foo& x" to |
| 2557 | /// prevent the copy. |
| 2558 | /// 3) for (const foo x : foos) where x is constructed from a reference foo. |
| 2559 | /// Suggest "const foo &x" to prevent the copy. |
| 2560 | static void DiagnoseForRangeVariableCopies(Sema &SemaRef, |
| 2561 | const CXXForRangeStmt *ForStmt) { |
| 2562 | if (SemaRef.Diags.isIgnored(diag::warn_for_range_const_reference_copy, |
| 2563 | ForStmt->getLocStart()) && |
| 2564 | SemaRef.Diags.isIgnored(diag::warn_for_range_variable_always_copy, |
| 2565 | ForStmt->getLocStart()) && |
| 2566 | SemaRef.Diags.isIgnored(diag::warn_for_range_copy, |
| 2567 | ForStmt->getLocStart())) { |
| 2568 | return; |
| 2569 | } |
| 2570 | |
| 2571 | const VarDecl *VD = ForStmt->getLoopVariable(); |
| 2572 | if (!VD) |
| 2573 | return; |
| 2574 | |
| 2575 | QualType VariableType = VD->getType(); |
| 2576 | |
| 2577 | if (VariableType->isIncompleteType()) |
| 2578 | return; |
| 2579 | |
| 2580 | const Expr *InitExpr = VD->getInit(); |
| 2581 | if (!InitExpr) |
| 2582 | return; |
| 2583 | |
| 2584 | if (VariableType->isReferenceType()) { |
| 2585 | DiagnoseForRangeReferenceVariableCopies(SemaRef, VD, |
| 2586 | ForStmt->getRangeInit()->getType()); |
| 2587 | } else if (VariableType.isConstQualified()) { |
| 2588 | DiagnoseForRangeConstVariableCopies(SemaRef, VD); |
| 2589 | } |
| 2590 | } |
| 2591 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2592 | /// FinishCXXForRangeStmt - Attach the body to a C++0x for-range statement. |
| 2593 | /// This is a separate step from ActOnCXXForRangeStmt because analysis of the |
| 2594 | /// body cannot be performed until after the type of the range variable is |
| 2595 | /// determined. |
| 2596 | StmtResult Sema::FinishCXXForRangeStmt(Stmt *S, Stmt *B) { |
| 2597 | if (!S || !B) |
| 2598 | return StmtError(); |
| 2599 | |
Fariborz Jahanian | 0021347 | 2012-07-06 19:04:04 +0000 | [diff] [blame] | 2600 | if (isa<ObjCForCollectionStmt>(S)) |
| 2601 | return FinishObjCForCollectionStmt(S, B); |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 2602 | |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 2603 | CXXForRangeStmt *ForStmt = cast<CXXForRangeStmt>(S); |
| 2604 | ForStmt->setBody(B); |
| 2605 | |
| 2606 | DiagnoseEmptyStmtBody(ForStmt->getRParenLoc(), B, |
| 2607 | diag::warn_empty_range_based_for_body); |
| 2608 | |
Richard Trieu | 3e1d483 | 2015-04-13 22:08:55 +0000 | [diff] [blame] | 2609 | DiagnoseForRangeVariableCopies(*this, ForStmt); |
| 2610 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2611 | return S; |
| 2612 | } |
| 2613 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 2614 | StmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc, |
| 2615 | SourceLocation LabelLoc, |
| 2616 | LabelDecl *TheDecl) { |
| 2617 | getCurFunction()->setHasBranchIntoScope(); |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 2618 | TheDecl->markUsed(Context); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2619 | return new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 2620 | } |
Chris Lattner | 1c31050 | 2007-05-31 06:00:00 +0000 | [diff] [blame] | 2621 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2622 | StmtResult |
Chris Lattner | 34d9a51 | 2009-04-19 01:04:21 +0000 | [diff] [blame] | 2623 | Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2624 | Expr *E) { |
Eli Friedman | 8d7ff40 | 2009-03-26 00:18:06 +0000 | [diff] [blame] | 2625 | // Convert operand to void* |
Douglas Gregor | 30776d4 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 2626 | if (!E->isTypeDependent()) { |
| 2627 | QualType ETy = E->getType(); |
Chandler Carruth | 0021698 | 2010-01-31 10:26:25 +0000 | [diff] [blame] | 2628 | QualType DestTy = Context.getPointerType(Context.VoidTy.withConst()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2629 | ExprResult ExprRes = E; |
Douglas Gregor | 30776d4 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 2630 | AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2631 | CheckSingleAssignmentConstraints(DestTy, ExprRes); |
| 2632 | if (ExprRes.isInvalid()) |
| 2633 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2634 | E = ExprRes.get(); |
Chandler Carruth | 0021698 | 2010-01-31 10:26:25 +0000 | [diff] [blame] | 2635 | if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing)) |
Douglas Gregor | 30776d4 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 2636 | return StmtError(); |
| 2637 | } |
John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 2638 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2639 | ExprResult ExprRes = ActOnFinishFullExpr(E); |
| 2640 | if (ExprRes.isInvalid()) |
| 2641 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2642 | E = ExprRes.get(); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2643 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 2644 | getCurFunction()->setHasIndirectGoto(); |
John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 2645 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2646 | return new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 2649 | static void CheckJumpOutOfSEHFinally(Sema &S, SourceLocation Loc, |
| 2650 | const Scope &DestScope) { |
| 2651 | if (!S.CurrentSEHFinally.empty() && |
| 2652 | DestScope.Contains(*S.CurrentSEHFinally.back())) { |
| 2653 | S.Diag(Loc, diag::warn_jump_out_of_seh_finally); |
| 2654 | } |
| 2655 | } |
| 2656 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2657 | StmtResult |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2658 | Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 2659 | Scope *S = CurScope->getContinueParent(); |
| 2660 | if (!S) { |
| 2661 | // 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] | 2662 | return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 2663 | } |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 2664 | CheckJumpOutOfSEHFinally(*this, ContinueLoc, *S); |
Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2665 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2666 | return new (Context) ContinueStmt(ContinueLoc); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2669 | StmtResult |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2670 | Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 2671 | Scope *S = CurScope->getBreakParent(); |
| 2672 | if (!S) { |
| 2673 | // 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] | 2674 | return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 2675 | } |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 2676 | if (S->isOpenMPLoopScope()) |
| 2677 | return StmtError(Diag(BreakLoc, diag::err_omp_loop_cannot_use_stmt) |
| 2678 | << "break"); |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 2679 | CheckJumpOutOfSEHFinally(*this, BreakLoc, *S); |
Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2680 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2681 | return new (Context) BreakStmt(BreakLoc); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2684 | /// \brief Determine whether the given expression is a candidate for |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2685 | /// copy elision in either a return statement or a throw expression. |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2686 | /// |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2687 | /// \param ReturnType If we're determining the copy elision candidate for |
| 2688 | /// a return statement, this is the return type of the function. If we're |
| 2689 | /// determining the copy elision candidate for a throw expression, this will |
| 2690 | /// be a NULL type. |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2691 | /// |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2692 | /// \param E The expression being returned from the function or block, or |
| 2693 | /// being thrown. |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2694 | /// |
Douglas Gregor | 8639441 | 2011-05-20 15:00:53 +0000 | [diff] [blame] | 2695 | /// \param AllowFunctionParameter Whether we allow function parameters to |
| 2696 | /// be considered NRVO candidates. C++ prohibits this for NRVO itself, but |
| 2697 | /// we re-use this logic to determine whether we should try to move as part of |
| 2698 | /// a return or throw (which does allow function parameters). |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2699 | /// |
| 2700 | /// \returns The NRVO candidate variable, if the return statement may use the |
| 2701 | /// NRVO, or NULL if there is no such candidate. |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2702 | VarDecl *Sema::getCopyElisionCandidate(QualType ReturnType, |
| 2703 | Expr *E, |
| 2704 | bool AllowFunctionParameter) { |
| 2705 | if (!getLangOpts().CPlusPlus) |
| 2706 | return nullptr; |
| 2707 | |
| 2708 | // - in a return statement in a function [where] ... |
| 2709 | // ... the expression is the name of a non-volatile automatic object ... |
| 2710 | DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()); |
Alexey Bataev | 19acc3d | 2015-01-12 10:17:46 +0000 | [diff] [blame] | 2711 | if (!DR || DR->refersToEnclosingVariableOrCapture()) |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2712 | return nullptr; |
| 2713 | VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 2714 | if (!VD) |
| 2715 | return nullptr; |
| 2716 | |
| 2717 | if (isCopyElisionCandidate(ReturnType, VD, AllowFunctionParameter)) |
| 2718 | return VD; |
| 2719 | return nullptr; |
| 2720 | } |
| 2721 | |
| 2722 | bool Sema::isCopyElisionCandidate(QualType ReturnType, const VarDecl *VD, |
| 2723 | bool AllowFunctionParameter) { |
| 2724 | QualType VDType = VD->getType(); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2725 | // - in a return statement in a function with ... |
| 2726 | // ... a class return type ... |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2727 | if (!ReturnType.isNull() && !ReturnType->isDependentType()) { |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2728 | if (!ReturnType->isRecordType()) |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2729 | return false; |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2730 | // ... the same cv-unqualified type as the function return type ... |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2731 | if (!VDType->isDependentType() && |
| 2732 | !Context.hasSameUnqualifiedType(ReturnType, VDType)) |
| 2733 | return false; |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 2734 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | |
John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2736 | // ...object (other than a function or catch-clause parameter)... |
| 2737 | if (VD->getKind() != Decl::Var && |
| 2738 | !(AllowFunctionParameter && VD->getKind() == Decl::ParmVar)) |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2739 | return false; |
| 2740 | if (VD->isExceptionVariable()) return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2741 | |
John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2742 | // ...automatic... |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2743 | if (!VD->hasLocalStorage()) return false; |
John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2744 | |
| 2745 | // ...non-volatile... |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2746 | if (VD->getType().isVolatileQualified()) return false; |
John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2747 | |
| 2748 | // __block variables can't be allocated in a way that permits NRVO. |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2749 | if (VD->hasAttr<BlocksAttr>()) return false; |
John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2750 | |
| 2751 | // Variables with higher required alignment than their type's ABI |
| 2752 | // alignment cannot use NRVO. |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2753 | if (!VD->getType()->isDependentType() && VD->hasAttr<AlignedAttr>() && |
John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2754 | Context.getDeclAlign(VD) > Context.getTypeAlignInChars(VD->getType())) |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2755 | return false; |
John McCall | 03318c1 | 2011-11-11 03:57:31 +0000 | [diff] [blame] | 2756 | |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2757 | return true; |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2760 | /// \brief Perform the initialization of a potentially-movable value, which |
| 2761 | /// is the result of return value. |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2762 | /// |
| 2763 | /// This routine implements C++0x [class.copy]p33, which attempts to treat |
| 2764 | /// returned lvalues as rvalues in certain cases (to prefer move construction), |
| 2765 | /// then falls back to treating them as lvalues if that failed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2766 | ExprResult |
Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2767 | Sema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity, |
| 2768 | const VarDecl *NRVOCandidate, |
| 2769 | QualType ResultType, |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 2770 | Expr *Value, |
| 2771 | bool AllowNRVO) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2772 | // C++0x [class.copy]p33: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | // When the criteria for elision of a copy operation are met or would |
| 2774 | // be met save for the fact that the source object is a function |
| 2775 | // parameter, and the object to be copied is designated by an lvalue, |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2776 | // overload resolution to select the constructor for the copy is first |
| 2777 | // performed as if the object were designated by an rvalue. |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2778 | ExprResult Res = ExprError(); |
Douglas Gregor | 53e191ed | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 2779 | if (AllowNRVO && |
| 2780 | (NRVOCandidate || getCopyElisionCandidate(ResultType, Value, true))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2781 | ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack, |
Richard Smith | 9dd6e8f | 2012-05-15 05:04:02 +0000 | [diff] [blame] | 2782 | Value->getType(), CK_NoOp, Value, VK_XValue); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2783 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2784 | Expr *InitExpr = &AsRvalue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2785 | InitializationKind Kind |
Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2786 | = InitializationKind::CreateCopy(Value->getLocStart(), |
| 2787 | Value->getLocStart()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2788 | InitializationSequence Seq(*this, Entity, Kind, InitExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2789 | |
| 2790 | // [...] If overload resolution fails, or if the type of the first |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2791 | // parameter of the selected constructor is not an rvalue reference |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2792 | // to the object's type (possibly cv-qualified), overload resolution |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2793 | // is performed again, considering the object as an lvalue. |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 2794 | if (Seq) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2795 | for (InitializationSequence::step_iterator Step = Seq.step_begin(), |
| 2796 | StepEnd = Seq.step_end(); |
| 2797 | Step != StepEnd; ++Step) { |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 2798 | if (Step->Kind != InitializationSequence::SK_ConstructorInitialization) |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2799 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2800 | |
| 2801 | CXXConstructorDecl *Constructor |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2802 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2804 | const RValueReferenceType *RRefType |
Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2805 | = Constructor->getParamDecl(0)->getType() |
| 2806 | ->getAs<RValueReferenceType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2807 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2808 | // If we don't meet the criteria, break out now. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2809 | if (!RRefType || |
Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2810 | !Context.hasSameUnqualifiedType(RRefType->getPointeeType(), |
| 2811 | Context.getTypeDeclType(Constructor->getParent()))) |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2812 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2814 | // Promote "AsRvalue" to the heap, since we now need this |
| 2815 | // expression node to persist. |
Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2816 | Value = ImplicitCastExpr::Create(Context, Value->getType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2817 | CK_NoOp, Value, nullptr, VK_XValue); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2818 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2819 | // Complete type-checking the initialization of the return type |
| 2820 | // using the constructor we found. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2821 | Res = Seq.Perform(*this, Entity, Kind, Value); |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2822 | } |
| 2823 | } |
| 2824 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2825 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2826 | // 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] | 2827 | // above, or overload resolution failed. Either way, we need to try |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2828 | // (again) now with the return value expression as written. |
| 2829 | if (Res.isInvalid()) |
Douglas Gregor | 626fbed | 2011-01-21 21:08:57 +0000 | [diff] [blame] | 2830 | Res = PerformCopyInitialization(Entity, SourceLocation(), Value); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2831 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2832 | return Res; |
| 2833 | } |
| 2834 | |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2835 | /// \brief Determine whether the declared return type of the specified function |
| 2836 | /// contains 'auto'. |
| 2837 | static bool hasDeducedReturnType(FunctionDecl *FD) { |
| 2838 | const FunctionProtoType *FPT = |
| 2839 | FD->getTypeSourceInfo()->getType()->castAs<FunctionProtoType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2840 | return FPT->getReturnType()->isUndeducedType(); |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2843 | /// ActOnCapScopeReturnStmt - Utility routine to type-check return statements |
| 2844 | /// for capturing scopes. |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2845 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2846 | StmtResult |
Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2847 | Sema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
| 2848 | // If this is the first return we've seen, infer the return type. |
Richard Smith | 9155be1 | 2013-05-12 03:09:35 +0000 | [diff] [blame] | 2849 | // [expr.prim.lambda]p4 in C++11; block literals follow the same rules. |
Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2850 | CapturingScopeInfo *CurCap = cast<CapturingScopeInfo>(getCurFunction()); |
Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 2851 | QualType FnRetType = CurCap->ReturnType; |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2852 | LambdaScopeInfo *CurLambda = dyn_cast<LambdaScopeInfo>(CurCap); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 2853 | |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2854 | if (CurLambda && hasDeducedReturnType(CurLambda->CallOperator)) { |
| 2855 | // In C++1y, the return type may involve 'auto'. |
| 2856 | // FIXME: Blocks might have a return type of 'auto' explicitly specified. |
| 2857 | FunctionDecl *FD = CurLambda->CallOperator; |
| 2858 | if (CurCap->ReturnType.isNull()) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2859 | CurCap->ReturnType = FD->getReturnType(); |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2860 | |
| 2861 | AutoType *AT = CurCap->ReturnType->getContainedAutoType(); |
| 2862 | assert(AT && "lost auto type from lambda return type"); |
| 2863 | if (DeduceFunctionTypeFromReturnExpr(FD, ReturnLoc, RetValExp, AT)) { |
| 2864 | FD->setInvalidDecl(); |
| 2865 | return StmtError(); |
| 2866 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2867 | CurCap->ReturnType = FnRetType = FD->getReturnType(); |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2868 | } else if (CurCap->HasImplicitReturnType) { |
| 2869 | // For blocks/lambdas with implicit return types, we check each return |
| 2870 | // statement individually, and deduce the common return type when the block |
| 2871 | // or lambda is completed. |
| 2872 | // FIXME: Fold this into the 'auto' codepath above. |
Douglas Gregor | 940a550 | 2012-02-09 18:40:39 +0000 | [diff] [blame] | 2873 | if (RetValExp && !isa<InitListExpr>(RetValExp)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2874 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp); |
| 2875 | if (Result.isInvalid()) |
| 2876 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2877 | RetValExp = Result.get(); |
Douglas Gregor | 0aa91e0 | 2011-06-05 05:04:23 +0000 | [diff] [blame] | 2878 | |
Richard Smith | 5a0e50c | 2014-12-19 22:10:51 +0000 | [diff] [blame] | 2879 | // DR1048: even prior to C++14, we should use the 'auto' deduction rules |
| 2880 | // when deducing a return type for a lambda-expression (or by extension |
| 2881 | // for a block). These rules differ from the stated C++11 rules only in |
| 2882 | // that they remove top-level cv-qualifiers. |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2883 | if (!CurContext->isDependentContext()) |
Richard Smith | 5a0e50c | 2014-12-19 22:10:51 +0000 | [diff] [blame] | 2884 | FnRetType = RetValExp->getType().getUnqualifiedType(); |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2885 | else |
Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 2886 | FnRetType = CurCap->ReturnType = Context.DependentTy; |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 2887 | } else { |
Douglas Gregor | 940a550 | 2012-02-09 18:40:39 +0000 | [diff] [blame] | 2888 | if (RetValExp) { |
| 2889 | // C++11 [expr.lambda.prim]p4 bans inferring the result from an |
| 2890 | // initializer list, because it is not an expression (even |
| 2891 | // though we represent it as one). We still deduce 'void'. |
| 2892 | Diag(ReturnLoc, diag::err_lambda_return_init_list) |
| 2893 | << RetValExp->getSourceRange(); |
| 2894 | } |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 2895 | |
Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 2896 | FnRetType = Context.VoidTy; |
Fariborz Jahanian | 5c12ca8 | 2011-12-03 23:53:56 +0000 | [diff] [blame] | 2897 | } |
Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 2898 | |
| 2899 | // Although we'll properly infer the type of the block once it's completed, |
| 2900 | // make sure we provide a return type now for better error recovery. |
| 2901 | if (CurCap->ReturnType.isNull()) |
| 2902 | CurCap->ReturnType = FnRetType; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2903 | } |
Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2904 | assert(!FnRetType.isNull()); |
Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 2906 | if (BlockScopeInfo *CurBlock = dyn_cast<BlockScopeInfo>(CurCap)) { |
Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 2907 | if (CurBlock->FunctionType->getAs<FunctionType>()->getNoReturnAttr()) { |
| 2908 | Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr); |
| 2909 | return StmtError(); |
| 2910 | } |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 2911 | } else if (CapturedRegionScopeInfo *CurRegion = |
| 2912 | dyn_cast<CapturedRegionScopeInfo>(CurCap)) { |
| 2913 | Diag(ReturnLoc, diag::err_return_in_captured_stmt) << CurRegion->getRegionName(); |
| 2914 | return StmtError(); |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 2915 | } else { |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 2916 | assert(CurLambda && "unknown kind of captured scope"); |
| 2917 | if (CurLambda->CallOperator->getType()->getAs<FunctionType>() |
| 2918 | ->getNoReturnAttr()) { |
Douglas Gregor | cf11eb7 | 2012-02-15 16:20:15 +0000 | [diff] [blame] | 2919 | Diag(ReturnLoc, diag::err_noreturn_lambda_has_return_expr); |
| 2920 | return StmtError(); |
| 2921 | } |
| 2922 | } |
Mike Stump | 56ed2ea | 2009-04-29 21:40:37 +0000 | [diff] [blame] | 2923 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2924 | // Otherwise, verify that this result type matches the previous one. We are |
| 2925 | // pickier with blocks than for normal functions because we don't have GCC |
| 2926 | // compatibility to worry about here. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2927 | const VarDecl *NRVOCandidate = nullptr; |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 2928 | if (FnRetType->isDependentType()) { |
John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2929 | // Delay processing for now. TODO: there are lots of dependent |
| 2930 | // types we can conclusively prove aren't void. |
| 2931 | } else if (FnRetType->isVoidType()) { |
Sebastian Redl | 74b173e | 2012-02-22 17:38:04 +0000 | [diff] [blame] | 2932 | if (RetValExp && !isa<InitListExpr>(RetValExp) && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2933 | !(getLangOpts().CPlusPlus && |
John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2934 | (RetValExp->isTypeDependent() || |
| 2935 | RetValExp->getType()->isVoidType()))) { |
Fariborz Jahanian | 3ba24ba | 2012-03-21 16:45:13 +0000 | [diff] [blame] | 2936 | if (!getLangOpts().CPlusPlus && |
| 2937 | RetValExp->getType()->isVoidType()) |
Fariborz Jahanian | 0740ed9 | 2012-03-21 20:28:39 +0000 | [diff] [blame] | 2938 | Diag(ReturnLoc, diag::ext_return_has_void_expr) << "literal" << 2; |
Fariborz Jahanian | 3ba24ba | 2012-03-21 16:45:13 +0000 | [diff] [blame] | 2939 | else { |
| 2940 | Diag(ReturnLoc, diag::err_return_block_has_expr); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2941 | RetValExp = nullptr; |
Fariborz Jahanian | 3ba24ba | 2012-03-21 16:45:13 +0000 | [diff] [blame] | 2942 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2943 | } |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2944 | } else if (!RetValExp) { |
John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2945 | return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); |
| 2946 | } else if (!RetValExp->isTypeDependent()) { |
| 2947 | // we have a non-void block with an expression, continue checking |
Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2948 | |
John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2949 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 2950 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 2951 | // function return. |
Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2952 | |
John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2953 | // In C++ the return statement is handled via a copy initialization. |
| 2954 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
| 2955 | NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); |
| 2956 | InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, |
| 2957 | FnRetType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2958 | NRVOCandidate != nullptr); |
John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2959 | ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, |
| 2960 | FnRetType, RetValExp); |
| 2961 | if (Res.isInvalid()) { |
| 2962 | // FIXME: Cleanup temporaries here, anyway? |
| 2963 | return StmtError(); |
Anders Carlsson | 6f923f8 | 2010-01-29 18:30:20 +0000 | [diff] [blame] | 2964 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2965 | RetValExp = Res.get(); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 2966 | CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2967 | } else { |
| 2968 | NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2969 | } |
Sebastian Redl | 573feed | 2009-01-18 13:19:59 +0000 | [diff] [blame] | 2970 | |
John McCall | 75f92b5 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 2971 | if (RetValExp) { |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2972 | ExprResult ER = ActOnFinishFullExpr(RetValExp, ReturnLoc); |
| 2973 | if (ER.isInvalid()) |
| 2974 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2975 | RetValExp = ER.get(); |
John McCall | 75f92b5 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 2976 | } |
John McCall | 5500ef2 | 2011-08-17 22:09:46 +0000 | [diff] [blame] | 2977 | ReturnStmt *Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, |
| 2978 | NRVOCandidate); |
John McCall | 75f92b5 | 2011-08-17 21:34:14 +0000 | [diff] [blame] | 2979 | |
Jordan Rose | d39e5f1 | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 2980 | // If we need to check for the named return value optimization, |
| 2981 | // or if we need to infer the return type, |
| 2982 | // save the return statement in our scope for later processing. |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 2983 | if (CurCap->HasImplicitReturnType || NRVOCandidate) |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 2984 | FunctionScopes.back()->Returns.push_back(Result); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2985 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2986 | if (FunctionScopes.back()->FirstReturnLoc.isInvalid()) |
| 2987 | FunctionScopes.back()->FirstReturnLoc = ReturnLoc; |
| 2988 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2989 | return Result; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2990 | } |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 2991 | |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 2992 | namespace { |
| 2993 | /// \brief Marks all typedefs in all local classes in a type referenced. |
| 2994 | /// |
| 2995 | /// In a function like |
| 2996 | /// auto f() { |
| 2997 | /// struct S { typedef int a; }; |
| 2998 | /// return S(); |
| 2999 | /// } |
| 3000 | /// |
| 3001 | /// the local type escapes and could be referenced in some TUs but not in |
| 3002 | /// others. Pretend that all local typedefs are always referenced, to not warn |
| 3003 | /// on this. This isn't necessary if f has internal linkage, or the typedef |
| 3004 | /// is private. |
| 3005 | class LocalTypedefNameReferencer |
| 3006 | : public RecursiveASTVisitor<LocalTypedefNameReferencer> { |
| 3007 | public: |
| 3008 | LocalTypedefNameReferencer(Sema &S) : S(S) {} |
| 3009 | bool VisitRecordType(const RecordType *RT); |
| 3010 | private: |
| 3011 | Sema &S; |
| 3012 | }; |
| 3013 | bool LocalTypedefNameReferencer::VisitRecordType(const RecordType *RT) { |
| 3014 | auto *R = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 3015 | if (!R || !R->isLocalClass() || !R->isLocalClass()->isExternallyVisible() || |
| 3016 | R->isDependentType()) |
| 3017 | return true; |
| 3018 | for (auto *TmpD : R->decls()) |
| 3019 | if (auto *T = dyn_cast<TypedefNameDecl>(TmpD)) |
| 3020 | if (T->getAccess() != AS_private || R->hasFriends()) |
| 3021 | S.MarkAnyDeclReferenced(T->getLocation(), T, /*OdrUse=*/false); |
| 3022 | return true; |
| 3023 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3024 | } |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 3025 | |
Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 3026 | TypeLoc Sema::getReturnTypeLoc(FunctionDecl *FD) const { |
Saleem Abdulrasool | e8aab74 | 2014-10-17 17:20:33 +0000 | [diff] [blame] | 3027 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 3028 | while (auto ATL = TL.getAs<AttributedTypeLoc>()) |
| 3029 | TL = ATL.getModifiedLoc().IgnoreParens(); |
Saleem Abdulrasool | e8aab74 | 2014-10-17 17:20:33 +0000 | [diff] [blame] | 3030 | return TL.castAs<FunctionProtoTypeLoc>().getReturnLoc(); |
Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3033 | /// Deduce the return type for a function from a returned expression, per |
| 3034 | /// C++1y [dcl.spec.auto]p6. |
| 3035 | bool Sema::DeduceFunctionTypeFromReturnExpr(FunctionDecl *FD, |
| 3036 | SourceLocation ReturnLoc, |
| 3037 | Expr *&RetExpr, |
| 3038 | AutoType *AT) { |
Saleem Abdulrasool | 374b5aa | 2014-10-16 22:42:53 +0000 | [diff] [blame] | 3039 | TypeLoc OrigResultType = getReturnTypeLoc(FD); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3040 | QualType Deduced; |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3041 | |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3042 | if (RetExpr && isa<InitListExpr>(RetExpr)) { |
| 3043 | // If the deduction is for a return statement and the initializer is |
| 3044 | // a braced-init-list, the program is ill-formed. |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3045 | Diag(RetExpr->getExprLoc(), |
| 3046 | getCurLambda() ? diag::err_lambda_return_init_list |
| 3047 | : diag::err_auto_fn_return_init_list) |
| 3048 | << RetExpr->getSourceRange(); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3049 | return true; |
| 3050 | } |
| 3051 | |
| 3052 | if (FD->isDependentContext()) { |
| 3053 | // C++1y [dcl.spec.auto]p12: |
| 3054 | // Return type deduction [...] occurs when the definition is |
| 3055 | // instantiated even if the function body contains a return |
| 3056 | // statement with a non-type-dependent operand. |
| 3057 | assert(AT->isDeduced() && "should have deduced to dependent type"); |
| 3058 | return false; |
Douglas Gregor | 6032d5b | 2015-10-01 19:52:44 +0000 | [diff] [blame] | 3059 | } |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | 6032d5b | 2015-10-01 19:52:44 +0000 | [diff] [blame] | 3061 | if (RetExpr) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3062 | // Otherwise, [...] deduce a value for U using the rules of template |
| 3063 | // argument deduction. |
| 3064 | DeduceAutoResult DAR = DeduceAutoType(OrigResultType, RetExpr, Deduced); |
| 3065 | |
| 3066 | if (DAR == DAR_Failed && !FD->isInvalidDecl()) |
| 3067 | Diag(RetExpr->getExprLoc(), diag::err_auto_fn_deduction_failure) |
| 3068 | << OrigResultType.getType() << RetExpr->getType(); |
| 3069 | |
| 3070 | if (DAR != DAR_Succeeded) |
| 3071 | return true; |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 3072 | |
| 3073 | // If a local type is part of the returned type, mark its fields as |
| 3074 | // referenced. |
| 3075 | LocalTypedefNameReferencer Referencer(*this); |
| 3076 | Referencer.TraverseType(RetExpr->getType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3077 | } else { |
| 3078 | // In the case of a return with no operand, the initializer is considered |
| 3079 | // to be void(). |
| 3080 | // |
| 3081 | // Deduction here can only succeed if the return type is exactly 'cv auto' |
| 3082 | // or 'decltype(auto)', so just check for that case directly. |
| 3083 | if (!OrigResultType.getType()->getAs<AutoType>()) { |
| 3084 | Diag(ReturnLoc, diag::err_auto_fn_return_void_but_not_auto) |
| 3085 | << OrigResultType.getType(); |
| 3086 | return true; |
| 3087 | } |
| 3088 | // We always deduce U = void in this case. |
| 3089 | Deduced = SubstAutoType(OrigResultType.getType(), Context.VoidTy); |
| 3090 | if (Deduced.isNull()) |
| 3091 | return true; |
| 3092 | } |
| 3093 | |
| 3094 | // If a function with a declared return type that contains a placeholder type |
| 3095 | // has multiple return statements, the return type is deduced for each return |
| 3096 | // statement. [...] if the type deduced is not the same in each deduction, |
| 3097 | // the program is ill-formed. |
Argyrios Kyrtzidis | b4030df | 2016-01-30 01:51:20 +0000 | [diff] [blame] | 3098 | QualType DeducedT = AT->getDeducedType(); |
| 3099 | if (!DeducedT.isNull() && !FD->isInvalidDecl()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3100 | AutoType *NewAT = Deduced->getContainedAutoType(); |
Manman Ren | b4e8a1b | 2016-02-04 20:05:40 +0000 | [diff] [blame] | 3101 | // It is possible that NewAT->getDeducedType() is null. When that happens, |
| 3102 | // we should not crash, instead we ignore this deduction. |
| 3103 | if (NewAT->getDeducedType().isNull()) |
| 3104 | return false; |
| 3105 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3106 | CanQualType OldDeducedType = Context.getCanonicalFunctionResultType( |
Argyrios Kyrtzidis | b4030df | 2016-01-30 01:51:20 +0000 | [diff] [blame] | 3107 | DeducedT); |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3108 | CanQualType NewDeducedType = Context.getCanonicalFunctionResultType( |
| 3109 | NewAT->getDeducedType()); |
| 3110 | if (!FD->isDependentContext() && OldDeducedType != NewDeducedType) { |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3111 | const LambdaScopeInfo *LambdaSI = getCurLambda(); |
| 3112 | if (LambdaSI && LambdaSI->HasImplicitReturnType) { |
| 3113 | Diag(ReturnLoc, diag::err_typecheck_missing_return_type_incompatible) |
Argyrios Kyrtzidis | b4030df | 2016-01-30 01:51:20 +0000 | [diff] [blame] | 3114 | << NewAT->getDeducedType() << DeducedT |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3115 | << true /*IsLambda*/; |
| 3116 | } else { |
| 3117 | Diag(ReturnLoc, diag::err_auto_fn_different_deductions) |
| 3118 | << (AT->isDecltypeAuto() ? 1 : 0) |
Argyrios Kyrtzidis | b4030df | 2016-01-30 01:51:20 +0000 | [diff] [blame] | 3119 | << NewAT->getDeducedType() << DeducedT; |
Richard Smith | 4db51c2 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 3120 | } |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3121 | return true; |
| 3122 | } |
| 3123 | } else if (!FD->isInvalidDecl()) { |
| 3124 | // Update all declarations of the function to have the deduced return type. |
| 3125 | Context.adjustDeducedFunctionResultType(FD, Deduced); |
| 3126 | } |
| 3127 | |
| 3128 | return false; |
| 3129 | } |
| 3130 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3131 | StmtResult |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3132 | Sema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp, |
| 3133 | Scope *CurScope) { |
| 3134 | StmtResult R = BuildReturnStmt(ReturnLoc, RetValExp); |
| 3135 | if (R.isInvalid()) { |
| 3136 | return R; |
| 3137 | } |
| 3138 | |
| 3139 | if (VarDecl *VD = |
| 3140 | const_cast<VarDecl*>(cast<ReturnStmt>(R.get())->getNRVOCandidate())) { |
| 3141 | CurScope->addNRVOCandidate(VD); |
| 3142 | } else { |
| 3143 | CurScope->setNoNRVO(); |
| 3144 | } |
| 3145 | |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3146 | CheckJumpOutOfSEHFinally(*this, ReturnLoc, *CurScope->getFnParent()); |
| 3147 | |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3148 | return R; |
| 3149 | } |
| 3150 | |
| 3151 | StmtResult Sema::BuildReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { |
Douglas Gregor | 4385d8b | 2011-05-20 15:32:55 +0000 | [diff] [blame] | 3152 | // Check for unexpanded parameter packs. |
| 3153 | if (RetValExp && DiagnoseUnexpandedParameterPack(RetValExp)) |
| 3154 | return StmtError(); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3155 | |
Eli Friedman | 34b4906 | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 3156 | if (isa<CapturingScopeInfo>(getCurFunction())) |
| 3157 | return ActOnCapScopeReturnStmt(ReturnLoc, RetValExp); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3158 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3159 | QualType FnRetType; |
Eli Friedman | 410fc7a | 2012-03-30 01:13:43 +0000 | [diff] [blame] | 3160 | QualType RelatedRetType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3161 | const AttrVec *Attrs = nullptr; |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 3162 | bool isObjCMethod = false; |
| 3163 | |
Mike Stump | d00bc1a | 2009-04-29 00:43:21 +0000 | [diff] [blame] | 3164 | if (const FunctionDecl *FD = getCurFunctionDecl()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3165 | FnRetType = FD->getReturnType(); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 3166 | if (FD->hasAttrs()) |
| 3167 | Attrs = &FD->getAttrs(); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3168 | if (FD->isNoReturn()) |
Chris Lattner | 6e127a6 | 2009-05-31 19:32:13 +0000 | [diff] [blame] | 3169 | Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) |
Eli Friedman | de95878 | 2012-01-05 00:49:17 +0000 | [diff] [blame] | 3170 | << FD->getDeclName(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3171 | } else if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3172 | FnRetType = MD->getReturnType(); |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 3173 | isObjCMethod = true; |
| 3174 | if (MD->hasAttrs()) |
| 3175 | Attrs = &MD->getAttrs(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3176 | if (MD->hasRelatedResultType() && MD->getClassInterface()) { |
| 3177 | // In the implementation of a method with a related return type, the |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3178 | // type used to type-check the validity of return statements within the |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3179 | // 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] | 3180 | RelatedRetType = Context.getObjCInterfaceType(MD->getClassInterface()); |
| 3181 | RelatedRetType = Context.getObjCObjectPointerType(RelatedRetType); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 3182 | } |
| 3183 | } else // If we don't have a function/method context, bail. |
Steve Naroff | f3833d7 | 2009-03-03 00:45:38 +0000 | [diff] [blame] | 3184 | return StmtError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3186 | // FIXME: Add a flag to the ScopeInfo to indicate whether we're performing |
| 3187 | // deduction. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3188 | if (getLangOpts().CPlusPlus14) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3189 | if (AutoType *AT = FnRetType->getContainedAutoType()) { |
| 3190 | FunctionDecl *FD = cast<FunctionDecl>(CurContext); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3191 | if (DeduceFunctionTypeFromReturnExpr(FD, ReturnLoc, RetValExp, AT)) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3192 | FD->setInvalidDecl(); |
| 3193 | return StmtError(); |
| 3194 | } else { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3195 | FnRetType = FD->getReturnType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3196 | } |
| 3197 | } |
| 3198 | } |
| 3199 | |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3200 | bool HasDependentReturnType = FnRetType->isDependentType(); |
| 3201 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3202 | ReturnStmt *Result = nullptr; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3203 | if (FnRetType->isVoidType()) { |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3204 | if (RetValExp) { |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3205 | if (isa<InitListExpr>(RetValExp)) { |
| 3206 | // We simply never allow init lists as the return value of void |
| 3207 | // functions. This is compatible because this was never allowed before, |
| 3208 | // so there's no legacy code to deal with. |
| 3209 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 3210 | int FunctionKind = 0; |
| 3211 | if (isa<ObjCMethodDecl>(CurDecl)) |
| 3212 | FunctionKind = 1; |
| 3213 | else if (isa<CXXConstructorDecl>(CurDecl)) |
| 3214 | FunctionKind = 2; |
| 3215 | else if (isa<CXXDestructorDecl>(CurDecl)) |
| 3216 | FunctionKind = 3; |
| 3217 | |
| 3218 | Diag(ReturnLoc, diag::err_return_init_list) |
| 3219 | << CurDecl->getDeclName() << FunctionKind |
| 3220 | << RetValExp->getSourceRange(); |
| 3221 | |
| 3222 | // Drop the expression. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3223 | RetValExp = nullptr; |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3224 | } else if (!RetValExp->isTypeDependent()) { |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3225 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 3226 | unsigned D = diag::ext_return_has_expr; |
Fariborz Jahanian | a759848 | 2013-12-03 17:10:08 +0000 | [diff] [blame] | 3227 | if (RetValExp->getType()->isVoidType()) { |
| 3228 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 3229 | if (isa<CXXConstructorDecl>(CurDecl) || |
| 3230 | isa<CXXDestructorDecl>(CurDecl)) |
| 3231 | D = diag::err_ctor_dtor_returns_void; |
| 3232 | else |
| 3233 | D = diag::ext_return_has_void_expr; |
| 3234 | } |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3235 | else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3236 | ExprResult Result = RetValExp; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3237 | Result = IgnoredValueConversions(Result.get()); |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3238 | if (Result.isInvalid()) |
| 3239 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3240 | RetValExp = Result.get(); |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3241 | RetValExp = ImpCastExprToType(RetValExp, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3242 | Context.VoidTy, CK_ToVoid).get(); |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3243 | } |
Fariborz Jahanian | a759848 | 2013-12-03 17:10:08 +0000 | [diff] [blame] | 3244 | // return of void in constructor/destructor is illegal in C++. |
| 3245 | if (D == diag::err_ctor_dtor_returns_void) { |
| 3246 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
| 3247 | Diag(ReturnLoc, D) |
| 3248 | << CurDecl->getDeclName() << isa<CXXDestructorDecl>(CurDecl) |
| 3249 | << RetValExp->getSourceRange(); |
| 3250 | } |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3251 | // return (some void expression); is legal in C++. |
Fariborz Jahanian | a759848 | 2013-12-03 17:10:08 +0000 | [diff] [blame] | 3252 | else if (D != diag::ext_return_has_void_expr || |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 3253 | !getLangOpts().CPlusPlus) { |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3254 | NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); |
Chandler Carruth | 1406d6c | 2011-06-30 08:56:22 +0000 | [diff] [blame] | 3255 | |
| 3256 | int FunctionKind = 0; |
| 3257 | if (isa<ObjCMethodDecl>(CurDecl)) |
| 3258 | FunctionKind = 1; |
| 3259 | else if (isa<CXXConstructorDecl>(CurDecl)) |
| 3260 | FunctionKind = 2; |
| 3261 | else if (isa<CXXDestructorDecl>(CurDecl)) |
| 3262 | FunctionKind = 3; |
| 3263 | |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3264 | Diag(ReturnLoc, D) |
Chandler Carruth | 1406d6c | 2011-06-30 08:56:22 +0000 | [diff] [blame] | 3265 | << CurDecl->getDeclName() << FunctionKind |
Nick Lewycky | 1be750a | 2011-06-01 07:44:31 +0000 | [diff] [blame] | 3266 | << RetValExp->getSourceRange(); |
| 3267 | } |
Chris Lattner | 0cb00d6 | 2008-12-18 02:03:48 +0000 | [diff] [blame] | 3268 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3269 | |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3270 | if (RetValExp) { |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3271 | ExprResult ER = ActOnFinishFullExpr(RetValExp, ReturnLoc); |
| 3272 | if (ER.isInvalid()) |
| 3273 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3274 | RetValExp = ER.get(); |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 3275 | } |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 3276 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3277 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3278 | Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, nullptr); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3279 | } else if (!RetValExp && !HasDependentReturnType) { |
David Majnemer | 2887ad3 | 2014-12-13 08:12:56 +0000 | [diff] [blame] | 3280 | FunctionDecl *FD = getCurFunctionDecl(); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3281 | |
David Majnemer | 2887ad3 | 2014-12-13 08:12:56 +0000 | [diff] [blame] | 3282 | unsigned DiagID; |
| 3283 | if (getLangOpts().CPlusPlus11 && FD && FD->isConstexpr()) { |
| 3284 | // C++11 [stmt.return]p2 |
| 3285 | DiagID = diag::err_constexpr_return_missing_expr; |
| 3286 | FD->setInvalidDecl(); |
| 3287 | } else if (getLangOpts().C99) { |
| 3288 | // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 3289 | DiagID = diag::ext_return_missing_expr; |
| 3290 | } else { |
| 3291 | // C90 6.6.6.4p4 |
| 3292 | DiagID = diag::warn_return_missing_expr; |
| 3293 | } |
| 3294 | |
| 3295 | if (FD) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3296 | Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3297 | else |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 3298 | Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; |
David Majnemer | 2887ad3 | 2014-12-13 08:12:56 +0000 | [diff] [blame] | 3299 | |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3300 | Result = new (Context) ReturnStmt(ReturnLoc); |
| 3301 | } else { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3302 | assert(RetValExp || HasDependentReturnType); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3303 | const VarDecl *NRVOCandidate = nullptr; |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3304 | |
| 3305 | QualType RetType = RelatedRetType.isNull() ? FnRetType : RelatedRetType; |
| 3306 | |
| 3307 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 3308 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 3309 | // function return. |
| 3310 | |
| 3311 | // In C++ the return statement is handled via a copy initialization, |
| 3312 | // the C version of which boils down to CheckSingleAssignmentConstraints. |
| 3313 | if (RetValExp) |
| 3314 | NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3315 | if (!HasDependentReturnType && !RetValExp->isTypeDependent()) { |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3316 | // we have a non-void function with an expression, continue checking |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3317 | InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3318 | RetType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3319 | NRVOCandidate != nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3320 | ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3321 | RetType, RetValExp); |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3322 | if (Res.isInvalid()) { |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3323 | // FIXME: Clean up temporaries here anyway? |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3324 | return StmtError(); |
| 3325 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3326 | RetValExp = Res.getAs<Expr>(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3327 | |
| 3328 | // If we have a related result type, we need to implicitly |
| 3329 | // convert back to the formal result type. We can't pretend to |
| 3330 | // initialize the result again --- we might end double-retaining |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3331 | // --- so instead we initialize a notional temporary. |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3332 | if (!RelatedRetType.isNull()) { |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 3333 | Entity = InitializedEntity::InitializeRelatedResult(getCurMethodDecl(), |
| 3334 | FnRetType); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3335 | Res = PerformCopyInitialization(Entity, ReturnLoc, RetValExp); |
| 3336 | if (Res.isInvalid()) { |
| 3337 | // FIXME: Clean up temporaries here anyway? |
| 3338 | return StmtError(); |
| 3339 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3340 | RetValExp = Res.getAs<Expr>(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Artyom Skrobov | 9f21344 | 2014-01-24 11:10:39 +0000 | [diff] [blame] | 3343 | CheckReturnValExpr(RetValExp, FnRetType, ReturnLoc, isObjCMethod, Attrs, |
| 3344 | getCurFunctionDecl()); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3345 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3346 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3347 | if (RetValExp) { |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3348 | ExprResult ER = ActOnFinishFullExpr(RetValExp, ReturnLoc); |
| 3349 | if (ER.isInvalid()) |
| 3350 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3351 | RetValExp = ER.get(); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3352 | } |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3353 | Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3354 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | |
| 3356 | // 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] | 3357 | // return statement in our scope for later processing. |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 3358 | if (Result->getNRVOCandidate()) |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 3359 | FunctionScopes.back()->Returns.push_back(Result); |
Chad Rosier | cc6a908 | 2012-06-20 18:51:04 +0000 | [diff] [blame] | 3360 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 3361 | if (FunctionScopes.back()->FirstReturnLoc.isInvalid()) |
| 3362 | FunctionScopes.back()->FirstReturnLoc = ReturnLoc; |
| 3363 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3364 | return Result; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3367 | StmtResult |
Sebastian Redl | 481bf3f | 2009-01-18 17:43:11 +0000 | [diff] [blame] | 3368 | Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3369 | SourceLocation RParen, Decl *Parm, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3370 | Stmt *Body) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3371 | VarDecl *Var = cast_or_null<VarDecl>(Parm); |
Douglas Gregor | f356419 | 2010-04-26 17:32:49 +0000 | [diff] [blame] | 3372 | if (Var && Var->isInvalidDecl()) |
| 3373 | return StmtError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3374 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3375 | return new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body); |
Fariborz Jahanian | 9e63b98 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3378 | StmtResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3379 | Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3380 | return new (Context) ObjCAtFinallyStmt(AtLoc, Body); |
Fariborz Jahanian | 71234d8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 3381 | } |
Fariborz Jahanian | f859ef2 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 3382 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3383 | StmtResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3384 | Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3385 | MultiStmtArg CatchStmts, Stmt *Finally) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3386 | if (!getLangOpts().ObjCExceptions) |
Anders Carlsson | ce8dd3a | 2011-02-19 23:53:54 +0000 | [diff] [blame] | 3387 | Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@try"; |
| 3388 | |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 3389 | getCurFunction()->setHasBranchProtectedScope(); |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3390 | unsigned NumCatchStmts = CatchStmts.size(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3391 | return ObjCAtTryStmt::Create(Context, AtLoc, Try, CatchStmts.data(), |
| 3392 | NumCatchStmts, Finally); |
Fariborz Jahanian | f859ef2 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
John McCall | 0bd3e40 | 2012-05-08 21:41:25 +0000 | [diff] [blame] | 3395 | StmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw) { |
Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3396 | if (Throw) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3397 | ExprResult Result = DefaultLvalueConversion(Throw); |
| 3398 | if (Result.isInvalid()) |
| 3399 | return StmtError(); |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3400 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3401 | Result = ActOnFinishFullExpr(Result.get()); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3402 | if (Result.isInvalid()) |
| 3403 | return StmtError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3404 | Throw = Result.get(); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3405 | |
Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3406 | QualType ThrowType = Throw->getType(); |
| 3407 | // Make sure the expression type is an ObjC pointer or "void *". |
| 3408 | if (!ThrowType->isDependentType() && |
| 3409 | !ThrowType->isObjCObjectPointerType()) { |
| 3410 | const PointerType *PT = ThrowType->getAs<PointerType>(); |
| 3411 | if (!PT || !PT->getPointeeType()->isVoidType()) |
| 3412 | return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) |
| 3413 | << Throw->getType() << Throw->getSourceRange()); |
| 3414 | } |
| 3415 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3416 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3417 | return new (Context) ObjCAtThrowStmt(AtLoc, Throw); |
Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3420 | StmtResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3421 | Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw, |
Douglas Gregor | 2900c16 | 2010-04-22 21:44:01 +0000 | [diff] [blame] | 3422 | Scope *CurScope) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3423 | if (!getLangOpts().ObjCExceptions) |
Anders Carlsson | ce8dd3a | 2011-02-19 23:53:54 +0000 | [diff] [blame] | 3424 | Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@throw"; |
| 3425 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3426 | if (!Throw) { |
Nico Weber | 9af63b2 | 2015-03-09 02:34:29 +0000 | [diff] [blame] | 3427 | // @throw without an expression designates a rethrow (which must occur |
Steve Naroff | 5ee2c02 | 2009-02-11 20:05:44 +0000 | [diff] [blame] | 3428 | // in the context of an @catch clause). |
| 3429 | Scope *AtCatchParent = CurScope; |
| 3430 | while (AtCatchParent && !AtCatchParent->isAtCatchScope()) |
| 3431 | AtCatchParent = AtCatchParent->getParent(); |
| 3432 | if (!AtCatchParent) |
Steve Naroff | c49b22a | 2009-02-12 18:09:32 +0000 | [diff] [blame] | 3433 | return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3434 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3435 | return BuildObjCAtThrowStmt(AtLoc, Throw); |
Fariborz Jahanian | adfbbc3 | 2007-11-07 02:00:49 +0000 | [diff] [blame] | 3436 | } |
Fariborz Jahanian | f859ef2 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 3437 | |
John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3438 | ExprResult |
| 3439 | Sema::ActOnObjCAtSynchronizedOperand(SourceLocation atLoc, Expr *operand) { |
| 3440 | ExprResult result = DefaultLvalueConversion(operand); |
| 3441 | if (result.isInvalid()) |
| 3442 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3443 | operand = result.get(); |
John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3444 | |
| 3445 | // Make sure the expression type is an ObjC pointer or "void *". |
| 3446 | QualType type = operand->getType(); |
| 3447 | if (!type->isDependentType() && |
| 3448 | !type->isObjCObjectPointerType()) { |
| 3449 | const PointerType *pointerType = type->getAs<PointerType>(); |
Jordan Rose | 5790d52 | 2014-08-12 16:20:36 +0000 | [diff] [blame] | 3450 | if (!pointerType || !pointerType->getPointeeType()->isVoidType()) { |
| 3451 | if (getLangOpts().CPlusPlus) { |
| 3452 | if (RequireCompleteType(atLoc, type, |
| 3453 | diag::err_incomplete_receiver_type)) |
| 3454 | return Diag(atLoc, diag::error_objc_synchronized_expects_object) |
| 3455 | << type << operand->getSourceRange(); |
| 3456 | |
| 3457 | ExprResult result = PerformContextuallyConvertToObjCPointer(operand); |
| 3458 | if (!result.isUsable()) |
| 3459 | return Diag(atLoc, diag::error_objc_synchronized_expects_object) |
| 3460 | << type << operand->getSourceRange(); |
| 3461 | |
| 3462 | operand = result.get(); |
| 3463 | } else { |
| 3464 | return Diag(atLoc, diag::error_objc_synchronized_expects_object) |
| 3465 | << type << operand->getSourceRange(); |
| 3466 | } |
| 3467 | } |
John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | // The operand to @synchronized is a full-expression. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 3471 | return ActOnFinishFullExpr(operand); |
John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3474 | StmtResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3475 | Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr, |
| 3476 | Stmt *SyncBody) { |
John McCall | d9bb743 | 2011-07-27 21:50:02 +0000 | [diff] [blame] | 3477 | // 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] | 3478 | getCurFunction()->setHasBranchProtectedScope(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3479 | return new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody); |
Fariborz Jahanian | 48085b8 | 2008-01-29 19:14:59 +0000 | [diff] [blame] | 3480 | } |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3481 | |
| 3482 | /// ActOnCXXCatchBlock - Takes an exception declaration and a handler block |
| 3483 | /// and creates a proper catch handler from them. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3484 | StmtResult |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3485 | Sema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3486 | Stmt *HandlerBlock) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3487 | // There's nothing to test that ActOnExceptionDecl didn't already test. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3488 | return new (Context) |
| 3489 | CXXCatchStmt(CatchLoc, cast_or_null<VarDecl>(ExDecl), HandlerBlock); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 3490 | } |
Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3491 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3492 | StmtResult |
| 3493 | Sema::ActOnObjCAutoreleasePoolStmt(SourceLocation AtLoc, Stmt *Body) { |
| 3494 | getCurFunction()->setHasBranchProtectedScope(); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3495 | return new (Context) ObjCAutoreleasePoolStmt(AtLoc, Body); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3498 | namespace { |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3499 | class CatchHandlerType { |
| 3500 | QualType QT; |
| 3501 | unsigned IsPointer : 1; |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 3502 | |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3503 | // This is a special constructor to be used only with DenseMapInfo's |
| 3504 | // getEmptyKey() and getTombstoneKey() functions. |
| 3505 | friend struct llvm::DenseMapInfo<CatchHandlerType>; |
| 3506 | enum Unique { ForDenseMap }; |
| 3507 | CatchHandlerType(QualType QT, Unique) : QT(QT), IsPointer(false) {} |
| 3508 | |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3509 | public: |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3510 | /// Used when creating a CatchHandlerType from a handler type; will determine |
Eric Christopher | 2c4555a | 2015-06-19 01:52:53 +0000 | [diff] [blame] | 3511 | /// whether the type is a pointer or reference and will strip off the top |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3512 | /// level pointer and cv-qualifiers. |
| 3513 | CatchHandlerType(QualType Q) : QT(Q), IsPointer(false) { |
| 3514 | if (QT->isPointerType()) |
| 3515 | IsPointer = true; |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3516 | |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3517 | if (IsPointer || QT->isReferenceType()) |
| 3518 | QT = QT->getPointeeType(); |
| 3519 | QT = QT.getUnqualifiedType(); |
| 3520 | } |
| 3521 | |
| 3522 | /// Used when creating a CatchHandlerType from a base class type; pretends the |
| 3523 | /// type passed in had the pointer qualifier, does not need to get an |
| 3524 | /// unqualified type. |
| 3525 | CatchHandlerType(QualType QT, bool IsPointer) |
| 3526 | : QT(QT), IsPointer(IsPointer) {} |
| 3527 | |
| 3528 | QualType underlying() const { return QT; } |
| 3529 | bool isPointer() const { return IsPointer; } |
| 3530 | |
| 3531 | friend bool operator==(const CatchHandlerType &LHS, |
| 3532 | const CatchHandlerType &RHS) { |
| 3533 | // If the pointer qualification does not match, we can return early. |
| 3534 | if (LHS.IsPointer != RHS.IsPointer) |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3535 | return false; |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3536 | // Otherwise, check the underlying type without cv-qualifiers. |
| 3537 | return LHS.QT == RHS.QT; |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3538 | } |
| 3539 | }; |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3540 | } // namespace |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3541 | |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3542 | namespace llvm { |
| 3543 | template <> struct DenseMapInfo<CatchHandlerType> { |
| 3544 | static CatchHandlerType getEmptyKey() { |
| 3545 | return CatchHandlerType(DenseMapInfo<QualType>::getEmptyKey(), |
| 3546 | CatchHandlerType::ForDenseMap); |
| 3547 | } |
| 3548 | |
| 3549 | static CatchHandlerType getTombstoneKey() { |
| 3550 | return CatchHandlerType(DenseMapInfo<QualType>::getTombstoneKey(), |
| 3551 | CatchHandlerType::ForDenseMap); |
| 3552 | } |
| 3553 | |
| 3554 | static unsigned getHashValue(const CatchHandlerType &Base) { |
| 3555 | return DenseMapInfo<QualType>::getHashValue(Base.underlying()); |
| 3556 | } |
| 3557 | |
| 3558 | static bool isEqual(const CatchHandlerType &LHS, |
| 3559 | const CatchHandlerType &RHS) { |
| 3560 | return LHS == RHS; |
| 3561 | } |
| 3562 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3563 | } |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3564 | |
| 3565 | namespace { |
| 3566 | class CatchTypePublicBases { |
| 3567 | ASTContext &Ctx; |
| 3568 | const llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> &TypesToCheck; |
| 3569 | const bool CheckAgainstPointer; |
| 3570 | |
| 3571 | CXXCatchStmt *FoundHandler; |
| 3572 | CanQualType FoundHandlerType; |
| 3573 | |
| 3574 | public: |
| 3575 | CatchTypePublicBases( |
| 3576 | ASTContext &Ctx, |
| 3577 | const llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> &T, bool C) |
| 3578 | : Ctx(Ctx), TypesToCheck(T), CheckAgainstPointer(C), |
| 3579 | FoundHandler(nullptr) {} |
| 3580 | |
| 3581 | CXXCatchStmt *getFoundHandler() const { return FoundHandler; } |
| 3582 | CanQualType getFoundHandlerType() const { return FoundHandlerType; } |
| 3583 | |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 3584 | bool operator()(const CXXBaseSpecifier *S, CXXBasePath &) { |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3585 | if (S->getAccessSpecifier() == AccessSpecifier::AS_public) { |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 3586 | CatchHandlerType Check(S->getType(), CheckAgainstPointer); |
Benjamin Kramer | 536ffdf | 2016-02-13 15:49:17 +0000 | [diff] [blame] | 3587 | const auto &M = TypesToCheck; |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3588 | auto I = M.find(Check); |
| 3589 | if (I != M.end()) { |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 3590 | FoundHandler = I->second; |
| 3591 | FoundHandlerType = Ctx.getCanonicalType(S->getType()); |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3592 | return true; |
| 3593 | } |
| 3594 | } |
| 3595 | return false; |
| 3596 | } |
| 3597 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3598 | } |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 3599 | |
Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3600 | /// ActOnCXXTryBlock - Takes a try compound-statement and a number of |
| 3601 | /// handlers and creates a try statement from them. |
Robert Wilhelm | cafda82 | 2013-08-22 09:20:03 +0000 | [diff] [blame] | 3602 | StmtResult Sema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock, |
| 3603 | ArrayRef<Stmt *> Handlers) { |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 3604 | // Don't report an error if 'try' is used in system headers. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3605 | if (!getLangOpts().CXXExceptions && |
Anders Carlsson | d99dbcc | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 3606 | !getSourceManager().isInSystemHeader(TryLoc)) |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3607 | Diag(TryLoc, diag::err_exceptions_disabled) << "try"; |
Anders Carlsson | 68b36af | 2011-02-19 19:26:44 +0000 | [diff] [blame] | 3608 | |
Alexander Musman | a8e9d2e | 2014-06-03 10:16:47 +0000 | [diff] [blame] | 3609 | if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope()) |
| 3610 | Diag(TryLoc, diag::err_omp_simd_region_cannot_use_stmt) << "try"; |
| 3611 | |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3612 | sema::FunctionScopeInfo *FSI = getCurFunction(); |
| 3613 | |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3614 | // C++ try is incompatible with SEH __try. |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3615 | if (!getLangOpts().Borland && FSI->FirstSEHTryLoc.isValid()) { |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3616 | Diag(TryLoc, diag::err_mixing_cxx_try_seh_try); |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3617 | Diag(FSI->FirstSEHTryLoc, diag::note_conflicting_try_here) << "'__try'"; |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Robert Wilhelm | cafda82 | 2013-08-22 09:20:03 +0000 | [diff] [blame] | 3620 | const unsigned NumHandlers = Handlers.size(); |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3621 | assert(!Handlers.empty() && |
Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3622 | "The parser shouldn't call this if there are no handlers."); |
Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3623 | |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3624 | llvm::DenseMap<CatchHandlerType, CXXCatchStmt *> HandledTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3625 | for (unsigned i = 0; i < NumHandlers; ++i) { |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3626 | CXXCatchStmt *H = cast<CXXCatchStmt>(Handlers[i]); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3628 | // Diagnose when the handler is a catch-all handler, but it isn't the last |
| 3629 | // handler for the try block. [except.handle]p5. Also, skip exception |
| 3630 | // declarations that are invalid, since we can't usefully report on them. |
| 3631 | if (!H->getExceptionDecl()) { |
| 3632 | if (i < NumHandlers - 1) |
| 3633 | return StmtError(Diag(H->getLocStart(), diag::err_early_catch_all)); |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3634 | continue; |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3635 | } else if (H->getExceptionDecl()->isInvalidDecl()) |
| 3636 | continue; |
| 3637 | |
| 3638 | // Walk the type hierarchy to diagnose when this type has already been |
| 3639 | // handled (duplication), or cannot be handled (derivation inversion). We |
| 3640 | // ignore top-level cv-qualifiers, per [except.handle]p3 |
Aaron Ballman | aa301de | 2015-04-08 00:13:33 +0000 | [diff] [blame] | 3641 | CatchHandlerType HandlerCHT = |
| 3642 | (QualType)Context.getCanonicalType(H->getCaughtType()); |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3643 | |
| 3644 | // We can ignore whether the type is a reference or a pointer; we need the |
| 3645 | // underlying declaration type in order to get at the underlying record |
| 3646 | // decl, if there is one. |
| 3647 | QualType Underlying = HandlerCHT.underlying(); |
| 3648 | if (auto *RD = Underlying->getAsCXXRecordDecl()) { |
| 3649 | if (!RD->hasDefinition()) |
| 3650 | continue; |
| 3651 | // Check that none of the public, unambiguous base classes are in the |
| 3652 | // map ([except.handle]p1). Give the base classes the same pointer |
| 3653 | // qualification as the original type we are basing off of. This allows |
| 3654 | // comparison against the handler type using the same top-level pointer |
| 3655 | // as the original type. |
| 3656 | CXXBasePaths Paths; |
| 3657 | Paths.setOrigin(RD); |
| 3658 | CatchTypePublicBases CTPB(Context, HandledTypes, HandlerCHT.isPointer()); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 3659 | if (RD->lookupInBases(CTPB, Paths)) { |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3660 | const CXXCatchStmt *Problem = CTPB.getFoundHandler(); |
| 3661 | if (!Paths.isAmbiguous(CTPB.getFoundHandlerType())) { |
| 3662 | Diag(H->getExceptionDecl()->getTypeSpecStartLoc(), |
| 3663 | diag::warn_exception_caught_by_earlier_handler) |
| 3664 | << H->getCaughtType(); |
| 3665 | Diag(Problem->getExceptionDecl()->getTypeSpecStartLoc(), |
| 3666 | diag::note_previous_exception_handler) |
| 3667 | << Problem->getCaughtType(); |
| 3668 | } |
| 3669 | } |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3670 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | |
Aaron Ballman | 8aee64290 | 2015-04-08 00:05:29 +0000 | [diff] [blame] | 3672 | // Add the type the list of ones we have handled; diagnose if we've already |
| 3673 | // handled it. |
| 3674 | auto R = HandledTypes.insert(std::make_pair(H->getCaughtType(), H)); |
| 3675 | if (!R.second) { |
| 3676 | const CXXCatchStmt *Problem = R.first->second; |
| 3677 | Diag(H->getExceptionDecl()->getTypeSpecStartLoc(), |
| 3678 | diag::warn_exception_caught_by_earlier_handler) |
| 3679 | << H->getCaughtType(); |
| 3680 | Diag(Problem->getExceptionDecl()->getTypeSpecStartLoc(), |
| 3681 | diag::note_previous_exception_handler) |
| 3682 | << Problem->getCaughtType(); |
Sebastian Redl | 63c4da0 | 2009-07-29 17:15:45 +0000 | [diff] [blame] | 3683 | } |
| 3684 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3685 | |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3686 | FSI->setHasCXXTry(TryLoc); |
John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 3687 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3688 | return CXXTryStmt::Create(Context, TryLoc, TryBlock, Handlers); |
Sebastian Redl | 9b244a8 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 3689 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3690 | |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3691 | StmtResult Sema::ActOnSEHTryBlock(bool IsCXXTry, SourceLocation TryLoc, |
| 3692 | Stmt *TryBlock, Stmt *Handler) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3693 | assert(TryBlock && Handler); |
| 3694 | |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3695 | sema::FunctionScopeInfo *FSI = getCurFunction(); |
| 3696 | |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3697 | // SEH __try is incompatible with C++ try. Borland appears to support this, |
| 3698 | // however. |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3699 | if (!getLangOpts().Borland) { |
| 3700 | if (FSI->FirstCXXTryLoc.isValid()) { |
| 3701 | Diag(TryLoc, diag::err_mixing_cxx_try_seh_try); |
| 3702 | Diag(FSI->FirstCXXTryLoc, diag::note_conflicting_try_here) << "'try'"; |
| 3703 | } |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3704 | } |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3705 | |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 3706 | FSI->setHasSEHTry(TryLoc); |
| 3707 | |
| 3708 | // Reject __try in Obj-C methods, blocks, and captured decls, since we don't |
| 3709 | // track if they use SEH. |
| 3710 | DeclContext *DC = CurContext; |
| 3711 | while (DC && !DC->isFunctionOrMethod()) |
| 3712 | DC = DC->getParent(); |
| 3713 | FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DC); |
| 3714 | if (FD) |
| 3715 | FD->setUsesSEHTry(true); |
| 3716 | else |
| 3717 | Diag(TryLoc, diag::err_seh_try_outside_functions); |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3718 | |
Reid Kleckner | 8819a40 | 2015-07-10 00:16:25 +0000 | [diff] [blame] | 3719 | // Reject __try on unsupported targets. |
| 3720 | if (!Context.getTargetInfo().isSEHTrySupported()) |
| 3721 | Diag(TryLoc, diag::err_seh_try_unsupported); |
| 3722 | |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 3723 | return SEHTryStmt::Create(Context, IsCXXTry, TryLoc, TryBlock, Handler); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | StmtResult |
| 3727 | Sema::ActOnSEHExceptBlock(SourceLocation Loc, |
| 3728 | Expr *FilterExpr, |
| 3729 | Stmt *Block) { |
| 3730 | assert(FilterExpr && Block); |
| 3731 | |
| 3732 | if(!FilterExpr->getType()->isIntegerType()) { |
Francois Pichet | fbf7e17 | 2011-06-02 00:47:27 +0000 | [diff] [blame] | 3733 | return StmtError(Diag(FilterExpr->getExprLoc(), |
| 3734 | diag::err_filter_expression_integral) |
| 3735 | << FilterExpr->getType()); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3738 | return SEHExceptStmt::Create(Context,Loc,FilterExpr,Block); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3741 | void Sema::ActOnStartSEHFinallyBlock() { |
| 3742 | CurrentSEHFinally.push_back(CurScope); |
| 3743 | } |
| 3744 | |
Nico Weber | ce90329 | 2015-03-09 03:17:15 +0000 | [diff] [blame] | 3745 | void Sema::ActOnAbortSEHFinallyBlock() { |
| 3746 | CurrentSEHFinally.pop_back(); |
| 3747 | } |
| 3748 | |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3749 | StmtResult Sema::ActOnFinishSEHFinallyBlock(SourceLocation Loc, Stmt *Block) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3750 | assert(Block); |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3751 | CurrentSEHFinally.pop_back(); |
| 3752 | return SEHFinallyStmt::Create(Context, Loc, Block); |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 3753 | } |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3754 | |
Nico Weber | c7d0596 | 2014-07-06 22:32:59 +0000 | [diff] [blame] | 3755 | StmtResult |
| 3756 | Sema::ActOnSEHLeaveStmt(SourceLocation Loc, Scope *CurScope) { |
Nico Weber | eb61d4d | 2014-07-06 22:53:19 +0000 | [diff] [blame] | 3757 | Scope *SEHTryParent = CurScope; |
| 3758 | while (SEHTryParent && !SEHTryParent->isSEHTryScope()) |
| 3759 | SEHTryParent = SEHTryParent->getParent(); |
| 3760 | if (!SEHTryParent) |
| 3761 | return StmtError(Diag(Loc, diag::err_ms___leave_not_in___try)); |
Nico Weber | d64657f | 2015-03-09 02:47:59 +0000 | [diff] [blame] | 3762 | CheckJumpOutOfSEHFinally(*this, Loc, *SEHTryParent); |
Nico Weber | eb61d4d | 2014-07-06 22:53:19 +0000 | [diff] [blame] | 3763 | |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 3764 | return new (Context) SEHLeaveStmt(Loc); |
Nico Weber | c7d0596 | 2014-07-06 22:32:59 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3767 | StmtResult Sema::BuildMSDependentExistsStmt(SourceLocation KeywordLoc, |
| 3768 | bool IsIfExists, |
| 3769 | NestedNameSpecifierLoc QualifierLoc, |
| 3770 | DeclarationNameInfo NameInfo, |
| 3771 | Stmt *Nested) |
| 3772 | { |
| 3773 | return new (Context) MSDependentExistsStmt(KeywordLoc, IsIfExists, |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3774 | QualifierLoc, NameInfo, |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3775 | cast<CompoundStmt>(Nested)); |
| 3776 | } |
| 3777 | |
| 3778 | |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3779 | StmtResult Sema::ActOnMSDependentExistsStmt(SourceLocation KeywordLoc, |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3780 | bool IsIfExists, |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3781 | CXXScopeSpec &SS, |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3782 | UnqualifiedId &Name, |
| 3783 | Stmt *Nested) { |
Chad Rosier | 02a8439 | 2012-08-10 17:56:09 +0000 | [diff] [blame] | 3784 | return BuildMSDependentExistsStmt(KeywordLoc, IsIfExists, |
Douglas Gregor | deb4a2be | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 3785 | SS.getWithLocInContext(Context), |
| 3786 | GetNameFromUnqualifiedId(Name), |
| 3787 | Nested); |
| 3788 | } |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3789 | |
| 3790 | RecordDecl* |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 3791 | Sema::CreateCapturedStmtRecordDecl(CapturedDecl *&CD, SourceLocation Loc, |
| 3792 | unsigned NumParams) { |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3793 | DeclContext *DC = CurContext; |
| 3794 | while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext())) |
| 3795 | DC = DC->getParent(); |
| 3796 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3797 | RecordDecl *RD = nullptr; |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3798 | if (getLangOpts().CPlusPlus) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3799 | RD = CXXRecordDecl::Create(Context, TTK_Struct, DC, Loc, Loc, |
| 3800 | /*Id=*/nullptr); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3801 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3802 | RD = RecordDecl::Create(Context, TTK_Struct, DC, Loc, Loc, /*Id=*/nullptr); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3803 | |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 3804 | RD->setCapturedRecord(); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3805 | DC->addDecl(RD); |
| 3806 | RD->setImplicit(); |
| 3807 | RD->startDefinition(); |
| 3808 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 3809 | assert(NumParams > 0 && "CapturedStmt requires context parameter"); |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 3810 | CD = CapturedDecl::Create(Context, CurContext, NumParams); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3811 | DC->addDecl(CD); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3812 | return RD; |
| 3813 | } |
| 3814 | |
| 3815 | static void buildCapturedStmtCaptureList( |
| 3816 | SmallVectorImpl<CapturedStmt::Capture> &Captures, |
| 3817 | SmallVectorImpl<Expr *> &CaptureInits, |
| 3818 | ArrayRef<CapturingScopeInfo::Capture> Candidates) { |
| 3819 | |
| 3820 | typedef ArrayRef<CapturingScopeInfo::Capture>::const_iterator CaptureIter; |
| 3821 | for (CaptureIter Cap = Candidates.begin(); Cap != Candidates.end(); ++Cap) { |
| 3822 | |
| 3823 | if (Cap->isThisCapture()) { |
| 3824 | Captures.push_back(CapturedStmt::Capture(Cap->getLocation(), |
| 3825 | CapturedStmt::VCK_This)); |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3826 | CaptureInits.push_back(Cap->getInitExpr()); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3827 | continue; |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 3828 | } else if (Cap->isVLATypeCapture()) { |
| 3829 | Captures.push_back( |
| 3830 | CapturedStmt::Capture(Cap->getLocation(), CapturedStmt::VCK_VLAType)); |
| 3831 | CaptureInits.push_back(nullptr); |
| 3832 | continue; |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3835 | Captures.push_back(CapturedStmt::Capture(Cap->getLocation(), |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 3836 | Cap->isReferenceCapture() |
| 3837 | ? CapturedStmt::VCK_ByRef |
| 3838 | : CapturedStmt::VCK_ByCopy, |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3839 | Cap->getVariable())); |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3840 | CaptureInits.push_back(Cap->getInitExpr()); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope, |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 3845 | CapturedRegionKind Kind, |
| 3846 | unsigned NumParams) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 3847 | CapturedDecl *CD = nullptr; |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 3848 | RecordDecl *RD = CreateCapturedStmtRecordDecl(CD, Loc, NumParams); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3849 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 3850 | // Build the context parameter |
| 3851 | DeclContext *DC = CapturedDecl::castToDeclContext(CD); |
| 3852 | IdentifierInfo *ParamName = &Context.Idents.get("__context"); |
| 3853 | QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD)); |
| 3854 | ImplicitParamDecl *Param |
| 3855 | = ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType); |
| 3856 | DC->addDecl(Param); |
| 3857 | |
| 3858 | CD->setContextParam(0, Param); |
| 3859 | |
| 3860 | // Enter the capturing scope for this captured region. |
| 3861 | PushCapturedRegionScope(CurScope, CD, RD, Kind); |
| 3862 | |
| 3863 | if (CurScope) |
| 3864 | PushDeclContext(CurScope, CD); |
| 3865 | else |
| 3866 | CurContext = CD; |
| 3867 | |
| 3868 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 3869 | } |
| 3870 | |
| 3871 | void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope, |
| 3872 | CapturedRegionKind Kind, |
| 3873 | ArrayRef<CapturedParamNameType> Params) { |
| 3874 | CapturedDecl *CD = nullptr; |
| 3875 | RecordDecl *RD = CreateCapturedStmtRecordDecl(CD, Loc, Params.size()); |
| 3876 | |
| 3877 | // Build the context parameter |
| 3878 | DeclContext *DC = CapturedDecl::castToDeclContext(CD); |
| 3879 | bool ContextIsFound = false; |
| 3880 | unsigned ParamNum = 0; |
| 3881 | for (ArrayRef<CapturedParamNameType>::iterator I = Params.begin(), |
| 3882 | E = Params.end(); |
| 3883 | I != E; ++I, ++ParamNum) { |
| 3884 | if (I->second.isNull()) { |
| 3885 | assert(!ContextIsFound && |
| 3886 | "null type has been found already for '__context' parameter"); |
| 3887 | IdentifierInfo *ParamName = &Context.Idents.get("__context"); |
| 3888 | QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD)); |
| 3889 | ImplicitParamDecl *Param |
| 3890 | = ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType); |
| 3891 | DC->addDecl(Param); |
| 3892 | CD->setContextParam(ParamNum, Param); |
| 3893 | ContextIsFound = true; |
| 3894 | } else { |
| 3895 | IdentifierInfo *ParamName = &Context.Idents.get(I->first); |
| 3896 | ImplicitParamDecl *Param |
| 3897 | = ImplicitParamDecl::Create(Context, DC, Loc, ParamName, I->second); |
| 3898 | DC->addDecl(Param); |
| 3899 | CD->setParam(ParamNum, Param); |
| 3900 | } |
| 3901 | } |
| 3902 | assert(ContextIsFound && "no null type for '__context' parameter"); |
Alexey Bataev | 301a2d9 | 2014-05-14 10:40:54 +0000 | [diff] [blame] | 3903 | if (!ContextIsFound) { |
| 3904 | // Add __context implicitly if it is not specified. |
| 3905 | IdentifierInfo *ParamName = &Context.Idents.get("__context"); |
| 3906 | QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD)); |
| 3907 | ImplicitParamDecl *Param = |
| 3908 | ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType); |
| 3909 | DC->addDecl(Param); |
| 3910 | CD->setContextParam(ParamNum, Param); |
| 3911 | } |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3912 | // Enter the capturing scope for this captured region. |
| 3913 | PushCapturedRegionScope(CurScope, CD, RD, Kind); |
| 3914 | |
| 3915 | if (CurScope) |
| 3916 | PushDeclContext(CurScope, CD); |
| 3917 | else |
| 3918 | CurContext = CD; |
| 3919 | |
| 3920 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 3921 | } |
| 3922 | |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 3923 | void Sema::ActOnCapturedRegionError() { |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3924 | DiscardCleanupsInEvaluationContext(); |
| 3925 | PopExpressionEvaluationContext(); |
| 3926 | |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3927 | CapturedRegionScopeInfo *RSI = getCurCapturedRegion(); |
| 3928 | RecordDecl *Record = RSI->TheRecordDecl; |
| 3929 | Record->setInvalidDecl(); |
| 3930 | |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 3931 | SmallVector<Decl*, 4> Fields(Record->fields()); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 3932 | ActOnFields(/*Scope=*/nullptr, Record->getLocation(), Record, Fields, |
| 3933 | SourceLocation(), SourceLocation(), /*AttributeList=*/nullptr); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3934 | |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 3935 | PopDeclContext(); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3936 | PopFunctionScopeInfo(); |
| 3937 | } |
| 3938 | |
| 3939 | StmtResult Sema::ActOnCapturedRegionEnd(Stmt *S) { |
| 3940 | CapturedRegionScopeInfo *RSI = getCurCapturedRegion(); |
| 3941 | |
| 3942 | SmallVector<CapturedStmt::Capture, 4> Captures; |
| 3943 | SmallVector<Expr *, 4> CaptureInits; |
| 3944 | buildCapturedStmtCaptureList(Captures, CaptureInits, RSI->Captures); |
| 3945 | |
| 3946 | CapturedDecl *CD = RSI->TheCapturedDecl; |
| 3947 | RecordDecl *RD = RSI->TheRecordDecl; |
| 3948 | |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 3949 | CapturedStmt *Res = CapturedStmt::Create(getASTContext(), S, |
| 3950 | RSI->CapRegionKind, Captures, |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3951 | CaptureInits, CD, RD); |
| 3952 | |
| 3953 | CD->setBody(Res->getCapturedStmt()); |
| 3954 | RD->completeDefinition(); |
| 3955 | |
Wei Pan | 17fbf6e | 2013-05-04 03:59:06 +0000 | [diff] [blame] | 3956 | DiscardCleanupsInEvaluationContext(); |
| 3957 | PopExpressionEvaluationContext(); |
| 3958 | |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3959 | PopDeclContext(); |
| 3960 | PopFunctionScopeInfo(); |
| 3961 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3962 | return Res; |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3963 | } |