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