diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h --- a/clang/include/clang/AST/Mangle.h +++ b/clang/include/clang/AST/Mangle.h @@ -89,6 +89,8 @@ return Result.first->second; } + virtual StringRef getLambdaString(const CXXRecordDecl *Lambda) = 0; + /// @name Mangler Entry Points /// @{ diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp --- a/clang/lib/AST/ItaniumMangle.cpp +++ b/clang/lib/AST/ItaniumMangle.cpp @@ -203,6 +203,10 @@ disc = discriminator-2; return true; } + + StringRef getLambdaString(const CXXRecordDecl *Lambda) override { + return StringRef(); + } /// @} }; diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp --- a/clang/lib/AST/MicrosoftMangle.cpp +++ b/clang/lib/AST/MicrosoftMangle.cpp @@ -228,6 +228,34 @@ return true; } + StringRef getLambdaString(const CXXRecordDecl *Lambda) override { + assert(Lambda->isLambda() && "RD must be a lambda!"); + llvm::SmallString<10> Name("getLambdaContextDecl(); + unsigned LambdaManglingNumber = Lambda->getLambdaManglingNumber(); + unsigned LambdaId; + const ParmVarDecl *Parm = dyn_cast_or_null(LambdaContextDecl); + const FunctionDecl *Func = + Parm ? dyn_cast(Parm->getDeclContext()) : nullptr; + + if (Func) { + unsigned DefaultArgNo = + Func->getNumParams() - Parm->getFunctionScopeIndex(); + Name += llvm::utostr(DefaultArgNo); + Name += "_"; + } + + if (LambdaManglingNumber) + LambdaId = LambdaManglingNumber; + else + LambdaId = getLambdaId(Lambda); + + Name += llvm::utostr(LambdaId); + Name += ">"; + return StringRef(Name); + } + unsigned getLambdaId(const CXXRecordDecl *RD) { assert(RD->isLambda() && "RD must be a lambda!"); assert(!RD->isExternallyVisible() && "RD must not be visible!"); @@ -972,32 +1000,10 @@ if (const CXXRecordDecl *Record = dyn_cast(TD)) { if (Record->isLambda()) { - llvm::SmallString<10> Name("getLambdaContextDecl(); unsigned LambdaManglingNumber = Record->getLambdaManglingNumber(); - unsigned LambdaId; - const ParmVarDecl *Parm = - dyn_cast_or_null(LambdaContextDecl); - const FunctionDecl *Func = - Parm ? dyn_cast(Parm->getDeclContext()) : nullptr; - - if (Func) { - unsigned DefaultArgNo = - Func->getNumParams() - Parm->getFunctionScopeIndex(); - Name += llvm::utostr(DefaultArgNo); - Name += "_"; - } - - if (LambdaManglingNumber) - LambdaId = LambdaManglingNumber; - else - LambdaId = Context.getLambdaId(Record); - - Name += llvm::utostr(LambdaId); - Name += ">"; - - mangleSourceName(Name); + Decl *LambdaContextDecl = Record->getLambdaContextDecl(); // If the context is a variable or a class member and not a parameter, // it is encoded in a qualified name. diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -317,8 +317,9 @@ if (const IdentifierInfo *II = RD->getIdentifier()) return II->getName(); - // The CodeView printer in LLVM wants to see the names of unnamed types: it is - // used to reconstruct the fully qualified type names. + // The CodeView printer in LLVM wants to see the names of unnamed types + // because they need to have a unique identifier. + // These names are used to reconstruct the fully qualified type names. if (CGM.getCodeGenOpts().EmitCodeView) { if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) { assert(RD->getDeclContext() == D->getDeclContext() && @@ -342,6 +343,12 @@ // associate typedef mangled in if they have one. Name = TND->getName(); + // Give lambdas a display name based on their name mangling. + if (const CXXRecordDecl *CXXRD = dyn_cast(RD)) + if (CXXRD->isLambda()) + return internString( + CGM.getCXXABI().getMangleContext().getLambdaString(CXXRD)); + if (!Name.empty()) { SmallString<256> UnnamedType("" // MSVC-SAME: identifier: ".?AV@?0??main@@9@" // MSVC-SAME: ) diff --git a/clang/test/CodeGenCXX/debug-info-gline-tables-only-codeview.cpp b/clang/test/CodeGenCXX/debug-info-gline-tables-only-codeview.cpp --- a/clang/test/CodeGenCXX/debug-info-gline-tables-only-codeview.cpp +++ b/clang/test/CodeGenCXX/debug-info-gline-tables-only-codeview.cpp @@ -6,10 +6,15 @@ namespace NS { struct C { void m() {} + // Test externally visible lambda. + void lambda2() { []() {}(); } }; void f() {} } +// Test non- externally visible lambda. +auto lambda1 = []() { return 1; }; + NS::C c; void test() { @@ -27,4 +32,16 @@ // CHECK-NOT: identifier // CHECK: ![[MTYPE]] = !DISubroutineType(types: !{{.*}}) c.m(); + + // CHECK: !DISubprogram(name: "operator()", scope: ![[LAMBDA1:[0-9]+]] + // CHECK: ![[LAMBDA1]] = !DICompositeType(tag: DW_TAG_class_type, + // CHECK-SAME: name: "" + // CHECK-SAME: flags: DIFlagFwdDecl + lambda1(); + + // CHECK: !DISubprogram(name: "operator()", scope: ![[LAMBDA2:[0-9]+]] + // CHECK: ![[LAMBDA2]] = !DICompositeType(tag: DW_TAG_class_type, + // CHECK-SAME: name: "" + // CHECK-SAME: flags: DIFlagFwdDecl + c.lambda2(); }