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