Commit | Line | Data |
---|---|---|
bb279dc0 | 1 | /* Demangler for GNU C++ - main program |
b3adc24a | 2 | Copyright (C) 1989-2020 Free Software Foundation, Inc. |
bb279dc0 ZW |
3 | Written by James Clark (jjc@jclark.uucp) |
4 | Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling | |
5 | Modified by Satish Pai (pai@apollo.hp.com) for HP demangling | |
6 | ||
32866df7 | 7 | This file is part of GNU Binutils. |
bb279dc0 | 8 | |
32866df7 NC |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or (at | |
12 | your option) any later version. | |
bb279dc0 | 13 | |
32866df7 NC |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
bb279dc0 | 18 | |
cbf1f5df NC |
19 | You should have received a copy of the GNU General Public License |
20 | along with GCC; see the file COPYING. If not, write to the Free | |
21 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA | |
22 | 02110-1301, USA. */ | |
bb279dc0 | 23 | |
3db64b00 | 24 | #include "sysdep.h" |
bb279dc0 | 25 | #include "bfd.h" |
bb279dc0 ZW |
26 | #include "libiberty.h" |
27 | #include "demangle.h" | |
28 | #include "getopt.h" | |
29 | #include "safe-ctype.h" | |
3db64b00 | 30 | #include "bucomm.h" |
bb279dc0 | 31 | |
ec948987 | 32 | static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE; |
cbf1f5df | 33 | static int strip_underscore = TARGET_PREPENDS_UNDERSCORE; |
bb279dc0 | 34 | |
cbf1f5df NC |
35 | static const struct option long_options[] = |
36 | { | |
37 | {"strip-underscore", no_argument, NULL, '_'}, | |
38 | {"format", required_argument, NULL, 's'}, | |
39 | {"help", no_argument, NULL, 'h'}, | |
40 | {"no-params", no_argument, NULL, 'p'}, | |
41 | {"no-strip-underscores", no_argument, NULL, 'n'}, | |
cbf1f5df | 42 | {"no-verbose", no_argument, NULL, 'i'}, |
ec948987 | 43 | {"types", no_argument, NULL, 't'}, |
cbf1f5df | 44 | {"version", no_argument, NULL, 'v'}, |
af03af8f NC |
45 | {"recurse-limit", no_argument, NULL, 'R'}, |
46 | {"recursion-limit", no_argument, NULL, 'R'}, | |
47 | {"no-recurse-limit", no_argument, NULL, 'r'}, | |
48 | {"no-recursion-limit", no_argument, NULL, 'r'}, | |
cbf1f5df NC |
49 | {NULL, no_argument, NULL, 0} |
50 | }; | |
bb279dc0 ZW |
51 | |
52 | static void | |
2da42df6 | 53 | demangle_it (char *mangled_name) |
bb279dc0 ZW |
54 | { |
55 | char *result; | |
cbf1f5df NC |
56 | unsigned int skip_first = 0; |
57 | ||
ec948987 NC |
58 | /* _ and $ are sometimes found at the start of function names |
59 | in assembler sources in order to distinguish them from other | |
60 | names (eg register names). So skip them here. */ | |
cbf1f5df NC |
61 | if (mangled_name[0] == '.' || mangled_name[0] == '$') |
62 | ++skip_first; | |
63 | if (strip_underscore && mangled_name[skip_first] == '_') | |
64 | ++skip_first; | |
65 | ||
66 | result = cplus_demangle (mangled_name + skip_first, flags); | |
bb279dc0 | 67 | |
bb279dc0 | 68 | if (result == NULL) |
1afcb04c | 69 | printf ("%s", mangled_name); |
bb279dc0 ZW |
70 | else |
71 | { | |
cbf1f5df NC |
72 | if (mangled_name[0] == '.') |
73 | putchar ('.'); | |
1afcb04c | 74 | printf ("%s", result); |
bb279dc0 ZW |
75 | free (result); |
76 | } | |
77 | } | |
78 | ||
2da42df6 AJ |
79 | static void |
80 | print_demangler_list (FILE *stream) | |
bb279dc0 | 81 | { |
2da42df6 | 82 | const struct demangler_engine *demangler; |
bb279dc0 ZW |
83 | |
84 | fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name); | |
2da42df6 | 85 | |
bb279dc0 ZW |
86 | for (demangler = libiberty_demanglers + 1; |
87 | demangler->demangling_style != unknown_demangling; | |
88 | ++demangler) | |
89 | fprintf (stream, ",%s", demangler->demangling_style_name); | |
90 | ||
91 | fprintf (stream, "}"); | |
92 | } | |
93 | ||
1e0f0b4d | 94 | ATTRIBUTE_NORETURN static void |
2da42df6 | 95 | usage (FILE *stream, int status) |
bb279dc0 ZW |
96 | { |
97 | fprintf (stream, "\ | |
cbf1f5df | 98 | Usage: %s [options] [mangled names]\n", program_name); |
bb279dc0 | 99 | fprintf (stream, "\ |
cbf1f5df NC |
100 | Options are:\n\ |
101 | [-_|--strip-underscore] Ignore first leading underscore%s\n", | |
102 | TARGET_PREPENDS_UNDERSCORE ? " (default)" : ""); | |
103 | fprintf (stream, "\ | |
104 | [-n|--no-strip-underscore] Do not ignore a leading underscore%s\n", | |
105 | TARGET_PREPENDS_UNDERSCORE ? "" : " (default)"); | |
bb279dc0 | 106 | fprintf (stream, "\ |
cbf1f5df | 107 | [-p|--no-params] Do not display function arguments\n\ |
cbf1f5df | 108 | [-i|--no-verbose] Do not show implementation details (if any)\n\ |
af03af8f NC |
109 | [-R|--recurse-limit] Enable a limit on recursion whilst demangling. [Default]\n\ |
110 | ]-r|--no-recurse-limit] Disable a limit on recursion whilst demangling\n\ | |
ec948987 | 111 | [-t|--types] Also attempt to demangle type encodings\n\ |
cbf1f5df | 112 | [-s|--format "); |
bb279dc0 ZW |
113 | print_demangler_list (stream); |
114 | fprintf (stream, "]\n"); | |
115 | ||
116 | fprintf (stream, "\ | |
cbf1f5df NC |
117 | [@<file>] Read extra options from <file>\n\ |
118 | [-h|--help] Display this information\n\ | |
119 | [-v|--version] Show the version information\n\ | |
120 | Demangled names are displayed to stdout.\n\ | |
121 | If a name cannot be demangled it is just echoed to stdout.\n\ | |
122 | If no names are provided on the command line, stdin is read.\n"); | |
92f01d61 JM |
123 | if (REPORT_BUGS_TO[0] && status == 0) |
124 | fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO); | |
bb279dc0 ZW |
125 | exit (status); |
126 | } | |
127 | ||
2da42df6 | 128 | /* Return the string of non-alnum characters that may occur |
bb279dc0 ZW |
129 | as a valid symbol component, in the standard assembler symbol |
130 | syntax. */ | |
131 | ||
132 | static const char * | |
2da42df6 | 133 | standard_symbol_characters (void) |
bb279dc0 ZW |
134 | { |
135 | return "_$."; | |
136 | } | |
137 | ||
2da42df6 | 138 | extern int main (int, char **); |
bb279dc0 ZW |
139 | |
140 | int | |
2da42df6 | 141 | main (int argc, char **argv) |
bb279dc0 | 142 | { |
bb279dc0 ZW |
143 | int c; |
144 | const char *valid_symbols; | |
145 | enum demangling_styles style = auto_demangling; | |
146 | ||
147 | program_name = argv[0]; | |
148 | xmalloc_set_program_name (program_name); | |
86eafac0 | 149 | bfd_set_error_program_name (program_name); |
bb279dc0 | 150 | |
869b9d07 MM |
151 | expandargv (&argc, &argv); |
152 | ||
af03af8f | 153 | while ((c = getopt_long (argc, argv, "_hinprRs:tv", long_options, (int *) 0)) != EOF) |
bb279dc0 ZW |
154 | { |
155 | switch (c) | |
156 | { | |
157 | case '?': | |
158 | usage (stderr, 1); | |
159 | break; | |
160 | case 'h': | |
161 | usage (stdout, 0); | |
162 | case 'n': | |
163 | strip_underscore = 0; | |
164 | break; | |
4e48c9dd ILT |
165 | case 'p': |
166 | flags &= ~ DMGL_PARAMS; | |
167 | break; | |
af03af8f NC |
168 | case 'r': |
169 | flags |= DMGL_NO_RECURSE_LIMIT; | |
170 | break; | |
171 | case 'R': | |
172 | flags &= ~ DMGL_NO_RECURSE_LIMIT; | |
173 | break; | |
cbf1f5df | 174 | case 't': |
ec948987 | 175 | flags |= DMGL_TYPES; |
cbf1f5df NC |
176 | break; |
177 | case 'i': | |
178 | flags &= ~ DMGL_VERBOSE; | |
179 | break; | |
bb279dc0 ZW |
180 | case 'v': |
181 | print_version ("c++filt"); | |
cbf1f5df | 182 | return 0; |
bb279dc0 ZW |
183 | case '_': |
184 | strip_underscore = 1; | |
185 | break; | |
186 | case 's': | |
cbf1f5df NC |
187 | style = cplus_demangle_name_to_style (optarg); |
188 | if (style == unknown_demangling) | |
189 | { | |
190 | fprintf (stderr, "%s: unknown demangling style `%s'\n", | |
191 | program_name, optarg); | |
192 | return 1; | |
193 | } | |
194 | cplus_demangle_set_style (style); | |
bb279dc0 ZW |
195 | break; |
196 | } | |
197 | } | |
198 | ||
199 | if (optind < argc) | |
200 | { | |
201 | for ( ; optind < argc; optind++) | |
ec948987 NC |
202 | { |
203 | demangle_it (argv[optind]); | |
204 | putchar ('\n'); | |
205 | } | |
cbf1f5df NC |
206 | |
207 | return 0; | |
bb279dc0 | 208 | } |
cbf1f5df NC |
209 | |
210 | switch (current_demangling_style) | |
bb279dc0 | 211 | { |
236f4ebe NC |
212 | case auto_demangling: |
213 | case gnu_v3_demangling: | |
cbf1f5df | 214 | case java_demangling: |
cbf1f5df | 215 | case gnat_demangling: |
da37262b | 216 | case dlang_demangling: |
a85db0a6 | 217 | case rust_demangling: |
236f4ebe | 218 | valid_symbols = standard_symbol_characters (); |
cbf1f5df NC |
219 | break; |
220 | default: | |
221 | /* Folks should explicitly indicate the appropriate alphabet for | |
222 | each demangling. Providing a default would allow the | |
223 | question to go unconsidered. */ | |
224 | fatal ("Internal error: no symbol alphabet for current style"); | |
225 | } | |
226 | ||
227 | for (;;) | |
228 | { | |
229 | static char mbuffer[32767]; | |
230 | unsigned i = 0; | |
231 | ||
232 | c = getchar (); | |
233 | /* Try to read a mangled name. */ | |
234 | while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c))) | |
bb279dc0 | 235 | { |
cbf1f5df NC |
236 | if (i >= sizeof (mbuffer) - 1) |
237 | break; | |
238 | mbuffer[i++] = c; | |
239 | c = getchar (); | |
bb279dc0 ZW |
240 | } |
241 | ||
cbf1f5df | 242 | if (i > 0) |
bb279dc0 | 243 | { |
cbf1f5df NC |
244 | mbuffer[i] = 0; |
245 | demangle_it (mbuffer); | |
bb279dc0 | 246 | } |
ec948987 | 247 | |
cbf1f5df NC |
248 | if (c == EOF) |
249 | break; | |
ec948987 NC |
250 | |
251 | /* Echo the whitespace characters so that the output looks | |
252 | like the input, only with the mangled names demangled. */ | |
253 | putchar (c); | |
02aec879 AH |
254 | if (c == '\n') |
255 | fflush (stdout); | |
bb279dc0 ZW |
256 | } |
257 | ||
ec948987 | 258 | fflush (stdout); |
cbf1f5df | 259 | return 0; |
bb279dc0 | 260 | } |