[llvm] [llvm-cxxfilt][macOS] Don't strip underscores on macOS by default (PR #106233)
Michael Buch via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 27 13:27:24 PDT 2024
https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/106233
>From 30f23192f63a60358ac4cfae6d26228295cfa982 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Thu, 22 Aug 2024 15:17:27 +0100
Subject: [PATCH 1/5] [llvm-cxxfilt][macOS] Don't strip underscores on macOS by
default
Currently, `llvm-cxxfilt` will strip the leading underscore of
its input on macOS. Historically MachO symbols were prefixed
with an extra underscore and this is why this default exists.
However, since this default was introduced the `ItaniumDemangler`
supports all of the following mangling prefixes:
`_Z`, `__Z`, `___Z`, `____Z`. So really `llvm-cxxfilt` can
simply forward the mangled name to the demangler and let the
library decide whether it's a valid encoding.
Compiling C++ on macOS nowadays will generate symbols with `_Z`
and `___Z` prefixes. So users trying to demangle these symbols
will have to know that they need to add the `-n` prefix. This
routinely catches people off-guard.
This patch removes the `-n` default for macOS and allows calling
into the `ItaniumDemangler` with all the `_Z` prefixes that the
demangler supports (1-4 underscores).
rdar://132714940
---
llvm/lib/Demangle/Demangle.cpp | 5 +++--
llvm/test/tools/llvm-cxxfilt/invalid.test | 6 ++++--
.../strip-underscore-default-darwin.test | 7 -------
llvm/test/tools/llvm-cxxfilt/strip-underscore.test | 12 +++++++-----
llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp | 6 +-----
5 files changed, 15 insertions(+), 21 deletions(-)
delete mode 100644 llvm/test/tools/llvm-cxxfilt/strip-underscore-default-darwin.test
diff --git a/llvm/lib/Demangle/Demangle.cpp b/llvm/lib/Demangle/Demangle.cpp
index 117b849d1c7848..f0f7eacac98e64 100644
--- a/llvm/lib/Demangle/Demangle.cpp
+++ b/llvm/lib/Demangle/Demangle.cpp
@@ -38,8 +38,9 @@ std::string llvm::demangle(std::string_view MangledName) {
}
static bool isItaniumEncoding(std::string_view S) {
- // Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.
- return starts_with(S, "_Z") || starts_with(S, "___Z");
+ // Itanium demangler supports prefixes with 1-4 underscores.
+ const size_t Pos = S.find_first_not_of('_');
+ return Pos > 0 && Pos <= 4 && S[Pos] == 'Z';
}
static bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); }
diff --git a/llvm/test/tools/llvm-cxxfilt/invalid.test b/llvm/test/tools/llvm-cxxfilt/invalid.test
index 13866f3fb90eb9..f1cf583936bcf8 100644
--- a/llvm/test/tools/llvm-cxxfilt/invalid.test
+++ b/llvm/test/tools/llvm-cxxfilt/invalid.test
@@ -1,6 +1,8 @@
-RUN: llvm-cxxfilt -n _Z1fi __Z1fi f ___ZSt1ff_block_invoke | FileCheck %s
+RUN: llvm-cxxfilt -n _Z1fi __Z1fi _____Z1fi f ___ZSt1ff_block_invoke ____ZSt1ff_block_invoke | FileCheck %s
CHECK: f(int)
-CHECK-NEXT: __Z1fi
+CHECK-NEXT: f(int)
+CHECK-NEXT: _____Z1fi
CHECK-NEXT: f
CHECK-NEXT: invocation function for block in std::f(float)
+CHECK-NEXT: invocation function for block in std::f(float)
diff --git a/llvm/test/tools/llvm-cxxfilt/strip-underscore-default-darwin.test b/llvm/test/tools/llvm-cxxfilt/strip-underscore-default-darwin.test
deleted file mode 100644
index 7dcffa05a04887..00000000000000
--- a/llvm/test/tools/llvm-cxxfilt/strip-underscore-default-darwin.test
+++ /dev/null
@@ -1,7 +0,0 @@
-REQUIRES: system-darwin
-
-## Show that on darwin, the default is to strip the leading underscore.
-
-RUN: llvm-cxxfilt __Z1fv _Z2bav | FileCheck %s
-CHECK: f()
-CHECK: _Z2bav
diff --git a/llvm/test/tools/llvm-cxxfilt/strip-underscore.test b/llvm/test/tools/llvm-cxxfilt/strip-underscore.test
index 2b7057fbfbae82..6eb0de8302c3a4 100644
--- a/llvm/test/tools/llvm-cxxfilt/strip-underscore.test
+++ b/llvm/test/tools/llvm-cxxfilt/strip-underscore.test
@@ -1,17 +1,19 @@
## Show the behaviour of --[no-]strip-underscore. This test does not test
## the platform-specific default behaviour. This is tested elsewhere.
-RUN: llvm-cxxfilt -_ __ZN2ns1fE _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-STRIPPED
-RUN: llvm-cxxfilt --strip-underscore __ZN2ns1fE _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-STRIPPED
-RUN: llvm-cxxfilt -n __ZN2ns1fE _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-UNSTRIPPED
-RUN: llvm-cxxfilt --no-strip-underscore __ZN2ns1fE _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-UNSTRIPPED
+RUN: llvm-cxxfilt -_ ___ZN2ns1fE _____Z1fi_block_invoke _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-STRIPPED
+RUN: llvm-cxxfilt --strip-underscore __ZN2ns1fE _____Z1fi_block_invoke _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-STRIPPED
+RUN: llvm-cxxfilt -n ___ZN2ns1fE _____Z1fi_block_invoke _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-UNSTRIPPED
+RUN: llvm-cxxfilt --no-strip-underscore ___ZN2ns1fE _____Z1fi_block_invoke _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-UNSTRIPPED
CHECK-STRIPPED: ns::f
+CHECK-STRIPPED: invocation function for block in f(int)
CHECK-STRIPPED: _ZSt1f
CHECK-STRIPPED: _f
CHECK-STRIPPED: ._Z3f.0v
-CHECK-UNSTRIPPED: __ZN2ns1fE
+CHECK-UNSTRIPPED: ___ZN2ns1fE
+CHECK-UNSTRIPPED: _____Z1fi_block_invoke
CHECK-UNSTRIPPED: std::f
CHECK-UNSTRIPPED: _f
CHECK-UNSTRIPPED: _._Z3f.0v
diff --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
index d63f507619a0e9..0ce2f4b2e62d42 100644
--- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
+++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
@@ -54,7 +54,7 @@ class CxxfiltOptTable : public opt::GenericOptTable {
} // namespace
static bool ParseParams;
-static bool StripUnderscore;
+static bool StripUnderscore = false;
static bool Types;
static StringRef ToolName;
@@ -165,13 +165,9 @@ int llvm_cxxfilt_main(int argc, char **argv, const llvm::ToolContext &) {
return 0;
}
- // The default value depends on the default triple. Mach-O has symbols
- // prefixed with "_", so strip by default.
if (opt::Arg *A =
Args.getLastArg(OPT_strip_underscore, OPT_no_strip_underscore))
StripUnderscore = A->getOption().matches(OPT_strip_underscore);
- else
- StripUnderscore = Triple(sys::getProcessTriple()).isOSBinFormatMachO();
ParseParams = !Args.hasArg(OPT_no_params);
>From bc6114a1263f22a67237f73a2fc2c6caafad731a Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Tue, 27 Aug 2024 17:00:09 +0100
Subject: [PATCH 2/5] fixup! remove redundant test
---
.../test/tools/llvm-cxxfilt/strip-underscore-default.test | 8 --------
1 file changed, 8 deletions(-)
delete mode 100644 llvm/test/tools/llvm-cxxfilt/strip-underscore-default.test
diff --git a/llvm/test/tools/llvm-cxxfilt/strip-underscore-default.test b/llvm/test/tools/llvm-cxxfilt/strip-underscore-default.test
deleted file mode 100644
index 6d6fa51132b240..00000000000000
--- a/llvm/test/tools/llvm-cxxfilt/strip-underscore-default.test
+++ /dev/null
@@ -1,8 +0,0 @@
-UNSUPPORTED: system-darwin
-
-## Show that on non-darwin systems, the default is to strip the leading
-## underscore.
-
-RUN: llvm-cxxfilt __Z1fv _Z2bav | FileCheck %s
-CHECK: __Z1fv
-CHECK: ba()
>From af55b9b9b7497361636fa71966ea297a9ce0cb3e Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Tue, 27 Aug 2024 21:01:23 +0100
Subject: [PATCH 3/5] fixup! simplify setting StripUnderscore
---
llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
index 0ce2f4b2e62d42..5c1ed23bbda7b2 100644
--- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
+++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
@@ -54,7 +54,7 @@ class CxxfiltOptTable : public opt::GenericOptTable {
} // namespace
static bool ParseParams;
-static bool StripUnderscore = false;
+static bool StripUnderscore;
static bool Types;
static StringRef ToolName;
@@ -165,9 +165,7 @@ int llvm_cxxfilt_main(int argc, char **argv, const llvm::ToolContext &) {
return 0;
}
- if (opt::Arg *A =
- Args.getLastArg(OPT_strip_underscore, OPT_no_strip_underscore))
- StripUnderscore = A->getOption().matches(OPT_strip_underscore);
+ StripUnderscore = Args.hasFlag(OPT_strip_underscore, OPT_no_strip_underscore, false);
ParseParams = !Args.hasArg(OPT_no_params);
>From ad1d4ca4e7d02f494f57b1704bc2bd44898d783e Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Tue, 27 Aug 2024 21:01:57 +0100
Subject: [PATCH 4/5] fixup! clang-format
---
llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
index 5c1ed23bbda7b2..f90adb6cacb990 100644
--- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
+++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
@@ -165,7 +165,8 @@ int llvm_cxxfilt_main(int argc, char **argv, const llvm::ToolContext &) {
return 0;
}
- StripUnderscore = Args.hasFlag(OPT_strip_underscore, OPT_no_strip_underscore, false);
+ StripUnderscore =
+ Args.hasFlag(OPT_strip_underscore, OPT_no_strip_underscore, false);
ParseParams = !Args.hasArg(OPT_no_params);
>From 7877a46cf72a9c2ed11303c81e9f4280ef2a8f6c Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Tue, 27 Aug 2024 21:27:05 +0100
Subject: [PATCH 5/5] fixup! add test for -n -_ override behaviour
---
llvm/test/tools/llvm-cxxfilt/strip-underscore.test | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/llvm/test/tools/llvm-cxxfilt/strip-underscore.test b/llvm/test/tools/llvm-cxxfilt/strip-underscore.test
index 6eb0de8302c3a4..60c530acf0be4a 100644
--- a/llvm/test/tools/llvm-cxxfilt/strip-underscore.test
+++ b/llvm/test/tools/llvm-cxxfilt/strip-underscore.test
@@ -1,10 +1,11 @@
-## Show the behaviour of --[no-]strip-underscore. This test does not test
-## the platform-specific default behaviour. This is tested elsewhere.
+## Show the behaviour of --[no-]strip-underscore.
RUN: llvm-cxxfilt -_ ___ZN2ns1fE _____Z1fi_block_invoke _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-STRIPPED
RUN: llvm-cxxfilt --strip-underscore __ZN2ns1fE _____Z1fi_block_invoke _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-STRIPPED
RUN: llvm-cxxfilt -n ___ZN2ns1fE _____Z1fi_block_invoke _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-UNSTRIPPED
RUN: llvm-cxxfilt --no-strip-underscore ___ZN2ns1fE _____Z1fi_block_invoke _ZSt1f _f _._Z3f.0v | FileCheck %s -check-prefix CHECK-UNSTRIPPED
+RUN: llvm-cxxfilt -n -_ _ZSt1f | FileCheck %s -check-prefix OVERRIDE-STRIPPED
+RUN: llvm-cxxfilt -_ -n _ZSt1f | FileCheck %s -check-prefix OVERRIDE-UNSTRIPPED
CHECK-STRIPPED: ns::f
CHECK-STRIPPED: invocation function for block in f(int)
@@ -17,3 +18,6 @@ CHECK-UNSTRIPPED: _____Z1fi_block_invoke
CHECK-UNSTRIPPED: std::f
CHECK-UNSTRIPPED: _f
CHECK-UNSTRIPPED: _._Z3f.0v
+
+OVERRIDE-STRIPPED: _ZSt1f
+OVERRIDE-UNSTRIPPED: std::f
More information about the llvm-commits
mailing list