gdb/macroscope.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Functions for deciding which macros are currently in scope.
  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3.    Contributed by Red Hat, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"

  16. #include "macroscope.h"
  17. #include "symtab.h"
  18. #include "source.h"
  19. #include "target.h"
  20. #include "frame.h"
  21. #include "inferior.h"
  22. #include "complaints.h"

  23. /* A table of user-defined macros.  Unlike the macro tables used for
  24.    symtabs, this one uses xmalloc for all its allocation, not an
  25.    obstack, and it doesn't bcache anything; it just xmallocs things.  So
  26.    it's perfectly possible to remove things from this, or redefine
  27.    things.  */
  28. struct macro_table *macro_user_macros;


  29. struct macro_scope *
  30. sal_macro_scope (struct symtab_and_line sal)
  31. {
  32.   struct macro_source_file *main_file, *inclusion;
  33.   struct macro_scope *ms;
  34.   struct compunit_symtab *cust;

  35.   if (sal.symtab == NULL)
  36.     return NULL;
  37.   cust = SYMTAB_COMPUNIT (sal.symtab);
  38.   if (COMPUNIT_MACRO_TABLE (cust) == NULL)
  39.     return NULL;

  40.   ms = (struct macro_scope *) xmalloc (sizeof (*ms));

  41.   main_file = macro_main (COMPUNIT_MACRO_TABLE (cust));
  42.   inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename);

  43.   if (inclusion)
  44.     {
  45.       ms->file = inclusion;
  46.       ms->line = sal.line;
  47.     }
  48.   else
  49.     {
  50.       /* There are, unfortunately, cases where a compilation unit can
  51.          have a symtab for a source file that doesn't appear in the
  52.          macro table.  For example, at the moment, Dwarf doesn't have
  53.          any way in the .debug_macinfo section to describe the effect
  54.          of #line directives, so if you debug a YACC parser you'll get
  55.          a macro table which only mentions the .c files generated by
  56.          YACC, but symtabs that mention the .y files consumed by YACC.

  57.          In the long run, we should extend the Dwarf macro info
  58.          representation to handle #line directives, and get GCC to
  59.          emit it.

  60.          For the time being, though, we'll just treat these as
  61.          occurring at the end of the main source file.  */
  62.       ms->file = main_file;
  63.       ms->line = -1;

  64.       complaint (&symfile_complaints,
  65.                  _("symtab found for `%s', but that file\n"
  66.                  "is not covered in the compilation unit's macro information"),
  67.                  symtab_to_filename_for_display (sal.symtab));
  68.     }

  69.   return ms;
  70. }


  71. struct macro_scope *
  72. user_macro_scope (void)
  73. {
  74.   struct macro_scope *ms;

  75.   ms = XNEW (struct macro_scope);
  76.   ms->file = macro_main (macro_user_macros);
  77.   ms->line = -1;
  78.   return ms;
  79. }

  80. struct macro_scope *
  81. default_macro_scope (void)
  82. {
  83.   struct symtab_and_line sal;
  84.   struct macro_scope *ms;
  85.   struct frame_info *frame;
  86.   CORE_ADDR pc;

  87.   /* If there's a selected frame, use its PC.  */
  88.   frame = deprecated_safe_get_selected_frame ();
  89.   if (frame && get_frame_pc_if_available (frame, &pc))
  90.     sal = find_pc_line (pc, 0);

  91.   /* Fall back to the current listing position.  */
  92.   else
  93.     {
  94.       /* Don't call select_source_symtab here.  That can raise an
  95.          error if symbols aren't loaded, but GDB calls the expression
  96.          evaluator in all sorts of contexts.

  97.          For example, commands like `set width' call the expression
  98.          evaluator to evaluate their numeric arguments.  If the
  99.          current language is C, then that may call this function to
  100.          choose a scope for macro expansion.  If you don't have any
  101.          symbol files loaded, then get_current_or_default would raise an
  102.          error.  But `set width' shouldn't raise an error just because
  103.          it can't decide which scope to macro-expand its argument in.  */
  104.       struct symtab_and_line cursal =
  105.                               get_current_source_symtab_and_line ();

  106.       sal.symtab = cursal.symtab;
  107.       sal.line = cursal.line;
  108.     }

  109.   ms = sal_macro_scope (sal);
  110.   if (! ms)
  111.     ms = user_macro_scope ();

  112.   return ms;
  113. }


  114. /* Look up the definition of the macro named NAME in scope at the source
  115.    location given by BATON, which must be a pointer to a `struct
  116.    macro_scope' structure.  */
  117. struct macro_definition *
  118. standard_macro_lookup (const char *name, void *baton)
  119. {
  120.   struct macro_scope *ms = (struct macro_scope *) baton;
  121.   struct macro_definition *result;

  122.   /* Give user-defined macros priority over all others.  */
  123.   result = macro_lookup_definition (macro_main (macro_user_macros), -1, name);
  124.   if (! result)
  125.     result = macro_lookup_definition (ms->file, ms->line, name);
  126.   return result;
  127. }

  128. /* Provide a prototype to silence -Wmissing-prototypes.  */
  129. extern initialize_file_ftype _initialize_macroscope;

  130. void
  131. _initialize_macroscope (void)
  132. {
  133.   macro_user_macros = new_macro_table (NULL, NULL, NULL);
  134.   macro_set_main (macro_user_macros, "<user-defined>");
  135.   macro_allow_redefinitions (macro_user_macros);
  136. }