Don't use -fdiagnostics-color=never for rustc
[deliverable/binutils-gdb.git] / gdb / demangle.c
CommitLineData
c906108c 1/* Basic C++ demangling support for GDB.
1bac305b 2
61baf725 3 Copyright (C) 1991-2017 Free Software Foundation, Inc.
1bac305b 4
c906108c
SS
5 Written by Fred Fish at Cygnus Support.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22
23/* This file contains support code for C++ demangling that is common
0963b4bd 24 to a styles of demangling, and GDB specific. */
c906108c
SS
25
26#include "defs.h"
439250fb 27#include "cli/cli-utils.h" /* for skip_to_space */
c906108c
SS
28#include "command.h"
29#include "gdbcmd.h"
30#include "demangle.h"
50f182aa 31#include "gdb-demangle.h"
439250fb
DE
32#include "language.h"
33
c906108c
SS
34/* Select the default C++ demangling style to use. The default is "auto",
35 which allows gdb to attempt to pick an appropriate demangling style for
36 the executable it has loaded. It can be set to a specific style ("gnu",
37 "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
38 selection of the style unless you do an explicit "set demangle auto".
39 To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
0963b4bd 40 the appropriate target configuration file. */
c906108c
SS
41
42#ifndef DEFAULT_DEMANGLING_STYLE
43#define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
44#endif
45
439250fb
DE
46static void demangle_command (char *, int);
47
50f182aa
DE
48/* See documentation in gdb-demangle.h. */
49int demangle = 1;
50
51static void
52show_demangle (struct ui_file *file, int from_tty,
53 struct cmd_list_element *c, const char *value)
54{
55 fprintf_filtered (file,
56 _("Demangling of encoded C++/ObjC names "
57 "when displaying symbols is %s.\n"),
58 value);
59}
60
61/* See documentation in gdb-demangle.h. */
62int asm_demangle = 0;
63
64static void
65show_asm_demangle (struct ui_file *file, int from_tty,
66 struct cmd_list_element *c, const char *value)
67{
68 fprintf_filtered (file,
69 _("Demangling of C++/ObjC names in "
70 "disassembly listings is %s.\n"),
71 value);
72}
392a587b 73
c906108c
SS
74/* String name for the current demangling style. Set by the
75 "set demangle-style" command, printed as part of the output by the
0963b4bd 76 "show demangle-style" command. */
c906108c 77
10217050 78static const char *current_demangling_style_string;
c906108c 79
fa58ee11
EZ
80/* The array of names of the known demanglyng styles. Generated by
81 _initialize_demangler from libiberty_demanglers[] array. */
82
83static const char **demangling_style_names;
920d2a44
AC
84static void
85show_demangling_style_names(struct ui_file *file, int from_tty,
86 struct cmd_list_element *c, const char *value)
87{
88 fprintf_filtered (file, _("The current C++ demangling style is \"%s\".\n"),
89 value);
90}
91
c906108c
SS
92/* Set current demangling style. Called by the "set demangle-style"
93 command after it has updated the current_demangling_style_string to
94 match what the user has entered.
95
96 If the user has entered a string that matches a known demangling style
97 name in the demanglers[] array then just leave the string alone and update
98 the current_demangling_style enum value to match.
99
100 If the user has entered a string that doesn't match, including an empty
101 string, then print a list of the currently known styles and restore
102 the current_demangling_style_string to match the current_demangling_style
103 enum value.
104
105 Note: Assumes that current_demangling_style_string always points to
0963b4bd 106 a malloc'd string, even if it is a null-string. */
c906108c
SS
107
108static void
fba45db2 109set_demangling_command (char *ignore, int from_tty, struct cmd_list_element *c)
c906108c 110{
770de199 111 const struct demangler_engine *dem;
10217050 112 int i;
c906108c
SS
113
114 /* First just try to match whatever style name the user supplied with
115 one of the known ones. Don't bother special casing for an empty
116 name, we just treat it as any other style name that doesn't match.
0963b4bd 117 If we match, update the current demangling style enum. */
c906108c 118
10217050 119 for (dem = libiberty_demanglers, i = 0;
770de199
DB
120 dem->demangling_style != unknown_demangling;
121 dem++)
c906108c 122 {
bde58177
AC
123 if (strcmp (current_demangling_style_string,
124 dem->demangling_style_name) == 0)
c906108c
SS
125 {
126 current_demangling_style = dem->demangling_style;
10217050 127 current_demangling_style_string = demangling_style_names[i];
c906108c
SS
128 break;
129 }
10217050 130 i++;
c906108c
SS
131 }
132
10217050
PA
133 /* We should have found a match, given we only add known styles to
134 the enumeration list. */
135 gdb_assert (dem->demangling_style != unknown_demangling);
c906108c
SS
136}
137
8343f86c
DJ
138/* G++ uses a special character to indicate certain internal names. Which
139 character it is depends on the platform:
140 - Usually '$' on systems where the assembler will accept that
141 - Usually '.' otherwise (this includes most sysv4-like systems and most
142 ELF targets)
143 - Occasionally '_' if neither of the above is usable
144
145 We check '$' first because it is the safest, and '.' often has another
146 meaning. We don't currently try to handle '_' because the precise forms
147 of the names are different on those targets. */
148
149static char cplus_markers[] = {'$', '.', '\0'};
c906108c 150
50f182aa
DE
151/* See documentation in gdb-demangle.h. */
152
c906108c 153int
fba45db2 154is_cplus_marker (int c)
c906108c
SS
155{
156 return c && strchr (cplus_markers, c) != NULL;
157}
158
439250fb
DE
159/* Demangle the given string in the current language. */
160
161static void
162demangle_command (char *args, int from_tty)
163{
45343786
TT
164 char *demangled;
165 const char *name;
166 const char *arg_start;
439250fb
DE
167 int processing_args = 1;
168 const struct language_defn *lang;
439250fb 169
45343786
TT
170 std::string arg_buf = args != NULL ? args : "";
171 arg_start = arg_buf.c_str ();
439250fb 172
45343786 173 gdb::unique_xmalloc_ptr<char> lang_name;
439250fb
DE
174 while (processing_args
175 && *arg_start == '-')
176 {
45343786 177 const char *p = skip_to_space_const (arg_start);
439250fb
DE
178
179 if (strncmp (arg_start, "-l", p - arg_start) == 0)
45343786 180 lang_name.reset (extract_arg_const (&p));
439250fb
DE
181 else if (strncmp (arg_start, "--", p - arg_start) == 0)
182 processing_args = 0;
183 else
184 {
45343786 185 gdb::unique_xmalloc_ptr<char> option (extract_arg_const (&p));
439250fb 186 error (_("Unrecognized option '%s' to demangle command. "
45343786 187 "Try \"help demangle\"."), option.get ());
439250fb
DE
188 }
189
45343786 190 arg_start = skip_spaces_const (p);
439250fb
DE
191 }
192
193 name = arg_start;
194
195 if (*name == '\0')
196 error (_("Usage: demangle [-l language] [--] name"));
197
198 if (lang_name != NULL)
199 {
200 enum language lang_enum;
201
45343786 202 lang_enum = language_enum (lang_name.get ());
439250fb 203 if (lang_enum == language_unknown)
45343786 204 error (_("Unknown language \"%s\""), lang_name.get ());
439250fb
DE
205 lang = language_def (lang_enum);
206 }
207 else
208 lang = current_language;
209
210 demangled = language_demangle (lang, name, DMGL_ANSI | DMGL_PARAMS);
211 if (demangled != NULL)
212 {
213 printf_filtered ("%s\n", demangled);
214 xfree (demangled);
215 }
216 else
217 error (_("Can't demangle \"%s\""), name);
439250fb
DE
218}
219
50f182aa
DE
220extern initialize_file_ftype _initialize_demangler; /* -Wmissing-prototypes */
221
c906108c 222void
fba45db2 223_initialize_demangler (void)
c906108c 224{
fa58ee11
EZ
225 int i, ndems;
226
10217050
PA
227 /* Fill the demangling_style_names[] array, and set the default
228 demangling style chosen at compilation time. */
fa58ee11
EZ
229 for (ndems = 0;
230 libiberty_demanglers[ndems].demangling_style != unknown_demangling;
231 ndems++)
232 ;
224c3ddb 233 demangling_style_names = XCNEWVEC (const char *, ndems + 1);
fa58ee11
EZ
234 for (i = 0;
235 libiberty_demanglers[i].demangling_style != unknown_demangling;
236 i++)
10217050
PA
237 {
238 demangling_style_names[i]
239 = xstrdup (libiberty_demanglers[i].demangling_style_name);
240
241 if (current_demangling_style_string == NULL
242 && strcmp (DEFAULT_DEMANGLING_STYLE, demangling_style_names[i]) == 0)
243 current_demangling_style_string = demangling_style_names[i];
244 }
fa58ee11 245
50f182aa
DE
246 add_setshow_boolean_cmd ("demangle", class_support, &demangle, _("\
247Set demangling of encoded C++/ObjC names when displaying symbols."), _("\
248Show demangling of encoded C++/ObjC names when displaying symbols."), NULL,
249 NULL,
250 show_demangle,
251 &setprintlist, &showprintlist);
252
253 add_setshow_boolean_cmd ("asm-demangle", class_support, &asm_demangle, _("\
254Set demangling of C++/ObjC names in disassembly listings."), _("\
255Show demangling of C++/ObjC names in disassembly listings."), NULL,
256 NULL,
257 show_asm_demangle,
258 &setprintlist, &showprintlist);
259
7ab04401
AC
260 add_setshow_enum_cmd ("demangle-style", class_support,
261 demangling_style_names,
10217050 262 &current_demangling_style_string, _("\
7ab04401
AC
263Set the current C++ demangling style."), _("\
264Show the current C++ demangling style."), _("\
265Use `set demangle-style' without arguments for a list of demangling styles."),
266 set_demangling_command,
920d2a44 267 show_demangling_style_names,
7ab04401 268 &setlist, &showlist);
439250fb
DE
269
270 add_cmd ("demangle", class_support, demangle_command, _("\
271Demangle a mangled name.\n\
272Usage: demangle [-l language] [--] name\n\
273If LANGUAGE is not specified, NAME is demangled in the current language."),
274 &cmdlist);
c906108c 275}
This page took 1.157611 seconds and 4 git commands to generate.