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