1 /* C preprocessor macro expansion commands for GDB.
2 Copyright 2002 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "macroscope.h"
31 /* The `macro' prefix command. */
33 static struct cmd_list_element *macrolist;
36 macro_command (char *arg, int from_tty)
39 ("\"macro\" must be followed by the name of a macro command.\n");
40 help_list (macrolist, "macro ", -1, gdb_stdout);
45 /* Macro expansion commands. */
49 macro_expand_command (char *exp, int from_tty)
51 struct macro_scope *ms = NULL;
52 char *expanded = NULL;
53 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
54 make_cleanup (free_current_contents, &expanded);
56 /* You know, when the user doesn't specify any expression, it would be
57 really cool if this defaulted to the last expression evaluated.
58 Then it would be easy to ask, "Hey, what did I just evaluate?" But
59 at the moment, the `print' commands don't save the last expression
60 evaluated, just its value. */
62 error ("You must follow the `macro expand' command with the"
66 ms = default_macro_scope ();
69 expanded = macro_expand (exp, standard_macro_lookup, ms);
70 fputs_filtered ("expands to: ", gdb_stdout);
71 fputs_filtered (expanded, gdb_stdout);
72 fputs_filtered ("\n", gdb_stdout);
75 fputs_filtered ("GDB has no preprocessor macro information for "
79 do_cleanups (cleanup_chain);
85 macro_expand_once_command (char *exp, int from_tty)
87 struct macro_scope *ms = NULL;
88 char *expanded = NULL;
89 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
90 make_cleanup (free_current_contents, &expanded);
92 /* You know, when the user doesn't specify any expression, it would be
93 really cool if this defaulted to the last expression evaluated.
94 And it should set the once-expanded text as the new `last
95 expression'. That way, you could just hit return over and over and
96 see the expression expanded one level at a time. */
98 error ("You must follow the `macro expand-once' command with"
100 "you want to expand.");
102 ms = default_macro_scope ();
105 expanded = macro_expand_once (exp, standard_macro_lookup, ms);
106 fputs_filtered ("expands to: ", gdb_stdout);
107 fputs_filtered (expanded, gdb_stdout);
108 fputs_filtered ("\n", gdb_stdout);
111 fputs_filtered ("GDB has no preprocessor macro information for "
115 do_cleanups (cleanup_chain);
121 show_pp_source_pos (struct ui_file *stream,
122 struct macro_source_file *file,
125 fprintf_filtered (stream, "%s:%d\n", file->filename, line);
127 while (file->included_by)
129 fprintf_filtered (gdb_stdout, " included at %s:%d\n",
130 file->included_by->filename,
131 file->included_at_line);
132 file = file->included_by;
138 info_macro_command (char *name, int from_tty)
140 struct macro_scope *ms = NULL;
141 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
142 struct macro_definition *d;
144 if (! name || ! *name)
145 error ("You must follow the `info macro' command with the name"
147 "whose definition you want to see.");
149 ms = default_macro_scope ();
151 error ("GDB has no preprocessor macro information for that code.");
153 d = macro_lookup_definition (ms->file, ms->line, name);
157 struct macro_source_file *file
158 = macro_definition_location (ms->file, ms->line, name, &line);
160 fprintf_filtered (gdb_stdout, "Defined at ");
161 show_pp_source_pos (gdb_stdout, file, line);
162 fprintf_filtered (gdb_stdout, "#define %s", name);
163 if (d->kind == macro_function_like)
167 fputs_filtered ("(", gdb_stdout);
168 for (i = 0; i < d->argc; i++)
170 fputs_filtered (d->argv[i], gdb_stdout);
172 fputs_filtered (", ", gdb_stdout);
174 fputs_filtered (")", gdb_stdout);
176 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
180 fprintf_filtered (gdb_stdout,
181 "The symbol `%s' has no definition as a C/C++"
182 " preprocessor macro\n"
184 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
187 do_cleanups (cleanup_chain);
192 /* User-defined macros. */
194 /* A table of user-defined macros. Unlike the macro tables used for
195 symtabs, this one uses xmalloc for all its allocation, not an
196 obstack, and it doesn't bcache anything; it just xmallocs things. So
197 it's perfectly possible to remove things from this, or redefine
199 static struct macro_table *user_macros;
202 macro_define_command (char *exp, int from_tty)
204 error ("Command not implemented yet.");
209 macro_undef_command (char *exp, int from_tty)
211 error ("Command not implemented yet.");
216 macro_list_command (char *exp, int from_tty)
218 error ("Command not implemented yet.");
223 /* Initializing the `macrocmd' module. */
226 _initialize_macrocmd (void)
228 struct cmd_list_element *c;
230 /* We introduce a new command prefix, `macro', under which we'll put
231 the various commands for working with preprocessor macros. */
233 ("macro", class_info, macro_command,
234 "Prefix for commands dealing with C preprocessor macros.",
235 ¯olist, "macro ", 0, &cmdlist);
238 ("expand", no_class, macro_expand_command,
239 "Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n"
240 "Show the expanded expression.",
242 add_alias_cmd ("exp", "expand", no_class, 1, ¯olist);
244 ("expand-once", no_class, macro_expand_once_command,
245 "Expand C/C++ preprocessor macro invocations appearing directly in"
247 "Show the expanded expression.\n"
249 "This command differs from `macro expand' in that it only expands macro\n"
250 "invocations that appear directly in EXPRESSION; if expanding a macro\n"
251 "introduces further macro invocations, those are left unexpanded.\n"
253 "`macro expand-once' helps you see how a particular macro expands,\n"
254 "whereas `macro expand' shows you how all the macros involved in an\n"
255 "expression work together to yield a pre-processed expression.",
257 add_alias_cmd ("exp1", "expand-once", no_class, 1, ¯olist);
260 ("macro", no_class, info_macro_command,
261 "Show the definition of MACRO, and its source location.",
265 ("define", no_class, macro_define_command,
266 "Define a new C/C++ preprocessor macro.\n"
267 "The GDB command `macro define DEFINITION' is equivalent to placing a\n"
268 "preprocessor directive of the form `#define DEFINITION' such that the\n"
269 "definition is visible in all the inferior's source files.\n"
271 " (gdb) macro define PI (3.1415926)\n"
272 " (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))",
276 ("undef", no_class, macro_undef_command,
277 "Remove the definition of the C/C++ preprocessor macro with the"
282 ("list", no_class, macro_list_command,
283 "List all the macros defined using the `macro define' command.",
286 user_macros = new_macro_table (0, 0);