Merge branch 'vendor/XZ'
[dragonfly.git] / contrib / binutils-2.25 / binutils / cxxfilt.c
1 /* Demangler for GNU C++ - main program
2    Copyright (C) 1989-2014 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
6
7    This file is part of GNU Binutils.
8
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.
13
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.
18
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
22    02110-1301, USA.  */
23
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "libiberty.h"
27 #include "demangle.h"
28 #include "getopt.h"
29 #include "safe-ctype.h"
30 #include "bucomm.h"
31
32 static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
33 static int strip_underscore = TARGET_PREPENDS_UNDERSCORE;
34
35 static const struct option long_options[] =
36 {
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   {NULL, no_argument, NULL, 0}
46 };
47
48 static void
49 demangle_it (char *mangled_name)
50 {
51   char *result;
52   unsigned int skip_first = 0;
53
54   /* _ and $ are sometimes found at the start of function names
55      in assembler sources in order to distinguish them from other
56      names (eg register names).  So skip them here.  */
57   if (mangled_name[0] == '.' || mangled_name[0] == '$')
58     ++skip_first;
59   if (strip_underscore && mangled_name[skip_first] == '_')
60     ++skip_first;
61
62   result = cplus_demangle (mangled_name + skip_first, flags);
63
64   if (result == NULL)
65     printf ("%s", mangled_name);
66   else
67     {
68       if (mangled_name[0] == '.')
69         putchar ('.');
70       printf ("%s", result);
71       free (result);
72     }
73 }
74
75 static void
76 print_demangler_list (FILE *stream)
77 {
78   const struct demangler_engine *demangler;
79
80   fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
81
82   for (demangler = libiberty_demanglers + 1;
83        demangler->demangling_style != unknown_demangling;
84        ++demangler)
85     fprintf (stream, ",%s", demangler->demangling_style_name);
86
87   fprintf (stream, "}");
88 }
89
90 static void
91 usage (FILE *stream, int status)
92 {
93   fprintf (stream, "\
94 Usage: %s [options] [mangled names]\n", program_name);
95   fprintf (stream, "\
96 Options are:\n\
97   [-_|--strip-underscore]     Ignore first leading underscore%s\n",
98            TARGET_PREPENDS_UNDERSCORE ? " (default)" : "");
99   fprintf (stream, "\
100   [-n|--no-strip-underscore]  Do not ignore a leading underscore%s\n",
101            TARGET_PREPENDS_UNDERSCORE ? "" : " (default)");
102   fprintf (stream, "\
103   [-p|--no-params]            Do not display function arguments\n\
104   [-i|--no-verbose]           Do not show implementation details (if any)\n\
105   [-t|--types]                Also attempt to demangle type encodings\n\
106   [-s|--format ");
107   print_demangler_list (stream);
108   fprintf (stream, "]\n");
109
110   fprintf (stream, "\
111   [@<file>]                   Read extra options from <file>\n\
112   [-h|--help]                 Display this information\n\
113   [-v|--version]              Show the version information\n\
114 Demangled names are displayed to stdout.\n\
115 If a name cannot be demangled it is just echoed to stdout.\n\
116 If no names are provided on the command line, stdin is read.\n");
117   if (REPORT_BUGS_TO[0] && status == 0)
118     fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
119   exit (status);
120 }
121
122 /* Return the string of non-alnum characters that may occur
123    as a valid symbol component, in the standard assembler symbol
124    syntax.  */
125
126 static const char *
127 standard_symbol_characters (void)
128 {
129   return "_$.";
130 }
131
132 /* Return the string of non-alnum characters that may occur
133    as a valid symbol name component in an HP object file.
134
135    Note that, since HP's compiler generates object code straight from
136    C++ source, without going through an assembler, its mangled
137    identifiers can use all sorts of characters that no assembler would
138    tolerate, so the alphabet this function creates is a little odd.
139    Here are some sample mangled identifiers offered by HP:
140
141         typeid*__XT24AddressIndExpClassMember_
142         [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
143         __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
144
145    This still seems really weird to me, since nowhere else in this
146    file is there anything to recognize curly brackets, parens, etc.
147    I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
148    this is right, but I still strongly suspect that there's a
149    misunderstanding here.
150
151    If we decide it's better for c++filt to use HP's assembler syntax
152    to scrape identifiers out of its input, here's the definition of
153    the symbol name syntax from the HP assembler manual:
154
155        Symbols are composed of uppercase and lowercase letters, decimal
156        digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
157        underscore (_). A symbol can begin with a letter, digit underscore or
158        dollar sign. If a symbol begins with a digit, it must contain a
159        non-digit character.
160
161    So have fun.  */
162 static const char *
163 hp_symbol_characters (void)
164 {
165   return "_$.<>#,*&[]:(){}";
166 }
167
168 extern int main (int, char **);
169
170 int
171 main (int argc, char **argv)
172 {
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   expandargv (&argc, &argv);
181
182   while ((c = getopt_long (argc, argv, "_hinps:tv", 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 'p':
195           flags &= ~ DMGL_PARAMS;
196           break;
197         case 't':
198           flags |= DMGL_TYPES;
199           break;
200         case 'i':
201           flags &= ~ DMGL_VERBOSE;
202           break;
203         case 'v':
204           print_version ("c++filt");
205           return 0;
206         case '_':
207           strip_underscore = 1;
208           break;
209         case 's':
210           style = cplus_demangle_name_to_style (optarg);
211           if (style == unknown_demangling)
212             {
213               fprintf (stderr, "%s: unknown demangling style `%s'\n",
214                        program_name, optarg);
215               return 1;
216             }
217           cplus_demangle_set_style (style);
218           break;
219         }
220     }
221
222   if (optind < argc)
223     {
224       for ( ; optind < argc; optind++)
225         {
226           demangle_it (argv[optind]);
227           putchar ('\n');
228         }
229
230       return 0;
231     }
232
233   switch (current_demangling_style)
234     {
235     case gnu_demangling:
236     case lucid_demangling:
237     case arm_demangling:
238     case java_demangling:
239     case edg_demangling:
240     case gnat_demangling:
241     case gnu_v3_demangling:
242     case auto_demangling:
243       valid_symbols = standard_symbol_characters ();
244       break;
245     case hp_demangling:
246       valid_symbols = hp_symbol_characters ();
247       break;
248     default:
249       /* Folks should explicitly indicate the appropriate alphabet for
250          each demangling.  Providing a default would allow the
251          question to go unconsidered.  */
252       fatal ("Internal error: no symbol alphabet for current style");
253     }
254
255   for (;;)
256     {
257       static char mbuffer[32767];
258       unsigned i = 0;
259
260       c = getchar ();
261       /* Try to read a mangled name.  */
262       while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
263         {
264           if (i >= sizeof (mbuffer) - 1)
265             break;
266           mbuffer[i++] = c;
267           c = getchar ();
268         }
269
270       if (i > 0)
271         {
272           mbuffer[i] = 0;
273           demangle_it (mbuffer);
274         }
275
276       if (c == EOF)
277         break;
278
279       /* Echo the whitespace characters so that the output looks
280          like the input, only with the mangled names demangled.  */
281       putchar (c);
282       if (c == '\n')
283         fflush (stdout);
284     }
285
286   fflush (stdout);
287   return 0;
288 }