*** empty log message ***
[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);
53 make_cleanup (free_current_contents, &expanded);
54
55 /* You know, when the user doesn't specify any expression, it would be
56 really cool if this defaulted to the last expression evaluated.
57 Then it would be easy to ask, "Hey, what did I just evaluate?" But
58 at the moment, the `print' commands don't save the last expression
59 evaluated, just its value. */
60 if (! exp || ! *exp)
8a3fe4f8 61 error (_("You must follow the `macro expand' command with the"
6821892e 62 " expression you\n"
8a3fe4f8 63 "want to expand."));
6821892e
JB
64
65 ms = default_macro_scope ();
66 if (ms)
67 {
68 expanded = macro_expand (exp, standard_macro_lookup, ms);
69 fputs_filtered ("expands to: ", gdb_stdout);
70 fputs_filtered (expanded, gdb_stdout);
71 fputs_filtered ("\n", gdb_stdout);
72 }
73 else
74 fputs_filtered ("GDB has no preprocessor macro information for "
75 "that code.\n",
76 gdb_stdout);
77
78 do_cleanups (cleanup_chain);
79 return;
80}
81
82
83static void
84macro_expand_once_command (char *exp, int from_tty)
85{
86 struct macro_scope *ms = NULL;
87 char *expanded = NULL;
88 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
89 make_cleanup (free_current_contents, &expanded);
90
91 /* You know, when the user doesn't specify any expression, it would be
92 really cool if this defaulted to the last expression evaluated.
93 And it should set the once-expanded text as the new `last
94 expression'. That way, you could just hit return over and over and
95 see the expression expanded one level at a time. */
96 if (! exp || ! *exp)
8a3fe4f8 97 error (_("You must follow the `macro expand-once' command with"
6821892e 98 " the expression\n"
8a3fe4f8 99 "you want to expand."));
6821892e
JB
100
101 ms = default_macro_scope ();
102 if (ms)
103 {
104 expanded = macro_expand_once (exp, standard_macro_lookup, ms);
105 fputs_filtered ("expands to: ", gdb_stdout);
106 fputs_filtered (expanded, gdb_stdout);
107 fputs_filtered ("\n", gdb_stdout);
108 }
109 else
110 fputs_filtered ("GDB has no preprocessor macro information for "
111 "that code.\n",
112 gdb_stdout);
113
114 do_cleanups (cleanup_chain);
115 return;
116}
117
118
119static void
120show_pp_source_pos (struct ui_file *stream,
121 struct macro_source_file *file,
122 int line)
123{
124 fprintf_filtered (stream, "%s:%d\n", file->filename, line);
125
126 while (file->included_by)
127 {
128 fprintf_filtered (gdb_stdout, " included at %s:%d\n",
129 file->included_by->filename,
130 file->included_at_line);
131 file = file->included_by;
132 }
133}
134
135
136static void
475b0867 137info_macro_command (char *name, int from_tty)
6821892e
JB
138{
139 struct macro_scope *ms = NULL;
140 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
141 struct macro_definition *d;
142
143 if (! name || ! *name)
8a3fe4f8 144 error (_("You must follow the `info macro' command with the name"
6821892e 145 " of the macro\n"
8a3fe4f8 146 "whose definition you want to see."));
6821892e
JB
147
148 ms = default_macro_scope ();
149 if (! ms)
8a3fe4f8 150 error (_("GDB has no preprocessor macro information for that code."));
6821892e
JB
151
152 d = macro_lookup_definition (ms->file, ms->line, name);
153 if (d)
154 {
155 int line;
156 struct macro_source_file *file
157 = macro_definition_location (ms->file, ms->line, name, &line);
158
159 fprintf_filtered (gdb_stdout, "Defined at ");
160 show_pp_source_pos (gdb_stdout, file, line);
484086b7
JK
161 if (line != 0)
162 fprintf_filtered (gdb_stdout, "#define %s", name);
163 else
164 fprintf_filtered (gdb_stdout, "-D%s", name);
6821892e
JB
165 if (d->kind == macro_function_like)
166 {
167 int i;
168
169 fputs_filtered ("(", gdb_stdout);
170 for (i = 0; i < d->argc; i++)
171 {
172 fputs_filtered (d->argv[i], gdb_stdout);
173 if (i + 1 < d->argc)
174 fputs_filtered (", ", gdb_stdout);
175 }
176 fputs_filtered (")", gdb_stdout);
177 }
484086b7
JK
178 if (line != 0)
179 fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
180 else
181 fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
6821892e
JB
182 }
183 else
184 {
185 fprintf_filtered (gdb_stdout,
186 "The symbol `%s' has no definition as a C/C++"
187 " preprocessor macro\n"
188 "at ", name);
189 show_pp_source_pos (gdb_stdout, ms->file, ms->line);
190 }
191
192 do_cleanups (cleanup_chain);
193}
194
195
196\f
197/* User-defined macros. */
198
d7d9f01e
TT
199static void
200skip_ws (char **expp)
201{
202 while (macro_is_whitespace (**expp))
203 ++*expp;
204}
205
2fae03e8
TT
206/* Try to find the bounds of an identifier. If an identifier is
207 found, returns a newly allocated string; otherwise returns NULL.
208 EXPP is a pointer to an input string; it is updated to point to the
209 text following the identifier. If IS_PARAMETER is true, this
210 function will also allow "..." forms as used in varargs macro
211 parameters. */
212
d7d9f01e 213static char *
2fae03e8 214extract_identifier (char **expp, int is_parameter)
d7d9f01e
TT
215{
216 char *result;
217 char *p = *expp;
218 unsigned int len;
2fae03e8
TT
219
220 if (is_parameter && !strncmp (p, "...", 3))
221 {
222 /* Ok. */
223 }
224 else
225 {
226 if (! *p || ! macro_is_identifier_nondigit (*p))
227 return NULL;
228 for (++p;
229 *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
230 ++p)
231 ;
232 }
233
234 if (is_parameter && !strncmp (p, "...", 3))
235 p += 3;
236
d7d9f01e
TT
237 len = p - *expp;
238 result = (char *) xmalloc (len + 1);
239 memcpy (result, *expp, len);
240 result[len] = '\0';
241 *expp += len;
242 return result;
243}
244
245/* Helper function to clean up a temporarily-constructed macro object.
246 This assumes that the contents were all allocated with xmalloc. */
247static void
248free_macro_definition_ptr (void *ptr)
249{
250 int i;
251 struct macro_definition *loc = (struct macro_definition *) ptr;
252 for (i = 0; i < loc->argc; ++i)
253 xfree ((char *) loc->argv[i]);
254 xfree ((char *) loc->argv);
255 /* Note that the 'replacement' field is not allocated. */
256}
6821892e
JB
257
258static void
259macro_define_command (char *exp, int from_tty)
260{
d7d9f01e
TT
261 struct macro_definition new_macro;
262 char *name = NULL;
886a217c
TT
263 struct cleanup *cleanup_chain;
264
265 if (!exp)
266 error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
267
268 cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
d7d9f01e
TT
269 make_cleanup (free_current_contents, &name);
270
271 memset (&new_macro, 0, sizeof (struct macro_definition));
272
273 skip_ws (&exp);
2fae03e8 274 name = extract_identifier (&exp, 0);
d7d9f01e
TT
275 if (! name)
276 error (_("Invalid macro name."));
277 if (*exp == '(')
278 {
279 /* Function-like macro. */
280 int alloced = 5;
281 char **argv = (char **) xmalloc (alloced * sizeof (char *));
282
283 new_macro.kind = macro_function_like;
284 new_macro.argc = 0;
285 new_macro.argv = (const char * const *) argv;
286
287 /* Skip the '(' and whitespace. */
288 ++exp;
289 skip_ws (&exp);
290
291 while (*exp != ')')
292 {
293 int i;
294
295 if (new_macro.argc == alloced)
296 {
297 alloced *= 2;
298 argv = (char **) xrealloc (argv, alloced * sizeof (char *));
299 /* Must update new_macro as well... */
300 new_macro.argv = (const char * const *) argv;
301 }
2fae03e8 302 argv[new_macro.argc] = extract_identifier (&exp, 1);
d7d9f01e
TT
303 if (! argv[new_macro.argc])
304 error (_("Macro is missing an argument."));
305 ++new_macro.argc;
306
307 for (i = new_macro.argc - 2; i >= 0; --i)
308 {
309 if (! strcmp (argv[i], argv[new_macro.argc - 1]))
310 error (_("Two macro arguments with identical names."));
311 }
312
313 skip_ws (&exp);
314 if (*exp == ',')
315 {
316 ++exp;
317 skip_ws (&exp);
318 }
319 else if (*exp != ')')
320 error (_("',' or ')' expected at end of macro arguments."));
321 }
322 /* Skip the closing paren. */
323 ++exp;
cc704ebe 324 skip_ws (&exp);
d7d9f01e
TT
325
326 macro_define_function (macro_main (macro_user_macros), -1, name,
327 new_macro.argc, (const char **) new_macro.argv,
328 exp);
329 }
330 else
cc704ebe
TT
331 {
332 skip_ws (&exp);
333 macro_define_object (macro_main (macro_user_macros), -1, name, exp);
334 }
d7d9f01e
TT
335
336 do_cleanups (cleanup_chain);
6821892e
JB
337}
338
339
340static void
341macro_undef_command (char *exp, int from_tty)
342{
d7d9f01e 343 char *name;
886a217c
TT
344
345 if (!exp)
346 error (_("usage: macro undef NAME"));
347
d7d9f01e 348 skip_ws (&exp);
2fae03e8 349 name = extract_identifier (&exp, 0);
d7d9f01e
TT
350 if (! name)
351 error (_("Invalid macro name."));
352 macro_undef (macro_main (macro_user_macros), -1, name);
353 xfree (name);
354}
355
356
357static void
9a044a89
TT
358print_one_macro (const char *name, const struct macro_definition *macro,
359 void *ignore)
d7d9f01e
TT
360{
361 fprintf_filtered (gdb_stdout, "macro define %s", name);
362 if (macro->kind == macro_function_like)
363 {
364 int i;
365 fprintf_filtered (gdb_stdout, "(");
366 for (i = 0; i < macro->argc; ++i)
367 fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
368 macro->argv[i]);
369 fprintf_filtered (gdb_stdout, ")");
370 }
cc704ebe 371 fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
6821892e
JB
372}
373
374
375static void
376macro_list_command (char *exp, int from_tty)
377{
9a044a89 378 macro_for_each (macro_user_macros, print_one_macro, NULL);
6821892e
JB
379}
380
381
382\f
383/* Initializing the `macrocmd' module. */
384
a78f21af 385extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
b9362cc7 386
6821892e
JB
387void
388_initialize_macrocmd (void)
389{
6821892e
JB
390 /* We introduce a new command prefix, `macro', under which we'll put
391 the various commands for working with preprocessor macros. */
1bedd215
AC
392 add_prefix_cmd ("macro", class_info, macro_command,
393 _("Prefix for commands dealing with C preprocessor macros."),
394 &macrolist, "macro ", 0, &cmdlist);
6821892e 395
1a966eab
AC
396 add_cmd ("expand", no_class, macro_expand_command, _("\
397Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
398Show the expanded expression."),
399 &macrolist);
6821892e 400 add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
1a966eab
AC
401 add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
402Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
403Show the expanded expression.\n\
404\n\
405This command differs from `macro expand' in that it only expands macro\n\
406invocations that appear directly in EXPRESSION; if expanding a macro\n\
407introduces further macro invocations, those are left unexpanded.\n\
408\n\
409`macro expand-once' helps you see how a particular macro expands,\n\
410whereas `macro expand' shows you how all the macros involved in an\n\
411expression work together to yield a pre-processed expression."),
412 &macrolist);
6821892e
JB
413 add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
414
1a966eab
AC
415 add_cmd ("macro", no_class, info_macro_command,
416 _("Show the definition of MACRO, and its source location."),
417 &infolist);
418
419 add_cmd ("define", no_class, macro_define_command, _("\
420Define a new C/C++ preprocessor macro.\n\
421The GDB command `macro define DEFINITION' is equivalent to placing a\n\
422preprocessor directive of the form `#define DEFINITION' such that the\n\
423definition is visible in all the inferior's source files.\n\
424For example:\n\
425 (gdb) macro define PI (3.1415926)\n\
426 (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
427 &macrolist);
428
429 add_cmd ("undef", no_class, macro_undef_command, _("\
430Remove the definition of the C/C++ preprocessor macro with the given name."),
431 &macrolist);
432
433 add_cmd ("list", no_class, macro_list_command,
434 _("List all the macros defined using the `macro define' command."),
435 &macrolist);
6821892e 436}
This page took 0.991416 seconds and 4 git commands to generate.