|
| 1 | +/* Demangler for D/C++ - gcc and MS style mangling |
| 2 | +
|
| 3 | + Copyright (C) 2015 Free Software Foundation, Inc. |
| 4 | + Written by Rainer Schuetze (r.sagitario@gmx.de) |
| 5 | +
|
| 6 | + This file is using part of GNU Binutils, inspired by cxxfilt |
| 7 | +
|
| 8 | + This program is free software; you can redistribute it and/or modify |
| 9 | + it under the terms of the GNU General Public License as published by |
| 10 | + the Free Software Foundation; either version 3 of the License, or (at |
| 11 | + your option) any later version. |
| 12 | +
|
| 13 | + This program is distributed in the hope that it will be useful, |
| 14 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + GNU General Public License for more details. |
| 17 | +
|
| 18 | + You should have received a copy of the GNU General Public License |
| 19 | + along with GCC; see the file COPYING. If not, write to the Free |
| 20 | + Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
| 21 | + 02110-1301, USA. */ |
| 22 | + |
| 23 | +import core.demangle; |
| 24 | +import core.stdc.stdio; |
| 25 | +import core.stdc.stdlib; |
| 26 | +import core.stdc.string; |
| 27 | + |
| 28 | +extern(Windows) uint UnDecorateSymbolName(in char* DecoratedName, char* UnDecoratedName, |
| 29 | + uint UndecoratedLength, uint Flags); |
| 30 | +extern(C) char* dlang_demangle(const char* mangled_name, uint flags); |
| 31 | +extern(C) char* cplus_demangle(const char* mangled_name, uint flags); |
| 32 | + |
| 33 | +enum DMGL_PARAMS = (1 << 0); /* Include function args */ |
| 34 | +enum DMGL_ANSI = (1 << 1); /* Include const, volatile, etc */ |
| 35 | +enum DMGL_VERBOSE = (1 << 3); /* Include implementation details. */ |
| 36 | + |
| 37 | +uint flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE; |
| 38 | +char msvc_buffer[32768]; |
| 39 | + |
| 40 | +char* msvc_demangle (char *mangled_name) |
| 41 | +{ |
| 42 | + if (UnDecorateSymbolName (mangled_name, msvc_buffer.ptr, msvc_buffer.length, 0) == 0) |
| 43 | + return null; |
| 44 | + return msvc_buffer.ptr; |
| 45 | +} |
| 46 | + |
| 47 | +char* d_demangle (char[] mangled) |
| 48 | +{ |
| 49 | + size_t pos = 0; |
| 50 | + string s = decodeDmdString (mangled, pos); |
| 51 | + char[] obuf; |
| 52 | + if (pos == mangled.length) |
| 53 | + obuf = demangle(s, msvc_buffer); |
| 54 | + else |
| 55 | + obuf = demangle(mangled, msvc_buffer); |
| 56 | + if (obuf.ptr != msvc_buffer.ptr) |
| 57 | + return null; |
| 58 | + msvc_buffer[obuf.length] = 0; |
| 59 | + return msvc_buffer.ptr; |
| 60 | +} |
| 61 | + |
| 62 | +void print_demangled (char[] mangled) |
| 63 | +{ |
| 64 | + char *result; |
| 65 | + char[] initial = mangled; |
| 66 | + |
| 67 | + /* . and $ are sometimes found at the start of function names |
| 68 | + in assembler sources in order to distinguish them from other |
| 69 | + names (eg register names). So skip them here. */ |
| 70 | + if (mangled[0] == '.' || mangled[0] == '$') |
| 71 | + mangled = mangled[1..$]; |
| 72 | + |
| 73 | + if (mangled.length > 1 && mangled[0] == '_' && mangled[1] == 'D') |
| 74 | + result = d_demangle (mangled); |
| 75 | + else if (mangled.length > 0 && mangled[0] == '?') |
| 76 | + result = msvc_demangle (mangled.ptr); |
| 77 | + else |
| 78 | + result = cplus_demangle (mangled.ptr, flags); |
| 79 | + |
| 80 | + if (result == null) |
| 81 | + printf ("%s", initial.ptr); |
| 82 | + else |
| 83 | + { |
| 84 | + if (initial.ptr != mangled.ptr) |
| 85 | + putchar (initial[0]); |
| 86 | + printf ("%s", result); |
| 87 | + if (result != msvc_buffer.ptr) |
| 88 | + free (result); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +bool isAlnum(int c) |
| 93 | +{ |
| 94 | + return c <= 'z' && c >= '0' && (c <= '9' || c >= 'a' || (c >= 'A' && c <= 'Z')); |
| 95 | +} |
| 96 | + |
| 97 | +int main (char[][] argv) |
| 98 | +{ |
| 99 | + int c; |
| 100 | + const char *valid_symbols = "_$.?@"; |
| 101 | + |
| 102 | + if (argv.length > 1) |
| 103 | + { |
| 104 | + foreach(a; argv[1..$]) |
| 105 | + { |
| 106 | + print_demangled(a); |
| 107 | + putchar ('\n'); |
| 108 | + } |
| 109 | + return 0; |
| 110 | + } |
| 111 | + for (;;) |
| 112 | + { |
| 113 | + static char mbuffer[32767]; |
| 114 | + uint i = 0; |
| 115 | + |
| 116 | + c = getchar (); |
| 117 | + /* Try to read a mangled name. Assume non-ascii characters to be part of the name */ |
| 118 | + while (c != EOF && (isAlnum (c) || strchr (valid_symbols, c) || c >= 128)) |
| 119 | + { |
| 120 | + if (i >= mbuffer.length - 1) |
| 121 | + break; |
| 122 | + mbuffer[i++] = cast(char) c; |
| 123 | + c = getchar (); |
| 124 | + } |
| 125 | + |
| 126 | + if (i > 0) |
| 127 | + { |
| 128 | + mbuffer[i] = 0; |
| 129 | + print_demangled (mbuffer[0..i]); |
| 130 | + } |
| 131 | + |
| 132 | + if (c == EOF) |
| 133 | + break; |
| 134 | + |
| 135 | + /* Echo the whitespace characters so that the output looks |
| 136 | + like the input, only with the mangled names demangled. */ |
| 137 | + putchar (c); |
| 138 | + if (c == '\n') |
| 139 | + fflush (stdout); |
| 140 | + } |
| 141 | + |
| 142 | + fflush (stdout); |
| 143 | + return 0; |
| 144 | +} |
0 commit comments