use gnulib's update-copyright script to update copyright years
[deliverable/binutils-gdb.git] / gdb / macrocmd.c
CommitLineData
6821892e 1/* C preprocessor macro expansion commands for GDB.
7b6bb8da
JB
2 Copyright (C) 2002, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
6821892e
JB
4 Contributed by Red Hat, Inc.
5
6 This file is part of GDB.
7
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
6821892e
JB
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6821892e
JB
20
21
22#include "defs.h"
23#include "macrotab.h"
24#include "macroexp.h"
25#include "macroscope.h"
71eba9c2 26#include "cli/cli-utils.h"
6821892e
JB
27#include "command.h"
28#include "gdbcmd.h"
d7d9f01e 29#include "gdb_string.h"
6821892e
JB
30
31\f
32/* The `macro' prefix command. */
33
34static struct cmd_list_element *macrolist;
35
36static void
37macro_command (char *arg, int from_tty)
38{
39 printf_unfiltered
40 ("\"macro\" must be followed by the name of a macro command.\n");
41 help_list (macrolist, "macro ", -1, gdb_stdout);
42}
43
44
45\f
46/* Macro expansion commands. */
47
48
71eba9c2 49/* Prints an informational message regarding the lack of macro information. */
50static void macro_inform_no_debuginfo()
51{
52 fputs_filtered ("GDB has no preprocessor macro information for "
53 "that code.",
54 gdb_stdout);
55}
56
6821892e
JB
57static void
58macro_expand_command (char *exp, int from_tty)
59{
60 struct macro_scope *ms = NULL;
61 char *expanded = NULL;
62 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
b8d56208 63
6821892e
JB
64 make_cleanup (free_current_contents, &expanded);
65
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. */
71 if (! exp || ! *exp)
8a3fe4f8 72 error (_("You must follow the `macro expand' command with the"
6821892e 73 " expression you\n"
8a3fe4f8 74 "want to expand."));
6821892e
JB
75
76 ms = default_macro_scope ();
77 if (ms)
78 {
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);
83 }
84 else
71eba9c2 85 macro_inform_no_debuginfo ();
6821892e
JB
86
87 do_cleanups (cleanup_chain);
88 return;
89}
90
91
92static void
93macro_expand_once_command (char *exp, int from_tty)
94{
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);
99
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. */
105 if (! exp || ! *exp)
8a3fe4f8 106 error (_("You must follow the `macro expand-once' command with"
6821892e 107 " the expression\n"
8a3fe4f8 108 "you want to expand."));
6821892e
JB
109
110 ms = default_macro_scope ();
111 if (ms)
112 {
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);
117 }
118 else
71eba9c2 119 macro_inform_no_debuginfo ();
6821892e
JB
120
121 do_cleanups (cleanup_chain);
122 return;
123}
124
9b158ba0 125/* Outputs the include path of a macro starting at FILE and LINE to STREAM.
6821892e 126
9b158ba0 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. */
6821892e
JB
129static void
130show_pp_source_pos (struct ui_file *stream,
131 struct macro_source_file *file,
132 int line)
133{
134 fprintf_filtered (stream, "%s:%d\n", file->filename, line);
135
136 while (file->included_by)
137 {
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;
142 }
143}
144
9b158ba0 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
148 line number in FILE.
6821892e 149
9b158ba0 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. */
6821892e 152static void
9b158ba0 153print_macro_definition (const char *name,
154 const struct macro_definition *d,
155 struct macro_source_file *file,
156 int line)
6821892e 157{
6821892e
JB
158 fprintf_filtered (gdb_stdout, "Defined at ");
159 show_pp_source_pos (gdb_stdout, file, line);
9b158ba0 160
484086b7
JK
161 if (line != 0)
162 fprintf_filtered (gdb_stdout, "#define %s", name);
163 else
164 fprintf_filtered (gdb_stdout, "-D%s", name);
9b158ba0 165
6821892e
JB
166 if (d->kind == macro_function_like)
167 {
168 int i;
169
170 fputs_filtered ("(", gdb_stdout);
171 for (i = 0; i < d->argc; i++)
172 {
173 fputs_filtered (d->argv[i], gdb_stdout);
174 if (i + 1 < d->argc)
175 fputs_filtered (", ", gdb_stdout);
176 }
177 fputs_filtered (")", gdb_stdout);
178 }
9b158ba0 179
484086b7
JK
180 if (line != 0)
181 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
182 else
183 fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
9b158ba0 184}
185
9b158ba0 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. */
191static void
192print_macro_callback (const char *name, const struct macro_definition *macro,
193 struct macro_source_file *source, int line,
194 void *user_data)
195{
196 if (! user_data || strcmp (user_data, name) == 0)
197 print_macro_definition (name, macro, source, line);
198}
199
71eba9c2 200/* The implementation of the `info macro' command. */
9b158ba0 201static void
71eba9c2 202info_macro_command (char *args, int from_tty)
9b158ba0 203{
204 struct macro_scope *ms = NULL;
71eba9c2 205 struct cleanup *cleanup_chain;
206 char *name;
207 int show_all_macros_named = 0;
208 char *arg_start = args;
209 int processing_args = 1;
210
211 while (processing_args
212 && arg_start && *arg_start == '-' && *arg_start != '\0')
213 {
214 char *p = skip_to_space (arg_start);
215
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") */
223 processing_args = 0;
224 else
225 {
226 /* Relies on modified 'args' not making it in to history */
227 *p = '\0';
228 error (_("Unrecognized option '%s' to info macro command. "
229 "Try \"help info macro\"."), arg_start);
230 }
231
232 arg_start = skip_spaces (p);
233 }
234
235 name = arg_start;
9b158ba0 236
237 if (! name || ! *name)
71eba9c2 238 error (_("You must follow the `info macro' command with the name"
239 " of the macro\n"
240 "whose definition you want to see."));
9b158ba0 241
242 ms = default_macro_scope ();
71eba9c2 243 cleanup_chain = make_cleanup (free_current_contents, &ms);
9b158ba0 244
71eba9c2 245 if (! ms)
246 macro_inform_no_debuginfo ();
247 else if (show_all_macros_named)
248 macro_for_each (ms->file->table, print_macro_callback, name);
249 else
250 {
251 struct macro_definition *d;
252
253 d = macro_lookup_definition (ms->file, ms->line, name);
254 if (d)
255 {
256 int line;
257 struct macro_source_file *file
258 = macro_definition_location (ms->file, ms->line, name, &line);
259
260 print_macro_definition (name, d, file, line);
261 }
262 else
263 {
264 fprintf_filtered (gdb_stdout,
265 "The symbol `%s' has no definition as a C/C++"
266 " preprocessor macro\n"
267 "at ", name);
268 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
269 }
270 }
9b158ba0 271
9b158ba0 272 do_cleanups (cleanup_chain);
273}
274
275/* Implementation of the "info macros" command. */
276static void
277info_macros_command (char *args, int from_tty)
278{
279 struct macro_scope *ms = NULL;
280 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
281
282 if (args == NULL)
283 ms = default_macro_scope ();
284 else
285 {
286 struct symtabs_and_lines sals = decode_line_spec (args, 0);
287
288 if (sals.nelts)
289 ms = sal_macro_scope (sals.sals[0]);
290 }
291
292 if (! ms || ! ms->file || ! ms->file->table)
71eba9c2 293 macro_inform_no_debuginfo ();
294 else
295 macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);
9b158ba0 296
9b158ba0 297 do_cleanups (cleanup_chain);
298}
6821892e
JB
299
300\f
301/* User-defined macros. */
302
d7d9f01e
TT
303static void
304skip_ws (char **expp)
305{
306 while (macro_is_whitespace (**expp))
307 ++*expp;
308}
309
2fae03e8
TT
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
315 parameters. */
316
d7d9f01e 317static char *
2fae03e8 318extract_identifier (char **expp, int is_parameter)
d7d9f01e
TT
319{
320 char *result;
321 char *p = *expp;
322 unsigned int len;
2fae03e8
TT
323
324 if (is_parameter && !strncmp (p, "...", 3))
325 {
326 /* Ok. */
327 }
328 else
329 {
330 if (! *p || ! macro_is_identifier_nondigit (*p))
331 return NULL;
332 for (++p;
333 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
334 ++p)
335 ;
336 }
337
338 if (is_parameter && !strncmp (p, "...", 3))
339 p += 3;
340
d7d9f01e
TT
341 len = p - *expp;
342 result = (char *) xmalloc (len + 1);
343 memcpy (result, *expp, len);
344 result[len] = '\0';
345 *expp += len;
346 return result;
347}
348
349/* Helper function to clean up a temporarily-constructed macro object.
350 This assumes that the contents were all allocated with xmalloc. */
351static void
352free_macro_definition_ptr (void *ptr)
353{
354 int i;
355 struct macro_definition *loc = (struct macro_definition *) ptr;
b8d56208 356
d7d9f01e
TT
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. */
361}
6821892e
JB
362
363static void
364macro_define_command (char *exp, int from_tty)
365{
d7d9f01e
TT
366 struct macro_definition new_macro;
367 char *name = NULL;
886a217c
TT
368 struct cleanup *cleanup_chain;
369
370 if (!exp)
371 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
372
373 cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
d7d9f01e
TT
374 make_cleanup (free_current_contents, &name);
375
376 memset (&new_macro, 0, sizeof (struct macro_definition));
377
378 skip_ws (&exp);
2fae03e8 379 name = extract_identifier (&exp, 0);
d7d9f01e
TT
380 if (! name)
381 error (_("Invalid macro name."));
382 if (*exp == '(')
383 {
384 /* Function-like macro. */
385 int alloced = 5;
386 char **argv = (char **) xmalloc (alloced * sizeof (char *));
387
388 new_macro.kind = macro_function_like;
389 new_macro.argc = 0;
390 new_macro.argv = (const char * const *) argv;
391
392 /* Skip the '(' and whitespace. */
393 ++exp;
394 skip_ws (&exp);
395
396 while (*exp != ')')
397 {
398 int i;
399
400 if (new_macro.argc == alloced)
401 {
402 alloced *= 2;
403 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
025bb325 404 /* Must update new_macro as well... */
d7d9f01e
TT
405 new_macro.argv = (const char * const *) argv;
406 }
2fae03e8 407 argv[new_macro.argc] = extract_identifier (&exp, 1);
d7d9f01e
TT
408 if (! argv[new_macro.argc])
409 error (_("Macro is missing an argument."));
410 ++new_macro.argc;
411
412 for (i = new_macro.argc - 2; i >= 0; --i)
413 {
414 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
415 error (_("Two macro arguments with identical names."));
416 }
417
418 skip_ws (&exp);
419 if (*exp == ',')
420 {
421 ++exp;
422 skip_ws (&exp);
423 }
424 else if (*exp != ')')
425 error (_("',' or ')' expected at end of macro arguments."));
426 }
427 /* Skip the closing paren. */
428 ++exp;
cc704ebe 429 skip_ws (&exp);
d7d9f01e
TT
430
431 macro_define_function (macro_main (macro_user_macros), -1, name,
432 new_macro.argc, (const char **) new_macro.argv,
433 exp);
434 }
435 else
cc704ebe
TT
436 {
437 skip_ws (&exp);
438 macro_define_object (macro_main (macro_user_macros), -1, name, exp);
439 }
d7d9f01e
TT
440
441 do_cleanups (cleanup_chain);
6821892e
JB
442}
443
444
445static void
446macro_undef_command (char *exp, int from_tty)
447{
d7d9f01e 448 char *name;
886a217c
TT
449
450 if (!exp)
451 error (_("usage: macro undef NAME"));
452
d7d9f01e 453 skip_ws (&exp);
2fae03e8 454 name = extract_identifier (&exp, 0);
d7d9f01e
TT
455 if (! name)
456 error (_("Invalid macro name."));
457 macro_undef (macro_main (macro_user_macros), -1, name);
458 xfree (name);
459}
460
461
462static void
9a044a89 463print_one_macro (const char *name, const struct macro_definition *macro,
9b158ba0 464 struct macro_source_file *source, int line,
9a044a89 465 void *ignore)
d7d9f01e
TT
466{
467 fprintf_filtered (gdb_stdout, "macro define %s", name);
468 if (macro->kind == macro_function_like)
469 {
470 int i;
b8d56208 471
d7d9f01e
TT
472 fprintf_filtered (gdb_stdout, "(");
473 for (i = 0; i < macro->argc; ++i)
474 fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
475 macro->argv[i]);
476 fprintf_filtered (gdb_stdout, ")");
477 }
cc704ebe 478 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
6821892e
JB
479}
480
481
482static void
483macro_list_command (char *exp, int from_tty)
484{
9a044a89 485 macro_for_each (macro_user_macros, print_one_macro, NULL);
6821892e
JB
486}
487
6821892e
JB
488\f
489/* Initializing the `macrocmd' module. */
490
a78f21af 491extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
b9362cc7 492
6821892e
JB
493void
494_initialize_macrocmd (void)
495{
6821892e
JB
496 /* We introduce a new command prefix, `macro', under which we'll put
497 the various commands for working with preprocessor macros. */
1bedd215
AC
498 add_prefix_cmd ("macro", class_info, macro_command,
499 _("Prefix for commands dealing with C preprocessor macros."),
500 &macrolist, "macro ", 0, &cmdlist);
6821892e 501
1a966eab
AC
502 add_cmd ("expand", no_class, macro_expand_command, _("\
503Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
504Show the expanded expression."),
505 &macrolist);
6821892e 506 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
1a966eab
AC
507 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
508Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
509Show the expanded expression.\n\
510\n\
511This command differs from `macro expand' in that it only expands macro\n\
512invocations that appear directly in EXPRESSION; if expanding a macro\n\
513introduces further macro invocations, those are left unexpanded.\n\
514\n\
515`macro expand-once' helps you see how a particular macro expands,\n\
516whereas `macro expand' shows you how all the macros involved in an\n\
517expression work together to yield a pre-processed expression."),
518 &macrolist);
6821892e
JB
519 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
520
1a966eab 521 add_cmd ("macro", no_class, info_macro_command,
71eba9c2 522 _("Show the definition of MACRO, and it's source location.\n\
523Usage: info macro [-a|-all] [--] MACRO\n\
524Options: \n\
525 -a, --all Output all definitions of MACRO in the current compilation\
526 unit.\n\
527 -- Specify the end of arguments and the beginning of the MACRO."),
528
1a966eab
AC
529 &infolist);
530
9b158ba0 531 add_cmd ("macros", no_class, info_macros_command,
532 _("Show the definitions of all macros at LINESPEC, or the current \
533source location.\n\
534Usage: info macros [LINESPEC]"),
535 &infolist);
536
1a966eab
AC
537 add_cmd ("define", no_class, macro_define_command, _("\
538Define a new C/C++ preprocessor macro.\n\
539The GDB command `macro define DEFINITION' is equivalent to placing a\n\
540preprocessor directive of the form `#define DEFINITION' such that the\n\
541definition is visible in all the inferior's source files.\n\
542For example:\n\
543 (gdb) macro define PI (3.1415926)\n\
544 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
545 &macrolist);
546
547 add_cmd ("undef", no_class, macro_undef_command, _("\
548Remove the definition of the C/C++ preprocessor macro with the given name."),
549 &macrolist);
550
551 add_cmd ("list", no_class, macro_list_command,
552 _("List all the macros defined using the `macro define' command."),
553 &macrolist);
6821892e 554}
This page took 1.126937 seconds and 4 git commands to generate.