Skip to content

Commit 4573f4c

Browse files
committed
Update LLVM lib/Demangle to 87039c048c0cbc3d8cbba86187269b006bf2f373
This adds support for demangling Rust symbols. Added two simple test cases for demangling Rust. Ran: cp ~/src/llvm-project/llvm/include/llvm/Demangle/*.h third_party/llvm/include/llvm/Demangle/ cp ~/src/llvm-project/llvm/lib/Demangle/*.cpp third_party/llvm/lib/Demangle/ cp ~/src/llvm-project/llvm/LICENSE.TXT third_party/llvm/LICENSE.txt
1 parent 282b288 commit 4573f4c

16 files changed

+1891
-110
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ endif()
3333
include_directories(third_party/llvm/include)
3434
add_executable(demumble
3535
demumble.cc
36+
third_party/llvm/lib/Demangle/Demangle.cpp
3637
third_party/llvm/lib/Demangle/ItaniumDemangle.cpp
3738
third_party/llvm/lib/Demangle/MicrosoftDemangle.cpp
3839
third_party/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
40+
third_party/llvm/lib/Demangle/RustDemangle.cpp
3941
)
4042
set_target_properties(demumble PROPERTIES CXX_STANDARD 14
4143
CXX_STANDARD_REQUIRED ON)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# demumble
22

3-
`demumble` demangles both Itanium and Visual Studio symbols. It runs on both
4-
POSIX and Windows.
3+
`demumble` demangles both Itanium, Rust, and Visual Studio symbols. It runs on
4+
both POSIX and Windows.
55

66
$ demumble _Z4funcPci
77
func(char*, int)
88
$ demumble '?Fx_i@@YAHP6AHH@Z@Z'
99
int __cdecl Fx_i(int (__cdecl *)(int))
10-
10+
1111
## Download
1212

1313
There are prebuilt x64 binaries for Linux, Mac (10.9+), and Windows on the

demumble.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ static void print_demangled(const char* format, const char* s, size_t* n_used) {
2626
if (char* itanium = llvm::itaniumDemangle(s, NULL, NULL, NULL)) {
2727
printf(format, itanium, s);
2828
free(itanium);
29+
} else if (char* rust = llvm::rustDemangle(s, NULL, NULL, NULL)) {
30+
printf(format, rust, s);
31+
free(rust);
2932
} else if (char* ms = llvm::microsoftDemangle(s, n_used, NULL, NULL, NULL)) {
3033
printf(format, ms, s);
3134
free(ms);
@@ -39,6 +42,12 @@ static bool is_mangle_char_itanium(char c) {
3942
(c >= '0' && c <= '9') || c == '_' || c == '$';
4043
}
4144

45+
static bool is_mangle_char_rust(char c) {
46+
// See https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html.
47+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
48+
(c >= '0' && c <= '9') || c == '_';
49+
}
50+
4251
static bool is_mangle_char_win(char c) {
4352
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
4453
(c >= '0' && c <= '9') || strchr("?_@$", c);
@@ -53,6 +62,11 @@ static bool is_plausible_itanium_prefix(char* s) {
5362
return strstr(prefix, "_Z");
5463
}
5564

65+
static bool is_plausible_rust_prefix(char* s) {
66+
// Rust symbols start with "_R".
67+
return s[0] == '_' && s[1] == 'R';
68+
}
69+
5670
static char buf[8192];
5771
int main(int argc, char* argv[]) {
5872
enum { kPrintAll, kPrintMatching } print_mode = kPrintAll;
@@ -121,6 +135,9 @@ int main(int argc, char* argv[]) {
121135
else if (is_plausible_itanium_prefix(cur))
122136
while (cur + n_sym != end && is_mangle_char_itanium(cur[n_sym]))
123137
++n_sym;
138+
else if (is_plausible_rust_prefix(cur))
139+
while (cur + n_sym != end && is_mangle_char_rust(cur[n_sym]))
140+
++n_sym;
124141
else {
125142
if (print_mode == kPrintAll)
126143
printf("_");

demumble_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
('demumble hello', 'hello\n'),
77
('demumble _Z4funcPci _Z1fv', 'func(char*, int)\nf()\n'),
88
('demumble < _Z4funcPci _Z1fv', 'func(char*, int)\nf()\n'),
9+
('demumble _RINvNtC3std3mem8align_ofdE _RNvNvC5mylib3foo3bar',
10+
'std::mem::align_of::<f64>\nmylib::foo::bar\n'),
11+
('demumble < _RINvNtC3std3mem8align_ofdE _RNvNvC5mylib3foo3bar',
12+
'std::mem::align_of::<f64>\nmylib::foo::bar\n'),
913
('demumble ?Fxi@@YAHP6AHH@Z@Z', 'int __cdecl Fxi(int (__cdecl *)(int))\n'),
1014
('demumble ??0S@@QEAA@$$QEAU0@@Z', 'public: __cdecl S::S(struct S &&)\n'),
1115
('demumble ??_C@_02PCEFGMJL@hi?$AA@', '"hi"\n'),

0 commit comments

Comments
 (0)