2010-10-08 Bernd Schmidt <bernds@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / macrocmd.c
CommitLineData
6821892e 1/* C preprocessor macro expansion commands for GDB.
4c38e0a4 2 Copyright (C) 2002, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6821892e
JB
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
6821892e
JB
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6821892e
JB
19
20
21#include "defs.h"
22#include "macrotab.h"
23#include "macroexp.h"
24#include "macroscope.h"
25#include "command.h"
26#include "gdbcmd.h"
d7d9f01e 27#include "gdb_string.h"
6821892e
JB
28
29\f
30/* The `macro' prefix command. */
31
32static struct cmd_list_element *macrolist;
33
34static void
35macro_command (char *arg, int from_tty)
36{
37 printf_unfiltered
38 ("\"macro\" must be followed by the name of a macro command.\n");
39 help_list (macrolist, "macro ", -1, gdb_stdout);
40}
41
42
43\f
44/* Macro expansion commands. */
45
46
47static void
48macro_expand_command (char *exp, int from_tty)
49{
50 struct macro_scope *ms = NULL;
51 char *expanded = NULL;
52 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
b8d56208 53
6821892e
JB
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);
484086b7
JK
162 if (line != 0)
163 fprintf_filtered (gdb_stdout, "#define %s", name);
164 else
165 fprintf_filtered (gdb_stdout, "-D%s", name);
6821892e
JB
166 if (d->kind == macro_function_like)
167 {
168 int i;
169
170 fputs_filtered ("(", gdb_stdout);
171 for (i = 0; i < d->argc; i++)
172 {
173 fputs_filtered (d->argv[i], gdb_stdout);
174 if (i + 1 < d->argc)
175 fputs_filtered (", ", gdb_stdout);
176 }
177 fputs_filtered (")", gdb_stdout);
178 }
484086b7
JK
179 if (line != 0)
180 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
181 else
182 fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
6821892e
JB
183 }
184 else
185 {
186 fprintf_filtered (gdb_stdout,
187 "The symbol `%s' has no definition as a C/C++"
188 " preprocessor macro\n"
189 "at ", name);
190 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
191 }
192
193 do_cleanups (cleanup_chain);
194}
195
196
197\f
198/* User-defined macros. */
199
d7d9f01e
TT
200static void
201skip_ws (char **expp)
202{
203 while (macro_is_whitespace (**expp))
204 ++*expp;
205}
206
2fae03e8
TT
207/* Try to find the bounds of an identifier. If an identifier is
208 found, returns a newly allocated string; otherwise returns NULL.
209 EXPP is a pointer to an input string; it is updated to point to the
210 text following the identifier. If IS_PARAMETER is true, this
211 function will also allow "..." forms as used in varargs macro
212 parameters. */
213
d7d9f01e 214static char *
2fae03e8 215extract_identifier (char **expp, int is_parameter)
d7d9f01e
TT
216{
217 char *result;
218 char *p = *expp;
219 unsigned int len;
2fae03e8
TT
220
221 if (is_parameter && !strncmp (p, "...", 3))
222 {
223 /* Ok. */
224 }
225 else
226 {
227 if (! *p || ! macro_is_identifier_nondigit (*p))
228 return NULL;
229 for (++p;
230 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
231 ++p)
232 ;
233 }
234
235 if (is_parameter && !strncmp (p, "...", 3))
236 p += 3;
237
d7d9f01e
TT
238 len = p - *expp;
239 result = (char *) xmalloc (len + 1);
240 memcpy (result, *expp, len);
241 result[len] = '\0';
242 *expp += len;
243 return result;
244}
245
246/* Helper function to clean up a temporarily-constructed macro object.
247 This assumes that the contents were all allocated with xmalloc. */
248static void
249free_macro_definition_ptr (void *ptr)
250{
251 int i;
252 struct macro_definition *loc = (struct macro_definition *) ptr;
b8d56208 253
d7d9f01e
TT
254 for (i = 0; i < loc->argc; ++i)
255 xfree ((char *) loc->argv[i]);
256 xfree ((char *) loc->argv);
257 /* Note that the 'replacement' field is not allocated. */
258}
6821892e
JB
259
260static void
261macro_define_command (char *exp, int from_tty)
262{
d7d9f01e
TT
263 struct macro_definition new_macro;
264 char *name = NULL;
886a217c
TT
265 struct cleanup *cleanup_chain;
266
267 if (!exp)
268 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
269
270 cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
d7d9f01e
TT
271 make_cleanup (free_current_contents, &name);
272
273 memset (&new_macro, 0, sizeof (struct macro_definition));
274
275 skip_ws (&exp);
2fae03e8 276 name = extract_identifier (&exp, 0);
d7d9f01e
TT
277 if (! name)
278 error (_("Invalid macro name."));
279 if (*exp == '(')
280 {
281 /* Function-like macro. */
282 int alloced = 5;
283 char **argv = (char **) xmalloc (alloced * sizeof (char *));
284
285 new_macro.kind = macro_function_like;
286 new_macro.argc = 0;
287 new_macro.argv = (const char * const *) argv;
288
289 /* Skip the '(' and whitespace. */
290 ++exp;
291 skip_ws (&exp);
292
293 while (*exp != ')')
294 {
295 int i;
296
297 if (new_macro.argc == alloced)
298 {
299 alloced *= 2;
300 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
301 /* Must update new_macro as well... */
302 new_macro.argv = (const char * const *) argv;
303 }
2fae03e8 304 argv[new_macro.argc] = extract_identifier (&exp, 1);
d7d9f01e
TT
305 if (! argv[new_macro.argc])
306 error (_("Macro is missing an argument."));
307 ++new_macro.argc;
308
309 for (i = new_macro.argc - 2; i >= 0; --i)
310 {
311 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
312 error (_("Two macro arguments with identical names."));
313 }
314
315 skip_ws (&exp);
316 if (*exp == ',')
317 {
318 ++exp;
319 skip_ws (&exp);
320 }
321 else if (*exp != ')')
322 error (_("',' or ')' expected at end of macro arguments."));
323 }
324 /* Skip the closing paren. */
325 ++exp;
cc704ebe 326 skip_ws (&exp);
d7d9f01e
TT
327
328 macro_define_function (macro_main (macro_user_macros), -1, name,
329 new_macro.argc, (const char **) new_macro.argv,
330 exp);
331 }
332 else
cc704ebe
TT
333 {
334 skip_ws (&exp);
335 macro_define_object (macro_main (macro_user_macros), -1, name, exp);
336 }
d7d9f01e
TT
337
338 do_cleanups (cleanup_chain);
6821892e
JB
339}
340
341
342static void
343macro_undef_command (char *exp, int from_tty)
344{
d7d9f01e 345 char *name;
886a217c
TT
346
347 if (!exp)
348 error (_("usage: macro undef NAME"));
349
d7d9f01e 350 skip_ws (&exp);
2fae03e8 351 name = extract_identifier (&exp, 0);
d7d9f01e
TT
352 if (! name)
353 error (_("Invalid macro name."));
354 macro_undef (macro_main (macro_user_macros), -1, name);
355 xfree (name);
356}
357
358
359static void
9a044a89
TT
360print_one_macro (const char *name, const struct macro_definition *macro,
361 void *ignore)
d7d9f01e
TT
362{
363 fprintf_filtered (gdb_stdout, "macro define %s", name);
364 if (macro->kind == macro_function_like)
365 {
366 int i;
b8d56208 367
d7d9f01e
TT
368 fprintf_filtered (gdb_stdout, "(");
369 for (i = 0; i < macro->argc; ++i)
370 fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
371 macro->argv[i]);
372 fprintf_filtered (gdb_stdout, ")");
373 }
cc704ebe 374 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
6821892e
JB
375}
376
377
378static void
379macro_list_command (char *exp, int from_tty)
380{
9a044a89 381 macro_for_each (macro_user_macros, print_one_macro, NULL);
6821892e
JB
382}
383
384
385\f
386/* Initializing the `macrocmd' module. */
387
a78f21af 388extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
b9362cc7 389
6821892e
JB
390void
391_initialize_macrocmd (void)
392{
6821892e
JB
393 /* We introduce a new command prefix, `macro', under which we'll put
394 the various commands for working with preprocessor macros. */
1bedd215
AC
395 add_prefix_cmd ("macro", class_info, macro_command,
396 _("Prefix for commands dealing with C preprocessor macros."),
397 &macrolist, "macro ", 0, &cmdlist);
6821892e 398
1a966eab
AC
399 add_cmd ("expand", no_class, macro_expand_command, _("\
400Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
401Show the expanded expression."),
402 &macrolist);
6821892e 403 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
1a966eab
AC
404 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
405Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
406Show the expanded expression.\n\
407\n\
408This command differs from `macro expand' in that it only expands macro\n\
409invocations that appear directly in EXPRESSION; if expanding a macro\n\
410introduces further macro invocations, those are left unexpanded.\n\
411\n\
412`macro expand-once' helps you see how a particular macro expands,\n\
413whereas `macro expand' shows you how all the macros involved in an\n\
414expression work together to yield a pre-processed expression."),
415 &macrolist);
6821892e
JB
416 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
417
1a966eab
AC
418 add_cmd ("macro", no_class, info_macro_command,
419 _("Show the definition of MACRO, and its source location."),
420 &infolist);
421
422 add_cmd ("define", no_class, macro_define_command, _("\
423Define a new C/C++ preprocessor macro.\n\
424The GDB command `macro define DEFINITION' is equivalent to placing a\n\
425preprocessor directive of the form `#define DEFINITION' such that the\n\
426definition is visible in all the inferior's source files.\n\
427For example:\n\
428 (gdb) macro define PI (3.1415926)\n\
429 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
430 &macrolist);
431
432 add_cmd ("undef", no_class, macro_undef_command, _("\
433Remove the definition of the C/C++ preprocessor macro with the given name."),
434 &macrolist);
435
436 add_cmd ("list", no_class, macro_list_command,
437 _("List all the macros defined using the `macro define' command."),
438 &macrolist);
6821892e 439}
This page took 0.703094 seconds and 4 git commands to generate.