[NFC] Remove implicit `this` capture in lambdas (#6761)

When declaring a lambda with a value-capture default [=, ...], the this
pointer is implicitly captured by value as well. This results in
potentially-unintuitive behavior and has been deprecated in C++20. It
produces a warning in newer versions of clang
(https://reviews.llvm.org/D142639).

This PR makes the implicit captures explicit, preventing the warning. It
does not change the compiled code at all, since it's just removing some
syntactic sugar.
This commit is contained in:
Devon Loehr 2024-08-01 14:50:30 -04:00 коммит произвёл GitHub
Родитель 8b33431849
Коммит 5ecee0b599
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 86 добавлений и 75 удалений

Просмотреть файл

@ -341,7 +341,7 @@ namespace {
for (QualType PT : T->getParamTypes()) for (QualType PT : T->getParamTypes())
dumpTypeAsChild(PT); dumpTypeAsChild(PT);
if (EPI.Variadic) if (EPI.Variadic)
dumpChild([=] { OS << "..."; }); dumpChild([this] { OS << "..."; });
} }
void VisitUnresolvedUsingType(const UnresolvedUsingType *T) { void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
dumpDeclRef(T->getDecl()); dumpDeclRef(T->getDecl());
@ -646,7 +646,7 @@ void ASTDumper::dumpTypeAsChild(QualType T) {
if (!SQT.Quals.hasQualifiers()) if (!SQT.Quals.hasQualifiers())
return dumpTypeAsChild(SQT.Ty); return dumpTypeAsChild(SQT.Ty);
dumpChild([=] { dumpChild([this, T] {
OS << "QualType"; OS << "QualType";
dumpPointer(T.getAsOpaquePtr()); dumpPointer(T.getAsOpaquePtr());
OS << " "; OS << " ";
@ -657,7 +657,7 @@ void ASTDumper::dumpTypeAsChild(QualType T) {
} }
void ASTDumper::dumpTypeAsChild(const Type *T) { void ASTDumper::dumpTypeAsChild(const Type *T) {
dumpChild([=] { dumpChild([this, T] {
if (!T) { if (!T) {
ColorScope Color(*this, NullColor); ColorScope Color(*this, NullColor);
OS << "<<<NULL>>>"; OS << "<<<NULL>>>";
@ -714,7 +714,7 @@ void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
if (!D) if (!D)
return; return;
dumpChild([=]{ dumpChild([this, Label, D] {
if (Label) if (Label)
OS << Label << ' '; OS << Label << ' ';
dumpBareDeclRef(D); dumpBareDeclRef(D);
@ -748,7 +748,7 @@ void ASTDumper::dumpDeclContext(const DeclContext *DC) {
// HLSL Change Ends // HLSL Change Ends
if (DC->hasExternalLexicalStorage()) { if (DC->hasExternalLexicalStorage()) {
dumpChild([=]{ dumpChild([this] {
ColorScope Color(*this, UndeserializedColor); ColorScope Color(*this, UndeserializedColor);
OS << "<undeserialized declarations>"; OS << "<undeserialized declarations>";
}); });
@ -756,7 +756,7 @@ void ASTDumper::dumpDeclContext(const DeclContext *DC) {
} }
void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) { void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
dumpChild([=] { dumpChild([this, DC, DumpDecls] {
OS << "StoredDeclsMap "; OS << "StoredDeclsMap ";
dumpBareDeclRef(cast<Decl>(DC)); dumpBareDeclRef(cast<Decl>(DC));
@ -774,7 +774,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
DeclarationName Name = I.getLookupName(); DeclarationName Name = I.getLookupName();
DeclContextLookupResult R = *I++; DeclContextLookupResult R = *I++;
dumpChild([=] { dumpChild([this, Name, R, DumpDecls] {
OS << "DeclarationName "; OS << "DeclarationName ";
{ {
ColorScope Color(*this, DeclNameColor); ColorScope Color(*this, DeclNameColor);
@ -783,7 +783,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end(); for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
RI != RE; ++RI) { RI != RE; ++RI) {
dumpChild([=] { dumpChild([this, RI, DumpDecls] {
dumpBareDeclRef(*RI); dumpBareDeclRef(*RI);
if ((*RI)->isHidden()) if ((*RI)->isHidden())
@ -805,7 +805,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
} }
if (HasUndeserializedLookups) { if (HasUndeserializedLookups) {
dumpChild([=] { dumpChild([this] {
ColorScope Color(*this, UndeserializedColor); ColorScope Color(*this, UndeserializedColor);
OS << "<undeserialized lookups>"; OS << "<undeserialized lookups>";
}); });
@ -814,7 +814,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
} }
void ASTDumper::dumpAttr(const Attr *A) { void ASTDumper::dumpAttr(const Attr *A) {
dumpChild([=] { dumpChild([this, A] {
{ {
ColorScope Color(*this, AttrColor); ColorScope Color(*this, AttrColor);
@ -886,7 +886,7 @@ void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
} }
void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) { void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
dumpChild([=] { dumpChild([this, Init] {
OS << "CXXCtorInitializer"; OS << "CXXCtorInitializer";
if (Init->isAnyMemberInitializer()) { if (Init->isAnyMemberInitializer()) {
OS << ' '; OS << ' ';
@ -927,7 +927,7 @@ void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
} }
void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) { void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
dumpChild([=] { dumpChild([this, A, R] {
OS << "TemplateArgument"; OS << "TemplateArgument";
if (R.isValid()) if (R.isValid())
dumpSourceRange(R); dumpSourceRange(R);
@ -993,19 +993,23 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRef<hlsl::UnusualAnnotatio
{ {
for (auto It = UA.begin(), E = UA.end(); It != E; ++It) for (auto It = UA.begin(), E = UA.end(); It != E; ++It)
{ {
dumpChild([=] { dumpChild([this, It] {
{ {
ColorScope Color(*this, AttrColor); ColorScope Color(*this, AttrColor);
switch ((*It)->getKind()) switch ((*It)->getKind())
{ {
case hlsl::UnusualAnnotation::UA_ConstantPacking: case hlsl::UnusualAnnotation::UA_ConstantPacking:
OS << "ConstantPacking"; break; OS << "ConstantPacking";
case hlsl::UnusualAnnotation::UA_RegisterAssignment: break;
OS << "RegisterAssignment"; break; case hlsl::UnusualAnnotation::UA_RegisterAssignment:
case hlsl::UnusualAnnotation::UA_SemanticDecl: OS << "RegisterAssignment";
OS << "SemanticDecl"; break; break;
case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier: case hlsl::UnusualAnnotation::UA_SemanticDecl:
OS << "PayloadAccessQualifier"; break; OS << "SemanticDecl";
break;
case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier:
OS << "PayloadAccessQualifier";
break;
} }
} }
dumpPointer(It); dumpPointer(It);
@ -1014,48 +1018,52 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRef<hlsl::UnusualAnnotatio
switch ((*It)->getKind()) switch ((*It)->getKind())
{ {
case hlsl::UnusualAnnotation::UA_ConstantPacking: { case hlsl::UnusualAnnotation::UA_ConstantPacking: {
const hlsl::ConstantPacking* constantPacking = cast<hlsl::ConstantPacking>(*It); const hlsl::ConstantPacking *constantPacking =
OS << " packoffset(c"; cast<hlsl::ConstantPacking>(*It);
OS << constantPacking->Subcomponent; OS << " packoffset(c";
OS << "."; OS << constantPacking->Subcomponent;
const char *xyzw[4] = { "x", "y", "z", "w" }; OS << ".";
if(constantPacking->ComponentOffset < 4) const char *xyzw[4] = {"x", "y", "z", "w"};
OS << xyzw[constantPacking->ComponentOffset]; if (constantPacking->ComponentOffset < 4)
else OS << xyzw[constantPacking->ComponentOffset];
OS << "<invalid>"; else
OS << ")"; OS << "<invalid>";
if (!constantPacking->IsValid) OS << ")";
OS << " invalid"; if (!constantPacking->IsValid)
break; OS << " invalid";
} break;
}
case hlsl::UnusualAnnotation::UA_RegisterAssignment: { case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
const hlsl::RegisterAssignment* registerAssignment = cast<hlsl::RegisterAssignment>(*It); const hlsl::RegisterAssignment *registerAssignment =
OS << " register("; cast<hlsl::RegisterAssignment>(*It);
if (!registerAssignment->ShaderProfile.empty()) OS << " register(";
OS << registerAssignment->ShaderProfile << ", "; if (!registerAssignment->ShaderProfile.empty())
bool needsComma = false; OS << registerAssignment->ShaderProfile << ", ";
if (!registerAssignment->isSpaceOnly()) { bool needsComma = false;
if (!registerAssignment->RegisterType) if (!registerAssignment->isSpaceOnly()) {
OS << "invalid"; if (!registerAssignment->RegisterType)
else OS << "invalid";
OS << StringRef(&registerAssignment->RegisterType, 1); else
OS << registerAssignment->RegisterNumber + registerAssignment->RegisterOffset; OS << StringRef(&registerAssignment->RegisterType, 1);
needsComma = true; OS << registerAssignment->RegisterNumber +
} registerAssignment->RegisterOffset;
if (registerAssignment->RegisterSpace.hasValue()) { needsComma = true;
if (needsComma) OS << ", ";
OS << "space" << registerAssignment->RegisterSpace.getValue();
}
OS << ")";
if (!registerAssignment->IsValid)
OS << " invalid";
break;
} }
if (registerAssignment->RegisterSpace.hasValue()) {
if (needsComma)
OS << ", ";
OS << "space" << registerAssignment->RegisterSpace.getValue();
}
OS << ")";
if (!registerAssignment->IsValid)
OS << " invalid";
break;
}
case hlsl::UnusualAnnotation::UA_SemanticDecl: { case hlsl::UnusualAnnotation::UA_SemanticDecl: {
const hlsl::SemanticDecl* semanticDecl = cast<hlsl::SemanticDecl>(*It); const hlsl::SemanticDecl *semanticDecl = cast<hlsl::SemanticDecl>(*It);
OS << " \"" << semanticDecl->SemanticName << "\""; OS << " \"" << semanticDecl->SemanticName << "\"";
break; break;
} }
case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier: { case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier: {
const hlsl::PayloadAccessAnnotation *annotation = const hlsl::PayloadAccessAnnotation *annotation =
cast<hlsl::PayloadAccessAnnotation>(*It); cast<hlsl::PayloadAccessAnnotation>(*It);
@ -1081,7 +1089,7 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRef<hlsl::UnusualAnnotatio
// HLSL Change Ends // HLSL Change Ends
void ASTDumper::dumpDecl(const Decl *D) { void ASTDumper::dumpDecl(const Decl *D) {
dumpChild([=] { dumpChild([this, D] {
if (!D) { if (!D) {
ColorScope Color(*this, NullColor); ColorScope Color(*this, NullColor);
OS << "<<<NULL>>>"; OS << "<<<NULL>>>";
@ -1113,7 +1121,7 @@ void ASTDumper::dumpDecl(const Decl *D) {
if (auto *ND = dyn_cast<NamedDecl>(D)) if (auto *ND = dyn_cast<NamedDecl>(D))
for (Module *M : D->getASTContext().getModulesWithMergedDefinition( for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
const_cast<NamedDecl *>(ND))) const_cast<NamedDecl *>(ND)))
dumpChild([=] { OS << "also in " << M->getFullModuleName(); }); dumpChild([this, M] { OS << "also in " << M->getFullModuleName(); });
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
if (ND->isHidden()) if (ND->isHidden())
OS << " hidden"; OS << " hidden";
@ -1253,7 +1261,8 @@ void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
dumpDecl(*I); dumpDecl(*I);
if (!D->param_begin() && D->getNumParams()) if (!D->param_begin() && D->getNumParams())
dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; }); dumpChild(
[this, D] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
else else
for (FunctionDecl::param_const_iterator I = D->param_begin(), for (FunctionDecl::param_const_iterator I = D->param_begin(),
E = D->param_end(); E = D->param_end();
@ -1356,7 +1365,7 @@ void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
return; return;
for (const auto &I : D->bases()) { for (const auto &I : D->bases()) {
dumpChild([=] { dumpChild([this, I] {
if (I.isVirtual()) if (I.isVirtual())
OS << "virtual "; OS << "virtual ";
dumpAccessSpecifier(I.getAccessSpecifier()); dumpAccessSpecifier(I.getAccessSpecifier());
@ -1595,7 +1604,7 @@ void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
} }
if (D->isVariadic()) if (D->isVariadic())
dumpChild([=] { OS << "..."; }); dumpChild([this] { OS << "..."; });
if (D->hasBody()) if (D->hasBody())
dumpStmt(D->getBody()); dumpStmt(D->getBody());
@ -1723,13 +1732,13 @@ void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
dumpDecl(I); dumpDecl(I);
if (D->isVariadic()) if (D->isVariadic())
dumpChild([=]{ OS << "..."; }); dumpChild([this] { OS << "..."; });
if (D->capturesCXXThis()) if (D->capturesCXXThis())
dumpChild([=]{ OS << "capture this"; }); dumpChild([this] { OS << "capture this"; });
for (const auto &I : D->captures()) { for (const auto &I : D->captures()) {
dumpChild([=] { dumpChild([this, I] {
OS << "capture"; OS << "capture";
if (I.isByRef()) if (I.isByRef())
OS << " byref"; OS << " byref";
@ -1751,7 +1760,7 @@ void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void ASTDumper::dumpStmt(const Stmt *S) { void ASTDumper::dumpStmt(const Stmt *S) {
dumpChild([=] { dumpChild([this, S] {
if (!S) { if (!S) {
ColorScope Color(*this, NullColor); ColorScope Color(*this, NullColor);
OS << "<<<NULL>>>"; OS << "<<<NULL>>>";
@ -1965,7 +1974,7 @@ void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) { void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
VisitExpr(ILE); VisitExpr(ILE);
if (auto *Filler = ILE->getArrayFiller()) { if (auto *Filler = ILE->getArrayFiller()) {
dumpChild([=] { dumpChild([this, Filler] {
OS << "array filler"; OS << "array filler";
dumpStmt(Filler); dumpStmt(Filler);
}); });
@ -2302,7 +2311,7 @@ void ASTDumper::dumpFullComment(const FullComment *C) {
} }
void ASTDumper::dumpComment(const Comment *C) { void ASTDumper::dumpComment(const Comment *C) {
dumpChild([=] { dumpChild([this, C] {
if (!C) { if (!C) {
ColorScope Color(*this, NullColor); ColorScope Color(*this, NullColor);
OS << "<<<NULL>>>"; OS << "<<<NULL>>>";

Просмотреть файл

@ -169,9 +169,10 @@ namespace clang {
// Link LinkModule into this module if present, preserving its validity. // Link LinkModule into this module if present, preserving its validity.
if (LinkModule) { if (LinkModule) {
if (Linker::LinkModules( if (Linker::LinkModules(M, LinkModule.get(),
M, LinkModule.get(), [this](const DiagnosticInfo &DI) {
[=](const DiagnosticInfo &DI) { linkerDiagnosticHandler(DI); })) linkerDiagnosticHandler(DI);
}))
return; return;
} }

Просмотреть файл

@ -1964,7 +1964,8 @@ Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
"Diagnosing an empty lookup with explicit template args!"); "Diagnosing an empty lookup with explicit template args!");
*Out = CorrectTypoDelayed( *Out = CorrectTypoDelayed(
R.getLookupNameInfo(), R.getLookupKind(), S, &SS, std::move(CCC), R.getLookupNameInfo(), R.getLookupKind(), S, &SS, std::move(CCC),
[=](const TypoCorrection &TC) { [this, SS, Name, TypoLoc, Args, diagnostic,
diagnostic_suggest](const TypoCorrection &TC) {
emitEmptyLookupTypoDiagnostic(TC, *this, SS, Name, TypoLoc, Args, emitEmptyLookupTypoDiagnostic(TC, *this, SS, Name, TypoLoc, Args,
diagnostic, diagnostic_suggest); diagnostic, diagnostic_suggest);
}, },