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