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