r371124 - Implement Microsoft-compatible mangling for decomposition declarations.

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 5 14:08:51 PDT 2019


Author: nico
Date: Thu Sep  5 14:08:50 2019
New Revision: 371124

URL: http://llvm.org/viewvc/llvm-project?rev=371124&view=rev
Log:
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 <epastor at google.com>!

Differential Revision: https://reviews.llvm.org/D67202

Added:
    cfe/trunk/test/CodeGenCXX/mangle-ms-cxx17.cpp
Modified:
    cfe/trunk/include/clang/AST/Mangle.h
    cfe/trunk/lib/AST/MicrosoftMangle.cpp

Modified: cfe/trunk/include/clang/AST/Mangle.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Mangle.h?rev=371124&r1=371123&r2=371124&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Mangle.h (original)
+++ cfe/trunk/include/clang/AST/Mangle.h Thu Sep  5 14:08:50 2019
@@ -56,7 +56,7 @@ private:
 
   llvm::DenseMap<const BlockDecl*, unsigned> GlobalBlockIds;
   llvm::DenseMap<const BlockDecl*, unsigned> LocalBlockIds;
-  llvm::DenseMap<const TagDecl*, uint64_t> AnonStructIds;
+  llvm::DenseMap<const NamedDecl*, uint64_t> AnonStructIds;
 
 public:
   ManglerKind getKind() const { return Kind; }
@@ -82,9 +82,9 @@ public:
     return Result.first->second;
   }
 
-  uint64_t getAnonymousStructId(const TagDecl *TD) {
-    std::pair<llvm::DenseMap<const TagDecl *, uint64_t>::iterator, bool>
-        Result = AnonStructIds.insert(std::make_pair(TD, AnonStructIds.size()));
+  uint64_t getAnonymousStructId(const NamedDecl *D) {
+    std::pair<llvm::DenseMap<const NamedDecl *, uint64_t>::iterator, bool>
+        Result = AnonStructIds.insert(std::make_pair(D, AnonStructIds.size()));
     return Result.first->second;
   }
 

Modified: cfe/trunk/lib/AST/MicrosoftMangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/MicrosoftMangle.cpp?rev=371124&r1=371123&r2=371124&view=diff
==============================================================================
--- cfe/trunk/lib/AST/MicrosoftMangle.cpp (original)
+++ cfe/trunk/lib/AST/MicrosoftMangle.cpp Thu Sep  5 14:08:50 2019
@@ -868,16 +868,11 @@ void MicrosoftCXXNameMangler::mangleUnqu
       }
 
       if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(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;
       }

Added: cfe/trunk/test/CodeGenCXX/mangle-ms-cxx17.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/mangle-ms-cxx17.cpp?rev=371124&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/mangle-ms-cxx17.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/mangle-ms-cxx17.cpp Thu Sep  5 14:08:50 2019
@@ -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();




More information about the cfe-commits mailing list