* config/tc-xgate.c (md_begin): Fix mistake made when going from
[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{
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 {
39cf75f7
DE
286 struct symtabs_and_lines sals =
287 decode_line_with_current_source (args, 0);
9b158ba0 288
289 if (sals.nelts)
290 ms = sal_macro_scope (sals.sals[0]);
291 }
292
293 if (! ms || ! ms->file || ! ms->file->table)
71eba9c2 294 macro_inform_no_debuginfo ();
295 else
296 macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);
9b158ba0 297
9b158ba0 298 do_cleanups (cleanup_chain);
299}
6821892e
JB
300
301\f
302/* User-defined macros. */
303
d7d9f01e
TT
304static void
305skip_ws (char **expp)
306{
307 while (macro_is_whitespace (**expp))
308 ++*expp;
309}
310
2fae03e8
TT
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
d7d9f01e 318static char *
2fae03e8 319extract_identifier (char **expp, int is_parameter)
d7d9f01e
TT
320{
321 char *result;
322 char *p = *expp;
323 unsigned int len;
2fae03e8
TT
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
d7d9f01e
TT
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. */
352static void
353free_macro_definition_ptr (void *ptr)
354{
355 int i;
356 struct macro_definition *loc = (struct macro_definition *) ptr;
b8d56208 357
d7d9f01e
TT
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}
6821892e
JB
363
364static void
365macro_define_command (char *exp, int from_tty)
366{
d7d9f01e
TT
367 struct macro_definition new_macro;
368 char *name = NULL;
886a217c
TT
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);
d7d9f01e
TT
375 make_cleanup (free_current_contents, &name);
376
377 memset (&new_macro, 0, sizeof (struct macro_definition));
378
379 skip_ws (&exp);
2fae03e8 380 name = extract_identifier (&exp, 0);
d7d9f01e
TT
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 *));
025bb325 405 /* Must update new_macro as well... */
d7d9f01e
TT
406 new_macro.argv = (const char * const *) argv;
407 }
2fae03e8 408 argv[new_macro.argc] = extract_identifier (&exp, 1);
d7d9f01e
TT
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;
cc704ebe 430 skip_ws (&exp);
d7d9f01e
TT
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
cc704ebe
TT
437 {
438 skip_ws (&exp);
439 macro_define_object (macro_main (macro_user_macros), -1, name, exp);
440 }
d7d9f01e
TT
441
442 do_cleanups (cleanup_chain);
6821892e
JB
443}
444
445
446static void
447macro_undef_command (char *exp, int from_tty)
448{
d7d9f01e 449 char *name;
886a217c
TT
450
451 if (!exp)
452 error (_("usage: macro undef NAME"));
453
d7d9f01e 454 skip_ws (&exp);
2fae03e8 455 name = extract_identifier (&exp, 0);
d7d9f01e
TT
456 if (! name)
457 error (_("Invalid macro name."));
458 macro_undef (macro_main (macro_user_macros), -1, name);
459 xfree (name);
460}
461
462
463static void
9a044a89 464print_one_macro (const char *name, const struct macro_definition *macro,
9b158ba0 465 struct macro_source_file *source, int line,
9a044a89 466 void *ignore)
d7d9f01e
TT
467{
468 fprintf_filtered (gdb_stdout, "macro define %s", name);
469 if (macro->kind == macro_function_like)
470 {
471 int i;
b8d56208 472
d7d9f01e
TT
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 }
cc704ebe 479 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
6821892e
JB
480}
481
482
483static void
484macro_list_command (char *exp, int from_tty)
485{
9a044a89 486 macro_for_each (macro_user_macros, print_one_macro, NULL);
6821892e
JB
487}
488
6821892e
JB
489\f
490/* Initializing the `macrocmd' module. */
491
a78f21af 492extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
b9362cc7 493
6821892e
JB
494void
495_initialize_macrocmd (void)
496{
6821892e
JB
497 /* We introduce a new command prefix, `macro', under which we'll put
498 the various commands for working with preprocessor macros. */
1bedd215
AC
499 add_prefix_cmd ("macro", class_info, macro_command,
500 _("Prefix for commands dealing with C preprocessor macros."),
501 &macrolist, "macro ", 0, &cmdlist);
6821892e 502
1a966eab
AC
503 add_cmd ("expand", no_class, macro_expand_command, _("\
504Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
505Show the expanded expression."),
506 &macrolist);
6821892e 507 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
1a966eab
AC
508 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
509Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
510Show the expanded expression.\n\
511\n\
512This command differs from `macro expand' in that it only expands macro\n\
513invocations that appear directly in EXPRESSION; if expanding a macro\n\
514introduces further macro invocations, those are left unexpanded.\n\
515\n\
516`macro expand-once' helps you see how a particular macro expands,\n\
517whereas `macro expand' shows you how all the macros involved in an\n\
518expression work together to yield a pre-processed expression."),
519 &macrolist);
6821892e
JB
520 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
521
1a966eab 522 add_cmd ("macro", no_class, info_macro_command,
71eba9c2 523 _("Show the definition of MACRO, and it's source location.\n\
524Usage: info macro [-a|-all] [--] MACRO\n\
525Options: \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
1a966eab
AC
530 &infolist);
531
9b158ba0 532 add_cmd ("macros", no_class, info_macros_command,
533 _("Show the definitions of all macros at LINESPEC, or the current \
534source location.\n\
535Usage: info macros [LINESPEC]"),
536 &infolist);
537
1a966eab
AC
538 add_cmd ("define", no_class, macro_define_command, _("\
539Define a new C/C++ preprocessor macro.\n\
540The GDB command `macro define DEFINITION' is equivalent to placing a\n\
541preprocessor directive of the form `#define DEFINITION' such that the\n\
542definition is visible in all the inferior's source files.\n\
543For 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, _("\
549Remove 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);
6821892e 555}
This page took 0.993767 seconds and 4 git commands to generate.