macrotab.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /* C preprocessor macro tables for GDB.
  2. Copyright (C) 2002-2022 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 "gdbsupport/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. gdb::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 t->bcache->insert (addr, len);
  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 (const char *) 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 = (struct macro_key *) 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
  317. = (struct macro_source_file *) macro_alloc (sizeof (*f), t);
  318. memset (f, 0, sizeof (*f));
  319. f->table = t;
  320. f->filename = macro_bcache_str (t, filename);
  321. f->includes = 0;
  322. return f;
  323. }
  324. /* Free a source file, and all the source files it #included. */
  325. static void
  326. free_macro_source_file (struct macro_source_file *src)
  327. {
  328. struct macro_source_file *child, *next_child;
  329. /* Free this file's children. */
  330. for (child = src->includes; child; child = next_child)
  331. {
  332. next_child = child->next_included;
  333. free_macro_source_file (child);
  334. }
  335. macro_bcache_free (src->table, (char *) src->filename);
  336. macro_free (src, src->table);
  337. }
  338. struct macro_source_file *
  339. macro_set_main (struct macro_table *t,
  340. const char *filename)
  341. {
  342. /* You can't change a table's main source file. What would that do
  343. to the tree? */
  344. gdb_assert (! t->main_source);
  345. t->main_source = new_source_file (t, filename);
  346. return t->main_source;
  347. }
  348. struct macro_source_file *
  349. macro_main (struct macro_table *t)
  350. {
  351. gdb_assert (t->main_source);
  352. return t->main_source;
  353. }
  354. void
  355. macro_allow_redefinitions (struct macro_table *t)
  356. {
  357. gdb_assert (! t->obstack);
  358. t->redef_ok = 1;
  359. }
  360. struct macro_source_file *
  361. macro_include (struct macro_source_file *source,
  362. int line,
  363. const char *included)
  364. {
  365. struct macro_source_file *newobj;
  366. struct macro_source_file **link;
  367. /* Find the right position in SOURCE's `includes' list for the new
  368. file. Skip inclusions at earlier lines, until we find one at the
  369. same line or later --- or until the end of the list. */
  370. for (link = &source->includes;
  371. *link && (*link)->included_at_line < line;
  372. link = &(*link)->next_included)
  373. ;
  374. /* Did we find another file already #included at the same line as
  375. the new one? */
  376. if (*link && line == (*link)->included_at_line)
  377. {
  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. std::string link_fullname = macro_source_fullname (*link);
  385. std::string source_fullname = macro_source_fullname (source);
  386. complaint (_("both `%s' and `%s' allegedly #included at %s:%d"),
  387. included, link_fullname.c_str (), source_fullname.c_str (),
  388. line);
  389. /* Now, choose a new, unoccupied line number for this
  390. #inclusion, after the alleged #inclusion line. */
  391. while (*link && line == (*link)->included_at_line)
  392. {
  393. /* This line number is taken, so try the next line. */
  394. line++;
  395. link = &(*link)->next_included;
  396. }
  397. }
  398. /* At this point, we know that LINE is an unused line number, and
  399. *LINK points to the entry an #inclusion at that line should
  400. precede. */
  401. newobj = new_source_file (source->table, included);
  402. newobj->included_by = source;
  403. newobj->included_at_line = line;
  404. newobj->next_included = *link;
  405. *link = newobj;
  406. return newobj;
  407. }
  408. struct macro_source_file *
  409. macro_lookup_inclusion (struct macro_source_file *source, const char *name)
  410. {
  411. /* Is SOURCE itself named NAME? */
  412. if (filename_cmp (name, source->filename) == 0)
  413. return source;
  414. /* It's not us. Try all our children, and return the lowest. */
  415. {
  416. struct macro_source_file *child;
  417. struct macro_source_file *best = NULL;
  418. int best_depth = 0;
  419. for (child = source->includes; child; child = child->next_included)
  420. {
  421. struct macro_source_file *result
  422. = macro_lookup_inclusion (child, name);
  423. if (result)
  424. {
  425. int result_depth = inclusion_depth (result);
  426. if (! best || result_depth < best_depth)
  427. {
  428. best = result;
  429. best_depth = result_depth;
  430. }
  431. }
  432. }
  433. return best;
  434. }
  435. }
  436. /* Registering and looking up macro definitions. */
  437. /* Construct a definition for a macro in table T. Cache all strings,
  438. and the macro_definition structure itself, in T's bcache. */
  439. static struct macro_definition *
  440. new_macro_definition (struct macro_table *t,
  441. enum macro_kind kind,
  442. int argc, const char **argv,
  443. const char *replacement)
  444. {
  445. struct macro_definition *d
  446. = (struct macro_definition *) macro_alloc (sizeof (*d), t);
  447. memset (d, 0, sizeof (*d));
  448. d->table = t;
  449. d->kind = kind;
  450. d->replacement = macro_bcache_str (t, replacement);
  451. d->argc = argc;
  452. if (kind == macro_function_like)
  453. {
  454. int i;
  455. const char **cached_argv;
  456. int cached_argv_size = argc * sizeof (*cached_argv);
  457. /* Bcache all the arguments. */
  458. cached_argv = (const char **) alloca (cached_argv_size);
  459. for (i = 0; i < argc; i++)
  460. cached_argv[i] = macro_bcache_str (t, argv[i]);
  461. /* Now bcache the array of argument pointers itself. */
  462. d->argv = ((const char * const *)
  463. 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. std::string source_fullname = macro_source_fullname (source);
  592. std::string found_key_fullname
  593. = macro_source_fullname (found_key->start_file);
  594. complaint (_("macro `%s' redefined at %s:%d; "
  595. "original definition at %s:%d"),
  596. name, source_fullname.c_str (), line,
  597. found_key_fullname.c_str (),
  598. found_key->start_line);
  599. }
  600. return found_key;
  601. }
  602. else
  603. return 0;
  604. }
  605. /* A helper function to define a new object-like or function-like macro
  606. according to KIND. When KIND is macro_object_like,
  607. the macro_special_kind must be provided as ARGC, and ARGV must be NULL.
  608. When KIND is macro_function_like, ARGC and ARGV are giving the function
  609. arguments. */
  610. static void
  611. macro_define_internal (struct macro_source_file *source, int line,
  612. const char *name, enum macro_kind kind,
  613. int argc, const char **argv,
  614. const char *replacement)
  615. {
  616. struct macro_table *t = source->table;
  617. struct macro_key *k = NULL;
  618. struct macro_definition *d;
  619. if (! t->redef_ok)
  620. k = check_for_redefinition (source, line,
  621. name, kind,
  622. argc, argv,
  623. replacement);
  624. /* If we're redefining a symbol, and the existing key would be
  625. identical to our new key, then the splay_tree_insert function
  626. will try to delete the old definition. When the definition is
  627. living on an obstack, this isn't a happy thing.
  628. Since this only happens in the presence of questionable debug
  629. info, we just ignore all definitions after the first. The only
  630. case I know of where this arises is in GCC's output for
  631. predefined macros, and all the definitions are the same in that
  632. case. */
  633. if (k && ! key_compare (k, name, source, line))
  634. return;
  635. k = new_macro_key (t, name, source, line);
  636. d = new_macro_definition (t, kind, argc, argv, replacement);
  637. splay_tree_insert (t->definitions, (splay_tree_key) k, (splay_tree_value) d);
  638. }
  639. /* A helper function to define a new object-like macro. */
  640. static void
  641. macro_define_object_internal (struct macro_source_file *source, int line,
  642. const char *name, const char *replacement,
  643. enum macro_special_kind special_kind)
  644. {
  645. macro_define_internal (source, line,
  646. name, macro_object_like,
  647. special_kind, NULL,
  648. replacement);
  649. }
  650. void
  651. macro_define_object (struct macro_source_file *source, int line,
  652. const char *name, const char *replacement)
  653. {
  654. macro_define_object_internal (source, line, name, replacement,
  655. macro_ordinary);
  656. }
  657. /* See macrotab.h. */
  658. void
  659. macro_define_special (struct macro_table *table)
  660. {
  661. macro_define_object_internal (table->main_source, -1, "__FILE__", "",
  662. macro_FILE);
  663. macro_define_object_internal (table->main_source, -1, "__LINE__", "",
  664. macro_LINE);
  665. }
  666. void
  667. macro_define_function (struct macro_source_file *source, int line,
  668. const char *name, int argc, const char **argv,
  669. const char *replacement)
  670. {
  671. macro_define_internal (source, line,
  672. name, macro_function_like,
  673. argc, argv,
  674. replacement);
  675. }
  676. void
  677. macro_undef (struct macro_source_file *source, int line,
  678. const char *name)
  679. {
  680. splay_tree_node n = find_definition (name, source, line);
  681. if (n)
  682. {
  683. struct macro_key *key = (struct macro_key *) n->key;
  684. /* If we're removing a definition at exactly the same point that
  685. we defined it, then just delete the entry altogether. GCC
  686. 4.1.2 will generate DWARF that says to do this if you pass it
  687. arguments like '-DFOO -UFOO -DFOO=2'. */
  688. if (source == key->start_file
  689. && line == key->start_line)
  690. splay_tree_remove (source->table->definitions, n->key);
  691. else
  692. {
  693. /* This function is the only place a macro's end-of-scope
  694. location gets set to anything other than "end of the
  695. compilation unit" (i.e., end_file is zero). So if this
  696. macro already has its end-of-scope set, then we're
  697. probably seeing a second #undefinition for the same
  698. #definition. */
  699. if (key->end_file)
  700. {
  701. std::string source_fullname = macro_source_fullname (source);
  702. std::string key_fullname = macro_source_fullname (key->end_file);
  703. complaint (_("macro '%s' is #undefined twice,"
  704. " at %s:%d and %s:%d"),
  705. name, source_fullname.c_str (), line,
  706. key_fullname.c_str (),
  707. key->end_line);
  708. }
  709. /* Whether or not we've seen a prior #undefinition, wipe out
  710. the old ending point, and make this the ending point. */
  711. key->end_file = source;
  712. key->end_line = line;
  713. }
  714. }
  715. else
  716. {
  717. /* According to the ISO C standard, an #undef for a symbol that
  718. has no macro definition in scope is ignored. So we should
  719. ignore it too. */
  720. #if 0
  721. complaint (_("no definition for macro `%s' in scope to #undef at %s:%d"),
  722. name, source->filename, line);
  723. #endif
  724. }
  725. }
  726. /* A helper function that rewrites the definition of a special macro,
  727. when needed. */
  728. static struct macro_definition *
  729. fixup_definition (const char *filename, int line, struct macro_definition *def)
  730. {
  731. static gdb::unique_xmalloc_ptr<char> saved_expansion;
  732. if (def->kind == macro_object_like)
  733. {
  734. if (def->argc == macro_FILE)
  735. {
  736. saved_expansion = macro_stringify (filename);
  737. def->replacement = saved_expansion.get ();
  738. }
  739. else if (def->argc == macro_LINE)
  740. {
  741. saved_expansion = xstrprintf ("%d", line);
  742. def->replacement = saved_expansion.get ();
  743. }
  744. }
  745. return def;
  746. }
  747. struct macro_definition *
  748. macro_lookup_definition (struct macro_source_file *source,
  749. int line, const char *name)
  750. {
  751. splay_tree_node n = find_definition (name, source, line);
  752. if (n)
  753. {
  754. std::string source_fullname = macro_source_fullname (source);
  755. return fixup_definition (source_fullname.c_str (), line,
  756. (struct macro_definition *) n->value);
  757. }
  758. else
  759. return 0;
  760. }
  761. struct macro_source_file *
  762. macro_definition_location (struct macro_source_file *source,
  763. int line,
  764. const char *name,
  765. int *definition_line)
  766. {
  767. splay_tree_node n = find_definition (name, source, line);
  768. if (n)
  769. {
  770. struct macro_key *key = (struct macro_key *) n->key;
  771. *definition_line = key->start_line;
  772. return key->start_file;
  773. }
  774. else
  775. return 0;
  776. }
  777. /* The type for callback data for iterating the splay tree in
  778. macro_for_each and macro_for_each_in_scope. Only the latter uses
  779. the FILE and LINE fields. */
  780. struct macro_for_each_data
  781. {
  782. gdb::function_view<macro_callback_fn> fn;
  783. struct macro_source_file *file;
  784. int line;
  785. };
  786. /* Helper function for macro_for_each. */
  787. static int
  788. foreach_macro (splay_tree_node node, void *arg)
  789. {
  790. struct macro_for_each_data *datum = (struct macro_for_each_data *) arg;
  791. struct macro_key *key = (struct macro_key *) node->key;
  792. struct macro_definition *def;
  793. std::string key_fullname = macro_source_fullname (key->start_file);
  794. def = fixup_definition (key_fullname.c_str (), key->start_line,
  795. (struct macro_definition *) node->value);
  796. datum->fn (key->name, def, key->start_file, key->start_line);
  797. return 0;
  798. }
  799. /* Call FN for every macro in TABLE. */
  800. void
  801. macro_for_each (struct macro_table *table,
  802. gdb::function_view<macro_callback_fn> fn)
  803. {
  804. struct macro_for_each_data datum;
  805. datum.fn = fn;
  806. datum.file = NULL;
  807. datum.line = 0;
  808. splay_tree_foreach (table->definitions, foreach_macro, &datum);
  809. }
  810. static int
  811. foreach_macro_in_scope (splay_tree_node node, void *info)
  812. {
  813. struct macro_for_each_data *datum = (struct macro_for_each_data *) info;
  814. struct macro_key *key = (struct macro_key *) node->key;
  815. struct macro_definition *def;
  816. std::string datum_fullname = macro_source_fullname (datum->file);
  817. def = fixup_definition (datum_fullname.c_str (), datum->line,
  818. (struct macro_definition *) node->value);
  819. /* See if this macro is defined before the passed-in line, and
  820. extends past that line. */
  821. if (compare_locations (key->start_file, key->start_line,
  822. datum->file, datum->line) < 0
  823. && (!key->end_file
  824. || compare_locations (key->end_file, key->end_line,
  825. datum->file, datum->line) >= 0))
  826. datum->fn (key->name, def, key->start_file, key->start_line);
  827. return 0;
  828. }
  829. /* Call FN for every macro is visible in SCOPE. */
  830. void
  831. macro_for_each_in_scope (struct macro_source_file *file, int line,
  832. gdb::function_view<macro_callback_fn> fn)
  833. {
  834. struct macro_for_each_data datum;
  835. datum.fn = fn;
  836. datum.file = file;
  837. datum.line = line;
  838. splay_tree_foreach (file->table->definitions,
  839. foreach_macro_in_scope, &datum);
  840. }
  841. /* Creating and freeing macro tables. */
  842. struct macro_table *
  843. new_macro_table (struct obstack *obstack, gdb::bcache *b,
  844. struct compunit_symtab *cust)
  845. {
  846. struct macro_table *t;
  847. /* First, get storage for the `struct macro_table' itself. */
  848. if (obstack)
  849. t = XOBNEW (obstack, struct macro_table);
  850. else
  851. t = XNEW (struct macro_table);
  852. memset (t, 0, sizeof (*t));
  853. t->obstack = obstack;
  854. t->bcache = b;
  855. t->main_source = NULL;
  856. t->compunit_symtab = cust;
  857. t->redef_ok = 0;
  858. t->definitions = (splay_tree_new_with_allocator
  859. (macro_tree_compare,
  860. ((splay_tree_delete_key_fn) macro_tree_delete_key),
  861. ((splay_tree_delete_value_fn) macro_tree_delete_value),
  862. ((splay_tree_allocate_fn) macro_alloc),
  863. ((splay_tree_deallocate_fn) macro_free),
  864. t));
  865. return t;
  866. }
  867. void
  868. free_macro_table (struct macro_table *table)
  869. {
  870. /* Free the source file tree. */
  871. free_macro_source_file (table->main_source);
  872. /* Free the table of macro definitions. */
  873. splay_tree_delete (table->definitions);
  874. }
  875. /* See macrotab.h for the comment. */
  876. std::string
  877. macro_source_fullname (struct macro_source_file *file)
  878. {
  879. const char *comp_dir = NULL;
  880. if (file->table->compunit_symtab != NULL)
  881. comp_dir = file->table->compunit_symtab->dirname ();
  882. if (comp_dir == NULL || IS_ABSOLUTE_PATH (file->filename))
  883. return file->filename;
  884. return std::string (comp_dir) + SLASH_STRING + file->filename;
  885. }