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