1 /* Demangler for GNU C++ - main program
2 Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003 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
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
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, 59 Temple Place - Suite 330, Boston, MA
28 #include "libiberty.h"
31 #include "safe-ctype.h"
33 static int flags
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_VERBOSE
;
35 static void demangle_it (char *);
36 static void usage (FILE *, int) ATTRIBUTE_NORETURN
;
37 static void print_demangler_list (FILE *);
40 demangle_it (char *mangled_name
)
44 /* For command line args, also try to demangle type encodings. */
45 result
= cplus_demangle (mangled_name
, flags
| DMGL_TYPES
);
48 printf ("%s\n", mangled_name
);
52 printf ("%s\n", result
);
58 print_demangler_list (FILE *stream
)
60 const struct demangler_engine
*demangler
;
62 fprintf (stream
, "{%s", libiberty_demanglers
->demangling_style_name
);
64 for (demangler
= libiberty_demanglers
+ 1;
65 demangler
->demangling_style
!= unknown_demangling
;
67 fprintf (stream
, ",%s", demangler
->demangling_style_name
);
69 fprintf (stream
, "}");
73 usage (FILE *stream
, int status
)
76 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores]\n\
77 [-p] [--no-params]\n",
82 print_demangler_list (stream
);
83 fprintf (stream
, "]\n");
87 print_demangler_list (stream
);
88 fprintf (stream
, "]\n");
91 [--help] [--version] [arg...]\n");
95 static char mbuffer
[32767];
97 int strip_underscore
= 0;
99 static const struct option long_options
[] = {
100 {"strip-underscores", no_argument
, 0, '_'},
101 {"format", required_argument
, 0, 's'},
102 {"help", no_argument
, 0, 'h'},
103 {"no-params", no_argument
, 0, 'p'},
104 {"no-strip-underscores", no_argument
, 0, 'n'},
105 {"version", no_argument
, 0, 'v'},
106 {0, no_argument
, 0, 0}
109 static const char *standard_symbol_characters (void);
111 static const char *hp_symbol_characters (void);
113 /* Return the string of non-alnum characters that may occur
114 as a valid symbol component, in the standard assembler symbol
118 standard_symbol_characters (void)
124 /* Return the string of non-alnum characters that may occur
125 as a valid symbol name component in an HP object file.
127 Note that, since HP's compiler generates object code straight from
128 C++ source, without going through an assembler, its mangled
129 identifiers can use all sorts of characters that no assembler would
130 tolerate, so the alphabet this function creates is a little odd.
131 Here are some sample mangled identifiers offered by HP:
133 typeid*__XT24AddressIndExpClassMember_
134 [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
135 __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
137 This still seems really weird to me, since nowhere else in this
138 file is there anything to recognize curly brackets, parens, etc.
139 I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
140 this is right, but I still strongly suspect that there's a
141 misunderstanding here.
143 If we decide it's better for c++filt to use HP's assembler syntax
144 to scrape identifiers out of its input, here's the definition of
145 the symbol name syntax from the HP assembler manual:
147 Symbols are composed of uppercase and lowercase letters, decimal
148 digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
149 underscore (_). A symbol can begin with a letter, digit underscore or
150 dollar sign. If a symbol begins with a digit, it must contain a
155 hp_symbol_characters (void)
157 return "_$.<>#,*&[]:(){}";
160 extern int main (int, char **);
163 main (int argc
, char **argv
)
167 const char *valid_symbols
;
168 enum demangling_styles style
= auto_demangling
;
170 program_name
= argv
[0];
171 xmalloc_set_program_name (program_name
);
173 strip_underscore
= TARGET_PREPENDS_UNDERSCORE
;
175 while ((c
= getopt_long (argc
, argv
, "_nps:", long_options
, (int *) 0)) != EOF
)
185 strip_underscore
= 0;
188 flags
&= ~ DMGL_PARAMS
;
191 print_version ("c++filt");
194 strip_underscore
= 1;
198 style
= cplus_demangle_name_to_style (optarg
);
199 if (style
== unknown_demangling
)
201 fprintf (stderr
, "%s: unknown demangling style `%s'\n",
202 program_name
, optarg
);
206 cplus_demangle_set_style (style
);
214 for ( ; optind
< argc
; optind
++)
216 demangle_it (argv
[optind
]);
221 switch (current_demangling_style
)
224 case lucid_demangling
:
226 case java_demangling
:
228 case gnat_demangling
:
229 case gnu_v3_demangling
:
230 case auto_demangling
:
231 valid_symbols
= standard_symbol_characters ();
234 valid_symbols
= hp_symbol_characters ();
237 /* Folks should explicitly indicate the appropriate alphabet for
238 each demangling. Providing a default would allow the
239 question to go unconsidered. */
240 fatal ("Internal error: no symbol alphabet for current style");
247 /* Try to read a label. */
248 while (c
!= EOF
&& (ISALNUM (c
) || strchr (valid_symbols
, c
)))
250 if (i
>= sizeof (mbuffer
) - 1)
257 unsigned skip_first
= 0;
260 if (mbuffer
[0] == '.' || mbuffer
[0] == '$')
262 if (strip_underscore
&& mbuffer
[skip_first
] == '_')
268 flags
|= (int) style
;
269 result
= cplus_demangle (mbuffer
+ skip_first
, flags
);
272 if (mbuffer
[0] == '.')
274 fputs (result
, stdout
);
278 fputs (mbuffer
, stdout
);