2005-02-14 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / macrocmd.c
CommitLineData
6821892e
JB
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
33static struct cmd_list_element *macrolist;
34
35static void
36macro_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
48static void
49macro_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)
8a3fe4f8 62 error (_("You must follow the `macro expand' command with the"
6821892e 63 " expression you\n"
8a3fe4f8 64 "want to expand."));
6821892e
JB
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
84static void
85macro_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)
8a3fe4f8 98 error (_("You must follow the `macro expand-once' command with"
6821892e 99 " the expression\n"
8a3fe4f8 100 "you want to expand."));
6821892e
JB
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
120static void
121show_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
137static void
475b0867 138info_macro_command (char *name, int from_tty)
6821892e
JB
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)
8a3fe4f8 145 error (_("You must follow the `info macro' command with the name"
6821892e 146 " of the macro\n"
8a3fe4f8 147 "whose definition you want to see."));
6821892e
JB
148
149 ms = default_macro_scope ();
150 if (! ms)
8a3fe4f8 151 error (_("GDB has no preprocessor macro information for that code."));
6821892e
JB
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. */
199static struct macro_table *user_macros;
200
201static void
202macro_define_command (char *exp, int from_tty)
203{
8a3fe4f8 204 error (_("Command not implemented yet."));
6821892e
JB
205}
206
207
208static void
209macro_undef_command (char *exp, int from_tty)
210{
8a3fe4f8 211 error (_("Command not implemented yet."));
6821892e
JB
212}
213
214
215static void
216macro_list_command (char *exp, int from_tty)
217{
8a3fe4f8 218 error (_("Command not implemented yet."));
6821892e
JB
219}
220
221
222\f
223/* Initializing the `macrocmd' module. */
224
a78f21af 225extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
b9362cc7 226
6821892e
JB
227void
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
475b0867 262 ("macro", no_class, info_macro_command,
6821892e 263 "Show the definition of MACRO, and its source location.",
475b0867 264 &infolist);
6821892e
JB
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}
This page took 0.309524 seconds and 4 git commands to generate.