add -Wold-style-declaration
[deliverable/binutils-gdb.git] / gdb / macrocmd.c
CommitLineData
6821892e 1/* C preprocessor macro expansion commands for GDB.
28e7fd62 2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
6821892e
JB
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
6821892e
JB
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6821892e
JB
19
20
21#include "defs.h"
22#include "macrotab.h"
23#include "macroexp.h"
24#include "macroscope.h"
71eba9c2 25#include "cli/cli-utils.h"
6821892e
JB
26#include "command.h"
27#include "gdbcmd.h"
d7d9f01e 28#include "gdb_string.h"
39cf75f7 29#include "linespec.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{
daefa854 52 puts_filtered ("GDB has no preprocessor macro information for that code.\n");
71eba9c2 53}
54
6821892e
JB
55static void
56macro_expand_command (char *exp, int from_tty)
57{
58 struct macro_scope *ms = NULL;
59 char *expanded = NULL;
60 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
b8d56208 61
6821892e
JB
62 make_cleanup (free_current_contents, &expanded);
63
64 /* You know, when the user doesn't specify any expression, it would be
65 really cool if this defaulted to the last expression evaluated.
66 Then it would be easy to ask, "Hey, what did I just evaluate?" But
67 at the moment, the `print' commands don't save the last expression
68 evaluated, just its value. */
69 if (! exp || ! *exp)
8a3fe4f8 70 error (_("You must follow the `macro expand' command with the"
6821892e 71 " expression you\n"
8a3fe4f8 72 "want to expand."));
6821892e
JB
73
74 ms = default_macro_scope ();
75 if (ms)
76 {
77 expanded = macro_expand (exp, standard_macro_lookup, ms);
78 fputs_filtered ("expands to: ", gdb_stdout);
79 fputs_filtered (expanded, gdb_stdout);
80 fputs_filtered ("\n", gdb_stdout);
81 }
82 else
71eba9c2 83 macro_inform_no_debuginfo ();
6821892e
JB
84
85 do_cleanups (cleanup_chain);
86 return;
87}
88
89
90static void
91macro_expand_once_command (char *exp, int from_tty)
92{
93 struct macro_scope *ms = NULL;
94 char *expanded = NULL;
95 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
96 make_cleanup (free_current_contents, &expanded);
97
98 /* You know, when the user doesn't specify any expression, it would be
99 really cool if this defaulted to the last expression evaluated.
100 And it should set the once-expanded text as the new `last
101 expression'. That way, you could just hit return over and over and
102 see the expression expanded one level at a time. */
103 if (! exp || ! *exp)
8a3fe4f8 104 error (_("You must follow the `macro expand-once' command with"
6821892e 105 " the expression\n"
8a3fe4f8 106 "you want to expand."));
6821892e
JB
107
108 ms = default_macro_scope ();
109 if (ms)
110 {
111 expanded = macro_expand_once (exp, standard_macro_lookup, ms);
112 fputs_filtered ("expands to: ", gdb_stdout);
113 fputs_filtered (expanded, gdb_stdout);
114 fputs_filtered ("\n", gdb_stdout);
115 }
116 else
71eba9c2 117 macro_inform_no_debuginfo ();
6821892e
JB
118
119 do_cleanups (cleanup_chain);
120 return;
121}
122
9b158ba0 123/* Outputs the include path of a macro starting at FILE and LINE to STREAM.
6821892e 124
9b158ba0 125 Care should be taken that this function does not cause any lookups into
126 the splay tree so that it can be safely used while iterating. */
6821892e
JB
127static void
128show_pp_source_pos (struct ui_file *stream,
129 struct macro_source_file *file,
130 int line)
131{
233d95b5
JK
132 char *fullname;
133
134 fullname = macro_source_fullname (file);
135 fprintf_filtered (stream, "%s:%d\n", fullname, line);
136 xfree (fullname);
6821892e
JB
137
138 while (file->included_by)
139 {
233d95b5
JK
140 fullname = macro_source_fullname (file->included_by);
141 fprintf_filtered (gdb_stdout, " included at %s:%d\n", fullname,
6821892e 142 file->included_at_line);
233d95b5 143 xfree (fullname);
6821892e
JB
144 file = file->included_by;
145 }
146}
147
9b158ba0 148/* Outputs a macro for human consumption, detailing the include path
149 and macro definition. NAME is the name of the macro.
150 D the definition. FILE the start of the include path, and LINE the
151 line number in FILE.
6821892e 152
9b158ba0 153 Care should be taken that this function does not cause any lookups into
154 the splay tree so that it can be safely used while iterating. */
6821892e 155static void
9b158ba0 156print_macro_definition (const char *name,
157 const struct macro_definition *d,
158 struct macro_source_file *file,
159 int line)
6821892e 160{
6821892e
JB
161 fprintf_filtered (gdb_stdout, "Defined at ");
162 show_pp_source_pos (gdb_stdout, file, line);
9b158ba0 163
484086b7
JK
164 if (line != 0)
165 fprintf_filtered (gdb_stdout, "#define %s", name);
166 else
167 fprintf_filtered (gdb_stdout, "-D%s", name);
9b158ba0 168
6821892e
JB
169 if (d->kind == macro_function_like)
170 {
171 int i;
172
173 fputs_filtered ("(", gdb_stdout);
174 for (i = 0; i < d->argc; i++)
175 {
176 fputs_filtered (d->argv[i], gdb_stdout);
177 if (i + 1 < d->argc)
178 fputs_filtered (", ", gdb_stdout);
179 }
180 fputs_filtered (")", gdb_stdout);
181 }
9b158ba0 182
484086b7
JK
183 if (line != 0)
184 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
185 else
186 fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
9b158ba0 187}
188
9b158ba0 189/* A callback function for usage with macro_for_each and friends.
190 If USER_DATA is null all macros will be printed.
191 Otherwise USER_DATA is considered to be a string, printing
192 only macros who's NAME matches USER_DATA. Other arguments are
193 routed to print_macro_definition. */
194static void
195print_macro_callback (const char *name, const struct macro_definition *macro,
196 struct macro_source_file *source, int line,
197 void *user_data)
198{
199 if (! user_data || strcmp (user_data, name) == 0)
200 print_macro_definition (name, macro, source, line);
201}
202
71eba9c2 203/* The implementation of the `info macro' command. */
9b158ba0 204static void
71eba9c2 205info_macro_command (char *args, int from_tty)
9b158ba0 206{
207 struct macro_scope *ms = NULL;
71eba9c2 208 struct cleanup *cleanup_chain;
209 char *name;
210 int show_all_macros_named = 0;
211 char *arg_start = args;
212 int processing_args = 1;
213
214 while (processing_args
215 && arg_start && *arg_start == '-' && *arg_start != '\0')
216 {
217 char *p = skip_to_space (arg_start);
218
219 if (strncmp (arg_start, "-a", p - arg_start) == 0
220 || strncmp (arg_start, "-all", p - arg_start) == 0)
221 show_all_macros_named = 1;
222 else if (strncmp (arg_start, "--", p - arg_start) == 0)
223 /* Our macro support seems rather C specific but this would
224 seem necessary for languages allowing - in macro names.
225 e.g. Scheme's (defmacro ->foo () "bar\n") */
226 processing_args = 0;
227 else
228 {
229 /* Relies on modified 'args' not making it in to history */
230 *p = '\0';
231 error (_("Unrecognized option '%s' to info macro command. "
232 "Try \"help info macro\"."), arg_start);
233 }
234
235 arg_start = skip_spaces (p);
236 }
237
238 name = arg_start;
9b158ba0 239
240 if (! name || ! *name)
71eba9c2 241 error (_("You must follow the `info macro' command with the name"
242 " of the macro\n"
243 "whose definition you want to see."));
9b158ba0 244
245 ms = default_macro_scope ();
71eba9c2 246 cleanup_chain = make_cleanup (free_current_contents, &ms);
9b158ba0 247
71eba9c2 248 if (! ms)
249 macro_inform_no_debuginfo ();
250 else if (show_all_macros_named)
251 macro_for_each (ms->file->table, print_macro_callback, name);
252 else
253 {
254 struct macro_definition *d;
255
256 d = macro_lookup_definition (ms->file, ms->line, name);
257 if (d)
258 {
259 int line;
260 struct macro_source_file *file
261 = macro_definition_location (ms->file, ms->line, name, &line);
262
263 print_macro_definition (name, d, file, line);
264 }
265 else
266 {
267 fprintf_filtered (gdb_stdout,
268 "The symbol `%s' has no definition as a C/C++"
269 " preprocessor macro\n"
270 "at ", name);
271 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
272 }
273 }
9b158ba0 274
9b158ba0 275 do_cleanups (cleanup_chain);
276}
277
278/* Implementation of the "info macros" command. */
279static void
280info_macros_command (char *args, int from_tty)
281{
282 struct macro_scope *ms = NULL;
283 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
284
285 if (args == NULL)
286 ms = default_macro_scope ();
287 else
288 {
39cf75f7
DE
289 struct symtabs_and_lines sals =
290 decode_line_with_current_source (args, 0);
9b158ba0 291
292 if (sals.nelts)
293 ms = sal_macro_scope (sals.sals[0]);
294 }
295
296 if (! ms || ! ms->file || ! ms->file->table)
71eba9c2 297 macro_inform_no_debuginfo ();
298 else
299 macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);
9b158ba0 300
9b158ba0 301 do_cleanups (cleanup_chain);
302}
6821892e
JB
303
304\f
305/* User-defined macros. */
306
d7d9f01e
TT
307static void
308skip_ws (char **expp)
309{
310 while (macro_is_whitespace (**expp))
311 ++*expp;
312}
313
2fae03e8
TT
314/* Try to find the bounds of an identifier. If an identifier is
315 found, returns a newly allocated string; otherwise returns NULL.
316 EXPP is a pointer to an input string; it is updated to point to the
317 text following the identifier. If IS_PARAMETER is true, this
318 function will also allow "..." forms as used in varargs macro
319 parameters. */
320
d7d9f01e 321static char *
2fae03e8 322extract_identifier (char **expp, int is_parameter)
d7d9f01e
TT
323{
324 char *result;
325 char *p = *expp;
326 unsigned int len;
2fae03e8
TT
327
328 if (is_parameter && !strncmp (p, "...", 3))
329 {
330 /* Ok. */
331 }
332 else
333 {
334 if (! *p || ! macro_is_identifier_nondigit (*p))
335 return NULL;
336 for (++p;
337 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
338 ++p)
339 ;
340 }
341
342 if (is_parameter && !strncmp (p, "...", 3))
343 p += 3;
344
d7d9f01e
TT
345 len = p - *expp;
346 result = (char *) xmalloc (len + 1);
347 memcpy (result, *expp, len);
348 result[len] = '\0';
349 *expp += len;
350 return result;
351}
352
353/* Helper function to clean up a temporarily-constructed macro object.
354 This assumes that the contents were all allocated with xmalloc. */
355static void
356free_macro_definition_ptr (void *ptr)
357{
358 int i;
359 struct macro_definition *loc = (struct macro_definition *) ptr;
b8d56208 360
d7d9f01e
TT
361 for (i = 0; i < loc->argc; ++i)
362 xfree ((char *) loc->argv[i]);
363 xfree ((char *) loc->argv);
364 /* Note that the 'replacement' field is not allocated. */
365}
6821892e
JB
366
367static void
368macro_define_command (char *exp, int from_tty)
369{
d7d9f01e
TT
370 struct macro_definition new_macro;
371 char *name = NULL;
886a217c
TT
372 struct cleanup *cleanup_chain;
373
374 if (!exp)
375 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
376
377 cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
d7d9f01e
TT
378 make_cleanup (free_current_contents, &name);
379
380 memset (&new_macro, 0, sizeof (struct macro_definition));
381
382 skip_ws (&exp);
2fae03e8 383 name = extract_identifier (&exp, 0);
d7d9f01e
TT
384 if (! name)
385 error (_("Invalid macro name."));
386 if (*exp == '(')
387 {
388 /* Function-like macro. */
389 int alloced = 5;
390 char **argv = (char **) xmalloc (alloced * sizeof (char *));
391
392 new_macro.kind = macro_function_like;
393 new_macro.argc = 0;
394 new_macro.argv = (const char * const *) argv;
395
396 /* Skip the '(' and whitespace. */
397 ++exp;
398 skip_ws (&exp);
399
400 while (*exp != ')')
401 {
402 int i;
403
404 if (new_macro.argc == alloced)
405 {
406 alloced *= 2;
407 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
025bb325 408 /* Must update new_macro as well... */
d7d9f01e
TT
409 new_macro.argv = (const char * const *) argv;
410 }
2fae03e8 411 argv[new_macro.argc] = extract_identifier (&exp, 1);
d7d9f01e
TT
412 if (! argv[new_macro.argc])
413 error (_("Macro is missing an argument."));
414 ++new_macro.argc;
415
416 for (i = new_macro.argc - 2; i >= 0; --i)
417 {
418 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
419 error (_("Two macro arguments with identical names."));
420 }
421
422 skip_ws (&exp);
423 if (*exp == ',')
424 {
425 ++exp;
426 skip_ws (&exp);
427 }
428 else if (*exp != ')')
429 error (_("',' or ')' expected at end of macro arguments."));
430 }
431 /* Skip the closing paren. */
432 ++exp;
cc704ebe 433 skip_ws (&exp);
d7d9f01e
TT
434
435 macro_define_function (macro_main (macro_user_macros), -1, name,
436 new_macro.argc, (const char **) new_macro.argv,
437 exp);
438 }
439 else
cc704ebe
TT
440 {
441 skip_ws (&exp);
442 macro_define_object (macro_main (macro_user_macros), -1, name, exp);
443 }
d7d9f01e
TT
444
445 do_cleanups (cleanup_chain);
6821892e
JB
446}
447
448
449static void
450macro_undef_command (char *exp, int from_tty)
451{
d7d9f01e 452 char *name;
886a217c
TT
453
454 if (!exp)
455 error (_("usage: macro undef NAME"));
456
d7d9f01e 457 skip_ws (&exp);
2fae03e8 458 name = extract_identifier (&exp, 0);
d7d9f01e
TT
459 if (! name)
460 error (_("Invalid macro name."));
461 macro_undef (macro_main (macro_user_macros), -1, name);
462 xfree (name);
463}
464
465
466static void
9a044a89 467print_one_macro (const char *name, const struct macro_definition *macro,
9b158ba0 468 struct macro_source_file *source, int line,
9a044a89 469 void *ignore)
d7d9f01e
TT
470{
471 fprintf_filtered (gdb_stdout, "macro define %s", name);
472 if (macro->kind == macro_function_like)
473 {
474 int i;
b8d56208 475
d7d9f01e
TT
476 fprintf_filtered (gdb_stdout, "(");
477 for (i = 0; i < macro->argc; ++i)
478 fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
479 macro->argv[i]);
480 fprintf_filtered (gdb_stdout, ")");
481 }
cc704ebe 482 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
6821892e
JB
483}
484
485
486static void
487macro_list_command (char *exp, int from_tty)
488{
9a044a89 489 macro_for_each (macro_user_macros, print_one_macro, NULL);
6821892e
JB
490}
491
6821892e
JB
492\f
493/* Initializing the `macrocmd' module. */
494
a78f21af 495extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
b9362cc7 496
6821892e
JB
497void
498_initialize_macrocmd (void)
499{
6821892e
JB
500 /* We introduce a new command prefix, `macro', under which we'll put
501 the various commands for working with preprocessor macros. */
1bedd215
AC
502 add_prefix_cmd ("macro", class_info, macro_command,
503 _("Prefix for commands dealing with C preprocessor macros."),
504 &macrolist, "macro ", 0, &cmdlist);
6821892e 505
1a966eab
AC
506 add_cmd ("expand", no_class, macro_expand_command, _("\
507Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
508Show the expanded expression."),
509 &macrolist);
6821892e 510 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
1a966eab
AC
511 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
512Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
513Show the expanded expression.\n\
514\n\
515This command differs from `macro expand' in that it only expands macro\n\
516invocations that appear directly in EXPRESSION; if expanding a macro\n\
517introduces further macro invocations, those are left unexpanded.\n\
518\n\
519`macro expand-once' helps you see how a particular macro expands,\n\
520whereas `macro expand' shows you how all the macros involved in an\n\
521expression work together to yield a pre-processed expression."),
522 &macrolist);
6821892e
JB
523 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
524
1a966eab 525 add_cmd ("macro", no_class, info_macro_command,
71eba9c2 526 _("Show the definition of MACRO, and it's source location.\n\
527Usage: info macro [-a|-all] [--] MACRO\n\
528Options: \n\
529 -a, --all Output all definitions of MACRO in the current compilation\
530 unit.\n\
531 -- Specify the end of arguments and the beginning of the MACRO."),
532
1a966eab
AC
533 &infolist);
534
9b158ba0 535 add_cmd ("macros", no_class, info_macros_command,
536 _("Show the definitions of all macros at LINESPEC, or the current \
537source location.\n\
538Usage: info macros [LINESPEC]"),
539 &infolist);
540
1a966eab
AC
541 add_cmd ("define", no_class, macro_define_command, _("\
542Define a new C/C++ preprocessor macro.\n\
543The GDB command `macro define DEFINITION' is equivalent to placing a\n\
544preprocessor directive of the form `#define DEFINITION' such that the\n\
545definition is visible in all the inferior's source files.\n\
546For example:\n\
547 (gdb) macro define PI (3.1415926)\n\
548 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
549 &macrolist);
550
551 add_cmd ("undef", no_class, macro_undef_command, _("\
552Remove the definition of the C/C++ preprocessor macro with the given name."),
553 &macrolist);
554
555 add_cmd ("list", no_class, macro_list_command,
556 _("List all the macros defined using the `macro define' command."),
557 &macrolist);
6821892e 558}
This page took 0.983789 seconds and 4 git commands to generate.