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