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