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