1 /* Demangler for GNU C++ - main program
2 Copyright (C) 1989-2019 Free Software Foundation, Inc.
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
7 This file is part of GNU Binutils.
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.
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.
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
26 #include "libiberty.h"
29 #include "safe-ctype.h"
32 static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
33 static int strip_underscore = TARGET_PREPENDS_UNDERSCORE;
35 static const struct option long_options[] =
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'},
42 {"no-verbose", no_argument, NULL, 'i'},
43 {"types", no_argument, NULL, 't'},
44 {"version", no_argument, NULL, 'v'},
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'},
49 {NULL, no_argument, NULL, 0}
53 demangle_it (char *mangled_name)
56 unsigned int skip_first = 0;
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. */
61 if (mangled_name[0] == '.' || mangled_name[0] == '$')
63 if (strip_underscore && mangled_name[skip_first] == '_')
66 result = cplus_demangle (mangled_name + skip_first, flags);
69 printf ("%s", mangled_name);
72 if (mangled_name[0] == '.')
74 printf ("%s", result);
80 print_demangler_list (FILE *stream)
82 const struct demangler_engine *demangler;
84 fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
86 for (demangler = libiberty_demanglers + 1;
87 demangler->demangling_style != unknown_demangling;
89 fprintf (stream, ",%s", demangler->demangling_style_name);
91 fprintf (stream, "}");
94 ATTRIBUTE_NORETURN static void
95 usage (FILE *stream, int status)
98 Usage: %s [options] [mangled names]\n", program_name);
101 [-_|--strip-underscore] Ignore first leading underscore%s\n",
102 TARGET_PREPENDS_UNDERSCORE ? " (default)" : "");
104 [-n|--no-strip-underscore] Do not ignore a leading underscore%s\n",
105 TARGET_PREPENDS_UNDERSCORE ? "" : " (default)");
107 [-p|--no-params] Do not display function arguments\n\
108 [-i|--no-verbose] Do not show implementation details (if any)\n\
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\
111 [-t|--types] Also attempt to demangle type encodings\n\
113 print_demangler_list (stream);
114 fprintf (stream, "]\n");
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");
123 if (REPORT_BUGS_TO[0] && status == 0)
124 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
128 /* Return the string of non-alnum characters that may occur
129 as a valid symbol component, in the standard assembler symbol
133 standard_symbol_characters (void)
138 extern int main (int, char **);
141 main (int argc, char **argv)
144 const char *valid_symbols;
145 enum demangling_styles style = auto_demangling;
147 program_name = argv[0];
148 xmalloc_set_program_name (program_name);
149 bfd_set_error_program_name (program_name);
151 expandargv (&argc, &argv);
153 while ((c = getopt_long (argc, argv, "_hinprRs:tv", long_options, (int *) 0)) != EOF)
163 strip_underscore = 0;
166 flags &= ~ DMGL_PARAMS;
169 flags |= DMGL_NO_RECURSE_LIMIT;
172 flags &= ~ DMGL_NO_RECURSE_LIMIT;
178 flags &= ~ DMGL_VERBOSE;
181 print_version ("c++filt");
184 strip_underscore = 1;
187 style = cplus_demangle_name_to_style (optarg);
188 if (style == unknown_demangling)
190 fprintf (stderr, "%s: unknown demangling style `%s'\n",
191 program_name, optarg);
194 cplus_demangle_set_style (style);
201 for ( ; optind < argc; optind++)
203 demangle_it (argv[optind]);
210 switch (current_demangling_style)
212 case auto_demangling:
213 case gnu_v3_demangling:
214 case java_demangling:
215 case gnat_demangling:
216 case dlang_demangling:
217 case rust_demangling:
218 valid_symbols = standard_symbol_characters ();
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");
229 static char mbuffer[32767];
233 /* Try to read a mangled name. */
234 while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
236 if (i >= sizeof (mbuffer) - 1)
245 demangle_it (mbuffer);
251 /* Echo the whitespace characters so that the output looks
252 like the input, only with the mangled names demangled. */