OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.2/] [gdb/] [macrocmd.c] - Blame information for rev 414

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.