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