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