Automatic date update in version.in
[binutils-gdb.git] / gdb / macrocmd.c
blobddabfede0f55b39bbab0d8d5296ee136d445bea1
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 "gdbcmd.h"
28 #include "linespec.h"
31 /* The `macro' prefix command. */
33 static struct cmd_list_element *macrolist;
36 /* Macro expansion commands. */
39 /* Prints an informational message regarding the lack of macro information. */
40 static void
41 macro_inform_no_debuginfo (void)
43 gdb_puts ("GDB has no preprocessor macro information for that code.\n");
46 static void
47 macro_expand_command (const char *exp, int from_tty)
49 /* You know, when the user doesn't specify any expression, it would be
50 really cool if this defaulted to the last expression evaluated.
51 Then it would be easy to ask, "Hey, what did I just evaluate?" But
52 at the moment, the `print' commands don't save the last expression
53 evaluated, just its value. */
54 if (! exp || ! *exp)
55 error (_("You must follow the `macro expand' command with the"
56 " expression you\n"
57 "want to expand."));
59 gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope ();
61 if (ms != nullptr)
63 gdb::unique_xmalloc_ptr<char> expanded = macro_expand (exp, *ms);
65 gdb_puts ("expands to: ");
66 gdb_puts (expanded.get ());
67 gdb_puts ("\n");
69 else
70 macro_inform_no_debuginfo ();
74 static void
75 macro_expand_once_command (const char *exp, int from_tty)
77 /* You know, when the user doesn't specify any expression, it would be
78 really cool if this defaulted to the last expression evaluated.
79 And it should set the once-expanded text as the new `last
80 expression'. That way, you could just hit return over and over and
81 see the expression expanded one level at a time. */
82 if (! exp || ! *exp)
83 error (_("You must follow the `macro expand-once' command with"
84 " the expression\n"
85 "you want to expand."));
87 gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope ();
89 if (ms != nullptr)
91 gdb::unique_xmalloc_ptr<char> expanded = macro_expand_once (exp, *ms);
93 gdb_puts ("expands to: ");
94 gdb_puts (expanded.get ());
95 gdb_puts ("\n");
97 else
98 macro_inform_no_debuginfo ();
101 /* Outputs the include path of a macro starting at FILE and LINE to STREAM.
103 Care should be taken that this function does not cause any lookups into
104 the splay tree so that it can be safely used while iterating. */
105 static void
106 show_pp_source_pos (struct ui_file *stream,
107 struct macro_source_file *file,
108 int line)
110 std::string fullname = macro_source_fullname (file);
111 gdb_printf (stream, "%ps:%d\n",
112 styled_string (file_name_style.style (),
113 fullname.c_str ()),
114 line);
116 while (file->included_by)
118 fullname = macro_source_fullname (file->included_by);
119 gdb_puts (_(" included at "), stream);
120 fputs_styled (fullname.c_str (), file_name_style.style (), stream);
121 gdb_printf (stream, ":%d\n", file->included_at_line);
122 file = file->included_by;
126 /* Outputs a macro for human consumption, detailing the include path
127 and macro definition. NAME is the name of the macro.
128 D the definition. FILE the start of the include path, and LINE the
129 line number in FILE.
131 Care should be taken that this function does not cause any lookups into
132 the splay tree so that it can be safely used while iterating. */
133 static void
134 print_macro_definition (const char *name,
135 const struct macro_definition *d,
136 struct macro_source_file *file,
137 int line)
139 gdb_printf ("Defined at ");
140 show_pp_source_pos (gdb_stdout, file, line);
142 if (line != 0)
143 gdb_printf ("#define %s", name);
144 else
145 gdb_printf ("-D%s", name);
147 if (d->kind == macro_function_like)
149 int i;
151 gdb_puts ("(");
152 for (i = 0; i < d->argc; i++)
154 gdb_puts (d->argv[i]);
155 if (i + 1 < d->argc)
156 gdb_puts (", ");
158 gdb_puts (")");
161 if (line != 0)
162 gdb_printf (" %s\n", d->replacement);
163 else
164 gdb_printf ("=%s\n", d->replacement);
167 /* The implementation of the `info macro' command. */
168 static void
169 info_macro_command (const char *args, int from_tty)
171 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
172 const char *name;
173 int show_all_macros_named = 0;
174 const char *arg_start = args;
175 int processing_args = 1;
177 while (processing_args
178 && arg_start && *arg_start == '-' && *arg_start != '\0')
180 const char *p = skip_to_space (arg_start);
182 if (strncmp (arg_start, "-a", p - arg_start) == 0
183 || strncmp (arg_start, "-all", p - arg_start) == 0)
184 show_all_macros_named = 1;
185 else if (strncmp (arg_start, "--", p - arg_start) == 0)
186 /* Our macro support seems rather C specific but this would
187 seem necessary for languages allowing - in macro names.
188 e.g. Scheme's (defmacro ->foo () "bar\n") */
189 processing_args = 0;
190 else
191 report_unrecognized_option_error ("info macro", arg_start);
193 arg_start = skip_spaces (p);
196 name = arg_start;
198 if (! name || ! *name)
199 error (_("You must follow the `info macro' command with the name"
200 " of the macro\n"
201 "whose definition you want to see."));
203 ms = default_macro_scope ();
205 if (! ms)
206 macro_inform_no_debuginfo ();
207 else if (show_all_macros_named)
208 macro_for_each (ms->file->table, [&] (const char *macro_name,
209 const macro_definition *macro,
210 macro_source_file *source,
211 int line)
213 if (strcmp (name, macro_name) == 0)
214 print_macro_definition (name, macro, source, line);
216 else
218 struct macro_definition *d;
220 d = macro_lookup_definition (ms->file, ms->line, name);
221 if (d)
223 int line;
224 struct macro_source_file *file
225 = macro_definition_location (ms->file, ms->line, name, &line);
227 print_macro_definition (name, d, file, line);
229 else
231 gdb_printf ("The symbol `%s' has no definition as a C/C++"
232 " preprocessor macro\n"
233 "at ", name);
234 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
239 /* Implementation of the "info macros" command. */
240 static void
241 info_macros_command (const char *args, int from_tty)
243 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
245 if (args == NULL)
246 ms = default_macro_scope ();
247 else
249 std::vector<symtab_and_line> sals
250 = decode_line_with_current_source (args, 0);
252 if (!sals.empty ())
253 ms = sal_macro_scope (sals[0]);
256 if (! ms || ! ms->file || ! ms->file->table)
257 macro_inform_no_debuginfo ();
258 else
259 macro_for_each_in_scope (ms->file, ms->line, print_macro_definition);
263 /* User-defined macros. */
265 static void
266 skip_ws (const char **expp)
268 while (macro_is_whitespace (**expp))
269 ++*expp;
272 /* Try to find the bounds of an identifier. If an identifier is
273 found, returns a newly allocated string; otherwise returns NULL.
274 EXPP is a pointer to an input string; it is updated to point to the
275 text following the identifier. If IS_PARAMETER is true, this
276 function will also allow "..." forms as used in varargs macro
277 parameters. */
279 static gdb::unique_xmalloc_ptr<char>
280 extract_identifier (const char **expp, int is_parameter)
282 char *result;
283 const char *p = *expp;
284 unsigned int len;
286 if (is_parameter && startswith (p, "..."))
288 /* Ok. */
290 else
292 if (! *p || ! macro_is_identifier_nondigit (*p))
293 return NULL;
294 for (++p;
295 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
296 ++p)
300 if (is_parameter && startswith (p, "..."))
301 p += 3;
303 len = p - *expp;
304 result = (char *) xmalloc (len + 1);
305 memcpy (result, *expp, len);
306 result[len] = '\0';
307 *expp += len;
308 return gdb::unique_xmalloc_ptr<char> (result);
311 struct temporary_macro_definition : public macro_definition
313 temporary_macro_definition ()
315 table = nullptr;
316 kind = macro_object_like;
317 argc = 0;
318 argv = nullptr;
319 replacement = nullptr;
322 ~temporary_macro_definition ()
324 int i;
326 for (i = 0; i < argc; ++i)
327 xfree ((char *) argv[i]);
328 xfree ((char *) argv);
329 /* Note that the 'replacement' field is not allocated. */
333 static void
334 macro_define_command (const char *exp, int from_tty)
336 temporary_macro_definition new_macro;
338 if (!exp)
339 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
341 skip_ws (&exp);
342 gdb::unique_xmalloc_ptr<char> name = extract_identifier (&exp, 0);
343 if (name == NULL)
344 error (_("Invalid macro name."));
345 if (*exp == '(')
347 /* Function-like macro. */
348 int alloced = 5;
349 char **argv = XNEWVEC (char *, alloced);
351 new_macro.kind = macro_function_like;
352 new_macro.argc = 0;
353 new_macro.argv = (const char * const *) argv;
355 /* Skip the '(' and whitespace. */
356 ++exp;
357 skip_ws (&exp);
359 while (*exp != ')')
361 int i;
363 if (new_macro.argc == alloced)
365 alloced *= 2;
366 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
367 /* Must update new_macro as well... */
368 new_macro.argv = (const char * const *) argv;
370 argv[new_macro.argc] = extract_identifier (&exp, 1).release ();
371 if (! argv[new_macro.argc])
372 error (_("Macro is missing an argument."));
373 ++new_macro.argc;
375 for (i = new_macro.argc - 2; i >= 0; --i)
377 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
378 error (_("Two macro arguments with identical names."));
381 skip_ws (&exp);
382 if (*exp == ',')
384 ++exp;
385 skip_ws (&exp);
387 else if (*exp != ')')
388 error (_("',' or ')' expected at end of macro arguments."));
390 /* Skip the closing paren. */
391 ++exp;
392 skip_ws (&exp);
394 macro_define_function (macro_main (macro_user_macros), -1, name.get (),
395 new_macro.argc, (const char **) new_macro.argv,
396 exp);
398 else
400 skip_ws (&exp);
401 macro_define_object (macro_main (macro_user_macros), -1, name.get (),
402 exp);
407 static void
408 macro_undef_command (const char *exp, int from_tty)
410 if (!exp)
411 error (_("usage: macro undef NAME"));
413 skip_ws (&exp);
414 gdb::unique_xmalloc_ptr<char> name = extract_identifier (&exp, 0);
415 if (name == nullptr)
416 error (_("Invalid macro name."));
417 macro_undef (macro_main (macro_user_macros), -1, name.get ());
421 static void
422 print_one_macro (const char *name, const struct macro_definition *macro,
423 struct macro_source_file *source, int line)
425 gdb_printf ("macro define %s", name);
426 if (macro->kind == macro_function_like)
428 int i;
430 gdb_printf ("(");
431 for (i = 0; i < macro->argc; ++i)
432 gdb_printf ("%s%s", (i > 0) ? ", " : "",
433 macro->argv[i]);
434 gdb_printf (")");
436 gdb_printf (" %s\n", macro->replacement);
440 static void
441 macro_list_command (const char *exp, int from_tty)
443 macro_for_each (macro_user_macros, print_one_macro);
446 /* Initializing the `macrocmd' module. */
448 void _initialize_macrocmd ();
449 void
450 _initialize_macrocmd ()
452 /* We introduce a new command prefix, `macro', under which we'll put
453 the various commands for working with preprocessor macros. */
454 add_basic_prefix_cmd ("macro", class_info,
455 _("Prefix for commands dealing with C preprocessor macros."),
456 &macrolist, 0, &cmdlist);
458 cmd_list_element *macro_expand_cmd
459 = add_cmd ("expand", no_class, macro_expand_command, _("\
460 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
461 Show the expanded expression."),
462 &macrolist);
463 add_alias_cmd ("exp", macro_expand_cmd, no_class, 1, &macrolist);
465 cmd_list_element *macro_expand_once_cmd
466 = add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
467 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
468 Show the expanded expression.\n\
470 This command differs from `macro expand' in that it only expands macro\n\
471 invocations that appear directly in EXPRESSION; if expanding a macro\n\
472 introduces further macro invocations, those are left unexpanded.\n\
474 `macro expand-once' helps you see how a particular macro expands,\n\
475 whereas `macro expand' shows you how all the macros involved in an\n\
476 expression work together to yield a pre-processed expression."),
477 &macrolist);
478 add_alias_cmd ("exp1", macro_expand_once_cmd, no_class, 1, &macrolist);
480 add_info ("macro", info_macro_command,
481 _("Show the definition of MACRO, and it's source location.\n\
482 Usage: info macro [-a|-all] [--] MACRO\n\
483 Options: \n\
484 -a, --all Output all definitions of MACRO in the current compilation\
485 unit.\n\
486 -- Specify the end of arguments and the beginning of the MACRO."));
488 add_info ("macros", info_macros_command,
489 _("Show the definitions of all macros at LINESPEC, or the current \
490 source location.\n\
491 Usage: info macros [LINESPEC]"));
493 add_cmd ("define", no_class, macro_define_command, _("\
494 Define a new C/C++ preprocessor macro.\n\
495 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
496 preprocessor directive of the form `#define DEFINITION' such that the\n\
497 definition is visible in all the inferior's source files.\n\
498 For example:\n\
499 (gdb) macro define PI (3.1415926)\n\
500 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
501 &macrolist);
503 add_cmd ("undef", no_class, macro_undef_command, _("\
504 Remove the definition of the C/C++ preprocessor macro with the given name."),
505 &macrolist);
507 add_cmd ("list", no_class, macro_list_command,
508 _("List all the macros defined using the `macro define' command."),
509 &macrolist);