From 5ecee0b599465fee96a1d51e3802be5d00f771c1 Mon Sep 17 00:00:00 2001 From: Devon Loehr Date: Thu, 1 Aug 2024 14:50:30 -0400 Subject: [PATCH] [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. --- tools/clang/lib/AST/ASTDumper.cpp | 151 ++++++++++++---------- tools/clang/lib/CodeGen/CodeGenAction.cpp | 7 +- tools/clang/lib/Sema/SemaExpr.cpp | 3 +- 3 files changed, 86 insertions(+), 75 deletions(-) diff --git a/tools/clang/lib/AST/ASTDumper.cpp b/tools/clang/lib/AST/ASTDumper.cpp index a29b7dea6..334542d7f 100644 --- a/tools/clang/lib/AST/ASTDumper.cpp +++ b/tools/clang/lib/AST/ASTDumper.cpp @@ -341,7 +341,7 @@ namespace { for (QualType PT : T->getParamTypes()) dumpTypeAsChild(PT); if (EPI.Variadic) - dumpChild([=] { OS << "..."; }); + dumpChild([this] { OS << "..."; }); } void VisitUnresolvedUsingType(const UnresolvedUsingType *T) { dumpDeclRef(T->getDecl()); @@ -646,7 +646,7 @@ void ASTDumper::dumpTypeAsChild(QualType T) { if (!SQT.Quals.hasQualifiers()) return dumpTypeAsChild(SQT.Ty); - dumpChild([=] { + dumpChild([this, T] { OS << "QualType"; dumpPointer(T.getAsOpaquePtr()); OS << " "; @@ -657,7 +657,7 @@ void ASTDumper::dumpTypeAsChild(QualType T) { } void ASTDumper::dumpTypeAsChild(const Type *T) { - dumpChild([=] { + dumpChild([this, T] { if (!T) { ColorScope Color(*this, NullColor); OS << "<<>>"; @@ -714,7 +714,7 @@ void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) { if (!D) return; - dumpChild([=]{ + dumpChild([this, Label, D] { if (Label) OS << Label << ' '; dumpBareDeclRef(D); @@ -748,7 +748,7 @@ void ASTDumper::dumpDeclContext(const DeclContext *DC) { // HLSL Change Ends if (DC->hasExternalLexicalStorage()) { - dumpChild([=]{ + dumpChild([this] { ColorScope Color(*this, UndeserializedColor); OS << ""; }); @@ -756,7 +756,7 @@ void ASTDumper::dumpDeclContext(const DeclContext *DC) { } void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) { - dumpChild([=] { + dumpChild([this, DC, DumpDecls] { OS << "StoredDeclsMap "; dumpBareDeclRef(cast(DC)); @@ -774,7 +774,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) { DeclarationName Name = I.getLookupName(); DeclContextLookupResult R = *I++; - dumpChild([=] { + dumpChild([this, Name, R, DumpDecls] { OS << "DeclarationName "; { 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(); RI != RE; ++RI) { - dumpChild([=] { + dumpChild([this, RI, DumpDecls] { dumpBareDeclRef(*RI); if ((*RI)->isHidden()) @@ -805,7 +805,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) { } if (HasUndeserializedLookups) { - dumpChild([=] { + dumpChild([this] { ColorScope Color(*this, UndeserializedColor); OS << ""; }); @@ -814,7 +814,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) { } void ASTDumper::dumpAttr(const Attr *A) { - dumpChild([=] { + dumpChild([this, A] { { ColorScope Color(*this, AttrColor); @@ -886,7 +886,7 @@ void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) { } void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) { - dumpChild([=] { + dumpChild([this, Init] { OS << "CXXCtorInitializer"; if (Init->isAnyMemberInitializer()) { OS << ' '; @@ -927,7 +927,7 @@ void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) { } void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) { - dumpChild([=] { + dumpChild([this, A, R] { OS << "TemplateArgument"; if (R.isValid()) dumpSourceRange(R); @@ -993,19 +993,23 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRefgetKind()) { - case hlsl::UnusualAnnotation::UA_ConstantPacking: - OS << "ConstantPacking"; break; - case hlsl::UnusualAnnotation::UA_RegisterAssignment: - OS << "RegisterAssignment"; break; - case hlsl::UnusualAnnotation::UA_SemanticDecl: - OS << "SemanticDecl"; break; - case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier: - OS << "PayloadAccessQualifier"; break; + case hlsl::UnusualAnnotation::UA_ConstantPacking: + OS << "ConstantPacking"; + break; + case hlsl::UnusualAnnotation::UA_RegisterAssignment: + OS << "RegisterAssignment"; + break; + case hlsl::UnusualAnnotation::UA_SemanticDecl: + OS << "SemanticDecl"; + break; + case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier: + OS << "PayloadAccessQualifier"; + break; } } dumpPointer(It); @@ -1014,48 +1018,52 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRefgetKind()) { case hlsl::UnusualAnnotation::UA_ConstantPacking: { - const hlsl::ConstantPacking* constantPacking = cast(*It); - OS << " packoffset(c"; - OS << constantPacking->Subcomponent; - OS << "."; - const char *xyzw[4] = { "x", "y", "z", "w" }; - if(constantPacking->ComponentOffset < 4) - OS << xyzw[constantPacking->ComponentOffset]; - else - OS << ""; - OS << ")"; - if (!constantPacking->IsValid) - OS << " invalid"; - break; - } + const hlsl::ConstantPacking *constantPacking = + cast(*It); + OS << " packoffset(c"; + OS << constantPacking->Subcomponent; + OS << "."; + const char *xyzw[4] = {"x", "y", "z", "w"}; + if (constantPacking->ComponentOffset < 4) + OS << xyzw[constantPacking->ComponentOffset]; + else + OS << ""; + OS << ")"; + if (!constantPacking->IsValid) + OS << " invalid"; + break; + } case hlsl::UnusualAnnotation::UA_RegisterAssignment: { - const hlsl::RegisterAssignment* registerAssignment = cast(*It); - OS << " register("; - if (!registerAssignment->ShaderProfile.empty()) - OS << registerAssignment->ShaderProfile << ", "; - bool needsComma = false; - if (!registerAssignment->isSpaceOnly()) { - if (!registerAssignment->RegisterType) - OS << "invalid"; - else - OS << StringRef(®isterAssignment->RegisterType, 1); - OS << registerAssignment->RegisterNumber + registerAssignment->RegisterOffset; - needsComma = true; - } - if (registerAssignment->RegisterSpace.hasValue()) { - if (needsComma) OS << ", "; - OS << "space" << registerAssignment->RegisterSpace.getValue(); - } - OS << ")"; - if (!registerAssignment->IsValid) - OS << " invalid"; - break; + const hlsl::RegisterAssignment *registerAssignment = + cast(*It); + OS << " register("; + if (!registerAssignment->ShaderProfile.empty()) + OS << registerAssignment->ShaderProfile << ", "; + bool needsComma = false; + if (!registerAssignment->isSpaceOnly()) { + if (!registerAssignment->RegisterType) + OS << "invalid"; + else + OS << StringRef(®isterAssignment->RegisterType, 1); + OS << registerAssignment->RegisterNumber + + registerAssignment->RegisterOffset; + needsComma = true; } + if (registerAssignment->RegisterSpace.hasValue()) { + if (needsComma) + OS << ", "; + OS << "space" << registerAssignment->RegisterSpace.getValue(); + } + OS << ")"; + if (!registerAssignment->IsValid) + OS << " invalid"; + break; + } case hlsl::UnusualAnnotation::UA_SemanticDecl: { - const hlsl::SemanticDecl* semanticDecl = cast(*It); - OS << " \"" << semanticDecl->SemanticName << "\""; - break; - } + const hlsl::SemanticDecl *semanticDecl = cast(*It); + OS << " \"" << semanticDecl->SemanticName << "\""; + break; + } case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier: { const hlsl::PayloadAccessAnnotation *annotation = cast(*It); @@ -1081,7 +1089,7 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRef>>"; @@ -1113,7 +1121,7 @@ void ASTDumper::dumpDecl(const Decl *D) { if (auto *ND = dyn_cast(D)) for (Module *M : D->getASTContext().getModulesWithMergedDefinition( const_cast(ND))) - dumpChild([=] { OS << "also in " << M->getFullModuleName(); }); + dumpChild([this, M] { OS << "also in " << M->getFullModuleName(); }); if (const NamedDecl *ND = dyn_cast(D)) if (ND->isHidden()) OS << " hidden"; @@ -1253,7 +1261,8 @@ void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) { dumpDecl(*I); if (!D->param_begin() && D->getNumParams()) - dumpChild([=] { OS << "<getNumParams() << ">>"; }); + dumpChild( + [this, D] { OS << "<getNumParams() << ">>"; }); else for (FunctionDecl::param_const_iterator I = D->param_begin(), E = D->param_end(); @@ -1356,7 +1365,7 @@ void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) { return; for (const auto &I : D->bases()) { - dumpChild([=] { + dumpChild([this, I] { if (I.isVirtual()) OS << "virtual "; dumpAccessSpecifier(I.getAccessSpecifier()); @@ -1595,7 +1604,7 @@ void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) { } if (D->isVariadic()) - dumpChild([=] { OS << "..."; }); + dumpChild([this] { OS << "..."; }); if (D->hasBody()) dumpStmt(D->getBody()); @@ -1723,13 +1732,13 @@ void ASTDumper::VisitBlockDecl(const BlockDecl *D) { dumpDecl(I); if (D->isVariadic()) - dumpChild([=]{ OS << "..."; }); + dumpChild([this] { OS << "..."; }); if (D->capturesCXXThis()) - dumpChild([=]{ OS << "capture this"; }); + dumpChild([this] { OS << "capture this"; }); for (const auto &I : D->captures()) { - dumpChild([=] { + dumpChild([this, I] { OS << "capture"; if (I.isByRef()) OS << " byref"; @@ -1751,7 +1760,7 @@ void ASTDumper::VisitBlockDecl(const BlockDecl *D) { //===----------------------------------------------------------------------===// void ASTDumper::dumpStmt(const Stmt *S) { - dumpChild([=] { + dumpChild([this, S] { if (!S) { ColorScope Color(*this, NullColor); OS << "<<>>"; @@ -1965,7 +1974,7 @@ void ASTDumper::VisitStringLiteral(const StringLiteral *Str) { void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) { VisitExpr(ILE); if (auto *Filler = ILE->getArrayFiller()) { - dumpChild([=] { + dumpChild([this, Filler] { OS << "array filler"; dumpStmt(Filler); }); @@ -2302,7 +2311,7 @@ void ASTDumper::dumpFullComment(const FullComment *C) { } void ASTDumper::dumpComment(const Comment *C) { - dumpChild([=] { + dumpChild([this, C] { if (!C) { ColorScope Color(*this, NullColor); OS << "<<>>"; diff --git a/tools/clang/lib/CodeGen/CodeGenAction.cpp b/tools/clang/lib/CodeGen/CodeGenAction.cpp index 68ebaadf5..d8339f25b 100644 --- a/tools/clang/lib/CodeGen/CodeGenAction.cpp +++ b/tools/clang/lib/CodeGen/CodeGenAction.cpp @@ -169,9 +169,10 @@ namespace clang { // Link LinkModule into this module if present, preserving its validity. if (LinkModule) { - if (Linker::LinkModules( - M, LinkModule.get(), - [=](const DiagnosticInfo &DI) { linkerDiagnosticHandler(DI); })) + if (Linker::LinkModules(M, LinkModule.get(), + [this](const DiagnosticInfo &DI) { + linkerDiagnosticHandler(DI); + })) return; } diff --git a/tools/clang/lib/Sema/SemaExpr.cpp b/tools/clang/lib/Sema/SemaExpr.cpp index 92d84293d..c8c762a0a 100644 --- a/tools/clang/lib/Sema/SemaExpr.cpp +++ b/tools/clang/lib/Sema/SemaExpr.cpp @@ -1964,7 +1964,8 @@ Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, "Diagnosing an empty lookup with explicit template args!"); *Out = CorrectTypoDelayed( 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, diagnostic, diagnostic_suggest); },