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