15796c8dcSSimon Schubert /* Functions for deciding which macros are currently in scope. 2*cf7f2e2dSJohn Marino Copyright (C) 2002, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 35796c8dcSSimon Schubert Contributed by Red Hat, Inc. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert This file is part of GDB. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 105796c8dcSSimon Schubert (at your option) any later version. 115796c8dcSSimon Schubert 125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155796c8dcSSimon Schubert GNU General Public License for more details. 165796c8dcSSimon Schubert 175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert #include "defs.h" 215796c8dcSSimon Schubert 225796c8dcSSimon Schubert #include "macroscope.h" 235796c8dcSSimon Schubert #include "symtab.h" 245796c8dcSSimon Schubert #include "source.h" 255796c8dcSSimon Schubert #include "target.h" 265796c8dcSSimon Schubert #include "frame.h" 275796c8dcSSimon Schubert #include "inferior.h" 285796c8dcSSimon Schubert #include "complaints.h" 295796c8dcSSimon Schubert 305796c8dcSSimon Schubert /* A table of user-defined macros. Unlike the macro tables used for 315796c8dcSSimon Schubert symtabs, this one uses xmalloc for all its allocation, not an 325796c8dcSSimon Schubert obstack, and it doesn't bcache anything; it just xmallocs things. So 335796c8dcSSimon Schubert it's perfectly possible to remove things from this, or redefine 345796c8dcSSimon Schubert things. */ 355796c8dcSSimon Schubert struct macro_table *macro_user_macros; 365796c8dcSSimon Schubert 375796c8dcSSimon Schubert 385796c8dcSSimon Schubert struct macro_scope * 395796c8dcSSimon Schubert sal_macro_scope (struct symtab_and_line sal) 405796c8dcSSimon Schubert { 415796c8dcSSimon Schubert struct macro_source_file *main_file, *inclusion; 425796c8dcSSimon Schubert struct macro_scope *ms; 435796c8dcSSimon Schubert 445796c8dcSSimon Schubert if (! sal.symtab 455796c8dcSSimon Schubert || ! sal.symtab->macro_table) 465796c8dcSSimon Schubert return 0; 475796c8dcSSimon Schubert 485796c8dcSSimon Schubert ms = (struct macro_scope *) xmalloc (sizeof (*ms)); 495796c8dcSSimon Schubert 505796c8dcSSimon Schubert main_file = macro_main (sal.symtab->macro_table); 515796c8dcSSimon Schubert inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename); 525796c8dcSSimon Schubert 535796c8dcSSimon Schubert if (inclusion) 545796c8dcSSimon Schubert { 555796c8dcSSimon Schubert ms->file = inclusion; 565796c8dcSSimon Schubert ms->line = sal.line; 575796c8dcSSimon Schubert } 585796c8dcSSimon Schubert else 595796c8dcSSimon Schubert { 605796c8dcSSimon Schubert /* There are, unfortunately, cases where a compilation unit can 615796c8dcSSimon Schubert have a symtab for a source file that doesn't appear in the 625796c8dcSSimon Schubert macro table. For example, at the moment, Dwarf doesn't have 635796c8dcSSimon Schubert any way in the .debug_macinfo section to describe the effect 645796c8dcSSimon Schubert of #line directives, so if you debug a YACC parser you'll get 655796c8dcSSimon Schubert a macro table which only mentions the .c files generated by 665796c8dcSSimon Schubert YACC, but symtabs that mention the .y files consumed by YACC. 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert In the long run, we should extend the Dwarf macro info 695796c8dcSSimon Schubert representation to handle #line directives, and get GCC to 705796c8dcSSimon Schubert emit it. 715796c8dcSSimon Schubert 725796c8dcSSimon Schubert For the time being, though, we'll just treat these as 735796c8dcSSimon Schubert occurring at the end of the main source file. */ 745796c8dcSSimon Schubert ms->file = main_file; 755796c8dcSSimon Schubert ms->line = -1; 765796c8dcSSimon Schubert 775796c8dcSSimon Schubert complaint (&symfile_complaints, 785796c8dcSSimon Schubert _("symtab found for `%s', but that file\n" 795796c8dcSSimon Schubert "is not covered in the compilation unit's macro information"), 805796c8dcSSimon Schubert sal.symtab->filename); 815796c8dcSSimon Schubert } 825796c8dcSSimon Schubert 835796c8dcSSimon Schubert return ms; 845796c8dcSSimon Schubert } 855796c8dcSSimon Schubert 865796c8dcSSimon Schubert 875796c8dcSSimon Schubert struct macro_scope * 885796c8dcSSimon Schubert user_macro_scope (void) 895796c8dcSSimon Schubert { 905796c8dcSSimon Schubert struct macro_scope *ms; 91*cf7f2e2dSJohn Marino 925796c8dcSSimon Schubert ms = XNEW (struct macro_scope); 935796c8dcSSimon Schubert ms->file = macro_main (macro_user_macros); 945796c8dcSSimon Schubert ms->line = -1; 955796c8dcSSimon Schubert return ms; 965796c8dcSSimon Schubert } 975796c8dcSSimon Schubert 985796c8dcSSimon Schubert struct macro_scope * 995796c8dcSSimon Schubert default_macro_scope (void) 1005796c8dcSSimon Schubert { 1015796c8dcSSimon Schubert struct symtab_and_line sal; 1025796c8dcSSimon Schubert struct macro_scope *ms; 1035796c8dcSSimon Schubert struct frame_info *frame; 1045796c8dcSSimon Schubert 1055796c8dcSSimon Schubert /* If there's a selected frame, use its PC. */ 1065796c8dcSSimon Schubert frame = deprecated_safe_get_selected_frame (); 1075796c8dcSSimon Schubert if (frame) 1085796c8dcSSimon Schubert sal = find_pc_line (get_frame_pc (frame), 0); 1095796c8dcSSimon Schubert 1105796c8dcSSimon Schubert /* Fall back to the current listing position. */ 1115796c8dcSSimon Schubert else 1125796c8dcSSimon Schubert { 1135796c8dcSSimon Schubert /* Don't call select_source_symtab here. That can raise an 1145796c8dcSSimon Schubert error if symbols aren't loaded, but GDB calls the expression 1155796c8dcSSimon Schubert evaluator in all sorts of contexts. 1165796c8dcSSimon Schubert 1175796c8dcSSimon Schubert For example, commands like `set width' call the expression 1185796c8dcSSimon Schubert evaluator to evaluate their numeric arguments. If the 1195796c8dcSSimon Schubert current language is C, then that may call this function to 1205796c8dcSSimon Schubert choose a scope for macro expansion. If you don't have any 1215796c8dcSSimon Schubert symbol files loaded, then get_current_or_default would raise an 1225796c8dcSSimon Schubert error. But `set width' shouldn't raise an error just because 1235796c8dcSSimon Schubert it can't decide which scope to macro-expand its argument in. */ 1245796c8dcSSimon Schubert struct symtab_and_line cursal = 1255796c8dcSSimon Schubert get_current_source_symtab_and_line (); 1265796c8dcSSimon Schubert 1275796c8dcSSimon Schubert sal.symtab = cursal.symtab; 1285796c8dcSSimon Schubert sal.line = cursal.line; 1295796c8dcSSimon Schubert } 1305796c8dcSSimon Schubert 1315796c8dcSSimon Schubert ms = sal_macro_scope (sal); 1325796c8dcSSimon Schubert if (! ms) 1335796c8dcSSimon Schubert ms = user_macro_scope (); 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert return ms; 1365796c8dcSSimon Schubert } 1375796c8dcSSimon Schubert 1385796c8dcSSimon Schubert 1395796c8dcSSimon Schubert /* Look up the definition of the macro named NAME in scope at the source 1405796c8dcSSimon Schubert location given by BATON, which must be a pointer to a `struct 1415796c8dcSSimon Schubert macro_scope' structure. */ 1425796c8dcSSimon Schubert struct macro_definition * 1435796c8dcSSimon Schubert standard_macro_lookup (const char *name, void *baton) 1445796c8dcSSimon Schubert { 1455796c8dcSSimon Schubert struct macro_scope *ms = (struct macro_scope *) baton; 1465796c8dcSSimon Schubert struct macro_definition *result; 1475796c8dcSSimon Schubert 1485796c8dcSSimon Schubert /* Give user-defined macros priority over all others. */ 1495796c8dcSSimon Schubert result = macro_lookup_definition (macro_main (macro_user_macros), -1, name); 1505796c8dcSSimon Schubert if (! result) 1515796c8dcSSimon Schubert result = macro_lookup_definition (ms->file, ms->line, name); 1525796c8dcSSimon Schubert return result; 1535796c8dcSSimon Schubert } 1545796c8dcSSimon Schubert 1555796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes. */ 1565796c8dcSSimon Schubert extern initialize_file_ftype _initialize_macroscope; 1575796c8dcSSimon Schubert 1585796c8dcSSimon Schubert void 1595796c8dcSSimon Schubert _initialize_macroscope (void) 1605796c8dcSSimon Schubert { 1615796c8dcSSimon Schubert macro_user_macros = new_macro_table (0, 0); 1625796c8dcSSimon Schubert macro_set_main (macro_user_macros, "<user-defined>"); 1635796c8dcSSimon Schubert macro_allow_redefinitions (macro_user_macros); 1645796c8dcSSimon Schubert } 165