PR macros/12999
[deliverable/binutils-gdb.git] / gdb / macrotab.h
1 /* Interface to C preprocessor macro tables for GDB.
2 Copyright (C) 2002, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #ifndef MACROTAB_H
22 #define MACROTAB_H
23
24 struct obstack;
25 struct bcache;
26
27 /* How do we represent a source location? I mean, how should we
28 represent them within GDB; the user wants to use all sorts of
29 ambiguous abbreviations, like "break 32" and "break foo.c:32"
30 ("foo.c" may have been #included into several compilation units),
31 but what do we disambiguate those things to?
32
33 - Answer 1: "Filename and line number." (Or column number, if
34 you're picky.) That's not quite good enough. For example, the
35 same source file can be #included into several different
36 compilation units --- which #inclusion do you mean?
37
38 - Answer 2: "Compilation unit, filename, and line number." This is
39 a pretty good answer; GDB's `struct symtab_and_line' basically
40 embodies this representation. But it's still ambiguous; what if a
41 given compilation unit #includes the same file twice --- how can I
42 set a breakpoint on line 12 of the fifth #inclusion of "foo.c"?
43
44 - Answer 3: "Compilation unit, chain of #inclusions, and line
45 number." This is analogous to the way GCC reports errors in
46 #include files:
47
48 $ gcc -c base.c
49 In file included from header2.h:8,
50 from header1.h:3,
51 from base.c:5:
52 header3.h:1: parse error before ')' token
53 $
54
55 GCC tells you exactly what path of #inclusions led you to the
56 problem. It gives you complete information, in a way that the
57 following would not:
58
59 $ gcc -c base.c
60 header3.h:1: parse error before ')' token
61 $
62
63 Converting all of GDB to use this is a big task, and I'm not really
64 suggesting it should be a priority. But this module's whole
65 purpose is to maintain structures describing the macro expansion
66 process, so I think it's appropriate for us to take a little care
67 to do that in a complete fashion.
68
69 In this interface, the first line of a file is numbered 1, not 0.
70 This is the same convention the rest of GDB uses. */
71
72
73 /* A table of all the macro definitions for a given compilation unit. */
74 struct macro_table;
75
76 /* The definition of a single macro. */
77 struct macro_definition;
78
79 /* A source file that participated in a compilation unit --- either a
80 main file, or an #included file. If a file is #included more than
81 once, the presence of the `included_from' and `included_at_line'
82 members means that we need to make one instance of this structure
83 for each #inclusion. Taken as a group, these structures form a
84 tree mapping the #inclusions that contributed to the compilation
85 unit, with the main source file as its root.
86
87 Beware --- not every source file mentioned in a compilation unit's
88 symtab structures will appear in the #inclusion tree! As of Oct
89 2002, GCC does record the effect of #line directives in the source
90 line info, but not in macro info. This means that GDB's symtabs
91 (built from the former, among other things) may mention filenames
92 that the #inclusion tree (built from the latter) doesn't have any
93 record of. See macroscope.c:sal_macro_scope for how to accomodate
94 this.
95
96 It's worth noting that libcpp has a simpler way of representing all
97 this, which we should consider switching to. It might even be
98 suitable for ordinary non-macro line number info.
99
100 Suppose you take your main source file, and after each line
101 containing an #include directive you insert the text of the
102 #included file. The result is a big file that pretty much
103 corresponds to the full text the compiler's going to see. There's
104 a one-to-one correspondence between lines in the big file and
105 per-inclusion lines in the source files. (Obviously, #include
106 directives that are #if'd out don't count. And you'll need to
107 append a newline to any file that doesn't end in one, to avoid
108 splicing the last #included line with the next line of the
109 #including file.)
110
111 Libcpp calls line numbers in this big imaginary file "logical line
112 numbers", and has a data structure called a "line map" that can map
113 logical line numbers onto actual source filenames and line numbers,
114 and also tell you the chain of #inclusions responsible for any
115 particular logical line number. Basically, this means you can pass
116 around a single line number and some kind of "compilation unit"
117 object and you get nice, unambiguous source code locations that
118 distinguish between multiple #inclusions of the same file, etc.
119
120 Pretty neat, huh? */
121
122 struct macro_source_file
123 {
124
125 /* The macro table for the compilation unit this source location is
126 a part of. */
127 struct macro_table *table;
128
129 /* A source file --- possibly a header file. */
130 const char *filename;
131
132 /* The location we were #included from, or zero if we are the
133 compilation unit's main source file. */
134 struct macro_source_file *included_by;
135
136 /* If `included_from' is non-zero, the line number in that source
137 file at which we were included. */
138 int included_at_line;
139
140 /* Head of a linked list of the source files #included by this file;
141 our children in the #inclusion tree. This list is sorted by its
142 elements' `included_at_line' values, which are unique. (The
143 macro splay tree's ordering function needs this property.) */
144 struct macro_source_file *includes;
145
146 /* The next file #included by our `included_from' file; our sibling
147 in the #inclusion tree. */
148 struct macro_source_file *next_included;
149 };
150
151
152 /* Create a new, empty macro table. Allocate it in OBSTACK, or use
153 xmalloc if OBSTACK is zero. Use BCACHE to store all macro names,
154 arguments, definitions, and anything else that might be the same
155 amongst compilation units in an executable file; if BCACHE is zero,
156 don't cache these things.
157
158 Note that, if either OBSTACK or BCACHE are non-zero, then removing
159 information from the table may leak memory. Neither obstacks nor
160 bcaches really allow you to remove information, so although we can
161 update the data structure to record the change, we can't free the
162 old data. At the moment, since we only provide obstacks and
163 bcaches for macro tables for symtabs, this isn't a problem; only
164 odd debugging information makes a definition and then deletes it at
165 the same source location (although 'gcc -DFOO -UFOO -DFOO=2' does
166 do that in GCC 4.1.2.). */
167 struct macro_table *new_macro_table (struct obstack *obstack,
168 struct bcache *bcache);
169
170
171 /* Free TABLE, and any macro definitions, source file structures,
172 etc. it owns. This will raise an internal error if TABLE was
173 allocated on an obstack, or if it uses a bcache. */
174 void free_macro_table (struct macro_table *table);
175
176
177 /* Set FILENAME as the main source file of TABLE. Return a source
178 file structure describing that file; if we record the #definition
179 of macros, or the #inclusion of other files into FILENAME, we'll
180 use that source file structure to indicate the context.
181
182 The "main source file" is the one that was given to the compiler;
183 all other source files that contributed to the compilation unit are
184 #included, directly or indirectly, from this one.
185
186 The macro table makes its own copy of FILENAME; the caller is
187 responsible for freeing FILENAME when it is no longer needed. */
188 struct macro_source_file *macro_set_main (struct macro_table *table,
189 const char *filename);
190
191
192 /* Return the main source file of the macro table TABLE. */
193 struct macro_source_file *macro_main (struct macro_table *table);
194
195 /* Mark the macro table TABLE so that macros defined in this table can
196 be redefined without error. Note that it invalid to call this if
197 TABLE is allocated on an obstack. */
198 void macro_allow_redefinitions (struct macro_table *table);
199
200
201 /* Record a #inclusion.
202 Record in SOURCE's macro table that, at line number LINE in SOURCE,
203 we #included the file INCLUDED. Return a source file structure we
204 can use for symbols #defined or files #included into that. If we've
205 already created a source file structure for this #inclusion, return
206 the same structure we created last time.
207
208 The first line of the source file has a line number of 1, not 0.
209
210 The macro table makes its own copy of INCLUDED; the caller is
211 responsible for freeing INCLUDED when it is no longer needed. */
212 struct macro_source_file *macro_include (struct macro_source_file *source,
213 int line,
214 const char *included);
215
216
217 /* Find any source file structure for a file named NAME, either
218 included into SOURCE, or SOURCE itself. Return zero if we have
219 none. NAME is only the final portion of the filename, not the full
220 path. e.g., `stdio.h', not `/usr/include/stdio.h'. If NAME
221 appears more than once in the inclusion tree, return the
222 least-nested inclusion --- the one closest to the main source file. */
223 struct macro_source_file *(macro_lookup_inclusion
224 (struct macro_source_file *source,
225 const char *name));
226
227
228 /* Record an object-like #definition (i.e., one with no parameter list).
229 Record in SOURCE's macro table that, at line number LINE in SOURCE,
230 we #defined a preprocessor symbol named NAME, whose replacement
231 string is REPLACEMENT. This function makes copies of NAME and
232 REPLACEMENT; the caller is responsible for freeing them. */
233 void macro_define_object (struct macro_source_file *source, int line,
234 const char *name, const char *replacement);
235
236
237 /* Record an function-like #definition (i.e., one with a parameter list).
238
239 Record in SOURCE's macro table that, at line number LINE in SOURCE,
240 we #defined a preprocessor symbol named NAME, with ARGC arguments
241 whose names are given in ARGV, whose replacement string is REPLACEMENT. If
242 the macro takes a variable number of arguments, then ARGC should be
243 one greater than the number of named arguments, and ARGV[ARGC-1]
244 should be the string "...". This function makes its own copies of
245 NAME, ARGV, and REPLACEMENT; the caller is responsible for freeing
246 them. */
247 void macro_define_function (struct macro_source_file *source, int line,
248 const char *name, int argc, const char **argv,
249 const char *replacement);
250
251
252 /* Record an #undefinition.
253 Record in SOURCE's macro table that, at line number LINE in SOURCE,
254 we removed the definition for the preprocessor symbol named NAME. */
255 void macro_undef (struct macro_source_file *source, int line,
256 const char *name);
257
258 /* Different kinds of macro definitions. */
259 enum macro_kind
260 {
261 macro_object_like,
262 macro_function_like
263 };
264
265
266 /* A preprocessor symbol definition. */
267 struct macro_definition
268 {
269 /* The table this definition lives in. */
270 struct macro_table *table;
271
272 /* What kind of macro it is. */
273 ENUM_BITFIELD (macro_kind) kind : 1;
274
275 /* If `kind' is `macro_function_like', the number of arguments it
276 takes, and their names. The names, and the array of pointers to
277 them, are in the table's bcache, if it has one. */
278 int argc : 31;
279 const char * const *argv;
280
281 /* The replacement string (body) of the macro. This is in the
282 table's bcache, if it has one. */
283 const char *replacement;
284 };
285
286
287 /* Return a pointer to the macro definition for NAME in scope at line
288 number LINE of SOURCE. If LINE is -1, return the definition in
289 effect at the end of the file. The macro table owns the structure;
290 the caller need not free it. Return zero if NAME is not #defined
291 at that point. */
292 struct macro_definition *(macro_lookup_definition
293 (struct macro_source_file *source,
294 int line, const char *name));
295
296
297 /* Return the source location of the definition for NAME in scope at
298 line number LINE of SOURCE. Set *DEFINITION_LINE to the line
299 number of the definition, and return a source file structure for
300 the file. Return zero if NAME has no definition in scope at that
301 point, and leave *DEFINITION_LINE unchanged. */
302 struct macro_source_file *(macro_definition_location
303 (struct macro_source_file *source,
304 int line,
305 const char *name,
306 int *definition_line));
307
308 /* Callback function when walking a macro table. NAME is the name of
309 the macro, and DEFINITION is the definition. SOURCE is the file at the
310 start of the include path, and LINE is the line number of the SOURCE file
311 where the macro was defined. USER_DATA is an arbitrary pointer which is
312 passed by the caller to macro_for_each or macro_for_each_in_scope. */
313 typedef void (*macro_callback_fn) (const char *name,
314 const struct macro_definition *definition,
315 struct macro_source_file *source,
316 int line,
317 void *user_data);
318
319 /* Call the function FN for each macro in the macro table TABLE.
320 USER_DATA is passed, untranslated, to FN. */
321 void macro_for_each (struct macro_table *table, macro_callback_fn fn,
322 void *user_data);
323
324 /* Call the function FN for each macro that is visible in a given
325 scope. The scope is represented by FILE and LINE. USER_DATA is
326 passed, untranslated, to FN. */
327 void macro_for_each_in_scope (struct macro_source_file *file, int line,
328 macro_callback_fn fn,
329 void *user_data);
330
331
332 #endif /* MACROTAB_H */
This page took 0.0374 seconds and 5 git commands to generate.