GDB (API)
/home/stan/gdb/src/gdb/macroscope.c
Go to the documentation of this file.
00001 /* Functions for deciding which macros are currently in scope.
00002    Copyright (C) 2002-2013 Free Software Foundation, Inc.
00003    Contributed by Red Hat, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 
00022 #include "macroscope.h"
00023 #include "symtab.h"
00024 #include "source.h"
00025 #include "target.h"
00026 #include "frame.h"
00027 #include "inferior.h"
00028 #include "complaints.h"
00029 
00030 /* A table of user-defined macros.  Unlike the macro tables used for
00031    symtabs, this one uses xmalloc for all its allocation, not an
00032    obstack, and it doesn't bcache anything; it just xmallocs things.  So
00033    it's perfectly possible to remove things from this, or redefine
00034    things.  */
00035 struct macro_table *macro_user_macros;
00036 
00037 
00038 struct macro_scope *
00039 sal_macro_scope (struct symtab_and_line sal)
00040 {
00041   struct macro_source_file *main_file, *inclusion;
00042   struct macro_scope *ms;
00043 
00044   if (! sal.symtab
00045       || ! sal.symtab->macro_table)
00046     return 0;
00047 
00048   ms = (struct macro_scope *) xmalloc (sizeof (*ms));
00049 
00050   main_file = macro_main (sal.symtab->macro_table);
00051   inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename);
00052 
00053   if (inclusion)
00054     {
00055       ms->file = inclusion;
00056       ms->line = sal.line;
00057     }
00058   else
00059     {
00060       /* There are, unfortunately, cases where a compilation unit can
00061          have a symtab for a source file that doesn't appear in the
00062          macro table.  For example, at the moment, Dwarf doesn't have
00063          any way in the .debug_macinfo section to describe the effect
00064          of #line directives, so if you debug a YACC parser you'll get
00065          a macro table which only mentions the .c files generated by
00066          YACC, but symtabs that mention the .y files consumed by YACC.
00067 
00068          In the long run, we should extend the Dwarf macro info
00069          representation to handle #line directives, and get GCC to
00070          emit it.
00071 
00072          For the time being, though, we'll just treat these as
00073          occurring at the end of the main source file.  */
00074       ms->file = main_file;
00075       ms->line = -1;
00076 
00077       complaint (&symfile_complaints,
00078                  _("symtab found for `%s', but that file\n"
00079                  "is not covered in the compilation unit's macro information"),
00080                  symtab_to_filename_for_display (sal.symtab));
00081     }
00082 
00083   return ms;
00084 }
00085 
00086 
00087 struct macro_scope *
00088 user_macro_scope (void)
00089 {
00090   struct macro_scope *ms;
00091 
00092   ms = XNEW (struct macro_scope);
00093   ms->file = macro_main (macro_user_macros);
00094   ms->line = -1;
00095   return ms;
00096 }
00097 
00098 struct macro_scope *
00099 default_macro_scope (void)
00100 {
00101   struct symtab_and_line sal;
00102   struct macro_scope *ms;
00103   struct frame_info *frame;
00104   CORE_ADDR pc;
00105 
00106   /* If there's a selected frame, use its PC.  */
00107   frame = deprecated_safe_get_selected_frame ();
00108   if (frame && get_frame_pc_if_available (frame, &pc))
00109     sal = find_pc_line (pc, 0);
00110 
00111   /* Fall back to the current listing position.  */
00112   else
00113     {
00114       /* Don't call select_source_symtab here.  That can raise an
00115          error if symbols aren't loaded, but GDB calls the expression
00116          evaluator in all sorts of contexts.
00117 
00118          For example, commands like `set width' call the expression
00119          evaluator to evaluate their numeric arguments.  If the
00120          current language is C, then that may call this function to
00121          choose a scope for macro expansion.  If you don't have any
00122          symbol files loaded, then get_current_or_default would raise an
00123          error.  But `set width' shouldn't raise an error just because
00124          it can't decide which scope to macro-expand its argument in.  */
00125       struct symtab_and_line cursal = 
00126                         get_current_source_symtab_and_line ();
00127       
00128       sal.symtab = cursal.symtab;
00129       sal.line = cursal.line;
00130     }
00131 
00132   ms = sal_macro_scope (sal);
00133   if (! ms)
00134     ms = user_macro_scope ();
00135 
00136   return ms;
00137 }
00138 
00139 
00140 /* Look up the definition of the macro named NAME in scope at the source
00141    location given by BATON, which must be a pointer to a `struct
00142    macro_scope' structure.  */
00143 struct macro_definition *
00144 standard_macro_lookup (const char *name, void *baton)
00145 {
00146   struct macro_scope *ms = (struct macro_scope *) baton;
00147   struct macro_definition *result;
00148 
00149   /* Give user-defined macros priority over all others.  */
00150   result = macro_lookup_definition (macro_main (macro_user_macros), -1, name);
00151   if (! result)
00152     result = macro_lookup_definition (ms->file, ms->line, name);
00153   return result;
00154 }
00155 
00156 /* Provide a prototype to silence -Wmissing-prototypes.  */
00157 extern initialize_file_ftype _initialize_macroscope;
00158 
00159 void
00160 _initialize_macroscope (void)
00161 {
00162   macro_user_macros = new_macro_table (NULL, NULL, NULL);
00163   macro_set_main (macro_user_macros, "<user-defined>");
00164   macro_allow_redefinitions (macro_user_macros);
00165 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines