From d83daae45a5611e7a3739909185b749240c220b8 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Thu, 5 Sep 2019 21:08:50 +0000 Subject: [PATCH] Implement Microsoft-compatible mangling for decomposition declarations. Match cl.exe's mangling for decomposition declarations. Decomposition declarations are considered to be anonymous structs, and use the same convention as for anonymous struct/union declarations. Naming confirmed to match https://godbolt.org/z/K2osJa Patch from Eric Astor ! Differential Revision: https://reviews.llvm.org/D67202 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@371124 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Mangle.h | 8 ++++---- lib/AST/MicrosoftMangle.cpp | 15 +++++---------- test/CodeGenCXX/mangle-ms-cxx17.cpp | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 test/CodeGenCXX/mangle-ms-cxx17.cpp diff --git a/include/clang/AST/Mangle.h b/include/clang/AST/Mangle.h index e3c95ddb07..5db5c5b977 100644 --- a/include/clang/AST/Mangle.h +++ b/include/clang/AST/Mangle.h @@ -56,7 +56,7 @@ private: llvm::DenseMap GlobalBlockIds; llvm::DenseMap LocalBlockIds; - llvm::DenseMap AnonStructIds; + llvm::DenseMap AnonStructIds; public: ManglerKind getKind() const { return Kind; } @@ -82,9 +82,9 @@ public: return Result.first->second; } - uint64_t getAnonymousStructId(const TagDecl *TD) { - std::pair::iterator, bool> - Result = AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size())); + uint64_t getAnonymousStructId(const NamedDecl *D) { + std::pair::iterator, bool> + Result = AnonStructIds.insert(std::make_pair(D, AnonStructIds.size())); return Result.first->second; } diff --git a/lib/AST/MicrosoftMangle.cpp b/lib/AST/MicrosoftMangle.cpp index f7a456e06a..7ad8b52f7f 100644 --- a/lib/AST/MicrosoftMangle.cpp +++ b/lib/AST/MicrosoftMangle.cpp @@ -868,16 +868,11 @@ void MicrosoftCXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND, } if (const DecompositionDecl *DD = dyn_cast(ND)) { - // FIXME: Invented mangling for decomposition declarations: - // [X,Y,Z] - // where X,Y,Z are the names of the bindings. - llvm::SmallString<128> Name("["); - for (auto *BD : DD->bindings()) { - if (Name.size() > 1) - Name += ','; - Name += BD->getDeclName().getAsIdentifierInfo()->getName(); - } - Name += ']'; + // Decomposition declarations are considered anonymous, and get + // numbered with a $S prefix. + llvm::SmallString<64> Name("$S"); + // Get a unique id for the anonymous struct. + Name += llvm::utostr(Context.getAnonymousStructId(DD) + 1); mangleSourceName(Name); break; } diff --git a/test/CodeGenCXX/mangle-ms-cxx17.cpp b/test/CodeGenCXX/mangle-ms-cxx17.cpp new file mode 100644 index 0000000000..897f0d6c9b --- /dev/null +++ b/test/CodeGenCXX/mangle-ms-cxx17.cpp @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -std=c++1z -fms-extensions -emit-llvm %s -o - -triple=i386-pc-win32 -fms-compatibility-version=19.10 | FileCheck -allow-deprecated-dag-overlap %s --check-prefix=CHECK --check-prefix=MSVC2017 +// RUN: %clang_cc1 -std=c++1z -fms-extensions -emit-llvm %s -o - -triple=i386-pc-win32 -fms-compatibility-version=19.00 | FileCheck -allow-deprecated-dag-overlap %s --check-prefix=CHECK --check-prefix=MSVC2015 + +struct S { + int x; + double y; +}; +S f(); + +// CHECK-DAG: "?$S1@@3US@@B" +const auto [x0, y0] = f(); +// CHECK-DAG: "?$S2@@3US@@B" +const auto [x1, y1] = f(); + +static union { +int a; +double b; +}; + +// CHECK-DAG: "?$S4@@3US@@B" +const auto [x2, y2] = f(); -- 2.40.0