]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/macroscope.c
configure: require libzstd >= 1.4.0
[thirdparty/binutils-gdb.git] / gdb / macroscope.c
CommitLineData
6821892e 1/* Functions for deciding which macros are currently in scope.
4a94e368 2 Copyright (C) 2002-2022 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 29
d7d9f01e
TT
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. */
35struct macro_table *macro_user_macros;
36
6821892e 37
f6c2623e 38gdb::unique_xmalloc_ptr<struct macro_scope>
6821892e
JB
39sal_macro_scope (struct symtab_and_line sal)
40{
d5d6fca5 41 struct macro_source_file *main_file, *inclusion;
43f3e411 42 struct compunit_symtab *cust;
6821892e 43
43f3e411
DE
44 if (sal.symtab == NULL)
45 return NULL;
c6159652
SM
46
47 cust = sal.symtab->compunit ();
10cc645b 48 if (cust->macro_table () == NULL)
43f3e411 49 return NULL;
6821892e 50
f6c2623e 51 gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope));
6821892e 52
10cc645b 53 main_file = macro_main (cust->macro_table ());
f71ad555 54 inclusion = macro_lookup_inclusion (main_file, sal.symtab->filename_for_id);
6821892e 55
568f8739
JB
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
dda83cd7
SM
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. */
d5d6fca5 77 ms->file = main_file;
568f8739
JB
78 ms->line = -1;
79
b98664d3 80 complaint (_("symtab found for `%s', but that file\n"
dda83cd7
SM
81 "is not covered in the compilation unit's macro information"),
82 symtab_to_filename_for_display (sal.symtab));
568f8739 83 }
6821892e
JB
84
85 return ms;
86}
87
88
f6c2623e 89gdb::unique_xmalloc_ptr<struct macro_scope>
d7d9f01e
TT
90user_macro_scope (void)
91{
f6c2623e 92 gdb::unique_xmalloc_ptr<struct macro_scope> ms (XNEW (struct macro_scope));
d7d9f01e
TT
93 ms->file = macro_main (macro_user_macros);
94 ms->line = -1;
95 return ms;
96}
97
f6c2623e 98gdb::unique_xmalloc_ptr<struct macro_scope>
480dd42a 99default_macro_scope (void)
6821892e
JB
100{
101 struct symtab_and_line sal;
f6c2623e 102 gdb::unique_xmalloc_ptr<struct macro_scope> ms;
bd2b40ac 103 frame_info_ptr frame;
e3eebbd7 104 CORE_ADDR pc;
6821892e 105
206415a3
DJ
106 /* If there's a selected frame, use its PC. */
107 frame = deprecated_safe_get_selected_frame ();
e3eebbd7
PA
108 if (frame && get_frame_pc_if_available (frame, &pc))
109 sal = find_pc_line (pc, 0);
110
206415a3 111 /* Fall back to the current listing position. */
6821892e
JB
112 else
113 {
114 /* Don't call select_source_symtab here. That can raise an
dda83cd7
SM
115 error if symbols aren't loaded, but GDB calls the expression
116 evaluator in all sorts of contexts.
117
118 For example, commands like `set width' call the expression
119 evaluator to evaluate their numeric arguments. If the
120 current language is C, then that may call this function to
121 choose a scope for macro expansion. If you don't have any
122 symbol files loaded, then get_current_or_default would raise an
123 error. But `set width' shouldn't raise an error just because
124 it can't decide which scope to macro-expand its argument in. */
24b21115
SM
125 struct symtab_and_line cursal
126 = get_current_source_symtab_and_line ();
0378c332
FN
127
128 sal.symtab = cursal.symtab;
129 sal.line = cursal.line;
6821892e
JB
130 }
131
d7d9f01e
TT
132 ms = sal_macro_scope (sal);
133 if (! ms)
134 ms = user_macro_scope ();
135
136 return ms;
6821892e
JB
137}
138
139
140/* Look up the definition of the macro named NAME in scope at the source
141 location given by BATON, which must be a pointer to a `struct
142 macro_scope' structure. */
143struct macro_definition *
211d5b1c 144standard_macro_lookup (const char *name, const macro_scope &ms)
6821892e 145{
d7d9f01e 146 /* Give user-defined macros priority over all others. */
211d5b1c
SM
147 macro_definition *result
148 = macro_lookup_definition (macro_main (macro_user_macros), -1, name);
149
150 if (result == nullptr)
151 result = macro_lookup_definition (ms.file, ms.line, name);
152
d7d9f01e
TT
153 return result;
154}
155
6c265988 156void _initialize_macroscope ();
d7d9f01e 157void
6c265988 158_initialize_macroscope ()
d7d9f01e 159{
233d95b5 160 macro_user_macros = new_macro_table (NULL, NULL, NULL);
d7d9f01e
TT
161 macro_set_main (macro_user_macros, "<user-defined>");
162 macro_allow_redefinitions (macro_user_macros);
6821892e 163}