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"
28 #include "gdb_string.h"
31 /* The `macro' prefix command. */
33 static struct cmd_list_element
*macrolist
;
36 macro_command (char *arg
, int from_tty
)
39 ("\"macro\" must be followed by the name of a macro command.\n");
40 help_list (macrolist
, "macro ", -1, gdb_stdout
);
45 /* Macro expansion commands. */
49 macro_expand_command (char *exp
, int from_tty
)
51 struct macro_scope
*ms
= NULL
;
52 char *expanded
= NULL
;
53 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
55 make_cleanup (free_current_contents
, &expanded
);
57 /* You know, when the user doesn't specify any expression, it would be
58 really cool if this defaulted to the last expression evaluated.
59 Then it would be easy to ask, "Hey, what did I just evaluate?" But
60 at the moment, the `print' commands don't save the last expression
61 evaluated, just its value. */
63 error (_("You must follow the `macro expand' command with the"
67 ms
= default_macro_scope ();
70 expanded
= macro_expand (exp
, standard_macro_lookup
, ms
);
71 fputs_filtered ("expands to: ", gdb_stdout
);
72 fputs_filtered (expanded
, gdb_stdout
);
73 fputs_filtered ("\n", gdb_stdout
);
76 fputs_filtered ("GDB has no preprocessor macro information for "
80 do_cleanups (cleanup_chain
);
86 macro_expand_once_command (char *exp
, int from_tty
)
88 struct macro_scope
*ms
= NULL
;
89 char *expanded
= NULL
;
90 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
91 make_cleanup (free_current_contents
, &expanded
);
93 /* You know, when the user doesn't specify any expression, it would be
94 really cool if this defaulted to the last expression evaluated.
95 And it should set the once-expanded text as the new `last
96 expression'. That way, you could just hit return over and over and
97 see the expression expanded one level at a time. */
99 error (_("You must follow the `macro expand-once' command with"
101 "you want to expand."));
103 ms
= default_macro_scope ();
106 expanded
= macro_expand_once (exp
, standard_macro_lookup
, ms
);
107 fputs_filtered ("expands to: ", gdb_stdout
);
108 fputs_filtered (expanded
, gdb_stdout
);
109 fputs_filtered ("\n", gdb_stdout
);
112 fputs_filtered ("GDB has no preprocessor macro information for "
116 do_cleanups (cleanup_chain
);
122 show_pp_source_pos (struct ui_file
*stream
,
123 struct macro_source_file
*file
,
126 fprintf_filtered (stream
, "%s:%d\n", file
->filename
, line
);
128 while (file
->included_by
)
130 fprintf_filtered (gdb_stdout
, " included at %s:%d\n",
131 file
->included_by
->filename
,
132 file
->included_at_line
);
133 file
= file
->included_by
;
139 info_macro_command (char *name
, int from_tty
)
141 struct macro_scope
*ms
= NULL
;
142 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
143 struct macro_definition
*d
;
145 if (! name
|| ! *name
)
146 error (_("You must follow the `info macro' command with the name"
148 "whose definition you want to see."));
150 ms
= default_macro_scope ();
152 error (_("GDB has no preprocessor macro information for that code."));
154 d
= macro_lookup_definition (ms
->file
, ms
->line
, name
);
158 struct macro_source_file
*file
159 = macro_definition_location (ms
->file
, ms
->line
, name
, &line
);
161 fprintf_filtered (gdb_stdout
, "Defined at ");
162 show_pp_source_pos (gdb_stdout
, file
, line
);
164 fprintf_filtered (gdb_stdout
, "#define %s", name
);
166 fprintf_filtered (gdb_stdout
, "-D%s", name
);
167 if (d
->kind
== macro_function_like
)
171 fputs_filtered ("(", gdb_stdout
);
172 for (i
= 0; i
< d
->argc
; i
++)
174 fputs_filtered (d
->argv
[i
], gdb_stdout
);
176 fputs_filtered (", ", gdb_stdout
);
178 fputs_filtered (")", gdb_stdout
);
181 fprintf_filtered (gdb_stdout
, " %s\n", d
->replacement
);
183 fprintf_filtered (gdb_stdout
, "=%s\n", d
->replacement
);
187 fprintf_filtered (gdb_stdout
,
188 "The symbol `%s' has no definition as a C/C++"
189 " preprocessor macro\n"
191 show_pp_source_pos (gdb_stdout
, ms
->file
, ms
->line
);
194 do_cleanups (cleanup_chain
);
199 /* User-defined macros. */
202 skip_ws (char **expp
)
204 while (macro_is_whitespace (**expp
))
208 /* Try to find the bounds of an identifier. If an identifier is
209 found, returns a newly allocated string; otherwise returns NULL.
210 EXPP is a pointer to an input string; it is updated to point to the
211 text following the identifier. If IS_PARAMETER is true, this
212 function will also allow "..." forms as used in varargs macro
216 extract_identifier (char **expp
, int is_parameter
)
222 if (is_parameter
&& !strncmp (p
, "...", 3))
228 if (! *p
|| ! macro_is_identifier_nondigit (*p
))
231 *p
&& (macro_is_identifier_nondigit (*p
) || macro_is_digit (*p
));
236 if (is_parameter
&& !strncmp (p
, "...", 3))
240 result
= (char *) xmalloc (len
+ 1);
241 memcpy (result
, *expp
, len
);
247 /* Helper function to clean up a temporarily-constructed macro object.
248 This assumes that the contents were all allocated with xmalloc. */
250 free_macro_definition_ptr (void *ptr
)
253 struct macro_definition
*loc
= (struct macro_definition
*) ptr
;
255 for (i
= 0; i
< loc
->argc
; ++i
)
256 xfree ((char *) loc
->argv
[i
]);
257 xfree ((char *) loc
->argv
);
258 /* Note that the 'replacement' field is not allocated. */
262 macro_define_command (char *exp
, int from_tty
)
264 struct macro_definition new_macro
;
266 struct cleanup
*cleanup_chain
;
269 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
271 cleanup_chain
= make_cleanup (free_macro_definition_ptr
, &new_macro
);
272 make_cleanup (free_current_contents
, &name
);
274 memset (&new_macro
, 0, sizeof (struct macro_definition
));
277 name
= extract_identifier (&exp
, 0);
279 error (_("Invalid macro name."));
282 /* Function-like macro. */
284 char **argv
= (char **) xmalloc (alloced
* sizeof (char *));
286 new_macro
.kind
= macro_function_like
;
288 new_macro
.argv
= (const char * const *) argv
;
290 /* Skip the '(' and whitespace. */
298 if (new_macro
.argc
== alloced
)
301 argv
= (char **) xrealloc (argv
, alloced
* sizeof (char *));
302 /* Must update new_macro as well... */
303 new_macro
.argv
= (const char * const *) argv
;
305 argv
[new_macro
.argc
] = extract_identifier (&exp
, 1);
306 if (! argv
[new_macro
.argc
])
307 error (_("Macro is missing an argument."));
310 for (i
= new_macro
.argc
- 2; i
>= 0; --i
)
312 if (! strcmp (argv
[i
], argv
[new_macro
.argc
- 1]))
313 error (_("Two macro arguments with identical names."));
322 else if (*exp
!= ')')
323 error (_("',' or ')' expected at end of macro arguments."));
325 /* Skip the closing paren. */
329 macro_define_function (macro_main (macro_user_macros
), -1, name
,
330 new_macro
.argc
, (const char **) new_macro
.argv
,
336 macro_define_object (macro_main (macro_user_macros
), -1, name
, exp
);
339 do_cleanups (cleanup_chain
);
344 macro_undef_command (char *exp
, int from_tty
)
349 error (_("usage: macro undef NAME"));
352 name
= extract_identifier (&exp
, 0);
354 error (_("Invalid macro name."));
355 macro_undef (macro_main (macro_user_macros
), -1, name
);
361 print_one_macro (const char *name
, const struct macro_definition
*macro
,
364 fprintf_filtered (gdb_stdout
, "macro define %s", name
);
365 if (macro
->kind
== macro_function_like
)
369 fprintf_filtered (gdb_stdout
, "(");
370 for (i
= 0; i
< macro
->argc
; ++i
)
371 fprintf_filtered (gdb_stdout
, "%s%s", (i
> 0) ? ", " : "",
373 fprintf_filtered (gdb_stdout
, ")");
375 fprintf_filtered (gdb_stdout
, " %s\n", macro
->replacement
);
380 macro_list_command (char *exp
, int from_tty
)
382 macro_for_each (macro_user_macros
, print_one_macro
, NULL
);
387 /* Initializing the `macrocmd' module. */
389 extern initialize_file_ftype _initialize_macrocmd
; /* -Wmissing-prototypes */
392 _initialize_macrocmd (void)
394 /* We introduce a new command prefix, `macro', under which we'll put
395 the various commands for working with preprocessor macros. */
396 add_prefix_cmd ("macro", class_info
, macro_command
,
397 _("Prefix for commands dealing with C preprocessor macros."),
398 ¯olist
, "macro ", 0, &cmdlist
);
400 add_cmd ("expand", no_class
, macro_expand_command
, _("\
401 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
402 Show the expanded expression."),
404 add_alias_cmd ("exp", "expand", no_class
, 1, ¯olist
);
405 add_cmd ("expand-once", no_class
, macro_expand_once_command
, _("\
406 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
407 Show the expanded expression.\n\
409 This command differs from `macro expand' in that it only expands macro\n\
410 invocations that appear directly in EXPRESSION; if expanding a macro\n\
411 introduces further macro invocations, those are left unexpanded.\n\
413 `macro expand-once' helps you see how a particular macro expands,\n\
414 whereas `macro expand' shows you how all the macros involved in an\n\
415 expression work together to yield a pre-processed expression."),
417 add_alias_cmd ("exp1", "expand-once", no_class
, 1, ¯olist
);
419 add_cmd ("macro", no_class
, info_macro_command
,
420 _("Show the definition of MACRO, and its source location."),
423 add_cmd ("define", no_class
, macro_define_command
, _("\
424 Define a new C/C++ preprocessor macro.\n\
425 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
426 preprocessor directive of the form `#define DEFINITION' such that the\n\
427 definition is visible in all the inferior's source files.\n\
429 (gdb) macro define PI (3.1415926)\n\
430 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
433 add_cmd ("undef", no_class
, macro_undef_command
, _("\
434 Remove the definition of the C/C++ preprocessor macro with the given name."),
437 add_cmd ("list", no_class
, macro_list_command
,
438 _("List all the macros defined using the `macro define' command."),