1 /* C preprocessor macro expansion commands for GDB.
2 Copyright (C) 2002, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "macroscope.h"
26 #include "cli/cli-utils.h"
29 #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 fputs_filtered ("GDB has no preprocessor macro information for "
58 macro_expand_command (char *exp
, int from_tty
)
60 struct macro_scope
*ms
= NULL
;
61 char *expanded
= NULL
;
62 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
64 make_cleanup (free_current_contents
, &expanded
);
66 /* You know, when the user doesn't specify any expression, it would be
67 really cool if this defaulted to the last expression evaluated.
68 Then it would be easy to ask, "Hey, what did I just evaluate?" But
69 at the moment, the `print' commands don't save the last expression
70 evaluated, just its value. */
72 error (_("You must follow the `macro expand' command with the"
76 ms
= default_macro_scope ();
79 expanded
= macro_expand (exp
, standard_macro_lookup
, ms
);
80 fputs_filtered ("expands to: ", gdb_stdout
);
81 fputs_filtered (expanded
, gdb_stdout
);
82 fputs_filtered ("\n", gdb_stdout
);
85 macro_inform_no_debuginfo ();
87 do_cleanups (cleanup_chain
);
93 macro_expand_once_command (char *exp
, int from_tty
)
95 struct macro_scope
*ms
= NULL
;
96 char *expanded
= NULL
;
97 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
98 make_cleanup (free_current_contents
, &expanded
);
100 /* You know, when the user doesn't specify any expression, it would be
101 really cool if this defaulted to the last expression evaluated.
102 And it should set the once-expanded text as the new `last
103 expression'. That way, you could just hit return over and over and
104 see the expression expanded one level at a time. */
106 error (_("You must follow the `macro expand-once' command with"
108 "you want to expand."));
110 ms
= default_macro_scope ();
113 expanded
= macro_expand_once (exp
, standard_macro_lookup
, ms
);
114 fputs_filtered ("expands to: ", gdb_stdout
);
115 fputs_filtered (expanded
, gdb_stdout
);
116 fputs_filtered ("\n", gdb_stdout
);
119 macro_inform_no_debuginfo ();
121 do_cleanups (cleanup_chain
);
125 /* Outputs the include path of a macro starting at FILE and LINE to STREAM.
127 Care should be taken that this function does not cause any lookups into
128 the splay tree so that it can be safely used while iterating. */
130 show_pp_source_pos (struct ui_file
*stream
,
131 struct macro_source_file
*file
,
134 fprintf_filtered (stream
, "%s:%d\n", file
->filename
, line
);
136 while (file
->included_by
)
138 fprintf_filtered (gdb_stdout
, " included at %s:%d\n",
139 file
->included_by
->filename
,
140 file
->included_at_line
);
141 file
= file
->included_by
;
145 /* Outputs a macro for human consumption, detailing the include path
146 and macro definition. NAME is the name of the macro.
147 D the definition. FILE the start of the include path, and LINE the
150 Care should be taken that this function does not cause any lookups into
151 the splay tree so that it can be safely used while iterating. */
153 print_macro_definition (const char *name
,
154 const struct macro_definition
*d
,
155 struct macro_source_file
*file
,
158 fprintf_filtered (gdb_stdout
, "Defined at ");
159 show_pp_source_pos (gdb_stdout
, file
, line
);
162 fprintf_filtered (gdb_stdout
, "#define %s", name
);
164 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
);
181 fprintf_filtered (gdb_stdout
, " %s\n", d
->replacement
);
183 fprintf_filtered (gdb_stdout
, "=%s\n", d
->replacement
);
186 /* A callback function for usage with macro_for_each and friends.
187 If USER_DATA is null all macros will be printed.
188 Otherwise USER_DATA is considered to be a string, printing
189 only macros who's NAME matches USER_DATA. Other arguments are
190 routed to print_macro_definition. */
192 print_macro_callback (const char *name
, const struct macro_definition
*macro
,
193 struct macro_source_file
*source
, int line
,
196 if (! user_data
|| strcmp (user_data
, name
) == 0)
197 print_macro_definition (name
, macro
, source
, line
);
200 /* The implementation of the `info macro' command. */
202 info_macro_command (char *args
, int from_tty
)
204 struct macro_scope
*ms
= NULL
;
205 struct cleanup
*cleanup_chain
;
207 int show_all_macros_named
= 0;
208 char *arg_start
= args
;
209 int processing_args
= 1;
211 while (processing_args
212 && arg_start
&& *arg_start
== '-' && *arg_start
!= '\0')
214 char *p
= skip_to_space (arg_start
);
216 if (strncmp (arg_start
, "-a", p
- arg_start
) == 0
217 || strncmp (arg_start
, "-all", p
- arg_start
) == 0)
218 show_all_macros_named
= 1;
219 else if (strncmp (arg_start
, "--", p
- arg_start
) == 0)
220 /* Our macro support seems rather C specific but this would
221 seem necessary for languages allowing - in macro names.
222 e.g. Scheme's (defmacro ->foo () "bar\n") */
226 /* Relies on modified 'args' not making it in to history */
228 error (_("Unrecognized option '%s' to info macro command. "
229 "Try \"help info macro\"."), arg_start
);
232 arg_start
= skip_spaces (p
);
237 if (! name
|| ! *name
)
238 error (_("You must follow the `info macro' command with the name"
240 "whose definition you want to see."));
242 ms
= default_macro_scope ();
243 cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
246 macro_inform_no_debuginfo ();
247 else if (show_all_macros_named
)
248 macro_for_each (ms
->file
->table
, print_macro_callback
, name
);
251 struct macro_definition
*d
;
253 d
= macro_lookup_definition (ms
->file
, ms
->line
, name
);
257 struct macro_source_file
*file
258 = macro_definition_location (ms
->file
, ms
->line
, name
, &line
);
260 print_macro_definition (name
, d
, file
, line
);
264 fprintf_filtered (gdb_stdout
,
265 "The symbol `%s' has no definition as a C/C++"
266 " preprocessor macro\n"
268 show_pp_source_pos (gdb_stdout
, ms
->file
, ms
->line
);
272 do_cleanups (cleanup_chain
);
275 /* Implementation of the "info macros" command. */
277 info_macros_command (char *args
, int from_tty
)
279 struct macro_scope
*ms
= NULL
;
280 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
283 ms
= default_macro_scope ();
286 struct symtabs_and_lines sals
= decode_line_spec (args
, 0);
289 ms
= sal_macro_scope (sals
.sals
[0]);
292 if (! ms
|| ! ms
->file
|| ! ms
->file
->table
)
293 macro_inform_no_debuginfo ();
295 macro_for_each_in_scope (ms
->file
, ms
->line
, print_macro_callback
, NULL
);
297 do_cleanups (cleanup_chain
);
301 /* User-defined macros. */
304 skip_ws (char **expp
)
306 while (macro_is_whitespace (**expp
))
310 /* Try to find the bounds of an identifier. If an identifier is
311 found, returns a newly allocated string; otherwise returns NULL.
312 EXPP is a pointer to an input string; it is updated to point to the
313 text following the identifier. If IS_PARAMETER is true, this
314 function will also allow "..." forms as used in varargs macro
318 extract_identifier (char **expp
, int is_parameter
)
324 if (is_parameter
&& !strncmp (p
, "...", 3))
330 if (! *p
|| ! macro_is_identifier_nondigit (*p
))
333 *p
&& (macro_is_identifier_nondigit (*p
) || macro_is_digit (*p
));
338 if (is_parameter
&& !strncmp (p
, "...", 3))
342 result
= (char *) xmalloc (len
+ 1);
343 memcpy (result
, *expp
, len
);
349 /* Helper function to clean up a temporarily-constructed macro object.
350 This assumes that the contents were all allocated with xmalloc. */
352 free_macro_definition_ptr (void *ptr
)
355 struct macro_definition
*loc
= (struct macro_definition
*) ptr
;
357 for (i
= 0; i
< loc
->argc
; ++i
)
358 xfree ((char *) loc
->argv
[i
]);
359 xfree ((char *) loc
->argv
);
360 /* Note that the 'replacement' field is not allocated. */
364 macro_define_command (char *exp
, int from_tty
)
366 struct macro_definition new_macro
;
368 struct cleanup
*cleanup_chain
;
371 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
373 cleanup_chain
= make_cleanup (free_macro_definition_ptr
, &new_macro
);
374 make_cleanup (free_current_contents
, &name
);
376 memset (&new_macro
, 0, sizeof (struct macro_definition
));
379 name
= extract_identifier (&exp
, 0);
381 error (_("Invalid macro name."));
384 /* Function-like macro. */
386 char **argv
= (char **) xmalloc (alloced
* sizeof (char *));
388 new_macro
.kind
= macro_function_like
;
390 new_macro
.argv
= (const char * const *) argv
;
392 /* Skip the '(' and whitespace. */
400 if (new_macro
.argc
== alloced
)
403 argv
= (char **) xrealloc (argv
, alloced
* sizeof (char *));
404 /* Must update new_macro as well... */
405 new_macro
.argv
= (const char * const *) argv
;
407 argv
[new_macro
.argc
] = extract_identifier (&exp
, 1);
408 if (! argv
[new_macro
.argc
])
409 error (_("Macro is missing an argument."));
412 for (i
= new_macro
.argc
- 2; i
>= 0; --i
)
414 if (! strcmp (argv
[i
], argv
[new_macro
.argc
- 1]))
415 error (_("Two macro arguments with identical names."));
424 else if (*exp
!= ')')
425 error (_("',' or ')' expected at end of macro arguments."));
427 /* Skip the closing paren. */
431 macro_define_function (macro_main (macro_user_macros
), -1, name
,
432 new_macro
.argc
, (const char **) new_macro
.argv
,
438 macro_define_object (macro_main (macro_user_macros
), -1, name
, exp
);
441 do_cleanups (cleanup_chain
);
446 macro_undef_command (char *exp
, int from_tty
)
451 error (_("usage: macro undef NAME"));
454 name
= extract_identifier (&exp
, 0);
456 error (_("Invalid macro name."));
457 macro_undef (macro_main (macro_user_macros
), -1, name
);
463 print_one_macro (const char *name
, const struct macro_definition
*macro
,
464 struct macro_source_file
*source
, int line
,
467 fprintf_filtered (gdb_stdout
, "macro define %s", name
);
468 if (macro
->kind
== macro_function_like
)
472 fprintf_filtered (gdb_stdout
, "(");
473 for (i
= 0; i
< macro
->argc
; ++i
)
474 fprintf_filtered (gdb_stdout
, "%s%s", (i
> 0) ? ", " : "",
476 fprintf_filtered (gdb_stdout
, ")");
478 fprintf_filtered (gdb_stdout
, " %s\n", macro
->replacement
);
483 macro_list_command (char *exp
, int from_tty
)
485 macro_for_each (macro_user_macros
, print_one_macro
, NULL
);
489 /* Initializing the `macrocmd' module. */
491 extern initialize_file_ftype _initialize_macrocmd
; /* -Wmissing-prototypes */
494 _initialize_macrocmd (void)
496 /* We introduce a new command prefix, `macro', under which we'll put
497 the various commands for working with preprocessor macros. */
498 add_prefix_cmd ("macro", class_info
, macro_command
,
499 _("Prefix for commands dealing with C preprocessor macros."),
500 ¯olist
, "macro ", 0, &cmdlist
);
502 add_cmd ("expand", no_class
, macro_expand_command
, _("\
503 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
504 Show the expanded expression."),
506 add_alias_cmd ("exp", "expand", no_class
, 1, ¯olist
);
507 add_cmd ("expand-once", no_class
, macro_expand_once_command
, _("\
508 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
509 Show the expanded expression.\n\
511 This command differs from `macro expand' in that it only expands macro\n\
512 invocations that appear directly in EXPRESSION; if expanding a macro\n\
513 introduces further macro invocations, those are left unexpanded.\n\
515 `macro expand-once' helps you see how a particular macro expands,\n\
516 whereas `macro expand' shows you how all the macros involved in an\n\
517 expression work together to yield a pre-processed expression."),
519 add_alias_cmd ("exp1", "expand-once", no_class
, 1, ¯olist
);
521 add_cmd ("macro", no_class
, info_macro_command
,
522 _("Show the definition of MACRO, and it's source location.\n\
523 Usage: info macro [-a|-all] [--] MACRO\n\
525 -a, --all Output all definitions of MACRO in the current compilation\
527 -- Specify the end of arguments and the beginning of the MACRO."),
531 add_cmd ("macros", no_class
, info_macros_command
,
532 _("Show the definitions of all macros at LINESPEC, or the current \
534 Usage: info macros [LINESPEC]"),
537 add_cmd ("define", no_class
, macro_define_command
, _("\
538 Define a new C/C++ preprocessor macro.\n\
539 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
540 preprocessor directive of the form `#define DEFINITION' such that the\n\
541 definition is visible in all the inferior's source files.\n\
543 (gdb) macro define PI (3.1415926)\n\
544 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
547 add_cmd ("undef", no_class
, macro_undef_command
, _("\
548 Remove the definition of the C/C++ preprocessor macro with the given name."),
551 add_cmd ("list", no_class
, macro_list_command
,
552 _("List all the macros defined using the `macro define' command."),