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