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