]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/macrocmd.c
2003-06-11 Andrew Cagney <cagney@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / macrocmd.c
1 /* C preprocessor macro expansion commands for GDB.
2 Copyright 2002 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "defs.h"
24 #include "macrotab.h"
25 #include "macroexp.h"
26 #include "macroscope.h"
27 #include "command.h"
28 #include "gdbcmd.h"
29
30 \f
31 /* The `macro' prefix command. */
32
33 static struct cmd_list_element *macrolist;
34
35 static void
36 macro_command (char *arg, int from_tty)
37 {
38 printf_unfiltered
39 ("\"macro\" must be followed by the name of a macro command.\n");
40 help_list (macrolist, "macro ", -1, gdb_stdout);
41 }
42
43
44 \f
45 /* Macro expansion commands. */
46
47
48 static void
49 macro_expand_command (char *exp, int from_tty)
50 {
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);
55
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. */
61 if (! exp || ! *exp)
62 error ("You must follow the `macro expand' command with the"
63 " expression you\n"
64 "want to expand.");
65
66 ms = default_macro_scope ();
67 if (ms)
68 {
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);
73 }
74 else
75 fputs_filtered ("GDB has no preprocessor macro information for "
76 "that code.\n",
77 gdb_stdout);
78
79 do_cleanups (cleanup_chain);
80 return;
81 }
82
83
84 static void
85 macro_expand_once_command (char *exp, int from_tty)
86 {
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);
91
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. */
97 if (! exp || ! *exp)
98 error ("You must follow the `macro expand-once' command with"
99 " the expression\n"
100 "you want to expand.");
101
102 ms = default_macro_scope ();
103 if (ms)
104 {
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);
109 }
110 else
111 fputs_filtered ("GDB has no preprocessor macro information for "
112 "that code.\n",
113 gdb_stdout);
114
115 do_cleanups (cleanup_chain);
116 return;
117 }
118
119
120 static void
121 show_pp_source_pos (struct ui_file *stream,
122 struct macro_source_file *file,
123 int line)
124 {
125 fprintf_filtered (stream, "%s:%d\n", file->filename, line);
126
127 while (file->included_by)
128 {
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;
133 }
134 }
135
136
137 static void
138 info_macro_command (char *name, int from_tty)
139 {
140 struct macro_scope *ms = NULL;
141 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
142 struct macro_definition *d;
143
144 if (! name || ! *name)
145 error ("You must follow the `info macro' command with the name"
146 " of the macro\n"
147 "whose definition you want to see.");
148
149 ms = default_macro_scope ();
150 if (! ms)
151 error ("GDB has no preprocessor macro information for that code.");
152
153 d = macro_lookup_definition (ms->file, ms->line, name);
154 if (d)
155 {
156 int line;
157 struct macro_source_file *file
158 = macro_definition_location (ms->file, ms->line, name, &line);
159
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)
164 {
165 int i;
166
167 fputs_filtered ("(", gdb_stdout);
168 for (i = 0; i < d->argc; i++)
169 {
170 fputs_filtered (d->argv[i], gdb_stdout);
171 if (i + 1 < d->argc)
172 fputs_filtered (", ", gdb_stdout);
173 }
174 fputs_filtered (")", gdb_stdout);
175 }
176 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
177 }
178 else
179 {
180 fprintf_filtered (gdb_stdout,
181 "The symbol `%s' has no definition as a C/C++"
182 " preprocessor macro\n"
183 "at ", name);
184 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
185 }
186
187 do_cleanups (cleanup_chain);
188 }
189
190
191 \f
192 /* User-defined macros. */
193
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
198 things. */
199 static struct macro_table *user_macros;
200
201 static void
202 macro_define_command (char *exp, int from_tty)
203 {
204 error ("Command not implemented yet.");
205 }
206
207
208 static void
209 macro_undef_command (char *exp, int from_tty)
210 {
211 error ("Command not implemented yet.");
212 }
213
214
215 static void
216 macro_list_command (char *exp, int from_tty)
217 {
218 error ("Command not implemented yet.");
219 }
220
221
222 \f
223 /* Initializing the `macrocmd' module. */
224
225 extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
226
227 void
228 _initialize_macrocmd (void)
229 {
230 struct cmd_list_element *c;
231
232 /* We introduce a new command prefix, `macro', under which we'll put
233 the various commands for working with preprocessor macros. */
234 add_prefix_cmd
235 ("macro", class_info, macro_command,
236 "Prefix for commands dealing with C preprocessor macros.",
237 &macrolist, "macro ", 0, &cmdlist);
238
239 add_cmd
240 ("expand", no_class, macro_expand_command,
241 "Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n"
242 "Show the expanded expression.",
243 &macrolist);
244 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
245 add_cmd
246 ("expand-once", no_class, macro_expand_once_command,
247 "Expand C/C++ preprocessor macro invocations appearing directly in"
248 " EXPRESSION.\n"
249 "Show the expanded expression.\n"
250 "\n"
251 "This command differs from `macro expand' in that it only expands macro\n"
252 "invocations that appear directly in EXPRESSION; if expanding a macro\n"
253 "introduces further macro invocations, those are left unexpanded.\n"
254 "\n"
255 "`macro expand-once' helps you see how a particular macro expands,\n"
256 "whereas `macro expand' shows you how all the macros involved in an\n"
257 "expression work together to yield a pre-processed expression.",
258 &macrolist);
259 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
260
261 add_cmd
262 ("macro", no_class, info_macro_command,
263 "Show the definition of MACRO, and its source location.",
264 &infolist);
265
266 add_cmd
267 ("define", no_class, macro_define_command,
268 "Define a new C/C++ preprocessor macro.\n"
269 "The GDB command `macro define DEFINITION' is equivalent to placing a\n"
270 "preprocessor directive of the form `#define DEFINITION' such that the\n"
271 "definition is visible in all the inferior's source files.\n"
272 "For example:\n"
273 " (gdb) macro define PI (3.1415926)\n"
274 " (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))",
275 &macrolist);
276
277 add_cmd
278 ("undef", no_class, macro_undef_command,
279 "Remove the definition of the C/C++ preprocessor macro with the"
280 " given name.",
281 &macrolist);
282
283 add_cmd
284 ("list", no_class, macro_list_command,
285 "List all the macros defined using the `macro define' command.",
286 &macrolist);
287
288 user_macros = new_macro_table (0, 0);
289 }