]>
Commit | Line | Data |
---|---|---|
bb279dc0 ZW |
1 | /* Demangler for GNU C++ - main program |
2 | Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999, | |
cbf1f5df | 3 | 2000, 2001, 2002, 2003, 2005 Free Software Foundation, Inc. |
bb279dc0 ZW |
4 | Written by James Clark (jjc@jclark.uucp) |
5 | Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling | |
6 | Modified by Satish Pai (pai@apollo.hp.com) for HP demangling | |
7 | ||
cbf1f5df | 8 | This file is part of GCC. |
bb279dc0 | 9 | |
cbf1f5df NC |
10 | GCC is free software; you can redistribute it and/or modify it under |
11 | the terms of the GNU General Public License as published by the Free | |
12 | Software Foundation; either version 2, or (at your option) any later | |
13 | version. | |
bb279dc0 | 14 | |
cbf1f5df NC |
15 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
16 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 | for more details. | |
bb279dc0 | 19 | |
cbf1f5df NC |
20 | You should have received a copy of the GNU General Public License |
21 | along with GCC; see the file COPYING. If not, write to the Free | |
22 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA | |
23 | 02110-1301, USA. */ | |
bb279dc0 ZW |
24 | |
25 | #include "config.h" | |
26 | #include "bfd.h" | |
27 | #include "bucomm.h" | |
28 | #include "libiberty.h" | |
29 | #include "demangle.h" | |
30 | #include "getopt.h" | |
31 | #include "safe-ctype.h" | |
32 | ||
cbf1f5df NC |
33 | static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE | DMGL_TYPES; |
34 | static int strip_underscore = TARGET_PREPENDS_UNDERSCORE; | |
bb279dc0 | 35 | |
cbf1f5df NC |
36 | static const struct option long_options[] = |
37 | { | |
38 | {"strip-underscore", no_argument, NULL, '_'}, | |
39 | {"format", required_argument, NULL, 's'}, | |
40 | {"help", no_argument, NULL, 'h'}, | |
41 | {"no-params", no_argument, NULL, 'p'}, | |
42 | {"no-strip-underscores", no_argument, NULL, 'n'}, | |
43 | {"no-types", no_argument, NULL, 't'}, | |
44 | {"no-verbose", no_argument, NULL, 'i'}, | |
45 | {"version", no_argument, NULL, 'v'}, | |
46 | {NULL, no_argument, NULL, 0} | |
47 | }; | |
bb279dc0 ZW |
48 | |
49 | static void | |
2da42df6 | 50 | demangle_it (char *mangled_name) |
bb279dc0 ZW |
51 | { |
52 | char *result; | |
cbf1f5df NC |
53 | unsigned int skip_first = 0; |
54 | ||
55 | if (mangled_name[0] == '.' || mangled_name[0] == '$') | |
56 | ++skip_first; | |
57 | if (strip_underscore && mangled_name[skip_first] == '_') | |
58 | ++skip_first; | |
59 | ||
60 | result = cplus_demangle (mangled_name + skip_first, flags); | |
bb279dc0 | 61 | |
bb279dc0 | 62 | if (result == NULL) |
cbf1f5df | 63 | puts (mangled_name); |
bb279dc0 ZW |
64 | else |
65 | { | |
cbf1f5df NC |
66 | if (mangled_name[0] == '.') |
67 | putchar ('.'); | |
68 | puts (result); | |
bb279dc0 ZW |
69 | free (result); |
70 | } | |
71 | } | |
72 | ||
2da42df6 AJ |
73 | static void |
74 | print_demangler_list (FILE *stream) | |
bb279dc0 | 75 | { |
2da42df6 | 76 | const struct demangler_engine *demangler; |
bb279dc0 ZW |
77 | |
78 | fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name); | |
2da42df6 | 79 | |
bb279dc0 ZW |
80 | for (demangler = libiberty_demanglers + 1; |
81 | demangler->demangling_style != unknown_demangling; | |
82 | ++demangler) | |
83 | fprintf (stream, ",%s", demangler->demangling_style_name); | |
84 | ||
85 | fprintf (stream, "}"); | |
86 | } | |
87 | ||
88 | static void | |
2da42df6 | 89 | usage (FILE *stream, int status) |
bb279dc0 ZW |
90 | { |
91 | fprintf (stream, "\ | |
cbf1f5df | 92 | Usage: %s [options] [mangled names]\n", program_name); |
bb279dc0 | 93 | fprintf (stream, "\ |
cbf1f5df NC |
94 | Options are:\n\ |
95 | [-_|--strip-underscore] Ignore first leading underscore%s\n", | |
96 | TARGET_PREPENDS_UNDERSCORE ? " (default)" : ""); | |
97 | fprintf (stream, "\ | |
98 | [-n|--no-strip-underscore] Do not ignore a leading underscore%s\n", | |
99 | TARGET_PREPENDS_UNDERSCORE ? "" : " (default)"); | |
bb279dc0 | 100 | fprintf (stream, "\ |
cbf1f5df NC |
101 | [-p|--no-params] Do not display function arguments\n\ |
102 | [-t|--no-types] Do not try to demangle type encodings\n\ | |
103 | [-i|--no-verbose] Do not show implementation details (if any)\n\ | |
104 | [-s|--format "); | |
bb279dc0 ZW |
105 | print_demangler_list (stream); |
106 | fprintf (stream, "]\n"); | |
107 | ||
108 | fprintf (stream, "\ | |
cbf1f5df NC |
109 | [@<file>] Read extra options from <file>\n\ |
110 | [-h|--help] Display this information\n\ | |
111 | [-v|--version] Show the version information\n\ | |
112 | Demangled names are displayed to stdout.\n\ | |
113 | If a name cannot be demangled it is just echoed to stdout.\n\ | |
114 | If no names are provided on the command line, stdin is read.\n"); | |
bb279dc0 ZW |
115 | exit (status); |
116 | } | |
117 | ||
2da42df6 | 118 | /* Return the string of non-alnum characters that may occur |
bb279dc0 ZW |
119 | as a valid symbol component, in the standard assembler symbol |
120 | syntax. */ | |
121 | ||
122 | static const char * | |
2da42df6 | 123 | standard_symbol_characters (void) |
bb279dc0 ZW |
124 | { |
125 | return "_$."; | |
126 | } | |
127 | ||
bb279dc0 ZW |
128 | /* Return the string of non-alnum characters that may occur |
129 | as a valid symbol name component in an HP object file. | |
130 | ||
131 | Note that, since HP's compiler generates object code straight from | |
132 | C++ source, without going through an assembler, its mangled | |
133 | identifiers can use all sorts of characters that no assembler would | |
134 | tolerate, so the alphabet this function creates is a little odd. | |
135 | Here are some sample mangled identifiers offered by HP: | |
136 | ||
137 | typeid*__XT24AddressIndExpClassMember_ | |
138 | [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv | |
139 | __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv | |
140 | ||
141 | This still seems really weird to me, since nowhere else in this | |
142 | file is there anything to recognize curly brackets, parens, etc. | |
143 | I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me | |
144 | this is right, but I still strongly suspect that there's a | |
145 | misunderstanding here. | |
146 | ||
147 | If we decide it's better for c++filt to use HP's assembler syntax | |
148 | to scrape identifiers out of its input, here's the definition of | |
149 | the symbol name syntax from the HP assembler manual: | |
150 | ||
151 | Symbols are composed of uppercase and lowercase letters, decimal | |
152 | digits, dollar symbol, period (.), ampersand (&), pound sign(#) and | |
153 | underscore (_). A symbol can begin with a letter, digit underscore or | |
154 | dollar sign. If a symbol begins with a digit, it must contain a | |
155 | non-digit character. | |
156 | ||
157 | So have fun. */ | |
158 | static const char * | |
2da42df6 | 159 | hp_symbol_characters (void) |
bb279dc0 ZW |
160 | { |
161 | return "_$.<>#,*&[]:(){}"; | |
162 | } | |
163 | ||
2da42df6 | 164 | extern int main (int, char **); |
bb279dc0 ZW |
165 | |
166 | int | |
2da42df6 | 167 | main (int argc, char **argv) |
bb279dc0 | 168 | { |
bb279dc0 ZW |
169 | int c; |
170 | const char *valid_symbols; | |
171 | enum demangling_styles style = auto_demangling; | |
172 | ||
173 | program_name = argv[0]; | |
174 | xmalloc_set_program_name (program_name); | |
175 | ||
869b9d07 MM |
176 | expandargv (&argc, &argv); |
177 | ||
cbf1f5df | 178 | while ((c = getopt_long (argc, argv, "_hinps:tv", long_options, (int *) 0)) != EOF) |
bb279dc0 ZW |
179 | { |
180 | switch (c) | |
181 | { | |
182 | case '?': | |
183 | usage (stderr, 1); | |
184 | break; | |
185 | case 'h': | |
186 | usage (stdout, 0); | |
187 | case 'n': | |
188 | strip_underscore = 0; | |
189 | break; | |
4e48c9dd ILT |
190 | case 'p': |
191 | flags &= ~ DMGL_PARAMS; | |
192 | break; | |
cbf1f5df NC |
193 | case 't': |
194 | flags &= ~ DMGL_TYPES; | |
195 | break; | |
196 | case 'i': | |
197 | flags &= ~ DMGL_VERBOSE; | |
198 | break; | |
bb279dc0 ZW |
199 | case 'v': |
200 | print_version ("c++filt"); | |
cbf1f5df | 201 | return 0; |
bb279dc0 ZW |
202 | case '_': |
203 | strip_underscore = 1; | |
204 | break; | |
205 | case 's': | |
cbf1f5df NC |
206 | style = cplus_demangle_name_to_style (optarg); |
207 | if (style == unknown_demangling) | |
208 | { | |
209 | fprintf (stderr, "%s: unknown demangling style `%s'\n", | |
210 | program_name, optarg); | |
211 | return 1; | |
212 | } | |
213 | cplus_demangle_set_style (style); | |
bb279dc0 ZW |
214 | break; |
215 | } | |
216 | } | |
217 | ||
218 | if (optind < argc) | |
219 | { | |
220 | for ( ; optind < argc; optind++) | |
cbf1f5df NC |
221 | demangle_it (argv[optind]); |
222 | ||
223 | return 0; | |
bb279dc0 | 224 | } |
cbf1f5df NC |
225 | |
226 | switch (current_demangling_style) | |
bb279dc0 | 227 | { |
cbf1f5df NC |
228 | case gnu_demangling: |
229 | case lucid_demangling: | |
230 | case arm_demangling: | |
231 | case java_demangling: | |
232 | case edg_demangling: | |
233 | case gnat_demangling: | |
234 | case gnu_v3_demangling: | |
235 | case auto_demangling: | |
236 | valid_symbols = standard_symbol_characters (); | |
237 | break; | |
238 | case hp_demangling: | |
239 | valid_symbols = hp_symbol_characters (); | |
240 | break; | |
241 | default: | |
242 | /* Folks should explicitly indicate the appropriate alphabet for | |
243 | each demangling. Providing a default would allow the | |
244 | question to go unconsidered. */ | |
245 | fatal ("Internal error: no symbol alphabet for current style"); | |
246 | } | |
247 | ||
248 | for (;;) | |
249 | { | |
250 | static char mbuffer[32767]; | |
251 | unsigned i = 0; | |
252 | ||
253 | c = getchar (); | |
254 | /* Try to read a mangled name. */ | |
255 | while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c))) | |
bb279dc0 | 256 | { |
cbf1f5df NC |
257 | if (i >= sizeof (mbuffer) - 1) |
258 | break; | |
259 | mbuffer[i++] = c; | |
260 | c = getchar (); | |
bb279dc0 ZW |
261 | } |
262 | ||
cbf1f5df | 263 | if (i > 0) |
bb279dc0 | 264 | { |
cbf1f5df NC |
265 | mbuffer[i] = 0; |
266 | demangle_it (mbuffer); | |
bb279dc0 ZW |
267 | fflush (stdout); |
268 | } | |
cbf1f5df NC |
269 | if (c == EOF) |
270 | break; | |
bb279dc0 ZW |
271 | } |
272 | ||
cbf1f5df | 273 | return 0; |
bb279dc0 | 274 | } |