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