daily update
[binutils.git] / binutils / cxxfilt.c
bloba8ec43ba1180823fe4e65aa372ccb17e386be922
1 /* Demangler for GNU C++ - main program
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
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
8 This file is part of GCC.
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.
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.
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. */
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"
33 static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
34 static int strip_underscore = TARGET_PREPENDS_UNDERSCORE;
36 static const struct option long_options[] =
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-verbose", no_argument, NULL, 'i'},
44 {"types", no_argument, NULL, 't'},
45 {"version", no_argument, NULL, 'v'},
46 {NULL, no_argument, NULL, 0}
49 static void
50 demangle_it (char *mangled_name)
52 char *result;
53 unsigned int skip_first = 0;
55 /* _ and $ are sometimes found at the start of function names
56 in assembler sources in order to distinguish them from other
57 names (eg register names). So skip them here. */
58 if (mangled_name[0] == '.' || mangled_name[0] == '$')
59 ++skip_first;
60 if (strip_underscore && mangled_name[skip_first] == '_')
61 ++skip_first;
63 result = cplus_demangle (mangled_name + skip_first, flags);
65 if (result == NULL)
66 printf (mangled_name);
67 else
69 if (mangled_name[0] == '.')
70 putchar ('.');
71 printf (result);
72 free (result);
76 static void
77 print_demangler_list (FILE *stream)
79 const struct demangler_engine *demangler;
81 fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
83 for (demangler = libiberty_demanglers + 1;
84 demangler->demangling_style != unknown_demangling;
85 ++demangler)
86 fprintf (stream, ",%s", demangler->demangling_style_name);
88 fprintf (stream, "}");
91 static void
92 usage (FILE *stream, int status)
94 fprintf (stream, "\
95 Usage: %s [options] [mangled names]\n", program_name);
96 fprintf (stream, "\
97 Options are:\n\
98 [-_|--strip-underscore] Ignore first leading underscore%s\n",
99 TARGET_PREPENDS_UNDERSCORE ? " (default)" : "");
100 fprintf (stream, "\
101 [-n|--no-strip-underscore] Do not ignore a leading underscore%s\n",
102 TARGET_PREPENDS_UNDERSCORE ? "" : " (default)");
103 fprintf (stream, "\
104 [-p|--no-params] Do not display function arguments\n\
105 [-i|--no-verbose] Do not show implementation details (if any)\n\
106 [-t|--types] Also attempt to demangle type encodings\n\
107 [-s|--format ");
108 print_demangler_list (stream);
109 fprintf (stream, "]\n");
111 fprintf (stream, "\
112 [@<file>] Read extra options from <file>\n\
113 [-h|--help] Display this information\n\
114 [-v|--version] Show the version information\n\
115 Demangled names are displayed to stdout.\n\
116 If a name cannot be demangled it is just echoed to stdout.\n\
117 If no names are provided on the command line, stdin is read.\n");
118 exit (status);
121 /* Return the string of non-alnum characters that may occur
122 as a valid symbol component, in the standard assembler symbol
123 syntax. */
125 static const char *
126 standard_symbol_characters (void)
128 return "_$.";
131 /* Return the string of non-alnum characters that may occur
132 as a valid symbol name component in an HP object file.
134 Note that, since HP's compiler generates object code straight from
135 C++ source, without going through an assembler, its mangled
136 identifiers can use all sorts of characters that no assembler would
137 tolerate, so the alphabet this function creates is a little odd.
138 Here are some sample mangled identifiers offered by HP:
140 typeid*__XT24AddressIndExpClassMember_
141 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
142 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
144 This still seems really weird to me, since nowhere else in this
145 file is there anything to recognize curly brackets, parens, etc.
146 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
147 this is right, but I still strongly suspect that there's a
148 misunderstanding here.
150 If we decide it's better for c++filt to use HP's assembler syntax
151 to scrape identifiers out of its input, here's the definition of
152 the symbol name syntax from the HP assembler manual:
154 Symbols are composed of uppercase and lowercase letters, decimal
155 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
156 underscore (_). A symbol can begin with a letter, digit underscore or
157 dollar sign. If a symbol begins with a digit, it must contain a
158 non-digit character.
160 So have fun. */
161 static const char *
162 hp_symbol_characters (void)
164 return "_$.<>#,*&[]:(){}";
167 extern int main (int, char **);
170 main (int argc, char **argv)
172 int c;
173 const char *valid_symbols;
174 enum demangling_styles style = auto_demangling;
176 program_name = argv[0];
177 xmalloc_set_program_name (program_name);
179 expandargv (&argc, &argv);
181 while ((c = getopt_long (argc, argv, "_hinps:tv", long_options, (int *) 0)) != EOF)
183 switch (c)
185 case '?':
186 usage (stderr, 1);
187 break;
188 case 'h':
189 usage (stdout, 0);
190 case 'n':
191 strip_underscore = 0;
192 break;
193 case 'p':
194 flags &= ~ DMGL_PARAMS;
195 break;
196 case 't':
197 flags |= DMGL_TYPES;
198 break;
199 case 'i':
200 flags &= ~ DMGL_VERBOSE;
201 break;
202 case 'v':
203 print_version ("c++filt");
204 return 0;
205 case '_':
206 strip_underscore = 1;
207 break;
208 case 's':
209 style = cplus_demangle_name_to_style (optarg);
210 if (style == unknown_demangling)
212 fprintf (stderr, "%s: unknown demangling style `%s'\n",
213 program_name, optarg);
214 return 1;
216 cplus_demangle_set_style (style);
217 break;
221 if (optind < argc)
223 for ( ; optind < argc; optind++)
225 demangle_it (argv[optind]);
226 putchar ('\n');
229 return 0;
232 switch (current_demangling_style)
234 case gnu_demangling:
235 case lucid_demangling:
236 case arm_demangling:
237 case java_demangling:
238 case edg_demangling:
239 case gnat_demangling:
240 case gnu_v3_demangling:
241 case auto_demangling:
242 valid_symbols = standard_symbol_characters ();
243 break;
244 case hp_demangling:
245 valid_symbols = hp_symbol_characters ();
246 break;
247 default:
248 /* Folks should explicitly indicate the appropriate alphabet for
249 each demangling. Providing a default would allow the
250 question to go unconsidered. */
251 fatal ("Internal error: no symbol alphabet for current style");
254 for (;;)
256 static char mbuffer[32767];
257 unsigned i = 0;
259 c = getchar ();
260 /* Try to read a mangled name. */
261 while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
263 if (i >= sizeof (mbuffer) - 1)
264 break;
265 mbuffer[i++] = c;
266 c = getchar ();
269 if (i > 0)
271 mbuffer[i] = 0;
272 demangle_it (mbuffer);
275 if (c == EOF)
276 break;
278 /* Echo the whitespace characters so that the output looks
279 like the input, only with the mangled names demangled. */
280 putchar (c);
281 if (c == '\n')
282 fflush (stdout);
285 fflush (stdout);
286 return 0;