gdb/macrotab.c - gdb

Data types defined

Functions defined

Source code

  1. /* C preprocessor macro tables for GDB.
  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3.    Contributed by Red Hat, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "gdb_obstack.h"
  17. #include "splay-tree.h"
  18. #include "filenames.h"
  19. #include "symtab.h"
  20. #include "symfile.h"
  21. #include "objfiles.h"
  22. #include "macrotab.h"
  23. #include "bcache.h"
  24. #include "complaints.h"
  25. #include "macroexp.h"


  26. /* The macro table structure.  */

  27. struct macro_table
  28. {
  29.   /* The obstack this table's data should be allocated in, or zero if
  30.      we should use xmalloc.  */
  31.   struct obstack *obstack;

  32.   /* The bcache we should use to hold macro names, argument names, and
  33.      definitions, or zero if we should use xmalloc.  */
  34.   struct bcache *bcache;

  35.   /* The main source file for this compilation unit --- the one whose
  36.      name was given to the compiler.  This is the root of the
  37.      #inclusion tree; everything else is #included from here.  */
  38.   struct macro_source_file *main_source;

  39.   /* Backlink to containing compilation unit, or NULL if there isn't one.  */
  40.   struct compunit_symtab *compunit_symtab;

  41.   /* True if macros in this table can be redefined without issuing an
  42.      error.  */
  43.   int redef_ok;

  44.   /* The table of macro definitions.  This is a splay tree (an ordered
  45.      binary tree that stays balanced, effectively), sorted by macro
  46.      name.  Where a macro gets defined more than once (presumably with
  47.      an #undefinition in between), we sort the definitions by the
  48.      order they would appear in the preprocessor's output.  That is,
  49.      if `a.c' #includes `m.h' and then #includes `n.h', and both
  50.      header files #define X (with an #undef somewhere in between),
  51.      then the definition from `m.h' appears in our splay tree before
  52.      the one from `n.h'.

  53.      The splay tree's keys are `struct macro_key' pointers;
  54.      the values are `struct macro_definition' pointers.

  55.      The splay tree, its nodes, and the keys and values are allocated
  56.      in obstack, if it's non-zero, or with xmalloc otherwise.  The
  57.      macro names, argument names, argument name arrays, and definition
  58.      strings are all allocated in bcache, if non-zero, or with xmalloc
  59.      otherwise.  */
  60.   splay_tree definitions;
  61. };



  62. /* Allocation and freeing functions.  */

  63. /* Allocate SIZE bytes of memory appropriately for the macro table T.
  64.    This just checks whether T has an obstack, or whether its pieces
  65.    should be allocated with xmalloc.  */
  66. static void *
  67. macro_alloc (int size, struct macro_table *t)
  68. {
  69.   if (t->obstack)
  70.     return obstack_alloc (t->obstack, size);
  71.   else
  72.     return xmalloc (size);
  73. }


  74. static void
  75. macro_free (void *object, struct macro_table *t)
  76. {
  77.   if (t->obstack)
  78.     /* There are cases where we need to remove entries from a macro
  79.        table, even when reading debugging information.  This should be
  80.        rare, and there's no easy way to free arbitrary data from an
  81.        obstack, so we just leak it.  */
  82.     ;
  83.   else
  84.     xfree (object);
  85. }


  86. /* If the macro table T has a bcache, then cache the LEN bytes at ADDR
  87.    there, and return the cached copy.  Otherwise, just xmalloc a copy
  88.    of the bytes, and return a pointer to that.  */
  89. static const void *
  90. macro_bcache (struct macro_table *t, const void *addr, int len)
  91. {
  92.   if (t->bcache)
  93.     return bcache (addr, len, t->bcache);
  94.   else
  95.     {
  96.       void *copy = xmalloc (len);

  97.       memcpy (copy, addr, len);
  98.       return copy;
  99.     }
  100. }


  101. /* If the macro table T has a bcache, cache the null-terminated string
  102.    S there, and return a pointer to the cached copy.  Otherwise,
  103.    xmalloc a copy and return that.  */
  104. static const char *
  105. macro_bcache_str (struct macro_table *t, const char *s)
  106. {
  107.   return macro_bcache (t, s, strlen (s) + 1);
  108. }


  109. /* Free a possibly bcached object OBJ.  That is, if the macro table T
  110.    has a bcache, do nothing; otherwise, xfree OBJ.  */
  111. static void
  112. macro_bcache_free (struct macro_table *t, void *obj)
  113. {
  114.   if (t->bcache)
  115.     /* There are cases where we need to remove entries from a macro
  116.        table, even when reading debugging information.  This should be
  117.        rare, and there's no easy way to free data from a bcache, so we
  118.        just leak it.  */
  119.     ;
  120.   else
  121.     xfree (obj);
  122. }



  123. /* Macro tree keys, w/their comparison, allocation, and freeing functions.  */

  124. /* A key in the splay tree.  */
  125. struct macro_key
  126. {
  127.   /* The table we're in.  We only need this in order to free it, since
  128.      the splay tree library's key and value freeing functions require
  129.      that the key or value contain all the information needed to free
  130.      themselves.  */
  131.   struct macro_table *table;

  132.   /* The name of the macro.  This is in the table's bcache, if it has
  133.      one.  */
  134.   const char *name;

  135.   /* The source file and line number where the definition's scope
  136.      begins.  This is also the line of the definition itself.  */
  137.   struct macro_source_file *start_file;
  138.   int start_line;

  139.   /* The first source file and line after the definition's scope.
  140.      (That is, the scope does not include this endpoint.)  If end_file
  141.      is zero, then the definition extends to the end of the
  142.      compilation unit.  */
  143.   struct macro_source_file *end_file;
  144.   int end_line;
  145. };


  146. /* Return the #inclusion depth of the source file FILE.  This is the
  147.    number of #inclusions it took to reach this file.  For the main
  148.    source file, the #inclusion depth is zero; for a file it #includes
  149.    directly, the depth would be one; and so on.  */
  150. static int
  151. inclusion_depth (struct macro_source_file *file)
  152. {
  153.   int depth;

  154.   for (depth = 0; file->included_by; depth++)
  155.     file = file->included_by;

  156.   return depth;
  157. }


  158. /* Compare two source locations (from the same compilation unit).
  159.    This is part of the comparison function for the tree of
  160.    definitions.

  161.    LINE1 and LINE2 are line numbers in the source files FILE1 and
  162.    FILE2.  Return a value:
  163.    - less than zero if {LINE,FILE}1 comes before {LINE,FILE}2,
  164.    - greater than zero if {LINE,FILE}1 comes after {LINE,FILE}2, or
  165.    - zero if they are equal.

  166.    When the two locations are in different source files --- perhaps
  167.    one is in a header, while another is in the main source file --- we
  168.    order them by where they would appear in the fully pre-processed
  169.    sources, where all the #included files have been substituted into
  170.    their places.  */
  171. static int
  172. compare_locations (struct macro_source_file *file1, int line1,
  173.                    struct macro_source_file *file2, int line2)
  174. {
  175.   /* We want to treat positions in an #included file as coming *after*
  176.      the line containing the #include, but *before* the line after the
  177.      include.  As we walk up the #inclusion tree toward the main
  178.      source file, we update fileX and lineX as we go; includedX
  179.      indicates whether the original position was from the #included
  180.      file.  */
  181.   int included1 = 0;
  182.   int included2 = 0;

  183.   /* If a file is zero, that means "end of compilation unit."  Handle
  184.      that specially.  */
  185.   if (! file1)
  186.     {
  187.       if (! file2)
  188.         return 0;
  189.       else
  190.         return 1;
  191.     }
  192.   else if (! file2)
  193.     return -1;

  194.   /* If the two files are not the same, find their common ancestor in
  195.      the #inclusion tree.  */
  196.   if (file1 != file2)
  197.     {
  198.       /* If one file is deeper than the other, walk up the #inclusion
  199.          chain until the two files are at least at the same *depth*.
  200.          Then, walk up both files in synchrony until they're the same
  201.          file.  That file is the common ancestor.  */
  202.       int depth1 = inclusion_depth (file1);
  203.       int depth2 = inclusion_depth (file2);

  204.       /* Only one of these while loops will ever execute in any given
  205.          case.  */
  206.       while (depth1 > depth2)
  207.         {
  208.           line1 = file1->included_at_line;
  209.           file1 = file1->included_by;
  210.           included1 = 1;
  211.           depth1--;
  212.         }
  213.       while (depth2 > depth1)
  214.         {
  215.           line2 = file2->included_at_line;
  216.           file2 = file2->included_by;
  217.           included2 = 1;
  218.           depth2--;
  219.         }

  220.       /* Now both file1 and file2 are at the same depth.  Walk toward
  221.          the root of the tree until we find where the branches meet.  */
  222.       while (file1 != file2)
  223.         {
  224.           line1 = file1->included_at_line;
  225.           file1 = file1->included_by;
  226.           /* At this point, we know that the case the includedX flags
  227.              are trying to deal with won't come up, but we'll just
  228.              maintain them anyway.  */
  229.           included1 = 1;

  230.           line2 = file2->included_at_line;
  231.           file2 = file2->included_by;
  232.           included2 = 1;

  233.           /* Sanity check.  If file1 and file2 are really from the
  234.              same compilation unit, then they should both be part of
  235.              the same tree, and this shouldn't happen.  */
  236.           gdb_assert (file1 && file2);
  237.         }
  238.     }

  239.   /* Now we've got two line numbers in the same file.  */
  240.   if (line1 == line2)
  241.     {
  242.       /* They can't both be from #included files.  Then we shouldn't
  243.          have walked up this far.  */
  244.       gdb_assert (! included1 || ! included2);

  245.       /* Any #included position comes after a non-#included position
  246.          with the same line number in the #including file.  */
  247.       if (included1)
  248.         return 1;
  249.       else if (included2)
  250.         return -1;
  251.       else
  252.         return 0;
  253.     }
  254.   else
  255.     return line1 - line2;
  256. }


  257. /* Compare a macro key KEY against NAME, the source file FILE, and
  258.    line number LINE.

  259.    Sort definitions by name; for two definitions with the same name,
  260.    place the one whose definition comes earlier before the one whose
  261.    definition comes later.

  262.    Return -1, 0, or 1 if key comes before, is identical to, or comes
  263.    after NAME, FILE, and LINE.  */
  264. static int
  265. key_compare (struct macro_key *key,
  266.              const char *name, struct macro_source_file *file, int line)
  267. {
  268.   int names = strcmp (key->name, name);

  269.   if (names)
  270.     return names;

  271.   return compare_locations (key->start_file, key->start_line,
  272.                             file, line);
  273. }


  274. /* The macro tree comparison function, typed for the splay tree
  275.    library's happiness.  */
  276. static int
  277. macro_tree_compare (splay_tree_key untyped_key1,
  278.                     splay_tree_key untyped_key2)
  279. {
  280.   struct macro_key *key1 = (struct macro_key *) untyped_key1;
  281.   struct macro_key *key2 = (struct macro_key *) untyped_key2;

  282.   return key_compare (key1, key2->name, key2->start_file, key2->start_line);
  283. }


  284. /* Construct a new macro key node for a macro in table T whose name is
  285.    NAME, and whose scope starts at LINE in FILE; register the name in
  286.    the bcache.  */
  287. static struct macro_key *
  288. new_macro_key (struct macro_table *t,
  289.                const char *name,
  290.                struct macro_source_file *file,
  291.                int line)
  292. {
  293.   struct macro_key *k = macro_alloc (sizeof (*k), t);

  294.   memset (k, 0, sizeof (*k));
  295.   k->table = t;
  296.   k->name = macro_bcache_str (t, name);
  297.   k->start_file = file;
  298.   k->start_line = line;
  299.   k->end_file = 0;

  300.   return k;
  301. }


  302. static void
  303. macro_tree_delete_key (void *untyped_key)
  304. {
  305.   struct macro_key *key = (struct macro_key *) untyped_key;

  306.   macro_bcache_free (key->table, (char *) key->name);
  307.   macro_free (key, key->table);
  308. }



  309. /* Building and querying the tree of #included files.  */


  310. /* Allocate and initialize a new source file structure.  */
  311. static struct macro_source_file *
  312. new_source_file (struct macro_table *t,
  313.                  const char *filename)
  314. {
  315.   /* Get space for the source file structure itself.  */
  316.   struct macro_source_file *f = macro_alloc (sizeof (*f), t);

  317.   memset (f, 0, sizeof (*f));
  318.   f->table = t;
  319.   f->filename = macro_bcache_str (t, filename);
  320.   f->includes = 0;

  321.   return f;
  322. }


  323. /* Free a source file, and all the source files it #included.  */
  324. static void
  325. free_macro_source_file (struct macro_source_file *src)
  326. {
  327.   struct macro_source_file *child, *next_child;

  328.   /* Free this file's children.  */
  329.   for (child = src->includes; child; child = next_child)
  330.     {
  331.       next_child = child->next_included;
  332.       free_macro_source_file (child);
  333.     }

  334.   macro_bcache_free (src->table, (char *) src->filename);
  335.   macro_free (src, src->table);
  336. }


  337. struct macro_source_file *
  338. macro_set_main (struct macro_table *t,
  339.                 const char *filename)
  340. {
  341.   /* You can't change a table's main source file.  What would that do
  342.      to the tree?  */
  343.   gdb_assert (! t->main_source);

  344.   t->main_source = new_source_file (t, filename);

  345.   return t->main_source;
  346. }


  347. struct macro_source_file *
  348. macro_main (struct macro_table *t)
  349. {
  350.   gdb_assert (t->main_source);

  351.   return t->main_source;
  352. }


  353. void
  354. macro_allow_redefinitions (struct macro_table *t)
  355. {
  356.   gdb_assert (! t->obstack);
  357.   t->redef_ok = 1;
  358. }


  359. struct macro_source_file *
  360. macro_include (struct macro_source_file *source,
  361.                int line,
  362.                const char *included)
  363. {
  364.   struct macro_source_file *new;
  365.   struct macro_source_file **link;

  366.   /* Find the right position in SOURCE's `includes' list for the new
  367.      file.  Skip inclusions at earlier lines, until we find one at the
  368.      same line or later --- or until the end of the list.  */
  369.   for (link = &source->includes;
  370.        *link && (*link)->included_at_line < line;
  371.        link = &(*link)->next_included)
  372.     ;

  373.   /* Did we find another file already #included at the same line as
  374.      the new one?  */
  375.   if (*link && line == (*link)->included_at_line)
  376.     {
  377.       char *link_fullname, *source_fullname;

  378.       /* This means the compiler is emitting bogus debug info.  (GCC
  379.          circa March 2002 did this.)  It also means that the splay
  380.          tree ordering function, macro_tree_compare, will abort,
  381.          because it can't tell which #inclusion came first.  But GDB
  382.          should tolerate bad debug info.  So:

  383.          First, squawk.  */

  384.       link_fullname = macro_source_fullname (*link);
  385.       source_fullname = macro_source_fullname (source);
  386.       complaint (&symfile_complaints,
  387.                  _("both `%s' and `%s' allegedly #included at %s:%d"),
  388.                  included, link_fullname, source_fullname, line);
  389.       xfree (source_fullname);
  390.       xfree (link_fullname);

  391.       /* Now, choose a new, unoccupied line number for this
  392.          #inclusion, after the alleged #inclusion line.  */
  393.       while (*link && line == (*link)->included_at_line)
  394.         {
  395.           /* This line number is taken, so try the next line.  */
  396.           line++;
  397.           link = &(*link)->next_included;
  398.         }
  399.     }

  400.   /* At this point, we know that LINE is an unused line number, and
  401.      *LINK points to the entry an #inclusion at that line should
  402.      precede.  */
  403.   new = new_source_file (source->table, included);
  404.   new->included_by = source;
  405.   new->included_at_line = line;
  406.   new->next_included = *link;
  407.   *link = new;

  408.   return new;
  409. }


  410. struct macro_source_file *
  411. macro_lookup_inclusion (struct macro_source_file *source, const char *name)
  412. {
  413.   /* Is SOURCE itself named NAME?  */
  414.   if (filename_cmp (name, source->filename) == 0)
  415.     return source;

  416.   /* It's not us.  Try all our children, and return the lowest.  */
  417.   {
  418.     struct macro_source_file *child;
  419.     struct macro_source_file *best = NULL;
  420.     int best_depth = 0;

  421.     for (child = source->includes; child; child = child->next_included)
  422.       {
  423.         struct macro_source_file *result
  424.           = macro_lookup_inclusion (child, name);

  425.         if (result)
  426.           {
  427.             int result_depth = inclusion_depth (result);

  428.             if (! best || result_depth < best_depth)
  429.               {
  430.                 best = result;
  431.                 best_depth = result_depth;
  432.               }
  433.           }
  434.       }

  435.     return best;
  436.   }
  437. }



  438. /* Registering and looking up macro definitions.  */


  439. /* Construct a definition for a macro in table T.  Cache all strings,
  440.    and the macro_definition structure itself, in T's bcache.  */
  441. static struct macro_definition *
  442. new_macro_definition (struct macro_table *t,
  443.                       enum macro_kind kind,
  444.                       int argc, const char **argv,
  445.                       const char *replacement)
  446. {
  447.   struct macro_definition *d = macro_alloc (sizeof (*d), t);

  448.   memset (d, 0, sizeof (*d));
  449.   d->table = t;
  450.   d->kind = kind;
  451.   d->replacement = macro_bcache_str (t, replacement);
  452.   d->argc = argc;

  453.   if (kind == macro_function_like)
  454.     {
  455.       int i;
  456.       const char **cached_argv;
  457.       int cached_argv_size = argc * sizeof (*cached_argv);

  458.       /* Bcache all the arguments.  */
  459.       cached_argv = alloca (cached_argv_size);
  460.       for (i = 0; i < argc; i++)
  461.         cached_argv[i] = macro_bcache_str (t, argv[i]);

  462.       /* Now bcache the array of argument pointers itself.  */
  463.       d->argv = macro_bcache (t, cached_argv, cached_argv_size);
  464.     }

  465.   /* We don't bcache the entire definition structure because it's got
  466.      a pointer to the macro table in it; since each compilation unit
  467.      has its own macro table, you'd only get bcache hits for identical
  468.      definitions within a compilation unit, which seems unlikely.

  469.      "So, why do macro definitions have pointers to their macro tables
  470.      at all?"  Well, when the splay tree library wants to free a
  471.      node's value, it calls the value freeing function with nothing
  472.      but the value itself.  It makes the (apparently reasonable)
  473.      assumption that the value carries enough information to free
  474.      itself.  But not all macro tables have bcaches, so not all macro
  475.      definitions would be bcached.  There's no way to tell whether a
  476.      given definition is bcached without knowing which table the
  477.      definition belongs to.  ...  blah.  The thing's only sixteen
  478.      bytes anyway, and we can still bcache the name, args, and
  479.      definition, so we just don't bother bcaching the definition
  480.      structure itself.  */
  481.   return d;
  482. }


  483. /* Free a macro definition.  */
  484. static void
  485. macro_tree_delete_value (void *untyped_definition)
  486. {
  487.   struct macro_definition *d = (struct macro_definition *) untyped_definition;
  488.   struct macro_table *t = d->table;

  489.   if (d->kind == macro_function_like)
  490.     {
  491.       int i;

  492.       for (i = 0; i < d->argc; i++)
  493.         macro_bcache_free (t, (char *) d->argv[i]);
  494.       macro_bcache_free (t, (char **) d->argv);
  495.     }

  496.   macro_bcache_free (t, (char *) d->replacement);
  497.   macro_free (d, t);
  498. }


  499. /* Find the splay tree node for the definition of NAME at LINE in
  500.    SOURCE, or zero if there is none.  */
  501. static splay_tree_node
  502. find_definition (const char *name,
  503.                  struct macro_source_file *file,
  504.                  int line)
  505. {
  506.   struct macro_table *t = file->table;
  507.   splay_tree_node n;

  508.   /* Construct a macro_key object, just for the query.  */
  509.   struct macro_key query;

  510.   query.name = name;
  511.   query.start_file = file;
  512.   query.start_line = line;
  513.   query.end_file = NULL;

  514.   n = splay_tree_lookup (t->definitions, (splay_tree_key) &query);
  515.   if (! n)
  516.     {
  517.       /* It's okay for us to do two queries like this: the real work
  518.          of the searching is done when we splay, and splaying the tree
  519.          a second time at the same key is a constant time operation.
  520.          If this still bugs you, you could always just extend the
  521.          splay tree library with a predecessor-or-equal operation, and
  522.          use that.  */
  523.       splay_tree_node pred = splay_tree_predecessor (t->definitions,
  524.                                                      (splay_tree_key) &query);

  525.       if (pred)
  526.         {
  527.           /* Make sure this predecessor actually has the right name.
  528.              We just want to search within a given name's definitions.  */
  529.           struct macro_key *found = (struct macro_key *) pred->key;

  530.           if (strcmp (found->name, name) == 0)
  531.             n = pred;
  532.         }
  533.     }

  534.   if (n)
  535.     {
  536.       struct macro_key *found = (struct macro_key *) n->key;

  537.       /* Okay, so this definition has the right name, and its scope
  538.          begins before the given source location.  But does its scope
  539.          end after the given source location?  */
  540.       if (compare_locations (file, line, found->end_file, found->end_line) < 0)
  541.         return n;
  542.       else
  543.         return 0;
  544.     }
  545.   else
  546.     return 0;
  547. }


  548. /* If NAME already has a definition in scope at LINE in SOURCE, return
  549.    the key.  If the old definition is different from the definition
  550.    given by KIND, ARGC, ARGV, and REPLACEMENT, complain, too.
  551.    Otherwise, return zero.  (ARGC and ARGV are meaningless unless KIND
  552.    is `macro_function_like'.)  */
  553. static struct macro_key *
  554. check_for_redefinition (struct macro_source_file *source, int line,
  555.                         const char *name, enum macro_kind kind,
  556.                         int argc, const char **argv,
  557.                         const char *replacement)
  558. {
  559.   splay_tree_node n = find_definition (name, source, line);

  560.   if (n)
  561.     {
  562.       struct macro_key *found_key = (struct macro_key *) n->key;
  563.       struct macro_definition *found_def
  564.         = (struct macro_definition *) n->value;
  565.       int same = 1;

  566.       /* Is this definition the same as the existing one?
  567.          According to the standard, this comparison needs to be done
  568.          on lists of tokens, not byte-by-byte, as we do here.  But
  569.          that's too hard for us at the moment, and comparing
  570.          byte-by-byte will only yield false negatives (i.e., extra
  571.          warning messages), not false positives (i.e., unnoticed
  572.          definition changes).  */
  573.       if (kind != found_def->kind)
  574.         same = 0;
  575.       else if (strcmp (replacement, found_def->replacement))
  576.         same = 0;
  577.       else if (kind == macro_function_like)
  578.         {
  579.           if (argc != found_def->argc)
  580.             same = 0;
  581.           else
  582.             {
  583.               int i;

  584.               for (i = 0; i < argc; i++)
  585.                 if (strcmp (argv[i], found_def->argv[i]))
  586.                   same = 0;
  587.             }
  588.         }

  589.       if (! same)
  590.         {
  591.           char *source_fullname, *found_key_fullname;

  592.           source_fullname = macro_source_fullname (source);
  593.           found_key_fullname = macro_source_fullname (found_key->start_file);
  594.           complaint (&symfile_complaints,
  595.                      _("macro `%s' redefined at %s:%d; "
  596.                        "original definition at %s:%d"),
  597.                      name, source_fullname, line, found_key_fullname,
  598.                      found_key->start_line);
  599.           xfree (found_key_fullname);
  600.           xfree (source_fullname);
  601.         }

  602.       return found_key;
  603.     }
  604.   else
  605.     return 0;
  606. }

  607. /* A helper function to define a new object-like macro.  */

  608. static void
  609. macro_define_object_internal (struct macro_source_file *source, int line,
  610.                               const char *name, const char *replacement,
  611.                               enum macro_special_kind kind)
  612. {
  613.   struct macro_table *t = source->table;
  614.   struct macro_key *k = NULL;
  615.   struct macro_definition *d;

  616.   if (! t->redef_ok)
  617.     k = check_for_redefinition (source, line,
  618.                                 name, macro_object_like,
  619.                                 0, 0,
  620.                                 replacement);

  621.   /* If we're redefining a symbol, and the existing key would be
  622.      identical to our new key, then the splay_tree_insert function
  623.      will try to delete the old definition.  When the definition is
  624.      living on an obstack, this isn't a happy thing.

  625.      Since this only happens in the presence of questionable debug
  626.      info, we just ignore all definitions after the first.  The only
  627.      case I know of where this arises is in GCC's output for
  628.      predefined macros, and all the definitions are the same in that
  629.      case.  */
  630.   if (k && ! key_compare (k, name, source, line))
  631.     return;

  632.   k = new_macro_key (t, name, source, line);
  633.   d = new_macro_definition (t, macro_object_like, kind, 0, replacement);
  634.   splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
  635. }

  636. void
  637. macro_define_object (struct macro_source_file *source, int line,
  638.                      const char *name, const char *replacement)
  639. {
  640.   macro_define_object_internal (source, line, name, replacement,
  641.                                 macro_ordinary);
  642. }

  643. /* See macrotab.h.  */

  644. void
  645. macro_define_special (struct macro_table *table)
  646. {
  647.   macro_define_object_internal (table->main_source, -1, "__FILE__", "",
  648.                                 macro_FILE);
  649.   macro_define_object_internal (table->main_source, -1, "__LINE__", "",
  650.                                 macro_LINE);
  651. }

  652. void
  653. macro_define_function (struct macro_source_file *source, int line,
  654.                        const char *name, int argc, const char **argv,
  655.                        const char *replacement)
  656. {
  657.   struct macro_table *t = source->table;
  658.   struct macro_key *k = NULL;
  659.   struct macro_definition *d;

  660.   if (! t->redef_ok)
  661.     k = check_for_redefinition (source, line,
  662.                                 name, macro_function_like,
  663.                                 argc, argv,
  664.                                 replacement);

  665.   /* See comments about duplicate keys in macro_define_object.  */
  666.   if (k && ! key_compare (k, name, source, line))
  667.     return;

  668.   /* We should also check here that all the argument names in ARGV are
  669.      distinct.  */

  670.   k = new_macro_key (t, name, source, line);
  671.   d = new_macro_definition (t, macro_function_like, argc, argv, replacement);
  672.   splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
  673. }


  674. void
  675. macro_undef (struct macro_source_file *source, int line,
  676.              const char *name)
  677. {
  678.   splay_tree_node n = find_definition (name, source, line);

  679.   if (n)
  680.     {
  681.       struct macro_key *key = (struct macro_key *) n->key;

  682.       /* If we're removing a definition at exactly the same point that
  683.          we defined it, then just delete the entry altogether.  GCC
  684.          4.1.2 will generate DWARF that says to do this if you pass it
  685.          arguments like '-DFOO -UFOO -DFOO=2'.  */
  686.       if (source == key->start_file
  687.           && line == key->start_line)
  688.         splay_tree_remove (source->table->definitions, n->key);

  689.       else
  690.         {
  691.           /* This function is the only place a macro's end-of-scope
  692.              location gets set to anything other than "end of the
  693.              compilation unit" (i.e., end_file is zero).  So if this
  694.              macro already has its end-of-scope set, then we're
  695.              probably seeing a second #undefinition for the same
  696.              #definition.  */
  697.           if (key->end_file)
  698.             {
  699.               char *source_fullname, *key_fullname;

  700.               source_fullname = macro_source_fullname (source);
  701.               key_fullname = macro_source_fullname (key->end_file);
  702.               complaint (&symfile_complaints,
  703.                          _("macro '%s' is #undefined twice,"
  704.                            " at %s:%d and %s:%d"),
  705.                          name, source_fullname, line, key_fullname,
  706.                          key->end_line);
  707.               xfree (key_fullname);
  708.               xfree (source_fullname);
  709.             }

  710.           /* Whether or not we've seen a prior #undefinition, wipe out
  711.              the old ending point, and make this the ending point.  */
  712.           key->end_file = source;
  713.           key->end_line = line;
  714.         }
  715.     }
  716.   else
  717.     {
  718.       /* According to the ISO C standard, an #undef for a symbol that
  719.          has no macro definition in scope is ignored.  So we should
  720.          ignore it too.  */
  721. #if 0
  722.       complaint (&symfile_complaints,
  723.                  _("no definition for macro `%s' in scope to #undef at %s:%d"),
  724.                  name, source->filename, line);
  725. #endif
  726.     }
  727. }

  728. /* A helper function that rewrites the definition of a special macro,
  729.    when needed.  */

  730. static struct macro_definition *
  731. fixup_definition (const char *filename, int line, struct macro_definition *def)
  732. {
  733.   static char *saved_expansion;

  734.   if (saved_expansion)
  735.     {
  736.       xfree (saved_expansion);
  737.       saved_expansion = NULL;
  738.     }

  739.   if (def->kind == macro_object_like)
  740.     {
  741.       if (def->argc == macro_FILE)
  742.         {
  743.           saved_expansion = macro_stringify (filename);
  744.           def->replacement = saved_expansion;
  745.         }
  746.       else if (def->argc == macro_LINE)
  747.         {
  748.           saved_expansion = xstrprintf ("%d", line);
  749.           def->replacement = saved_expansion;
  750.         }
  751.     }

  752.   return def;
  753. }

  754. struct macro_definition *
  755. macro_lookup_definition (struct macro_source_file *source,
  756.                          int line, const char *name)
  757. {
  758.   splay_tree_node n = find_definition (name, source, line);

  759.   if (n)
  760.     {
  761.       struct macro_definition *retval;
  762.       char *source_fullname;

  763.       source_fullname = macro_source_fullname (source);
  764.       retval = fixup_definition (source_fullname, line,
  765.                                  (struct macro_definition *) n->value);
  766.       xfree (source_fullname);
  767.       return retval;
  768.     }
  769.   else
  770.     return 0;
  771. }


  772. struct macro_source_file *
  773. macro_definition_location (struct macro_source_file *source,
  774.                            int line,
  775.                            const char *name,
  776.                            int *definition_line)
  777. {
  778.   splay_tree_node n = find_definition (name, source, line);

  779.   if (n)
  780.     {
  781.       struct macro_key *key = (struct macro_key *) n->key;

  782.       *definition_line = key->start_line;
  783.       return key->start_file;
  784.     }
  785.   else
  786.     return 0;
  787. }


  788. /* The type for callback data for iterating the splay tree in
  789.    macro_for_each and macro_for_each_in_scope.  Only the latter uses
  790.    the FILE and LINE fields.  */
  791. struct macro_for_each_data
  792. {
  793.   macro_callback_fn fn;
  794.   void *user_data;
  795.   struct macro_source_file *file;
  796.   int line;
  797. };

  798. /* Helper function for macro_for_each.  */
  799. static int
  800. foreach_macro (splay_tree_node node, void *arg)
  801. {
  802.   struct macro_for_each_data *datum = (struct macro_for_each_data *) arg;
  803.   struct macro_key *key = (struct macro_key *) node->key;
  804.   struct macro_definition *def;
  805.   char *key_fullname;

  806.   key_fullname = macro_source_fullname (key->start_file);
  807.   def = fixup_definition (key_fullname, key->start_line,
  808.                           (struct macro_definition *) node->value);
  809.   xfree (key_fullname);

  810.   (*datum->fn) (key->name, def, key->start_file, key->start_line,
  811.                 datum->user_data);
  812.   return 0;
  813. }

  814. /* Call FN for every macro in TABLE.  */
  815. void
  816. macro_for_each (struct macro_table *table, macro_callback_fn fn,
  817.                 void *user_data)
  818. {
  819.   struct macro_for_each_data datum;

  820.   datum.fn = fn;
  821.   datum.user_data = user_data;
  822.   datum.file = NULL;
  823.   datum.line = 0;
  824.   splay_tree_foreach (table->definitions, foreach_macro, &datum);
  825. }

  826. static int
  827. foreach_macro_in_scope (splay_tree_node node, void *info)
  828. {
  829.   struct macro_for_each_data *datum = (struct macro_for_each_data *) info;
  830.   struct macro_key *key = (struct macro_key *) node->key;
  831.   struct macro_definition *def;
  832.   char *datum_fullname;

  833.   datum_fullname = macro_source_fullname (datum->file);
  834.   def = fixup_definition (datum_fullname, datum->line,
  835.                           (struct macro_definition *) node->value);
  836.   xfree (datum_fullname);

  837.   /* See if this macro is defined before the passed-in line, and
  838.      extends past that line.  */
  839.   if (compare_locations (key->start_file, key->start_line,
  840.                          datum->file, datum->line) < 0
  841.       && (!key->end_file
  842.           || compare_locations (key->end_file, key->end_line,
  843.                                 datum->file, datum->line) >= 0))
  844.     (*datum->fn) (key->name, def, key->start_file, key->start_line,
  845.                   datum->user_data);
  846.   return 0;
  847. }

  848. /* Call FN for every macro is visible in SCOPE.  */
  849. void
  850. macro_for_each_in_scope (struct macro_source_file *file, int line,
  851.                          macro_callback_fn fn, void *user_data)
  852. {
  853.   struct macro_for_each_data datum;

  854.   datum.fn = fn;
  855.   datum.user_data = user_data;
  856.   datum.file = file;
  857.   datum.line = line;
  858.   splay_tree_foreach (file->table->definitions,
  859.                       foreach_macro_in_scope, &datum);
  860. }



  861. /* Creating and freeing macro tables.  */


  862. struct macro_table *
  863. new_macro_table (struct obstack *obstack, struct bcache *b,
  864.                  struct compunit_symtab *cust)
  865. {
  866.   struct macro_table *t;

  867.   /* First, get storage for the `struct macro_table' itself.  */
  868.   if (obstack)
  869.     t = obstack_alloc (obstack, sizeof (*t));
  870.   else
  871.     t = xmalloc (sizeof (*t));

  872.   memset (t, 0, sizeof (*t));
  873.   t->obstack = obstack;
  874.   t->bcache = b;
  875.   t->main_source = NULL;
  876.   t->compunit_symtab = cust;
  877.   t->redef_ok = 0;
  878.   t->definitions = (splay_tree_new_with_allocator
  879.                     (macro_tree_compare,
  880.                      ((splay_tree_delete_key_fn) macro_tree_delete_key),
  881.                      ((splay_tree_delete_value_fn) macro_tree_delete_value),
  882.                      ((splay_tree_allocate_fn) macro_alloc),
  883.                      ((splay_tree_deallocate_fn) macro_free),
  884.                      t));

  885.   return t;
  886. }


  887. void
  888. free_macro_table (struct macro_table *table)
  889. {
  890.   /* Free the source file tree.  */
  891.   free_macro_source_file (table->main_source);

  892.   /* Free the table of macro definitions.  */
  893.   splay_tree_delete (table->definitions);
  894. }

  895. /* See macrotab.h for the comment.  */

  896. char *
  897. macro_source_fullname (struct macro_source_file *file)
  898. {
  899.   const char *comp_dir = NULL;

  900.   if (file->table->compunit_symtab != NULL)
  901.     comp_dir = COMPUNIT_DIRNAME (file->table->compunit_symtab);

  902.   if (comp_dir == NULL || IS_ABSOLUTE_PATH (file->filename))
  903.     return xstrdup (file->filename);

  904.   return concat (comp_dir, SLASH_STRING, file->filename, NULL);
  905. }