This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gdb / demangle.c
1 /* Basic C++ demangling support for GDB.
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Written by Fred Fish at Cygnus Support.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23
24 /*  This file contains support code for C++ demangling that is common
25    to a styles of demangling, and GDB specific. */
26
27 #include "defs.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "demangle.h"
31 #include "gdb_string.h"
32
33 /* Select the default C++ demangling style to use.  The default is "auto",
34    which allows gdb to attempt to pick an appropriate demangling style for
35    the executable it has loaded.  It can be set to a specific style ("gnu",
36    "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
37    selection of the style unless you do an explicit "set demangle auto".
38    To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
39    the appropriate target configuration file. */
40
41 #ifndef DEFAULT_DEMANGLING_STYLE
42 #define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
43 #endif
44
45 extern void _initialize_demangler (void);
46
47 /* String name for the current demangling style.  Set by the
48    "set demangle-style" command, printed as part of the output by the
49    "show demangle-style" command. */
50
51 static char *current_demangling_style_string;
52
53 /* The array of names of the known demanglyng styles.  Generated by
54    _initialize_demangler from libiberty_demanglers[] array.  */
55
56 static const char **demangling_style_names;
57
58 static void set_demangling_command (char *, int, struct cmd_list_element *);
59
60 /* Set current demangling style.  Called by the "set demangle-style"
61    command after it has updated the current_demangling_style_string to
62    match what the user has entered.
63
64    If the user has entered a string that matches a known demangling style
65    name in the demanglers[] array then just leave the string alone and update
66    the current_demangling_style enum value to match.
67
68    If the user has entered a string that doesn't match, including an empty
69    string, then print a list of the currently known styles and restore
70    the current_demangling_style_string to match the current_demangling_style
71    enum value.
72
73    Note:  Assumes that current_demangling_style_string always points to
74    a malloc'd string, even if it is a null-string. */
75
76 static void
77 set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
78 {
79   const struct demangler_engine *dem;
80
81   /*  First just try to match whatever style name the user supplied with
82      one of the known ones.  Don't bother special casing for an empty
83      name, we just treat it as any other style name that doesn't match.
84      If we match, update the current demangling style enum. */
85
86   for (dem = libiberty_demanglers; 
87        dem->demangling_style != unknown_demangling; 
88        dem++)
89     {
90       if (STREQ (current_demangling_style_string,
91                  dem->demangling_style_name))
92         {
93           current_demangling_style = dem->demangling_style;
94           break;
95         }
96     }
97
98   /* Check to see if we found a match.  If not, gripe about any non-empty
99      style name and supply a list of valid ones.  FIXME:  This should
100      probably be done with some sort of completion and with help. */
101
102   if (dem->demangling_style == unknown_demangling)
103     {
104       if (*current_demangling_style_string != '\0')
105         {
106           printf_unfiltered ("Unknown demangling style `%s'.\n",
107                              current_demangling_style_string);
108         }
109       printf_unfiltered ("The currently understood settings are:\n\n");
110       for (dem = libiberty_demanglers; 
111            dem->demangling_style != unknown_demangling; 
112            dem++)
113         {
114           printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
115                              dem->demangling_style_doc);
116           if (dem->demangling_style == current_demangling_style)
117             {
118               xfree (current_demangling_style_string);
119               current_demangling_style_string =
120                 savestring (dem->demangling_style_name,
121                             strlen (dem->demangling_style_name));
122             }
123         }
124       if (current_demangling_style == unknown_demangling)
125         {
126           /* This can happen during initialization if gdb is compiled with
127              a DEMANGLING_STYLE value that is unknown, so pick the first
128              one as the default. */
129           current_demangling_style = libiberty_demanglers[0].demangling_style;
130           current_demangling_style_string =
131             savestring (
132               libiberty_demanglers[0].demangling_style_name,
133               strlen (libiberty_demanglers[0].demangling_style_name));
134           warning ("`%s' style demangling chosen as the default.\n",
135                    current_demangling_style_string);
136         }
137     }
138 }
139
140 /* Fake a "set demangle-style" command. */
141
142 void
143 set_demangling_style (char *style)
144 {
145   if (current_demangling_style_string != NULL)
146     {
147       xfree (current_demangling_style_string);
148     }
149   current_demangling_style_string = savestring (style, strlen (style));
150   set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
151 }
152
153 /* In order to allow a single demangler executable to demangle strings
154    using various common values of CPLUS_MARKER, as well as any specific
155    one set at compile time, we maintain a string containing all the
156    commonly used ones, and check to see if the marker we are looking for
157    is in that string.  CPLUS_MARKER is usually '$' on systems where the
158    assembler can deal with that.  Where the assembler can't, it's usually
159    '.' (but on many systems '.' is used for other things).  We put the
160    current defined CPLUS_MARKER first (which defaults to '$'), followed
161    by the next most common value, followed by an explicit '$' in case
162    the value of CPLUS_MARKER is not '$'.
163
164    We could avoid this if we could just get g++ to tell us what the actual
165    cplus marker character is as part of the debug information, perhaps by
166    ensuring that it is the character that terminates the gcc<n>_compiled
167    marker symbol (FIXME). */
168
169 static char cplus_markers[] =
170 {CPLUS_MARKER, '.', '$', '\0'};
171
172 int
173 is_cplus_marker (int c)
174 {
175   return c && strchr (cplus_markers, c) != NULL;
176 }
177
178 void
179 _initialize_demangler (void)
180 {
181   struct cmd_list_element *set, *show;
182   int i, ndems;
183
184   /* Fill the demangling_style_names[] array.  */
185   for (ndems = 0;
186        libiberty_demanglers[ndems].demangling_style != unknown_demangling; 
187        ndems++)
188     ;
189   demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
190   for (i = 0;
191        libiberty_demanglers[i].demangling_style != unknown_demangling; 
192        i++)
193     demangling_style_names[i] =
194       xstrdup (libiberty_demanglers[i].demangling_style_name);
195
196   set = add_set_enum_cmd ("demangle-style", class_support,
197                           demangling_style_names,
198                           (const char **) &current_demangling_style_string,
199                           "Set the current C++ demangling style.\n\
200 Use `set demangle-style' without arguments for a list of demangling styles.",
201                           &setlist);
202   show = add_show_from_set (set, &showlist);
203   set_cmd_sfunc (set, set_demangling_command);
204
205   /* Set the default demangling style chosen at compilation time. */
206   set_demangling_style (DEFAULT_DEMANGLING_STYLE);
207   set_cplus_marker_for_demangling (CPLUS_MARKER);
208 }