1 /* C preprocessor macro expansion commands for GDB.
2 Copyright (C) 2002-2013 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"
25 #include "cli/cli-utils.h"
28 #include "gdb_string.h"
32 /* The `macro' prefix command. */
34 static struct cmd_list_element
*macrolist
;
37 macro_command (char *arg
, int from_tty
)
40 ("\"macro\" must be followed by the name of a macro command.\n");
41 help_list (macrolist
, "macro ", -1, gdb_stdout
);
46 /* Macro expansion commands. */
49 /* Prints an informational message regarding the lack of macro information. */
50 static void macro_inform_no_debuginfo()
52 puts_filtered ("GDB has no preprocessor macro information for that code.\n");
56 macro_expand_command (char *exp
, int from_tty
)
58 struct macro_scope
*ms
= NULL
;
59 char *expanded
= NULL
;
60 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
62 make_cleanup (free_current_contents
, &expanded
);
64 /* You know, when the user doesn't specify any expression, it would be
65 really cool if this defaulted to the last expression evaluated.
66 Then it would be easy to ask, "Hey, what did I just evaluate?" But
67 at the moment, the `print' commands don't save the last expression
68 evaluated, just its value. */
70 error (_("You must follow the `macro expand' command with the"
74 ms
= default_macro_scope ();
77 expanded
= macro_expand (exp
, standard_macro_lookup
, ms
);
78 fputs_filtered ("expands to: ", gdb_stdout
);
79 fputs_filtered (expanded
, gdb_stdout
);
80 fputs_filtered ("\n", gdb_stdout
);
83 macro_inform_no_debuginfo ();
85 do_cleanups (cleanup_chain
);
91 macro_expand_once_command (char *exp
, int from_tty
)
93 struct macro_scope
*ms
= NULL
;
94 char *expanded
= NULL
;
95 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
96 make_cleanup (free_current_contents
, &expanded
);
98 /* You know, when the user doesn't specify any expression, it would be
99 really cool if this defaulted to the last expression evaluated.
100 And it should set the once-expanded text as the new `last
101 expression'. That way, you could just hit return over and over and
102 see the expression expanded one level at a time. */
104 error (_("You must follow the `macro expand-once' command with"
106 "you want to expand."));
108 ms
= default_macro_scope ();
111 expanded
= macro_expand_once (exp
, standard_macro_lookup
, ms
);
112 fputs_filtered ("expands to: ", gdb_stdout
);
113 fputs_filtered (expanded
, gdb_stdout
);
114 fputs_filtered ("\n", gdb_stdout
);
117 macro_inform_no_debuginfo ();
119 do_cleanups (cleanup_chain
);
123 /* Outputs the include path of a macro starting at FILE and LINE to STREAM.
125 Care should be taken that this function does not cause any lookups into
126 the splay tree so that it can be safely used while iterating. */
128 show_pp_source_pos (struct ui_file
*stream
,
129 struct macro_source_file
*file
,
132 fprintf_filtered (stream
, "%s:%d\n", file
->filename
, line
);
134 while (file
->included_by
)
136 fprintf_filtered (gdb_stdout
, " included at %s:%d\n",
137 file
->included_by
->filename
,
138 file
->included_at_line
);
139 file
= file
->included_by
;
143 /* Outputs a macro for human consumption, detailing the include path
144 and macro definition. NAME is the name of the macro.
145 D the definition. FILE the start of the include path, and LINE the
148 Care should be taken that this function does not cause any lookups into
149 the splay tree so that it can be safely used while iterating. */
151 print_macro_definition (const char *name
,
152 const struct macro_definition
*d
,
153 struct macro_source_file
*file
,
156 fprintf_filtered (gdb_stdout
, "Defined at ");
157 show_pp_source_pos (gdb_stdout
, file
, line
);
160 fprintf_filtered (gdb_stdout
, "#define %s", name
);
162 fprintf_filtered (gdb_stdout
, "-D%s", name
);
164 if (d
->kind
== macro_function_like
)
168 fputs_filtered ("(", gdb_stdout
);
169 for (i
= 0; i
< d
->argc
; i
++)
171 fputs_filtered (d
->argv
[i
], gdb_stdout
);
173 fputs_filtered (", ", gdb_stdout
);
175 fputs_filtered (")", gdb_stdout
);
179 fprintf_filtered (gdb_stdout
, " %s\n", d
->replacement
);
181 fprintf_filtered (gdb_stdout
, "=%s\n", d
->replacement
);
184 /* A callback function for usage with macro_for_each and friends.
185 If USER_DATA is null all macros will be printed.
186 Otherwise USER_DATA is considered to be a string, printing
187 only macros who's NAME matches USER_DATA. Other arguments are
188 routed to print_macro_definition. */
190 print_macro_callback (const char *name
, const struct macro_definition
*macro
,
191 struct macro_source_file
*source
, int line
,
194 if (! user_data
|| strcmp (user_data
, name
) == 0)
195 print_macro_definition (name
, macro
, source
, line
);
198 /* The implementation of the `info macro' command. */
200 info_macro_command (char *args
, int from_tty
)
202 struct macro_scope
*ms
= NULL
;
203 struct cleanup
*cleanup_chain
;
205 int show_all_macros_named
= 0;
206 char *arg_start
= args
;
207 int processing_args
= 1;
209 while (processing_args
210 && arg_start
&& *arg_start
== '-' && *arg_start
!= '\0')
212 char *p
= skip_to_space (arg_start
);
214 if (strncmp (arg_start
, "-a", p
- arg_start
) == 0
215 || strncmp (arg_start
, "-all", p
- arg_start
) == 0)
216 show_all_macros_named
= 1;
217 else if (strncmp (arg_start
, "--", p
- arg_start
) == 0)
218 /* Our macro support seems rather C specific but this would
219 seem necessary for languages allowing - in macro names.
220 e.g. Scheme's (defmacro ->foo () "bar\n") */
224 /* Relies on modified 'args' not making it in to history */
226 error (_("Unrecognized option '%s' to info macro command. "
227 "Try \"help info macro\"."), arg_start
);
230 arg_start
= skip_spaces (p
);
235 if (! name
|| ! *name
)
236 error (_("You must follow the `info macro' command with the name"
238 "whose definition you want to see."));
240 ms
= default_macro_scope ();
241 cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
244 macro_inform_no_debuginfo ();
245 else if (show_all_macros_named
)
246 macro_for_each (ms
->file
->table
, print_macro_callback
, name
);
249 struct macro_definition
*d
;
251 d
= macro_lookup_definition (ms
->file
, ms
->line
, name
);
255 struct macro_source_file
*file
256 = macro_definition_location (ms
->file
, ms
->line
, name
, &line
);
258 print_macro_definition (name
, d
, file
, line
);
262 fprintf_filtered (gdb_stdout
,
263 "The symbol `%s' has no definition as a C/C++"
264 " preprocessor macro\n"
266 show_pp_source_pos (gdb_stdout
, ms
->file
, ms
->line
);
270 do_cleanups (cleanup_chain
);
273 /* Implementation of the "info macros" command. */
275 info_macros_command (char *args
, int from_tty
)
277 struct macro_scope
*ms
= NULL
;
278 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
281 ms
= default_macro_scope ();
284 struct symtabs_and_lines sals
=
285 decode_line_with_current_source (args
, 0);
288 ms
= sal_macro_scope (sals
.sals
[0]);
291 if (! ms
|| ! ms
->file
|| ! ms
->file
->table
)
292 macro_inform_no_debuginfo ();
294 macro_for_each_in_scope (ms
->file
, ms
->line
, print_macro_callback
, NULL
);
296 do_cleanups (cleanup_chain
);
300 /* User-defined macros. */
303 skip_ws (char **expp
)
305 while (macro_is_whitespace (**expp
))
309 /* Try to find the bounds of an identifier. If an identifier is
310 found, returns a newly allocated string; otherwise returns NULL.
311 EXPP is a pointer to an input string; it is updated to point to the
312 text following the identifier. If IS_PARAMETER is true, this
313 function will also allow "..." forms as used in varargs macro
317 extract_identifier (char **expp
, int is_parameter
)
323 if (is_parameter
&& !strncmp (p
, "...", 3))
329 if (! *p
|| ! macro_is_identifier_nondigit (*p
))
332 *p
&& (macro_is_identifier_nondigit (*p
) || macro_is_digit (*p
));
337 if (is_parameter
&& !strncmp (p
, "...", 3))
341 result
= (char *) xmalloc (len
+ 1);
342 memcpy (result
, *expp
, len
);
348 /* Helper function to clean up a temporarily-constructed macro object.
349 This assumes that the contents were all allocated with xmalloc. */
351 free_macro_definition_ptr (void *ptr
)
354 struct macro_definition
*loc
= (struct macro_definition
*) ptr
;
356 for (i
= 0; i
< loc
->argc
; ++i
)
357 xfree ((char *) loc
->argv
[i
]);
358 xfree ((char *) loc
->argv
);
359 /* Note that the 'replacement' field is not allocated. */
363 macro_define_command (char *exp
, int from_tty
)
365 struct macro_definition new_macro
;
367 struct cleanup
*cleanup_chain
;
370 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
372 cleanup_chain
= make_cleanup (free_macro_definition_ptr
, &new_macro
);
373 make_cleanup (free_current_contents
, &name
);
375 memset (&new_macro
, 0, sizeof (struct macro_definition
));
378 name
= extract_identifier (&exp
, 0);
380 error (_("Invalid macro name."));
383 /* Function-like macro. */
385 char **argv
= (char **) xmalloc (alloced
* sizeof (char *));
387 new_macro
.kind
= macro_function_like
;
389 new_macro
.argv
= (const char * const *) argv
;
391 /* Skip the '(' and whitespace. */
399 if (new_macro
.argc
== alloced
)
402 argv
= (char **) xrealloc (argv
, alloced
* sizeof (char *));
403 /* Must update new_macro as well... */
404 new_macro
.argv
= (const char * const *) argv
;
406 argv
[new_macro
.argc
] = extract_identifier (&exp
, 1);
407 if (! argv
[new_macro
.argc
])
408 error (_("Macro is missing an argument."));
411 for (i
= new_macro
.argc
- 2; i
>= 0; --i
)
413 if (! strcmp (argv
[i
], argv
[new_macro
.argc
- 1]))
414 error (_("Two macro arguments with identical names."));
423 else if (*exp
!= ')')
424 error (_("',' or ')' expected at end of macro arguments."));
426 /* Skip the closing paren. */
430 macro_define_function (macro_main (macro_user_macros
), -1, name
,
431 new_macro
.argc
, (const char **) new_macro
.argv
,
437 macro_define_object (macro_main (macro_user_macros
), -1, name
, exp
);
440 do_cleanups (cleanup_chain
);
445 macro_undef_command (char *exp
, int from_tty
)
450 error (_("usage: macro undef NAME"));
453 name
= extract_identifier (&exp
, 0);
455 error (_("Invalid macro name."));
456 macro_undef (macro_main (macro_user_macros
), -1, name
);
462 print_one_macro (const char *name
, const struct macro_definition
*macro
,
463 struct macro_source_file
*source
, int line
,
466 fprintf_filtered (gdb_stdout
, "macro define %s", name
);
467 if (macro
->kind
== macro_function_like
)
471 fprintf_filtered (gdb_stdout
, "(");
472 for (i
= 0; i
< macro
->argc
; ++i
)
473 fprintf_filtered (gdb_stdout
, "%s%s", (i
> 0) ? ", " : "",
475 fprintf_filtered (gdb_stdout
, ")");
477 fprintf_filtered (gdb_stdout
, " %s\n", macro
->replacement
);
482 macro_list_command (char *exp
, int from_tty
)
484 macro_for_each (macro_user_macros
, print_one_macro
, NULL
);
488 /* Initializing the `macrocmd' module. */
490 extern initialize_file_ftype _initialize_macrocmd
; /* -Wmissing-prototypes */
493 _initialize_macrocmd (void)
495 /* We introduce a new command prefix, `macro', under which we'll put
496 the various commands for working with preprocessor macros. */
497 add_prefix_cmd ("macro", class_info
, macro_command
,
498 _("Prefix for commands dealing with C preprocessor macros."),
499 ¯olist
, "macro ", 0, &cmdlist
);
501 add_cmd ("expand", no_class
, macro_expand_command
, _("\
502 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
503 Show the expanded expression."),
505 add_alias_cmd ("exp", "expand", no_class
, 1, ¯olist
);
506 add_cmd ("expand-once", no_class
, macro_expand_once_command
, _("\
507 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
508 Show the expanded expression.\n\
510 This command differs from `macro expand' in that it only expands macro\n\
511 invocations that appear directly in EXPRESSION; if expanding a macro\n\
512 introduces further macro invocations, those are left unexpanded.\n\
514 `macro expand-once' helps you see how a particular macro expands,\n\
515 whereas `macro expand' shows you how all the macros involved in an\n\
516 expression work together to yield a pre-processed expression."),
518 add_alias_cmd ("exp1", "expand-once", no_class
, 1, ¯olist
);
520 add_cmd ("macro", no_class
, info_macro_command
,
521 _("Show the definition of MACRO, and it's source location.\n\
522 Usage: info macro [-a|-all] [--] MACRO\n\
524 -a, --all Output all definitions of MACRO in the current compilation\
526 -- Specify the end of arguments and the beginning of the MACRO."),
530 add_cmd ("macros", no_class
, info_macros_command
,
531 _("Show the definitions of all macros at LINESPEC, or the current \
533 Usage: info macros [LINESPEC]"),
536 add_cmd ("define", no_class
, macro_define_command
, _("\
537 Define a new C/C++ preprocessor macro.\n\
538 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
539 preprocessor directive of the form `#define DEFINITION' such that the\n\
540 definition is visible in all the inferior's source files.\n\
542 (gdb) macro define PI (3.1415926)\n\
543 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
546 add_cmd ("undef", no_class
, macro_undef_command
, _("\
547 Remove the definition of the C/C++ preprocessor macro with the given name."),
550 add_cmd ("list", no_class
, macro_list_command
,
551 _("List all the macros defined using the `macro define' command."),