Index: lib/AST/MicrosoftMangle.cpp =================================================================== --- lib/AST/MicrosoftMangle.cpp +++ lib/AST/MicrosoftMangle.cpp @@ -824,9 +824,10 @@ if (const CXXRecordDecl *Record = dyn_cast(TD)) { if (Record->isLambda()) { llvm::SmallString<10> Name("getLambdaManglingNumber(); unsigned LambdaId; - if (Record->getLambdaManglingNumber()) - LambdaId = Record->getLambdaManglingNumber(); + if (LambdaManglingNumber) + LambdaId = LambdaManglingNumber; else LambdaId = Context.getLambdaId(Record); @@ -834,6 +835,17 @@ Name += ">"; mangleSourceName(Name); + + // If the context of a closure type is an initializer for a class + // member (static or nonstatic), it is encoded in a qualified name. + if (LambdaManglingNumber) { + if (Decl *Context = Record->getLambdaContextDecl()) { + if ((isa(Context) || isa(Context)) && + Context->getDeclContext()->isRecord()) { + mangleUnqualifiedName(cast(Context)); + } + } + } break; } } Index: test/CodeGenCXX/mangle-ms-cxx11.cpp =================================================================== --- test/CodeGenCXX/mangle-ms-cxx11.cpp +++ test/CodeGenCXX/mangle-ms-cxx11.cpp @@ -318,3 +318,20 @@ // CHECK-DAG: @"\01?unaligned_foo8@unaligned_foo8_S@@QFCEXXZ" +namespace PR31197 { +struct A { + // CHECK-DAG: define linkonce_odr x86_thiscallcc i32* @"\01??R@x@A@PR31197@@QBE@XZ"( + int *x = []() { + static int white; + // CHECK-DAG: @"\01?white@?1???R@x@A@PR31197@@QBE@XZ@4HA" + return &white; + }(); + // CHECK-DAG: define linkonce_odr x86_thiscallcc i32* @"\01??R@y@A@PR31197@@QBE@XZ"( + int *y = []() { + static int black; + // CHECK-DAG: @"\01?black@?1???R@y@A@PR31197@@QBE@XZ@4HA" + return &black; + }(); +}; +A a; +}