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