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