kernel - cleanup vfs_cache debugging
[dragonfly.git] / contrib / gdb-7 / gdb / macrotab.c
blob2e5592ba0676017863c808e5e0897c48d5ba4a10
1 /* C preprocessor macro tables for GDB.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "gdb_obstack.h"
22 #include "splay-tree.h"
23 #include "filenames.h"
24 #include "symtab.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "macrotab.h"
28 #include "gdb_assert.h"
29 #include "bcache.h"
30 #include "complaints.h"
31 #include "macroexp.h"
34 /* The macro table structure. */
36 struct macro_table
38 /* The obstack this table's data should be allocated in, or zero if
39 we should use xmalloc. */
40 struct obstack *obstack;
42 /* The bcache we should use to hold macro names, argument names, and
43 definitions, or zero if we should use xmalloc. */
44 struct bcache *bcache;
46 /* The main source file for this compilation unit --- the one whose
47 name was given to the compiler. This is the root of the
48 #inclusion tree; everything else is #included from here. */
49 struct macro_source_file *main_source;
51 /* Compilation directory for all files of this macro table. It is allocated
52 on objfile's obstack. */
53 const char *comp_dir;
55 /* True if macros in this table can be redefined without issuing an
56 error. */
57 int redef_ok;
59 /* The table of macro definitions. This is a splay tree (an ordered
60 binary tree that stays balanced, effectively), sorted by macro
61 name. Where a macro gets defined more than once (presumably with
62 an #undefinition in between), we sort the definitions by the
63 order they would appear in the preprocessor's output. That is,
64 if `a.c' #includes `m.h' and then #includes `n.h', and both
65 header files #define X (with an #undef somewhere in between),
66 then the definition from `m.h' appears in our splay tree before
67 the one from `n.h'.
69 The splay tree's keys are `struct macro_key' pointers;
70 the values are `struct macro_definition' pointers.
72 The splay tree, its nodes, and the keys and values are allocated
73 in obstack, if it's non-zero, or with xmalloc otherwise. The
74 macro names, argument names, argument name arrays, and definition
75 strings are all allocated in bcache, if non-zero, or with xmalloc
76 otherwise. */
77 splay_tree definitions;
82 /* Allocation and freeing functions. */
84 /* Allocate SIZE bytes of memory appropriately for the macro table T.
85 This just checks whether T has an obstack, or whether its pieces
86 should be allocated with xmalloc. */
87 static void *
88 macro_alloc (int size, struct macro_table *t)
90 if (t->obstack)
91 return obstack_alloc (t->obstack, size);
92 else
93 return xmalloc (size);
97 static void
98 macro_free (void *object, struct macro_table *t)
100 if (t->obstack)
101 /* There are cases where we need to remove entries from a macro
102 table, even when reading debugging information. This should be
103 rare, and there's no easy way to free arbitrary data from an
104 obstack, so we just leak it. */
106 else
107 xfree (object);
111 /* If the macro table T has a bcache, then cache the LEN bytes at ADDR
112 there, and return the cached copy. Otherwise, just xmalloc a copy
113 of the bytes, and return a pointer to that. */
114 static const void *
115 macro_bcache (struct macro_table *t, const void *addr, int len)
117 if (t->bcache)
118 return bcache (addr, len, t->bcache);
119 else
121 void *copy = xmalloc (len);
123 memcpy (copy, addr, len);
124 return copy;
129 /* If the macro table T has a bcache, cache the null-terminated string
130 S there, and return a pointer to the cached copy. Otherwise,
131 xmalloc a copy and return that. */
132 static const char *
133 macro_bcache_str (struct macro_table *t, const char *s)
135 return macro_bcache (t, s, strlen (s) + 1);
139 /* Free a possibly bcached object OBJ. That is, if the macro table T
140 has a bcache, do nothing; otherwise, xfree OBJ. */
141 static void
142 macro_bcache_free (struct macro_table *t, void *obj)
144 if (t->bcache)
145 /* There are cases where we need to remove entries from a macro
146 table, even when reading debugging information. This should be
147 rare, and there's no easy way to free data from a bcache, so we
148 just leak it. */
150 else
151 xfree (obj);
156 /* Macro tree keys, w/their comparison, allocation, and freeing functions. */
158 /* A key in the splay tree. */
159 struct macro_key
161 /* The table we're in. We only need this in order to free it, since
162 the splay tree library's key and value freeing functions require
163 that the key or value contain all the information needed to free
164 themselves. */
165 struct macro_table *table;
167 /* The name of the macro. This is in the table's bcache, if it has
168 one. */
169 const char *name;
171 /* The source file and line number where the definition's scope
172 begins. This is also the line of the definition itself. */
173 struct macro_source_file *start_file;
174 int start_line;
176 /* The first source file and line after the definition's scope.
177 (That is, the scope does not include this endpoint.) If end_file
178 is zero, then the definition extends to the end of the
179 compilation unit. */
180 struct macro_source_file *end_file;
181 int end_line;
185 /* Return the #inclusion depth of the source file FILE. This is the
186 number of #inclusions it took to reach this file. For the main
187 source file, the #inclusion depth is zero; for a file it #includes
188 directly, the depth would be one; and so on. */
189 static int
190 inclusion_depth (struct macro_source_file *file)
192 int depth;
194 for (depth = 0; file->included_by; depth++)
195 file = file->included_by;
197 return depth;
201 /* Compare two source locations (from the same compilation unit).
202 This is part of the comparison function for the tree of
203 definitions.
205 LINE1 and LINE2 are line numbers in the source files FILE1 and
206 FILE2. Return a value:
207 - less than zero if {LINE,FILE}1 comes before {LINE,FILE}2,
208 - greater than zero if {LINE,FILE}1 comes after {LINE,FILE}2, or
209 - zero if they are equal.
211 When the two locations are in different source files --- perhaps
212 one is in a header, while another is in the main source file --- we
213 order them by where they would appear in the fully pre-processed
214 sources, where all the #included files have been substituted into
215 their places. */
216 static int
217 compare_locations (struct macro_source_file *file1, int line1,
218 struct macro_source_file *file2, int line2)
220 /* We want to treat positions in an #included file as coming *after*
221 the line containing the #include, but *before* the line after the
222 include. As we walk up the #inclusion tree toward the main
223 source file, we update fileX and lineX as we go; includedX
224 indicates whether the original position was from the #included
225 file. */
226 int included1 = 0;
227 int included2 = 0;
229 /* If a file is zero, that means "end of compilation unit." Handle
230 that specially. */
231 if (! file1)
233 if (! file2)
234 return 0;
235 else
236 return 1;
238 else if (! file2)
239 return -1;
241 /* If the two files are not the same, find their common ancestor in
242 the #inclusion tree. */
243 if (file1 != file2)
245 /* If one file is deeper than the other, walk up the #inclusion
246 chain until the two files are at least at the same *depth*.
247 Then, walk up both files in synchrony until they're the same
248 file. That file is the common ancestor. */
249 int depth1 = inclusion_depth (file1);
250 int depth2 = inclusion_depth (file2);
252 /* Only one of these while loops will ever execute in any given
253 case. */
254 while (depth1 > depth2)
256 line1 = file1->included_at_line;
257 file1 = file1->included_by;
258 included1 = 1;
259 depth1--;
261 while (depth2 > depth1)
263 line2 = file2->included_at_line;
264 file2 = file2->included_by;
265 included2 = 1;
266 depth2--;
269 /* Now both file1 and file2 are at the same depth. Walk toward
270 the root of the tree until we find where the branches meet. */
271 while (file1 != file2)
273 line1 = file1->included_at_line;
274 file1 = file1->included_by;
275 /* At this point, we know that the case the includedX flags
276 are trying to deal with won't come up, but we'll just
277 maintain them anyway. */
278 included1 = 1;
280 line2 = file2->included_at_line;
281 file2 = file2->included_by;
282 included2 = 1;
284 /* Sanity check. If file1 and file2 are really from the
285 same compilation unit, then they should both be part of
286 the same tree, and this shouldn't happen. */
287 gdb_assert (file1 && file2);
291 /* Now we've got two line numbers in the same file. */
292 if (line1 == line2)
294 /* They can't both be from #included files. Then we shouldn't
295 have walked up this far. */
296 gdb_assert (! included1 || ! included2);
298 /* Any #included position comes after a non-#included position
299 with the same line number in the #including file. */
300 if (included1)
301 return 1;
302 else if (included2)
303 return -1;
304 else
305 return 0;
307 else
308 return line1 - line2;
312 /* Compare a macro key KEY against NAME, the source file FILE, and
313 line number LINE.
315 Sort definitions by name; for two definitions with the same name,
316 place the one whose definition comes earlier before the one whose
317 definition comes later.
319 Return -1, 0, or 1 if key comes before, is identical to, or comes
320 after NAME, FILE, and LINE. */
321 static int
322 key_compare (struct macro_key *key,
323 const char *name, struct macro_source_file *file, int line)
325 int names = strcmp (key->name, name);
327 if (names)
328 return names;
330 return compare_locations (key->start_file, key->start_line,
331 file, line);
335 /* The macro tree comparison function, typed for the splay tree
336 library's happiness. */
337 static int
338 macro_tree_compare (splay_tree_key untyped_key1,
339 splay_tree_key untyped_key2)
341 struct macro_key *key1 = (struct macro_key *) untyped_key1;
342 struct macro_key *key2 = (struct macro_key *) untyped_key2;
344 return key_compare (key1, key2->name, key2->start_file, key2->start_line);
348 /* Construct a new macro key node for a macro in table T whose name is
349 NAME, and whose scope starts at LINE in FILE; register the name in
350 the bcache. */
351 static struct macro_key *
352 new_macro_key (struct macro_table *t,
353 const char *name,
354 struct macro_source_file *file,
355 int line)
357 struct macro_key *k = macro_alloc (sizeof (*k), t);
359 memset (k, 0, sizeof (*k));
360 k->table = t;
361 k->name = macro_bcache_str (t, name);
362 k->start_file = file;
363 k->start_line = line;
364 k->end_file = 0;
366 return k;
370 static void
371 macro_tree_delete_key (void *untyped_key)
373 struct macro_key *key = (struct macro_key *) untyped_key;
375 macro_bcache_free (key->table, (char *) key->name);
376 macro_free (key, key->table);
381 /* Building and querying the tree of #included files. */
384 /* Allocate and initialize a new source file structure. */
385 static struct macro_source_file *
386 new_source_file (struct macro_table *t,
387 const char *filename)
389 /* Get space for the source file structure itself. */
390 struct macro_source_file *f = macro_alloc (sizeof (*f), t);
392 memset (f, 0, sizeof (*f));
393 f->table = t;
394 f->filename = macro_bcache_str (t, filename);
395 f->includes = 0;
397 return f;
401 /* Free a source file, and all the source files it #included. */
402 static void
403 free_macro_source_file (struct macro_source_file *src)
405 struct macro_source_file *child, *next_child;
407 /* Free this file's children. */
408 for (child = src->includes; child; child = next_child)
410 next_child = child->next_included;
411 free_macro_source_file (child);
414 macro_bcache_free (src->table, (char *) src->filename);
415 macro_free (src, src->table);
419 struct macro_source_file *
420 macro_set_main (struct macro_table *t,
421 const char *filename)
423 /* You can't change a table's main source file. What would that do
424 to the tree? */
425 gdb_assert (! t->main_source);
427 t->main_source = new_source_file (t, filename);
429 return t->main_source;
433 struct macro_source_file *
434 macro_main (struct macro_table *t)
436 gdb_assert (t->main_source);
438 return t->main_source;
442 void
443 macro_allow_redefinitions (struct macro_table *t)
445 gdb_assert (! t->obstack);
446 t->redef_ok = 1;
450 struct macro_source_file *
451 macro_include (struct macro_source_file *source,
452 int line,
453 const char *included)
455 struct macro_source_file *new;
456 struct macro_source_file **link;
458 /* Find the right position in SOURCE's `includes' list for the new
459 file. Skip inclusions at earlier lines, until we find one at the
460 same line or later --- or until the end of the list. */
461 for (link = &source->includes;
462 *link && (*link)->included_at_line < line;
463 link = &(*link)->next_included)
466 /* Did we find another file already #included at the same line as
467 the new one? */
468 if (*link && line == (*link)->included_at_line)
470 char *link_fullname, *source_fullname;
472 /* This means the compiler is emitting bogus debug info. (GCC
473 circa March 2002 did this.) It also means that the splay
474 tree ordering function, macro_tree_compare, will abort,
475 because it can't tell which #inclusion came first. But GDB
476 should tolerate bad debug info. So:
478 First, squawk. */
480 link_fullname = macro_source_fullname (*link);
481 source_fullname = macro_source_fullname (source);
482 complaint (&symfile_complaints,
483 _("both `%s' and `%s' allegedly #included at %s:%d"),
484 included, link_fullname, source_fullname, line);
485 xfree (source_fullname);
486 xfree (link_fullname);
488 /* Now, choose a new, unoccupied line number for this
489 #inclusion, after the alleged #inclusion line. */
490 while (*link && line == (*link)->included_at_line)
492 /* This line number is taken, so try the next line. */
493 line++;
494 link = &(*link)->next_included;
498 /* At this point, we know that LINE is an unused line number, and
499 *LINK points to the entry an #inclusion at that line should
500 precede. */
501 new = new_source_file (source->table, included);
502 new->included_by = source;
503 new->included_at_line = line;
504 new->next_included = *link;
505 *link = new;
507 return new;
511 struct macro_source_file *
512 macro_lookup_inclusion (struct macro_source_file *source, const char *name)
514 /* Is SOURCE itself named NAME? */
515 if (filename_cmp (name, source->filename) == 0)
516 return source;
518 /* It's not us. Try all our children, and return the lowest. */
520 struct macro_source_file *child;
521 struct macro_source_file *best = NULL;
522 int best_depth = 0;
524 for (child = source->includes; child; child = child->next_included)
526 struct macro_source_file *result
527 = macro_lookup_inclusion (child, name);
529 if (result)
531 int result_depth = inclusion_depth (result);
533 if (! best || result_depth < best_depth)
535 best = result;
536 best_depth = result_depth;
541 return best;
547 /* Registering and looking up macro definitions. */
550 /* Construct a definition for a macro in table T. Cache all strings,
551 and the macro_definition structure itself, in T's bcache. */
552 static struct macro_definition *
553 new_macro_definition (struct macro_table *t,
554 enum macro_kind kind,
555 int argc, const char **argv,
556 const char *replacement)
558 struct macro_definition *d = macro_alloc (sizeof (*d), t);
560 memset (d, 0, sizeof (*d));
561 d->table = t;
562 d->kind = kind;
563 d->replacement = macro_bcache_str (t, replacement);
564 d->argc = argc;
566 if (kind == macro_function_like)
568 int i;
569 const char **cached_argv;
570 int cached_argv_size = argc * sizeof (*cached_argv);
572 /* Bcache all the arguments. */
573 cached_argv = alloca (cached_argv_size);
574 for (i = 0; i < argc; i++)
575 cached_argv[i] = macro_bcache_str (t, argv[i]);
577 /* Now bcache the array of argument pointers itself. */
578 d->argv = macro_bcache (t, cached_argv, cached_argv_size);
581 /* We don't bcache the entire definition structure because it's got
582 a pointer to the macro table in it; since each compilation unit
583 has its own macro table, you'd only get bcache hits for identical
584 definitions within a compilation unit, which seems unlikely.
586 "So, why do macro definitions have pointers to their macro tables
587 at all?" Well, when the splay tree library wants to free a
588 node's value, it calls the value freeing function with nothing
589 but the value itself. It makes the (apparently reasonable)
590 assumption that the value carries enough information to free
591 itself. But not all macro tables have bcaches, so not all macro
592 definitions would be bcached. There's no way to tell whether a
593 given definition is bcached without knowing which table the
594 definition belongs to. ... blah. The thing's only sixteen
595 bytes anyway, and we can still bcache the name, args, and
596 definition, so we just don't bother bcaching the definition
597 structure itself. */
598 return d;
602 /* Free a macro definition. */
603 static void
604 macro_tree_delete_value (void *untyped_definition)
606 struct macro_definition *d = (struct macro_definition *) untyped_definition;
607 struct macro_table *t = d->table;
609 if (d->kind == macro_function_like)
611 int i;
613 for (i = 0; i < d->argc; i++)
614 macro_bcache_free (t, (char *) d->argv[i]);
615 macro_bcache_free (t, (char **) d->argv);
618 macro_bcache_free (t, (char *) d->replacement);
619 macro_free (d, t);
623 /* Find the splay tree node for the definition of NAME at LINE in
624 SOURCE, or zero if there is none. */
625 static splay_tree_node
626 find_definition (const char *name,
627 struct macro_source_file *file,
628 int line)
630 struct macro_table *t = file->table;
631 splay_tree_node n;
633 /* Construct a macro_key object, just for the query. */
634 struct macro_key query;
636 query.name = name;
637 query.start_file = file;
638 query.start_line = line;
639 query.end_file = NULL;
641 n = splay_tree_lookup (t->definitions, (splay_tree_key) &query);
642 if (! n)
644 /* It's okay for us to do two queries like this: the real work
645 of the searching is done when we splay, and splaying the tree
646 a second time at the same key is a constant time operation.
647 If this still bugs you, you could always just extend the
648 splay tree library with a predecessor-or-equal operation, and
649 use that. */
650 splay_tree_node pred = splay_tree_predecessor (t->definitions,
651 (splay_tree_key) &query);
653 if (pred)
655 /* Make sure this predecessor actually has the right name.
656 We just want to search within a given name's definitions. */
657 struct macro_key *found = (struct macro_key *) pred->key;
659 if (strcmp (found->name, name) == 0)
660 n = pred;
664 if (n)
666 struct macro_key *found = (struct macro_key *) n->key;
668 /* Okay, so this definition has the right name, and its scope
669 begins before the given source location. But does its scope
670 end after the given source location? */
671 if (compare_locations (file, line, found->end_file, found->end_line) < 0)
672 return n;
673 else
674 return 0;
676 else
677 return 0;
681 /* If NAME already has a definition in scope at LINE in SOURCE, return
682 the key. If the old definition is different from the definition
683 given by KIND, ARGC, ARGV, and REPLACEMENT, complain, too.
684 Otherwise, return zero. (ARGC and ARGV are meaningless unless KIND
685 is `macro_function_like'.) */
686 static struct macro_key *
687 check_for_redefinition (struct macro_source_file *source, int line,
688 const char *name, enum macro_kind kind,
689 int argc, const char **argv,
690 const char *replacement)
692 splay_tree_node n = find_definition (name, source, line);
694 if (n)
696 struct macro_key *found_key = (struct macro_key *) n->key;
697 struct macro_definition *found_def
698 = (struct macro_definition *) n->value;
699 int same = 1;
701 /* Is this definition the same as the existing one?
702 According to the standard, this comparison needs to be done
703 on lists of tokens, not byte-by-byte, as we do here. But
704 that's too hard for us at the moment, and comparing
705 byte-by-byte will only yield false negatives (i.e., extra
706 warning messages), not false positives (i.e., unnoticed
707 definition changes). */
708 if (kind != found_def->kind)
709 same = 0;
710 else if (strcmp (replacement, found_def->replacement))
711 same = 0;
712 else if (kind == macro_function_like)
714 if (argc != found_def->argc)
715 same = 0;
716 else
718 int i;
720 for (i = 0; i < argc; i++)
721 if (strcmp (argv[i], found_def->argv[i]))
722 same = 0;
726 if (! same)
728 char *source_fullname, *found_key_fullname;
730 source_fullname = macro_source_fullname (source);
731 found_key_fullname = macro_source_fullname (found_key->start_file);
732 complaint (&symfile_complaints,
733 _("macro `%s' redefined at %s:%d; "
734 "original definition at %s:%d"),
735 name, source_fullname, line, found_key_fullname,
736 found_key->start_line);
737 xfree (found_key_fullname);
738 xfree (source_fullname);
741 return found_key;
743 else
744 return 0;
747 /* A helper function to define a new object-like macro. */
749 static void
750 macro_define_object_internal (struct macro_source_file *source, int line,
751 const char *name, const char *replacement,
752 enum macro_special_kind kind)
754 struct macro_table *t = source->table;
755 struct macro_key *k = NULL;
756 struct macro_definition *d;
758 if (! t->redef_ok)
759 k = check_for_redefinition (source, line,
760 name, macro_object_like,
761 0, 0,
762 replacement);
764 /* If we're redefining a symbol, and the existing key would be
765 identical to our new key, then the splay_tree_insert function
766 will try to delete the old definition. When the definition is
767 living on an obstack, this isn't a happy thing.
769 Since this only happens in the presence of questionable debug
770 info, we just ignore all definitions after the first. The only
771 case I know of where this arises is in GCC's output for
772 predefined macros, and all the definitions are the same in that
773 case. */
774 if (k && ! key_compare (k, name, source, line))
775 return;
777 k = new_macro_key (t, name, source, line);
778 d = new_macro_definition (t, macro_object_like, kind, 0, replacement);
779 splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
782 void
783 macro_define_object (struct macro_source_file *source, int line,
784 const char *name, const char *replacement)
786 macro_define_object_internal (source, line, name, replacement,
787 macro_ordinary);
790 /* See macrotab.h. */
792 void
793 macro_define_special (struct macro_table *table)
795 macro_define_object_internal (table->main_source, -1, "__FILE__", "",
796 macro_FILE);
797 macro_define_object_internal (table->main_source, -1, "__LINE__", "",
798 macro_LINE);
801 void
802 macro_define_function (struct macro_source_file *source, int line,
803 const char *name, int argc, const char **argv,
804 const char *replacement)
806 struct macro_table *t = source->table;
807 struct macro_key *k = NULL;
808 struct macro_definition *d;
810 if (! t->redef_ok)
811 k = check_for_redefinition (source, line,
812 name, macro_function_like,
813 argc, argv,
814 replacement);
816 /* See comments about duplicate keys in macro_define_object. */
817 if (k && ! key_compare (k, name, source, line))
818 return;
820 /* We should also check here that all the argument names in ARGV are
821 distinct. */
823 k = new_macro_key (t, name, source, line);
824 d = new_macro_definition (t, macro_function_like, argc, argv, replacement);
825 splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
829 void
830 macro_undef (struct macro_source_file *source, int line,
831 const char *name)
833 splay_tree_node n = find_definition (name, source, line);
835 if (n)
837 struct macro_key *key = (struct macro_key *) n->key;
839 /* If we're removing a definition at exactly the same point that
840 we defined it, then just delete the entry altogether. GCC
841 4.1.2 will generate DWARF that says to do this if you pass it
842 arguments like '-DFOO -UFOO -DFOO=2'. */
843 if (source == key->start_file
844 && line == key->start_line)
845 splay_tree_remove (source->table->definitions, n->key);
847 else
849 /* This function is the only place a macro's end-of-scope
850 location gets set to anything other than "end of the
851 compilation unit" (i.e., end_file is zero). So if this
852 macro already has its end-of-scope set, then we're
853 probably seeing a second #undefinition for the same
854 #definition. */
855 if (key->end_file)
857 char *source_fullname, *key_fullname;
859 source_fullname = macro_source_fullname (source);
860 key_fullname = macro_source_fullname (key->end_file);
861 complaint (&symfile_complaints,
862 _("macro '%s' is #undefined twice,"
863 " at %s:%d and %s:%d"),
864 name, source_fullname, line, key_fullname,
865 key->end_line);
866 xfree (key_fullname);
867 xfree (source_fullname);
870 /* Whether or not we've seen a prior #undefinition, wipe out
871 the old ending point, and make this the ending point. */
872 key->end_file = source;
873 key->end_line = line;
876 else
878 /* According to the ISO C standard, an #undef for a symbol that
879 has no macro definition in scope is ignored. So we should
880 ignore it too. */
881 #if 0
882 complaint (&symfile_complaints,
883 _("no definition for macro `%s' in scope to #undef at %s:%d"),
884 name, source->filename, line);
885 #endif
889 /* A helper function that rewrites the definition of a special macro,
890 when needed. */
892 static struct macro_definition *
893 fixup_definition (const char *filename, int line, struct macro_definition *def)
895 static char *saved_expansion;
897 if (saved_expansion)
899 xfree (saved_expansion);
900 saved_expansion = NULL;
903 if (def->kind == macro_object_like)
905 if (def->argc == macro_FILE)
907 saved_expansion = macro_stringify (filename);
908 def->replacement = saved_expansion;
910 else if (def->argc == macro_LINE)
912 saved_expansion = xstrprintf ("%d", line);
913 def->replacement = saved_expansion;
917 return def;
920 struct macro_definition *
921 macro_lookup_definition (struct macro_source_file *source,
922 int line, const char *name)
924 splay_tree_node n = find_definition (name, source, line);
926 if (n)
928 struct macro_definition *retval;
929 char *source_fullname;
931 source_fullname = macro_source_fullname (source);
932 retval = fixup_definition (source_fullname, line,
933 (struct macro_definition *) n->value);
934 xfree (source_fullname);
935 return retval;
937 else
938 return 0;
942 struct macro_source_file *
943 macro_definition_location (struct macro_source_file *source,
944 int line,
945 const char *name,
946 int *definition_line)
948 splay_tree_node n = find_definition (name, source, line);
950 if (n)
952 struct macro_key *key = (struct macro_key *) n->key;
954 *definition_line = key->start_line;
955 return key->start_file;
957 else
958 return 0;
962 /* The type for callback data for iterating the splay tree in
963 macro_for_each and macro_for_each_in_scope. Only the latter uses
964 the FILE and LINE fields. */
965 struct macro_for_each_data
967 macro_callback_fn fn;
968 void *user_data;
969 struct macro_source_file *file;
970 int line;
973 /* Helper function for macro_for_each. */
974 static int
975 foreach_macro (splay_tree_node node, void *arg)
977 struct macro_for_each_data *datum = (struct macro_for_each_data *) arg;
978 struct macro_key *key = (struct macro_key *) node->key;
979 struct macro_definition *def;
980 char *key_fullname;
982 key_fullname = macro_source_fullname (key->start_file);
983 def = fixup_definition (key_fullname, key->start_line,
984 (struct macro_definition *) node->value);
985 xfree (key_fullname);
987 (*datum->fn) (key->name, def, key->start_file, key->start_line,
988 datum->user_data);
989 return 0;
992 /* Call FN for every macro in TABLE. */
993 void
994 macro_for_each (struct macro_table *table, macro_callback_fn fn,
995 void *user_data)
997 struct macro_for_each_data datum;
999 datum.fn = fn;
1000 datum.user_data = user_data;
1001 datum.file = NULL;
1002 datum.line = 0;
1003 splay_tree_foreach (table->definitions, foreach_macro, &datum);
1006 static int
1007 foreach_macro_in_scope (splay_tree_node node, void *info)
1009 struct macro_for_each_data *datum = (struct macro_for_each_data *) info;
1010 struct macro_key *key = (struct macro_key *) node->key;
1011 struct macro_definition *def;
1012 char *datum_fullname;
1014 datum_fullname = macro_source_fullname (datum->file);
1015 def = fixup_definition (datum_fullname, datum->line,
1016 (struct macro_definition *) node->value);
1017 xfree (datum_fullname);
1019 /* See if this macro is defined before the passed-in line, and
1020 extends past that line. */
1021 if (compare_locations (key->start_file, key->start_line,
1022 datum->file, datum->line) < 0
1023 && (!key->end_file
1024 || compare_locations (key->end_file, key->end_line,
1025 datum->file, datum->line) >= 0))
1026 (*datum->fn) (key->name, def, key->start_file, key->start_line,
1027 datum->user_data);
1028 return 0;
1031 /* Call FN for every macro is visible in SCOPE. */
1032 void
1033 macro_for_each_in_scope (struct macro_source_file *file, int line,
1034 macro_callback_fn fn, void *user_data)
1036 struct macro_for_each_data datum;
1038 datum.fn = fn;
1039 datum.user_data = user_data;
1040 datum.file = file;
1041 datum.line = line;
1042 splay_tree_foreach (file->table->definitions,
1043 foreach_macro_in_scope, &datum);
1048 /* Creating and freeing macro tables. */
1051 struct macro_table *
1052 new_macro_table (struct obstack *obstack, struct bcache *b,
1053 const char *comp_dir)
1055 struct macro_table *t;
1057 /* First, get storage for the `struct macro_table' itself. */
1058 if (obstack)
1059 t = obstack_alloc (obstack, sizeof (*t));
1060 else
1061 t = xmalloc (sizeof (*t));
1063 memset (t, 0, sizeof (*t));
1064 t->obstack = obstack;
1065 t->bcache = b;
1066 t->main_source = NULL;
1067 t->comp_dir = comp_dir;
1068 t->redef_ok = 0;
1069 t->definitions = (splay_tree_new_with_allocator
1070 (macro_tree_compare,
1071 ((splay_tree_delete_key_fn) macro_tree_delete_key),
1072 ((splay_tree_delete_value_fn) macro_tree_delete_value),
1073 ((splay_tree_allocate_fn) macro_alloc),
1074 ((splay_tree_deallocate_fn) macro_free),
1075 t));
1077 return t;
1081 void
1082 free_macro_table (struct macro_table *table)
1084 /* Free the source file tree. */
1085 free_macro_source_file (table->main_source);
1087 /* Free the table of macro definitions. */
1088 splay_tree_delete (table->definitions);
1091 /* See macrotab.h for the comment. */
1093 char *
1094 macro_source_fullname (struct macro_source_file *file)
1096 if (file->table->comp_dir == NULL || IS_ABSOLUTE_PATH (file->filename))
1097 return xstrdup (file->filename);
1099 return concat (file->table->comp_dir, SLASH_STRING, file->filename, NULL);