*** empty log message ***
[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. */
1bedd215
AC
234 add_prefix_cmd ("macro", class_info, macro_command,
235 _("Prefix for commands dealing with C preprocessor macros."),
236 &macrolist, "macro ", 0, &cmdlist);
6821892e 237
1a966eab
AC
238 add_cmd ("expand", no_class, macro_expand_command, _("\
239Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
240Show the expanded expression."),
241 &macrolist);
6821892e 242 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
1a966eab
AC
243 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
244Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
245Show the expanded expression.\n\
246\n\
247This command differs from `macro expand' in that it only expands macro\n\
248invocations that appear directly in EXPRESSION; if expanding a macro\n\
249introduces further macro invocations, those are left unexpanded.\n\
250\n\
251`macro expand-once' helps you see how a particular macro expands,\n\
252whereas `macro expand' shows you how all the macros involved in an\n\
253expression work together to yield a pre-processed expression."),
254 &macrolist);
6821892e
JB
255 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
256
1a966eab
AC
257 add_cmd ("macro", no_class, info_macro_command,
258 _("Show the definition of MACRO, and its source location."),
259 &infolist);
260
261 add_cmd ("define", no_class, macro_define_command, _("\
262Define a new C/C++ preprocessor macro.\n\
263The GDB command `macro define DEFINITION' is equivalent to placing a\n\
264preprocessor directive of the form `#define DEFINITION' such that the\n\
265definition is visible in all the inferior's source files.\n\
266For example:\n\
267 (gdb) macro define PI (3.1415926)\n\
268 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
269 &macrolist);
270
271 add_cmd ("undef", no_class, macro_undef_command, _("\
272Remove the definition of the C/C++ preprocessor macro with the given name."),
273 &macrolist);
274
275 add_cmd ("list", no_class, macro_list_command,
276 _("List all the macros defined using the `macro define' command."),
277 &macrolist);
6821892e
JB
278
279 user_macros = new_macro_table (0, 0);
280}
This page took 0.327866 seconds and 4 git commands to generate.