diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2015-11-11 01:36:17 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2015-11-11 01:36:17 +0000 |
commit | 42b10572443e3f3f3c4a113f88ad4e9b504df900 (patch) | |
tree | b826b89beb307ed24b6ffedf840c5605ccabb3f4 /clang/lib | |
parent | 754cd11d9019e62a4c6a0aee62159e707f6f51d9 (diff) | |
download | bcm5719-llvm-42b10572443e3f3f3c4a113f88ad4e9b504df900.tar.gz bcm5719-llvm-42b10572443e3f3f3c4a113f88ad4e9b504df900.zip |
N3922: direct-list-initialization of an auto-typed variable no longer deduces a
std::initializer_list<T> type. Instead, the list must contain a single element
and the type is deduced from that.
In Clang 3.7, we warned by default on all the cases that would change meaning
due to this change. In Clang 3.8, we will support only the new rules -- per
the request in N3922, this change is applied as a Defect Report against earlier
versions of the C++ standard.
This change is not entirely trivial, because for lambda init-captures we
previously did not track the difference between direct-list-initialization and
copy-list-initialization. The difference was not previously observable, because
the two forms of initialization always did the same thing (the elements of the
initializer list were always copy-initialized regardless of the initialization
style used for the init-capture).
llvm-svn: 252688
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Parse/ParseExprCXX.cpp | 24 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 197 | ||||
-rw-r--r-- | clang/lib/Sema/SemaLambda.cpp | 96 | ||||
-rw-r--r-- | clang/lib/Sema/TreeTransform.h | 11 |
4 files changed, 165 insertions, 163 deletions
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp index d5f188104ed..229b76adde8 100644 --- a/clang/lib/Parse/ParseExprCXX.cpp +++ b/clang/lib/Parse/ParseExprCXX.cpp @@ -841,6 +841,7 @@ Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro, // Parse capture. LambdaCaptureKind Kind = LCK_ByCopy; + LambdaCaptureInitKind InitKind = LambdaCaptureInitKind::NoInit; SourceLocation Loc; IdentifierInfo *Id = nullptr; SourceLocation EllipsisLoc; @@ -878,6 +879,8 @@ Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro, BalancedDelimiterTracker Parens(*this, tok::l_paren); Parens.consumeOpen(); + InitKind = LambdaCaptureInitKind::DirectInit; + ExprVector Exprs; CommaLocsTy Commas; if (SkippedInits) { @@ -898,14 +901,13 @@ Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro, // to save the necessary state, and restore it later. EnterExpressionEvaluationContext EC(Actions, Sema::PotentiallyEvaluated); - bool HadEquals = TryConsumeToken(tok::equal); + + if (TryConsumeToken(tok::equal)) + InitKind = LambdaCaptureInitKind::CopyInit; + else + InitKind = LambdaCaptureInitKind::ListInit; if (!SkippedInits) { - // Warn on constructs that will change meaning when we implement N3922 - if (!HadEquals && Tok.is(tok::l_brace)) { - Diag(Tok, diag::warn_init_capture_direct_list_init) - << FixItHint::CreateInsertion(Tok.getLocation(), "="); - } Init = ParseInitializer(); } else if (Tok.is(tok::l_brace)) { BalancedDelimiterTracker Braces(*this, tok::l_brace); @@ -993,19 +995,19 @@ Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro, // If x was not const, the second use would require 'L' to capture, and // that would be an error. - ParsedType InitCaptureParsedType; + ParsedType InitCaptureType; if (Init.isUsable()) { // Get the pointer and store it in an lvalue, so we can use it as an // out argument. Expr *InitExpr = Init.get(); // This performs any lvalue-to-rvalue conversions if necessary, which // can affect what gets captured in the containing decl-context. - QualType InitCaptureType = Actions.performLambdaInitCaptureInitialization( - Loc, Kind == LCK_ByRef, Id, InitExpr); + InitCaptureType = Actions.actOnLambdaInitCaptureInitialization( + Loc, Kind == LCK_ByRef, Id, InitKind, InitExpr); Init = InitExpr; - InitCaptureParsedType.set(InitCaptureType); } - Intro.addCapture(Kind, Loc, Id, EllipsisLoc, Init, InitCaptureParsedType); + Intro.addCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init, + InitCaptureType); } T.consumeClose(); diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 92bbadd627d..ed86e854894 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -9012,6 +9012,96 @@ namespace { } } +QualType Sema::deduceVarTypeFromInitializer(VarDecl *VDecl, + DeclarationName Name, QualType Type, + TypeSourceInfo *TSI, + SourceRange Range, bool DirectInit, + Expr *Init) { + bool IsInitCapture = !VDecl; + assert((!VDecl || !VDecl->isInitCapture()) && + "init captures are expected to be deduced prior to initialization"); + + ArrayRef<Expr *> DeduceInits = Init; + if (DirectInit) { + if (auto *PL = dyn_cast<ParenListExpr>(Init)) + DeduceInits = PL->exprs(); + else if (auto *IL = dyn_cast<InitListExpr>(Init)) + DeduceInits = IL->inits(); + } + + // Deduction only works if we have exactly one source expression. + if (DeduceInits.empty()) { + // It isn't possible to write this directly, but it is possible to + // end up in this situation with "auto x(some_pack...);" + Diag(Init->getLocStart(), IsInitCapture + ? diag::err_init_capture_no_expression + : diag::err_auto_var_init_no_expression) + << Name << Type << Range; + return QualType(); + } + + if (DeduceInits.size() > 1) { + Diag(DeduceInits[1]->getLocStart(), + IsInitCapture ? diag::err_init_capture_multiple_expressions + : diag::err_auto_var_init_multiple_expressions) + << Name << Type << Range; + return QualType(); + } + + Expr *DeduceInit = DeduceInits[0]; + if (DirectInit && isa<InitListExpr>(DeduceInit)) { + Diag(Init->getLocStart(), IsInitCapture + ? diag::err_init_capture_paren_braces + : diag::err_auto_var_init_paren_braces) + << isa<InitListExpr>(Init) << Name << Type << Range; + return QualType(); + } + + // Expressions default to 'id' when we're in a debugger. + bool DefaultedAnyToId = false; + if (getLangOpts().DebuggerCastResultToId && + Init->getType() == Context.UnknownAnyTy && !IsInitCapture) { + ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); + if (Result.isInvalid()) { + return QualType(); + } + Init = Result.get(); + DefaultedAnyToId = true; + } + + QualType DeducedType; + if (DeduceAutoType(TSI, DeduceInit, DeducedType) == DAR_Failed) { + if (!IsInitCapture) + DiagnoseAutoDeductionFailure(VDecl, DeduceInit); + else if (isa<InitListExpr>(Init)) + Diag(Range.getBegin(), + diag::err_init_capture_deduction_failure_from_init_list) + << Name + << (DeduceInit->getType().isNull() ? TSI->getType() + : DeduceInit->getType()) + << DeduceInit->getSourceRange(); + else + Diag(Range.getBegin(), diag::err_init_capture_deduction_failure) + << Name << TSI->getType() + << (DeduceInit->getType().isNull() ? TSI->getType() + : DeduceInit->getType()) + << DeduceInit->getSourceRange(); + } + + // Warn if we deduced 'id'. 'auto' usually implies type-safety, but using + // 'id' instead of a specific object type prevents most of our usual + // checks. + // We only want to warn outside of template instantiations, though: + // inside a template, the 'id' could have come from a parameter. + if (ActiveTemplateInstantiations.empty() && !DefaultedAnyToId && + !IsInitCapture && !DeducedType.isNull() && DeducedType->isObjCIdType()) { + SourceLocation Loc = TSI->getTypeLoc().getBeginLoc(); + Diag(Loc, diag::warn_auto_var_is_id) << Name << Range; + } + + return DeducedType; +} + /// AddInitializerToDecl - Adds the initializer Init to the /// declaration dcl. If DirectInit is true, this is C++ direct /// initialization rather than copy initialization. @@ -9039,79 +9129,27 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, RealDecl->setInvalidDecl(); return; } - ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); // C++11 [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. if (TypeMayContainAuto && VDecl->getType()->isUndeducedType()) { // Attempt typo correction early so that the type of the init expression can - // be deduced based on the chosen correction:if the original init contains a + // be deduced based on the chosen correction if the original init contains a // TypoExpr. ExprResult Res = CorrectDelayedTyposInExpr(Init, VDecl); if (!Res.isUsable()) { RealDecl->setInvalidDecl(); return; } + Init = Res.get(); - if (Res.get() != Init) { - Init = Res.get(); - if (CXXDirectInit) - CXXDirectInit = dyn_cast<ParenListExpr>(Init); - } - - Expr *DeduceInit = Init; - // Initializer could be a C++ direct-initializer. Deduction only works if it - // contains exactly one expression. - if (CXXDirectInit) { - if (CXXDirectInit->getNumExprs() == 0) { - // It isn't possible to write this directly, but it is possible to - // end up in this situation with "auto x(some_pack...);" - Diag(CXXDirectInit->getLocStart(), - VDecl->isInitCapture() ? diag::err_init_capture_no_expression - : diag::err_auto_var_init_no_expression) - << VDecl->getDeclName() << VDecl->getType() - << VDecl->getSourceRange(); - RealDecl->setInvalidDecl(); - return; - } else if (CXXDirectInit->getNumExprs() > 1) { - Diag(CXXDirectInit->getExpr(1)->getLocStart(), - VDecl->isInitCapture() - ? diag::err_init_capture_multiple_expressions - : diag::err_auto_var_init_multiple_expressions) - << VDecl->getDeclName() << VDecl->getType() - << VDecl->getSourceRange(); - RealDecl->setInvalidDecl(); - return; - } else { - DeduceInit = CXXDirectInit->getExpr(0); - if (isa<InitListExpr>(DeduceInit)) - Diag(CXXDirectInit->getLocStart(), - diag::err_auto_var_init_paren_braces) - << VDecl->getDeclName() << VDecl->getType() - << VDecl->getSourceRange(); - } - } - - // Expressions default to 'id' when we're in a debugger. - bool DefaultedToAuto = false; - if (getLangOpts().DebuggerCastResultToId && - Init->getType() == Context.UnknownAnyTy) { - ExprResult Result = forceUnknownAnyToType(Init, Context.getObjCIdType()); - if (Result.isInvalid()) { - VDecl->setInvalidDecl(); - return; - } - Init = Result.get(); - DefaultedToAuto = true; - } - - QualType DeducedType; - if (DeduceAutoType(VDecl->getTypeSourceInfo(), DeduceInit, DeducedType) == - DAR_Failed) - DiagnoseAutoDeductionFailure(VDecl, DeduceInit); + QualType DeducedType = deduceVarTypeFromInitializer( + VDecl, VDecl->getDeclName(), VDecl->getType(), + VDecl->getTypeSourceInfo(), VDecl->getSourceRange(), DirectInit, Init); if (DeducedType.isNull()) { RealDecl->setInvalidDecl(); return; } + VDecl->setType(DeducedType); assert(VDecl->isLinkageValid()); @@ -9119,38 +9157,18 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) VDecl->setInvalidDecl(); - // Warn if we deduced 'id'. 'auto' usually implies type-safety, but using - // 'id' instead of a specific object type prevents most of our usual checks. - // We only want to warn outside of template instantiations, though: - // inside a template, the 'id' could have come from a parameter. - if (ActiveTemplateInstantiations.empty() && !DefaultedToAuto && - DeducedType->isObjCIdType()) { - SourceLocation Loc = - VDecl->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); - Diag(Loc, diag::warn_auto_var_is_id) - << VDecl->getDeclName() << DeduceInit->getSourceRange(); - } - // If this is a redeclaration, check that the type we just deduced matches // the previously declared type. if (VarDecl *Old = VDecl->getPreviousDecl()) { // We never need to merge the type, because we cannot form an incomplete // array of auto, nor deduce such a type. - MergeVarDeclTypes(VDecl, Old, /*MergeTypeWithPrevious*/false); + MergeVarDeclTypes(VDecl, Old, /*MergeTypeWithPrevious*/ false); } // Check the deduced type is valid for a variable declaration. CheckVariableDeclarationType(VDecl); if (VDecl->isInvalidDecl()) return; - - // If all looks well, warn if this is a case that will change meaning when - // we implement N3922. - if (DirectInit && !CXXDirectInit && isa<InitListExpr>(Init)) { - Diag(Init->getLocStart(), - diag::warn_auto_var_direct_list_init) - << FixItHint::CreateInsertion(Init->getLocStart(), "="); - } } // dllimport cannot be used on variable definitions. @@ -9262,17 +9280,18 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, } // Perform the initialization. + ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); if (!VDecl->isInvalidDecl()) { InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); - InitializationKind Kind - = DirectInit ? - CXXDirectInit ? InitializationKind::CreateDirect(VDecl->getLocation(), - Init->getLocStart(), - Init->getLocEnd()) - : InitializationKind::CreateDirectList( - VDecl->getLocation()) - : InitializationKind::CreateCopy(VDecl->getLocation(), - Init->getLocStart()); + InitializationKind Kind = + DirectInit + ? CXXDirectInit + ? InitializationKind::CreateDirect(VDecl->getLocation(), + Init->getLocStart(), + Init->getLocEnd()) + : InitializationKind::CreateDirectList(VDecl->getLocation()) + : InitializationKind::CreateCopy(VDecl->getLocation(), + Init->getLocStart()); MultiExprArg Args = Init; if (CXXDirectInit) @@ -9336,7 +9355,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, if (VDecl->getType().getObjCLifetime() == Qualifiers::OCL_Strong && !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Init->getLocStart())) - getCurFunction()->markSafeWeakUse(Init); + getCurFunction()->markSafeWeakUse(Init); } // The initialization is usually a full-expression. diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp index a8f109df284..80d497f4e50 100644 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -699,18 +699,11 @@ void Sema::deduceClosureReturnType(CapturingScopeInfo &CSI) { } } -QualType Sema::performLambdaInitCaptureInitialization(SourceLocation Loc, - bool ByRef, - IdentifierInfo *Id, - Expr *&Init) { - - // We do not need to distinguish between direct-list-initialization - // and copy-list-initialization here, because we will always deduce - // std::initializer_list<T>, and direct- and copy-list-initialization - // always behave the same for such a type. - // FIXME: We should model whether an '=' was present. - const bool IsDirectInit = isa<ParenListExpr>(Init) || isa<InitListExpr>(Init); - +QualType Sema::buildLambdaInitCaptureInitialization(SourceLocation Loc, + bool ByRef, + IdentifierInfo *Id, + bool IsDirectInit, + Expr *&Init) { // Create an 'auto' or 'auto&' TypeSourceInfo that we can use to // deduce against. QualType DeductType = Context.getAutoDeductType(); @@ -723,50 +716,16 @@ QualType Sema::performLambdaInitCaptureInitialization(SourceLocation Loc, } TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, DeductType); - // Are we a non-list direct initialization? - ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); - - Expr *DeduceInit = Init; - // Initializer could be a C++ direct-initializer. Deduction only works if it - // contains exactly one expression. - if (CXXDirectInit) { - if (CXXDirectInit->getNumExprs() == 0) { - Diag(CXXDirectInit->getLocStart(), diag::err_init_capture_no_expression) - << DeclarationName(Id) << TSI->getType() << Loc; - return QualType(); - } else if (CXXDirectInit->getNumExprs() > 1) { - Diag(CXXDirectInit->getExpr(1)->getLocStart(), - diag::err_init_capture_multiple_expressions) - << DeclarationName(Id) << TSI->getType() << Loc; - return QualType(); - } else { - DeduceInit = CXXDirectInit->getExpr(0); - if (isa<InitListExpr>(DeduceInit)) - Diag(CXXDirectInit->getLocStart(), diag::err_init_capture_paren_braces) - << DeclarationName(Id) << Loc; - } - } - - // Now deduce against the initialization expression and store the deduced - // type below. - QualType DeducedType; - if (DeduceAutoType(TSI, DeduceInit, DeducedType) == DAR_Failed) { - if (isa<InitListExpr>(Init)) - Diag(Loc, diag::err_init_capture_deduction_failure_from_init_list) - << DeclarationName(Id) - << (DeduceInit->getType().isNull() ? TSI->getType() - : DeduceInit->getType()) - << DeduceInit->getSourceRange(); - else - Diag(Loc, diag::err_init_capture_deduction_failure) - << DeclarationName(Id) << TSI->getType() - << (DeduceInit->getType().isNull() ? TSI->getType() - : DeduceInit->getType()) - << DeduceInit->getSourceRange(); - } + // Deduce the type of the init capture. + QualType DeducedType = deduceVarTypeFromInitializer( + /*VarDecl*/nullptr, DeclarationName(Id), DeductType, TSI, + SourceRange(Loc, Loc), IsDirectInit, Init); if (DeducedType.isNull()) return QualType(); + // Are we a non-list direct initialization? + ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); + // Perform initialization analysis and ensure any implicit conversions // (such as lvalue-to-rvalue) are enforced. InitializedEntity Entity = @@ -803,9 +762,10 @@ QualType Sema::performLambdaInitCaptureInitialization(SourceLocation Loc, return DeducedType; } -VarDecl *Sema::createLambdaInitCaptureVarDecl(SourceLocation Loc, - QualType InitCaptureType, IdentifierInfo *Id, Expr *Init) { - +VarDecl *Sema::createLambdaInitCaptureVarDecl(SourceLocation Loc, + QualType InitCaptureType, + IdentifierInfo *Id, + unsigned InitStyle, Expr *Init) { TypeSourceInfo *TSI = Context.getTrivialTypeSourceInfo(InitCaptureType, Loc); // Create a dummy variable representing the init-capture. This is not actually @@ -816,6 +776,8 @@ VarDecl *Sema::createLambdaInitCaptureVarDecl(SourceLocation Loc, Loc, Id, InitCaptureType, TSI, SC_Auto); NewVD->setInitCapture(true); NewVD->setReferenced(true); + // FIXME: Pass in a VarDecl::InitializationStyle. + NewVD->setInitStyle(static_cast<VarDecl::InitializationStyle>(InitStyle)); NewVD->markUsed(Context); NewVD->setInit(Init); return NewVD; @@ -1014,8 +976,23 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, // in this case. if (C->InitCaptureType.get().isNull()) continue; - Var = createLambdaInitCaptureVarDecl(C->Loc, C->InitCaptureType.get(), - C->Id, C->Init.get()); + + unsigned InitStyle; + switch (C->InitKind) { + case LambdaCaptureInitKind::NoInit: + llvm_unreachable("not an init-capture?"); + case LambdaCaptureInitKind::CopyInit: + InitStyle = VarDecl::CInit; + break; + case LambdaCaptureInitKind::DirectInit: + InitStyle = VarDecl::CallInit; + break; + case LambdaCaptureInitKind::ListInit: + InitStyle = VarDecl::ListInit; + break; + } + Var = createLambdaInitCaptureVarDecl(C->Loc, C->InitCaptureType.get(), + C->Id, InitStyle, C->Init.get()); // C++1y [expr.prim.lambda]p11: // An init-capture behaves as if it declares and explicitly // captures a variable [...] whose declarative region is the @@ -1023,6 +1000,9 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, if (Var) PushOnScopeChains(Var, CurScope, false); } else { + assert(C->InitKind == LambdaCaptureInitKind::NoInit && + "init capture has valid but null init?"); + // C++11 [expr.prim.lambda]p8: // If a lambda-capture includes a capture-default that is &, the // identifiers in the lambda-capture shall not be preceded by &. diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 176e57165ff..12f18c7489d 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -9623,9 +9623,10 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) { VarDecl *OldVD = C->getCapturedVar(); QualType NewInitCaptureType = - getSema().performLambdaInitCaptureInitialization(C->getLocation(), - OldVD->getType()->isReferenceType(), OldVD->getIdentifier(), - NewExprInit); + getSema().buildLambdaInitCaptureInitialization( + C->getLocation(), OldVD->getType()->isReferenceType(), + OldVD->getIdentifier(), + C->getCapturedVar()->getInitStyle() != VarDecl::CInit, NewExprInit); NewExprInitResult = NewExprInit; InitCaptureExprsAndTypes[C - E->capture_begin()] = std::make_pair(NewExprInitResult, NewInitCaptureType); @@ -9732,8 +9733,8 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) { } VarDecl *OldVD = C->getCapturedVar(); VarDecl *NewVD = getSema().createLambdaInitCaptureVarDecl( - OldVD->getLocation(), InitExprTypePair.second, - OldVD->getIdentifier(), Init.get()); + OldVD->getLocation(), InitExprTypePair.second, OldVD->getIdentifier(), + OldVD->getInitStyle(), Init.get()); if (!NewVD) Invalid = true; else { |