Commit | Line | Data |
---|---|---|
6821892e | 1 | /* C preprocessor macro expansion commands for GDB. |
61baf725 | 2 | Copyright (C) 2002-2017 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" | |
39cf75f7 | 28 | #include "linespec.h" |
6821892e JB |
29 | |
30 | \f | |
31 | /* The `macro' prefix command. */ | |
32 | ||
33 | static struct cmd_list_element *macrolist; | |
34 | ||
35 | static void | |
36 | macro_command (char *arg, int from_tty) | |
37 | { | |
38 | printf_unfiltered | |
39 | ("\"macro\" must be followed by the name of a macro command.\n"); | |
635c7e8a | 40 | help_list (macrolist, "macro ", all_commands, gdb_stdout); |
6821892e JB |
41 | } |
42 | ||
43 | ||
44 | \f | |
45 | /* Macro expansion commands. */ | |
46 | ||
47 | ||
71eba9c2 | 48 | /* Prints an informational message regarding the lack of macro information. */ |
dd9aa048 TT |
49 | static void |
50 | macro_inform_no_debuginfo (void) | |
71eba9c2 | 51 | { |
daefa854 | 52 | puts_filtered ("GDB has no preprocessor macro information for that code.\n"); |
71eba9c2 | 53 | } |
54 | ||
6821892e JB |
55 | static void |
56 | macro_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 | ||
90 | static void | |
91 | macro_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 |
127 | static void |
128 | show_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 | 155 | static void |
9b158ba0 | 156 | print_macro_definition (const char *name, |
157 | const struct macro_definition *d, | |
158 | struct macro_source_file *file, | |
159 | int line) | |
6821892e | 160 | { |
dd756689 TT |
161 | fprintf_filtered (gdb_stdout, "Defined at "); |
162 | show_pp_source_pos (gdb_stdout, file, line); | |
163 | ||
164 | if (line != 0) | |
165 | fprintf_filtered (gdb_stdout, "#define %s", name); | |
166 | else | |
167 | fprintf_filtered (gdb_stdout, "-D%s", name); | |
168 | ||
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 | } | |
182 | ||
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 | ||
71eba9c2 | 189 | /* The implementation of the `info macro' command. */ |
9b158ba0 | 190 | static void |
71eba9c2 | 191 | info_macro_command (char *args, int from_tty) |
9b158ba0 | 192 | { |
193 | struct macro_scope *ms = NULL; | |
71eba9c2 | 194 | struct cleanup *cleanup_chain; |
195 | char *name; | |
196 | int show_all_macros_named = 0; | |
197 | char *arg_start = args; | |
198 | int processing_args = 1; | |
199 | ||
200 | while (processing_args | |
201 | && arg_start && *arg_start == '-' && *arg_start != '\0') | |
202 | { | |
203 | char *p = skip_to_space (arg_start); | |
204 | ||
205 | if (strncmp (arg_start, "-a", p - arg_start) == 0 | |
206 | || strncmp (arg_start, "-all", p - arg_start) == 0) | |
207 | show_all_macros_named = 1; | |
208 | else if (strncmp (arg_start, "--", p - arg_start) == 0) | |
209 | /* Our macro support seems rather C specific but this would | |
210 | seem necessary for languages allowing - in macro names. | |
211 | e.g. Scheme's (defmacro ->foo () "bar\n") */ | |
212 | processing_args = 0; | |
213 | else | |
214 | { | |
215 | /* Relies on modified 'args' not making it in to history */ | |
216 | *p = '\0'; | |
217 | error (_("Unrecognized option '%s' to info macro command. " | |
218 | "Try \"help info macro\"."), arg_start); | |
219 | } | |
220 | ||
221 | arg_start = skip_spaces (p); | |
222 | } | |
223 | ||
224 | name = arg_start; | |
9b158ba0 | 225 | |
226 | if (! name || ! *name) | |
71eba9c2 | 227 | error (_("You must follow the `info macro' command with the name" |
228 | " of the macro\n" | |
229 | "whose definition you want to see.")); | |
9b158ba0 | 230 | |
231 | ms = default_macro_scope (); | |
71eba9c2 | 232 | cleanup_chain = make_cleanup (free_current_contents, &ms); |
9b158ba0 | 233 | |
71eba9c2 | 234 | if (! ms) |
235 | macro_inform_no_debuginfo (); | |
236 | else if (show_all_macros_named) | |
14bc53a8 PA |
237 | macro_for_each (ms->file->table, [&] (const char *macro_name, |
238 | const macro_definition *macro, | |
239 | macro_source_file *source, | |
240 | int line) | |
241 | { | |
242 | if (strcmp (name, macro_name) == 0) | |
243 | print_macro_definition (name, macro, source, line); | |
244 | }); | |
71eba9c2 | 245 | else |
246 | { | |
247 | struct macro_definition *d; | |
248 | ||
249 | d = macro_lookup_definition (ms->file, ms->line, name); | |
250 | if (d) | |
251 | { | |
252 | int line; | |
253 | struct macro_source_file *file | |
254 | = macro_definition_location (ms->file, ms->line, name, &line); | |
255 | ||
256 | print_macro_definition (name, d, file, line); | |
257 | } | |
258 | else | |
259 | { | |
260 | fprintf_filtered (gdb_stdout, | |
261 | "The symbol `%s' has no definition as a C/C++" | |
262 | " preprocessor macro\n" | |
263 | "at ", name); | |
264 | show_pp_source_pos (gdb_stdout, ms->file, ms->line); | |
265 | } | |
266 | } | |
9b158ba0 | 267 | |
9b158ba0 | 268 | do_cleanups (cleanup_chain); |
269 | } | |
270 | ||
271 | /* Implementation of the "info macros" command. */ | |
272 | static void | |
273 | info_macros_command (char *args, int from_tty) | |
274 | { | |
275 | struct macro_scope *ms = NULL; | |
276 | struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms); | |
277 | ||
278 | if (args == NULL) | |
279 | ms = default_macro_scope (); | |
280 | else | |
281 | { | |
39cf75f7 DE |
282 | struct symtabs_and_lines sals = |
283 | decode_line_with_current_source (args, 0); | |
9b158ba0 | 284 | |
285 | if (sals.nelts) | |
286 | ms = sal_macro_scope (sals.sals[0]); | |
287 | } | |
288 | ||
289 | if (! ms || ! ms->file || ! ms->file->table) | |
71eba9c2 | 290 | macro_inform_no_debuginfo (); |
291 | else | |
14bc53a8 | 292 | macro_for_each_in_scope (ms->file, ms->line, print_macro_definition); |
9b158ba0 | 293 | |
9b158ba0 | 294 | do_cleanups (cleanup_chain); |
295 | } | |
6821892e JB |
296 | |
297 | \f | |
298 | /* User-defined macros. */ | |
299 | ||
d7d9f01e TT |
300 | static void |
301 | skip_ws (char **expp) | |
302 | { | |
303 | while (macro_is_whitespace (**expp)) | |
304 | ++*expp; | |
305 | } | |
306 | ||
2fae03e8 TT |
307 | /* Try to find the bounds of an identifier. If an identifier is |
308 | found, returns a newly allocated string; otherwise returns NULL. | |
309 | EXPP is a pointer to an input string; it is updated to point to the | |
310 | text following the identifier. If IS_PARAMETER is true, this | |
311 | function will also allow "..." forms as used in varargs macro | |
312 | parameters. */ | |
313 | ||
d7d9f01e | 314 | static char * |
2fae03e8 | 315 | extract_identifier (char **expp, int is_parameter) |
d7d9f01e TT |
316 | { |
317 | char *result; | |
318 | char *p = *expp; | |
319 | unsigned int len; | |
2fae03e8 | 320 | |
61012eef | 321 | if (is_parameter && startswith (p, "...")) |
2fae03e8 TT |
322 | { |
323 | /* Ok. */ | |
324 | } | |
325 | else | |
326 | { | |
327 | if (! *p || ! macro_is_identifier_nondigit (*p)) | |
328 | return NULL; | |
329 | for (++p; | |
330 | *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p)); | |
331 | ++p) | |
332 | ; | |
333 | } | |
334 | ||
61012eef | 335 | if (is_parameter && startswith (p, "...")) |
2fae03e8 TT |
336 | p += 3; |
337 | ||
d7d9f01e TT |
338 | len = p - *expp; |
339 | result = (char *) xmalloc (len + 1); | |
340 | memcpy (result, *expp, len); | |
341 | result[len] = '\0'; | |
342 | *expp += len; | |
343 | return result; | |
344 | } | |
345 | ||
346 | /* Helper function to clean up a temporarily-constructed macro object. | |
347 | This assumes that the contents were all allocated with xmalloc. */ | |
348 | static void | |
349 | free_macro_definition_ptr (void *ptr) | |
350 | { | |
351 | int i; | |
352 | struct macro_definition *loc = (struct macro_definition *) ptr; | |
b8d56208 | 353 | |
d7d9f01e TT |
354 | for (i = 0; i < loc->argc; ++i) |
355 | xfree ((char *) loc->argv[i]); | |
356 | xfree ((char *) loc->argv); | |
357 | /* Note that the 'replacement' field is not allocated. */ | |
358 | } | |
6821892e JB |
359 | |
360 | static void | |
361 | macro_define_command (char *exp, int from_tty) | |
362 | { | |
d7d9f01e TT |
363 | struct macro_definition new_macro; |
364 | char *name = NULL; | |
886a217c TT |
365 | struct cleanup *cleanup_chain; |
366 | ||
367 | if (!exp) | |
368 | error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]")); | |
369 | ||
370 | cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro); | |
d7d9f01e TT |
371 | make_cleanup (free_current_contents, &name); |
372 | ||
373 | memset (&new_macro, 0, sizeof (struct macro_definition)); | |
374 | ||
375 | skip_ws (&exp); | |
2fae03e8 | 376 | name = extract_identifier (&exp, 0); |
d7d9f01e TT |
377 | if (! name) |
378 | error (_("Invalid macro name.")); | |
379 | if (*exp == '(') | |
380 | { | |
381 | /* Function-like macro. */ | |
382 | int alloced = 5; | |
8d749320 | 383 | char **argv = XNEWVEC (char *, alloced); |
d7d9f01e TT |
384 | |
385 | new_macro.kind = macro_function_like; | |
386 | new_macro.argc = 0; | |
387 | new_macro.argv = (const char * const *) argv; | |
388 | ||
389 | /* Skip the '(' and whitespace. */ | |
390 | ++exp; | |
391 | skip_ws (&exp); | |
392 | ||
393 | while (*exp != ')') | |
394 | { | |
395 | int i; | |
396 | ||
397 | if (new_macro.argc == alloced) | |
398 | { | |
399 | alloced *= 2; | |
400 | argv = (char **) xrealloc (argv, alloced * sizeof (char *)); | |
025bb325 | 401 | /* Must update new_macro as well... */ |
d7d9f01e TT |
402 | new_macro.argv = (const char * const *) argv; |
403 | } | |
2fae03e8 | 404 | argv[new_macro.argc] = extract_identifier (&exp, 1); |
d7d9f01e TT |
405 | if (! argv[new_macro.argc]) |
406 | error (_("Macro is missing an argument.")); | |
407 | ++new_macro.argc; | |
408 | ||
409 | for (i = new_macro.argc - 2; i >= 0; --i) | |
410 | { | |
411 | if (! strcmp (argv[i], argv[new_macro.argc - 1])) | |
412 | error (_("Two macro arguments with identical names.")); | |
413 | } | |
414 | ||
415 | skip_ws (&exp); | |
416 | if (*exp == ',') | |
417 | { | |
418 | ++exp; | |
419 | skip_ws (&exp); | |
420 | } | |
421 | else if (*exp != ')') | |
422 | error (_("',' or ')' expected at end of macro arguments.")); | |
423 | } | |
424 | /* Skip the closing paren. */ | |
425 | ++exp; | |
cc704ebe | 426 | skip_ws (&exp); |
d7d9f01e TT |
427 | |
428 | macro_define_function (macro_main (macro_user_macros), -1, name, | |
429 | new_macro.argc, (const char **) new_macro.argv, | |
430 | exp); | |
431 | } | |
432 | else | |
cc704ebe TT |
433 | { |
434 | skip_ws (&exp); | |
435 | macro_define_object (macro_main (macro_user_macros), -1, name, exp); | |
436 | } | |
d7d9f01e TT |
437 | |
438 | do_cleanups (cleanup_chain); | |
6821892e JB |
439 | } |
440 | ||
441 | ||
442 | static void | |
443 | macro_undef_command (char *exp, int from_tty) | |
444 | { | |
d7d9f01e | 445 | char *name; |
886a217c TT |
446 | |
447 | if (!exp) | |
448 | error (_("usage: macro undef NAME")); | |
449 | ||
d7d9f01e | 450 | skip_ws (&exp); |
2fae03e8 | 451 | name = extract_identifier (&exp, 0); |
d7d9f01e TT |
452 | if (! name) |
453 | error (_("Invalid macro name.")); | |
454 | macro_undef (macro_main (macro_user_macros), -1, name); | |
455 | xfree (name); | |
456 | } | |
457 | ||
458 | ||
459 | static void | |
9a044a89 | 460 | print_one_macro (const char *name, const struct macro_definition *macro, |
14bc53a8 | 461 | struct macro_source_file *source, int line) |
d7d9f01e TT |
462 | { |
463 | fprintf_filtered (gdb_stdout, "macro define %s", name); | |
464 | if (macro->kind == macro_function_like) | |
465 | { | |
466 | int i; | |
b8d56208 | 467 | |
d7d9f01e TT |
468 | fprintf_filtered (gdb_stdout, "("); |
469 | for (i = 0; i < macro->argc; ++i) | |
470 | fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "", | |
471 | macro->argv[i]); | |
472 | fprintf_filtered (gdb_stdout, ")"); | |
473 | } | |
cc704ebe | 474 | fprintf_filtered (gdb_stdout, " %s\n", macro->replacement); |
6821892e JB |
475 | } |
476 | ||
477 | ||
478 | static void | |
479 | macro_list_command (char *exp, int from_tty) | |
480 | { | |
14bc53a8 | 481 | macro_for_each (macro_user_macros, print_one_macro); |
6821892e JB |
482 | } |
483 | ||
6821892e JB |
484 | \f |
485 | /* Initializing the `macrocmd' module. */ | |
486 | ||
a78f21af | 487 | extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */ |
b9362cc7 | 488 | |
6821892e JB |
489 | void |
490 | _initialize_macrocmd (void) | |
491 | { | |
6821892e JB |
492 | /* We introduce a new command prefix, `macro', under which we'll put |
493 | the various commands for working with preprocessor macros. */ | |
1bedd215 AC |
494 | add_prefix_cmd ("macro", class_info, macro_command, |
495 | _("Prefix for commands dealing with C preprocessor macros."), | |
496 | ¯olist, "macro ", 0, &cmdlist); | |
6821892e | 497 | |
1a966eab AC |
498 | add_cmd ("expand", no_class, macro_expand_command, _("\ |
499 | Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\ | |
500 | Show the expanded expression."), | |
501 | ¯olist); | |
6821892e | 502 | add_alias_cmd ("exp", "expand", no_class, 1, ¯olist); |
1a966eab AC |
503 | add_cmd ("expand-once", no_class, macro_expand_once_command, _("\ |
504 | Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\ | |
505 | Show the expanded expression.\n\ | |
506 | \n\ | |
507 | This command differs from `macro expand' in that it only expands macro\n\ | |
508 | invocations that appear directly in EXPRESSION; if expanding a macro\n\ | |
509 | introduces further macro invocations, those are left unexpanded.\n\ | |
510 | \n\ | |
511 | `macro expand-once' helps you see how a particular macro expands,\n\ | |
512 | whereas `macro expand' shows you how all the macros involved in an\n\ | |
513 | expression work together to yield a pre-processed expression."), | |
514 | ¯olist); | |
6821892e JB |
515 | add_alias_cmd ("exp1", "expand-once", no_class, 1, ¯olist); |
516 | ||
5f515954 AB |
517 | add_info ("macro", info_macro_command, |
518 | _("Show the definition of MACRO, and it's source location.\n\ | |
71eba9c2 | 519 | Usage: info macro [-a|-all] [--] MACRO\n\ |
520 | Options: \n\ | |
521 | -a, --all Output all definitions of MACRO in the current compilation\ | |
522 | unit.\n\ | |
5f515954 | 523 | -- Specify the end of arguments and the beginning of the MACRO.")); |
71eba9c2 | 524 | |
5f515954 AB |
525 | add_info ("macros", info_macros_command, |
526 | _("Show the definitions of all macros at LINESPEC, or the current \ | |
9b158ba0 | 527 | source location.\n\ |
5f515954 | 528 | Usage: info macros [LINESPEC]")); |
9b158ba0 | 529 | |
1a966eab AC |
530 | add_cmd ("define", no_class, macro_define_command, _("\ |
531 | Define a new C/C++ preprocessor macro.\n\ | |
532 | The GDB command `macro define DEFINITION' is equivalent to placing a\n\ | |
533 | preprocessor directive of the form `#define DEFINITION' such that the\n\ | |
534 | definition is visible in all the inferior's source files.\n\ | |
535 | For example:\n\ | |
536 | (gdb) macro define PI (3.1415926)\n\ | |
537 | (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"), | |
538 | ¯olist); | |
539 | ||
540 | add_cmd ("undef", no_class, macro_undef_command, _("\ | |
541 | Remove the definition of the C/C++ preprocessor macro with the given name."), | |
542 | ¯olist); | |
543 | ||
544 | add_cmd ("list", no_class, macro_list_command, | |
545 | _("List all the macros defined using the `macro define' command."), | |
546 | ¯olist); | |
6821892e | 547 | } |