1/* C preprocessor macro expansion commands for GDB.
2   Copyright (C) 2002, 2007 Free Software Foundation, Inc.
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
9   the Free Software Foundation; either version 3 of the License, or
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
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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"
27
28
29/* The `macro' prefix command.  */
30
31static struct cmd_list_element *macrolist;
32
33static void
34macro_command (char *arg, int from_tty)
35{
36  printf_unfiltered
37    ("\"macro\" must be followed by the name of a macro command.\n");
38  help_list (macrolist, "macro ", -1, gdb_stdout);
39}
40
41
42
43/* Macro expansion commands.  */
44
45
46static void
47macro_expand_command (char *exp, int from_tty)
48{
49  struct macro_scope *ms = NULL;
50  char *expanded = NULL;
51  struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
52  make_cleanup (free_current_contents, &expanded);
53
54  /* You know, when the user doesn't specify any expression, it would be
55     really cool if this defaulted to the last expression evaluated.
56     Then it would be easy to ask, "Hey, what did I just evaluate?"  But
57     at the moment, the `print' commands don't save the last expression
58     evaluated, just its value.  */
59  if (! exp || ! *exp)
60    error (_("You must follow the `macro expand' command with the"
61           " expression you\n"
62           "want to expand."));
63
64  ms = default_macro_scope ();
65  if (ms)
66    {
67      expanded = macro_expand (exp, standard_macro_lookup, ms);
68      fputs_filtered ("expands to: ", gdb_stdout);
69      fputs_filtered (expanded, gdb_stdout);
70      fputs_filtered ("\n", gdb_stdout);
71    }
72  else
73    fputs_filtered ("GDB has no preprocessor macro information for "
74                    "that code.\n",
75                    gdb_stdout);
76
77  do_cleanups (cleanup_chain);
78  return;
79}
80
81
82static void
83macro_expand_once_command (char *exp, int from_tty)
84{
85  struct macro_scope *ms = NULL;
86  char *expanded = NULL;
87  struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
88  make_cleanup (free_current_contents, &expanded);
89
90  /* You know, when the user doesn't specify any expression, it would be
91     really cool if this defaulted to the last expression evaluated.
92     And it should set the once-expanded text as the new `last
93     expression'.  That way, you could just hit return over and over and
94     see the expression expanded one level at a time.  */
95  if (! exp || ! *exp)
96    error (_("You must follow the `macro expand-once' command with"
97           " the expression\n"
98           "you want to expand."));
99
100  ms = default_macro_scope ();
101  if (ms)
102    {
103      expanded = macro_expand_once (exp, standard_macro_lookup, ms);
104      fputs_filtered ("expands to: ", gdb_stdout);
105      fputs_filtered (expanded, gdb_stdout);
106      fputs_filtered ("\n", gdb_stdout);
107    }
108  else
109    fputs_filtered ("GDB has no preprocessor macro information for "
110                    "that code.\n",
111                    gdb_stdout);
112
113  do_cleanups (cleanup_chain);
114  return;
115}
116
117
118static void
119show_pp_source_pos (struct ui_file *stream,
120                    struct macro_source_file *file,
121                    int line)
122{
123  fprintf_filtered (stream, "%s:%d\n", file->filename, line);
124
125  while (file->included_by)
126    {
127      fprintf_filtered (gdb_stdout, "  included at %s:%d\n",
128                        file->included_by->filename,
129                        file->included_at_line);
130      file = file->included_by;
131    }
132}
133
134
135static void
136info_macro_command (char *name, int from_tty)
137{
138  struct macro_scope *ms = NULL;
139  struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
140  struct macro_definition *d;
141
142  if (! name || ! *name)
143    error (_("You must follow the `info macro' command with the name"
144           " of the macro\n"
145           "whose definition you want to see."));
146
147  ms = default_macro_scope ();
148  if (! ms)
149    error (_("GDB has no preprocessor macro information for that code."));
150
151  d = macro_lookup_definition (ms->file, ms->line, name);
152  if (d)
153    {
154      int line;
155      struct macro_source_file *file
156        = macro_definition_location (ms->file, ms->line, name, &line);
157
158      fprintf_filtered (gdb_stdout, "Defined at ");
159      show_pp_source_pos (gdb_stdout, file, line);
160      fprintf_filtered (gdb_stdout, "#define %s", name);
161      if (d->kind == macro_function_like)
162        {
163          int i;
164
165          fputs_filtered ("(", gdb_stdout);
166          for (i = 0; i < d->argc; i++)
167            {
168              fputs_filtered (d->argv[i], gdb_stdout);
169              if (i + 1 < d->argc)
170                fputs_filtered (", ", gdb_stdout);
171            }
172          fputs_filtered (")", gdb_stdout);
173        }
174      fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
175    }
176  else
177    {
178      fprintf_filtered (gdb_stdout,
179                        "The symbol `%s' has no definition as a C/C++"
180                        " preprocessor macro\n"
181                        "at ", name);
182      show_pp_source_pos (gdb_stdout, ms->file, ms->line);
183    }
184
185  do_cleanups (cleanup_chain);
186}
187
188
189
190/* User-defined macros.  */
191
192/* A table of user-defined macros.  Unlike the macro tables used for
193   symtabs, this one uses xmalloc for all its allocation, not an
194   obstack, and it doesn't bcache anything; it just xmallocs things.  So
195   it's perfectly possible to remove things from this, or redefine
196   things.  */
197static struct macro_table *user_macros;
198
199static void
200macro_define_command (char *exp, int from_tty)
201{
202  error (_("Command not implemented yet."));
203}
204
205
206static void
207macro_undef_command (char *exp, int from_tty)
208{
209  error (_("Command not implemented yet."));
210}
211
212
213static void
214macro_list_command (char *exp, int from_tty)
215{
216  error (_("Command not implemented yet."));
217}
218
219
220
221/* Initializing the `macrocmd' module.  */
222
223extern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
224
225void
226_initialize_macrocmd (void)
227{
228  struct cmd_list_element *c;
229
230  /* We introduce a new command prefix, `macro', under which we'll put
231     the various commands for working with preprocessor macros.  */
232  add_prefix_cmd ("macro", class_info, macro_command,
233		  _("Prefix for commands dealing with C preprocessor macros."),
234		  &macrolist, "macro ", 0, &cmdlist);
235
236  add_cmd ("expand", no_class, macro_expand_command, _("\
237Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n\
238Show the expanded expression."),
239	   &macrolist);
240  add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
241  add_cmd ("expand-once", no_class, macro_expand_once_command, _("\
242Expand C/C++ preprocessor macro invocations appearing directly in EXPRESSION.\n\
243Show the expanded expression.\n\
244\n\
245This command differs from `macro expand' in that it only expands macro\n\
246invocations that appear directly in EXPRESSION; if expanding a macro\n\
247introduces further macro invocations, those are left unexpanded.\n\
248\n\
249`macro expand-once' helps you see how a particular macro expands,\n\
250whereas `macro expand' shows you how all the macros involved in an\n\
251expression work together to yield a pre-processed expression."),
252	   &macrolist);
253  add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
254
255  add_cmd ("macro", no_class, info_macro_command,
256	   _("Show the definition of MACRO, and its source location."),
257	   &infolist);
258
259  add_cmd ("define", no_class, macro_define_command, _("\
260Define a new C/C++ preprocessor macro.\n\
261The GDB command `macro define DEFINITION' is equivalent to placing a\n\
262preprocessor directive of the form `#define DEFINITION' such that the\n\
263definition is visible in all the inferior's source files.\n\
264For example:\n\
265  (gdb) macro define PI (3.1415926)\n\
266  (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))"),
267	   &macrolist);
268
269  add_cmd ("undef", no_class, macro_undef_command, _("\
270Remove the definition of the C/C++ preprocessor macro with the given name."),
271	   &macrolist);
272
273  add_cmd ("list", no_class, macro_list_command,
274	   _("List all the macros defined using the `macro define' command."),
275	   &macrolist);
276
277  user_macros = new_macro_table (0, 0);
278}
279