OSDN Git Service

* g++.dg/abi/vague1.C: Use xfail, rather than embedded Tcl code.
[pf3gnuchains/gcc-fork.git] / gcc / cp / cxxfilt.c
1 /* Demangler for GNU C++ - main program
2    Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002 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 "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "demangle.h"
30 #include "getopt.h"
31 #include "version.h"
32
33 static const char *program_name;
34 static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
35
36 static void demangle_it (char *);
37 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
38 static void fatal (const char *) ATTRIBUTE_NORETURN;
39 static void print_demangler_list (FILE *);
40
41 static void
42 demangle_it (char *mangled_name)
43 {
44   char *result;
45
46   /* For command line args, also try to demangle type encodings.  */
47   result = cplus_demangle (mangled_name, flags | DMGL_TYPES);
48   if (result == NULL)
49     {
50       printf ("%s\n", mangled_name);
51     }
52   else
53     {
54       printf ("%s\n", result);
55       free (result);
56     }
57 }
58
59 static void 
60 print_demangler_list (FILE *stream)
61 {
62   const struct demangler_engine *demangler; 
63
64   fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
65   
66   for (demangler = libiberty_demanglers + 1;
67        demangler->demangling_style != unknown_demangling;
68        ++demangler)
69     fprintf (stream, ",%s", demangler->demangling_style_name);
70
71   fprintf (stream, "}");
72 }
73
74 static void
75 usage (FILE *stream, int status)
76 {
77   fprintf (stream, "\
78 Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
79            program_name);
80
81   fprintf (stream, "\
82        [-s ");
83   print_demangler_list (stream);
84   fprintf (stream, "]\n");
85
86   fprintf (stream, "\
87        [--format ");
88   print_demangler_list (stream);
89   fprintf (stream, "]\n");
90
91   fprintf (stream, "\
92        [--help] [--version] [arg...]\n");
93   exit (status);
94 }
95
96 #define MBUF_SIZE 32767
97 char mbuffer[MBUF_SIZE];
98
99 bool strip_underscore = false;
100
101 static const struct option long_options[] = {
102   {"strip-underscores", no_argument, 0, '_'},
103   {"format", required_argument, 0, 's'},
104   {"help", no_argument, 0, 'h'},
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
173   strip_underscore = (USER_LABEL_PREFIX[0] == '_');
174
175   while ((c = getopt_long (argc, argv, "_ns:", long_options, (int *) 0)) != EOF)
176     {
177       switch (c)
178         {
179         case '?':
180           usage (stderr, 1);
181           break;
182         case 'h':
183           usage (stdout, 0);
184         case 'n':
185           strip_underscore = false;
186           break;
187         case 'v':
188           printf ("GNU %s (C++ demangler), version %s\n",
189                   program_name, version_string);
190           return (0);
191         case '_':
192           strip_underscore = true;
193           break;
194         case 's':
195           {
196             style = cplus_demangle_name_to_style (optarg);
197             if (style == unknown_demangling)
198               {
199                 fprintf (stderr, "%s: unknown demangling style `%s'\n",
200                          program_name, optarg);
201                 return (1);
202               }
203             else
204               cplus_demangle_set_style (style);
205           }
206           break;
207         }
208     }
209
210   if (optind < argc)
211     {
212       for ( ; optind < argc; optind++)
213         {
214           demangle_it (argv[optind]);
215         }
216     }
217   else
218     {
219       switch (current_demangling_style)
220         {
221         case gnu_demangling:
222         case lucid_demangling:
223         case arm_demangling:
224         case java_demangling:
225         case edg_demangling:
226         case gnat_demangling:
227         case gnu_v3_demangling:
228         case auto_demangling:
229           valid_symbols = standard_symbol_characters ();
230           break;
231         case hp_demangling:
232           valid_symbols = hp_symbol_characters ();
233           break;
234         default:
235           /* Folks should explicitly indicate the appropriate alphabet for
236              each demangling.  Providing a default would allow the
237              question to go unconsidered.  */
238           fatal ("Internal error: no symbol alphabet for current style");
239         }
240
241       for (;;)
242         {
243           int i = 0;
244           c = getchar ();
245           /* Try to read a label.  */
246           while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
247             {
248               if (i >= MBUF_SIZE-1)
249                 break;
250               mbuffer[i++] = c;
251               c = getchar ();
252             }
253           if (i > 0)
254             {
255               int skip_first = 0;
256
257               mbuffer[i] = 0;
258               if (mbuffer[0] == '.' || mbuffer[0] == '$')
259                 ++skip_first;
260               if (strip_underscore && mbuffer[skip_first] == '_')
261                 ++skip_first;
262
263               if (skip_first > i)
264                 skip_first = i;
265
266               flags |= (int) style;
267               result = cplus_demangle (mbuffer + skip_first, flags);
268               if (result)
269                 {
270                   if (mbuffer[0] == '.')
271                     putc ('.', stdout);
272                   fputs (result, stdout);
273                   free (result);
274                 }
275               else
276                 fputs (mbuffer, stdout);
277
278               fflush (stdout);
279             }
280           if (c == EOF)
281             break;
282           putchar (c);
283           fflush (stdout);
284         }
285     }
286
287   return (0);
288 }
289
290 static void
291 fatal (const char *str)
292 {
293   fprintf (stderr, "%s: %s\n", program_name, str);
294   exit (1);
295 }