1 /* C preprocessor macro tables for GDB.
2 Copyright (C) 2002-2018 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/>. */
21 #include "gdb_obstack.h"
22 #include "splay-tree.h"
23 #include "filenames.h"
29 #include "complaints.h"
33 /* The macro table structure. */
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 struct 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
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
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
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. */
86 macro_alloc (int size
, struct macro_table
*t
)
89 return obstack_alloc (t
->obstack
, size
);
91 return xmalloc (size
);
96 macro_free (void *object
, struct macro_table
*t
)
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. */
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. */
113 macro_bcache (struct macro_table
*t
, const void *addr
, int len
)
116 return bcache (addr
, len
, t
->bcache
);
119 void *copy
= xmalloc (len
);
121 memcpy (copy
, addr
, len
);
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. */
131 macro_bcache_str (struct macro_table
*t
, const char *s
)
133 return (const char *) macro_bcache (t
, s
, strlen (s
) + 1);
137 /* Free a possibly bcached object OBJ. That is, if the macro table T
138 has a bcache, do nothing; otherwise, xfree OBJ. */
140 macro_bcache_free (struct macro_table
*t
, void *obj
)
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
154 /* Macro tree keys, w/their comparison, allocation, and freeing functions. */
156 /* A key in the splay tree. */
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
163 struct macro_table
*table
;
165 /* The name of the macro. This is in the table's bcache, if it has
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
;
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
178 struct macro_source_file
*end_file
;
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. */
188 inclusion_depth (struct macro_source_file
*file
)
192 for (depth
= 0; file
->included_by
; depth
++)
193 file
= file
->included_by
;
199 /* Compare two source locations (from the same compilation unit).
200 This is part of the comparison function for the tree of
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.
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
215 compare_locations (struct macro_source_file
*file1
, int line1
,
216 struct macro_source_file
*file2
, int line2
)
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
227 /* If a file is zero, that means "end of compilation unit." Handle
239 /* If the two files are not the same, find their common ancestor in
240 the #inclusion tree. */
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
);
250 /* Only one of these while loops will ever execute in any given
252 while (depth1
> depth2
)
254 line1
= file1
->included_at_line
;
255 file1
= file1
->included_by
;
259 while (depth2
> depth1
)
261 line2
= file2
->included_at_line
;
262 file2
= file2
->included_by
;
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
)
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. */
278 line2
= file2
->included_at_line
;
279 file2
= file2
->included_by
;
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
);
289 /* Now we've got two line numbers in the same file. */
292 /* They can't both be from #included files. Then we shouldn't
293 have walked up this far. */
294 gdb_assert (! included1
|| ! included2
);
296 /* Any #included position comes after a non-#included position
297 with the same line number in the #including file. */
306 return line1
- line2
;
310 /* Compare a macro key KEY against NAME, the source file FILE, and
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.
317 Return -1, 0, or 1 if key comes before, is identical to, or comes
318 after NAME, FILE, and LINE. */
320 key_compare (struct macro_key
*key
,
321 const char *name
, struct macro_source_file
*file
, int line
)
323 int names
= strcmp (key
->name
, name
);
328 return compare_locations (key
->start_file
, key
->start_line
,
333 /* The macro tree comparison function, typed for the splay tree
334 library's happiness. */
336 macro_tree_compare (splay_tree_key untyped_key1
,
337 splay_tree_key untyped_key2
)
339 struct macro_key
*key1
= (struct macro_key
*) untyped_key1
;
340 struct macro_key
*key2
= (struct macro_key
*) untyped_key2
;
342 return key_compare (key1
, key2
->name
, key2
->start_file
, key2
->start_line
);
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
349 static struct macro_key
*
350 new_macro_key (struct macro_table
*t
,
352 struct macro_source_file
*file
,
355 struct macro_key
*k
= (struct macro_key
*) macro_alloc (sizeof (*k
), t
);
357 memset (k
, 0, sizeof (*k
));
359 k
->name
= macro_bcache_str (t
, name
);
360 k
->start_file
= file
;
361 k
->start_line
= line
;
369 macro_tree_delete_key (void *untyped_key
)
371 struct macro_key
*key
= (struct macro_key
*) untyped_key
;
373 macro_bcache_free (key
->table
, (char *) key
->name
);
374 macro_free (key
, key
->table
);
379 /* Building and querying the tree of #included files. */
382 /* Allocate and initialize a new source file structure. */
383 static struct macro_source_file
*
384 new_source_file (struct macro_table
*t
,
385 const char *filename
)
387 /* Get space for the source file structure itself. */
388 struct macro_source_file
*f
389 = (struct macro_source_file
*) macro_alloc (sizeof (*f
), t
);
391 memset (f
, 0, sizeof (*f
));
393 f
->filename
= macro_bcache_str (t
, filename
);
400 /* Free a source file, and all the source files it #included. */
402 free_macro_source_file (struct macro_source_file
*src
)
404 struct macro_source_file
*child
, *next_child
;
406 /* Free this file's children. */
407 for (child
= src
->includes
; child
; child
= next_child
)
409 next_child
= child
->next_included
;
410 free_macro_source_file (child
);
413 macro_bcache_free (src
->table
, (char *) src
->filename
);
414 macro_free (src
, src
->table
);
418 struct macro_source_file
*
419 macro_set_main (struct macro_table
*t
,
420 const char *filename
)
422 /* You can't change a table's main source file. What would that do
424 gdb_assert (! t
->main_source
);
426 t
->main_source
= new_source_file (t
, filename
);
428 return t
->main_source
;
432 struct macro_source_file
*
433 macro_main (struct macro_table
*t
)
435 gdb_assert (t
->main_source
);
437 return t
->main_source
;
442 macro_allow_redefinitions (struct macro_table
*t
)
444 gdb_assert (! t
->obstack
);
449 struct macro_source_file
*
450 macro_include (struct macro_source_file
*source
,
452 const char *included
)
454 struct macro_source_file
*newobj
;
455 struct macro_source_file
**link
;
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
)
465 /* Did we find another file already #included at the same line as
467 if (*link
&& line
== (*link
)->included_at_line
)
469 char *link_fullname
, *source_fullname
;
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:
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
);
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
)
491 /* This line number is taken, so try the next line. */
493 link
= &(*link
)->next_included
;
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
500 newobj
= new_source_file (source
->table
, included
);
501 newobj
->included_by
= source
;
502 newobj
->included_at_line
= line
;
503 newobj
->next_included
= *link
;
510 struct macro_source_file
*
511 macro_lookup_inclusion (struct macro_source_file
*source
, const char *name
)
513 /* Is SOURCE itself named NAME? */
514 if (filename_cmp (name
, source
->filename
) == 0)
517 /* It's not us. Try all our children, and return the lowest. */
519 struct macro_source_file
*child
;
520 struct macro_source_file
*best
= NULL
;
523 for (child
= source
->includes
; child
; child
= child
->next_included
)
525 struct macro_source_file
*result
526 = macro_lookup_inclusion (child
, name
);
530 int result_depth
= inclusion_depth (result
);
532 if (! best
|| result_depth
< best_depth
)
535 best_depth
= result_depth
;
546 /* Registering and looking up macro definitions. */
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
)
557 struct macro_definition
*d
558 = (struct macro_definition
*) macro_alloc (sizeof (*d
), t
);
560 memset (d
, 0, sizeof (*d
));
563 d
->replacement
= macro_bcache_str (t
, replacement
);
566 if (kind
== macro_function_like
)
569 const char **cached_argv
;
570 int cached_argv_size
= argc
* sizeof (*cached_argv
);
572 /* Bcache all the arguments. */
573 cached_argv
= (const char **) alloca (cached_argv_size
);
574 for (i
= 0; i
< argc
; i
++)
575 cached_argv
[i
] = macro_bcache_str (t
, argv
[i
]);
577 /* Now bcache the array of argument pointers itself. */
578 d
->argv
= ((const char * const *)
579 macro_bcache (t
, cached_argv
, cached_argv_size
));
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
603 /* Free a macro definition. */
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
)
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
);
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
,
631 struct macro_table
*t
= file
->table
;
634 /* Construct a macro_key object, just for the query. */
635 struct macro_key query
;
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
);
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
651 splay_tree_node pred
= splay_tree_predecessor (t
->definitions
,
652 (splay_tree_key
) &query
);
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)
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)
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, ARGC, ARGV, and REPLACEMENT, complain, too.
685 Otherwise, return zero. (ARGC and ARGV are meaningless unless KIND
686 is `macro_function_like'.) */
687 static struct macro_key
*
688 check_for_redefinition (struct macro_source_file
*source
, int line
,
689 const char *name
, enum macro_kind kind
,
690 int argc
, const char **argv
,
691 const char *replacement
)
693 splay_tree_node n
= find_definition (name
, source
, line
);
697 struct macro_key
*found_key
= (struct macro_key
*) n
->key
;
698 struct macro_definition
*found_def
699 = (struct macro_definition
*) n
->value
;
702 /* Is this definition the same as the existing one?
703 According to the standard, this comparison needs to be done
704 on lists of tokens, not byte-by-byte, as we do here. But
705 that's too hard for us at the moment, and comparing
706 byte-by-byte will only yield false negatives (i.e., extra
707 warning messages), not false positives (i.e., unnoticed
708 definition changes). */
709 if (kind
!= found_def
->kind
)
711 else if (strcmp (replacement
, found_def
->replacement
))
713 else if (kind
== macro_function_like
)
715 if (argc
!= found_def
->argc
)
721 for (i
= 0; i
< argc
; i
++)
722 if (strcmp (argv
[i
], found_def
->argv
[i
]))
729 char *source_fullname
, *found_key_fullname
;
731 source_fullname
= macro_source_fullname (source
);
732 found_key_fullname
= macro_source_fullname (found_key
->start_file
);
733 complaint (&symfile_complaints
,
734 _("macro `%s' redefined at %s:%d; "
735 "original definition at %s:%d"),
736 name
, source_fullname
, line
, found_key_fullname
,
737 found_key
->start_line
);
738 xfree (found_key_fullname
);
739 xfree (source_fullname
);
748 /* A helper function to define a new object-like macro. */
751 macro_define_object_internal (struct macro_source_file
*source
, int line
,
752 const char *name
, const char *replacement
,
753 enum macro_special_kind kind
)
755 struct macro_table
*t
= source
->table
;
756 struct macro_key
*k
= NULL
;
757 struct macro_definition
*d
;
760 k
= check_for_redefinition (source
, line
,
761 name
, macro_object_like
,
765 /* If we're redefining a symbol, and the existing key would be
766 identical to our new key, then the splay_tree_insert function
767 will try to delete the old definition. When the definition is
768 living on an obstack, this isn't a happy thing.
770 Since this only happens in the presence of questionable debug
771 info, we just ignore all definitions after the first. The only
772 case I know of where this arises is in GCC's output for
773 predefined macros, and all the definitions are the same in that
775 if (k
&& ! key_compare (k
, name
, source
, line
))
778 k
= new_macro_key (t
, name
, source
, line
);
779 d
= new_macro_definition (t
, macro_object_like
, kind
, 0, replacement
);
780 splay_tree_insert (t
->definitions
, (splay_tree_key
) k
, (splay_tree_value
) d
);
784 macro_define_object (struct macro_source_file
*source
, int line
,
785 const char *name
, const char *replacement
)
787 macro_define_object_internal (source
, line
, name
, replacement
,
791 /* See macrotab.h. */
794 macro_define_special (struct macro_table
*table
)
796 macro_define_object_internal (table
->main_source
, -1, "__FILE__", "",
798 macro_define_object_internal (table
->main_source
, -1, "__LINE__", "",
803 macro_define_function (struct macro_source_file
*source
, int line
,
804 const char *name
, int argc
, const char **argv
,
805 const char *replacement
)
807 struct macro_table
*t
= source
->table
;
808 struct macro_key
*k
= NULL
;
809 struct macro_definition
*d
;
812 k
= check_for_redefinition (source
, line
,
813 name
, macro_function_like
,
817 /* See comments about duplicate keys in macro_define_object. */
818 if (k
&& ! key_compare (k
, name
, source
, line
))
821 /* We should also check here that all the argument names in ARGV are
824 k
= new_macro_key (t
, name
, source
, line
);
825 d
= new_macro_definition (t
, macro_function_like
, argc
, argv
, replacement
);
826 splay_tree_insert (t
->definitions
, (splay_tree_key
) k
, (splay_tree_value
) d
);
831 macro_undef (struct macro_source_file
*source
, int line
,
834 splay_tree_node n
= find_definition (name
, source
, line
);
838 struct macro_key
*key
= (struct macro_key
*) n
->key
;
840 /* If we're removing a definition at exactly the same point that
841 we defined it, then just delete the entry altogether. GCC
842 4.1.2 will generate DWARF that says to do this if you pass it
843 arguments like '-DFOO -UFOO -DFOO=2'. */
844 if (source
== key
->start_file
845 && line
== key
->start_line
)
846 splay_tree_remove (source
->table
->definitions
, n
->key
);
850 /* This function is the only place a macro's end-of-scope
851 location gets set to anything other than "end of the
852 compilation unit" (i.e., end_file is zero). So if this
853 macro already has its end-of-scope set, then we're
854 probably seeing a second #undefinition for the same
858 char *source_fullname
, *key_fullname
;
860 source_fullname
= macro_source_fullname (source
);
861 key_fullname
= macro_source_fullname (key
->end_file
);
862 complaint (&symfile_complaints
,
863 _("macro '%s' is #undefined twice,"
864 " at %s:%d and %s:%d"),
865 name
, source_fullname
, line
, key_fullname
,
867 xfree (key_fullname
);
868 xfree (source_fullname
);
871 /* Whether or not we've seen a prior #undefinition, wipe out
872 the old ending point, and make this the ending point. */
873 key
->end_file
= source
;
874 key
->end_line
= line
;
879 /* According to the ISO C standard, an #undef for a symbol that
880 has no macro definition in scope is ignored. So we should
883 complaint (&symfile_complaints
,
884 _("no definition for macro `%s' in scope to #undef at %s:%d"),
885 name
, source
->filename
, line
);
890 /* A helper function that rewrites the definition of a special macro,
893 static struct macro_definition
*
894 fixup_definition (const char *filename
, int line
, struct macro_definition
*def
)
896 static char *saved_expansion
;
900 xfree (saved_expansion
);
901 saved_expansion
= NULL
;
904 if (def
->kind
== macro_object_like
)
906 if (def
->argc
== macro_FILE
)
908 saved_expansion
= macro_stringify (filename
);
909 def
->replacement
= saved_expansion
;
911 else if (def
->argc
== macro_LINE
)
913 saved_expansion
= xstrprintf ("%d", line
);
914 def
->replacement
= saved_expansion
;
921 struct macro_definition
*
922 macro_lookup_definition (struct macro_source_file
*source
,
923 int line
, const char *name
)
925 splay_tree_node n
= find_definition (name
, source
, line
);
929 struct macro_definition
*retval
;
930 char *source_fullname
;
932 source_fullname
= macro_source_fullname (source
);
933 retval
= fixup_definition (source_fullname
, line
,
934 (struct macro_definition
*) n
->value
);
935 xfree (source_fullname
);
943 struct macro_source_file
*
944 macro_definition_location (struct macro_source_file
*source
,
947 int *definition_line
)
949 splay_tree_node n
= find_definition (name
, source
, line
);
953 struct macro_key
*key
= (struct macro_key
*) n
->key
;
955 *definition_line
= key
->start_line
;
956 return key
->start_file
;
963 /* The type for callback data for iterating the splay tree in
964 macro_for_each and macro_for_each_in_scope. Only the latter uses
965 the FILE and LINE fields. */
966 struct macro_for_each_data
968 gdb::function_view
<macro_callback_fn
> fn
;
969 struct macro_source_file
*file
;
973 /* Helper function for macro_for_each. */
975 foreach_macro (splay_tree_node node
, void *arg
)
977 struct macro_for_each_data
*datum
= (struct macro_for_each_data
*) arg
;
978 struct macro_key
*key
= (struct macro_key
*) node
->key
;
979 struct macro_definition
*def
;
982 key_fullname
= macro_source_fullname (key
->start_file
);
983 def
= fixup_definition (key_fullname
, key
->start_line
,
984 (struct macro_definition
*) node
->value
);
985 xfree (key_fullname
);
987 datum
->fn (key
->name
, def
, key
->start_file
, key
->start_line
);
991 /* Call FN for every macro in TABLE. */
993 macro_for_each (struct macro_table
*table
,
994 gdb::function_view
<macro_callback_fn
> fn
)
996 struct macro_for_each_data datum
;
1001 splay_tree_foreach (table
->definitions
, foreach_macro
, &datum
);
1005 foreach_macro_in_scope (splay_tree_node node
, void *info
)
1007 struct macro_for_each_data
*datum
= (struct macro_for_each_data
*) info
;
1008 struct macro_key
*key
= (struct macro_key
*) node
->key
;
1009 struct macro_definition
*def
;
1010 char *datum_fullname
;
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
);
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
1022 || compare_locations (key
->end_file
, key
->end_line
,
1023 datum
->file
, datum
->line
) >= 0))
1024 datum
->fn (key
->name
, def
, key
->start_file
, key
->start_line
);
1028 /* Call FN for every macro is visible in SCOPE. */
1030 macro_for_each_in_scope (struct macro_source_file
*file
, int line
,
1031 gdb::function_view
<macro_callback_fn
> fn
)
1033 struct macro_for_each_data datum
;
1038 splay_tree_foreach (file
->table
->definitions
,
1039 foreach_macro_in_scope
, &datum
);
1044 /* Creating and freeing macro tables. */
1047 struct macro_table
*
1048 new_macro_table (struct obstack
*obstack
, struct bcache
*b
,
1049 struct compunit_symtab
*cust
)
1051 struct macro_table
*t
;
1053 /* First, get storage for the `struct macro_table' itself. */
1055 t
= XOBNEW (obstack
, struct macro_table
);
1057 t
= XNEW (struct macro_table
);
1059 memset (t
, 0, sizeof (*t
));
1060 t
->obstack
= obstack
;
1062 t
->main_source
= NULL
;
1063 t
->compunit_symtab
= cust
;
1065 t
->definitions
= (splay_tree_new_with_allocator
1066 (macro_tree_compare
,
1067 ((splay_tree_delete_key_fn
) macro_tree_delete_key
),
1068 ((splay_tree_delete_value_fn
) macro_tree_delete_value
),
1069 ((splay_tree_allocate_fn
) macro_alloc
),
1070 ((splay_tree_deallocate_fn
) macro_free
),
1078 free_macro_table (struct macro_table
*table
)
1080 /* Free the source file tree. */
1081 free_macro_source_file (table
->main_source
);
1083 /* Free the table of macro definitions. */
1084 splay_tree_delete (table
->definitions
);
1087 /* See macrotab.h for the comment. */
1090 macro_source_fullname (struct macro_source_file
*file
)
1092 const char *comp_dir
= NULL
;
1094 if (file
->table
->compunit_symtab
!= NULL
)
1095 comp_dir
= COMPUNIT_DIRNAME (file
->table
->compunit_symtab
);
1097 if (comp_dir
== NULL
|| IS_ABSOLUTE_PATH (file
->filename
))
1098 return xstrdup (file
->filename
);
1100 return concat (comp_dir
, SLASH_STRING
, file
->filename
, (char *) NULL
);