Move info definitions command to an info macro option.
[deliverable/binutils-gdb.git] / gdb / macrocmd.c
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.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 #include "defs.h"
23 #include "macrotab.h"
24 #include "macroexp.h"
25 #include "macroscope.h"
26 #include "cli/cli-utils.h"
27 #include "command.h"
28 #include "gdbcmd.h"
29 #include "gdb_string.h"
30
31 \f
32 /* The `macro' prefix command. */
33
34 static struct cmd_list_element *macrolist;
35
36 static void
37 macro_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
49 /* Prints an informational message regarding the lack of macro information. */
50 static void macro_inform_no_debuginfo()
51 {
52 fputs_filtered ("GDB has no preprocessor macro information for "
53 "that code.",
54 gdb_stdout);
55 }
56
57 static void
58 macro_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);
63
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)
72 error (_("You must follow the `macro expand' command with the"
73 " expression you\n"
74 "want to expand."));
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
85 macro_inform_no_debuginfo ();
86
87 do_cleanups (cleanup_chain);
88 return;
89 }
90
91
92 static void
93 macro_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)
106 error (_("You must follow the `macro expand-once' command with"
107 " the expression\n"
108 "you want to expand."));
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
119 macro_inform_no_debuginfo ();
120
121 do_cleanups (cleanup_chain);
122 return;
123 }
124
125 /* Outputs the include path of a macro starting at FILE and LINE to STREAM.
126
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. */
129 static void
130 show_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
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.
149
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. */
152 static void
153 print_macro_definition (const char *name,
154 const struct macro_definition *d,
155 struct macro_source_file *file,
156 int line)
157 {
158 fprintf_filtered (gdb_stdout, "Defined at ");
159 show_pp_source_pos (gdb_stdout, file, line);
160
161 if (line != 0)
162 fprintf_filtered (gdb_stdout, "#define %s", name);
163 else
164 fprintf_filtered (gdb_stdout, "-D%s", name);
165
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 }
179
180 if (line != 0)
181 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
182 else
183 fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
184 }
185
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. */
191 static void
192 print_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
200 /* The implementation of the `info macro' command. */
201 static void
202 info_macro_command (char *args, int from_tty)
203 {
204 struct macro_scope *ms = NULL;
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;
236
237 if (! name || ! *name)
238 error (_("You must follow the `info macro' command with the name"
239 " of the macro\n"
240 "whose definition you want to see."));
241
242 ms = default_macro_scope ();
243 cleanup_chain = make_cleanup (free_current_contents, &ms);
244
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 }
271
272 do_cleanups (cleanup_chain);
273 }
274
275 /* Implementation of the "info macros" command. */
276 static void
277 info_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)
293 macro_inform_no_debuginfo ();
294 else
295 macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);
296
297 do_cleanups (cleanup_chain);
298 }
299
300 \f
301 /* User-defined macros. */
302
303 static void
304 skip_ws (char **expp)
305 {
306 while (macro_is_whitespace (**expp))
307 ++*expp;
308 }
309
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
317 static char *
318 extract_identifier (char **expp, int is_parameter)
319 {
320 char *result;
321 char *p = *expp;
322 unsigned int len;
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
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. */
351 static void
352 free_macro_definition_ptr (void *ptr)
353 {
354 int i;
355 struct macro_definition *loc = (struct macro_definition *) ptr;
356
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 }
362
363 static void
364 macro_define_command (char *exp, int from_tty)
365 {
366 struct macro_definition new_macro;
367 char *name = NULL;
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);
374 make_cleanup (free_current_contents, &name);
375
376 memset (&new_macro, 0, sizeof (struct macro_definition));
377
378 skip_ws (&exp);
379 name = extract_identifier (&exp, 0);
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 *));
404 /* Must update new_macro as well... */
405 new_macro.argv = (const char * const *) argv;
406 }
407 argv[new_macro.argc] = extract_identifier (&exp, 1);
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;
429 skip_ws (&exp);
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
436 {
437 skip_ws (&exp);
438 macro_define_object (macro_main (macro_user_macros), -1, name, exp);
439 }
440
441 do_cleanups (cleanup_chain);
442 }
443
444
445 static void
446 macro_undef_command (char *exp, int from_tty)
447 {
448 char *name;
449
450 if (!exp)
451 error (_("usage: macro undef NAME"));
452
453 skip_ws (&exp);
454 name = extract_identifier (&exp, 0);
455 if (! name)
456 error (_("Invalid macro name."));
457 macro_undef (macro_main (macro_user_macros), -1, name);
458 xfree (name);
459 }
460
461
462 static void
463 print_one_macro (const char *name, const struct macro_definition *macro,
464 struct macro_source_file *source, int line,
465 void *ignore)
466 {
467 fprintf_filtered (gdb_stdout, "macro define %s", name);
468 if (macro->kind == macro_function_like)
469 {
470 int i;
471
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 }
478 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
479 }
480
481
482 static void
483 macro_list_command (char *exp, int from_tty)
484 {
485 macro_for_each (macro_user_macros, print_one_macro, NULL);
486 }
487
488 \f
489 /* Initializing the `macrocmd' module. */
490
491 extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
492
493 void
494 _initialize_macrocmd (void)
495 {
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 &macrolist, "macro ", 0, &cmdlist);
501
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."),
505 &macrolist);
506 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
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\
510 \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\
514 \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."),
518 &macrolist);
519 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
520
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\
524 Options: \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
529 &infolist);
530
531 add_cmd ("macros", no_class, info_macros_command,
532 _("Show the definitions of all macros at LINESPEC, or the current \
533 source location.\n\
534 Usage: info macros [LINESPEC]"),
535 &infolist);
536
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\
542 For 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, _("\
548 Remove 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);
554 }
This page took 0.068933 seconds and 5 git commands to generate.