[llvm] [MSVC][Demangling] Make Microsoft demangling more configurable (PR #202702)

Aaron Ballman via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 12 06:06:56 PDT 2026


================
@@ -0,0 +1,315 @@
+//===-- MicrosoftDemangleTest.cpp -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Demangle/Demangle.h"
+#include "gtest/gtest.h"
+#include <cstdlib>
+#include <string>
+#include <string_view>
+
+using namespace llvm;
+
+namespace {
+std::string microsoftDemangleString(std::string_view MangledName,
+                                    MSDemangleFlags Flags) {
+  char *Demangled = microsoftDemangle(MangledName, nullptr, nullptr, Flags);
+  if (!Demangled)
+    return std::string(MangledName);
+  std::string Result(Demangled);
+  std::free(Demangled);
+  return Result;
+}
+} // namespace
+
+TEST(MicrosoftDemangle, demangleFunction) {
+  EXPECT_EQ(microsoftDemangleString("?foo@@YAXXZ", MSDF_None),
+            "void __cdecl foo(void)");
+  EXPECT_EQ(microsoftDemangleString("?foo@@YAXXZ", MSDF_NoReturnType),
+            "__cdecl foo(void)");
+  EXPECT_EQ(microsoftDemangleString("?foo@@YAXXZ", MSDF_NoCallingConvention),
+            "void foo(void)");
+  EXPECT_EQ(microsoftDemangleString("?foo@@YAXXZ", MSDF_NoVoidParameter),
+            "void __cdecl foo()");
+  EXPECT_EQ(microsoftDemangleString("?foo@@YAXXZ",
+                                    MSDemangleFlags(MSDF_NoCallingConvention |
+                                                    MSDF_NoReturnType |
+                                                    MSDF_NoVoidParameter)),
+            "foo()");
+}
+
+TEST(MicrosoftDemangle, demangleFunctionWithParameters) {
+  EXPECT_EQ(microsoftDemangleString("?foo@@YAXHH at Z", MSDF_None),
+            "void __cdecl foo(int, int)");
+  EXPECT_EQ(microsoftDemangleString("?foo@@YAXHH at Z", MSDF_NoReturnType),
+            "__cdecl foo(int, int)");
+  EXPECT_EQ(microsoftDemangleString("?foo@@YAXHH at Z", MSDF_NoCallingConvention),
+            "void foo(int, int)");
+  EXPECT_EQ(microsoftDemangleString(
----------------
AaronBallman wrote:

How about the same tests (and a NoVoid test) for a signature like:
```
void foo(int (*)(void))
```
to ensure we strip the parts from the parameter type?

https://github.com/llvm/llvm-project/pull/202702


More information about the llvm-commits mailing list