| 1 | /* C preprocessor macro tables for GDB. |
| 2 | Copyright (C) 2002, 2007-2012 Free Software Foundation, Inc. |
| 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 |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 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 |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "gdb_obstack.h" |
| 22 | #include "splay-tree.h" |
| 23 | #include "filenames.h" |
| 24 | #include "symtab.h" |
| 25 | #include "symfile.h" |
| 26 | #include "objfiles.h" |
| 27 | #include "macrotab.h" |
| 28 | #include "gdb_assert.h" |
| 29 | #include "bcache.h" |
| 30 | #include "complaints.h" |
| 31 | #include "macroexp.h" |
| 32 | |
| 33 | \f |
| 34 | /* The macro table structure. */ |
| 35 | |
| 36 | struct 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. */ |
| 44 | struct bcache *bcache; |
| 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 | |
| 51 | /* True if macros in this table can be redefined without issuing an |
| 52 | error. */ |
| 53 | int redef_ok; |
| 54 | |
| 55 | /* The table of macro definitions. This is a splay tree (an ordered |
| 56 | binary tree that stays balanced, effectively), sorted by macro |
| 57 | name. Where a macro gets defined more than once (presumably with |
| 58 | an #undefinition in between), we sort the definitions by the |
| 59 | order they would appear in the preprocessor's output. That is, |
| 60 | if `a.c' #includes `m.h' and then #includes `n.h', and both |
| 61 | header files #define X (with an #undef somewhere in between), |
| 62 | then the definition from `m.h' appears in our splay tree before |
| 63 | the one from `n.h'. |
| 64 | |
| 65 | The splay tree's keys are `struct macro_key' pointers; |
| 66 | the values are `struct macro_definition' pointers. |
| 67 | |
| 68 | The splay tree, its nodes, and the keys and values are allocated |
| 69 | in obstack, if it's non-zero, or with xmalloc otherwise. The |
| 70 | macro names, argument names, argument name arrays, and definition |
| 71 | strings are all allocated in bcache, if non-zero, or with xmalloc |
| 72 | otherwise. */ |
| 73 | splay_tree definitions; |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | \f |
| 78 | /* Allocation and freeing functions. */ |
| 79 | |
| 80 | /* Allocate SIZE bytes of memory appropriately for the macro table T. |
| 81 | This just checks whether T has an obstack, or whether its pieces |
| 82 | should be allocated with xmalloc. */ |
| 83 | static void * |
| 84 | macro_alloc (int size, struct macro_table *t) |
| 85 | { |
| 86 | if (t->obstack) |
| 87 | return obstack_alloc (t->obstack, size); |
| 88 | else |
| 89 | return xmalloc (size); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void |
| 94 | macro_free (void *object, struct macro_table *t) |
| 95 | { |
| 96 | if (t->obstack) |
| 97 | /* There are cases where we need to remove entries from a macro |
| 98 | table, even when reading debugging information. This should be |
| 99 | rare, and there's no easy way to free arbitrary data from an |
| 100 | obstack, so we just leak it. */ |
| 101 | ; |
| 102 | else |
| 103 | xfree (object); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* If the macro table T has a bcache, then cache the LEN bytes at ADDR |
| 108 | there, and return the cached copy. Otherwise, just xmalloc a copy |
| 109 | of the bytes, and return a pointer to that. */ |
| 110 | static const void * |
| 111 | macro_bcache (struct macro_table *t, const void *addr, int len) |
| 112 | { |
| 113 | if (t->bcache) |
| 114 | return bcache (addr, len, t->bcache); |
| 115 | else |
| 116 | { |
| 117 | void *copy = xmalloc (len); |
| 118 | |
| 119 | memcpy (copy, addr, len); |
| 120 | return copy; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /* If the macro table T has a bcache, cache the null-terminated string |
| 126 | S there, and return a pointer to the cached copy. Otherwise, |
| 127 | xmalloc a copy and return that. */ |
| 128 | static const char * |
| 129 | macro_bcache_str (struct macro_table *t, const char *s) |
| 130 | { |
| 131 | return macro_bcache (t, s, strlen (s) + 1); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* Free a possibly bcached object OBJ. That is, if the macro table T |
| 136 | has a bcache, do nothing; otherwise, xfree OBJ. */ |
| 137 | static void |
| 138 | macro_bcache_free (struct macro_table *t, void *obj) |
| 139 | { |
| 140 | if (t->bcache) |
| 141 | /* There are cases where we need to remove entries from a macro |
| 142 | table, even when reading debugging information. This should be |
| 143 | rare, and there's no easy way to free data from a bcache, so we |
| 144 | just leak it. */ |
| 145 | ; |
| 146 | else |
| 147 | xfree (obj); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | \f |
| 152 | /* Macro tree keys, w/their comparison, allocation, and freeing functions. */ |
| 153 | |
| 154 | /* A key in the splay tree. */ |
| 155 | struct macro_key |
| 156 | { |
| 157 | /* The table we're in. We only need this in order to free it, since |
| 158 | the splay tree library's key and value freeing functions require |
| 159 | that the key or value contain all the information needed to free |
| 160 | themselves. */ |
| 161 | struct macro_table *table; |
| 162 | |
| 163 | /* The name of the macro. This is in the table's bcache, if it has |
| 164 | one. */ |
| 165 | const char *name; |
| 166 | |
| 167 | /* The source file and line number where the definition's scope |
| 168 | begins. This is also the line of the definition itself. */ |
| 169 | struct macro_source_file *start_file; |
| 170 | int start_line; |
| 171 | |
| 172 | /* The first source file and line after the definition's scope. |
| 173 | (That is, the scope does not include this endpoint.) If end_file |
| 174 | is zero, then the definition extends to the end of the |
| 175 | compilation unit. */ |
| 176 | struct macro_source_file *end_file; |
| 177 | int end_line; |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | /* Return the #inclusion depth of the source file FILE. This is the |
| 182 | number of #inclusions it took to reach this file. For the main |
| 183 | source file, the #inclusion depth is zero; for a file it #includes |
| 184 | directly, the depth would be one; and so on. */ |
| 185 | static int |
| 186 | inclusion_depth (struct macro_source_file *file) |
| 187 | { |
| 188 | int depth; |
| 189 | |
| 190 | for (depth = 0; file->included_by; depth++) |
| 191 | file = file->included_by; |
| 192 | |
| 193 | return depth; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /* Compare two source locations (from the same compilation unit). |
| 198 | This is part of the comparison function for the tree of |
| 199 | definitions. |
| 200 | |
| 201 | LINE1 and LINE2 are line numbers in the source files FILE1 and |
| 202 | FILE2. Return a value: |
| 203 | - less than zero if {LINE,FILE}1 comes before {LINE,FILE}2, |
| 204 | - greater than zero if {LINE,FILE}1 comes after {LINE,FILE}2, or |
| 205 | - zero if they are equal. |
| 206 | |
| 207 | When the two locations are in different source files --- perhaps |
| 208 | one is in a header, while another is in the main source file --- we |
| 209 | order them by where they would appear in the fully pre-processed |
| 210 | sources, where all the #included files have been substituted into |
| 211 | their places. */ |
| 212 | static int |
| 213 | compare_locations (struct macro_source_file *file1, int line1, |
| 214 | struct macro_source_file *file2, int line2) |
| 215 | { |
| 216 | /* We want to treat positions in an #included file as coming *after* |
| 217 | the line containing the #include, but *before* the line after the |
| 218 | include. As we walk up the #inclusion tree toward the main |
| 219 | source file, we update fileX and lineX as we go; includedX |
| 220 | indicates whether the original position was from the #included |
| 221 | file. */ |
| 222 | int included1 = 0; |
| 223 | int included2 = 0; |
| 224 | |
| 225 | /* If a file is zero, that means "end of compilation unit." Handle |
| 226 | that specially. */ |
| 227 | if (! file1) |
| 228 | { |
| 229 | if (! file2) |
| 230 | return 0; |
| 231 | else |
| 232 | return 1; |
| 233 | } |
| 234 | else if (! file2) |
| 235 | return -1; |
| 236 | |
| 237 | /* If the two files are not the same, find their common ancestor in |
| 238 | the #inclusion tree. */ |
| 239 | if (file1 != file2) |
| 240 | { |
| 241 | /* If one file is deeper than the other, walk up the #inclusion |
| 242 | chain until the two files are at least at the same *depth*. |
| 243 | Then, walk up both files in synchrony until they're the same |
| 244 | file. That file is the common ancestor. */ |
| 245 | int depth1 = inclusion_depth (file1); |
| 246 | int depth2 = inclusion_depth (file2); |
| 247 | |
| 248 | /* Only one of these while loops will ever execute in any given |
| 249 | case. */ |
| 250 | while (depth1 > depth2) |
| 251 | { |
| 252 | line1 = file1->included_at_line; |
| 253 | file1 = file1->included_by; |
| 254 | included1 = 1; |
| 255 | depth1--; |
| 256 | } |
| 257 | while (depth2 > depth1) |
| 258 | { |
| 259 | line2 = file2->included_at_line; |
| 260 | file2 = file2->included_by; |
| 261 | included2 = 1; |
| 262 | depth2--; |
| 263 | } |
| 264 | |
| 265 | /* Now both file1 and file2 are at the same depth. Walk toward |
| 266 | the root of the tree until we find where the branches meet. */ |
| 267 | while (file1 != file2) |
| 268 | { |
| 269 | line1 = file1->included_at_line; |
| 270 | file1 = file1->included_by; |
| 271 | /* At this point, we know that the case the includedX flags |
| 272 | are trying to deal with won't come up, but we'll just |
| 273 | maintain them anyway. */ |
| 274 | included1 = 1; |
| 275 | |
| 276 | line2 = file2->included_at_line; |
| 277 | file2 = file2->included_by; |
| 278 | included2 = 1; |
| 279 | |
| 280 | /* Sanity check. If file1 and file2 are really from the |
| 281 | same compilation unit, then they should both be part of |
| 282 | the same tree, and this shouldn't happen. */ |
| 283 | gdb_assert (file1 && file2); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /* Now we've got two line numbers in the same file. */ |
| 288 | if (line1 == line2) |
| 289 | { |
| 290 | /* They can't both be from #included files. Then we shouldn't |
| 291 | have walked up this far. */ |
| 292 | gdb_assert (! included1 || ! included2); |
| 293 | |
| 294 | /* Any #included position comes after a non-#included position |
| 295 | with the same line number in the #including file. */ |
| 296 | if (included1) |
| 297 | return 1; |
| 298 | else if (included2) |
| 299 | return -1; |
| 300 | else |
| 301 | return 0; |
| 302 | } |
| 303 | else |
| 304 | return line1 - line2; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /* Compare a macro key KEY against NAME, the source file FILE, and |
| 309 | line number LINE. |
| 310 | |
| 311 | Sort definitions by name; for two definitions with the same name, |
| 312 | place the one whose definition comes earlier before the one whose |
| 313 | definition comes later. |
| 314 | |
| 315 | Return -1, 0, or 1 if key comes before, is identical to, or comes |
| 316 | after NAME, FILE, and LINE. */ |
| 317 | static int |
| 318 | key_compare (struct macro_key *key, |
| 319 | const char *name, struct macro_source_file *file, int line) |
| 320 | { |
| 321 | int names = strcmp (key->name, name); |
| 322 | |
| 323 | if (names) |
| 324 | return names; |
| 325 | |
| 326 | return compare_locations (key->start_file, key->start_line, |
| 327 | file, line); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | /* The macro tree comparison function, typed for the splay tree |
| 332 | library's happiness. */ |
| 333 | static int |
| 334 | macro_tree_compare (splay_tree_key untyped_key1, |
| 335 | splay_tree_key untyped_key2) |
| 336 | { |
| 337 | struct macro_key *key1 = (struct macro_key *) untyped_key1; |
| 338 | struct macro_key *key2 = (struct macro_key *) untyped_key2; |
| 339 | |
| 340 | return key_compare (key1, key2->name, key2->start_file, key2->start_line); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* Construct a new macro key node for a macro in table T whose name is |
| 345 | NAME, and whose scope starts at LINE in FILE; register the name in |
| 346 | the bcache. */ |
| 347 | static struct macro_key * |
| 348 | new_macro_key (struct macro_table *t, |
| 349 | const char *name, |
| 350 | struct macro_source_file *file, |
| 351 | int line) |
| 352 | { |
| 353 | struct macro_key *k = macro_alloc (sizeof (*k), t); |
| 354 | |
| 355 | memset (k, 0, sizeof (*k)); |
| 356 | k->table = t; |
| 357 | k->name = macro_bcache_str (t, name); |
| 358 | k->start_file = file; |
| 359 | k->start_line = line; |
| 360 | k->end_file = 0; |
| 361 | |
| 362 | return k; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | static void |
| 367 | macro_tree_delete_key (void *untyped_key) |
| 368 | { |
| 369 | struct macro_key *key = (struct macro_key *) untyped_key; |
| 370 | |
| 371 | macro_bcache_free (key->table, (char *) key->name); |
| 372 | macro_free (key, key->table); |
| 373 | } |
| 374 | |
| 375 | |
| 376 | \f |
| 377 | /* Building and querying the tree of #included files. */ |
| 378 | |
| 379 | |
| 380 | /* Allocate and initialize a new source file structure. */ |
| 381 | static struct macro_source_file * |
| 382 | new_source_file (struct macro_table *t, |
| 383 | const char *filename) |
| 384 | { |
| 385 | /* Get space for the source file structure itself. */ |
| 386 | struct macro_source_file *f = macro_alloc (sizeof (*f), t); |
| 387 | |
| 388 | memset (f, 0, sizeof (*f)); |
| 389 | f->table = t; |
| 390 | f->filename = macro_bcache_str (t, filename); |
| 391 | f->includes = 0; |
| 392 | |
| 393 | return f; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /* Free a source file, and all the source files it #included. */ |
| 398 | static void |
| 399 | free_macro_source_file (struct macro_source_file *src) |
| 400 | { |
| 401 | struct macro_source_file *child, *next_child; |
| 402 | |
| 403 | /* Free this file's children. */ |
| 404 | for (child = src->includes; child; child = next_child) |
| 405 | { |
| 406 | next_child = child->next_included; |
| 407 | free_macro_source_file (child); |
| 408 | } |
| 409 | |
| 410 | macro_bcache_free (src->table, (char *) src->filename); |
| 411 | macro_free (src, src->table); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | struct macro_source_file * |
| 416 | macro_set_main (struct macro_table *t, |
| 417 | const char *filename) |
| 418 | { |
| 419 | /* You can't change a table's main source file. What would that do |
| 420 | to the tree? */ |
| 421 | gdb_assert (! t->main_source); |
| 422 | |
| 423 | t->main_source = new_source_file (t, filename); |
| 424 | |
| 425 | return t->main_source; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | struct macro_source_file * |
| 430 | macro_main (struct macro_table *t) |
| 431 | { |
| 432 | gdb_assert (t->main_source); |
| 433 | |
| 434 | return t->main_source; |
| 435 | } |
| 436 | |
| 437 | |
| 438 | void |
| 439 | macro_allow_redefinitions (struct macro_table *t) |
| 440 | { |
| 441 | gdb_assert (! t->obstack); |
| 442 | t->redef_ok = 1; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | struct macro_source_file * |
| 447 | macro_include (struct macro_source_file *source, |
| 448 | int line, |
| 449 | const char *included) |
| 450 | { |
| 451 | struct macro_source_file *new; |
| 452 | struct macro_source_file **link; |
| 453 | |
| 454 | /* Find the right position in SOURCE's `includes' list for the new |
| 455 | file. Skip inclusions at earlier lines, until we find one at the |
| 456 | same line or later --- or until the end of the list. */ |
| 457 | for (link = &source->includes; |
| 458 | *link && (*link)->included_at_line < line; |
| 459 | link = &(*link)->next_included) |
| 460 | ; |
| 461 | |
| 462 | /* Did we find another file already #included at the same line as |
| 463 | the new one? */ |
| 464 | if (*link && line == (*link)->included_at_line) |
| 465 | { |
| 466 | /* This means the compiler is emitting bogus debug info. (GCC |
| 467 | circa March 2002 did this.) It also means that the splay |
| 468 | tree ordering function, macro_tree_compare, will abort, |
| 469 | because it can't tell which #inclusion came first. But GDB |
| 470 | should tolerate bad debug info. So: |
| 471 | |
| 472 | First, squawk. */ |
| 473 | complaint (&symfile_complaints, |
| 474 | _("both `%s' and `%s' allegedly #included at %s:%d"), |
| 475 | included, (*link)->filename, source->filename, line); |
| 476 | |
| 477 | /* Now, choose a new, unoccupied line number for this |
| 478 | #inclusion, after the alleged #inclusion line. */ |
| 479 | while (*link && line == (*link)->included_at_line) |
| 480 | { |
| 481 | /* This line number is taken, so try the next line. */ |
| 482 | line++; |
| 483 | link = &(*link)->next_included; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /* At this point, we know that LINE is an unused line number, and |
| 488 | *LINK points to the entry an #inclusion at that line should |
| 489 | precede. */ |
| 490 | new = new_source_file (source->table, included); |
| 491 | new->included_by = source; |
| 492 | new->included_at_line = line; |
| 493 | new->next_included = *link; |
| 494 | *link = new; |
| 495 | |
| 496 | return new; |
| 497 | } |
| 498 | |
| 499 | |
| 500 | struct macro_source_file * |
| 501 | macro_lookup_inclusion (struct macro_source_file *source, const char *name) |
| 502 | { |
| 503 | /* Is SOURCE itself named NAME? */ |
| 504 | if (filename_cmp (name, source->filename) == 0) |
| 505 | return source; |
| 506 | |
| 507 | /* The filename in the source structure is probably a full path, but |
| 508 | NAME could be just the final component of the name. */ |
| 509 | { |
| 510 | int name_len = strlen (name); |
| 511 | int src_name_len = strlen (source->filename); |
| 512 | |
| 513 | /* We do mean < here, and not <=; if the lengths are the same, |
| 514 | then the filename_cmp above should have triggered, and we need to |
| 515 | check for a slash here. */ |
| 516 | if (name_len < src_name_len |
| 517 | && IS_DIR_SEPARATOR (source->filename[src_name_len - name_len - 1]) |
| 518 | && filename_cmp (name, |
| 519 | source->filename + src_name_len - name_len) == 0) |
| 520 | return source; |
| 521 | } |
| 522 | |
| 523 | /* It's not us. Try all our children, and return the lowest. */ |
| 524 | { |
| 525 | struct macro_source_file *child; |
| 526 | struct macro_source_file *best = NULL; |
| 527 | int best_depth = 0; |
| 528 | |
| 529 | for (child = source->includes; child; child = child->next_included) |
| 530 | { |
| 531 | struct macro_source_file *result |
| 532 | = macro_lookup_inclusion (child, name); |
| 533 | |
| 534 | if (result) |
| 535 | { |
| 536 | int result_depth = inclusion_depth (result); |
| 537 | |
| 538 | if (! best || result_depth < best_depth) |
| 539 | { |
| 540 | best = result; |
| 541 | best_depth = result_depth; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return best; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | |
| 551 | \f |
| 552 | /* Registering and looking up macro definitions. */ |
| 553 | |
| 554 | |
| 555 | /* Construct a definition for a macro in table T. Cache all strings, |
| 556 | and the macro_definition structure itself, in T's bcache. */ |
| 557 | static struct macro_definition * |
| 558 | new_macro_definition (struct macro_table *t, |
| 559 | enum macro_kind kind, |
| 560 | int argc, const char **argv, |
| 561 | const char *replacement) |
| 562 | { |
| 563 | struct macro_definition *d = macro_alloc (sizeof (*d), t); |
| 564 | |
| 565 | memset (d, 0, sizeof (*d)); |
| 566 | d->table = t; |
| 567 | d->kind = kind; |
| 568 | d->replacement = macro_bcache_str (t, replacement); |
| 569 | d->argc = argc; |
| 570 | |
| 571 | if (kind == macro_function_like) |
| 572 | { |
| 573 | int i; |
| 574 | const char **cached_argv; |
| 575 | int cached_argv_size = argc * sizeof (*cached_argv); |
| 576 | |
| 577 | /* Bcache all the arguments. */ |
| 578 | cached_argv = alloca (cached_argv_size); |
| 579 | for (i = 0; i < argc; i++) |
| 580 | cached_argv[i] = macro_bcache_str (t, argv[i]); |
| 581 | |
| 582 | /* Now bcache the array of argument pointers itself. */ |
| 583 | d->argv = macro_bcache (t, cached_argv, cached_argv_size); |
| 584 | } |
| 585 | |
| 586 | /* We don't bcache the entire definition structure because it's got |
| 587 | a pointer to the macro table in it; since each compilation unit |
| 588 | has its own macro table, you'd only get bcache hits for identical |
| 589 | definitions within a compilation unit, which seems unlikely. |
| 590 | |
| 591 | "So, why do macro definitions have pointers to their macro tables |
| 592 | at all?" Well, when the splay tree library wants to free a |
| 593 | node's value, it calls the value freeing function with nothing |
| 594 | but the value itself. It makes the (apparently reasonable) |
| 595 | assumption that the value carries enough information to free |
| 596 | itself. But not all macro tables have bcaches, so not all macro |
| 597 | definitions would be bcached. There's no way to tell whether a |
| 598 | given definition is bcached without knowing which table the |
| 599 | definition belongs to. ... blah. The thing's only sixteen |
| 600 | bytes anyway, and we can still bcache the name, args, and |
| 601 | definition, so we just don't bother bcaching the definition |
| 602 | structure itself. */ |
| 603 | return d; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | /* Free a macro definition. */ |
| 608 | static void |
| 609 | macro_tree_delete_value (void *untyped_definition) |
| 610 | { |
| 611 | struct macro_definition *d = (struct macro_definition *) untyped_definition; |
| 612 | struct macro_table *t = d->table; |
| 613 | |
| 614 | if (d->kind == macro_function_like) |
| 615 | { |
| 616 | int i; |
| 617 | |
| 618 | for (i = 0; i < d->argc; i++) |
| 619 | macro_bcache_free (t, (char *) d->argv[i]); |
| 620 | macro_bcache_free (t, (char **) d->argv); |
| 621 | } |
| 622 | |
| 623 | macro_bcache_free (t, (char *) d->replacement); |
| 624 | macro_free (d, t); |
| 625 | } |
| 626 | |
| 627 | |
| 628 | /* Find the splay tree node for the definition of NAME at LINE in |
| 629 | SOURCE, or zero if there is none. */ |
| 630 | static splay_tree_node |
| 631 | find_definition (const char *name, |
| 632 | struct macro_source_file *file, |
| 633 | int line) |
| 634 | { |
| 635 | struct macro_table *t = file->table; |
| 636 | splay_tree_node n; |
| 637 | |
| 638 | /* Construct a macro_key object, just for the query. */ |
| 639 | struct macro_key query; |
| 640 | |
| 641 | query.name = name; |
| 642 | query.start_file = file; |
| 643 | query.start_line = line; |
| 644 | query.end_file = NULL; |
| 645 | |
| 646 | n = splay_tree_lookup (t->definitions, (splay_tree_key) &query); |
| 647 | if (! n) |
| 648 | { |
| 649 | /* It's okay for us to do two queries like this: the real work |
| 650 | of the searching is done when we splay, and splaying the tree |
| 651 | a second time at the same key is a constant time operation. |
| 652 | If this still bugs you, you could always just extend the |
| 653 | splay tree library with a predecessor-or-equal operation, and |
| 654 | use that. */ |
| 655 | splay_tree_node pred = splay_tree_predecessor (t->definitions, |
| 656 | (splay_tree_key) &query); |
| 657 | |
| 658 | if (pred) |
| 659 | { |
| 660 | /* Make sure this predecessor actually has the right name. |
| 661 | We just want to search within a given name's definitions. */ |
| 662 | struct macro_key *found = (struct macro_key *) pred->key; |
| 663 | |
| 664 | if (strcmp (found->name, name) == 0) |
| 665 | n = pred; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (n) |
| 670 | { |
| 671 | struct macro_key *found = (struct macro_key *) n->key; |
| 672 | |
| 673 | /* Okay, so this definition has the right name, and its scope |
| 674 | begins before the given source location. But does its scope |
| 675 | end after the given source location? */ |
| 676 | if (compare_locations (file, line, found->end_file, found->end_line) < 0) |
| 677 | return n; |
| 678 | else |
| 679 | return 0; |
| 680 | } |
| 681 | else |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | /* If NAME already has a definition in scope at LINE in SOURCE, return |
| 687 | the key. If the old definition is different from the definition |
| 688 | given by KIND, ARGC, ARGV, and REPLACEMENT, complain, too. |
| 689 | Otherwise, return zero. (ARGC and ARGV are meaningless unless KIND |
| 690 | is `macro_function_like'.) */ |
| 691 | static struct macro_key * |
| 692 | check_for_redefinition (struct macro_source_file *source, int line, |
| 693 | const char *name, enum macro_kind kind, |
| 694 | int argc, const char **argv, |
| 695 | const char *replacement) |
| 696 | { |
| 697 | splay_tree_node n = find_definition (name, source, line); |
| 698 | |
| 699 | if (n) |
| 700 | { |
| 701 | struct macro_key *found_key = (struct macro_key *) n->key; |
| 702 | struct macro_definition *found_def |
| 703 | = (struct macro_definition *) n->value; |
| 704 | int same = 1; |
| 705 | |
| 706 | /* Is this definition the same as the existing one? |
| 707 | According to the standard, this comparison needs to be done |
| 708 | on lists of tokens, not byte-by-byte, as we do here. But |
| 709 | that's too hard for us at the moment, and comparing |
| 710 | byte-by-byte will only yield false negatives (i.e., extra |
| 711 | warning messages), not false positives (i.e., unnoticed |
| 712 | definition changes). */ |
| 713 | if (kind != found_def->kind) |
| 714 | same = 0; |
| 715 | else if (strcmp (replacement, found_def->replacement)) |
| 716 | same = 0; |
| 717 | else if (kind == macro_function_like) |
| 718 | { |
| 719 | if (argc != found_def->argc) |
| 720 | same = 0; |
| 721 | else |
| 722 | { |
| 723 | int i; |
| 724 | |
| 725 | for (i = 0; i < argc; i++) |
| 726 | if (strcmp (argv[i], found_def->argv[i])) |
| 727 | same = 0; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if (! same) |
| 732 | { |
| 733 | complaint (&symfile_complaints, |
| 734 | _("macro `%s' redefined at %s:%d; " |
| 735 | "original definition at %s:%d"), |
| 736 | name, source->filename, line, |
| 737 | found_key->start_file->filename, found_key->start_line); |
| 738 | } |
| 739 | |
| 740 | return found_key; |
| 741 | } |
| 742 | else |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | /* A helper function to define a new object-like macro. */ |
| 747 | |
| 748 | static void |
| 749 | macro_define_object_internal (struct macro_source_file *source, int line, |
| 750 | const char *name, const char *replacement, |
| 751 | enum macro_special_kind kind) |
| 752 | { |
| 753 | struct macro_table *t = source->table; |
| 754 | struct macro_key *k = NULL; |
| 755 | struct macro_definition *d; |
| 756 | |
| 757 | if (! t->redef_ok) |
| 758 | k = check_for_redefinition (source, line, |
| 759 | name, macro_object_like, |
| 760 | 0, 0, |
| 761 | replacement); |
| 762 | |
| 763 | /* If we're redefining a symbol, and the existing key would be |
| 764 | identical to our new key, then the splay_tree_insert function |
| 765 | will try to delete the old definition. When the definition is |
| 766 | living on an obstack, this isn't a happy thing. |
| 767 | |
| 768 | Since this only happens in the presence of questionable debug |
| 769 | info, we just ignore all definitions after the first. The only |
| 770 | case I know of where this arises is in GCC's output for |
| 771 | predefined macros, and all the definitions are the same in that |
| 772 | case. */ |
| 773 | if (k && ! key_compare (k, name, source, line)) |
| 774 | return; |
| 775 | |
| 776 | k = new_macro_key (t, name, source, line); |
| 777 | d = new_macro_definition (t, macro_object_like, kind, 0, replacement); |
| 778 | splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d); |
| 779 | } |
| 780 | |
| 781 | void |
| 782 | macro_define_object (struct macro_source_file *source, int line, |
| 783 | const char *name, const char *replacement) |
| 784 | { |
| 785 | macro_define_object_internal (source, line, name, replacement, |
| 786 | macro_ordinary); |
| 787 | } |
| 788 | |
| 789 | /* See macrotab.h. */ |
| 790 | |
| 791 | void |
| 792 | macro_define_special (struct macro_table *table) |
| 793 | { |
| 794 | macro_define_object_internal (table->main_source, -1, "__FILE__", "", |
| 795 | macro_FILE); |
| 796 | macro_define_object_internal (table->main_source, -1, "__LINE__", "", |
| 797 | macro_LINE); |
| 798 | } |
| 799 | |
| 800 | void |
| 801 | macro_define_function (struct macro_source_file *source, int line, |
| 802 | const char *name, int argc, const char **argv, |
| 803 | const char *replacement) |
| 804 | { |
| 805 | struct macro_table *t = source->table; |
| 806 | struct macro_key *k = NULL; |
| 807 | struct macro_definition *d; |
| 808 | |
| 809 | if (! t->redef_ok) |
| 810 | k = check_for_redefinition (source, line, |
| 811 | name, macro_function_like, |
| 812 | argc, argv, |
| 813 | replacement); |
| 814 | |
| 815 | /* See comments about duplicate keys in macro_define_object. */ |
| 816 | if (k && ! key_compare (k, name, source, line)) |
| 817 | return; |
| 818 | |
| 819 | /* We should also check here that all the argument names in ARGV are |
| 820 | distinct. */ |
| 821 | |
| 822 | k = new_macro_key (t, name, source, line); |
| 823 | d = new_macro_definition (t, macro_function_like, argc, argv, replacement); |
| 824 | splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d); |
| 825 | } |
| 826 | |
| 827 | |
| 828 | void |
| 829 | macro_undef (struct macro_source_file *source, int line, |
| 830 | const char *name) |
| 831 | { |
| 832 | splay_tree_node n = find_definition (name, source, line); |
| 833 | |
| 834 | if (n) |
| 835 | { |
| 836 | struct macro_key *key = (struct macro_key *) n->key; |
| 837 | |
| 838 | /* If we're removing a definition at exactly the same point that |
| 839 | we defined it, then just delete the entry altogether. GCC |
| 840 | 4.1.2 will generate DWARF that says to do this if you pass it |
| 841 | arguments like '-DFOO -UFOO -DFOO=2'. */ |
| 842 | if (source == key->start_file |
| 843 | && line == key->start_line) |
| 844 | splay_tree_remove (source->table->definitions, n->key); |
| 845 | |
| 846 | else |
| 847 | { |
| 848 | /* This function is the only place a macro's end-of-scope |
| 849 | location gets set to anything other than "end of the |
| 850 | compilation unit" (i.e., end_file is zero). So if this |
| 851 | macro already has its end-of-scope set, then we're |
| 852 | probably seeing a second #undefinition for the same |
| 853 | #definition. */ |
| 854 | if (key->end_file) |
| 855 | { |
| 856 | complaint (&symfile_complaints, |
| 857 | _("macro '%s' is #undefined twice," |
| 858 | " at %s:%d and %s:%d"), |
| 859 | name, |
| 860 | source->filename, line, |
| 861 | key->end_file->filename, key->end_line); |
| 862 | } |
| 863 | |
| 864 | /* Whether or not we've seen a prior #undefinition, wipe out |
| 865 | the old ending point, and make this the ending point. */ |
| 866 | key->end_file = source; |
| 867 | key->end_line = line; |
| 868 | } |
| 869 | } |
| 870 | else |
| 871 | { |
| 872 | /* According to the ISO C standard, an #undef for a symbol that |
| 873 | has no macro definition in scope is ignored. So we should |
| 874 | ignore it too. */ |
| 875 | #if 0 |
| 876 | complaint (&symfile_complaints, |
| 877 | _("no definition for macro `%s' in scope to #undef at %s:%d"), |
| 878 | name, source->filename, line); |
| 879 | #endif |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | /* A helper function that rewrites the definition of a special macro, |
| 884 | when needed. */ |
| 885 | |
| 886 | static struct macro_definition * |
| 887 | fixup_definition (const char *filename, int line, struct macro_definition *def) |
| 888 | { |
| 889 | static char *saved_expansion; |
| 890 | |
| 891 | if (saved_expansion) |
| 892 | { |
| 893 | xfree (saved_expansion); |
| 894 | saved_expansion = NULL; |
| 895 | } |
| 896 | |
| 897 | if (def->kind == macro_object_like) |
| 898 | { |
| 899 | if (def->argc == macro_FILE) |
| 900 | { |
| 901 | saved_expansion = macro_stringify (filename); |
| 902 | def->replacement = saved_expansion; |
| 903 | } |
| 904 | else if (def->argc == macro_LINE) |
| 905 | { |
| 906 | saved_expansion = xstrprintf ("%d", line); |
| 907 | def->replacement = saved_expansion; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | return def; |
| 912 | } |
| 913 | |
| 914 | struct macro_definition * |
| 915 | macro_lookup_definition (struct macro_source_file *source, |
| 916 | int line, const char *name) |
| 917 | { |
| 918 | splay_tree_node n = find_definition (name, source, line); |
| 919 | |
| 920 | if (n) |
| 921 | return fixup_definition (source->filename, line, |
| 922 | (struct macro_definition *) n->value); |
| 923 | else |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | struct macro_source_file * |
| 929 | macro_definition_location (struct macro_source_file *source, |
| 930 | int line, |
| 931 | const char *name, |
| 932 | int *definition_line) |
| 933 | { |
| 934 | splay_tree_node n = find_definition (name, source, line); |
| 935 | |
| 936 | if (n) |
| 937 | { |
| 938 | struct macro_key *key = (struct macro_key *) n->key; |
| 939 | |
| 940 | *definition_line = key->start_line; |
| 941 | return key->start_file; |
| 942 | } |
| 943 | else |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | |
| 948 | /* The type for callback data for iterating the splay tree in |
| 949 | macro_for_each and macro_for_each_in_scope. Only the latter uses |
| 950 | the FILE and LINE fields. */ |
| 951 | struct macro_for_each_data |
| 952 | { |
| 953 | macro_callback_fn fn; |
| 954 | void *user_data; |
| 955 | struct macro_source_file *file; |
| 956 | int line; |
| 957 | }; |
| 958 | |
| 959 | /* Helper function for macro_for_each. */ |
| 960 | static int |
| 961 | foreach_macro (splay_tree_node node, void *arg) |
| 962 | { |
| 963 | struct macro_for_each_data *datum = (struct macro_for_each_data *) arg; |
| 964 | struct macro_key *key = (struct macro_key *) node->key; |
| 965 | struct macro_definition *def |
| 966 | = fixup_definition (key->start_file->filename, key->start_line, |
| 967 | (struct macro_definition *) node->value); |
| 968 | |
| 969 | (*datum->fn) (key->name, def, key->start_file, key->start_line, |
| 970 | datum->user_data); |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | /* Call FN for every macro in TABLE. */ |
| 975 | void |
| 976 | macro_for_each (struct macro_table *table, macro_callback_fn fn, |
| 977 | void *user_data) |
| 978 | { |
| 979 | struct macro_for_each_data datum; |
| 980 | |
| 981 | datum.fn = fn; |
| 982 | datum.user_data = user_data; |
| 983 | datum.file = NULL; |
| 984 | datum.line = 0; |
| 985 | splay_tree_foreach (table->definitions, foreach_macro, &datum); |
| 986 | } |
| 987 | |
| 988 | static int |
| 989 | foreach_macro_in_scope (splay_tree_node node, void *info) |
| 990 | { |
| 991 | struct macro_for_each_data *datum = (struct macro_for_each_data *) info; |
| 992 | struct macro_key *key = (struct macro_key *) node->key; |
| 993 | struct macro_definition *def |
| 994 | = fixup_definition (datum->file->filename, datum->line, |
| 995 | (struct macro_definition *) node->value); |
| 996 | |
| 997 | /* See if this macro is defined before the passed-in line, and |
| 998 | extends past that line. */ |
| 999 | if (compare_locations (key->start_file, key->start_line, |
| 1000 | datum->file, datum->line) < 0 |
| 1001 | && (!key->end_file |
| 1002 | || compare_locations (key->end_file, key->end_line, |
| 1003 | datum->file, datum->line) >= 0)) |
| 1004 | (*datum->fn) (key->name, def, key->start_file, key->start_line, |
| 1005 | datum->user_data); |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /* Call FN for every macro is visible in SCOPE. */ |
| 1010 | void |
| 1011 | macro_for_each_in_scope (struct macro_source_file *file, int line, |
| 1012 | macro_callback_fn fn, void *user_data) |
| 1013 | { |
| 1014 | struct macro_for_each_data datum; |
| 1015 | |
| 1016 | datum.fn = fn; |
| 1017 | datum.user_data = user_data; |
| 1018 | datum.file = file; |
| 1019 | datum.line = line; |
| 1020 | splay_tree_foreach (file->table->definitions, |
| 1021 | foreach_macro_in_scope, &datum); |
| 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | \f |
| 1026 | /* Creating and freeing macro tables. */ |
| 1027 | |
| 1028 | |
| 1029 | struct macro_table * |
| 1030 | new_macro_table (struct obstack *obstack, |
| 1031 | struct bcache *b) |
| 1032 | { |
| 1033 | struct macro_table *t; |
| 1034 | |
| 1035 | /* First, get storage for the `struct macro_table' itself. */ |
| 1036 | if (obstack) |
| 1037 | t = obstack_alloc (obstack, sizeof (*t)); |
| 1038 | else |
| 1039 | t = xmalloc (sizeof (*t)); |
| 1040 | |
| 1041 | memset (t, 0, sizeof (*t)); |
| 1042 | t->obstack = obstack; |
| 1043 | t->bcache = b; |
| 1044 | t->main_source = NULL; |
| 1045 | t->redef_ok = 0; |
| 1046 | t->definitions = (splay_tree_new_with_allocator |
| 1047 | (macro_tree_compare, |
| 1048 | ((splay_tree_delete_key_fn) macro_tree_delete_key), |
| 1049 | ((splay_tree_delete_value_fn) macro_tree_delete_value), |
| 1050 | ((splay_tree_allocate_fn) macro_alloc), |
| 1051 | ((splay_tree_deallocate_fn) macro_free), |
| 1052 | t)); |
| 1053 | |
| 1054 | return t; |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | void |
| 1059 | free_macro_table (struct macro_table *table) |
| 1060 | { |
| 1061 | /* Free the source file tree. */ |
| 1062 | free_macro_source_file (table->main_source); |
| 1063 | |
| 1064 | /* Free the table of macro definitions. */ |
| 1065 | splay_tree_delete (table->definitions); |
| 1066 | } |