gdb/emacs/dir-locals: Update settings for c++-mode
[external/binutils.git] / binutils / cxxfilt.c
1 /* Demangler for GNU C++ - main program
2    Copyright (C) 1989-2018 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   {"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}
50 };
51
52 static void
53 demangle_it (char *mangled_name)
54 {
55   char *result;
56   unsigned int skip_first = 0;
57
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] == '$')
62     ++skip_first;
63   if (strip_underscore && mangled_name[skip_first] == '_')
64     ++skip_first;
65
66   result = cplus_demangle (mangled_name + skip_first, flags);
67
68   if (result == NULL)
69     printf ("%s", mangled_name);
70   else
71     {
72       if (mangled_name[0] == '.')
73         putchar ('.');
74       printf ("%s", result);
75       free (result);
76     }
77 }
78
79 static void
80 print_demangler_list (FILE *stream)
81 {
82   const struct demangler_engine *demangler;
83
84   fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
85
86   for (demangler = libiberty_demanglers + 1;
87        demangler->demangling_style != unknown_demangling;
88        ++demangler)
89     fprintf (stream, ",%s", demangler->demangling_style_name);
90
91   fprintf (stream, "}");
92 }
93
94 ATTRIBUTE_NORETURN static void
95 usage (FILE *stream, int status)
96 {
97   fprintf (stream, "\
98 Usage: %s [options] [mangled names]\n", program_name);
99   fprintf (stream, "\
100 Options are:\n\
101   [-_|--strip-underscore]     Ignore first leading underscore%s\n",
102            TARGET_PREPENDS_UNDERSCORE ? " (default)" : "");
103   fprintf (stream, "\
104   [-n|--no-strip-underscore]  Do not ignore a leading underscore%s\n",
105            TARGET_PREPENDS_UNDERSCORE ? "" : " (default)");
106   fprintf (stream, "\
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\
112   [-s|--format ");
113   print_demangler_list (stream);
114   fprintf (stream, "]\n");
115
116   fprintf (stream, "\
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);
125   exit (status);
126 }
127
128 /* Return the string of non-alnum characters that may occur
129    as a valid symbol component, in the standard assembler symbol
130    syntax.  */
131
132 static const char *
133 standard_symbol_characters (void)
134 {
135   return "_$.";
136 }
137
138 /* Return the string of non-alnum characters that may occur
139    as a valid symbol name component in an HP object file.
140
141    Note that, since HP's compiler generates object code straight from
142    C++ source, without going through an assembler, its mangled
143    identifiers can use all sorts of characters that no assembler would
144    tolerate, so the alphabet this function creates is a little odd.
145    Here are some sample mangled identifiers offered by HP:
146
147         typeid*__XT24AddressIndExpClassMember_
148         [Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
149         __ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
150
151    This still seems really weird to me, since nowhere else in this
152    file is there anything to recognize curly brackets, parens, etc.
153    I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
154    this is right, but I still strongly suspect that there's a
155    misunderstanding here.
156
157    If we decide it's better for c++filt to use HP's assembler syntax
158    to scrape identifiers out of its input, here's the definition of
159    the symbol name syntax from the HP assembler manual:
160
161        Symbols are composed of uppercase and lowercase letters, decimal
162        digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
163        underscore (_). A symbol can begin with a letter, digit underscore or
164        dollar sign. If a symbol begins with a digit, it must contain a
165        non-digit character.
166
167    So have fun.  */
168 static const char *
169 hp_symbol_characters (void)
170 {
171   return "_$.<>#,*&[]:(){}";
172 }
173
174 extern int main (int, char **);
175
176 int
177 main (int argc, char **argv)
178 {
179   int c;
180   const char *valid_symbols;
181   enum demangling_styles style = auto_demangling;
182
183   program_name = argv[0];
184   xmalloc_set_program_name (program_name);
185   bfd_set_error_program_name (program_name);
186
187   expandargv (&argc, &argv);
188
189   while ((c = getopt_long (argc, argv, "_hinprRs:tv", long_options, (int *) 0)) != EOF)
190     {
191       switch (c)
192         {
193         case '?':
194           usage (stderr, 1);
195           break;
196         case 'h':
197           usage (stdout, 0);
198         case 'n':
199           strip_underscore = 0;
200           break;
201         case 'p':
202           flags &= ~ DMGL_PARAMS;
203           break;
204         case 'r':
205           flags |= DMGL_NO_RECURSE_LIMIT;
206           break;
207         case 'R':
208           flags &= ~ DMGL_NO_RECURSE_LIMIT;
209           break;
210         case 't':
211           flags |= DMGL_TYPES;
212           break;
213         case 'i':
214           flags &= ~ DMGL_VERBOSE;
215           break;
216         case 'v':
217           print_version ("c++filt");
218           return 0;
219         case '_':
220           strip_underscore = 1;
221           break;
222         case 's':
223           style = cplus_demangle_name_to_style (optarg);
224           if (style == unknown_demangling)
225             {
226               fprintf (stderr, "%s: unknown demangling style `%s'\n",
227                        program_name, optarg);
228               return 1;
229             }
230           cplus_demangle_set_style (style);
231           break;
232         }
233     }
234
235   if (optind < argc)
236     {
237       for ( ; optind < argc; optind++)
238         {
239           demangle_it (argv[optind]);
240           putchar ('\n');
241         }
242
243       return 0;
244     }
245
246   switch (current_demangling_style)
247     {
248     case gnu_demangling:
249     case lucid_demangling:
250     case arm_demangling:
251     case java_demangling:
252     case edg_demangling:
253     case gnat_demangling:
254     case gnu_v3_demangling:
255     case dlang_demangling:
256     case rust_demangling:
257     case auto_demangling:
258       valid_symbols = standard_symbol_characters ();
259       break;
260     case hp_demangling:
261       valid_symbols = hp_symbol_characters ();
262       break;
263     default:
264       /* Folks should explicitly indicate the appropriate alphabet for
265          each demangling.  Providing a default would allow the
266          question to go unconsidered.  */
267       fatal ("Internal error: no symbol alphabet for current style");
268     }
269
270   for (;;)
271     {
272       static char mbuffer[32767];
273       unsigned i = 0;
274
275       c = getchar ();
276       /* Try to read a mangled name.  */
277       while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
278         {
279           if (i >= sizeof (mbuffer) - 1)
280             break;
281           mbuffer[i++] = c;
282           c = getchar ();
283         }
284
285       if (i > 0)
286         {
287           mbuffer[i] = 0;
288           demangle_it (mbuffer);
289         }
290
291       if (c == EOF)
292         break;
293
294       /* Echo the whitespace characters so that the output looks
295          like the input, only with the mangled names demangled.  */
296       putchar (c);
297       if (c == '\n')
298         fflush (stdout);
299     }
300
301   fflush (stdout);
302   return 0;
303 }