Commit | Line | Data |
---|---|---|
6821892e | 1 | /* Functions for deciding which macros are currently in scope. |
9b254dd1 | 2 | Copyright (C) 2002, 2007, 2008 Free Software Foundation, Inc. |
6821892e JB |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
6821892e JB |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
6821892e JB |
19 | |
20 | #include "defs.h" | |
21 | ||
22 | #include "macroscope.h" | |
23 | #include "symtab.h" | |
0378c332 | 24 | #include "source.h" |
6821892e JB |
25 | #include "target.h" |
26 | #include "frame.h" | |
27 | #include "inferior.h" | |
568f8739 | 28 | #include "complaints.h" |
6821892e JB |
29 | |
30 | ||
31 | struct macro_scope * | |
32 | sal_macro_scope (struct symtab_and_line sal) | |
33 | { | |
d5d6fca5 | 34 | struct macro_source_file *main_file, *inclusion; |
6821892e JB |
35 | struct macro_scope *ms; |
36 | ||
37 | if (! sal.symtab | |
38 | || ! sal.symtab->macro_table) | |
39 | return 0; | |
40 | ||
41 | ms = (struct macro_scope *) xmalloc (sizeof (*ms)); | |
42 | ||
d5d6fca5 DJ |
43 | main_file = macro_main (sal.symtab->macro_table); |
44 | inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename); | |
6821892e | 45 | |
568f8739 JB |
46 | if (inclusion) |
47 | { | |
48 | ms->file = inclusion; | |
49 | ms->line = sal.line; | |
50 | } | |
51 | else | |
52 | { | |
53 | /* There are, unfortunately, cases where a compilation unit can | |
54 | have a symtab for a source file that doesn't appear in the | |
55 | macro table. For example, at the moment, Dwarf doesn't have | |
56 | any way in the .debug_macinfo section to describe the effect | |
57 | of #line directives, so if you debug a YACC parser you'll get | |
58 | a macro table which only mentions the .c files generated by | |
59 | YACC, but symtabs that mention the .y files consumed by YACC. | |
60 | ||
61 | In the long run, we should extend the Dwarf macro info | |
62 | representation to handle #line directives, and get GCC to | |
63 | emit it. | |
64 | ||
65 | For the time being, though, we'll just treat these as | |
66 | occurring at the end of the main source file. */ | |
d5d6fca5 | 67 | ms->file = main_file; |
568f8739 JB |
68 | ms->line = -1; |
69 | ||
70 | complaint (&symfile_complaints, | |
e2e0b3e5 AC |
71 | _("symtab found for `%s', but that file\n" |
72 | "is not covered in the compilation unit's macro information"), | |
568f8739 JB |
73 | sal.symtab->filename); |
74 | } | |
6821892e JB |
75 | |
76 | return ms; | |
77 | } | |
78 | ||
79 | ||
80 | struct macro_scope * | |
480dd42a | 81 | default_macro_scope (void) |
6821892e JB |
82 | { |
83 | struct symtab_and_line sal; | |
6821892e | 84 | struct macro_scope *ms; |
206415a3 | 85 | struct frame_info *frame; |
6821892e | 86 | |
206415a3 DJ |
87 | /* If there's a selected frame, use its PC. */ |
88 | frame = deprecated_safe_get_selected_frame (); | |
89 | if (frame) | |
90 | sal = find_pc_line (get_frame_pc (frame), 0); | |
6821892e | 91 | |
206415a3 | 92 | /* Fall back to the current listing position. */ |
6821892e JB |
93 | else |
94 | { | |
95 | /* Don't call select_source_symtab here. That can raise an | |
96 | error if symbols aren't loaded, but GDB calls the expression | |
97 | evaluator in all sorts of contexts. | |
98 | ||
99 | For example, commands like `set width' call the expression | |
100 | evaluator to evaluate their numeric arguments. If the | |
101 | current language is C, then that may call this function to | |
102 | choose a scope for macro expansion. If you don't have any | |
0378c332 | 103 | symbol files loaded, then get_current_or_default would raise an |
6821892e JB |
104 | error. But `set width' shouldn't raise an error just because |
105 | it can't decide which scope to macro-expand its argument in. */ | |
0378c332 FN |
106 | struct symtab_and_line cursal = |
107 | get_current_source_symtab_and_line (); | |
108 | ||
109 | sal.symtab = cursal.symtab; | |
110 | sal.line = cursal.line; | |
6821892e JB |
111 | } |
112 | ||
113 | return sal_macro_scope (sal); | |
114 | } | |
115 | ||
116 | ||
117 | /* Look up the definition of the macro named NAME in scope at the source | |
118 | location given by BATON, which must be a pointer to a `struct | |
119 | macro_scope' structure. */ | |
120 | struct macro_definition * | |
121 | standard_macro_lookup (const char *name, void *baton) | |
122 | { | |
123 | struct macro_scope *ms = (struct macro_scope *) baton; | |
124 | ||
125 | return macro_lookup_definition (ms->file, ms->line, name); | |
126 | } |