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