GDB (xrefs)
/tmp/gdb-7.10/gdb/macroscope.c
Go to the documentation of this file.
1 /* Functions for deciding which macros are currently in scope.
2  Copyright (C) 2002-2015 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. */
36 
37 
38 struct macro_scope *
40 {
41  struct macro_source_file *main_file, *inclusion;
42  struct macro_scope *ms;
43  struct compunit_symtab *cust;
44 
45  if (sal.symtab == NULL)
46  return NULL;
47  cust = SYMTAB_COMPUNIT (sal.symtab);
48  if (COMPUNIT_MACRO_TABLE (cust) == NULL)
49  return NULL;
50 
51  ms = (struct macro_scope *) xmalloc (sizeof (*ms));
52 
53  main_file = macro_main (COMPUNIT_MACRO_TABLE (cust));
54  inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename);
55 
56  if (inclusion)
57  {
58  ms->file = inclusion;
59  ms->line = sal.line;
60  }
61  else
62  {
63  /* There are, unfortunately, cases where a compilation unit can
64  have a symtab for a source file that doesn't appear in the
65  macro table. For example, at the moment, Dwarf doesn't have
66  any way in the .debug_macinfo section to describe the effect
67  of #line directives, so if you debug a YACC parser you'll get
68  a macro table which only mentions the .c files generated by
69  YACC, but symtabs that mention the .y files consumed by YACC.
70 
71  In the long run, we should extend the Dwarf macro info
72  representation to handle #line directives, and get GCC to
73  emit it.
74 
75  For the time being, though, we'll just treat these as
76  occurring at the end of the main source file. */
77  ms->file = main_file;
78  ms->line = -1;
79 
81  _("symtab found for `%s', but that file\n"
82  "is not covered in the compilation unit's macro information"),
84  }
85 
86  return ms;
87 }
88 
89 
90 struct macro_scope *
92 {
93  struct macro_scope *ms;
94 
95  ms = XNEW (struct macro_scope);
96  ms->file = macro_main (macro_user_macros);
97  ms->line = -1;
98  return ms;
99 }
100 
101 struct macro_scope *
103 {
104  struct symtab_and_line sal;
105  struct macro_scope *ms;
106  struct frame_info *frame;
107  CORE_ADDR pc;
108 
109  /* If there's a selected frame, use its PC. */
111  if (frame && get_frame_pc_if_available (frame, &pc))
112  sal = find_pc_line (pc, 0);
113 
114  /* Fall back to the current listing position. */
115  else
116  {
117  /* Don't call select_source_symtab here. That can raise an
118  error if symbols aren't loaded, but GDB calls the expression
119  evaluator in all sorts of contexts.
120 
121  For example, commands like `set width' call the expression
122  evaluator to evaluate their numeric arguments. If the
123  current language is C, then that may call this function to
124  choose a scope for macro expansion. If you don't have any
125  symbol files loaded, then get_current_or_default would raise an
126  error. But `set width' shouldn't raise an error just because
127  it can't decide which scope to macro-expand its argument in. */
128  struct symtab_and_line cursal =
130 
131  sal.symtab = cursal.symtab;
132  sal.line = cursal.line;
133  }
134 
135  ms = sal_macro_scope (sal);
136  if (! ms)
137  ms = user_macro_scope ();
138 
139  return ms;
140 }
141 
142 
143 /* Look up the definition of the macro named NAME in scope at the source
144  location given by BATON, which must be a pointer to a `struct
145  macro_scope' structure. */
146 struct macro_definition *
147 standard_macro_lookup (const char *name, void *baton)
148 {
149  struct macro_scope *ms = (struct macro_scope *) baton;
150  struct macro_definition *result;
151 
152  /* Give user-defined macros priority over all others. */
153  result = macro_lookup_definition (macro_main (macro_user_macros), -1, name);
154  if (! result)
155  result = macro_lookup_definition (ms->file, ms->line, name);
156  return result;
157 }
158 
159 /* Provide a prototype to silence -Wmissing-prototypes. */
161 
162 void
164 {
165  macro_user_macros = new_macro_table (NULL, NULL, NULL);
166  macro_set_main (macro_user_macros, "<user-defined>");
167  macro_allow_redefinitions (macro_user_macros);
168 }
const char * symtab_to_filename_for_display(struct symtab *symtab)
Definition: source.c:1171
#define SYMTAB_COMPUNIT(symtab)
Definition: symtab.h:965
struct macro_source_file * file
Definition: macroscope.h:34
bfd_vma CORE_ADDR
Definition: common-types.h:41
struct macro_source_file * macro_lookup_inclusion(struct macro_source_file *source, const char *name)
Definition: macrotab.c:510
struct symtab_and_line get_current_source_symtab_and_line(void)
Definition: source.c:181
#define COMPUNIT_MACRO_TABLE(cust)
Definition: symtab.h:1104
struct macro_definition * standard_macro_lookup(const char *name, void *baton)
Definition: macroscope.c:147
int get_frame_pc_if_available(struct frame_info *frame, CORE_ADDR *pc)
Definition: frame.c:2224
const char * filename
Definition: symtab.h:943
#define _(String)
Definition: gdb_locale.h:40
struct macro_scope * sal_macro_scope(struct symtab_and_line sal)
Definition: macroscope.c:39
struct macro_scope * default_macro_scope(void)
Definition: macroscope.c:102
initialize_file_ftype _initialize_macroscope
struct macro_table * macro_user_macros
Definition: macroscope.c:35
const char *const name
Definition: aarch64-tdep.c:68
void initialize_file_ftype(void)
Definition: defs.h:281
struct symtab_and_line find_pc_line(CORE_ADDR pc, int notcurrent)
Definition: symtab.c:3315
struct macro_definition * macro_lookup_definition(struct macro_source_file *source, int line, const char *name)
Definition: macrotab.c:919
void complaint(struct complaints **complaints, const char *fmt,...)
Definition: complaints.c:251
struct frame_info * deprecated_safe_get_selected_frame(void)
Definition: frame.c:1564
void * xmalloc(YYSIZE_T)
struct symtab * symtab
Definition: symtab.h:1369
struct complaints * symfile_complaints
Definition: complaints.c:105
struct macro_source_file * macro_set_main(struct macro_table *t, const char *filename)
Definition: macrotab.c:418
void macro_allow_redefinitions(struct macro_table *t)
Definition: macrotab.c:441
struct macro_source_file * macro_main(struct macro_table *t)
Definition: macrotab.c:432
struct macro_table * new_macro_table(struct obstack *obstack, struct bcache *b, struct compunit_symtab *cust)
Definition: macrotab.c:1050
struct macro_scope * user_macro_scope(void)
Definition: macroscope.c:91