[PATCH 30/57][Arm][GAS] Add support for MVE instructions: vqmovnt, vqmovnb, vqmovunt...
[binutils-gdb.git] / gdb / macrocmd.c
blob0f81c3d5da4475f47299bd97c71f030c212e093a
1 /* C preprocessor macro expansion commands for GDB.
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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"
32 /* The `macro' prefix command. */
34 static struct cmd_list_element *macrolist;
36 static void
37 macro_command (const char *arg, int from_tty)
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);
46 /* Macro expansion commands. */
49 /* Prints an informational message regarding the lack of macro information. */
50 static void
51 macro_inform_no_debuginfo (void)
53 puts_filtered ("GDB has no preprocessor macro information for that code.\n");
56 static void
57 macro_expand_command (const char *exp, int from_tty)
59 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
60 gdb::unique_xmalloc_ptr<char> expanded;
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."));
72 ms = default_macro_scope ();
73 if (ms)
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);
80 else
81 macro_inform_no_debuginfo ();
85 static void
86 macro_expand_once_command (const char *exp, int from_tty)
88 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
89 gdb::unique_xmalloc_ptr<char> expanded;
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."));
101 ms = default_macro_scope ();
102 if (ms)
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);
109 else
110 macro_inform_no_debuginfo ();
113 /* Outputs the include path of a macro starting at FILE and LINE to STREAM.
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)
122 std::string fullname = macro_source_fullname (file);
123 fputs_styled (fullname.c_str (), file_name_style.style (), stream);
124 fprintf_filtered (stream, ":%d\n", line);
126 while (file->included_by)
128 fullname = macro_source_fullname (file->included_by);
129 fputs_filtered (_(" included at "), stream);
130 fputs_styled (fullname.c_str (), file_name_style.style (), stream);
131 fprintf_filtered (stream, ":%d\n", file->included_at_line);
132 file = file->included_by;
136 /* Outputs a macro for human consumption, detailing the include path
137 and macro definition. NAME is the name of the macro.
138 D the definition. FILE the start of the include path, and LINE the
139 line number in FILE.
141 Care should be taken that this function does not cause any lookups into
142 the splay tree so that it can be safely used while iterating. */
143 static void
144 print_macro_definition (const char *name,
145 const struct macro_definition *d,
146 struct macro_source_file *file,
147 int line)
149 fprintf_filtered (gdb_stdout, "Defined at ");
150 show_pp_source_pos (gdb_stdout, file, line);
152 if (line != 0)
153 fprintf_filtered (gdb_stdout, "#define %s", name);
154 else
155 fprintf_filtered (gdb_stdout, "-D%s", name);
157 if (d->kind == macro_function_like)
159 int i;
161 fputs_filtered ("(", gdb_stdout);
162 for (i = 0; i < d->argc; i++)
164 fputs_filtered (d->argv[i], gdb_stdout);
165 if (i + 1 < d->argc)
166 fputs_filtered (", ", gdb_stdout);
168 fputs_filtered (")", gdb_stdout);
171 if (line != 0)
172 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
173 else
174 fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
177 /* The implementation of the `info macro' command. */
178 static void
179 info_macro_command (const char *args, int from_tty)
181 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
182 const char *name;
183 int show_all_macros_named = 0;
184 const char *arg_start = args;
185 int processing_args = 1;
187 while (processing_args
188 && arg_start && *arg_start == '-' && *arg_start != '\0')
190 const char *p = skip_to_space (arg_start);
192 if (strncmp (arg_start, "-a", p - arg_start) == 0
193 || strncmp (arg_start, "-all", p - arg_start) == 0)
194 show_all_macros_named = 1;
195 else if (strncmp (arg_start, "--", p - arg_start) == 0)
196 /* Our macro support seems rather C specific but this would
197 seem necessary for languages allowing - in macro names.
198 e.g. Scheme's (defmacro ->foo () "bar\n") */
199 processing_args = 0;
200 else
201 report_unrecognized_option_error ("info macro", arg_start);
203 arg_start = skip_spaces (p);
206 name = arg_start;
208 if (! name || ! *name)
209 error (_("You must follow the `info macro' command with the name"
210 " of the macro\n"
211 "whose definition you want to see."));
213 ms = default_macro_scope ();
215 if (! ms)
216 macro_inform_no_debuginfo ();
217 else if (show_all_macros_named)
218 macro_for_each (ms->file->table, [&] (const char *macro_name,
219 const macro_definition *macro,
220 macro_source_file *source,
221 int line)
223 if (strcmp (name, macro_name) == 0)
224 print_macro_definition (name, macro, source, line);
226 else
228 struct macro_definition *d;
230 d = macro_lookup_definition (ms->file, ms->line, name);
231 if (d)
233 int line;
234 struct macro_source_file *file
235 = macro_definition_location (ms->file, ms->line, name, &line);
237 print_macro_definition (name, d, file, line);
239 else
241 fprintf_filtered (gdb_stdout,
242 "The symbol `%s' has no definition as a C/C++"
243 " preprocessor macro\n"
244 "at ", name);
245 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
250 /* Implementation of the "info macros" command. */
251 static void
252 info_macros_command (const char *args, int from_tty)
254 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
256 if (args == NULL)
257 ms = default_macro_scope ();
258 else
260 std::vector<symtab_and_line> sals
261 = decode_line_with_current_source (args, 0);
263 if (!sals.empty ())
264 ms = sal_macro_scope (sals[0]);
267 if (! ms || ! ms->file || ! ms->file->table)
268 macro_inform_no_debuginfo ();
269 else
270 macro_for_each_in_scope (ms->file, ms->line, print_macro_definition);
274 /* User-defined macros. */
276 static void
277 skip_ws (const char **expp)
279 while (macro_is_whitespace (**expp))
280 ++*expp;
283 /* Try to find the bounds of an identifier. If an identifier is
284 found, returns a newly allocated string; otherwise returns NULL.
285 EXPP is a pointer to an input string; it is updated to point to the
286 text following the identifier. If IS_PARAMETER is true, this
287 function will also allow "..." forms as used in varargs macro
288 parameters. */
290 static gdb::unique_xmalloc_ptr<char>
291 extract_identifier (const char **expp, int is_parameter)
293 char *result;
294 const char *p = *expp;
295 unsigned int len;
297 if (is_parameter && startswith (p, "..."))
299 /* Ok. */
301 else
303 if (! *p || ! macro_is_identifier_nondigit (*p))
304 return NULL;
305 for (++p;
306 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
307 ++p)
311 if (is_parameter && startswith (p, "..."))
312 p += 3;
314 len = p - *expp;
315 result = (char *) xmalloc (len + 1);
316 memcpy (result, *expp, len);
317 result[len] = '\0';
318 *expp += len;
319 return gdb::unique_xmalloc_ptr<char> (result);
322 struct temporary_macro_definition : public macro_definition
324 temporary_macro_definition ()
326 table = nullptr;
327 kind = macro_object_like;
328 argc = 0;
329 argv = nullptr;
330 replacement = nullptr;
333 ~temporary_macro_definition ()
335 int i;
337 for (i = 0; i < argc; ++i)
338 xfree ((char *) argv[i]);
339 xfree ((char *) argv);
340 /* Note that the 'replacement' field is not allocated. */
344 static void
345 macro_define_command (const char *exp, int from_tty)
347 temporary_macro_definition new_macro;
349 if (!exp)
350 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
352 skip_ws (&exp);
353 gdb::unique_xmalloc_ptr<char> name = extract_identifier (&exp, 0);
354 if (name == NULL)
355 error (_("Invalid macro name."));
356 if (*exp == '(')
358 /* Function-like macro. */
359 int alloced = 5;
360 char **argv = XNEWVEC (char *, alloced);
362 new_macro.kind = macro_function_like;
363 new_macro.argc = 0;
364 new_macro.argv = (const char * const *) argv;
366 /* Skip the '(' and whitespace. */
367 ++exp;
368 skip_ws (&exp);
370 while (*exp != ')')
372 int i;
374 if (new_macro.argc == alloced)
376 alloced *= 2;
377 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
378 /* Must update new_macro as well... */
379 new_macro.argv = (const char * const *) argv;
381 argv[new_macro.argc] = extract_identifier (&exp, 1).release ();
382 if (! argv[new_macro.argc])
383 error (_("Macro is missing an argument."));
384 ++new_macro.argc;
386 for (i = new_macro.argc - 2; i >= 0; --i)
388 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
389 error (_("Two macro arguments with identical names."));
392 skip_ws (&exp);
393 if (*exp == ',')
395 ++exp;
396 skip_ws (&exp);
398 else if (*exp != ')')
399 error (_("',' or ')' expected at end of macro arguments."));
401 /* Skip the closing paren. */
402 ++exp;
403 skip_ws (&exp);
405 macro_define_function (macro_main (macro_user_macros), -1, name.get (),
406 new_macro.argc, (const char **) new_macro.argv,
407 exp);
409 else
411 skip_ws (&exp);
412 macro_define_object (macro_main (macro_user_macros), -1, name.get (),
413 exp);
418 static void
419 macro_undef_command (const char *exp, int from_tty)
421 if (!exp)
422 error (_("usage: macro undef NAME"));
424 skip_ws (&exp);
425 gdb::unique_xmalloc_ptr<char> name = extract_identifier (&exp, 0);
426 if (name == nullptr)
427 error (_("Invalid macro name."));
428 macro_undef (macro_main (macro_user_macros), -1, name.get ());
432 static void
433 print_one_macro (const char *name, const struct macro_definition *macro,
434 struct macro_source_file *source, int line)
436 fprintf_filtered (gdb_stdout, "macro define %s", name);
437 if (macro->kind == macro_function_like)
439 int i;
441 fprintf_filtered (gdb_stdout, "(");
442 for (i = 0; i < macro->argc; ++i)
443 fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
444 macro->argv[i]);
445 fprintf_filtered (gdb_stdout, ")");
447 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
451 static void
452 macro_list_command (const char *exp, int from_tty)
454 macro_for_each (macro_user_macros, print_one_macro);
457 /* Initializing the `macrocmd' module. */
459 void
460 _initialize_macrocmd (void)
462 /* We introduce a new command prefix, `macro', under which we'll put
463 the various commands for working with preprocessor macros. */
464 add_prefix_cmd ("macro", class_info, macro_command,
465 _("Prefix for commands dealing with C preprocessor macros."),
466 &macrolist, "macro ", 0, &cmdlist);
468 add_cmd ("expand", no_class, macro_expand_command, _("\
469 Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
470 Show the expanded expression."),
471 &macrolist);
472 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
473 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
474 Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
475 Show the expanded expression.\n\
477 This command differs from `macro expand' in that it only expands macro\n\
478 invocations that appear directly in EXPRESSION; if expanding a macro\n\
479 introduces further macro invocations, those are left unexpanded.\n\
481 `macro expand-once' helps you see how a particular macro expands,\n\
482 whereas `macro expand' shows you how all the macros involved in an\n\
483 expression work together to yield a pre-processed expression."),
484 &macrolist);
485 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
487 add_info ("macro", info_macro_command,
488 _("Show the definition of MACRO, and it's source location.\n\
489 Usage: info macro [-a|-all] [--] MACRO\n\
490 Options: \n\
491 -a, --all Output all definitions of MACRO in the current compilation\
492 unit.\n\
493 -- Specify the end of arguments and the beginning of the MACRO."));
495 add_info ("macros", info_macros_command,
496 _("Show the definitions of all macros at LINESPEC, or the current \
497 source location.\n\
498 Usage: info macros [LINESPEC]"));
500 add_cmd ("define", no_class, macro_define_command, _("\
501 Define a new C/C++ preprocessor macro.\n\
502 The GDB command `macro define DEFINITION' is equivalent to placing a\n\
503 preprocessor directive of the form `#define DEFINITION' such that the\n\
504 definition is visible in all the inferior's source files.\n\
505 For example:\n\
506 (gdb) macro define PI (3.1415926)\n\
507 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
508 &macrolist);
510 add_cmd ("undef", no_class, macro_undef_command, _("\
511 Remove the definition of the C/C++ preprocessor macro with the given name."),
512 &macrolist);
514 add_cmd ("list", no_class, macro_list_command,
515 _("List all the macros defined using the `macro define' command."),
516 &macrolist);