Home | History | Annotate | Line # | Download | only in gdb
macroscope.c revision 1.1.1.8
      1 /* Functions for deciding which macros are currently in scope.
      2    Copyright (C) 2002-2024 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 "macroscope.h"
     22 #include "symtab.h"
     23 #include "source.h"
     24 #include "target.h"
     25 #include "frame.h"
     26 #include "inferior.h"
     27 #include "complaints.h"
     28 
     29 /* A table of user-defined macros.  Unlike the macro tables used for
     30    symtabs, this one uses xmalloc for all its allocation, not an
     31    obstack, and it doesn't bcache anything; it just xmallocs things.  So
     32    it's perfectly possible to remove things from this, or redefine
     33    things.  */
     34 struct macro_table *macro_user_macros;
     35 
     36 
     37 gdb::unique_xmalloc_ptr<struct macro_scope>
     38 sal_macro_scope (struct symtab_and_line sal)
     39 {
     40   struct macro_source_file *main_file, *inclusion;
     41   struct compunit_symtab *cust;
     42 
     43   if (sal.symtab == NULL)
     44     return NULL;
     45 
     46   cust = sal.symtab->compunit ();
     47   if (cust->macro_table () == NULL)
     48     return NULL;
     49 
     50   gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope));
     51 
     52   main_file = macro_main (cust->macro_table ());
     53   inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename_for_id);
     54 
     55   if (inclusion)
     56     {
     57       ms->file = inclusion;
     58       ms->line = sal.line;
     59     }
     60   else
     61     {
     62       /* There are, unfortunately, cases where a compilation unit can
     63 	 have a symtab for a source file that doesn't appear in the
     64 	 macro table.  For example, at the moment, Dwarf doesn't have
     65 	 any way in the .debug_macinfo section to describe the effect
     66 	 of #line directives, so if you debug a YACC parser you'll get
     67 	 a macro table which only mentions the .c files generated by
     68 	 YACC, but symtabs that mention the .y files consumed by YACC.
     69 
     70 	 In the long run, we should extend the Dwarf macro info
     71 	 representation to handle #line directives, and get GCC to
     72 	 emit it.
     73 
     74 	 For the time being, though, we'll just treat these as
     75 	 occurring at the end of the main source file.  */
     76       ms->file = main_file;
     77       ms->line = -1;
     78 
     79       complaint (_("symtab found for `%s', but that file\n"
     80 		 "is not covered in the compilation unit's macro information"),
     81 		 symtab_to_filename_for_display (sal.symtab));
     82     }
     83 
     84   return ms;
     85 }
     86 
     87 
     88 gdb::unique_xmalloc_ptr<struct macro_scope>
     89 user_macro_scope (void)
     90 {
     91   gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope));
     92   ms->file = macro_main (macro_user_macros);
     93   ms->line = -1;
     94   return ms;
     95 }
     96 
     97 gdb::unique_xmalloc_ptr<struct macro_scope>
     98 default_macro_scope (void)
     99 {
    100   struct symtab_and_line sal;
    101   gdb::unique_xmalloc_ptr<struct macro_scope> ms;
    102   frame_info_ptr frame;
    103   CORE_ADDR pc;
    104 
    105   /* If there's a selected frame, use its PC.  */
    106   frame = deprecated_safe_get_selected_frame ();
    107   if (frame && get_frame_pc_if_available (frame, &pc))
    108     sal = find_pc_line (pc, 0);
    109 
    110   /* Fall back to the current listing position.  */
    111   else
    112     {
    113       /* Don't call select_source_symtab here.  That can raise an
    114 	 error if symbols aren't loaded, but GDB calls the expression
    115 	 evaluator in all sorts of contexts.
    116 
    117 	 For example, commands like `set width' call the expression
    118 	 evaluator to evaluate their numeric arguments.  If the
    119 	 current language is C, then that may call this function to
    120 	 choose a scope for macro expansion.  If you don't have any
    121 	 symbol files loaded, then get_current_or_default would raise an
    122 	 error.  But `set width' shouldn't raise an error just because
    123 	 it can't decide which scope to macro-expand its argument in.  */
    124       struct symtab_and_line cursal
    125 	= get_current_source_symtab_and_line ();
    126 
    127       sal.symtab = cursal.symtab;
    128       sal.line = cursal.line;
    129     }
    130 
    131   ms = sal_macro_scope (sal);
    132   if (! ms)
    133     ms = user_macro_scope ();
    134 
    135   return ms;
    136 }
    137 
    138 
    139 /* Look up the definition of the macro named NAME in scope at the source
    140    location given by BATON, which must be a pointer to a `struct
    141    macro_scope' structure.  */
    142 struct macro_definition *
    143 standard_macro_lookup (const char *name, const macro_scope &ms)
    144 {
    145   /* Give user-defined macros priority over all others.  */
    146   macro_definition *result
    147     = macro_lookup_definition (macro_main (macro_user_macros), -1, name);
    148 
    149   if (result == nullptr)
    150     result = macro_lookup_definition (ms.file, ms.line, name);
    151 
    152   return result;
    153 }
    154 
    155 void _initialize_macroscope ();
    156 void
    157 _initialize_macroscope ()
    158 {
    159   macro_user_macros = new_macro_table (NULL, NULL, NULL);
    160   macro_set_main (macro_user_macros, "<user-defined>");
    161   macro_allow_redefinitions (macro_user_macros);
    162 }
    163