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