Update years in copyright notice for the GDB files.
[deliverable/binutils-gdb.git] / gdb / macrocmd.c
1 /* C preprocessor macro expansion commands for GDB.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20
21 #include "defs.h"
22 #include "macrotab.h"
23 #include "macroexp.h"
24 #include "macroscope.h"
25 #include "cli/cli-utils.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "gdb_string.h"
29 #include "linespec.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 =
287 decode_line_with_current_source (args, 0);
288
289 if (sals.nelts)
290 ms = sal_macro_scope (sals.sals[0]);
291 }
292
293 if (! ms || ! ms->file || ! ms->file->table)
294 macro_inform_no_debuginfo ();
295 else
296 macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);
297
298 do_cleanups (cleanup_chain);
299 }
300
301 \f
302 /* User-defined macros. */
303
304 static void
305 skip_ws (char **expp)
306 {
307 while (macro_is_whitespace (**expp))
308 ++*expp;
309 }
310
311 /* Try to find the bounds of an identifier. If an identifier is
312 found, returns a newly allocated string; otherwise returns NULL.
313 EXPP is a pointer to an input string; it is updated to point to the
314 text following the identifier. If IS_PARAMETER is true, this
315 function will also allow "..." forms as used in varargs macro
316 parameters. */
317
318 static char *
319 extract_identifier (char **expp, int is_parameter)
320 {
321 char *result;
322 char *p = *expp;
323 unsigned int len;
324
325 if (is_parameter && !strncmp (p, "...", 3))
326 {
327 /* Ok. */
328 }
329 else
330 {
331 if (! *p || ! macro_is_identifier_nondigit (*p))
332 return NULL;
333 for (++p;
334 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
335 ++p)
336 ;
337 }
338
339 if (is_parameter && !strncmp (p, "...", 3))
340 p += 3;
341
342 len = p - *expp;
343 result = (char *) xmalloc (len + 1);
344 memcpy (result, *expp, len);
345 result[len] = '\0';
346 *expp += len;
347 return result;
348 }
349
350 /* Helper function to clean up a temporarily-constructed macro object.
351 This assumes that the contents were all allocated with xmalloc. */
352 static void
353 free_macro_definition_ptr (void *ptr)
354 {
355 int i;
356 struct macro_definition *loc = (struct macro_definition *) ptr;
357
358 for (i = 0; i < loc->argc; ++i)
359 xfree ((char *) loc->argv[i]);
360 xfree ((char *) loc->argv);
361 /* Note that the 'replacement' field is not allocated. */
362 }
363
364 static void
365 macro_define_command (char *exp, int from_tty)
366 {
367 struct macro_definition new_macro;
368 char *name = NULL;
369 struct cleanup *cleanup_chain;
370
371 if (!exp)
372 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
373
374 cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
375 make_cleanup (free_current_contents, &name);
376
377 memset (&new_macro, 0, sizeof (struct macro_definition));
378
379 skip_ws (&exp);
380 name = extract_identifier (&exp, 0);
381 if (! name)
382 error (_("Invalid macro name."));
383 if (*exp == '(')
384 {
385 /* Function-like macro. */
386 int alloced = 5;
387 char **argv = (char **) xmalloc (alloced * sizeof (char *));
388
389 new_macro.kind = macro_function_like;
390 new_macro.argc = 0;
391 new_macro.argv = (const char * const *) argv;
392
393 /* Skip the '(' and whitespace. */
394 ++exp;
395 skip_ws (&exp);
396
397 while (*exp != ')')
398 {
399 int i;
400
401 if (new_macro.argc == alloced)
402 {
403 alloced *= 2;
404 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
405 /* Must update new_macro as well... */
406 new_macro.argv = (const char * const *) argv;
407 }
408 argv[new_macro.argc] = extract_identifier (&exp, 1);
409 if (! argv[new_macro.argc])
410 error (_("Macro is missing an argument."));
411 ++new_macro.argc;
412
413 for (i = new_macro.argc - 2; i >= 0; --i)
414 {
415 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
416 error (_("Two macro arguments with identical names."));
417 }
418
419 skip_ws (&exp);
420 if (*exp == ',')
421 {
422 ++exp;
423 skip_ws (&exp);
424 }
425 else if (*exp != ')')
426 error (_("',' or ')' expected at end of macro arguments."));
427 }
428 /* Skip the closing paren. */
429 ++exp;
430 skip_ws (&exp);
431
432 macro_define_function (macro_main (macro_user_macros), -1, name,
433 new_macro.argc, (const char **) new_macro.argv,
434 exp);
435 }
436 else
437 {
438 skip_ws (&exp);
439 macro_define_object (macro_main (macro_user_macros), -1, name, exp);
440 }
441
442 do_cleanups (cleanup_chain);
443 }
444
445
446 static void
447 macro_undef_command (char *exp, int from_tty)
448 {
449 char *name;
450
451 if (!exp)
452 error (_("usage: macro undef NAME"));
453
454 skip_ws (&exp);
455 name = extract_identifier (&exp, 0);
456 if (! name)
457 error (_("Invalid macro name."));
458 macro_undef (macro_main (macro_user_macros), -1, name);
459 xfree (name);
460 }
461
462
463 static void
464 print_one_macro (const char *name, const struct macro_definition *macro,
465 struct macro_source_file *source, int line,
466 void *ignore)
467 {
468 fprintf_filtered (gdb_stdout, "macro define %s", name);
469 if (macro->kind == macro_function_like)
470 {
471 int i;
472
473 fprintf_filtered (gdb_stdout, "(");
474 for (i = 0; i < macro->argc; ++i)
475 fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
476 macro->argv[i]);
477 fprintf_filtered (gdb_stdout, ")");
478 }
479 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
480 }
481
482
483 static void
484 macro_list_command (char *exp, int from_tty)
485 {
486 macro_for_each (macro_user_macros, print_one_macro, NULL);
487 }
488
489 \f
490 /* Initializing the `macrocmd' module. */
491
492 extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
493
494 void
495 _initialize_macrocmd (void)
496 {
497 /* We introduce a new command prefix, `macro', under which we'll put
498 the various commands for working with preprocessor macros. */
499 add_prefix_cmd ("macro", class_info, macro_command,
500 _("Prefix for commands dealing with C preprocessor macros."),
501 &macrolist, "macro ", 0, &cmdlist);
502
503 add_cmd ("expand", no_class, macro_expand_command, _("\
504 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
505 Show the expanded expression."),
506 &macrolist);
507 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
508 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
509 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
510 Show the expanded expression.\n\
511 \n\
512 This command differs from `macro expand' in that it only expands macro\n\
513 invocations that appear directly in EXPRESSION; if expanding a macro\n\
514 introduces further macro invocations, those are left unexpanded.\n\
515 \n\
516 `macro expand-once' helps you see how a particular macro expands,\n\
517 whereas `macro expand' shows you how all the macros involved in an\n\
518 expression work together to yield a pre-processed expression."),
519 &macrolist);
520 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
521
522 add_cmd ("macro", no_class, info_macro_command,
523 _("Show the definition of MACRO, and it's source location.\n\
524 Usage: info macro [-a|-all] [--] MACRO\n\
525 Options: \n\
526 -a, --all Output all definitions of MACRO in the current compilation\
527 unit.\n\
528 -- Specify the end of arguments and the beginning of the MACRO."),
529
530 &infolist);
531
532 add_cmd ("macros", no_class, info_macros_command,
533 _("Show the definitions of all macros at LINESPEC, or the current \
534 source location.\n\
535 Usage: info macros [LINESPEC]"),
536 &infolist);
537
538 add_cmd ("define", no_class, macro_define_command, _("\
539 Define a new C/C++ preprocessor macro.\n\
540 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
541 preprocessor directive of the form `#define DEFINITION' such that the\n\
542 definition is visible in all the inferior's source files.\n\
543 For example:\n\
544 (gdb) macro define PI (3.1415926)\n\
545 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
546 &macrolist);
547
548 add_cmd ("undef", no_class, macro_undef_command, _("\
549 Remove the definition of the C/C++ preprocessor macro with the given name."),
550 &macrolist);
551
552 add_cmd ("list", no_class, macro_list_command,
553 _("List all the macros defined using the `macro define' command."),
554 &macrolist);
555 }
This page took 0.041319 seconds and 5 git commands to generate.