1 /* C preprocessor macro expansion commands for GDB.
2 Copyright (C) 2002, 2007, 2008 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
);
53 make_cleanup (free_current_contents
, &expanded
);
55 /* You know, when the user doesn't specify any expression, it would be
56 really cool if this defaulted to the last expression evaluated.
57 Then it would be easy to ask, "Hey, what did I just evaluate?" But
58 at the moment, the `print' commands don't save the last expression
59 evaluated, just its value. */
61 error (_("You must follow the `macro expand' command with the"
65 ms
= default_macro_scope ();
68 expanded
= macro_expand (exp
, standard_macro_lookup
, ms
);
69 fputs_filtered ("expands to: ", gdb_stdout
);
70 fputs_filtered (expanded
, gdb_stdout
);
71 fputs_filtered ("\n", gdb_stdout
);
74 fputs_filtered ("GDB has no preprocessor macro information for "
78 do_cleanups (cleanup_chain
);
84 macro_expand_once_command (char *exp
, int from_tty
)
86 struct macro_scope
*ms
= NULL
;
87 char *expanded
= NULL
;
88 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
89 make_cleanup (free_current_contents
, &expanded
);
91 /* You know, when the user doesn't specify any expression, it would be
92 really cool if this defaulted to the last expression evaluated.
93 And it should set the once-expanded text as the new `last
94 expression'. That way, you could just hit return over and over and
95 see the expression expanded one level at a time. */
97 error (_("You must follow the `macro expand-once' command with"
99 "you want to expand."));
101 ms
= default_macro_scope ();
104 expanded
= macro_expand_once (exp
, standard_macro_lookup
, ms
);
105 fputs_filtered ("expands to: ", gdb_stdout
);
106 fputs_filtered (expanded
, gdb_stdout
);
107 fputs_filtered ("\n", gdb_stdout
);
110 fputs_filtered ("GDB has no preprocessor macro information for "
114 do_cleanups (cleanup_chain
);
120 show_pp_source_pos (struct ui_file
*stream
,
121 struct macro_source_file
*file
,
124 fprintf_filtered (stream
, "%s:%d\n", file
->filename
, line
);
126 while (file
->included_by
)
128 fprintf_filtered (gdb_stdout
, " included at %s:%d\n",
129 file
->included_by
->filename
,
130 file
->included_at_line
);
131 file
= file
->included_by
;
137 info_macro_command (char *name
, int from_tty
)
139 struct macro_scope
*ms
= NULL
;
140 struct cleanup
*cleanup_chain
= make_cleanup (free_current_contents
, &ms
);
141 struct macro_definition
*d
;
143 if (! name
|| ! *name
)
144 error (_("You must follow the `info macro' command with the name"
146 "whose definition you want to see."));
148 ms
= default_macro_scope ();
150 error (_("GDB has no preprocessor macro information for that code."));
152 d
= macro_lookup_definition (ms
->file
, ms
->line
, name
);
156 struct macro_source_file
*file
157 = macro_definition_location (ms
->file
, ms
->line
, name
, &line
);
159 fprintf_filtered (gdb_stdout
, "Defined at ");
160 show_pp_source_pos (gdb_stdout
, file
, line
);
161 fprintf_filtered (gdb_stdout
, "#define %s", name
);
162 if (d
->kind
== macro_function_like
)
166 fputs_filtered ("(", gdb_stdout
);
167 for (i
= 0; i
< d
->argc
; i
++)
169 fputs_filtered (d
->argv
[i
], gdb_stdout
);
171 fputs_filtered (", ", gdb_stdout
);
173 fputs_filtered (")", gdb_stdout
);
175 fprintf_filtered (gdb_stdout
, " %s\n", d
->replacement
);
179 fprintf_filtered (gdb_stdout
,
180 "The symbol `%s' has no definition as a C/C++"
181 " preprocessor macro\n"
183 show_pp_source_pos (gdb_stdout
, ms
->file
, ms
->line
);
186 do_cleanups (cleanup_chain
);
191 /* User-defined macros. */
194 skip_ws (char **expp
)
196 while (macro_is_whitespace (**expp
))
201 extract_identifier (char **expp
)
206 if (! *p
|| ! macro_is_identifier_nondigit (*p
))
209 *p
&& (macro_is_identifier_nondigit (*p
) || macro_is_digit (*p
));
213 result
= (char *) xmalloc (len
+ 1);
214 memcpy (result
, *expp
, len
);
220 /* Helper function to clean up a temporarily-constructed macro object.
221 This assumes that the contents were all allocated with xmalloc. */
223 free_macro_definition_ptr (void *ptr
)
226 struct macro_definition
*loc
= (struct macro_definition
*) ptr
;
227 for (i
= 0; i
< loc
->argc
; ++i
)
228 xfree ((char *) loc
->argv
[i
]);
229 xfree ((char *) loc
->argv
);
230 /* Note that the 'replacement' field is not allocated. */
234 macro_define_command (char *exp
, int from_tty
)
236 struct macro_definition new_macro
;
238 struct cleanup
*cleanup_chain
= make_cleanup (free_macro_definition_ptr
,
240 make_cleanup (free_current_contents
, &name
);
242 memset (&new_macro
, 0, sizeof (struct macro_definition
));
245 name
= extract_identifier (&exp
);
247 error (_("Invalid macro name."));
250 /* Function-like macro. */
252 char **argv
= (char **) xmalloc (alloced
* sizeof (char *));
254 new_macro
.kind
= macro_function_like
;
256 new_macro
.argv
= (const char * const *) argv
;
258 /* Skip the '(' and whitespace. */
266 if (new_macro
.argc
== alloced
)
269 argv
= (char **) xrealloc (argv
, alloced
* sizeof (char *));
270 /* Must update new_macro as well... */
271 new_macro
.argv
= (const char * const *) argv
;
273 argv
[new_macro
.argc
] = extract_identifier (&exp
);
274 if (! argv
[new_macro
.argc
])
275 error (_("Macro is missing an argument."));
278 for (i
= new_macro
.argc
- 2; i
>= 0; --i
)
280 if (! strcmp (argv
[i
], argv
[new_macro
.argc
- 1]))
281 error (_("Two macro arguments with identical names."));
290 else if (*exp
!= ')')
291 error (_("',' or ')' expected at end of macro arguments."));
293 /* Skip the closing paren. */
296 macro_define_function (macro_main (macro_user_macros
), -1, name
,
297 new_macro
.argc
, (const char **) new_macro
.argv
,
301 macro_define_object (macro_main (macro_user_macros
), -1, name
, exp
);
303 do_cleanups (cleanup_chain
);
308 macro_undef_command (char *exp
, int from_tty
)
312 name
= extract_identifier (&exp
);
314 error (_("Invalid macro name."));
315 macro_undef (macro_main (macro_user_macros
), -1, name
);
321 print_one_macro (const char *name
, const struct macro_definition
*macro
)
323 fprintf_filtered (gdb_stdout
, "macro define %s", name
);
324 if (macro
->kind
== macro_function_like
)
327 fprintf_filtered (gdb_stdout
, "(");
328 for (i
= 0; i
< macro
->argc
; ++i
)
329 fprintf_filtered (gdb_stdout
, "%s%s", (i
> 0) ? ", " : "",
331 fprintf_filtered (gdb_stdout
, ")");
333 /* Note that we don't need a leading space here -- "macro define"
335 fprintf_filtered (gdb_stdout
, "%s\n", macro
->replacement
);
340 macro_list_command (char *exp
, int from_tty
)
342 macro_for_each (macro_user_macros
, print_one_macro
);
347 /* Initializing the `macrocmd' module. */
349 extern initialize_file_ftype _initialize_macrocmd
; /* -Wmissing-prototypes */
352 _initialize_macrocmd (void)
354 struct cmd_list_element
*c
;
356 /* We introduce a new command prefix, `macro', under which we'll put
357 the various commands for working with preprocessor macros. */
358 add_prefix_cmd ("macro", class_info
, macro_command
,
359 _("Prefix for commands dealing with C preprocessor macros."),
360 ¯olist
, "macro ", 0, &cmdlist
);
362 add_cmd ("expand", no_class
, macro_expand_command
, _("\
363 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
364 Show the expanded expression."),
366 add_alias_cmd ("exp", "expand", no_class
, 1, ¯olist
);
367 add_cmd ("expand-once", no_class
, macro_expand_once_command
, _("\
368 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
369 Show the expanded expression.\n\
371 This command differs from `macro expand' in that it only expands macro\n\
372 invocations that appear directly in EXPRESSION; if expanding a macro\n\
373 introduces further macro invocations, those are left unexpanded.\n\
375 `macro expand-once' helps you see how a particular macro expands,\n\
376 whereas `macro expand' shows you how all the macros involved in an\n\
377 expression work together to yield a pre-processed expression."),
379 add_alias_cmd ("exp1", "expand-once", no_class
, 1, ¯olist
);
381 add_cmd ("macro", no_class
, info_macro_command
,
382 _("Show the definition of MACRO, and its source location."),
385 add_cmd ("define", no_class
, macro_define_command
, _("\
386 Define a new C/C++ preprocessor macro.\n\
387 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
388 preprocessor directive of the form `#define DEFINITION' such that the\n\
389 definition is visible in all the inferior's source files.\n\
391 (gdb) macro define PI (3.1415926)\n\
392 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
395 add_cmd ("undef", no_class
, macro_undef_command
, _("\
396 Remove the definition of the C/C++ preprocessor macro with the given name."),
399 add_cmd ("list", no_class
, macro_list_command
,
400 _("List all the macros defined using the `macro define' command."),