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