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