Skip to content

Commit 5ecee0b

Browse files
authored
[NFC] Remove implicit this capture in lambdas (microsoft#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.
1 parent 8b33431 commit 5ecee0b

File tree

3 files changed

+85
-74
lines changed

3 files changed

+85
-74
lines changed

tools/clang/lib/AST/ASTDumper.cpp

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ namespace {
341341
for (QualType PT : T->getParamTypes())
342342
dumpTypeAsChild(PT);
343343
if (EPI.Variadic)
344-
dumpChild([=] { OS << "..."; });
344+
dumpChild([this] { OS << "..."; });
345345
}
346346
void VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
347347
dumpDeclRef(T->getDecl());
@@ -646,7 +646,7 @@ void ASTDumper::dumpTypeAsChild(QualType T) {
646646
if (!SQT.Quals.hasQualifiers())
647647
return dumpTypeAsChild(SQT.Ty);
648648

649-
dumpChild([=] {
649+
dumpChild([this, T] {
650650
OS << "QualType";
651651
dumpPointer(T.getAsOpaquePtr());
652652
OS << " ";
@@ -657,7 +657,7 @@ void ASTDumper::dumpTypeAsChild(QualType T) {
657657
}
658658

659659
void ASTDumper::dumpTypeAsChild(const Type *T) {
660-
dumpChild([=] {
660+
dumpChild([this, T] {
661661
if (!T) {
662662
ColorScope Color(*this, NullColor);
663663
OS << "<<<NULL>>>";
@@ -714,7 +714,7 @@ void ASTDumper::dumpDeclRef(const Decl *D, const char *Label) {
714714
if (!D)
715715
return;
716716

717-
dumpChild([=]{
717+
dumpChild([this, Label, D] {
718718
if (Label)
719719
OS << Label << ' ';
720720
dumpBareDeclRef(D);
@@ -748,15 +748,15 @@ void ASTDumper::dumpDeclContext(const DeclContext *DC) {
748748
// HLSL Change Ends
749749

750750
if (DC->hasExternalLexicalStorage()) {
751-
dumpChild([=]{
751+
dumpChild([this] {
752752
ColorScope Color(*this, UndeserializedColor);
753753
OS << "<undeserialized declarations>";
754754
});
755755
}
756756
}
757757

758758
void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
759-
dumpChild([=] {
759+
dumpChild([this, DC, DumpDecls] {
760760
OS << "StoredDeclsMap ";
761761
dumpBareDeclRef(cast<Decl>(DC));
762762

@@ -774,7 +774,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
774774
DeclarationName Name = I.getLookupName();
775775
DeclContextLookupResult R = *I++;
776776

777-
dumpChild([=] {
777+
dumpChild([this, Name, R, DumpDecls] {
778778
OS << "DeclarationName ";
779779
{
780780
ColorScope Color(*this, DeclNameColor);
@@ -783,7 +783,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
783783

784784
for (DeclContextLookupResult::iterator RI = R.begin(), RE = R.end();
785785
RI != RE; ++RI) {
786-
dumpChild([=] {
786+
dumpChild([this, RI, DumpDecls] {
787787
dumpBareDeclRef(*RI);
788788

789789
if ((*RI)->isHidden())
@@ -805,7 +805,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
805805
}
806806

807807
if (HasUndeserializedLookups) {
808-
dumpChild([=] {
808+
dumpChild([this] {
809809
ColorScope Color(*this, UndeserializedColor);
810810
OS << "<undeserialized lookups>";
811811
});
@@ -814,7 +814,7 @@ void ASTDumper::dumpLookups(const DeclContext *DC, bool DumpDecls) {
814814
}
815815

816816
void ASTDumper::dumpAttr(const Attr *A) {
817-
dumpChild([=] {
817+
dumpChild([this, A] {
818818
{
819819
ColorScope Color(*this, AttrColor);
820820

@@ -886,7 +886,7 @@ void ASTDumper::dumpAccessSpecifier(AccessSpecifier AS) {
886886
}
887887

888888
void ASTDumper::dumpCXXCtorInitializer(const CXXCtorInitializer *Init) {
889-
dumpChild([=] {
889+
dumpChild([this, Init] {
890890
OS << "CXXCtorInitializer";
891891
if (Init->isAnyMemberInitializer()) {
892892
OS << ' ';
@@ -927,7 +927,7 @@ void ASTDumper::dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
927927
}
928928

929929
void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R) {
930-
dumpChild([=] {
930+
dumpChild([this, A, R] {
931931
OS << "TemplateArgument";
932932
if (R.isValid())
933933
dumpSourceRange(R);
@@ -993,19 +993,23 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRef<hlsl::UnusualAnnotatio
993993
{
994994
for (auto It = UA.begin(), E = UA.end(); It != E; ++It)
995995
{
996-
dumpChild([=] {
996+
dumpChild([this, It] {
997997
{
998998
ColorScope Color(*this, AttrColor);
999999
switch ((*It)->getKind())
10001000
{
1001-
case hlsl::UnusualAnnotation::UA_ConstantPacking:
1002-
OS << "ConstantPacking"; break;
1003-
case hlsl::UnusualAnnotation::UA_RegisterAssignment:
1004-
OS << "RegisterAssignment"; break;
1005-
case hlsl::UnusualAnnotation::UA_SemanticDecl:
1006-
OS << "SemanticDecl"; break;
1007-
case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier:
1008-
OS << "PayloadAccessQualifier"; break;
1001+
case hlsl::UnusualAnnotation::UA_ConstantPacking:
1002+
OS << "ConstantPacking";
1003+
break;
1004+
case hlsl::UnusualAnnotation::UA_RegisterAssignment:
1005+
OS << "RegisterAssignment";
1006+
break;
1007+
case hlsl::UnusualAnnotation::UA_SemanticDecl:
1008+
OS << "SemanticDecl";
1009+
break;
1010+
case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier:
1011+
OS << "PayloadAccessQualifier";
1012+
break;
10091013
}
10101014
}
10111015
dumpPointer(It);
@@ -1014,48 +1018,52 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRef<hlsl::UnusualAnnotatio
10141018
switch ((*It)->getKind())
10151019
{
10161020
case hlsl::UnusualAnnotation::UA_ConstantPacking: {
1017-
const hlsl::ConstantPacking* constantPacking = cast<hlsl::ConstantPacking>(*It);
1018-
OS << " packoffset(c";
1019-
OS << constantPacking->Subcomponent;
1020-
OS << ".";
1021-
const char *xyzw[4] = { "x", "y", "z", "w" };
1022-
if(constantPacking->ComponentOffset < 4)
1023-
OS << xyzw[constantPacking->ComponentOffset];
1021+
const hlsl::ConstantPacking *constantPacking =
1022+
cast<hlsl::ConstantPacking>(*It);
1023+
OS << " packoffset(c";
1024+
OS << constantPacking->Subcomponent;
1025+
OS << ".";
1026+
const char *xyzw[4] = {"x", "y", "z", "w"};
1027+
if (constantPacking->ComponentOffset < 4)
1028+
OS << xyzw[constantPacking->ComponentOffset];
1029+
else
1030+
OS << "<invalid>";
1031+
OS << ")";
1032+
if (!constantPacking->IsValid)
1033+
OS << " invalid";
1034+
break;
1035+
}
1036+
case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
1037+
const hlsl::RegisterAssignment *registerAssignment =
1038+
cast<hlsl::RegisterAssignment>(*It);
1039+
OS << " register(";
1040+
if (!registerAssignment->ShaderProfile.empty())
1041+
OS << registerAssignment->ShaderProfile << ", ";
1042+
bool needsComma = false;
1043+
if (!registerAssignment->isSpaceOnly()) {
1044+
if (!registerAssignment->RegisterType)
1045+
OS << "invalid";
10241046
else
1025-
OS << "<invalid>";
1026-
OS << ")";
1027-
if (!constantPacking->IsValid)
1028-
OS << " invalid";
1029-
break;
1047+
OS << StringRef(&registerAssignment->RegisterType, 1);
1048+
OS << registerAssignment->RegisterNumber +
1049+
registerAssignment->RegisterOffset;
1050+
needsComma = true;
10301051
}
1031-
case hlsl::UnusualAnnotation::UA_RegisterAssignment: {
1032-
const hlsl::RegisterAssignment* registerAssignment = cast<hlsl::RegisterAssignment>(*It);
1033-
OS << " register(";
1034-
if (!registerAssignment->ShaderProfile.empty())
1035-
OS << registerAssignment->ShaderProfile << ", ";
1036-
bool needsComma = false;
1037-
if (!registerAssignment->isSpaceOnly()) {
1038-
if (!registerAssignment->RegisterType)
1039-
OS << "invalid";
1040-
else
1041-
OS << StringRef(&registerAssignment->RegisterType, 1);
1042-
OS << registerAssignment->RegisterNumber + registerAssignment->RegisterOffset;
1043-
needsComma = true;
1044-
}
1045-
if (registerAssignment->RegisterSpace.hasValue()) {
1046-
if (needsComma) OS << ", ";
1047-
OS << "space" << registerAssignment->RegisterSpace.getValue();
1048-
}
1049-
OS << ")";
1050-
if (!registerAssignment->IsValid)
1051-
OS << " invalid";
1052-
break;
1052+
if (registerAssignment->RegisterSpace.hasValue()) {
1053+
if (needsComma)
1054+
OS << ", ";
1055+
OS << "space" << registerAssignment->RegisterSpace.getValue();
10531056
}
1057+
OS << ")";
1058+
if (!registerAssignment->IsValid)
1059+
OS << " invalid";
1060+
break;
1061+
}
10541062
case hlsl::UnusualAnnotation::UA_SemanticDecl: {
1055-
const hlsl::SemanticDecl* semanticDecl = cast<hlsl::SemanticDecl>(*It);
1056-
OS << " \"" << semanticDecl->SemanticName << "\"";
1057-
break;
1058-
}
1063+
const hlsl::SemanticDecl *semanticDecl = cast<hlsl::SemanticDecl>(*It);
1064+
OS << " \"" << semanticDecl->SemanticName << "\"";
1065+
break;
1066+
}
10591067
case hlsl::UnusualAnnotation::UA_PayloadAccessQualifier: {
10601068
const hlsl::PayloadAccessAnnotation *annotation =
10611069
cast<hlsl::PayloadAccessAnnotation>(*It);
@@ -1081,7 +1089,7 @@ void ASTDumper::dumpHLSLUnusualAnnotations(const ArrayRef<hlsl::UnusualAnnotatio
10811089
// HLSL Change Ends
10821090

10831091
void ASTDumper::dumpDecl(const Decl *D) {
1084-
dumpChild([=] {
1092+
dumpChild([this, D] {
10851093
if (!D) {
10861094
ColorScope Color(*this, NullColor);
10871095
OS << "<<<NULL>>>";
@@ -1113,7 +1121,7 @@ void ASTDumper::dumpDecl(const Decl *D) {
11131121
if (auto *ND = dyn_cast<NamedDecl>(D))
11141122
for (Module *M : D->getASTContext().getModulesWithMergedDefinition(
11151123
const_cast<NamedDecl *>(ND)))
1116-
dumpChild([=] { OS << "also in " << M->getFullModuleName(); });
1124+
dumpChild([this, M] { OS << "also in " << M->getFullModuleName(); });
11171125
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
11181126
if (ND->isHidden())
11191127
OS << " hidden";
@@ -1253,7 +1261,8 @@ void ASTDumper::VisitFunctionDecl(const FunctionDecl *D) {
12531261
dumpDecl(*I);
12541262

12551263
if (!D->param_begin() && D->getNumParams())
1256-
dumpChild([=] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
1264+
dumpChild(
1265+
[this, D] { OS << "<<NULL params x " << D->getNumParams() << ">>"; });
12571266
else
12581267
for (FunctionDecl::param_const_iterator I = D->param_begin(),
12591268
E = D->param_end();
@@ -1356,7 +1365,7 @@ void ASTDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) {
13561365
return;
13571366

13581367
for (const auto &I : D->bases()) {
1359-
dumpChild([=] {
1368+
dumpChild([this, I] {
13601369
if (I.isVirtual())
13611370
OS << "virtual ";
13621371
dumpAccessSpecifier(I.getAccessSpecifier());
@@ -1595,7 +1604,7 @@ void ASTDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
15951604
}
15961605

15971606
if (D->isVariadic())
1598-
dumpChild([=] { OS << "..."; });
1607+
dumpChild([this] { OS << "..."; });
15991608

16001609
if (D->hasBody())
16011610
dumpStmt(D->getBody());
@@ -1723,13 +1732,13 @@ void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
17231732
dumpDecl(I);
17241733

17251734
if (D->isVariadic())
1726-
dumpChild([=]{ OS << "..."; });
1735+
dumpChild([this] { OS << "..."; });
17271736

17281737
if (D->capturesCXXThis())
1729-
dumpChild([=]{ OS << "capture this"; });
1738+
dumpChild([this] { OS << "capture this"; });
17301739

17311740
for (const auto &I : D->captures()) {
1732-
dumpChild([=] {
1741+
dumpChild([this, I] {
17331742
OS << "capture";
17341743
if (I.isByRef())
17351744
OS << " byref";
@@ -1751,7 +1760,7 @@ void ASTDumper::VisitBlockDecl(const BlockDecl *D) {
17511760
//===----------------------------------------------------------------------===//
17521761

17531762
void ASTDumper::dumpStmt(const Stmt *S) {
1754-
dumpChild([=] {
1763+
dumpChild([this, S] {
17551764
if (!S) {
17561765
ColorScope Color(*this, NullColor);
17571766
OS << "<<<NULL>>>";
@@ -1965,7 +1974,7 @@ void ASTDumper::VisitStringLiteral(const StringLiteral *Str) {
19651974
void ASTDumper::VisitInitListExpr(const InitListExpr *ILE) {
19661975
VisitExpr(ILE);
19671976
if (auto *Filler = ILE->getArrayFiller()) {
1968-
dumpChild([=] {
1977+
dumpChild([this, Filler] {
19691978
OS << "array filler";
19701979
dumpStmt(Filler);
19711980
});
@@ -2302,7 +2311,7 @@ void ASTDumper::dumpFullComment(const FullComment *C) {
23022311
}
23032312

23042313
void ASTDumper::dumpComment(const Comment *C) {
2305-
dumpChild([=] {
2314+
dumpChild([this, C] {
23062315
if (!C) {
23072316
ColorScope Color(*this, NullColor);
23082317
OS << "<<<NULL>>>";

tools/clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ namespace clang {
169169

170170
// Link LinkModule into this module if present, preserving its validity.
171171
if (LinkModule) {
172-
if (Linker::LinkModules(
173-
M, LinkModule.get(),
174-
[=](const DiagnosticInfo &DI) { linkerDiagnosticHandler(DI); }))
172+
if (Linker::LinkModules(M, LinkModule.get(),
173+
[this](const DiagnosticInfo &DI) {
174+
linkerDiagnosticHandler(DI);
175+
}))
175176
return;
176177
}
177178

tools/clang/lib/Sema/SemaExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1964,7 +1964,8 @@ Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
19641964
"Diagnosing an empty lookup with explicit template args!");
19651965
*Out = CorrectTypoDelayed(
19661966
R.getLookupNameInfo(), R.getLookupKind(), S, &SS, std::move(CCC),
1967-
[=](const TypoCorrection &TC) {
1967+
[this, SS, Name, TypoLoc, Args, diagnostic,
1968+
diagnostic_suggest](const TypoCorrection &TC) {
19681969
emitEmptyLookupTypoDiagnostic(TC, *this, SS, Name, TypoLoc, Args,
19691970
diagnostic, diagnostic_suggest);
19701971
},

0 commit comments

Comments
 (0)