1 /* C preprocessor macro expansion commands for GDB.
2 Copyright (C) 2002, 2007, 2008, 2009, 2010 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/>. */
24 #include "macroscope.h"
27 #include "gdb_string.h"
30 /* The `macro' prefix command. */
32 static struct cmd_list_element *macrolist;
35 macro_command (char *arg, int from_tty)
38 ("\"macro\" must be followed by the name of a macro command.\n");
39 help_list (macrolist, "macro ", -1, gdb_stdout);
44 /* Macro expansion commands. */
48 macro_expand_command (char *exp, int from_tty)
50 struct macro_scope *ms = NULL;
51 char *expanded = NULL;
52 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
54 make_cleanup (free_current_contents, &expanded);
56 /* You know, when the user doesn't specify any expression, it would be
57 really cool if this defaulted to the last expression evaluated.
58 Then it would be easy to ask, "Hey, what did I just evaluate?" But
59 at the moment, the `print' commands don't save the last expression
60 evaluated, just its value. */
62 error (_("You must follow the `macro expand' command with the"
66 ms = default_macro_scope ();
69 expanded = macro_expand (exp, standard_macro_lookup, ms);
70 fputs_filtered ("expands to: ", gdb_stdout);
71 fputs_filtered (expanded, gdb_stdout);
72 fputs_filtered ("\n", gdb_stdout);
75 fputs_filtered ("GDB has no preprocessor macro information for "
79 do_cleanups (cleanup_chain);
85 macro_expand_once_command (char *exp, int from_tty)
87 struct macro_scope *ms = NULL;
88 char *expanded = NULL;
89 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
90 make_cleanup (free_current_contents, &expanded);
92 /* You know, when the user doesn't specify any expression, it would be
93 really cool if this defaulted to the last expression evaluated.
94 And it should set the once-expanded text as the new `last
95 expression'. That way, you could just hit return over and over and
96 see the expression expanded one level at a time. */
98 error (_("You must follow the `macro expand-once' command with"
100 "you want to expand."));
102 ms = default_macro_scope ();
105 expanded = macro_expand_once (exp, standard_macro_lookup, ms);
106 fputs_filtered ("expands to: ", gdb_stdout);
107 fputs_filtered (expanded, gdb_stdout);
108 fputs_filtered ("\n", gdb_stdout);
111 fputs_filtered ("GDB has no preprocessor macro information for "
115 do_cleanups (cleanup_chain);
121 show_pp_source_pos (struct ui_file *stream,
122 struct macro_source_file *file,
125 fprintf_filtered (stream, "%s:%d\n", file->filename, line);
127 while (file->included_by)
129 fprintf_filtered (gdb_stdout, " included at %s:%d\n",
130 file->included_by->filename,
131 file->included_at_line);
132 file = file->included_by;
138 info_macro_command (char *name, int from_tty)
140 struct macro_scope *ms = NULL;
141 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
142 struct macro_definition *d;
144 if (! name || ! *name)
145 error (_("You must follow the `info macro' command with the name"
147 "whose definition you want to see."));
149 ms = default_macro_scope ();
151 error (_("GDB has no preprocessor macro information for that code."));
153 d = macro_lookup_definition (ms->file, ms->line, name);
157 struct macro_source_file *file
158 = macro_definition_location (ms->file, ms->line, name, &line);
160 fprintf_filtered (gdb_stdout, "Defined at ");
161 show_pp_source_pos (gdb_stdout, file, line);
163 fprintf_filtered (gdb_stdout, "#define %s", name);
165 fprintf_filtered (gdb_stdout, "-D%s", name);
166 if (d->kind == macro_function_like)
170 fputs_filtered ("(", gdb_stdout);
171 for (i = 0; i < d->argc; i++)
173 fputs_filtered (d->argv[i], gdb_stdout);
175 fputs_filtered (", ", gdb_stdout);
177 fputs_filtered (")", gdb_stdout);
180 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
182 fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
186 fprintf_filtered (gdb_stdout,
187 "The symbol `%s' has no definition as a C/C++"
188 " preprocessor macro\n"
190 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
193 do_cleanups (cleanup_chain);
198 /* User-defined macros. */
201 skip_ws (char **expp)
203 while (macro_is_whitespace (**expp))
207 /* Try to find the bounds of an identifier. If an identifier is
208 found, returns a newly allocated string; otherwise returns NULL.
209 EXPP is a pointer to an input string; it is updated to point to the
210 text following the identifier. If IS_PARAMETER is true, this
211 function will also allow "..." forms as used in varargs macro
215 extract_identifier (char **expp, int is_parameter)
221 if (is_parameter && !strncmp (p, "...", 3))
227 if (! *p || ! macro_is_identifier_nondigit (*p))
230 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
235 if (is_parameter && !strncmp (p, "...", 3))
239 result = (char *) xmalloc (len + 1);
240 memcpy (result, *expp, len);
246 /* Helper function to clean up a temporarily-constructed macro object.
247 This assumes that the contents were all allocated with xmalloc. */
249 free_macro_definition_ptr (void *ptr)
252 struct macro_definition *loc = (struct macro_definition *) ptr;
254 for (i = 0; i < loc->argc; ++i)
255 xfree ((char *) loc->argv[i]);
256 xfree ((char *) loc->argv);
257 /* Note that the 'replacement' field is not allocated. */
261 macro_define_command (char *exp, int from_tty)
263 struct macro_definition new_macro;
265 struct cleanup *cleanup_chain;
268 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
270 cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
271 make_cleanup (free_current_contents, &name);
273 memset (&new_macro, 0, sizeof (struct macro_definition));
276 name = extract_identifier (&exp, 0);
278 error (_("Invalid macro name."));
281 /* Function-like macro. */
283 char **argv = (char **) xmalloc (alloced * sizeof (char *));
285 new_macro.kind = macro_function_like;
287 new_macro.argv = (const char * const *) argv;
289 /* Skip the '(' and whitespace. */
297 if (new_macro.argc == alloced)
300 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
301 /* Must update new_macro as well... */
302 new_macro.argv = (const char * const *) argv;
304 argv[new_macro.argc] = extract_identifier (&exp, 1);
305 if (! argv[new_macro.argc])
306 error (_("Macro is missing an argument."));
309 for (i = new_macro.argc - 2; i >= 0; --i)
311 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
312 error (_("Two macro arguments with identical names."));
321 else if (*exp != ')')
322 error (_("',' or ')' expected at end of macro arguments."));
324 /* Skip the closing paren. */
328 macro_define_function (macro_main (macro_user_macros), -1, name,
329 new_macro.argc, (const char **) new_macro.argv,
335 macro_define_object (macro_main (macro_user_macros), -1, name, exp);
338 do_cleanups (cleanup_chain);
343 macro_undef_command (char *exp, int from_tty)
348 error (_("usage: macro undef NAME"));
351 name = extract_identifier (&exp, 0);
353 error (_("Invalid macro name."));
354 macro_undef (macro_main (macro_user_macros), -1, name);
360 print_one_macro (const char *name, const struct macro_definition *macro,
363 fprintf_filtered (gdb_stdout, "macro define %s", name);
364 if (macro->kind == macro_function_like)
368 fprintf_filtered (gdb_stdout, "(");
369 for (i = 0; i < macro->argc; ++i)
370 fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
372 fprintf_filtered (gdb_stdout, ")");
374 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
379 macro_list_command (char *exp, int from_tty)
381 macro_for_each (macro_user_macros, print_one_macro, NULL);
386 /* Initializing the `macrocmd' module. */
388 extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
391 _initialize_macrocmd (void)
393 /* We introduce a new command prefix, `macro', under which we'll put
394 the various commands for working with preprocessor macros. */
395 add_prefix_cmd ("macro", class_info, macro_command,
396 _("Prefix for commands dealing with C preprocessor macros."),
397 ¯olist, "macro ", 0, &cmdlist);
399 add_cmd ("expand", no_class, macro_expand_command, _("\
400 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
401 Show the expanded expression."),
403 add_alias_cmd ("exp", "expand", no_class, 1, ¯olist);
404 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
405 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
406 Show the expanded expression.\n\
408 This command differs from `macro expand' in that it only expands macro\n\
409 invocations that appear directly in EXPRESSION; if expanding a macro\n\
410 introduces further macro invocations, those are left unexpanded.\n\
412 `macro expand-once' helps you see how a particular macro expands,\n\
413 whereas `macro expand' shows you how all the macros involved in an\n\
414 expression work together to yield a pre-processed expression."),
416 add_alias_cmd ("exp1", "expand-once", no_class, 1, ¯olist);
418 add_cmd ("macro", no_class, info_macro_command,
419 _("Show the definition of MACRO, and its source location."),
422 add_cmd ("define", no_class, macro_define_command, _("\
423 Define a new C/C++ preprocessor macro.\n\
424 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
425 preprocessor directive of the form `#define DEFINITION' such that the\n\
426 definition is visible in all the inferior's source files.\n\
428 (gdb) macro define PI (3.1415926)\n\
429 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
432 add_cmd ("undef", no_class, macro_undef_command, _("\
433 Remove the definition of the C/C++ preprocessor macro with the given name."),
436 add_cmd ("list", no_class, macro_list_command,
437 _("List all the macros defined using the `macro define' command."),