]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/demangle.c
Switch the license of all .c files to GPLv3.
[thirdparty/binutils-gdb.git] / gdb / demangle.c
CommitLineData
c906108c 1/* Basic C++ demangling support for GDB.
1bac305b 2
6aba47ca
DJ
3 Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2003, 2007 Free Software Foundation, Inc.
1bac305b 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
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
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
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
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
a14ed312 45extern void _initialize_demangler (void);
392a587b 46
c906108c
SS
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
51static char *current_demangling_style_string;
52
fa58ee11
EZ
53/* The array of names of the known demanglyng styles. Generated by
54 _initialize_demangler from libiberty_demanglers[] array. */
55
56static const char **demangling_style_names;
920d2a44
AC
57static void
58show_demangling_style_names(struct ui_file *file, int from_tty,
59 struct cmd_list_element *c, const char *value)
60{
61 fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
62 value);
63}
64
fa58ee11 65
a14ed312 66static void set_demangling_command (char *, int, struct cmd_list_element *);
c906108c
SS
67
68/* Set current demangling style. Called by the "set demangle-style"
69 command after it has updated the current_demangling_style_string to
70 match what the user has entered.
71
72 If the user has entered a string that matches a known demangling style
73 name in the demanglers[] array then just leave the string alone and update
74 the current_demangling_style enum value to match.
75
76 If the user has entered a string that doesn't match, including an empty
77 string, then print a list of the currently known styles and restore
78 the current_demangling_style_string to match the current_demangling_style
79 enum value.
80
81 Note: Assumes that current_demangling_style_string always points to
82 a malloc'd string, even if it is a null-string. */
83
84static void
fba45db2 85set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c 86{
770de199 87 const struct demangler_engine *dem;
c906108c
SS
88
89 /* First just try to match whatever style name the user supplied with
90 one of the known ones. Don't bother special casing for an empty
91 name, we just treat it as any other style name that doesn't match.
92 If we match, update the current demangling style enum. */
93
770de199
DB
94 for (dem = libiberty_demanglers;
95 dem->demangling_style != unknown_demangling;
96 dem++)
c906108c 97 {
bde58177
AC
98 if (strcmp (current_demangling_style_string,
99 dem->demangling_style_name) == 0)
c906108c
SS
100 {
101 current_demangling_style = dem->demangling_style;
102 break;
103 }
104 }
105
106 /* Check to see if we found a match. If not, gripe about any non-empty
107 style name and supply a list of valid ones. FIXME: This should
108 probably be done with some sort of completion and with help. */
109
770de199 110 if (dem->demangling_style == unknown_demangling)
c906108c
SS
111 {
112 if (*current_demangling_style_string != '\0')
113 {
a3f17187 114 printf_unfiltered (_("Unknown demangling style `%s'.\n"),
c906108c
SS
115 current_demangling_style_string);
116 }
a3f17187 117 printf_unfiltered (_("The currently understood settings are:\n\n"));
770de199
DB
118 for (dem = libiberty_demanglers;
119 dem->demangling_style != unknown_demangling;
120 dem++)
c906108c
SS
121 {
122 printf_unfiltered ("%-10s %s\n", dem->demangling_style_name,
123 dem->demangling_style_doc);
124 if (dem->demangling_style == current_demangling_style)
125 {
b8c9b27d 126 xfree (current_demangling_style_string);
c906108c
SS
127 current_demangling_style_string =
128 savestring (dem->demangling_style_name,
129 strlen (dem->demangling_style_name));
130 }
131 }
132 if (current_demangling_style == unknown_demangling)
133 {
134 /* This can happen during initialization if gdb is compiled with
135 a DEMANGLING_STYLE value that is unknown, so pick the first
136 one as the default. */
770de199 137 current_demangling_style = libiberty_demanglers[0].demangling_style;
c906108c 138 current_demangling_style_string =
770de199
DB
139 savestring (
140 libiberty_demanglers[0].demangling_style_name,
141 strlen (libiberty_demanglers[0].demangling_style_name));
8a3fe4f8 142 warning (_("`%s' style demangling chosen as the default."),
c906108c
SS
143 current_demangling_style_string);
144 }
145 }
146}
147
148/* Fake a "set demangle-style" command. */
149
150void
fba45db2 151set_demangling_style (char *style)
c906108c
SS
152{
153 if (current_demangling_style_string != NULL)
154 {
b8c9b27d 155 xfree (current_demangling_style_string);
c906108c
SS
156 }
157 current_demangling_style_string = savestring (style, strlen (style));
158 set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
159}
160
8343f86c
DJ
161/* G++ uses a special character to indicate certain internal names. Which
162 character it is depends on the platform:
163 - Usually '$' on systems where the assembler will accept that
164 - Usually '.' otherwise (this includes most sysv4-like systems and most
165 ELF targets)
166 - Occasionally '_' if neither of the above is usable
167
168 We check '$' first because it is the safest, and '.' often has another
169 meaning. We don't currently try to handle '_' because the precise forms
170 of the names are different on those targets. */
171
172static char cplus_markers[] = {'$', '.', '\0'};
c906108c
SS
173
174int
fba45db2 175is_cplus_marker (int c)
c906108c
SS
176{
177 return c && strchr (cplus_markers, c) != NULL;
178}
179
180void
fba45db2 181_initialize_demangler (void)
c906108c
SS
182{
183 struct cmd_list_element *set, *show;
fa58ee11
EZ
184 int i, ndems;
185
186 /* Fill the demangling_style_names[] array. */
187 for (ndems = 0;
188 libiberty_demanglers[ndems].demangling_style != unknown_demangling;
189 ndems++)
190 ;
9e0c176c 191 demangling_style_names = xcalloc (ndems + 1, sizeof (char *));
fa58ee11
EZ
192 for (i = 0;
193 libiberty_demanglers[i].demangling_style != unknown_demangling;
194 i++)
195 demangling_style_names[i] =
196 xstrdup (libiberty_demanglers[i].demangling_style_name);
197
7ab04401
AC
198 /* FIXME: cagney/2005-02-20: The code implementing this variable are
199 malloc-ing and free-ing current_demangling_style_string when it
200 should instead just point to an element of
201 demangling_style_names. */
202 add_setshow_enum_cmd ("demangle-style", class_support,
203 demangling_style_names,
204 (const char **) &current_demangling_style_string, _("\
205Set the current C++ demangling style."), _("\
206Show the current C++ demangling style."), _("\
207Use `set demangle-style' without arguments for a list of demangling styles."),
208 set_demangling_command,
920d2a44 209 show_demangling_style_names,
7ab04401 210 &setlist, &showlist);
c906108c
SS
211
212 /* Set the default demangling style chosen at compilation time. */
213 set_demangling_style (DEFAULT_DEMANGLING_STYLE);
c906108c 214}