~ubuntu-branches/ubuntu/hoary/binutils/hoary

« back to all changes in this revision

Viewing changes to binutils/cxxfilt.c

  • Committer: Bazaar Package Importer
  • Author(s): James Troup
  • Date: 2004-05-19 10:35:44 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040519103544-17h3o6e8pwndydrg
Tags: 2.14.90.0.7-8
debian/rules: don't use gcc-2.95 on m68k.  Thanks to Adam Conrad for
pointing this out.

Show diffs side-by-side

added added

removed removed

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