1130803Smarcel/* C preprocessor macro expansion commands for GDB.
2130803Smarcel   Copyright 2002 Free Software Foundation, Inc.
3130803Smarcel   Contributed by Red Hat, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel
23130803Smarcel#include "defs.h"
24130803Smarcel#include "macrotab.h"
25130803Smarcel#include "macroexp.h"
26130803Smarcel#include "macroscope.h"
27130803Smarcel#include "command.h"
28130803Smarcel#include "gdbcmd.h"
29130803Smarcel
30130803Smarcel
31130803Smarcel/* The `macro' prefix command.  */
32130803Smarcel
33130803Smarcelstatic struct cmd_list_element *macrolist;
34130803Smarcel
35130803Smarcelstatic void
36130803Smarcelmacro_command (char *arg, int from_tty)
37130803Smarcel{
38130803Smarcel  printf_unfiltered
39130803Smarcel    ("\"macro\" must be followed by the name of a macro command.\n");
40130803Smarcel  help_list (macrolist, "macro ", -1, gdb_stdout);
41130803Smarcel}
42130803Smarcel
43130803Smarcel
44130803Smarcel
45130803Smarcel/* Macro expansion commands.  */
46130803Smarcel
47130803Smarcel
48130803Smarcelstatic void
49130803Smarcelmacro_expand_command (char *exp, int from_tty)
50130803Smarcel{
51130803Smarcel  struct macro_scope *ms = NULL;
52130803Smarcel  char *expanded = NULL;
53130803Smarcel  struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
54130803Smarcel  make_cleanup (free_current_contents, &expanded);
55130803Smarcel
56130803Smarcel  /* You know, when the user doesn't specify any expression, it would be
57130803Smarcel     really cool if this defaulted to the last expression evaluated.
58130803Smarcel     Then it would be easy to ask, "Hey, what did I just evaluate?"  But
59130803Smarcel     at the moment, the `print' commands don't save the last expression
60130803Smarcel     evaluated, just its value.  */
61130803Smarcel  if (! exp || ! *exp)
62130803Smarcel    error ("You must follow the `macro expand' command with the"
63130803Smarcel           " expression you\n"
64130803Smarcel           "want to expand.");
65130803Smarcel
66130803Smarcel  ms = default_macro_scope ();
67130803Smarcel  if (ms)
68130803Smarcel    {
69130803Smarcel      expanded = macro_expand (exp, standard_macro_lookup, ms);
70130803Smarcel      fputs_filtered ("expands to: ", gdb_stdout);
71130803Smarcel      fputs_filtered (expanded, gdb_stdout);
72130803Smarcel      fputs_filtered ("\n", gdb_stdout);
73130803Smarcel    }
74130803Smarcel  else
75130803Smarcel    fputs_filtered ("GDB has no preprocessor macro information for "
76130803Smarcel                    "that code.\n",
77130803Smarcel                    gdb_stdout);
78130803Smarcel
79130803Smarcel  do_cleanups (cleanup_chain);
80130803Smarcel  return;
81130803Smarcel}
82130803Smarcel
83130803Smarcel
84130803Smarcelstatic void
85130803Smarcelmacro_expand_once_command (char *exp, int from_tty)
86130803Smarcel{
87130803Smarcel  struct macro_scope *ms = NULL;
88130803Smarcel  char *expanded = NULL;
89130803Smarcel  struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
90130803Smarcel  make_cleanup (free_current_contents, &expanded);
91130803Smarcel
92130803Smarcel  /* You know, when the user doesn't specify any expression, it would be
93130803Smarcel     really cool if this defaulted to the last expression evaluated.
94130803Smarcel     And it should set the once-expanded text as the new `last
95130803Smarcel     expression'.  That way, you could just hit return over and over and
96130803Smarcel     see the expression expanded one level at a time.  */
97130803Smarcel  if (! exp || ! *exp)
98130803Smarcel    error ("You must follow the `macro expand-once' command with"
99130803Smarcel           " the expression\n"
100130803Smarcel           "you want to expand.");
101130803Smarcel
102130803Smarcel  ms = default_macro_scope ();
103130803Smarcel  if (ms)
104130803Smarcel    {
105130803Smarcel      expanded = macro_expand_once (exp, standard_macro_lookup, ms);
106130803Smarcel      fputs_filtered ("expands to: ", gdb_stdout);
107130803Smarcel      fputs_filtered (expanded, gdb_stdout);
108130803Smarcel      fputs_filtered ("\n", gdb_stdout);
109130803Smarcel    }
110130803Smarcel  else
111130803Smarcel    fputs_filtered ("GDB has no preprocessor macro information for "
112130803Smarcel                    "that code.\n",
113130803Smarcel                    gdb_stdout);
114130803Smarcel
115130803Smarcel  do_cleanups (cleanup_chain);
116130803Smarcel  return;
117130803Smarcel}
118130803Smarcel
119130803Smarcel
120130803Smarcelstatic void
121130803Smarcelshow_pp_source_pos (struct ui_file *stream,
122130803Smarcel                    struct macro_source_file *file,
123130803Smarcel                    int line)
124130803Smarcel{
125130803Smarcel  fprintf_filtered (stream, "%s:%d\n", file->filename, line);
126130803Smarcel
127130803Smarcel  while (file->included_by)
128130803Smarcel    {
129130803Smarcel      fprintf_filtered (gdb_stdout, "  included at %s:%d\n",
130130803Smarcel                        file->included_by->filename,
131130803Smarcel                        file->included_at_line);
132130803Smarcel      file = file->included_by;
133130803Smarcel    }
134130803Smarcel}
135130803Smarcel
136130803Smarcel
137130803Smarcelstatic void
138130803Smarcelinfo_macro_command (char *name, int from_tty)
139130803Smarcel{
140130803Smarcel  struct macro_scope *ms = NULL;
141130803Smarcel  struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &ms);
142130803Smarcel  struct macro_definition *d;
143130803Smarcel
144130803Smarcel  if (! name || ! *name)
145130803Smarcel    error ("You must follow the `info macro' command with the name"
146130803Smarcel           " of the macro\n"
147130803Smarcel           "whose definition you want to see.");
148130803Smarcel
149130803Smarcel  ms = default_macro_scope ();
150130803Smarcel  if (! ms)
151130803Smarcel    error ("GDB has no preprocessor macro information for that code.");
152130803Smarcel
153130803Smarcel  d = macro_lookup_definition (ms->file, ms->line, name);
154130803Smarcel  if (d)
155130803Smarcel    {
156130803Smarcel      int line;
157130803Smarcel      struct macro_source_file *file
158130803Smarcel        = macro_definition_location (ms->file, ms->line, name, &line);
159130803Smarcel
160130803Smarcel      fprintf_filtered (gdb_stdout, "Defined at ");
161130803Smarcel      show_pp_source_pos (gdb_stdout, file, line);
162130803Smarcel      fprintf_filtered (gdb_stdout, "#define %s", name);
163130803Smarcel      if (d->kind == macro_function_like)
164130803Smarcel        {
165130803Smarcel          int i;
166130803Smarcel
167130803Smarcel          fputs_filtered ("(", gdb_stdout);
168130803Smarcel          for (i = 0; i < d->argc; i++)
169130803Smarcel            {
170130803Smarcel              fputs_filtered (d->argv[i], gdb_stdout);
171130803Smarcel              if (i + 1 < d->argc)
172130803Smarcel                fputs_filtered (", ", gdb_stdout);
173130803Smarcel            }
174130803Smarcel          fputs_filtered (")", gdb_stdout);
175130803Smarcel        }
176130803Smarcel      fprintf_filtered (gdb_stdout, " %s\n", d->replacement);
177130803Smarcel    }
178130803Smarcel  else
179130803Smarcel    {
180130803Smarcel      fprintf_filtered (gdb_stdout,
181130803Smarcel                        "The symbol `%s' has no definition as a C/C++"
182130803Smarcel                        " preprocessor macro\n"
183130803Smarcel                        "at ", name);
184130803Smarcel      show_pp_source_pos (gdb_stdout, ms->file, ms->line);
185130803Smarcel    }
186130803Smarcel
187130803Smarcel  do_cleanups (cleanup_chain);
188130803Smarcel}
189130803Smarcel
190130803Smarcel
191130803Smarcel
192130803Smarcel/* User-defined macros.  */
193130803Smarcel
194130803Smarcel/* A table of user-defined macros.  Unlike the macro tables used for
195130803Smarcel   symtabs, this one uses xmalloc for all its allocation, not an
196130803Smarcel   obstack, and it doesn't bcache anything; it just xmallocs things.  So
197130803Smarcel   it's perfectly possible to remove things from this, or redefine
198130803Smarcel   things.  */
199130803Smarcelstatic struct macro_table *user_macros;
200130803Smarcel
201130803Smarcelstatic void
202130803Smarcelmacro_define_command (char *exp, int from_tty)
203130803Smarcel{
204130803Smarcel  error ("Command not implemented yet.");
205130803Smarcel}
206130803Smarcel
207130803Smarcel
208130803Smarcelstatic void
209130803Smarcelmacro_undef_command (char *exp, int from_tty)
210130803Smarcel{
211130803Smarcel  error ("Command not implemented yet.");
212130803Smarcel}
213130803Smarcel
214130803Smarcel
215130803Smarcelstatic void
216130803Smarcelmacro_list_command (char *exp, int from_tty)
217130803Smarcel{
218130803Smarcel  error ("Command not implemented yet.");
219130803Smarcel}
220130803Smarcel
221130803Smarcel
222130803Smarcel
223130803Smarcel/* Initializing the `macrocmd' module.  */
224130803Smarcel
225130803Smarcelextern initialize_file_ftype _initialize_macrocmd; /* -Wmissing-prototypes */
226130803Smarcel
227130803Smarcelvoid
228130803Smarcel_initialize_macrocmd (void)
229130803Smarcel{
230130803Smarcel  struct cmd_list_element *c;
231130803Smarcel
232130803Smarcel  /* We introduce a new command prefix, `macro', under which we'll put
233130803Smarcel     the various commands for working with preprocessor macros.  */
234130803Smarcel  add_prefix_cmd
235130803Smarcel    ("macro", class_info, macro_command,
236130803Smarcel     "Prefix for commands dealing with C preprocessor macros.",
237130803Smarcel     &macrolist, "macro ", 0, &cmdlist);
238130803Smarcel
239130803Smarcel  add_cmd
240130803Smarcel    ("expand", no_class, macro_expand_command,
241130803Smarcel     "Fully expand any C/C++ preprocessor macro invocations in EXPRESSION.\n"
242130803Smarcel     "Show the expanded expression.",
243130803Smarcel     &macrolist);
244130803Smarcel  add_alias_cmd ("exp", "expand", no_class, 1, &macrolist);
245130803Smarcel  add_cmd
246130803Smarcel    ("expand-once", no_class, macro_expand_once_command,
247130803Smarcel     "Expand C/C++ preprocessor macro invocations appearing directly in"
248130803Smarcel     " EXPRESSION.\n"
249130803Smarcel     "Show the expanded expression.\n"
250130803Smarcel     "\n"
251130803Smarcel     "This command differs from `macro expand' in that it only expands macro\n"
252130803Smarcel     "invocations that appear directly in EXPRESSION; if expanding a macro\n"
253130803Smarcel     "introduces further macro invocations, those are left unexpanded.\n"
254130803Smarcel     "\n"
255130803Smarcel     "`macro expand-once' helps you see how a particular macro expands,\n"
256130803Smarcel     "whereas `macro expand' shows you how all the macros involved in an\n"
257130803Smarcel     "expression work together to yield a pre-processed expression.",
258130803Smarcel     &macrolist);
259130803Smarcel  add_alias_cmd ("exp1", "expand-once", no_class, 1, &macrolist);
260130803Smarcel
261130803Smarcel  add_cmd
262130803Smarcel    ("macro", no_class, info_macro_command,
263130803Smarcel     "Show the definition of MACRO, and its source location.",
264130803Smarcel     &infolist);
265130803Smarcel
266130803Smarcel  add_cmd
267130803Smarcel    ("define", no_class, macro_define_command,
268130803Smarcel     "Define a new C/C++ preprocessor macro.\n"
269130803Smarcel     "The GDB command `macro define DEFINITION' is equivalent to placing a\n"
270130803Smarcel     "preprocessor directive of the form `#define DEFINITION' such that the\n"
271130803Smarcel     "definition is visible in all the inferior's source files.\n"
272130803Smarcel     "For example:\n"
273130803Smarcel     "  (gdb) macro define PI (3.1415926)\n"
274130803Smarcel     "  (gdb) macro define MIN(x,y) ((x) < (y) ? (x) : (y))",
275130803Smarcel     &macrolist);
276130803Smarcel
277130803Smarcel  add_cmd
278130803Smarcel    ("undef", no_class, macro_undef_command,
279130803Smarcel     "Remove the definition of the C/C++ preprocessor macro with the"
280130803Smarcel     " given name.",
281130803Smarcel     &macrolist);
282130803Smarcel
283130803Smarcel  add_cmd
284130803Smarcel    ("list", no_class, macro_list_command,
285130803Smarcel     "List all the macros defined using the `macro define' command.",
286130803Smarcel     &macrolist);
287130803Smarcel
288130803Smarcel  user_macros = new_macro_table (0, 0);
289130803Smarcel}
290