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