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