More updated translations
[binutils-gdb.git] / gdb / macrocmd.c
blob2da5a5bcf929fa15b2be56adc9b61623cdd68dcf
1 /* C preprocessor macro expansion commands for GDB.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "macrotab.h"
22 #include "macroexp.h"
23 #include "macroscope.h"
24 #include "cli/cli-style.h"
25 #include "cli/cli-utils.h"
26 #include "command.h"
27 #include "cli/cli-cmds.h"
28 #include "linespec.h"
29 #include "ui-out.h"
32 /* The `macro' prefix command. */
34 static struct cmd_list_element *macrolist;
37 /* Macro expansion commands. */
40 /* Prints an informational message regarding the lack of macro information. */
41 static void
42 macro_inform_no_debuginfo (void)
44 gdb_puts ("GDB has no preprocessor macro information for that code.\n");
47 static void
48 macro_expand_command (const char *exp, int from_tty)
50 /* You know, when the user doesn't specify any expression, it would be
51 really cool if this defaulted to the last expression evaluated.
52 Then it would be easy to ask, "Hey, what did I just evaluate?" But
53 at the moment, the `print' commands don't save the last expression
54 evaluated, just its value. */
55 if (! exp || ! *exp)
56 error (_("You must follow the `macro expand' command with the"
57 " expression you\n"
58 "want to expand."));
60 gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope ();
62 if (ms != nullptr)
64 gdb::unique_xmalloc_ptr<char> expanded = macro_expand (exp, *ms);
66 gdb_puts ("expands to: ");
67 gdb_puts (expanded.get ());
68 gdb_puts ("\n");
70 else
71 macro_inform_no_debuginfo ();
75 static void
76 macro_expand_once_command (const char *exp, int from_tty)
78 /* You know, when the user doesn't specify any expression, it would be
79 really cool if this defaulted to the last expression evaluated.
80 And it should set the once-expanded text as the new `last
81 expression'. That way, you could just hit return over and over and
82 see the expression expanded one level at a time. */
83 if (! exp || ! *exp)
84 error (_("You must follow the `macro expand-once' command with"
85 " the expression\n"
86 "you want to expand."));
88 gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope ();
90 if (ms != nullptr)
92 gdb::unique_xmalloc_ptr<char> expanded = macro_expand_once (exp, *ms);
94 gdb_puts ("expands to: ");
95 gdb_puts (expanded.get ());
96 gdb_puts ("\n");
98 else
99 macro_inform_no_debuginfo ();
102 /* Outputs the include path of a macro starting at FILE and LINE to STREAM.
104 Care should be taken that this function does not cause any lookups into
105 the splay tree so that it can be safely used while iterating. */
106 static void
107 show_pp_source_pos (struct ui_file *stream,
108 struct macro_source_file *file,
109 int line)
111 std::string fullname = macro_source_fullname (file);
112 gdb_printf (stream, "%ps:%d\n",
113 styled_string (file_name_style.style (),
114 fullname.c_str ()),
115 line);
117 while (file->included_by)
119 fullname = macro_source_fullname (file->included_by);
120 gdb_puts (_(" included at "), stream);
121 fputs_styled (fullname.c_str (), file_name_style.style (), stream);
122 gdb_printf (stream, ":%d\n", file->included_at_line);
123 file = file->included_by;
127 /* Outputs a macro for human consumption, detailing the include path
128 and macro definition. NAME is the name of the macro.
129 D the definition. FILE the start of the include path, and LINE the
130 line number in FILE.
132 Care should be taken that this function does not cause any lookups into
133 the splay tree so that it can be safely used while iterating. */
134 static void
135 print_macro_definition (const char *name,
136 const struct macro_definition *d,
137 struct macro_source_file *file,
138 int line)
140 gdb_printf ("Defined at ");
141 show_pp_source_pos (gdb_stdout, file, line);
143 if (line != 0)
144 gdb_printf ("#define %s", name);
145 else
146 gdb_printf ("-D%s", name);
148 if (d->kind == macro_function_like)
150 int i;
152 gdb_puts ("(");
153 for (i = 0; i < d->argc; i++)
155 gdb_puts (d->argv[i]);
156 if (i + 1 < d->argc)
157 gdb_puts (", ");
159 gdb_puts (")");
162 if (line != 0)
163 gdb_printf (" %s\n", d->replacement);
164 else
165 gdb_printf ("=%s\n", d->replacement);
168 /* The implementation of the `info macro' command. */
169 static void
170 info_macro_command (const char *args, int from_tty)
172 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
173 const char *name;
174 int show_all_macros_named = 0;
175 const char *arg_start = args;
176 int processing_args = 1;
178 while (processing_args
179 && arg_start && *arg_start == '-' && *arg_start != '\0')
181 const char *p = skip_to_space (arg_start);
183 if (strncmp (arg_start, "-a", p - arg_start) == 0
184 || strncmp (arg_start, "-all", p - arg_start) == 0)
185 show_all_macros_named = 1;
186 else if (strncmp (arg_start, "--", p - arg_start) == 0)
187 /* Our macro support seems rather C specific but this would
188 seem necessary for languages allowing - in macro names.
189 e.g. Scheme's (defmacro ->foo () "bar\n") */
190 processing_args = 0;
191 else
192 report_unrecognized_option_error ("info macro", arg_start);
194 arg_start = skip_spaces (p);
197 name = arg_start;
199 if (! name || ! *name)
200 error (_("You must follow the `info macro' command with the name"
201 " of the macro\n"
202 "whose definition you want to see."));
204 ms = default_macro_scope ();
206 if (! ms)
207 macro_inform_no_debuginfo ();
208 else if (show_all_macros_named)
209 macro_for_each (ms->file->table, [&] (const char *macro_name,
210 const macro_definition *macro,
211 macro_source_file *source,
212 int line)
214 if (strcmp (name, macro_name) == 0)
215 print_macro_definition (name, macro, source, line);
217 else
219 struct macro_definition *d;
221 d = macro_lookup_definition (ms->file, ms->line, name);
222 if (d)
224 int line;
225 struct macro_source_file *file
226 = macro_definition_location (ms->file, ms->line, name, &line);
228 print_macro_definition (name, d, file, line);
230 else
232 gdb_printf ("The symbol `%s' has no definition as a C/C++"
233 " preprocessor macro\n"
234 "at ", name);
235 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
240 /* Implementation of the "info macros" command. */
241 static void
242 info_macros_command (const char *args, int from_tty)
244 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
246 if (args == NULL)
247 ms = default_macro_scope ();
248 else
250 std::vector<symtab_and_line> sals
251 = decode_line_with_current_source (args, 0);
253 if (!sals.empty ())
254 ms = sal_macro_scope (sals[0]);
257 if (! ms || ! ms->file || ! ms->file->table)
258 macro_inform_no_debuginfo ();
259 else
260 macro_for_each_in_scope (ms->file, ms->line, print_macro_definition);
264 /* User-defined macros. */
266 static void
267 skip_ws (const char **expp)
269 while (macro_is_whitespace (**expp))
270 ++*expp;
273 /* Try to find the bounds of an identifier. If an identifier is
274 found, return it; otherwise return an empty string.
276 EXPP is a pointer to an input string; it is updated to point to the
277 text following the identifier. If IS_PARAMETER is true, this
278 function will also allow "..." forms as used in varargs macro
279 parameters. */
281 static std::string
282 extract_identifier (const char **expp, int is_parameter)
284 const char *p = *expp;
286 if (is_parameter && startswith (p, "..."))
288 /* Ok. */
290 else
292 if (! *p || ! macro_is_identifier_nondigit (*p))
293 return {};
295 for (++p;
296 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
297 ++p)
301 if (is_parameter && startswith (p, "..."))
302 p += 3;
304 std::string result (*expp, p);
305 *expp = p;
307 return result;
310 static void
311 macro_define_command (const char *exp, int from_tty)
313 if (!exp)
314 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
316 skip_ws (&exp);
318 std::string name = extract_identifier (&exp, 0);
319 if (name.empty ())
320 error (_("Invalid macro name."));
322 if (*exp == '(')
324 /* Function-like macro. */
325 std::vector<std::string> argv;
327 /* Skip the '(' and whitespace. */
328 ++exp;
329 skip_ws (&exp);
331 while (*exp != ')')
333 argv.emplace_back (extract_identifier (&exp, 1));
334 if (argv.back ().empty ())
335 error (_("Macro is missing an argument."));
337 for (const auto &other_arg : argv)
339 if (&other_arg != &argv.back () && other_arg == argv.back ())
340 error (_("Two macro arguments with identical names."));
343 skip_ws (&exp);
344 if (*exp == ',')
346 ++exp;
347 skip_ws (&exp);
349 else if (*exp != ')')
350 error (_("',' or ')' expected at end of macro arguments."));
353 /* Skip the closing paren. */
354 ++exp;
355 skip_ws (&exp);
357 macro_define_function (macro_main (macro_user_macros), -1, name.c_str (),
358 argv, exp);
360 else
362 skip_ws (&exp);
363 macro_define_object (macro_main (macro_user_macros), -1, name.c_str (),
364 exp);
369 static void
370 macro_undef_command (const char *exp, int from_tty)
372 if (!exp)
373 error (_("usage: macro undef NAME"));
375 skip_ws (&exp);
377 std::string name = extract_identifier (&exp, 0);
378 if (name.empty ())
379 error (_("Invalid macro name."));
381 macro_undef (macro_main (macro_user_macros), -1, name.c_str ());
385 static void
386 print_one_macro (const char *name, const struct macro_definition *macro,
387 struct macro_source_file *source, int line)
389 gdb_printf ("macro define %s", name);
390 if (macro->kind == macro_function_like)
392 int i;
394 gdb_printf ("(");
395 for (i = 0; i < macro->argc; ++i)
396 gdb_printf ("%s%s", (i > 0) ? ", " : "",
397 macro->argv[i]);
398 gdb_printf (")");
400 gdb_printf (" %s\n", macro->replacement);
404 static void
405 macro_list_command (const char *exp, int from_tty)
407 macro_for_each (macro_user_macros, print_one_macro);
410 /* Initializing the `macrocmd' module. */
412 void _initialize_macrocmd ();
413 void
414 _initialize_macrocmd ()
416 /* We introduce a new command prefix, `macro', under which we'll put
417 the various commands for working with preprocessor macros. */
418 add_basic_prefix_cmd ("macro", class_info,
419 _("Prefix for commands dealing with C preprocessor macros."),
420 &macrolist, 0, &cmdlist);
422 cmd_list_element *macro_expand_cmd
423 = add_cmd ("expand", no_class, macro_expand_command, _("\
424 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
425 Show the expanded expression."),
426 &macrolist);
427 add_alias_cmd ("exp", macro_expand_cmd, no_class, 1, &macrolist);
429 cmd_list_element *macro_expand_once_cmd
430 = add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
431 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
432 Show the expanded expression.\n\
434 This command differs from `macro expand' in that it only expands macro\n\
435 invocations that appear directly in EXPRESSION; if expanding a macro\n\
436 introduces further macro invocations, those are left unexpanded.\n\
438 `macro expand-once' helps you see how a particular macro expands,\n\
439 whereas `macro expand' shows you how all the macros involved in an\n\
440 expression work together to yield a pre-processed expression."),
441 &macrolist);
442 add_alias_cmd ("exp1", macro_expand_once_cmd, no_class, 1, &macrolist);
444 add_info ("macro", info_macro_command,
445 _("Show the definition of MACRO, and it's source location.\n\
446 Usage: info macro [-a|-all] [--] MACRO\n\
447 Options:\n\
448 -a, --all Output all definitions of MACRO in the current compilation\
449 unit.\n\
450 -- Specify the end of arguments and the beginning of the MACRO."));
452 add_info ("macros", info_macros_command,
453 _("Show the definitions of all macros at LINESPEC, or the current \
454 source location.\n\
455 Usage: info macros [LINESPEC]"));
457 add_cmd ("define", no_class, macro_define_command, _("\
458 Define a new C/C++ preprocessor macro.\n\
459 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
460 preprocessor directive of the form `#define DEFINITION' such that the\n\
461 definition is visible in all the inferior's source files.\n\
462 For example:\n\
463 (gdb) macro define PI (3.1415926)\n\
464 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
465 &macrolist);
467 add_cmd ("undef", no_class, macro_undef_command, _("\
468 Remove the definition of the C/C++ preprocessor macro with the given name."),
469 &macrolist);
471 add_cmd ("list", no_class, macro_list_command,
472 _("List all the macros defined using the `macro define' command."),
473 &macrolist);