1 |
24 |
jeremybenn |
/* C preprocessor macro tables for GDB.
|
2 |
|
|
Copyright (C) 2002, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
|
19 |
|
|
|
20 |
|
|
#include "defs.h"
|
21 |
|
|
#include "gdb_obstack.h"
|
22 |
|
|
#include "splay-tree.h"
|
23 |
|
|
#include "symtab.h"
|
24 |
|
|
#include "symfile.h"
|
25 |
|
|
#include "objfiles.h"
|
26 |
|
|
#include "macrotab.h"
|
27 |
|
|
#include "gdb_assert.h"
|
28 |
|
|
#include "bcache.h"
|
29 |
|
|
#include "complaints.h"
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
/* The macro table structure. */
|
33 |
|
|
|
34 |
|
|
struct macro_table
|
35 |
|
|
{
|
36 |
|
|
/* The obstack this table's data should be allocated in, or zero if
|
37 |
|
|
we should use xmalloc. */
|
38 |
|
|
struct obstack *obstack;
|
39 |
|
|
|
40 |
|
|
/* The bcache we should use to hold macro names, argument names, and
|
41 |
|
|
definitions, or zero if we should use xmalloc. */
|
42 |
|
|
struct bcache *bcache;
|
43 |
|
|
|
44 |
|
|
/* The main source file for this compilation unit --- the one whose
|
45 |
|
|
name was given to the compiler. This is the root of the
|
46 |
|
|
#inclusion tree; everything else is #included from here. */
|
47 |
|
|
struct macro_source_file *main_source;
|
48 |
|
|
|
49 |
|
|
/* The table of macro definitions. This is a splay tree (an ordered
|
50 |
|
|
binary tree that stays balanced, effectively), sorted by macro
|
51 |
|
|
name. Where a macro gets defined more than once (presumably with
|
52 |
|
|
an #undefinition in between), we sort the definitions by the
|
53 |
|
|
order they would appear in the preprocessor's output. That is,
|
54 |
|
|
if `a.c' #includes `m.h' and then #includes `n.h', and both
|
55 |
|
|
header files #define X (with an #undef somewhere in between),
|
56 |
|
|
then the definition from `m.h' appears in our splay tree before
|
57 |
|
|
the one from `n.h'.
|
58 |
|
|
|
59 |
|
|
The splay tree's keys are `struct macro_key' pointers;
|
60 |
|
|
the values are `struct macro_definition' pointers.
|
61 |
|
|
|
62 |
|
|
The splay tree, its nodes, and the keys and values are allocated
|
63 |
|
|
in obstack, if it's non-zero, or with xmalloc otherwise. The
|
64 |
|
|
macro names, argument names, argument name arrays, and definition
|
65 |
|
|
strings are all allocated in bcache, if non-zero, or with xmalloc
|
66 |
|
|
otherwise. */
|
67 |
|
|
splay_tree definitions;
|
68 |
|
|
};
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
/* Allocation and freeing functions. */
|
73 |
|
|
|
74 |
|
|
/* Allocate SIZE bytes of memory appropriately for the macro table T.
|
75 |
|
|
This just checks whether T has an obstack, or whether its pieces
|
76 |
|
|
should be allocated with xmalloc. */
|
77 |
|
|
static void *
|
78 |
|
|
macro_alloc (int size, struct macro_table *t)
|
79 |
|
|
{
|
80 |
|
|
if (t->obstack)
|
81 |
|
|
return obstack_alloc (t->obstack, size);
|
82 |
|
|
else
|
83 |
|
|
return xmalloc (size);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
static void
|
88 |
|
|
macro_free (void *object, struct macro_table *t)
|
89 |
|
|
{
|
90 |
|
|
if (t->obstack)
|
91 |
|
|
/* There are cases where we need to remove entries from a macro
|
92 |
|
|
table, even when reading debugging information. This should be
|
93 |
|
|
rare, and there's no easy way to free arbitrary data from an
|
94 |
|
|
obstack, so we just leak it. */
|
95 |
|
|
;
|
96 |
|
|
else
|
97 |
|
|
xfree (object);
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/* If the macro table T has a bcache, then cache the LEN bytes at ADDR
|
102 |
|
|
there, and return the cached copy. Otherwise, just xmalloc a copy
|
103 |
|
|
of the bytes, and return a pointer to that. */
|
104 |
|
|
static const void *
|
105 |
|
|
macro_bcache (struct macro_table *t, const void *addr, int len)
|
106 |
|
|
{
|
107 |
|
|
if (t->bcache)
|
108 |
|
|
return bcache (addr, len, t->bcache);
|
109 |
|
|
else
|
110 |
|
|
{
|
111 |
|
|
void *copy = xmalloc (len);
|
112 |
|
|
memcpy (copy, addr, len);
|
113 |
|
|
return copy;
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
/* If the macro table T has a bcache, cache the null-terminated string
|
119 |
|
|
S there, and return a pointer to the cached copy. Otherwise,
|
120 |
|
|
xmalloc a copy and return that. */
|
121 |
|
|
static const char *
|
122 |
|
|
macro_bcache_str (struct macro_table *t, const char *s)
|
123 |
|
|
{
|
124 |
|
|
return (char *) macro_bcache (t, s, strlen (s) + 1);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
/* Free a possibly bcached object OBJ. That is, if the macro table T
|
129 |
|
|
has a bcache, do nothing; otherwise, xfree OBJ. */
|
130 |
|
|
static void
|
131 |
|
|
macro_bcache_free (struct macro_table *t, void *obj)
|
132 |
|
|
{
|
133 |
|
|
if (t->bcache)
|
134 |
|
|
/* There are cases where we need to remove entries from a macro
|
135 |
|
|
table, even when reading debugging information. This should be
|
136 |
|
|
rare, and there's no easy way to free data from a bcache, so we
|
137 |
|
|
just leak it. */
|
138 |
|
|
;
|
139 |
|
|
else
|
140 |
|
|
xfree (obj);
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
/* Macro tree keys, w/their comparison, allocation, and freeing functions. */
|
146 |
|
|
|
147 |
|
|
/* A key in the splay tree. */
|
148 |
|
|
struct macro_key
|
149 |
|
|
{
|
150 |
|
|
/* The table we're in. We only need this in order to free it, since
|
151 |
|
|
the splay tree library's key and value freeing functions require
|
152 |
|
|
that the key or value contain all the information needed to free
|
153 |
|
|
themselves. */
|
154 |
|
|
struct macro_table *table;
|
155 |
|
|
|
156 |
|
|
/* The name of the macro. This is in the table's bcache, if it has
|
157 |
|
|
one. */
|
158 |
|
|
const char *name;
|
159 |
|
|
|
160 |
|
|
/* The source file and line number where the definition's scope
|
161 |
|
|
begins. This is also the line of the definition itself. */
|
162 |
|
|
struct macro_source_file *start_file;
|
163 |
|
|
int start_line;
|
164 |
|
|
|
165 |
|
|
/* The first source file and line after the definition's scope.
|
166 |
|
|
(That is, the scope does not include this endpoint.) If end_file
|
167 |
|
|
is zero, then the definition extends to the end of the
|
168 |
|
|
compilation unit. */
|
169 |
|
|
struct macro_source_file *end_file;
|
170 |
|
|
int end_line;
|
171 |
|
|
};
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
/* Return the #inclusion depth of the source file FILE. This is the
|
175 |
|
|
number of #inclusions it took to reach this file. For the main
|
176 |
|
|
source file, the #inclusion depth is zero; for a file it #includes
|
177 |
|
|
directly, the depth would be one; and so on. */
|
178 |
|
|
static int
|
179 |
|
|
inclusion_depth (struct macro_source_file *file)
|
180 |
|
|
{
|
181 |
|
|
int depth;
|
182 |
|
|
|
183 |
|
|
for (depth = 0; file->included_by; depth++)
|
184 |
|
|
file = file->included_by;
|
185 |
|
|
|
186 |
|
|
return depth;
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
/* Compare two source locations (from the same compilation unit).
|
191 |
|
|
This is part of the comparison function for the tree of
|
192 |
|
|
definitions.
|
193 |
|
|
|
194 |
|
|
LINE1 and LINE2 are line numbers in the source files FILE1 and
|
195 |
|
|
FILE2. Return a value:
|
196 |
|
|
- less than zero if {LINE,FILE}1 comes before {LINE,FILE}2,
|
197 |
|
|
- greater than zero if {LINE,FILE}1 comes after {LINE,FILE}2, or
|
198 |
|
|
- zero if they are equal.
|
199 |
|
|
|
200 |
|
|
When the two locations are in different source files --- perhaps
|
201 |
|
|
one is in a header, while another is in the main source file --- we
|
202 |
|
|
order them by where they would appear in the fully pre-processed
|
203 |
|
|
sources, where all the #included files have been substituted into
|
204 |
|
|
their places. */
|
205 |
|
|
static int
|
206 |
|
|
compare_locations (struct macro_source_file *file1, int line1,
|
207 |
|
|
struct macro_source_file *file2, int line2)
|
208 |
|
|
{
|
209 |
|
|
/* We want to treat positions in an #included file as coming *after*
|
210 |
|
|
the line containing the #include, but *before* the line after the
|
211 |
|
|
include. As we walk up the #inclusion tree toward the main
|
212 |
|
|
source file, we update fileX and lineX as we go; includedX
|
213 |
|
|
indicates whether the original position was from the #included
|
214 |
|
|
file. */
|
215 |
|
|
int included1 = 0;
|
216 |
|
|
int included2 = 0;
|
217 |
|
|
|
218 |
|
|
/* If a file is zero, that means "end of compilation unit." Handle
|
219 |
|
|
that specially. */
|
220 |
|
|
if (! file1)
|
221 |
|
|
{
|
222 |
|
|
if (! file2)
|
223 |
|
|
return 0;
|
224 |
|
|
else
|
225 |
|
|
return 1;
|
226 |
|
|
}
|
227 |
|
|
else if (! file2)
|
228 |
|
|
return -1;
|
229 |
|
|
|
230 |
|
|
/* If the two files are not the same, find their common ancestor in
|
231 |
|
|
the #inclusion tree. */
|
232 |
|
|
if (file1 != file2)
|
233 |
|
|
{
|
234 |
|
|
/* If one file is deeper than the other, walk up the #inclusion
|
235 |
|
|
chain until the two files are at least at the same *depth*.
|
236 |
|
|
Then, walk up both files in synchrony until they're the same
|
237 |
|
|
file. That file is the common ancestor. */
|
238 |
|
|
int depth1 = inclusion_depth (file1);
|
239 |
|
|
int depth2 = inclusion_depth (file2);
|
240 |
|
|
|
241 |
|
|
/* Only one of these while loops will ever execute in any given
|
242 |
|
|
case. */
|
243 |
|
|
while (depth1 > depth2)
|
244 |
|
|
{
|
245 |
|
|
line1 = file1->included_at_line;
|
246 |
|
|
file1 = file1->included_by;
|
247 |
|
|
included1 = 1;
|
248 |
|
|
depth1--;
|
249 |
|
|
}
|
250 |
|
|
while (depth2 > depth1)
|
251 |
|
|
{
|
252 |
|
|
line2 = file2->included_at_line;
|
253 |
|
|
file2 = file2->included_by;
|
254 |
|
|
included2 = 1;
|
255 |
|
|
depth2--;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
/* Now both file1 and file2 are at the same depth. Walk toward
|
259 |
|
|
the root of the tree until we find where the branches meet. */
|
260 |
|
|
while (file1 != file2)
|
261 |
|
|
{
|
262 |
|
|
line1 = file1->included_at_line;
|
263 |
|
|
file1 = file1->included_by;
|
264 |
|
|
/* At this point, we know that the case the includedX flags
|
265 |
|
|
are trying to deal with won't come up, but we'll just
|
266 |
|
|
maintain them anyway. */
|
267 |
|
|
included1 = 1;
|
268 |
|
|
|
269 |
|
|
line2 = file2->included_at_line;
|
270 |
|
|
file2 = file2->included_by;
|
271 |
|
|
included2 = 1;
|
272 |
|
|
|
273 |
|
|
/* Sanity check. If file1 and file2 are really from the
|
274 |
|
|
same compilation unit, then they should both be part of
|
275 |
|
|
the same tree, and this shouldn't happen. */
|
276 |
|
|
gdb_assert (file1 && file2);
|
277 |
|
|
}
|
278 |
|
|
}
|
279 |
|
|
|
280 |
|
|
/* Now we've got two line numbers in the same file. */
|
281 |
|
|
if (line1 == line2)
|
282 |
|
|
{
|
283 |
|
|
/* They can't both be from #included files. Then we shouldn't
|
284 |
|
|
have walked up this far. */
|
285 |
|
|
gdb_assert (! included1 || ! included2);
|
286 |
|
|
|
287 |
|
|
/* Any #included position comes after a non-#included position
|
288 |
|
|
with the same line number in the #including file. */
|
289 |
|
|
if (included1)
|
290 |
|
|
return 1;
|
291 |
|
|
else if (included2)
|
292 |
|
|
return -1;
|
293 |
|
|
else
|
294 |
|
|
return 0;
|
295 |
|
|
}
|
296 |
|
|
else
|
297 |
|
|
return line1 - line2;
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
|
301 |
|
|
/* Compare a macro key KEY against NAME, the source file FILE, and
|
302 |
|
|
line number LINE.
|
303 |
|
|
|
304 |
|
|
Sort definitions by name; for two definitions with the same name,
|
305 |
|
|
place the one whose definition comes earlier before the one whose
|
306 |
|
|
definition comes later.
|
307 |
|
|
|
308 |
|
|
Return -1, 0, or 1 if key comes before, is identical to, or comes
|
309 |
|
|
after NAME, FILE, and LINE. */
|
310 |
|
|
static int
|
311 |
|
|
key_compare (struct macro_key *key,
|
312 |
|
|
const char *name, struct macro_source_file *file, int line)
|
313 |
|
|
{
|
314 |
|
|
int names = strcmp (key->name, name);
|
315 |
|
|
if (names)
|
316 |
|
|
return names;
|
317 |
|
|
|
318 |
|
|
return compare_locations (key->start_file, key->start_line,
|
319 |
|
|
file, line);
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
/* The macro tree comparison function, typed for the splay tree
|
324 |
|
|
library's happiness. */
|
325 |
|
|
static int
|
326 |
|
|
macro_tree_compare (splay_tree_key untyped_key1,
|
327 |
|
|
splay_tree_key untyped_key2)
|
328 |
|
|
{
|
329 |
|
|
struct macro_key *key1 = (struct macro_key *) untyped_key1;
|
330 |
|
|
struct macro_key *key2 = (struct macro_key *) untyped_key2;
|
331 |
|
|
|
332 |
|
|
return key_compare (key1, key2->name, key2->start_file, key2->start_line);
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
|
336 |
|
|
/* Construct a new macro key node for a macro in table T whose name is
|
337 |
|
|
NAME, and whose scope starts at LINE in FILE; register the name in
|
338 |
|
|
the bcache. */
|
339 |
|
|
static struct macro_key *
|
340 |
|
|
new_macro_key (struct macro_table *t,
|
341 |
|
|
const char *name,
|
342 |
|
|
struct macro_source_file *file,
|
343 |
|
|
int line)
|
344 |
|
|
{
|
345 |
|
|
struct macro_key *k = macro_alloc (sizeof (*k), t);
|
346 |
|
|
|
347 |
|
|
memset (k, 0, sizeof (*k));
|
348 |
|
|
k->table = t;
|
349 |
|
|
k->name = macro_bcache_str (t, name);
|
350 |
|
|
k->start_file = file;
|
351 |
|
|
k->start_line = line;
|
352 |
|
|
k->end_file = 0;
|
353 |
|
|
|
354 |
|
|
return k;
|
355 |
|
|
}
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
static void
|
359 |
|
|
macro_tree_delete_key (void *untyped_key)
|
360 |
|
|
{
|
361 |
|
|
struct macro_key *key = (struct macro_key *) untyped_key;
|
362 |
|
|
|
363 |
|
|
macro_bcache_free (key->table, (char *) key->name);
|
364 |
|
|
macro_free (key, key->table);
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
/* Building and querying the tree of #included files. */
|
370 |
|
|
|
371 |
|
|
|
372 |
|
|
/* Allocate and initialize a new source file structure. */
|
373 |
|
|
static struct macro_source_file *
|
374 |
|
|
new_source_file (struct macro_table *t,
|
375 |
|
|
const char *filename)
|
376 |
|
|
{
|
377 |
|
|
/* Get space for the source file structure itself. */
|
378 |
|
|
struct macro_source_file *f = macro_alloc (sizeof (*f), t);
|
379 |
|
|
|
380 |
|
|
memset (f, 0, sizeof (*f));
|
381 |
|
|
f->table = t;
|
382 |
|
|
f->filename = macro_bcache_str (t, filename);
|
383 |
|
|
f->includes = 0;
|
384 |
|
|
|
385 |
|
|
return f;
|
386 |
|
|
}
|
387 |
|
|
|
388 |
|
|
|
389 |
|
|
/* Free a source file, and all the source files it #included. */
|
390 |
|
|
static void
|
391 |
|
|
free_macro_source_file (struct macro_source_file *src)
|
392 |
|
|
{
|
393 |
|
|
struct macro_source_file *child, *next_child;
|
394 |
|
|
|
395 |
|
|
/* Free this file's children. */
|
396 |
|
|
for (child = src->includes; child; child = next_child)
|
397 |
|
|
{
|
398 |
|
|
next_child = child->next_included;
|
399 |
|
|
free_macro_source_file (child);
|
400 |
|
|
}
|
401 |
|
|
|
402 |
|
|
macro_bcache_free (src->table, (char *) src->filename);
|
403 |
|
|
macro_free (src, src->table);
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
|
407 |
|
|
struct macro_source_file *
|
408 |
|
|
macro_set_main (struct macro_table *t,
|
409 |
|
|
const char *filename)
|
410 |
|
|
{
|
411 |
|
|
/* You can't change a table's main source file. What would that do
|
412 |
|
|
to the tree? */
|
413 |
|
|
gdb_assert (! t->main_source);
|
414 |
|
|
|
415 |
|
|
t->main_source = new_source_file (t, filename);
|
416 |
|
|
|
417 |
|
|
return t->main_source;
|
418 |
|
|
}
|
419 |
|
|
|
420 |
|
|
|
421 |
|
|
struct macro_source_file *
|
422 |
|
|
macro_main (struct macro_table *t)
|
423 |
|
|
{
|
424 |
|
|
gdb_assert (t->main_source);
|
425 |
|
|
|
426 |
|
|
return t->main_source;
|
427 |
|
|
}
|
428 |
|
|
|
429 |
|
|
|
430 |
|
|
struct macro_source_file *
|
431 |
|
|
macro_include (struct macro_source_file *source,
|
432 |
|
|
int line,
|
433 |
|
|
const char *included)
|
434 |
|
|
{
|
435 |
|
|
struct macro_source_file *new;
|
436 |
|
|
struct macro_source_file **link;
|
437 |
|
|
|
438 |
|
|
/* Find the right position in SOURCE's `includes' list for the new
|
439 |
|
|
file. Skip inclusions at earlier lines, until we find one at the
|
440 |
|
|
same line or later --- or until the end of the list. */
|
441 |
|
|
for (link = &source->includes;
|
442 |
|
|
*link && (*link)->included_at_line < line;
|
443 |
|
|
link = &(*link)->next_included)
|
444 |
|
|
;
|
445 |
|
|
|
446 |
|
|
/* Did we find another file already #included at the same line as
|
447 |
|
|
the new one? */
|
448 |
|
|
if (*link && line == (*link)->included_at_line)
|
449 |
|
|
{
|
450 |
|
|
/* This means the compiler is emitting bogus debug info. (GCC
|
451 |
|
|
circa March 2002 did this.) It also means that the splay
|
452 |
|
|
tree ordering function, macro_tree_compare, will abort,
|
453 |
|
|
because it can't tell which #inclusion came first. But GDB
|
454 |
|
|
should tolerate bad debug info. So:
|
455 |
|
|
|
456 |
|
|
First, squawk. */
|
457 |
|
|
complaint (&symfile_complaints,
|
458 |
|
|
_("both `%s' and `%s' allegedly #included at %s:%d"), included,
|
459 |
|
|
(*link)->filename, source->filename, line);
|
460 |
|
|
|
461 |
|
|
/* Now, choose a new, unoccupied line number for this
|
462 |
|
|
#inclusion, after the alleged #inclusion line. */
|
463 |
|
|
while (*link && line == (*link)->included_at_line)
|
464 |
|
|
{
|
465 |
|
|
/* This line number is taken, so try the next line. */
|
466 |
|
|
line++;
|
467 |
|
|
link = &(*link)->next_included;
|
468 |
|
|
}
|
469 |
|
|
}
|
470 |
|
|
|
471 |
|
|
/* At this point, we know that LINE is an unused line number, and
|
472 |
|
|
*LINK points to the entry an #inclusion at that line should
|
473 |
|
|
precede. */
|
474 |
|
|
new = new_source_file (source->table, included);
|
475 |
|
|
new->included_by = source;
|
476 |
|
|
new->included_at_line = line;
|
477 |
|
|
new->next_included = *link;
|
478 |
|
|
*link = new;
|
479 |
|
|
|
480 |
|
|
return new;
|
481 |
|
|
}
|
482 |
|
|
|
483 |
|
|
|
484 |
|
|
struct macro_source_file *
|
485 |
|
|
macro_lookup_inclusion (struct macro_source_file *source, const char *name)
|
486 |
|
|
{
|
487 |
|
|
/* Is SOURCE itself named NAME? */
|
488 |
|
|
if (strcmp (name, source->filename) == 0)
|
489 |
|
|
return source;
|
490 |
|
|
|
491 |
|
|
/* The filename in the source structure is probably a full path, but
|
492 |
|
|
NAME could be just the final component of the name. */
|
493 |
|
|
{
|
494 |
|
|
int name_len = strlen (name);
|
495 |
|
|
int src_name_len = strlen (source->filename);
|
496 |
|
|
|
497 |
|
|
/* We do mean < here, and not <=; if the lengths are the same,
|
498 |
|
|
then the strcmp above should have triggered, and we need to
|
499 |
|
|
check for a slash here. */
|
500 |
|
|
if (name_len < src_name_len
|
501 |
|
|
&& source->filename[src_name_len - name_len - 1] == '/'
|
502 |
|
|
&& strcmp (name, source->filename + src_name_len - name_len) == 0)
|
503 |
|
|
return source;
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
/* It's not us. Try all our children, and return the lowest. */
|
507 |
|
|
{
|
508 |
|
|
struct macro_source_file *child;
|
509 |
|
|
struct macro_source_file *best = NULL;
|
510 |
|
|
int best_depth = 0;
|
511 |
|
|
|
512 |
|
|
for (child = source->includes; child; child = child->next_included)
|
513 |
|
|
{
|
514 |
|
|
struct macro_source_file *result
|
515 |
|
|
= macro_lookup_inclusion (child, name);
|
516 |
|
|
|
517 |
|
|
if (result)
|
518 |
|
|
{
|
519 |
|
|
int result_depth = inclusion_depth (result);
|
520 |
|
|
|
521 |
|
|
if (! best || result_depth < best_depth)
|
522 |
|
|
{
|
523 |
|
|
best = result;
|
524 |
|
|
best_depth = result_depth;
|
525 |
|
|
}
|
526 |
|
|
}
|
527 |
|
|
}
|
528 |
|
|
|
529 |
|
|
return best;
|
530 |
|
|
}
|
531 |
|
|
}
|
532 |
|
|
|
533 |
|
|
|
534 |
|
|
|
535 |
|
|
/* Registering and looking up macro definitions. */
|
536 |
|
|
|
537 |
|
|
|
538 |
|
|
/* Construct a definition for a macro in table T. Cache all strings,
|
539 |
|
|
and the macro_definition structure itself, in T's bcache. */
|
540 |
|
|
static struct macro_definition *
|
541 |
|
|
new_macro_definition (struct macro_table *t,
|
542 |
|
|
enum macro_kind kind,
|
543 |
|
|
int argc, const char **argv,
|
544 |
|
|
const char *replacement)
|
545 |
|
|
{
|
546 |
|
|
struct macro_definition *d = macro_alloc (sizeof (*d), t);
|
547 |
|
|
|
548 |
|
|
memset (d, 0, sizeof (*d));
|
549 |
|
|
d->table = t;
|
550 |
|
|
d->kind = kind;
|
551 |
|
|
d->replacement = macro_bcache_str (t, replacement);
|
552 |
|
|
|
553 |
|
|
if (kind == macro_function_like)
|
554 |
|
|
{
|
555 |
|
|
int i;
|
556 |
|
|
const char **cached_argv;
|
557 |
|
|
int cached_argv_size = argc * sizeof (*cached_argv);
|
558 |
|
|
|
559 |
|
|
/* Bcache all the arguments. */
|
560 |
|
|
cached_argv = alloca (cached_argv_size);
|
561 |
|
|
for (i = 0; i < argc; i++)
|
562 |
|
|
cached_argv[i] = macro_bcache_str (t, argv[i]);
|
563 |
|
|
|
564 |
|
|
/* Now bcache the array of argument pointers itself. */
|
565 |
|
|
d->argv = macro_bcache (t, cached_argv, cached_argv_size);
|
566 |
|
|
d->argc = argc;
|
567 |
|
|
}
|
568 |
|
|
|
569 |
|
|
/* We don't bcache the entire definition structure because it's got
|
570 |
|
|
a pointer to the macro table in it; since each compilation unit
|
571 |
|
|
has its own macro table, you'd only get bcache hits for identical
|
572 |
|
|
definitions within a compilation unit, which seems unlikely.
|
573 |
|
|
|
574 |
|
|
"So, why do macro definitions have pointers to their macro tables
|
575 |
|
|
at all?" Well, when the splay tree library wants to free a
|
576 |
|
|
node's value, it calls the value freeing function with nothing
|
577 |
|
|
but the value itself. It makes the (apparently reasonable)
|
578 |
|
|
assumption that the value carries enough information to free
|
579 |
|
|
itself. But not all macro tables have bcaches, so not all macro
|
580 |
|
|
definitions would be bcached. There's no way to tell whether a
|
581 |
|
|
given definition is bcached without knowing which table the
|
582 |
|
|
definition belongs to. ... blah. The thing's only sixteen
|
583 |
|
|
bytes anyway, and we can still bcache the name, args, and
|
584 |
|
|
definition, so we just don't bother bcaching the definition
|
585 |
|
|
structure itself. */
|
586 |
|
|
return d;
|
587 |
|
|
}
|
588 |
|
|
|
589 |
|
|
|
590 |
|
|
/* Free a macro definition. */
|
591 |
|
|
static void
|
592 |
|
|
macro_tree_delete_value (void *untyped_definition)
|
593 |
|
|
{
|
594 |
|
|
struct macro_definition *d = (struct macro_definition *) untyped_definition;
|
595 |
|
|
struct macro_table *t = d->table;
|
596 |
|
|
|
597 |
|
|
if (d->kind == macro_function_like)
|
598 |
|
|
{
|
599 |
|
|
int i;
|
600 |
|
|
|
601 |
|
|
for (i = 0; i < d->argc; i++)
|
602 |
|
|
macro_bcache_free (t, (char *) d->argv[i]);
|
603 |
|
|
macro_bcache_free (t, (char **) d->argv);
|
604 |
|
|
}
|
605 |
|
|
|
606 |
|
|
macro_bcache_free (t, (char *) d->replacement);
|
607 |
|
|
macro_free (d, t);
|
608 |
|
|
}
|
609 |
|
|
|
610 |
|
|
|
611 |
|
|
/* Find the splay tree node for the definition of NAME at LINE in
|
612 |
|
|
SOURCE, or zero if there is none. */
|
613 |
|
|
static splay_tree_node
|
614 |
|
|
find_definition (const char *name,
|
615 |
|
|
struct macro_source_file *file,
|
616 |
|
|
int line)
|
617 |
|
|
{
|
618 |
|
|
struct macro_table *t = file->table;
|
619 |
|
|
splay_tree_node n;
|
620 |
|
|
|
621 |
|
|
/* Construct a macro_key object, just for the query. */
|
622 |
|
|
struct macro_key query;
|
623 |
|
|
|
624 |
|
|
query.name = name;
|
625 |
|
|
query.start_file = file;
|
626 |
|
|
query.start_line = line;
|
627 |
|
|
query.end_file = NULL;
|
628 |
|
|
|
629 |
|
|
n = splay_tree_lookup (t->definitions, (splay_tree_key) &query);
|
630 |
|
|
if (! n)
|
631 |
|
|
{
|
632 |
|
|
/* It's okay for us to do two queries like this: the real work
|
633 |
|
|
of the searching is done when we splay, and splaying the tree
|
634 |
|
|
a second time at the same key is a constant time operation.
|
635 |
|
|
If this still bugs you, you could always just extend the
|
636 |
|
|
splay tree library with a predecessor-or-equal operation, and
|
637 |
|
|
use that. */
|
638 |
|
|
splay_tree_node pred = splay_tree_predecessor (t->definitions,
|
639 |
|
|
(splay_tree_key) &query);
|
640 |
|
|
|
641 |
|
|
if (pred)
|
642 |
|
|
{
|
643 |
|
|
/* Make sure this predecessor actually has the right name.
|
644 |
|
|
We just want to search within a given name's definitions. */
|
645 |
|
|
struct macro_key *found = (struct macro_key *) pred->key;
|
646 |
|
|
|
647 |
|
|
if (strcmp (found->name, name) == 0)
|
648 |
|
|
n = pred;
|
649 |
|
|
}
|
650 |
|
|
}
|
651 |
|
|
|
652 |
|
|
if (n)
|
653 |
|
|
{
|
654 |
|
|
struct macro_key *found = (struct macro_key *) n->key;
|
655 |
|
|
|
656 |
|
|
/* Okay, so this definition has the right name, and its scope
|
657 |
|
|
begins before the given source location. But does its scope
|
658 |
|
|
end after the given source location? */
|
659 |
|
|
if (compare_locations (file, line, found->end_file, found->end_line) < 0)
|
660 |
|
|
return n;
|
661 |
|
|
else
|
662 |
|
|
return 0;
|
663 |
|
|
}
|
664 |
|
|
else
|
665 |
|
|
return 0;
|
666 |
|
|
}
|
667 |
|
|
|
668 |
|
|
|
669 |
|
|
/* If NAME already has a definition in scope at LINE in SOURCE, return
|
670 |
|
|
the key. If the old definition is different from the definition
|
671 |
|
|
given by KIND, ARGC, ARGV, and REPLACEMENT, complain, too.
|
672 |
|
|
Otherwise, return zero. (ARGC and ARGV are meaningless unless KIND
|
673 |
|
|
is `macro_function_like'.) */
|
674 |
|
|
static struct macro_key *
|
675 |
|
|
check_for_redefinition (struct macro_source_file *source, int line,
|
676 |
|
|
const char *name, enum macro_kind kind,
|
677 |
|
|
int argc, const char **argv,
|
678 |
|
|
const char *replacement)
|
679 |
|
|
{
|
680 |
|
|
splay_tree_node n = find_definition (name, source, line);
|
681 |
|
|
|
682 |
|
|
if (n)
|
683 |
|
|
{
|
684 |
|
|
struct macro_key *found_key = (struct macro_key *) n->key;
|
685 |
|
|
struct macro_definition *found_def
|
686 |
|
|
= (struct macro_definition *) n->value;
|
687 |
|
|
int same = 1;
|
688 |
|
|
|
689 |
|
|
/* Is this definition the same as the existing one?
|
690 |
|
|
According to the standard, this comparison needs to be done
|
691 |
|
|
on lists of tokens, not byte-by-byte, as we do here. But
|
692 |
|
|
that's too hard for us at the moment, and comparing
|
693 |
|
|
byte-by-byte will only yield false negatives (i.e., extra
|
694 |
|
|
warning messages), not false positives (i.e., unnoticed
|
695 |
|
|
definition changes). */
|
696 |
|
|
if (kind != found_def->kind)
|
697 |
|
|
same = 0;
|
698 |
|
|
else if (strcmp (replacement, found_def->replacement))
|
699 |
|
|
same = 0;
|
700 |
|
|
else if (kind == macro_function_like)
|
701 |
|
|
{
|
702 |
|
|
if (argc != found_def->argc)
|
703 |
|
|
same = 0;
|
704 |
|
|
else
|
705 |
|
|
{
|
706 |
|
|
int i;
|
707 |
|
|
|
708 |
|
|
for (i = 0; i < argc; i++)
|
709 |
|
|
if (strcmp (argv[i], found_def->argv[i]))
|
710 |
|
|
same = 0;
|
711 |
|
|
}
|
712 |
|
|
}
|
713 |
|
|
|
714 |
|
|
if (! same)
|
715 |
|
|
{
|
716 |
|
|
complaint (&symfile_complaints,
|
717 |
|
|
_("macro `%s' redefined at %s:%d; original definition at %s:%d"),
|
718 |
|
|
name, source->filename, line,
|
719 |
|
|
found_key->start_file->filename, found_key->start_line);
|
720 |
|
|
}
|
721 |
|
|
|
722 |
|
|
return found_key;
|
723 |
|
|
}
|
724 |
|
|
else
|
725 |
|
|
return 0;
|
726 |
|
|
}
|
727 |
|
|
|
728 |
|
|
|
729 |
|
|
void
|
730 |
|
|
macro_define_object (struct macro_source_file *source, int line,
|
731 |
|
|
const char *name, const char *replacement)
|
732 |
|
|
{
|
733 |
|
|
struct macro_table *t = source->table;
|
734 |
|
|
struct macro_key *k;
|
735 |
|
|
struct macro_definition *d;
|
736 |
|
|
|
737 |
|
|
k = check_for_redefinition (source, line,
|
738 |
|
|
name, macro_object_like,
|
739 |
|
|
0, 0,
|
740 |
|
|
replacement);
|
741 |
|
|
|
742 |
|
|
/* If we're redefining a symbol, and the existing key would be
|
743 |
|
|
identical to our new key, then the splay_tree_insert function
|
744 |
|
|
will try to delete the old definition. When the definition is
|
745 |
|
|
living on an obstack, this isn't a happy thing.
|
746 |
|
|
|
747 |
|
|
Since this only happens in the presence of questionable debug
|
748 |
|
|
info, we just ignore all definitions after the first. The only
|
749 |
|
|
case I know of where this arises is in GCC's output for
|
750 |
|
|
predefined macros, and all the definitions are the same in that
|
751 |
|
|
case. */
|
752 |
|
|
if (k && ! key_compare (k, name, source, line))
|
753 |
|
|
return;
|
754 |
|
|
|
755 |
|
|
k = new_macro_key (t, name, source, line);
|
756 |
|
|
d = new_macro_definition (t, macro_object_like, 0, 0, replacement);
|
757 |
|
|
splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
|
758 |
|
|
}
|
759 |
|
|
|
760 |
|
|
|
761 |
|
|
void
|
762 |
|
|
macro_define_function (struct macro_source_file *source, int line,
|
763 |
|
|
const char *name, int argc, const char **argv,
|
764 |
|
|
const char *replacement)
|
765 |
|
|
{
|
766 |
|
|
struct macro_table *t = source->table;
|
767 |
|
|
struct macro_key *k;
|
768 |
|
|
struct macro_definition *d;
|
769 |
|
|
|
770 |
|
|
k = check_for_redefinition (source, line,
|
771 |
|
|
name, macro_function_like,
|
772 |
|
|
argc, argv,
|
773 |
|
|
replacement);
|
774 |
|
|
|
775 |
|
|
/* See comments about duplicate keys in macro_define_object. */
|
776 |
|
|
if (k && ! key_compare (k, name, source, line))
|
777 |
|
|
return;
|
778 |
|
|
|
779 |
|
|
/* We should also check here that all the argument names in ARGV are
|
780 |
|
|
distinct. */
|
781 |
|
|
|
782 |
|
|
k = new_macro_key (t, name, source, line);
|
783 |
|
|
d = new_macro_definition (t, macro_function_like, argc, argv, replacement);
|
784 |
|
|
splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
|
785 |
|
|
}
|
786 |
|
|
|
787 |
|
|
|
788 |
|
|
void
|
789 |
|
|
macro_undef (struct macro_source_file *source, int line,
|
790 |
|
|
const char *name)
|
791 |
|
|
{
|
792 |
|
|
splay_tree_node n = find_definition (name, source, line);
|
793 |
|
|
|
794 |
|
|
if (n)
|
795 |
|
|
{
|
796 |
|
|
struct macro_key *key = (struct macro_key *) n->key;
|
797 |
|
|
|
798 |
|
|
/* If we're removing a definition at exactly the same point that
|
799 |
|
|
we defined it, then just delete the entry altogether. GCC
|
800 |
|
|
4.1.2 will generate DWARF that says to do this if you pass it
|
801 |
|
|
arguments like '-DFOO -UFOO -DFOO=2'. */
|
802 |
|
|
if (source == key->start_file
|
803 |
|
|
&& line == key->start_line)
|
804 |
|
|
splay_tree_remove (source->table->definitions, n->key);
|
805 |
|
|
|
806 |
|
|
else
|
807 |
|
|
{
|
808 |
|
|
/* This function is the only place a macro's end-of-scope
|
809 |
|
|
location gets set to anything other than "end of the
|
810 |
|
|
compilation unit" (i.e., end_file is zero). So if this
|
811 |
|
|
macro already has its end-of-scope set, then we're
|
812 |
|
|
probably seeing a second #undefinition for the same
|
813 |
|
|
#definition. */
|
814 |
|
|
if (key->end_file)
|
815 |
|
|
{
|
816 |
|
|
complaint (&symfile_complaints,
|
817 |
|
|
_("macro '%s' is #undefined twice,"
|
818 |
|
|
" at %s:%d and %s:%d"),
|
819 |
|
|
name,
|
820 |
|
|
source->filename, line,
|
821 |
|
|
key->end_file->filename, key->end_line);
|
822 |
|
|
}
|
823 |
|
|
|
824 |
|
|
/* Whether or not we've seen a prior #undefinition, wipe out
|
825 |
|
|
the old ending point, and make this the ending point. */
|
826 |
|
|
key->end_file = source;
|
827 |
|
|
key->end_line = line;
|
828 |
|
|
}
|
829 |
|
|
}
|
830 |
|
|
else
|
831 |
|
|
{
|
832 |
|
|
/* According to the ISO C standard, an #undef for a symbol that
|
833 |
|
|
has no macro definition in scope is ignored. So we should
|
834 |
|
|
ignore it too. */
|
835 |
|
|
#if 0
|
836 |
|
|
complaint (&symfile_complaints,
|
837 |
|
|
_("no definition for macro `%s' in scope to #undef at %s:%d"),
|
838 |
|
|
name, source->filename, line);
|
839 |
|
|
#endif
|
840 |
|
|
}
|
841 |
|
|
}
|
842 |
|
|
|
843 |
|
|
|
844 |
|
|
struct macro_definition *
|
845 |
|
|
macro_lookup_definition (struct macro_source_file *source,
|
846 |
|
|
int line, const char *name)
|
847 |
|
|
{
|
848 |
|
|
splay_tree_node n = find_definition (name, source, line);
|
849 |
|
|
|
850 |
|
|
if (n)
|
851 |
|
|
return (struct macro_definition *) n->value;
|
852 |
|
|
else
|
853 |
|
|
return 0;
|
854 |
|
|
}
|
855 |
|
|
|
856 |
|
|
|
857 |
|
|
struct macro_source_file *
|
858 |
|
|
macro_definition_location (struct macro_source_file *source,
|
859 |
|
|
int line,
|
860 |
|
|
const char *name,
|
861 |
|
|
int *definition_line)
|
862 |
|
|
{
|
863 |
|
|
splay_tree_node n = find_definition (name, source, line);
|
864 |
|
|
|
865 |
|
|
if (n)
|
866 |
|
|
{
|
867 |
|
|
struct macro_key *key = (struct macro_key *) n->key;
|
868 |
|
|
*definition_line = key->start_line;
|
869 |
|
|
return key->start_file;
|
870 |
|
|
}
|
871 |
|
|
else
|
872 |
|
|
return 0;
|
873 |
|
|
}
|
874 |
|
|
|
875 |
|
|
|
876 |
|
|
|
877 |
|
|
/* Creating and freeing macro tables. */
|
878 |
|
|
|
879 |
|
|
|
880 |
|
|
struct macro_table *
|
881 |
|
|
new_macro_table (struct obstack *obstack,
|
882 |
|
|
struct bcache *b)
|
883 |
|
|
{
|
884 |
|
|
struct macro_table *t;
|
885 |
|
|
|
886 |
|
|
/* First, get storage for the `struct macro_table' itself. */
|
887 |
|
|
if (obstack)
|
888 |
|
|
t = obstack_alloc (obstack, sizeof (*t));
|
889 |
|
|
else
|
890 |
|
|
t = xmalloc (sizeof (*t));
|
891 |
|
|
|
892 |
|
|
memset (t, 0, sizeof (*t));
|
893 |
|
|
t->obstack = obstack;
|
894 |
|
|
t->bcache = b;
|
895 |
|
|
t->main_source = NULL;
|
896 |
|
|
t->definitions = (splay_tree_new_with_allocator
|
897 |
|
|
(macro_tree_compare,
|
898 |
|
|
((splay_tree_delete_key_fn) macro_tree_delete_key),
|
899 |
|
|
((splay_tree_delete_value_fn) macro_tree_delete_value),
|
900 |
|
|
((splay_tree_allocate_fn) macro_alloc),
|
901 |
|
|
((splay_tree_deallocate_fn) macro_free),
|
902 |
|
|
t));
|
903 |
|
|
|
904 |
|
|
return t;
|
905 |
|
|
}
|
906 |
|
|
|
907 |
|
|
|
908 |
|
|
void
|
909 |
|
|
free_macro_table (struct macro_table *table)
|
910 |
|
|
{
|
911 |
|
|
/* Free the source file tree. */
|
912 |
|
|
free_macro_source_file (table->main_source);
|
913 |
|
|
|
914 |
|
|
/* Free the table of macro definitions. */
|
915 |
|
|
splay_tree_delete (table->definitions);
|
916 |
|
|
}
|