*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / demangle.c
CommitLineData
c906108c 1/* Basic C++ demangling support for GDB.
1bac305b
AC
2
3 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 2001, 2003 Free Software Foundation, Inc.
5
c906108c
SS
6 Written by Fred Fish at Cygnus Support.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
c5aa993b
JM
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
c906108c
SS
24
25
26/* This file contains support code for C++ demangling that is common
27 to a styles of demangling, and GDB specific. */
28
29#include "defs.h"
30#include "command.h"
31#include "gdbcmd.h"
32#include "demangle.h"
33#include "gdb_string.h"
34
35/* Select the default C++ demangling style to use. The default is "auto",
36 which allows gdb to attempt to pick an appropriate demangling style for
37 the executable it has loaded. It can be set to a specific style ("gnu",
38 "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
39 selection of the style unless you do an explicit "set demangle auto".
40 To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
41 the appropriate target configuration file. */
42
43#ifndef DEFAULT_DEMANGLING_STYLE
44#define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
45#endif
46
a14ed312 47extern void _initialize_demangler (void);
392a587b 48
c906108c
SS
49/* String name for the current demangling style. Set by the
50 "set demangle-style" command, printed as part of the output by the
51 "show demangle-style" command. */
52
53static char *current_demangling_style_string;
54
fa58ee11
EZ
55/* The array of names of the known demanglyng styles. Generated by
56 _initialize_demangler from libiberty_demanglers[] array. */
57
58static const char **demangling_style_names;
920d2a44
AC
59static void
60show_demangling_style_names(struct ui_file *file, int from_tty,
61 struct cmd_list_element *c, const char *value)
62{
63 fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
64 value);
65}
66
fa58ee11 67
a14ed312 68static void set_demangling_command (char *, int, struct cmd_list_element *);
c906108c
SS
69
70/* Set current demangling style. Called by the "set demangle-style"
71 command after it has updated the current_demangling_style_string to
72 match what the user has entered.
73
74 If the user has entered a string that matches a known demangling style
75 name in the demanglers[] array then just leave the string alone and update
76 the current_demangling_style enum value to match.
77
78 If the user has entered a string that doesn't match, including an empty
79 string, then print a list of the currently known styles and restore
80 the current_demangling_style_string to match the current_demangling_style
81 enum value.
82
83 Note: Assumes that current_demangling_style_string always points to
84 a malloc'd string, even if it is a null-string. */
85
86static void
fba45db2 87set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c 88{
770de199 89 const struct demangler_engine *dem;
c906108c
SS
90
91 /* First just try to match whatever style name the user supplied with
92 one of the known ones. Don't bother special casing for an empty
93 name, we just treat it as any other style name that doesn't match.
94 If we match, update the current demangling style enum. */
95
770de199
DB
96 for (dem = libiberty_demanglers;
97 dem->demangling_style != unknown_demangling;
98 dem++)
c906108c 99 {
bde58177
AC
100 if (strcmp (current_demangling_style_string,
101 dem->demangling_style_name) == 0)
c906108c
SS
102 {
103 current_demangling_style = dem->demangling_style;
104 break;
105 }
106 }
107
108 /* Check to see if we found a match. If not, gripe about any non-empty
109 style name and supply a list of valid ones. FIXME: This should
110 probably be done with some sort of completion and with help. */
111
770de199 112 if (dem->demangling_style == unknown_demangling)
c906108c
SS
113 {
114 if (*current_demangling_style_string != '\0')
115 {
a3f17187 116 printf_unfiltered (_("Unknown demangling style `%s'.\n"),
c906108c
SS
117 current_demangling_style_string);
118 }
a3f17187 119 printf_unfiltered (_("The currently understood settings are:\n\n"));
770de199
DB
120 for (dem = libiberty_demanglers;
121 dem->demangling_style != unknown_demangling;
122 dem++)
c906108c
SS
123 {
124 printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
125 dem->demangling_style_doc);
126 if (dem->demangling_style == current_demangling_style)
127 {
b8c9b27d 128 xfree (current_demangling_style_string);
c906108c
SS
129 current_demangling_style_string =
130 savestring (dem->demangling_style_name,
131 strlen (dem->demangling_style_name));
132 }
133 }
134 if (current_demangling_style == unknown_demangling)
135 {
136 /* This can happen during initialization if gdb is compiled with
137 a DEMANGLING_STYLE value that is unknown, so pick the first
138 one as the default. */
770de199 139 current_demangling_style = libiberty_demanglers[0].demangling_style;
c906108c 140 current_demangling_style_string =
770de199
DB
141 savestring (
142 libiberty_demanglers[0].demangling_style_name,
143 strlen (libiberty_demanglers[0].demangling_style_name));
8a3fe4f8 144 warning (_("`%s' style demangling chosen as the default."),
c906108c
SS
145 current_demangling_style_string);
146 }
147 }
148}
149
150/* Fake a "set demangle-style" command. */
151
152void
fba45db2 153set_demangling_style (char *style)
c906108c
SS
154{
155 if (current_demangling_style_string != NULL)
156 {
b8c9b27d 157 xfree (current_demangling_style_string);
c906108c
SS
158 }
159 current_demangling_style_string = savestring (style, strlen (style));
160 set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
161}
162
8343f86c
DJ
163/* G++ uses a special character to indicate certain internal names. Which
164 character it is depends on the platform:
165 - Usually '$' on systems where the assembler will accept that
166 - Usually '.' otherwise (this includes most sysv4-like systems and most
167 ELF targets)
168 - Occasionally '_' if neither of the above is usable
169
170 We check '$' first because it is the safest, and '.' often has another
171 meaning. We don't currently try to handle '_' because the precise forms
172 of the names are different on those targets. */
173
174static char cplus_markers[] = {'$', '.', '\0'};
c906108c
SS
175
176int
fba45db2 177is_cplus_marker (int c)
c906108c
SS
178{
179 return c && strchr (cplus_markers, c) != NULL;
180}
181
182void
fba45db2 183_initialize_demangler (void)
c906108c
SS
184{
185 struct cmd_list_element *set, *show;
fa58ee11
EZ
186 int i, ndems;
187
188 /* Fill the demangling_style_names[] array. */
189 for (ndems = 0;
190 libiberty_demanglers[ndems].demangling_style != unknown_demangling;
191 ndems++)
192 ;
9e0c176c 193 demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
fa58ee11
EZ
194 for (i = 0;
195 libiberty_demanglers[i].demangling_style != unknown_demangling;
196 i++)
197 demangling_style_names[i] =
198 xstrdup (libiberty_demanglers[i].demangling_style_name);
199
7ab04401
AC
200 /* FIXME: cagney/2005-02-20: The code implementing this variable are
201 malloc-ing and free-ing current_demangling_style_string when it
202 should instead just point to an element of
203 demangling_style_names. */
204 add_setshow_enum_cmd ("demangle-style", class_support,
205 demangling_style_names,
206 (const char **) &current_demangling_style_string, _("\
207Set the current C++ demangling style."), _("\
208Show the current C++ demangling style."), _("\
209Use `set demangle-style' without arguments for a list of demangling styles."),
210 set_demangling_command,
920d2a44 211 show_demangling_style_names,
7ab04401 212 &setlist, &showlist);
c906108c
SS
213
214 /* Set the default demangling style chosen at compilation time. */
215 set_demangling_style (DEFAULT_DEMANGLING_STYLE);
c906108c 216}
This page took 0.387264 seconds and 4 git commands to generate.