xref: /dflybsd-src/contrib/gdb-7/gdb/macrocmd.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* C preprocessor macro expansion commands for GDB.
2*ef5ccd6cSJohn Marino    Copyright (C) 2002-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert    Contributed by Red Hat, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "macrotab.h"
235796c8dcSSimon Schubert #include "macroexp.h"
245796c8dcSSimon Schubert #include "macroscope.h"
25a45ae5f8SJohn Marino #include "cli/cli-utils.h"
265796c8dcSSimon Schubert #include "command.h"
275796c8dcSSimon Schubert #include "gdbcmd.h"
285796c8dcSSimon Schubert #include "gdb_string.h"
29*ef5ccd6cSJohn Marino #include "linespec.h"
305796c8dcSSimon Schubert 
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert /* The `macro' prefix command.  */
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert static struct cmd_list_element *macrolist;
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert static void
macro_command(char * arg,int from_tty)375796c8dcSSimon Schubert macro_command (char *arg, int from_tty)
385796c8dcSSimon Schubert {
395796c8dcSSimon Schubert   printf_unfiltered
405796c8dcSSimon Schubert     ("\"macro\" must be followed by the name of a macro command.\n");
415796c8dcSSimon Schubert   help_list (macrolist, "macro ", -1, gdb_stdout);
425796c8dcSSimon Schubert }
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert /* Macro expansion commands.  */
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert 
49a45ae5f8SJohn Marino /* Prints an informational message regarding the lack of macro information.  */
macro_inform_no_debuginfo()50a45ae5f8SJohn Marino static void macro_inform_no_debuginfo()
51a45ae5f8SJohn Marino {
52*ef5ccd6cSJohn Marino   puts_filtered ("GDB has no preprocessor macro information for that code.\n");
53a45ae5f8SJohn Marino }
54a45ae5f8SJohn Marino 
555796c8dcSSimon Schubert static void
macro_expand_command(char * exp,int from_tty)565796c8dcSSimon Schubert macro_expand_command (char *exp, int from_tty)
575796c8dcSSimon Schubert {
585796c8dcSSimon Schubert   struct macro_scope *ms = NULL;
595796c8dcSSimon Schubert   char *expanded = NULL;
605796c8dcSSimon Schubert   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
61cf7f2e2dSJohn Marino 
625796c8dcSSimon Schubert   make_cleanup (free_current_contents, &expanded);
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert   /* You know, when the user doesn't specify any expression, it would be
655796c8dcSSimon Schubert      really cool if this defaulted to the last expression evaluated.
665796c8dcSSimon Schubert      Then it would be easy to ask, "Hey, what did I just evaluate?"  But
675796c8dcSSimon Schubert      at the moment, the `print' commands don't save the last expression
685796c8dcSSimon Schubert      evaluated, just its value.  */
695796c8dcSSimon Schubert   if (! exp || ! *exp)
705796c8dcSSimon Schubert     error (_("You must follow the `macro expand' command with the"
715796c8dcSSimon Schubert            " expression you\n"
725796c8dcSSimon Schubert            "want to expand."));
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert   ms = default_macro_scope ();
755796c8dcSSimon Schubert   if (ms)
765796c8dcSSimon Schubert     {
775796c8dcSSimon Schubert       expanded = macro_expand (exp, standard_macro_lookup, ms);
785796c8dcSSimon Schubert       fputs_filtered ("expands to: ", gdb_stdout);
795796c8dcSSimon Schubert       fputs_filtered (expanded, gdb_stdout);
805796c8dcSSimon Schubert       fputs_filtered ("\n", gdb_stdout);
815796c8dcSSimon Schubert     }
825796c8dcSSimon Schubert   else
83a45ae5f8SJohn Marino     macro_inform_no_debuginfo ();
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert   do_cleanups (cleanup_chain);
865796c8dcSSimon Schubert   return;
875796c8dcSSimon Schubert }
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert static void
macro_expand_once_command(char * exp,int from_tty)915796c8dcSSimon Schubert macro_expand_once_command (char *exp, int from_tty)
925796c8dcSSimon Schubert {
935796c8dcSSimon Schubert   struct macro_scope *ms = NULL;
945796c8dcSSimon Schubert   char *expanded = NULL;
955796c8dcSSimon Schubert   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
965796c8dcSSimon Schubert   make_cleanup (free_current_contents, &expanded);
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert   /* You know, when the user doesn't specify any expression, it would be
995796c8dcSSimon Schubert      really cool if this defaulted to the last expression evaluated.
1005796c8dcSSimon Schubert      And it should set the once-expanded text as the new `last
1015796c8dcSSimon Schubert      expression'.  That way, you could just hit return over and over and
1025796c8dcSSimon Schubert      see the expression expanded one level at a time.  */
1035796c8dcSSimon Schubert   if (! exp || ! *exp)
1045796c8dcSSimon Schubert     error (_("You must follow the `macro expand-once' command with"
1055796c8dcSSimon Schubert            " the expression\n"
1065796c8dcSSimon Schubert            "you want to expand."));
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert   ms = default_macro_scope ();
1095796c8dcSSimon Schubert   if (ms)
1105796c8dcSSimon Schubert     {
1115796c8dcSSimon Schubert       expanded = macro_expand_once (exp, standard_macro_lookup, ms);
1125796c8dcSSimon Schubert       fputs_filtered ("expands to: ", gdb_stdout);
1135796c8dcSSimon Schubert       fputs_filtered (expanded, gdb_stdout);
1145796c8dcSSimon Schubert       fputs_filtered ("\n", gdb_stdout);
1155796c8dcSSimon Schubert     }
1165796c8dcSSimon Schubert   else
117a45ae5f8SJohn Marino     macro_inform_no_debuginfo ();
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert   do_cleanups (cleanup_chain);
1205796c8dcSSimon Schubert   return;
1215796c8dcSSimon Schubert }
1225796c8dcSSimon Schubert 
123a45ae5f8SJohn Marino /*  Outputs the include path of a macro starting at FILE and LINE to STREAM.
1245796c8dcSSimon Schubert 
125a45ae5f8SJohn Marino     Care should be taken that this function does not cause any lookups into
126a45ae5f8SJohn Marino     the splay tree so that it can be safely used while iterating.  */
1275796c8dcSSimon Schubert static void
show_pp_source_pos(struct ui_file * stream,struct macro_source_file * file,int line)1285796c8dcSSimon Schubert show_pp_source_pos (struct ui_file *stream,
1295796c8dcSSimon Schubert                     struct macro_source_file *file,
1305796c8dcSSimon Schubert                     int line)
1315796c8dcSSimon Schubert {
132*ef5ccd6cSJohn Marino   char *fullname;
133*ef5ccd6cSJohn Marino 
134*ef5ccd6cSJohn Marino   fullname = macro_source_fullname (file);
135*ef5ccd6cSJohn Marino   fprintf_filtered (stream, "%s:%d\n", fullname, line);
136*ef5ccd6cSJohn Marino   xfree (fullname);
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   while (file->included_by)
1395796c8dcSSimon Schubert     {
140*ef5ccd6cSJohn Marino       fullname = macro_source_fullname (file->included_by);
141*ef5ccd6cSJohn Marino       fprintf_filtered (gdb_stdout, "  included at %s:%d\n", fullname,
1425796c8dcSSimon Schubert                         file->included_at_line);
143*ef5ccd6cSJohn Marino       xfree (fullname);
1445796c8dcSSimon Schubert       file = file->included_by;
1455796c8dcSSimon Schubert     }
1465796c8dcSSimon Schubert }
1475796c8dcSSimon Schubert 
148a45ae5f8SJohn Marino /* Outputs a macro for human consumption, detailing the include path
149a45ae5f8SJohn Marino    and macro definition.  NAME is the name of the macro.
150a45ae5f8SJohn Marino    D the definition.  FILE the start of the include path, and LINE the
151a45ae5f8SJohn Marino    line number in FILE.
1525796c8dcSSimon Schubert 
153a45ae5f8SJohn Marino    Care should be taken that this function does not cause any lookups into
154a45ae5f8SJohn Marino    the splay tree so that it can be safely used while iterating.  */
1555796c8dcSSimon Schubert static void
print_macro_definition(const char * name,const struct macro_definition * d,struct macro_source_file * file,int line)156a45ae5f8SJohn Marino print_macro_definition (const char *name,
157a45ae5f8SJohn Marino 			const struct macro_definition *d,
158a45ae5f8SJohn Marino 			struct macro_source_file *file,
159a45ae5f8SJohn Marino 			int line)
1605796c8dcSSimon Schubert {
1615796c8dcSSimon Schubert       fprintf_filtered (gdb_stdout, "Defined at ");
1625796c8dcSSimon Schubert       show_pp_source_pos (gdb_stdout, file, line);
163a45ae5f8SJohn Marino 
1645796c8dcSSimon Schubert       if (line != 0)
1655796c8dcSSimon Schubert 	fprintf_filtered (gdb_stdout, "#define %s", name);
1665796c8dcSSimon Schubert       else
1675796c8dcSSimon Schubert 	fprintf_filtered (gdb_stdout, "-D%s", name);
168a45ae5f8SJohn Marino 
1695796c8dcSSimon Schubert       if (d->kind == macro_function_like)
1705796c8dcSSimon Schubert         {
1715796c8dcSSimon Schubert           int i;
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert           fputs_filtered ("(", gdb_stdout);
1745796c8dcSSimon Schubert           for (i = 0; i < d->argc; i++)
1755796c8dcSSimon Schubert             {
1765796c8dcSSimon Schubert               fputs_filtered (d->argv[i], gdb_stdout);
1775796c8dcSSimon Schubert               if (i + 1 < d->argc)
1785796c8dcSSimon Schubert                 fputs_filtered (", ", gdb_stdout);
1795796c8dcSSimon Schubert             }
1805796c8dcSSimon Schubert           fputs_filtered (")", gdb_stdout);
1815796c8dcSSimon Schubert         }
182a45ae5f8SJohn Marino 
1835796c8dcSSimon Schubert       if (line != 0)
1845796c8dcSSimon Schubert 	fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
1855796c8dcSSimon Schubert       else
1865796c8dcSSimon Schubert 	fprintf_filtered (gdb_stdout, "=%s\n", d->replacement);
1875796c8dcSSimon Schubert }
188a45ae5f8SJohn Marino 
189a45ae5f8SJohn Marino /* A callback function for usage with macro_for_each and friends.
190a45ae5f8SJohn Marino    If USER_DATA is null all macros will be printed.
191a45ae5f8SJohn Marino    Otherwise USER_DATA is considered to be a string, printing
192a45ae5f8SJohn Marino    only macros who's NAME matches USER_DATA.  Other arguments are
193a45ae5f8SJohn Marino    routed to print_macro_definition.  */
194a45ae5f8SJohn Marino static void
print_macro_callback(const char * name,const struct macro_definition * macro,struct macro_source_file * source,int line,void * user_data)195a45ae5f8SJohn Marino print_macro_callback (const char *name, const struct macro_definition *macro,
196a45ae5f8SJohn Marino 		   struct macro_source_file *source, int line,
197a45ae5f8SJohn Marino 		   void *user_data)
198a45ae5f8SJohn Marino {
199a45ae5f8SJohn Marino   if (! user_data || strcmp (user_data, name) == 0)
200a45ae5f8SJohn Marino     print_macro_definition (name, macro, source, line);
201a45ae5f8SJohn Marino }
202a45ae5f8SJohn Marino 
203a45ae5f8SJohn Marino /* The implementation of the `info macro' command.  */
204a45ae5f8SJohn Marino static void
info_macro_command(char * args,int from_tty)205a45ae5f8SJohn Marino info_macro_command (char *args, int from_tty)
206a45ae5f8SJohn Marino {
207a45ae5f8SJohn Marino   struct macro_scope *ms = NULL;
208a45ae5f8SJohn Marino   struct cleanup *cleanup_chain;
209a45ae5f8SJohn Marino   char *name;
210a45ae5f8SJohn Marino   int show_all_macros_named = 0;
211a45ae5f8SJohn Marino   char *arg_start = args;
212a45ae5f8SJohn Marino   int processing_args = 1;
213a45ae5f8SJohn Marino 
214a45ae5f8SJohn Marino   while (processing_args
215a45ae5f8SJohn Marino 	 && arg_start && *arg_start == '-' && *arg_start != '\0')
216a45ae5f8SJohn Marino     {
217a45ae5f8SJohn Marino       char *p = skip_to_space (arg_start);
218a45ae5f8SJohn Marino 
219a45ae5f8SJohn Marino       if (strncmp (arg_start, "-a", p - arg_start) == 0
220a45ae5f8SJohn Marino 	  || strncmp (arg_start, "-all", p - arg_start) == 0)
221a45ae5f8SJohn Marino 	show_all_macros_named = 1;
222a45ae5f8SJohn Marino       else if (strncmp (arg_start, "--", p - arg_start) == 0)
223a45ae5f8SJohn Marino           /* Our macro support seems rather C specific but this would
224a45ae5f8SJohn Marino              seem necessary for languages allowing - in macro names.
225a45ae5f8SJohn Marino 	     e.g. Scheme's (defmacro ->foo () "bar\n")  */
226a45ae5f8SJohn Marino 	processing_args = 0;
227a45ae5f8SJohn Marino       else
228a45ae5f8SJohn Marino 	{
229a45ae5f8SJohn Marino 	  /* Relies on modified 'args' not making it in to history */
230a45ae5f8SJohn Marino 	  *p = '\0';
231a45ae5f8SJohn Marino 	  error (_("Unrecognized option '%s' to info macro command.  "
232a45ae5f8SJohn Marino 		   "Try \"help info macro\"."), arg_start);
233a45ae5f8SJohn Marino 	}
234a45ae5f8SJohn Marino 
235a45ae5f8SJohn Marino         arg_start = skip_spaces (p);
236a45ae5f8SJohn Marino     }
237a45ae5f8SJohn Marino 
238a45ae5f8SJohn Marino   name = arg_start;
239a45ae5f8SJohn Marino 
240a45ae5f8SJohn Marino   if (! name || ! *name)
241a45ae5f8SJohn Marino     error (_("You must follow the `info macro' command with the name"
242a45ae5f8SJohn Marino 	     " of the macro\n"
243a45ae5f8SJohn Marino 	     "whose definition you want to see."));
244a45ae5f8SJohn Marino 
245a45ae5f8SJohn Marino   ms = default_macro_scope ();
246a45ae5f8SJohn Marino   cleanup_chain = make_cleanup (free_current_contents, &ms);
247a45ae5f8SJohn Marino 
248a45ae5f8SJohn Marino   if (! ms)
249a45ae5f8SJohn Marino     macro_inform_no_debuginfo ();
250a45ae5f8SJohn Marino   else if (show_all_macros_named)
251a45ae5f8SJohn Marino     macro_for_each (ms->file->table, print_macro_callback, name);
252a45ae5f8SJohn Marino   else
253a45ae5f8SJohn Marino     {
254a45ae5f8SJohn Marino       struct macro_definition *d;
255a45ae5f8SJohn Marino 
256a45ae5f8SJohn Marino       d = macro_lookup_definition (ms->file, ms->line, name);
257a45ae5f8SJohn Marino       if (d)
258a45ae5f8SJohn Marino 	{
259a45ae5f8SJohn Marino 	  int line;
260a45ae5f8SJohn Marino 	  struct macro_source_file *file
261a45ae5f8SJohn Marino 	    = macro_definition_location (ms->file, ms->line, name, &line);
262a45ae5f8SJohn Marino 
263a45ae5f8SJohn Marino 	  print_macro_definition (name, d, file, line);
264a45ae5f8SJohn Marino 	}
2655796c8dcSSimon Schubert       else
2665796c8dcSSimon Schubert         {
2675796c8dcSSimon Schubert           fprintf_filtered (gdb_stdout,
2685796c8dcSSimon Schubert                             "The symbol `%s' has no definition as a C/C++"
2695796c8dcSSimon Schubert                             " preprocessor macro\n"
2705796c8dcSSimon Schubert                             "at ", name);
2715796c8dcSSimon Schubert           show_pp_source_pos (gdb_stdout, ms->file, ms->line);
2725796c8dcSSimon Schubert 	}
273a45ae5f8SJohn Marino     }
2745796c8dcSSimon Schubert 
2755796c8dcSSimon Schubert   do_cleanups (cleanup_chain);
2765796c8dcSSimon Schubert }
2775796c8dcSSimon Schubert 
278a45ae5f8SJohn Marino /* Implementation of the "info macros" command. */
279a45ae5f8SJohn Marino static void
info_macros_command(char * args,int from_tty)280a45ae5f8SJohn Marino info_macros_command (char *args, int from_tty)
281a45ae5f8SJohn Marino {
282a45ae5f8SJohn Marino   struct macro_scope *ms = NULL;
283a45ae5f8SJohn Marino   struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
284a45ae5f8SJohn Marino 
285a45ae5f8SJohn Marino   if (args == NULL)
286a45ae5f8SJohn Marino     ms = default_macro_scope ();
287a45ae5f8SJohn Marino   else
288a45ae5f8SJohn Marino     {
289*ef5ccd6cSJohn Marino       struct symtabs_and_lines sals =
290*ef5ccd6cSJohn Marino 	decode_line_with_current_source (args, 0);
291a45ae5f8SJohn Marino 
292a45ae5f8SJohn Marino       if (sals.nelts)
293a45ae5f8SJohn Marino         ms = sal_macro_scope (sals.sals[0]);
294a45ae5f8SJohn Marino     }
295a45ae5f8SJohn Marino 
296a45ae5f8SJohn Marino   if (! ms || ! ms->file || ! ms->file->table)
297a45ae5f8SJohn Marino     macro_inform_no_debuginfo ();
298a45ae5f8SJohn Marino   else
299a45ae5f8SJohn Marino     macro_for_each_in_scope (ms->file, ms->line, print_macro_callback, NULL);
300a45ae5f8SJohn Marino 
301a45ae5f8SJohn Marino   do_cleanups (cleanup_chain);
302a45ae5f8SJohn Marino }
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert /* User-defined macros.  */
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert static void
skip_ws(char ** expp)3085796c8dcSSimon Schubert skip_ws (char **expp)
3095796c8dcSSimon Schubert {
3105796c8dcSSimon Schubert   while (macro_is_whitespace (**expp))
3115796c8dcSSimon Schubert     ++*expp;
3125796c8dcSSimon Schubert }
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert /* Try to find the bounds of an identifier.  If an identifier is
3155796c8dcSSimon Schubert    found, returns a newly allocated string; otherwise returns NULL.
3165796c8dcSSimon Schubert    EXPP is a pointer to an input string; it is updated to point to the
3175796c8dcSSimon Schubert    text following the identifier.  If IS_PARAMETER is true, this
3185796c8dcSSimon Schubert    function will also allow "..." forms as used in varargs macro
3195796c8dcSSimon Schubert    parameters.  */
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert static char *
extract_identifier(char ** expp,int is_parameter)3225796c8dcSSimon Schubert extract_identifier (char **expp, int is_parameter)
3235796c8dcSSimon Schubert {
3245796c8dcSSimon Schubert   char *result;
3255796c8dcSSimon Schubert   char *p = *expp;
3265796c8dcSSimon Schubert   unsigned int len;
3275796c8dcSSimon Schubert 
3285796c8dcSSimon Schubert   if (is_parameter && !strncmp (p, "...", 3))
3295796c8dcSSimon Schubert     {
3305796c8dcSSimon Schubert       /* Ok.  */
3315796c8dcSSimon Schubert     }
3325796c8dcSSimon Schubert   else
3335796c8dcSSimon Schubert     {
3345796c8dcSSimon Schubert       if (! *p || ! macro_is_identifier_nondigit (*p))
3355796c8dcSSimon Schubert 	return NULL;
3365796c8dcSSimon Schubert       for (++p;
3375796c8dcSSimon Schubert 	   *p && (macro_is_identifier_nondigit (*p) || macro_is_digit (*p));
3385796c8dcSSimon Schubert 	   ++p)
3395796c8dcSSimon Schubert 	;
3405796c8dcSSimon Schubert     }
3415796c8dcSSimon Schubert 
3425796c8dcSSimon Schubert   if (is_parameter && !strncmp (p, "...", 3))
3435796c8dcSSimon Schubert     p += 3;
3445796c8dcSSimon Schubert 
3455796c8dcSSimon Schubert   len = p - *expp;
3465796c8dcSSimon Schubert   result = (char *) xmalloc (len + 1);
3475796c8dcSSimon Schubert   memcpy (result, *expp, len);
3485796c8dcSSimon Schubert   result[len] = '\0';
3495796c8dcSSimon Schubert   *expp += len;
3505796c8dcSSimon Schubert   return result;
3515796c8dcSSimon Schubert }
3525796c8dcSSimon Schubert 
3535796c8dcSSimon Schubert /* Helper function to clean up a temporarily-constructed macro object.
3545796c8dcSSimon Schubert    This assumes that the contents were all allocated with xmalloc.  */
3555796c8dcSSimon Schubert static void
free_macro_definition_ptr(void * ptr)3565796c8dcSSimon Schubert free_macro_definition_ptr (void *ptr)
3575796c8dcSSimon Schubert {
3585796c8dcSSimon Schubert   int i;
3595796c8dcSSimon Schubert   struct macro_definition *loc = (struct macro_definition *) ptr;
360cf7f2e2dSJohn Marino 
3615796c8dcSSimon Schubert   for (i = 0; i < loc->argc; ++i)
3625796c8dcSSimon Schubert     xfree ((char *) loc->argv[i]);
3635796c8dcSSimon Schubert   xfree ((char *) loc->argv);
3645796c8dcSSimon Schubert   /* Note that the 'replacement' field is not allocated.  */
3655796c8dcSSimon Schubert }
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert static void
macro_define_command(char * exp,int from_tty)3685796c8dcSSimon Schubert macro_define_command (char *exp, int from_tty)
3695796c8dcSSimon Schubert {
3705796c8dcSSimon Schubert   struct macro_definition new_macro;
3715796c8dcSSimon Schubert   char *name = NULL;
3725796c8dcSSimon Schubert   struct cleanup *cleanup_chain;
3735796c8dcSSimon Schubert 
3745796c8dcSSimon Schubert   if (!exp)
3755796c8dcSSimon Schubert     error (_("usage: macro define NAME[(ARGUMENT-LIST)] [REPLACEMENT-LIST]"));
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert   cleanup_chain = make_cleanup (free_macro_definition_ptr, &new_macro);
3785796c8dcSSimon Schubert   make_cleanup (free_current_contents, &name);
3795796c8dcSSimon Schubert 
3805796c8dcSSimon Schubert   memset (&new_macro, 0, sizeof (struct macro_definition));
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert   skip_ws (&exp);
3835796c8dcSSimon Schubert   name = extract_identifier (&exp, 0);
3845796c8dcSSimon Schubert   if (! name)
3855796c8dcSSimon Schubert     error (_("Invalid macro name."));
3865796c8dcSSimon Schubert   if (*exp == '(')
3875796c8dcSSimon Schubert     {
3885796c8dcSSimon Schubert       /* Function-like macro.  */
3895796c8dcSSimon Schubert       int alloced = 5;
3905796c8dcSSimon Schubert       char **argv = (char **) xmalloc (alloced * sizeof (char *));
3915796c8dcSSimon Schubert 
3925796c8dcSSimon Schubert       new_macro.kind = macro_function_like;
3935796c8dcSSimon Schubert       new_macro.argc = 0;
3945796c8dcSSimon Schubert       new_macro.argv = (const char * const *) argv;
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert       /* Skip the '(' and whitespace.  */
3975796c8dcSSimon Schubert       ++exp;
3985796c8dcSSimon Schubert       skip_ws (&exp);
3995796c8dcSSimon Schubert 
4005796c8dcSSimon Schubert       while (*exp != ')')
4015796c8dcSSimon Schubert 	{
4025796c8dcSSimon Schubert 	  int i;
4035796c8dcSSimon Schubert 
4045796c8dcSSimon Schubert 	  if (new_macro.argc == alloced)
4055796c8dcSSimon Schubert 	    {
4065796c8dcSSimon Schubert 	      alloced *= 2;
4075796c8dcSSimon Schubert 	      argv = (char **) xrealloc (argv, alloced * sizeof (char *));
4085796c8dcSSimon Schubert 	      /* Must update new_macro as well...  */
4095796c8dcSSimon Schubert 	      new_macro.argv = (const char * const *) argv;
4105796c8dcSSimon Schubert 	    }
4115796c8dcSSimon Schubert 	  argv[new_macro.argc] = extract_identifier (&exp, 1);
4125796c8dcSSimon Schubert 	  if (! argv[new_macro.argc])
4135796c8dcSSimon Schubert 	    error (_("Macro is missing an argument."));
4145796c8dcSSimon Schubert 	  ++new_macro.argc;
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert 	  for (i = new_macro.argc - 2; i >= 0; --i)
4175796c8dcSSimon Schubert 	    {
4185796c8dcSSimon Schubert 	      if (! strcmp (argv[i], argv[new_macro.argc - 1]))
4195796c8dcSSimon Schubert 		error (_("Two macro arguments with identical names."));
4205796c8dcSSimon Schubert 	    }
4215796c8dcSSimon Schubert 
4225796c8dcSSimon Schubert 	  skip_ws (&exp);
4235796c8dcSSimon Schubert 	  if (*exp == ',')
4245796c8dcSSimon Schubert 	    {
4255796c8dcSSimon Schubert 	      ++exp;
4265796c8dcSSimon Schubert 	      skip_ws (&exp);
4275796c8dcSSimon Schubert 	    }
4285796c8dcSSimon Schubert 	  else if (*exp != ')')
4295796c8dcSSimon Schubert 	    error (_("',' or ')' expected at end of macro arguments."));
4305796c8dcSSimon Schubert 	}
4315796c8dcSSimon Schubert       /* Skip the closing paren.  */
4325796c8dcSSimon Schubert       ++exp;
4335796c8dcSSimon Schubert       skip_ws (&exp);
4345796c8dcSSimon Schubert 
4355796c8dcSSimon Schubert       macro_define_function (macro_main (macro_user_macros), -1, name,
4365796c8dcSSimon Schubert 			     new_macro.argc, (const char **) new_macro.argv,
4375796c8dcSSimon Schubert 			     exp);
4385796c8dcSSimon Schubert     }
4395796c8dcSSimon Schubert   else
4405796c8dcSSimon Schubert     {
4415796c8dcSSimon Schubert       skip_ws (&exp);
4425796c8dcSSimon Schubert       macro_define_object (macro_main (macro_user_macros), -1, name, exp);
4435796c8dcSSimon Schubert     }
4445796c8dcSSimon Schubert 
4455796c8dcSSimon Schubert   do_cleanups (cleanup_chain);
4465796c8dcSSimon Schubert }
4475796c8dcSSimon Schubert 
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert static void
macro_undef_command(char * exp,int from_tty)4505796c8dcSSimon Schubert macro_undef_command (char *exp, int from_tty)
4515796c8dcSSimon Schubert {
4525796c8dcSSimon Schubert   char *name;
4535796c8dcSSimon Schubert 
4545796c8dcSSimon Schubert   if (!exp)
4555796c8dcSSimon Schubert     error (_("usage: macro undef NAME"));
4565796c8dcSSimon Schubert 
4575796c8dcSSimon Schubert   skip_ws (&exp);
4585796c8dcSSimon Schubert   name = extract_identifier (&exp, 0);
4595796c8dcSSimon Schubert   if (! name)
4605796c8dcSSimon Schubert     error (_("Invalid macro name."));
4615796c8dcSSimon Schubert   macro_undef (macro_main (macro_user_macros), -1, name);
4625796c8dcSSimon Schubert   xfree (name);
4635796c8dcSSimon Schubert }
4645796c8dcSSimon Schubert 
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert static void
print_one_macro(const char * name,const struct macro_definition * macro,struct macro_source_file * source,int line,void * ignore)4675796c8dcSSimon Schubert print_one_macro (const char *name, const struct macro_definition *macro,
468a45ae5f8SJohn Marino 		 struct macro_source_file *source, int line,
4695796c8dcSSimon Schubert 		 void *ignore)
4705796c8dcSSimon Schubert {
4715796c8dcSSimon Schubert   fprintf_filtered (gdb_stdout, "macro define %s", name);
4725796c8dcSSimon Schubert   if (macro->kind == macro_function_like)
4735796c8dcSSimon Schubert     {
4745796c8dcSSimon Schubert       int i;
475cf7f2e2dSJohn Marino 
4765796c8dcSSimon Schubert       fprintf_filtered (gdb_stdout, "(");
4775796c8dcSSimon Schubert       for (i = 0; i < macro->argc; ++i)
4785796c8dcSSimon Schubert 	fprintf_filtered (gdb_stdout, "%s%s", (i > 0) ? ", " : "",
4795796c8dcSSimon Schubert 			  macro->argv[i]);
4805796c8dcSSimon Schubert       fprintf_filtered (gdb_stdout, ")");
4815796c8dcSSimon Schubert     }
4825796c8dcSSimon Schubert   fprintf_filtered (gdb_stdout, " %s\n", macro->replacement);
4835796c8dcSSimon Schubert }
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert 
4865796c8dcSSimon Schubert static void
macro_list_command(char * exp,int from_tty)4875796c8dcSSimon Schubert macro_list_command (char *exp, int from_tty)
4885796c8dcSSimon Schubert {
4895796c8dcSSimon Schubert   macro_for_each (macro_user_macros, print_one_macro, NULL);
4905796c8dcSSimon Schubert }
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert /* Initializing the `macrocmd' module.  */
4945796c8dcSSimon Schubert 
4955796c8dcSSimon Schubert extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
4965796c8dcSSimon Schubert 
4975796c8dcSSimon Schubert void
_initialize_macrocmd(void)4985796c8dcSSimon Schubert _initialize_macrocmd (void)
4995796c8dcSSimon Schubert {
5005796c8dcSSimon Schubert   /* We introduce a new command prefix, `macro', under which we'll put
5015796c8dcSSimon Schubert      the various commands for working with preprocessor macros.  */
5025796c8dcSSimon Schubert   add_prefix_cmd ("macro", class_info, macro_command,
5035796c8dcSSimon Schubert 		  _("Prefix for commands dealing with C preprocessor macros."),
5045796c8dcSSimon Schubert 		  &macrolist, "macro ", 0, &cmdlist);
5055796c8dcSSimon Schubert 
5065796c8dcSSimon Schubert   add_cmd ("expand", no_class, macro_expand_command, _("\
5075796c8dcSSimon Schubert Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
5085796c8dcSSimon Schubert Show the expanded expression."),
5095796c8dcSSimon Schubert 	   &macrolist);
5105796c8dcSSimon Schubert   add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
5115796c8dcSSimon Schubert   add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
5125796c8dcSSimon Schubert Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
5135796c8dcSSimon Schubert Show the expanded expression.\n\
5145796c8dcSSimon Schubert \n\
5155796c8dcSSimon Schubert This command differs from `macro expand' in that it only expands macro\n\
5165796c8dcSSimon Schubert invocations that appear directly in EXPRESSION; if expanding a macro\n\
5175796c8dcSSimon Schubert introduces further macro invocations, those are left unexpanded.\n\
5185796c8dcSSimon Schubert \n\
5195796c8dcSSimon Schubert `macro expand-once' helps you see how a particular macro expands,\n\
5205796c8dcSSimon Schubert whereas `macro expand' shows you how all the macros involved in an\n\
5215796c8dcSSimon Schubert expression work together to yield a pre-processed expression."),
5225796c8dcSSimon Schubert 	   &macrolist);
5235796c8dcSSimon Schubert   add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
5245796c8dcSSimon Schubert 
5255796c8dcSSimon Schubert   add_cmd ("macro", no_class, info_macro_command,
526a45ae5f8SJohn Marino 	   _("Show the definition of MACRO, and it's source location.\n\
527a45ae5f8SJohn Marino Usage: info macro [-a|-all] [--] MACRO\n\
528a45ae5f8SJohn Marino Options: \n\
529a45ae5f8SJohn Marino   -a, --all    Output all definitions of MACRO in the current compilation\
530a45ae5f8SJohn Marino  unit.\n\
531a45ae5f8SJohn Marino   --           Specify the end of arguments and the beginning of the MACRO."),
532a45ae5f8SJohn Marino 
533a45ae5f8SJohn Marino 	   &infolist);
534a45ae5f8SJohn Marino 
535a45ae5f8SJohn Marino   add_cmd ("macros", no_class, info_macros_command,
536a45ae5f8SJohn Marino 	   _("Show the definitions of all macros at LINESPEC, or the current \
537a45ae5f8SJohn Marino source location.\n\
538a45ae5f8SJohn Marino Usage: info macros [LINESPEC]"),
5395796c8dcSSimon Schubert 	   &infolist);
5405796c8dcSSimon Schubert 
5415796c8dcSSimon Schubert   add_cmd ("define", no_class, macro_define_command, _("\
5425796c8dcSSimon Schubert Define a new C/C++ preprocessor macro.\n\
5435796c8dcSSimon Schubert The GDB command `macro define DEFINITION' is equivalent to placing a\n\
5445796c8dcSSimon Schubert preprocessor directive of the form `#define DEFINITION' such that the\n\
5455796c8dcSSimon Schubert definition is visible in all the inferior's source files.\n\
5465796c8dcSSimon Schubert For example:\n\
5475796c8dcSSimon Schubert   (gdb) macro define PI (3.1415926)\n\
5485796c8dcSSimon Schubert   (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
5495796c8dcSSimon Schubert 	   &macrolist);
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert   add_cmd ("undef", no_class, macro_undef_command, _("\
5525796c8dcSSimon Schubert Remove the definition of the C/C++ preprocessor macro with the given name."),
5535796c8dcSSimon Schubert 	   &macrolist);
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert   add_cmd ("list", no_class, macro_list_command,
5565796c8dcSSimon Schubert 	   _("List all the macros defined using the `macro define' command."),
5575796c8dcSSimon Schubert 	   &macrolist);
5585796c8dcSSimon Schubert }
559