GDB (API)
/home/stan/gdb/src/gdb/macrotab.c
Go to the documentation of this file.
00001 /* C preprocessor macro tables for GDB.
00002    Copyright (C) 2002-2013 Free Software Foundation, Inc.
00003    Contributed by Red Hat, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdb_obstack.h"
00022 #include "splay-tree.h"
00023 #include "filenames.h"
00024 #include "symtab.h"
00025 #include "symfile.h"
00026 #include "objfiles.h"
00027 #include "macrotab.h"
00028 #include "gdb_assert.h"
00029 #include "bcache.h"
00030 #include "complaints.h"
00031 #include "macroexp.h"
00032 
00033 
00034 /* The macro table structure.  */
00035 
00036 struct macro_table
00037 {
00038   /* The obstack this table's data should be allocated in, or zero if
00039      we should use xmalloc.  */
00040   struct obstack *obstack;
00041 
00042   /* The bcache we should use to hold macro names, argument names, and
00043      definitions, or zero if we should use xmalloc.  */
00044   struct bcache *bcache;
00045 
00046   /* The main source file for this compilation unit --- the one whose
00047      name was given to the compiler.  This is the root of the
00048      #inclusion tree; everything else is #included from here.  */
00049   struct macro_source_file *main_source;
00050 
00051   /* Compilation directory for all files of this macro table.  It is allocated
00052      on objfile's obstack.  */
00053   const char *comp_dir;
00054 
00055   /* True if macros in this table can be redefined without issuing an
00056      error.  */
00057   int redef_ok;
00058 
00059   /* The table of macro definitions.  This is a splay tree (an ordered
00060      binary tree that stays balanced, effectively), sorted by macro
00061      name.  Where a macro gets defined more than once (presumably with
00062      an #undefinition in between), we sort the definitions by the
00063      order they would appear in the preprocessor's output.  That is,
00064      if `a.c' #includes `m.h' and then #includes `n.h', and both
00065      header files #define X (with an #undef somewhere in between),
00066      then the definition from `m.h' appears in our splay tree before
00067      the one from `n.h'.
00068 
00069      The splay tree's keys are `struct macro_key' pointers;
00070      the values are `struct macro_definition' pointers.
00071 
00072      The splay tree, its nodes, and the keys and values are allocated
00073      in obstack, if it's non-zero, or with xmalloc otherwise.  The
00074      macro names, argument names, argument name arrays, and definition
00075      strings are all allocated in bcache, if non-zero, or with xmalloc
00076      otherwise.  */
00077   splay_tree definitions;
00078 };
00079 
00080 
00081 
00082 /* Allocation and freeing functions.  */
00083 
00084 /* Allocate SIZE bytes of memory appropriately for the macro table T.
00085    This just checks whether T has an obstack, or whether its pieces
00086    should be allocated with xmalloc.  */
00087 static void *
00088 macro_alloc (int size, struct macro_table *t)
00089 {
00090   if (t->obstack)
00091     return obstack_alloc (t->obstack, size);
00092   else
00093     return xmalloc (size);
00094 }
00095 
00096 
00097 static void
00098 macro_free (void *object, struct macro_table *t)
00099 {
00100   if (t->obstack)
00101     /* There are cases where we need to remove entries from a macro
00102        table, even when reading debugging information.  This should be
00103        rare, and there's no easy way to free arbitrary data from an
00104        obstack, so we just leak it.  */
00105     ;
00106   else
00107     xfree (object);
00108 }
00109 
00110 
00111 /* If the macro table T has a bcache, then cache the LEN bytes at ADDR
00112    there, and return the cached copy.  Otherwise, just xmalloc a copy
00113    of the bytes, and return a pointer to that.  */
00114 static const void *
00115 macro_bcache (struct macro_table *t, const void *addr, int len)
00116 {
00117   if (t->bcache)
00118     return bcache (addr, len, t->bcache);
00119   else
00120     {
00121       void *copy = xmalloc (len);
00122 
00123       memcpy (copy, addr, len);
00124       return copy;
00125     }
00126 }
00127 
00128 
00129 /* If the macro table T has a bcache, cache the null-terminated string
00130    S there, and return a pointer to the cached copy.  Otherwise,
00131    xmalloc a copy and return that.  */
00132 static const char *
00133 macro_bcache_str (struct macro_table *t, const char *s)
00134 {
00135   return macro_bcache (t, s, strlen (s) + 1);
00136 }
00137 
00138 
00139 /* Free a possibly bcached object OBJ.  That is, if the macro table T
00140    has a bcache, do nothing; otherwise, xfree OBJ.  */
00141 static void
00142 macro_bcache_free (struct macro_table *t, void *obj)
00143 {
00144   if (t->bcache)
00145     /* There are cases where we need to remove entries from a macro
00146        table, even when reading debugging information.  This should be
00147        rare, and there's no easy way to free data from a bcache, so we
00148        just leak it.  */
00149     ;
00150   else
00151     xfree (obj);
00152 }
00153 
00154 
00155 
00156 /* Macro tree keys, w/their comparison, allocation, and freeing functions.  */
00157 
00158 /* A key in the splay tree.  */
00159 struct macro_key
00160 {
00161   /* The table we're in.  We only need this in order to free it, since
00162      the splay tree library's key and value freeing functions require
00163      that the key or value contain all the information needed to free
00164      themselves.  */
00165   struct macro_table *table;
00166 
00167   /* The name of the macro.  This is in the table's bcache, if it has
00168      one.  */
00169   const char *name;
00170 
00171   /* The source file and line number where the definition's scope
00172      begins.  This is also the line of the definition itself.  */
00173   struct macro_source_file *start_file;
00174   int start_line;
00175 
00176   /* The first source file and line after the definition's scope.
00177      (That is, the scope does not include this endpoint.)  If end_file
00178      is zero, then the definition extends to the end of the
00179      compilation unit.  */
00180   struct macro_source_file *end_file;
00181   int end_line;
00182 };
00183 
00184 
00185 /* Return the #inclusion depth of the source file FILE.  This is the
00186    number of #inclusions it took to reach this file.  For the main
00187    source file, the #inclusion depth is zero; for a file it #includes
00188    directly, the depth would be one; and so on.  */
00189 static int
00190 inclusion_depth (struct macro_source_file *file)
00191 {
00192   int depth;
00193 
00194   for (depth = 0; file->included_by; depth++)
00195     file = file->included_by;
00196 
00197   return depth;
00198 }
00199 
00200 
00201 /* Compare two source locations (from the same compilation unit).
00202    This is part of the comparison function for the tree of
00203    definitions.
00204 
00205    LINE1 and LINE2 are line numbers in the source files FILE1 and
00206    FILE2.  Return a value:
00207    - less than zero if {LINE,FILE}1 comes before {LINE,FILE}2,
00208    - greater than zero if {LINE,FILE}1 comes after {LINE,FILE}2, or
00209    - zero if they are equal.
00210 
00211    When the two locations are in different source files --- perhaps
00212    one is in a header, while another is in the main source file --- we
00213    order them by where they would appear in the fully pre-processed
00214    sources, where all the #included files have been substituted into
00215    their places.  */
00216 static int
00217 compare_locations (struct macro_source_file *file1, int line1, 
00218                    struct macro_source_file *file2, int line2)
00219 {
00220   /* We want to treat positions in an #included file as coming *after*
00221      the line containing the #include, but *before* the line after the
00222      include.  As we walk up the #inclusion tree toward the main
00223      source file, we update fileX and lineX as we go; includedX
00224      indicates whether the original position was from the #included
00225      file.  */
00226   int included1 = 0;
00227   int included2 = 0;
00228 
00229   /* If a file is zero, that means "end of compilation unit."  Handle
00230      that specially.  */
00231   if (! file1)
00232     {
00233       if (! file2)
00234         return 0;
00235       else
00236         return 1;
00237     }
00238   else if (! file2)
00239     return -1;
00240 
00241   /* If the two files are not the same, find their common ancestor in
00242      the #inclusion tree.  */
00243   if (file1 != file2)
00244     {
00245       /* If one file is deeper than the other, walk up the #inclusion
00246          chain until the two files are at least at the same *depth*.
00247          Then, walk up both files in synchrony until they're the same
00248          file.  That file is the common ancestor.  */
00249       int depth1 = inclusion_depth (file1);
00250       int depth2 = inclusion_depth (file2);
00251 
00252       /* Only one of these while loops will ever execute in any given
00253          case.  */
00254       while (depth1 > depth2)
00255         {
00256           line1 = file1->included_at_line;
00257           file1 = file1->included_by;
00258           included1 = 1;
00259           depth1--;
00260         }
00261       while (depth2 > depth1)
00262         {
00263           line2 = file2->included_at_line;
00264           file2 = file2->included_by;
00265           included2 = 1;
00266           depth2--;
00267         }
00268 
00269       /* Now both file1 and file2 are at the same depth.  Walk toward
00270          the root of the tree until we find where the branches meet.  */
00271       while (file1 != file2)
00272         {
00273           line1 = file1->included_at_line;
00274           file1 = file1->included_by;
00275           /* At this point, we know that the case the includedX flags
00276              are trying to deal with won't come up, but we'll just
00277              maintain them anyway.  */
00278           included1 = 1;
00279 
00280           line2 = file2->included_at_line;
00281           file2 = file2->included_by;
00282           included2 = 1;
00283 
00284           /* Sanity check.  If file1 and file2 are really from the
00285              same compilation unit, then they should both be part of
00286              the same tree, and this shouldn't happen.  */
00287           gdb_assert (file1 && file2);
00288         }
00289     }
00290 
00291   /* Now we've got two line numbers in the same file.  */
00292   if (line1 == line2)
00293     {
00294       /* They can't both be from #included files.  Then we shouldn't
00295          have walked up this far.  */
00296       gdb_assert (! included1 || ! included2);
00297 
00298       /* Any #included position comes after a non-#included position
00299          with the same line number in the #including file.  */
00300       if (included1)
00301         return 1;
00302       else if (included2)
00303         return -1;
00304       else
00305         return 0;
00306     }
00307   else
00308     return line1 - line2;
00309 }
00310 
00311 
00312 /* Compare a macro key KEY against NAME, the source file FILE, and
00313    line number LINE.
00314 
00315    Sort definitions by name; for two definitions with the same name,
00316    place the one whose definition comes earlier before the one whose
00317    definition comes later.
00318 
00319    Return -1, 0, or 1 if key comes before, is identical to, or comes
00320    after NAME, FILE, and LINE.  */
00321 static int
00322 key_compare (struct macro_key *key,
00323              const char *name, struct macro_source_file *file, int line)
00324 {
00325   int names = strcmp (key->name, name);
00326 
00327   if (names)
00328     return names;
00329 
00330   return compare_locations (key->start_file, key->start_line,
00331                             file, line);
00332 }
00333 
00334 
00335 /* The macro tree comparison function, typed for the splay tree
00336    library's happiness.  */
00337 static int
00338 macro_tree_compare (splay_tree_key untyped_key1,
00339                     splay_tree_key untyped_key2)
00340 {
00341   struct macro_key *key1 = (struct macro_key *) untyped_key1;
00342   struct macro_key *key2 = (struct macro_key *) untyped_key2;
00343 
00344   return key_compare (key1, key2->name, key2->start_file, key2->start_line);
00345 }
00346 
00347 
00348 /* Construct a new macro key node for a macro in table T whose name is
00349    NAME, and whose scope starts at LINE in FILE; register the name in
00350    the bcache.  */
00351 static struct macro_key *
00352 new_macro_key (struct macro_table *t,
00353                const char *name,
00354                struct macro_source_file *file,
00355                int line)
00356 {
00357   struct macro_key *k = macro_alloc (sizeof (*k), t);
00358 
00359   memset (k, 0, sizeof (*k));
00360   k->table = t;
00361   k->name = macro_bcache_str (t, name);
00362   k->start_file = file;
00363   k->start_line = line;
00364   k->end_file = 0;
00365 
00366   return k;
00367 }
00368 
00369 
00370 static void
00371 macro_tree_delete_key (void *untyped_key)
00372 {
00373   struct macro_key *key = (struct macro_key *) untyped_key;
00374 
00375   macro_bcache_free (key->table, (char *) key->name);
00376   macro_free (key, key->table);
00377 }
00378 
00379 
00380 
00381 /* Building and querying the tree of #included files.  */
00382 
00383 
00384 /* Allocate and initialize a new source file structure.  */
00385 static struct macro_source_file *
00386 new_source_file (struct macro_table *t,
00387                  const char *filename)
00388 {
00389   /* Get space for the source file structure itself.  */
00390   struct macro_source_file *f = macro_alloc (sizeof (*f), t);
00391 
00392   memset (f, 0, sizeof (*f));
00393   f->table = t;
00394   f->filename = macro_bcache_str (t, filename);
00395   f->includes = 0;
00396 
00397   return f;
00398 }
00399 
00400 
00401 /* Free a source file, and all the source files it #included.  */
00402 static void
00403 free_macro_source_file (struct macro_source_file *src)
00404 {
00405   struct macro_source_file *child, *next_child;
00406 
00407   /* Free this file's children.  */
00408   for (child = src->includes; child; child = next_child)
00409     {
00410       next_child = child->next_included;
00411       free_macro_source_file (child);
00412     }
00413 
00414   macro_bcache_free (src->table, (char *) src->filename);
00415   macro_free (src, src->table);
00416 }
00417 
00418 
00419 struct macro_source_file *
00420 macro_set_main (struct macro_table *t,
00421                 const char *filename)
00422 {
00423   /* You can't change a table's main source file.  What would that do
00424      to the tree?  */
00425   gdb_assert (! t->main_source);
00426 
00427   t->main_source = new_source_file (t, filename);
00428 
00429   return t->main_source;
00430 }
00431 
00432 
00433 struct macro_source_file *
00434 macro_main (struct macro_table *t)
00435 {
00436   gdb_assert (t->main_source);
00437 
00438   return t->main_source;
00439 }
00440 
00441 
00442 void
00443 macro_allow_redefinitions (struct macro_table *t)
00444 {
00445   gdb_assert (! t->obstack);
00446   t->redef_ok = 1;
00447 }
00448 
00449 
00450 struct macro_source_file *
00451 macro_include (struct macro_source_file *source,
00452                int line,
00453                const char *included)
00454 {
00455   struct macro_source_file *new;
00456   struct macro_source_file **link;
00457 
00458   /* Find the right position in SOURCE's `includes' list for the new
00459      file.  Skip inclusions at earlier lines, until we find one at the
00460      same line or later --- or until the end of the list.  */
00461   for (link = &source->includes;
00462        *link && (*link)->included_at_line < line;
00463        link = &(*link)->next_included)
00464     ;
00465 
00466   /* Did we find another file already #included at the same line as
00467      the new one?  */
00468   if (*link && line == (*link)->included_at_line)
00469     {
00470       char *link_fullname, *source_fullname;
00471 
00472       /* This means the compiler is emitting bogus debug info.  (GCC
00473          circa March 2002 did this.)  It also means that the splay
00474          tree ordering function, macro_tree_compare, will abort,
00475          because it can't tell which #inclusion came first.  But GDB
00476          should tolerate bad debug info.  So:
00477 
00478          First, squawk.  */
00479 
00480       link_fullname = macro_source_fullname (*link);
00481       source_fullname = macro_source_fullname (source);
00482       complaint (&symfile_complaints,
00483                  _("both `%s' and `%s' allegedly #included at %s:%d"),
00484                  included, link_fullname, source_fullname, line);
00485       xfree (source_fullname);
00486       xfree (link_fullname);
00487 
00488       /* Now, choose a new, unoccupied line number for this
00489          #inclusion, after the alleged #inclusion line.  */
00490       while (*link && line == (*link)->included_at_line)
00491         {
00492           /* This line number is taken, so try the next line.  */
00493           line++;
00494           link = &(*link)->next_included;
00495         }
00496     }
00497 
00498   /* At this point, we know that LINE is an unused line number, and
00499      *LINK points to the entry an #inclusion at that line should
00500      precede.  */
00501   new = new_source_file (source->table, included);
00502   new->included_by = source;
00503   new->included_at_line = line;
00504   new->next_included = *link;
00505   *link = new;
00506 
00507   return new;
00508 }
00509 
00510 
00511 struct macro_source_file *
00512 macro_lookup_inclusion (struct macro_source_file *source, const char *name)
00513 {
00514   /* Is SOURCE itself named NAME?  */
00515   if (filename_cmp (name, source->filename) == 0)
00516     return source;
00517 
00518   /* It's not us.  Try all our children, and return the lowest.  */
00519   {
00520     struct macro_source_file *child;
00521     struct macro_source_file *best = NULL;
00522     int best_depth = 0;
00523 
00524     for (child = source->includes; child; child = child->next_included)
00525       {
00526         struct macro_source_file *result
00527           = macro_lookup_inclusion (child, name);
00528 
00529         if (result)
00530           {
00531             int result_depth = inclusion_depth (result);
00532 
00533             if (! best || result_depth < best_depth)
00534               {
00535                 best = result;
00536                 best_depth = result_depth;
00537               }
00538           }
00539       }
00540 
00541     return best;
00542   }
00543 }
00544 
00545 
00546 
00547 /* Registering and looking up macro definitions.  */
00548 
00549 
00550 /* Construct a definition for a macro in table T.  Cache all strings,
00551    and the macro_definition structure itself, in T's bcache.  */
00552 static struct macro_definition *
00553 new_macro_definition (struct macro_table *t,
00554                       enum macro_kind kind,
00555                       int argc, const char **argv,
00556                       const char *replacement)
00557 {
00558   struct macro_definition *d = macro_alloc (sizeof (*d), t);
00559 
00560   memset (d, 0, sizeof (*d));
00561   d->table = t;
00562   d->kind = kind;
00563   d->replacement = macro_bcache_str (t, replacement);
00564   d->argc = argc;
00565 
00566   if (kind == macro_function_like)
00567     {
00568       int i;
00569       const char **cached_argv;
00570       int cached_argv_size = argc * sizeof (*cached_argv);
00571 
00572       /* Bcache all the arguments.  */
00573       cached_argv = alloca (cached_argv_size);
00574       for (i = 0; i < argc; i++)
00575         cached_argv[i] = macro_bcache_str (t, argv[i]);
00576 
00577       /* Now bcache the array of argument pointers itself.  */
00578       d->argv = macro_bcache (t, cached_argv, cached_argv_size);
00579     }
00580 
00581   /* We don't bcache the entire definition structure because it's got
00582      a pointer to the macro table in it; since each compilation unit
00583      has its own macro table, you'd only get bcache hits for identical
00584      definitions within a compilation unit, which seems unlikely.
00585 
00586      "So, why do macro definitions have pointers to their macro tables
00587      at all?"  Well, when the splay tree library wants to free a
00588      node's value, it calls the value freeing function with nothing
00589      but the value itself.  It makes the (apparently reasonable)
00590      assumption that the value carries enough information to free
00591      itself.  But not all macro tables have bcaches, so not all macro
00592      definitions would be bcached.  There's no way to tell whether a
00593      given definition is bcached without knowing which table the
00594      definition belongs to.  ...  blah.  The thing's only sixteen
00595      bytes anyway, and we can still bcache the name, args, and
00596      definition, so we just don't bother bcaching the definition
00597      structure itself.  */
00598   return d;
00599 }
00600 
00601 
00602 /* Free a macro definition.  */
00603 static void
00604 macro_tree_delete_value (void *untyped_definition)
00605 {
00606   struct macro_definition *d = (struct macro_definition *) untyped_definition;
00607   struct macro_table *t = d->table;
00608 
00609   if (d->kind == macro_function_like)
00610     {
00611       int i;
00612 
00613       for (i = 0; i < d->argc; i++)
00614         macro_bcache_free (t, (char *) d->argv[i]);
00615       macro_bcache_free (t, (char **) d->argv);
00616     }
00617   
00618   macro_bcache_free (t, (char *) d->replacement);
00619   macro_free (d, t);
00620 }
00621 
00622 
00623 /* Find the splay tree node for the definition of NAME at LINE in
00624    SOURCE, or zero if there is none.  */
00625 static splay_tree_node
00626 find_definition (const char *name,
00627                  struct macro_source_file *file,
00628                  int line)
00629 {
00630   struct macro_table *t = file->table;
00631   splay_tree_node n;
00632 
00633   /* Construct a macro_key object, just for the query.  */
00634   struct macro_key query;
00635 
00636   query.name = name;
00637   query.start_file = file;
00638   query.start_line = line;
00639   query.end_file = NULL;
00640 
00641   n = splay_tree_lookup (t->definitions, (splay_tree_key) &query);
00642   if (! n)
00643     {
00644       /* It's okay for us to do two queries like this: the real work
00645          of the searching is done when we splay, and splaying the tree
00646          a second time at the same key is a constant time operation.
00647          If this still bugs you, you could always just extend the
00648          splay tree library with a predecessor-or-equal operation, and
00649          use that.  */
00650       splay_tree_node pred = splay_tree_predecessor (t->definitions,
00651                                                      (splay_tree_key) &query);
00652      
00653       if (pred)
00654         {
00655           /* Make sure this predecessor actually has the right name.
00656              We just want to search within a given name's definitions.  */
00657           struct macro_key *found = (struct macro_key *) pred->key;
00658 
00659           if (strcmp (found->name, name) == 0)
00660             n = pred;
00661         }
00662     }
00663 
00664   if (n)
00665     {
00666       struct macro_key *found = (struct macro_key *) n->key;
00667 
00668       /* Okay, so this definition has the right name, and its scope
00669          begins before the given source location.  But does its scope
00670          end after the given source location?  */
00671       if (compare_locations (file, line, found->end_file, found->end_line) < 0)
00672         return n;
00673       else
00674         return 0;
00675     }
00676   else
00677     return 0;
00678 }
00679 
00680 
00681 /* If NAME already has a definition in scope at LINE in SOURCE, return
00682    the key.  If the old definition is different from the definition
00683    given by KIND, ARGC, ARGV, and REPLACEMENT, complain, too.
00684    Otherwise, return zero.  (ARGC and ARGV are meaningless unless KIND
00685    is `macro_function_like'.)  */
00686 static struct macro_key *
00687 check_for_redefinition (struct macro_source_file *source, int line,
00688                         const char *name, enum macro_kind kind,
00689                         int argc, const char **argv,
00690                         const char *replacement)
00691 {
00692   splay_tree_node n = find_definition (name, source, line);
00693 
00694   if (n)
00695     {
00696       struct macro_key *found_key = (struct macro_key *) n->key;
00697       struct macro_definition *found_def
00698         = (struct macro_definition *) n->value;
00699       int same = 1;
00700 
00701       /* Is this definition the same as the existing one?
00702          According to the standard, this comparison needs to be done
00703          on lists of tokens, not byte-by-byte, as we do here.  But
00704          that's too hard for us at the moment, and comparing
00705          byte-by-byte will only yield false negatives (i.e., extra
00706          warning messages), not false positives (i.e., unnoticed
00707          definition changes).  */
00708       if (kind != found_def->kind)
00709         same = 0;
00710       else if (strcmp (replacement, found_def->replacement))
00711         same = 0;
00712       else if (kind == macro_function_like)
00713         {
00714           if (argc != found_def->argc)
00715             same = 0;
00716           else
00717             {
00718               int i;
00719 
00720               for (i = 0; i < argc; i++)
00721                 if (strcmp (argv[i], found_def->argv[i]))
00722                   same = 0;
00723             }
00724         }
00725 
00726       if (! same)
00727         {
00728           char *source_fullname, *found_key_fullname;
00729           
00730           source_fullname = macro_source_fullname (source);
00731           found_key_fullname = macro_source_fullname (found_key->start_file);
00732           complaint (&symfile_complaints,
00733                      _("macro `%s' redefined at %s:%d; "
00734                        "original definition at %s:%d"),
00735                      name, source_fullname, line, found_key_fullname,
00736                      found_key->start_line);
00737           xfree (found_key_fullname);
00738           xfree (source_fullname);
00739         }
00740 
00741       return found_key;
00742     }
00743   else
00744     return 0;
00745 }
00746 
00747 /* A helper function to define a new object-like macro.  */
00748 
00749 static void
00750 macro_define_object_internal (struct macro_source_file *source, int line,
00751                               const char *name, const char *replacement,
00752                               enum macro_special_kind kind)
00753 {
00754   struct macro_table *t = source->table;
00755   struct macro_key *k = NULL;
00756   struct macro_definition *d;
00757 
00758   if (! t->redef_ok)
00759     k = check_for_redefinition (source, line, 
00760                                 name, macro_object_like,
00761                                 0, 0,
00762                                 replacement);
00763 
00764   /* If we're redefining a symbol, and the existing key would be
00765      identical to our new key, then the splay_tree_insert function
00766      will try to delete the old definition.  When the definition is
00767      living on an obstack, this isn't a happy thing.
00768 
00769      Since this only happens in the presence of questionable debug
00770      info, we just ignore all definitions after the first.  The only
00771      case I know of where this arises is in GCC's output for
00772      predefined macros, and all the definitions are the same in that
00773      case.  */
00774   if (k && ! key_compare (k, name, source, line))
00775     return;
00776 
00777   k = new_macro_key (t, name, source, line);
00778   d = new_macro_definition (t, macro_object_like, kind, 0, replacement);
00779   splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
00780 }
00781 
00782 void
00783 macro_define_object (struct macro_source_file *source, int line,
00784                      const char *name, const char *replacement)
00785 {
00786   macro_define_object_internal (source, line, name, replacement,
00787                                 macro_ordinary);
00788 }
00789 
00790 /* See macrotab.h.  */
00791 
00792 void
00793 macro_define_special (struct macro_table *table)
00794 {
00795   macro_define_object_internal (table->main_source, -1, "__FILE__", "",
00796                                 macro_FILE);
00797   macro_define_object_internal (table->main_source, -1, "__LINE__", "",
00798                                 macro_LINE);
00799 }
00800 
00801 void
00802 macro_define_function (struct macro_source_file *source, int line,
00803                        const char *name, int argc, const char **argv,
00804                        const char *replacement)
00805 {
00806   struct macro_table *t = source->table;
00807   struct macro_key *k = NULL;
00808   struct macro_definition *d;
00809 
00810   if (! t->redef_ok)
00811     k = check_for_redefinition (source, line,
00812                                 name, macro_function_like,
00813                                 argc, argv,
00814                                 replacement);
00815 
00816   /* See comments about duplicate keys in macro_define_object.  */
00817   if (k && ! key_compare (k, name, source, line))
00818     return;
00819 
00820   /* We should also check here that all the argument names in ARGV are
00821      distinct.  */
00822 
00823   k = new_macro_key (t, name, source, line);
00824   d = new_macro_definition (t, macro_function_like, argc, argv, replacement);
00825   splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
00826 }
00827 
00828 
00829 void
00830 macro_undef (struct macro_source_file *source, int line,
00831              const char *name)
00832 {
00833   splay_tree_node n = find_definition (name, source, line);
00834 
00835   if (n)
00836     {
00837       struct macro_key *key = (struct macro_key *) n->key;
00838 
00839       /* If we're removing a definition at exactly the same point that
00840          we defined it, then just delete the entry altogether.  GCC
00841          4.1.2 will generate DWARF that says to do this if you pass it
00842          arguments like '-DFOO -UFOO -DFOO=2'.  */
00843       if (source == key->start_file
00844           && line == key->start_line)
00845         splay_tree_remove (source->table->definitions, n->key);
00846 
00847       else
00848         {
00849           /* This function is the only place a macro's end-of-scope
00850              location gets set to anything other than "end of the
00851              compilation unit" (i.e., end_file is zero).  So if this
00852              macro already has its end-of-scope set, then we're
00853              probably seeing a second #undefinition for the same
00854              #definition.  */
00855           if (key->end_file)
00856             {
00857               char *source_fullname, *key_fullname;
00858 
00859               source_fullname = macro_source_fullname (source);
00860               key_fullname = macro_source_fullname (key->end_file);
00861               complaint (&symfile_complaints,
00862                          _("macro '%s' is #undefined twice,"
00863                            " at %s:%d and %s:%d"),
00864                          name, source_fullname, line, key_fullname,
00865                          key->end_line);
00866               xfree (key_fullname);
00867               xfree (source_fullname);
00868             }
00869 
00870           /* Whether or not we've seen a prior #undefinition, wipe out
00871              the old ending point, and make this the ending point.  */
00872           key->end_file = source;
00873           key->end_line = line;
00874         }
00875     }
00876   else
00877     {
00878       /* According to the ISO C standard, an #undef for a symbol that
00879          has no macro definition in scope is ignored.  So we should
00880          ignore it too.  */
00881 #if 0
00882       complaint (&symfile_complaints,
00883                  _("no definition for macro `%s' in scope to #undef at %s:%d"),
00884                  name, source->filename, line);
00885 #endif
00886     }
00887 }
00888 
00889 /* A helper function that rewrites the definition of a special macro,
00890    when needed.  */
00891 
00892 static struct macro_definition *
00893 fixup_definition (const char *filename, int line, struct macro_definition *def)
00894 {
00895   static char *saved_expansion;
00896 
00897   if (saved_expansion)
00898     {
00899       xfree (saved_expansion);
00900       saved_expansion = NULL;
00901     }
00902 
00903   if (def->kind == macro_object_like)
00904     {
00905       if (def->argc == macro_FILE)
00906         {
00907           saved_expansion = macro_stringify (filename);
00908           def->replacement = saved_expansion;
00909         }
00910       else if (def->argc == macro_LINE)
00911         {
00912           saved_expansion = xstrprintf ("%d", line);
00913           def->replacement = saved_expansion;
00914         }
00915     }
00916 
00917   return def;
00918 }
00919 
00920 struct macro_definition *
00921 macro_lookup_definition (struct macro_source_file *source,
00922                          int line, const char *name)
00923 {
00924   splay_tree_node n = find_definition (name, source, line);
00925 
00926   if (n)
00927     {
00928       struct macro_definition *retval;
00929       char *source_fullname;
00930 
00931       source_fullname = macro_source_fullname (source);
00932       retval = fixup_definition (source_fullname, line,
00933                                  (struct macro_definition *) n->value);
00934       xfree (source_fullname);
00935       return retval;
00936     }
00937   else
00938     return 0;
00939 }
00940 
00941 
00942 struct macro_source_file *
00943 macro_definition_location (struct macro_source_file *source,
00944                            int line,
00945                            const char *name,
00946                            int *definition_line)
00947 {
00948   splay_tree_node n = find_definition (name, source, line);
00949 
00950   if (n)
00951     {
00952       struct macro_key *key = (struct macro_key *) n->key;
00953 
00954       *definition_line = key->start_line;
00955       return key->start_file;
00956     }
00957   else
00958     return 0;
00959 }
00960 
00961 
00962 /* The type for callback data for iterating the splay tree in
00963    macro_for_each and macro_for_each_in_scope.  Only the latter uses
00964    the FILE and LINE fields.  */
00965 struct macro_for_each_data
00966 {
00967   macro_callback_fn fn;
00968   void *user_data;
00969   struct macro_source_file *file;
00970   int line;
00971 };
00972 
00973 /* Helper function for macro_for_each.  */
00974 static int
00975 foreach_macro (splay_tree_node node, void *arg)
00976 {
00977   struct macro_for_each_data *datum = (struct macro_for_each_data *) arg;
00978   struct macro_key *key = (struct macro_key *) node->key;
00979   struct macro_definition *def;
00980   char *key_fullname;
00981 
00982   key_fullname = macro_source_fullname (key->start_file);
00983   def = fixup_definition (key_fullname, key->start_line,
00984                           (struct macro_definition *) node->value);
00985   xfree (key_fullname);
00986 
00987   (*datum->fn) (key->name, def, key->start_file, key->start_line,
00988                 datum->user_data);
00989   return 0;
00990 }
00991 
00992 /* Call FN for every macro in TABLE.  */
00993 void
00994 macro_for_each (struct macro_table *table, macro_callback_fn fn,
00995                 void *user_data)
00996 {
00997   struct macro_for_each_data datum;
00998 
00999   datum.fn = fn;
01000   datum.user_data = user_data;
01001   datum.file = NULL;
01002   datum.line = 0;
01003   splay_tree_foreach (table->definitions, foreach_macro, &datum);
01004 }
01005 
01006 static int
01007 foreach_macro_in_scope (splay_tree_node node, void *info)
01008 {
01009   struct macro_for_each_data *datum = (struct macro_for_each_data *) info;
01010   struct macro_key *key = (struct macro_key *) node->key;
01011   struct macro_definition *def;
01012   char *datum_fullname;
01013 
01014   datum_fullname = macro_source_fullname (datum->file);
01015   def = fixup_definition (datum_fullname, datum->line,
01016                           (struct macro_definition *) node->value);
01017   xfree (datum_fullname);
01018 
01019   /* See if this macro is defined before the passed-in line, and
01020      extends past that line.  */
01021   if (compare_locations (key->start_file, key->start_line,
01022                          datum->file, datum->line) < 0
01023       && (!key->end_file
01024           || compare_locations (key->end_file, key->end_line,
01025                                 datum->file, datum->line) >= 0))
01026     (*datum->fn) (key->name, def, key->start_file, key->start_line,
01027                   datum->user_data);
01028   return 0;
01029 }
01030 
01031 /* Call FN for every macro is visible in SCOPE.  */
01032 void
01033 macro_for_each_in_scope (struct macro_source_file *file, int line,
01034                          macro_callback_fn fn, void *user_data)
01035 {
01036   struct macro_for_each_data datum;
01037 
01038   datum.fn = fn;
01039   datum.user_data = user_data;
01040   datum.file = file;
01041   datum.line = line;
01042   splay_tree_foreach (file->table->definitions,
01043                       foreach_macro_in_scope, &datum);
01044 }
01045 
01046 
01047 
01048 /* Creating and freeing macro tables.  */
01049 
01050 
01051 struct macro_table *
01052 new_macro_table (struct obstack *obstack, struct bcache *b,
01053                  const char *comp_dir)
01054 {
01055   struct macro_table *t;
01056 
01057   /* First, get storage for the `struct macro_table' itself.  */
01058   if (obstack)
01059     t = obstack_alloc (obstack, sizeof (*t));
01060   else
01061     t = xmalloc (sizeof (*t));
01062 
01063   memset (t, 0, sizeof (*t));
01064   t->obstack = obstack;
01065   t->bcache = b;
01066   t->main_source = NULL;
01067   t->comp_dir = comp_dir;
01068   t->redef_ok = 0;
01069   t->definitions = (splay_tree_new_with_allocator
01070                     (macro_tree_compare,
01071                      ((splay_tree_delete_key_fn) macro_tree_delete_key),
01072                      ((splay_tree_delete_value_fn) macro_tree_delete_value),
01073                      ((splay_tree_allocate_fn) macro_alloc),
01074                      ((splay_tree_deallocate_fn) macro_free),
01075                      t));
01076   
01077   return t;
01078 }
01079 
01080 
01081 void
01082 free_macro_table (struct macro_table *table)
01083 {
01084   /* Free the source file tree.  */
01085   free_macro_source_file (table->main_source);
01086 
01087   /* Free the table of macro definitions.  */
01088   splay_tree_delete (table->definitions);
01089 }
01090 
01091 /* See macrotab.h for the comment.  */
01092 
01093 char *
01094 macro_source_fullname (struct macro_source_file *file)
01095 {
01096   if (file->table->comp_dir == NULL || IS_ABSOLUTE_PATH (file->filename))
01097     return xstrdup (file->filename);
01098 
01099   return concat (file->table->comp_dir, SLASH_STRING, file->filename, NULL);
01100 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines