Add translations for various sub-directories
[binutils-gdb.git] / gdb / macrotab.c
blob37ae0f233c539189534cd67a7989f63ab7efc250
1 /* C preprocessor macro tables for GDB.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 Contributed by Red Hat, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "gdbsupport/gdb_obstack.h"
21 #include "gdbsupport/pathstuff.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"
33 /* The macro table structure. */
35 struct macro_table
37 /* The obstack this table's data should be allocated in, or zero if
38 we should use xmalloc. */
39 struct obstack *obstack;
41 /* The bcache we should use to hold macro names, argument names, and
42 definitions, or zero if we should use xmalloc. */
43 gdb::bcache *bcache;
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;
50 /* Backlink to containing compilation unit, or NULL if there isn't one. */
51 struct compunit_symtab *compunit_symtab;
53 /* True if macros in this table can be redefined without issuing an
54 error. */
55 int redef_ok;
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'.
67 The splay tree's keys are `struct macro_key' pointers;
68 the values are `struct macro_definition' pointers.
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;
80 /* Allocation and freeing functions. */
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. */
85 static void *
86 macro_alloc (int size, struct macro_table *t)
88 if (t->obstack)
89 return obstack_alloc (t->obstack, size);
90 else
91 return xmalloc (size);
95 static void
96 macro_free (void *object, struct macro_table *t)
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. */
104 else
105 xfree (object);
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. */
112 template<typename U>
113 static const U *
114 macro_bcache (struct macro_table *t, const U *addr, int len)
116 if (t->bcache)
117 return t->bcache->insert (addr, len);
118 else
120 void *copy = xmalloc (len);
122 memcpy (copy, addr, len);
123 return (const U *) copy;
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)
134 return macro_bcache (t, s, strlen (s) + 1);
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)
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. */
149 else
150 xfree (obj);
155 /* Macro tree keys, w/their comparison, allocation, and freeing functions. */
157 /* A key in the splay tree. */
158 struct macro_key
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;
166 /* The name of the macro. This is in the table's bcache, if it has
167 one. */
168 const char *name;
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;
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;
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)
191 int depth;
193 for (depth = 0; file->included_by; depth++)
194 file = file->included_by;
196 return depth;
200 /* Compare two source locations (from the same compilation unit).
201 This is part of the comparison function for the tree of
202 definitions.
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.
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)
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;
228 /* If a file is zero, that means "end of compilation unit." Handle
229 that specially. */
230 if (! file1)
232 if (! file2)
233 return 0;
234 else
235 return 1;
237 else if (! file2)
238 return -1;
240 /* If the two files are not the same, find their common ancestor in
241 the #inclusion tree. */
242 if (file1 != file2)
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);
251 /* Only one of these while loops will ever execute in any given
252 case. */
253 while (depth1 > depth2)
255 line1 = file1->included_at_line;
256 file1 = file1->included_by;
257 included1 = 1;
258 depth1--;
260 while (depth2 > depth1)
262 line2 = file2->included_at_line;
263 file2 = file2->included_by;
264 included2 = 1;
265 depth2--;
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)
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;
279 line2 = file2->included_at_line;
280 file2 = file2->included_by;
281 included2 = 1;
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);
290 /* Now we've got two line numbers in the same file. */
291 if (line1 == line2)
293 /* They can't both be from #included files. Then we shouldn't
294 have walked up this far. */
295 gdb_assert (! included1 || ! included2);
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;
306 else
307 return line1 - line2;
311 /* Compare a macro key KEY against NAME, the source file FILE, and
312 line number LINE.
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.
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)
324 int names = strcmp (key->name, name);
326 if (names)
327 return names;
329 return compare_locations (key->start_file, key->start_line,
330 file, line);
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)
340 struct macro_key *key1 = (struct macro_key *) untyped_key1;
341 struct macro_key *key2 = (struct macro_key *) untyped_key2;
343 return key_compare (key1, key2->name, key2->start_file, key2->start_line);
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)
356 struct macro_key *k = (struct macro_key *) macro_alloc (sizeof (*k), t);
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;
365 return k;
369 static void
370 macro_tree_delete_key (void *untyped_key)
372 struct macro_key *key = (struct macro_key *) untyped_key;
374 macro_bcache_free (key->table, (char *) key->name);
375 macro_free (key, key->table);
380 /* Building and querying the tree of #included files. */
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)
388 /* Get space for the source file structure itself. */
389 struct macro_source_file *f
390 = (struct macro_source_file *) macro_alloc (sizeof (*f), t);
392 memset (f, 0, sizeof (*f));
393 f->table = t;
394 f->filename = macro_bcache_str (t, filename);
395 f->includes = 0;
397 return f;
401 /* Free a source file, and all the source files it #included. */
402 static void
403 free_macro_source_file (struct macro_source_file *src)
405 struct macro_source_file *child, *next_child;
407 /* Free this file's children. */
408 for (child = src->includes; child; child = next_child)
410 next_child = child->next_included;
411 free_macro_source_file (child);
414 macro_bcache_free (src->table, (char *) src->filename);
415 macro_free (src, src->table);
419 struct macro_source_file *
420 macro_set_main (struct macro_table *t,
421 const char *filename)
423 /* You can't change a table's main source file. What would that do
424 to the tree? */
425 gdb_assert (! t->main_source);
427 t->main_source = new_source_file (t, filename);
429 return t->main_source;
433 struct macro_source_file *
434 macro_main (struct macro_table *t)
436 gdb_assert (t->main_source);
438 return t->main_source;
442 void
443 macro_allow_redefinitions (struct macro_table *t)
445 gdb_assert (! t->obstack);
446 t->redef_ok = 1;
450 struct macro_source_file *
451 macro_include (struct macro_source_file *source,
452 int line,
453 const char *included)
455 struct macro_source_file *newobj;
456 struct macro_source_file **link;
458 /* Find the right position in SOURCE's `includes' list for the new
459 file. Skip inclusions at earlier lines, until we find one at the
460 same line or later --- or until the end of the list. */
461 for (link = &source->includes;
462 *link && (*link)->included_at_line < line;
463 link = &(*link)->next_included)
466 /* Did we find another file already #included at the same line as
467 the new one? */
468 if (*link && line == (*link)->included_at_line)
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:
476 First, squawk. */
478 std::string link_fullname = macro_source_fullname (*link);
479 std::string source_fullname = macro_source_fullname (source);
480 complaint (_("both `%s' and `%s' allegedly #included at %s:%d"),
481 included, link_fullname.c_str (), source_fullname.c_str (),
482 line);
484 /* Now, choose a new, unoccupied line number for this
485 #inclusion, after the alleged #inclusion line. */
486 while (*link && line == (*link)->included_at_line)
488 /* This line number is taken, so try the next line. */
489 line++;
490 link = &(*link)->next_included;
494 /* At this point, we know that LINE is an unused line number, and
495 *LINK points to the entry an #inclusion at that line should
496 precede. */
497 newobj = new_source_file (source->table, included);
498 newobj->included_by = source;
499 newobj->included_at_line = line;
500 newobj->next_included = *link;
501 *link = newobj;
503 return newobj;
507 struct macro_source_file *
508 macro_lookup_inclusion (struct macro_source_file *source, const char *name)
510 /* Is SOURCE itself named NAME? */
511 if (filename_cmp (name, source->filename) == 0)
512 return source;
514 /* It's not us. Try all our children, and return the lowest. */
516 struct macro_source_file *child;
517 struct macro_source_file *best = NULL;
518 int best_depth = 0;
520 for (child = source->includes; child; child = child->next_included)
522 struct macro_source_file *result
523 = macro_lookup_inclusion (child, name);
525 if (result)
527 int result_depth = inclusion_depth (result);
529 if (! best || result_depth < best_depth)
531 best = result;
532 best_depth = result_depth;
537 return best;
543 /* Registering and looking up macro definitions. */
546 /* Construct a definition for a macro in table T. Cache all strings,
547 and the macro_definition structure itself, in T's bcache. */
548 static macro_definition *
549 new_macro_definition (macro_table *t, macro_kind kind,
550 macro_special_kind special_kind,
551 const std::vector<std::string> &argv,
552 const char *replacement)
554 struct macro_definition *d
555 = (struct macro_definition *) macro_alloc (sizeof (*d), t);
557 memset (d, 0, sizeof (*d));
558 d->table = t;
559 d->kind = kind;
560 d->replacement = macro_bcache_str (t, replacement);
562 if (kind == macro_function_like)
564 d->argc = argv.size ();
566 /* Bcache all the arguments. */
567 if (d->argc > 0)
569 std::vector<const char *> cached_argv;
571 for (const auto &arg : argv)
572 cached_argv.push_back (macro_bcache_str (t, arg.c_str ()));
574 /* Now bcache the array of argument pointers itself. */
575 d->argv = macro_bcache (t, cached_argv.data (),
576 cached_argv.size () * sizeof (const char *));
579 else
580 d->argc = special_kind;
582 /* We don't bcache the entire definition structure because it's got
583 a pointer to the macro table in it; since each compilation unit
584 has its own macro table, you'd only get bcache hits for identical
585 definitions within a compilation unit, which seems unlikely.
587 "So, why do macro definitions have pointers to their macro tables
588 at all?" Well, when the splay tree library wants to free a
589 node's value, it calls the value freeing function with nothing
590 but the value itself. It makes the (apparently reasonable)
591 assumption that the value carries enough information to free
592 itself. But not all macro tables have bcaches, so not all macro
593 definitions would be bcached. There's no way to tell whether a
594 given definition is bcached without knowing which table the
595 definition belongs to. ... blah. The thing's only sixteen
596 bytes anyway, and we can still bcache the name, args, and
597 definition, so we just don't bother bcaching the definition
598 structure itself. */
599 return d;
603 /* Free a macro definition. */
604 static void
605 macro_tree_delete_value (void *untyped_definition)
607 struct macro_definition *d = (struct macro_definition *) untyped_definition;
608 struct macro_table *t = d->table;
610 if (d->kind == macro_function_like)
612 int i;
614 for (i = 0; i < d->argc; i++)
615 macro_bcache_free (t, (char *) d->argv[i]);
616 macro_bcache_free (t, (char **) d->argv);
619 macro_bcache_free (t, (char *) d->replacement);
620 macro_free (d, t);
624 /* Find the splay tree node for the definition of NAME at LINE in
625 SOURCE, or zero if there is none. */
626 static splay_tree_node
627 find_definition (const char *name,
628 struct macro_source_file *file,
629 int line)
631 struct macro_table *t = file->table;
632 splay_tree_node n;
634 /* Construct a macro_key object, just for the query. */
635 struct macro_key query;
637 query.name = name;
638 query.start_file = file;
639 query.start_line = line;
640 query.end_file = NULL;
642 n = splay_tree_lookup (t->definitions, (splay_tree_key) &query);
643 if (! n)
645 /* It's okay for us to do two queries like this: the real work
646 of the searching is done when we splay, and splaying the tree
647 a second time at the same key is a constant time operation.
648 If this still bugs you, you could always just extend the
649 splay tree library with a predecessor-or-equal operation, and
650 use that. */
651 splay_tree_node pred = splay_tree_predecessor (t->definitions,
652 (splay_tree_key) &query);
654 if (pred)
656 /* Make sure this predecessor actually has the right name.
657 We just want to search within a given name's definitions. */
658 struct macro_key *found = (struct macro_key *) pred->key;
660 if (strcmp (found->name, name) == 0)
661 n = pred;
665 if (n)
667 struct macro_key *found = (struct macro_key *) n->key;
669 /* Okay, so this definition has the right name, and its scope
670 begins before the given source location. But does its scope
671 end after the given source location? */
672 if (compare_locations (file, line, found->end_file, found->end_line) < 0)
673 return n;
674 else
675 return 0;
677 else
678 return 0;
682 /* If NAME already has a definition in scope at LINE in SOURCE, return
683 the key. If the old definition is different from the definition
684 given by KIND, ARGV, and REPLACEMENT, complain, too.
685 Otherwise, return nullptr. (ARGV is meaningless unless KIND
686 is `macro_function_like'.) */
687 static macro_key *
688 check_for_redefinition (macro_source_file *source, int line, const char *name,
689 macro_kind kind, const std::vector<std::string> &argv,
690 const char *replacement)
692 splay_tree_node n = find_definition (name, source, line);
694 if (n)
696 struct macro_key *found_key = (struct macro_key *) n->key;
697 struct macro_definition *found_def
698 = (struct macro_definition *) n->value;
699 int same = 1;
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)
714 if (argv.size () != found_def->argc)
715 same = 0;
716 else
718 int i = 0;
720 for (const auto &arg : argv)
721 if (arg != found_def->argv[i++])
722 same = 0;
726 if (! same)
728 std::string source_fullname = macro_source_fullname (source);
729 std::string found_key_fullname
730 = macro_source_fullname (found_key->start_file);
731 complaint (_("macro `%s' redefined at %s:%d; "
732 "original definition at %s:%d"),
733 name, source_fullname.c_str (), line,
734 found_key_fullname.c_str (),
735 found_key->start_line);
738 return found_key;
740 else
741 return 0;
744 /* A helper function to define a new object-like or function-like macro
745 according to KIND.
747 When KIND is macro_object_like, the possible special kind is given by
748 SPECIAL_KIND, and ARGV is meaningless.
750 When KIND is macro_function_like, ARGV gives the macro argument names, and
751 SPECIAL_KIND is meaningless. */
753 static void
754 macro_define_internal (macro_source_file *source, int line, const char *name,
755 macro_kind kind, macro_special_kind special_kind,
756 const std::vector<std::string> &argv,
757 const char *replacement)
759 struct macro_table *t = source->table;
760 struct macro_key *k = NULL;
761 struct macro_definition *d;
763 if (! t->redef_ok)
764 k = check_for_redefinition (source, line, name, kind, argv, replacement);
766 /* If we're redefining a symbol, and the existing key would be
767 identical to our new key, then the splay_tree_insert function
768 will try to delete the old definition. When the definition is
769 living on an obstack, this isn't a happy thing.
771 Since this only happens in the presence of questionable debug
772 info, we just ignore all definitions after the first. The only
773 case I know of where this arises is in GCC's output for
774 predefined macros, and all the definitions are the same in that
775 case. */
776 if (k && ! key_compare (k, name, source, line))
777 return;
779 k = new_macro_key (t, name, source, line);
780 d = new_macro_definition (t, kind, special_kind, argv, replacement);
781 splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
784 /* A helper function to define a new object-like macro. */
786 static void
787 macro_define_object_internal (struct macro_source_file *source, int line,
788 const char *name, const char *replacement,
789 enum macro_special_kind special_kind)
791 macro_define_internal (source, line, name, macro_object_like, special_kind,
792 {}, replacement);
795 void
796 macro_define_object (struct macro_source_file *source, int line,
797 const char *name, const char *replacement)
799 macro_define_object_internal (source, line, name, replacement,
800 macro_ordinary);
803 /* See macrotab.h. */
805 void
806 macro_define_special (struct macro_table *table)
808 macro_define_object_internal (table->main_source, -1, "__FILE__", "",
809 macro_FILE);
810 macro_define_object_internal (table->main_source, -1, "__LINE__", "",
811 macro_LINE);
814 void
815 macro_define_function (macro_source_file *source, int line, const char *name,
816 const std::vector<std::string> &argv,
817 const char *replacement)
819 macro_define_internal (source, line, name, macro_function_like,
820 macro_ordinary, argv, replacement);
823 void
824 macro_undef (struct macro_source_file *source, int line,
825 const char *name)
827 splay_tree_node n = find_definition (name, source, line);
829 if (n)
831 struct macro_key *key = (struct macro_key *) n->key;
833 /* If we're removing a definition at exactly the same point that
834 we defined it, then just delete the entry altogether. GCC
835 4.1.2 will generate DWARF that says to do this if you pass it
836 arguments like '-DFOO -UFOO -DFOO=2'. */
837 if (source == key->start_file
838 && line == key->start_line)
839 splay_tree_remove (source->table->definitions, n->key);
841 else
843 /* This function is the only place a macro's end-of-scope
844 location gets set to anything other than "end of the
845 compilation unit" (i.e., end_file is zero). So if this
846 macro already has its end-of-scope set, then we're
847 probably seeing a second #undefinition for the same
848 #definition. */
849 if (key->end_file)
851 std::string source_fullname = macro_source_fullname (source);
852 std::string key_fullname = macro_source_fullname (key->end_file);
853 complaint (_("macro '%s' is #undefined twice,"
854 " at %s:%d and %s:%d"),
855 name, source_fullname.c_str (), line,
856 key_fullname.c_str (),
857 key->end_line);
860 /* Whether or not we've seen a prior #undefinition, wipe out
861 the old ending point, and make this the ending point. */
862 key->end_file = source;
863 key->end_line = line;
866 else
868 /* According to the ISO C standard, an #undef for a symbol that
869 has no macro definition in scope is ignored. So we should
870 ignore it too. */
871 #if 0
872 complaint (_("no definition for macro `%s' in scope to #undef at %s:%d"),
873 name, source->filename, line);
874 #endif
878 /* A helper function that rewrites the definition of a special macro,
879 when needed. */
881 static struct macro_definition *
882 fixup_definition (const char *filename, int line, struct macro_definition *def)
884 static gdb::unique_xmalloc_ptr<char> saved_expansion;
886 if (def->kind == macro_object_like)
888 if (def->argc == macro_FILE)
890 saved_expansion = macro_stringify (filename);
891 def->replacement = saved_expansion.get ();
893 else if (def->argc == macro_LINE)
895 saved_expansion = xstrprintf ("%d", line);
896 def->replacement = saved_expansion.get ();
900 return def;
903 struct macro_definition *
904 macro_lookup_definition (struct macro_source_file *source,
905 int line, const char *name)
907 splay_tree_node n = find_definition (name, source, line);
909 if (n)
911 std::string source_fullname = macro_source_fullname (source);
912 return fixup_definition (source_fullname.c_str (), line,
913 (struct macro_definition *) n->value);
915 else
916 return 0;
920 struct macro_source_file *
921 macro_definition_location (struct macro_source_file *source,
922 int line,
923 const char *name,
924 int *definition_line)
926 splay_tree_node n = find_definition (name, source, line);
928 if (n)
930 struct macro_key *key = (struct macro_key *) n->key;
932 *definition_line = key->start_line;
933 return key->start_file;
935 else
936 return 0;
940 /* The type for callback data for iterating the splay tree in
941 macro_for_each and macro_for_each_in_scope. Only the latter uses
942 the FILE and LINE fields. */
943 struct macro_for_each_data
945 gdb::function_view<macro_callback_fn> fn;
946 struct macro_source_file *file;
947 int line;
950 /* Helper function for macro_for_each. */
951 static int
952 foreach_macro (splay_tree_node node, void *arg)
954 struct macro_for_each_data *datum = (struct macro_for_each_data *) arg;
955 struct macro_key *key = (struct macro_key *) node->key;
956 struct macro_definition *def;
958 std::string key_fullname = macro_source_fullname (key->start_file);
959 def = fixup_definition (key_fullname.c_str (), key->start_line,
960 (struct macro_definition *) node->value);
962 datum->fn (key->name, def, key->start_file, key->start_line);
963 return 0;
966 /* Call FN for every macro in TABLE. */
967 void
968 macro_for_each (struct macro_table *table,
969 gdb::function_view<macro_callback_fn> fn)
971 struct macro_for_each_data datum;
973 datum.fn = fn;
974 datum.file = NULL;
975 datum.line = 0;
976 splay_tree_foreach (table->definitions, foreach_macro, &datum);
979 static int
980 foreach_macro_in_scope (splay_tree_node node, void *info)
982 struct macro_for_each_data *datum = (struct macro_for_each_data *) info;
983 struct macro_key *key = (struct macro_key *) node->key;
984 struct macro_definition *def;
986 std::string datum_fullname = macro_source_fullname (datum->file);
987 def = fixup_definition (datum_fullname.c_str (), datum->line,
988 (struct macro_definition *) node->value);
990 /* See if this macro is defined before the passed-in line, and
991 extends past that line. */
992 if (compare_locations (key->start_file, key->start_line,
993 datum->file, datum->line) < 0
994 && (!key->end_file
995 || compare_locations (key->end_file, key->end_line,
996 datum->file, datum->line) >= 0))
997 datum->fn (key->name, def, key->start_file, key->start_line);
998 return 0;
1001 /* Call FN for every macro is visible in SCOPE. */
1002 void
1003 macro_for_each_in_scope (struct macro_source_file *file, int line,
1004 gdb::function_view<macro_callback_fn> fn)
1006 struct macro_for_each_data datum;
1008 datum.fn = fn;
1009 datum.file = file;
1010 datum.line = line;
1011 splay_tree_foreach (file->table->definitions,
1012 foreach_macro_in_scope, &datum);
1017 /* Creating and freeing macro tables. */
1020 struct macro_table *
1021 new_macro_table (struct obstack *obstack, gdb::bcache *b,
1022 struct compunit_symtab *cust)
1024 struct macro_table *t;
1026 /* First, get storage for the `struct macro_table' itself. */
1027 if (obstack)
1028 t = XOBNEW (obstack, struct macro_table);
1029 else
1030 t = XNEW (struct macro_table);
1032 memset (t, 0, sizeof (*t));
1033 t->obstack = obstack;
1034 t->bcache = b;
1035 t->main_source = NULL;
1036 t->compunit_symtab = cust;
1037 t->redef_ok = 0;
1038 t->definitions = (splay_tree_new_with_allocator
1039 (macro_tree_compare,
1040 ((splay_tree_delete_key_fn) macro_tree_delete_key),
1041 ((splay_tree_delete_value_fn) macro_tree_delete_value),
1042 ((splay_tree_allocate_fn) macro_alloc),
1043 ((splay_tree_deallocate_fn) macro_free),
1044 t));
1046 return t;
1050 void
1051 free_macro_table (struct macro_table *table)
1053 /* Free the source file tree. */
1054 free_macro_source_file (table->main_source);
1056 /* Free the table of macro definitions. */
1057 splay_tree_delete (table->definitions);
1060 /* See macrotab.h for the comment. */
1062 std::string
1063 macro_source_fullname (struct macro_source_file *file)
1065 const char *comp_dir = NULL;
1067 if (file->table->compunit_symtab != NULL)
1068 comp_dir = file->table->compunit_symtab->dirname ();
1070 if (comp_dir == NULL || IS_ABSOLUTE_PATH (file->filename))
1071 return file->filename;
1073 return path_join (comp_dir, file->filename);