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