gdb/macroexp.c - gdb

Data types defined

Functions defined

Source code

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

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "gdb_obstack.h"
  17. #include "bcache.h"
  18. #include "macrotab.h"
  19. #include "macroexp.h"
  20. #include "c-lang.h"



  21. /* A resizeable, substringable string type.  */


  22. /* A string type that we can resize, quickly append to, and use to
  23.    refer to substrings of other strings.  */
  24. struct macro_buffer
  25. {
  26.   /* An array of characters.  The first LEN bytes are the real text,
  27.      but there are SIZE bytes allocated to the array.  If SIZE is
  28.      zero, then this doesn't point to a malloc'ed block.  If SHARED is
  29.      non-zero, then this buffer is actually a pointer into some larger
  30.      string, and we shouldn't append characters to it, etc.  Because
  31.      of sharing, we can't assume in general that the text is
  32.      null-terminated.  */
  33.   char *text;

  34.   /* The number of characters in the string.  */
  35.   int len;

  36.   /* The number of characters allocated to the string.  If SHARED is
  37.      non-zero, this is meaningless; in this case, we set it to zero so
  38.      that any "do we have room to append something?" tests will fail,
  39.      so we don't always have to check SHARED before using this field.  */
  40.   int size;

  41.   /* Zero if TEXT can be safely realloc'ed (i.e., it's its own malloc
  42.      block).  Non-zero if TEXT is actually pointing into the middle of
  43.      some other block, and we shouldn't reallocate it.  */
  44.   int shared;

  45.   /* For detecting token splicing.

  46.      This is the index in TEXT of the first character of the token
  47.      that abuts the end of TEXT.  If TEXT contains no tokens, then we
  48.      set this equal to LEN.  If TEXT ends in whitespace, then there is
  49.      no token abutting the end of TEXT (it's just whitespace), and
  50.      again, we set this equal to LEN.  We set this to -1 if we don't
  51.      know the nature of TEXT.  */
  52.   int last_token;

  53.   /* If this buffer is holding the result from get_token, then this
  54.      is non-zero if it is an identifier token, zero otherwise.  */
  55.   int is_identifier;
  56. };


  57. /* Set the macro buffer *B to the empty string, guessing that its
  58.    final contents will fit in N bytes.  (It'll get resized if it
  59.    doesn't, so the guess doesn't have to be right.)  Allocate the
  60.    initial storage with xmalloc.  */
  61. static void
  62. init_buffer (struct macro_buffer *b, int n)
  63. {
  64.   b->size = n;
  65.   if (n > 0)
  66.     b->text = (char *) xmalloc (n);
  67.   else
  68.     b->text = NULL;
  69.   b->len = 0;
  70.   b->shared = 0;
  71.   b->last_token = -1;
  72. }


  73. /* Set the macro buffer *BUF to refer to the LEN bytes at ADDR, as a
  74.    shared substring.  */
  75. static void
  76. init_shared_buffer (struct macro_buffer *buf, char *addr, int len)
  77. {
  78.   buf->text = addr;
  79.   buf->len = len;
  80.   buf->shared = 1;
  81.   buf->size = 0;
  82.   buf->last_token = -1;
  83. }


  84. /* Free the text of the buffer B.  Raise an error if B is shared.  */
  85. static void
  86. free_buffer (struct macro_buffer *b)
  87. {
  88.   gdb_assert (! b->shared);
  89.   if (b->size)
  90.     xfree (b->text);
  91. }

  92. /* Like free_buffer, but return the text as an xstrdup()d string.
  93.    This only exists to try to make the API relatively clean.  */

  94. static char *
  95. free_buffer_return_text (struct macro_buffer *b)
  96. {
  97.   gdb_assert (! b->shared);
  98.   gdb_assert (b->size);
  99.   /* Nothing to do.  */
  100.   return b->text;
  101. }

  102. /* A cleanup function for macro buffers.  */
  103. static void
  104. cleanup_macro_buffer (void *untyped_buf)
  105. {
  106.   free_buffer ((struct macro_buffer *) untyped_buf);
  107. }


  108. /* Resize the buffer B to be at least N bytes long.  Raise an error if
  109.    B shouldn't be resized.  */
  110. static void
  111. resize_buffer (struct macro_buffer *b, int n)
  112. {
  113.   /* We shouldn't be trying to resize shared strings.  */
  114.   gdb_assert (! b->shared);

  115.   if (b->size == 0)
  116.     b->size = n;
  117.   else
  118.     while (b->size <= n)
  119.       b->size *= 2;

  120.   b->text = xrealloc (b->text, b->size);
  121. }


  122. /* Append the character C to the buffer B.  */
  123. static void
  124. appendc (struct macro_buffer *b, int c)
  125. {
  126.   int new_len = b->len + 1;

  127.   if (new_len > b->size)
  128.     resize_buffer (b, new_len);

  129.   b->text[b->len] = c;
  130.   b->len = new_len;
  131. }


  132. /* Append the LEN bytes at ADDR to the buffer B.  */
  133. static void
  134. appendmem (struct macro_buffer *b, char *addr, int len)
  135. {
  136.   int new_len = b->len + len;

  137.   if (new_len > b->size)
  138.     resize_buffer (b, new_len);

  139.   memcpy (b->text + b->len, addr, len);
  140.   b->len = new_len;
  141. }



  142. /* Recognizing preprocessor tokens.  */


  143. int
  144. macro_is_whitespace (int c)
  145. {
  146.   return (c == ' '
  147.           || c == '\t'
  148.           || c == '\n'
  149.           || c == '\v'
  150.           || c == '\f');
  151. }


  152. int
  153. macro_is_digit (int c)
  154. {
  155.   return ('0' <= c && c <= '9');
  156. }


  157. int
  158. macro_is_identifier_nondigit (int c)
  159. {
  160.   return (c == '_'
  161.           || ('a' <= c && c <= 'z')
  162.           || ('A' <= c && c <= 'Z'));
  163. }


  164. static void
  165. set_token (struct macro_buffer *tok, char *start, char *end)
  166. {
  167.   init_shared_buffer (tok, start, end - start);
  168.   tok->last_token = 0;

  169.   /* Presumed; get_identifier may overwrite this.  */
  170.   tok->is_identifier = 0;
  171. }


  172. static int
  173. get_comment (struct macro_buffer *tok, char *p, char *end)
  174. {
  175.   if (p + 2 > end)
  176.     return 0;
  177.   else if (p[0] == '/'
  178.            && p[1] == '*')
  179.     {
  180.       char *tok_start = p;

  181.       p += 2;

  182.       for (; p < end; p++)
  183.         if (p + 2 <= end
  184.             && p[0] == '*'
  185.             && p[1] == '/')
  186.           {
  187.             p += 2;
  188.             set_token (tok, tok_start, p);
  189.             return 1;
  190.           }

  191.       error (_("Unterminated comment in macro expansion."));
  192.     }
  193.   else if (p[0] == '/'
  194.            && p[1] == '/')
  195.     {
  196.       char *tok_start = p;

  197.       p += 2;
  198.       for (; p < end; p++)
  199.         if (*p == '\n')
  200.           break;

  201.       set_token (tok, tok_start, p);
  202.       return 1;
  203.     }
  204.   else
  205.     return 0;
  206. }


  207. static int
  208. get_identifier (struct macro_buffer *tok, char *p, char *end)
  209. {
  210.   if (p < end
  211.       && macro_is_identifier_nondigit (*p))
  212.     {
  213.       char *tok_start = p;

  214.       while (p < end
  215.              && (macro_is_identifier_nondigit (*p)
  216.                  || macro_is_digit (*p)))
  217.         p++;

  218.       set_token (tok, tok_start, p);
  219.       tok->is_identifier = 1;
  220.       return 1;
  221.     }
  222.   else
  223.     return 0;
  224. }


  225. static int
  226. get_pp_number (struct macro_buffer *tok, char *p, char *end)
  227. {
  228.   if (p < end
  229.       && (macro_is_digit (*p)
  230.           || (*p == '.'
  231.               && p + 2 <= end
  232.               && macro_is_digit (p[1]))))
  233.     {
  234.       char *tok_start = p;

  235.       while (p < end)
  236.         {
  237.           if (p + 2 <= end
  238.               && strchr ("eEpP", *p)
  239.               && (p[1] == '+' || p[1] == '-'))
  240.             p += 2;
  241.           else if (macro_is_digit (*p)
  242.                    || macro_is_identifier_nondigit (*p)
  243.                    || *p == '.')
  244.             p++;
  245.           else
  246.             break;
  247.         }

  248.       set_token (tok, tok_start, p);
  249.       return 1;
  250.     }
  251.   else
  252.     return 0;
  253. }



  254. /* If the text starting at P going up to (but not including) END
  255.    starts with a character constant, set *TOK to point to that
  256.    character constant, and return 1.  Otherwise, return zero.
  257.    Signal an error if it contains a malformed or incomplete character
  258.    constant.  */
  259. static int
  260. get_character_constant (struct macro_buffer *tok, char *p, char *end)
  261. {
  262.   /* ISO/IEC 9899:1999 (E)  Section 6.4.4.4  paragraph 1
  263.      But of course, what really matters is that we handle it the same
  264.      way GDB's C/C++ lexer does.  So we call parse_escape in utils.c
  265.      to handle escape sequences.  */
  266.   if ((p + 1 <= end && *p == '\'')
  267.       || (p + 2 <= end
  268.           && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
  269.           && p[1] == '\''))
  270.     {
  271.       char *tok_start = p;
  272.       int char_count = 0;

  273.       if (*p == '\'')
  274.         p++;
  275.       else if (*p == 'L' || *p == 'u' || *p == 'U')
  276.         p += 2;
  277.       else
  278.         gdb_assert_not_reached ("unexpected character constant");

  279.       for (;;)
  280.         {
  281.           if (p >= end)
  282.             error (_("Unmatched single quote."));
  283.           else if (*p == '\'')
  284.             {
  285.               if (!char_count)
  286.                 error (_("A character constant must contain at least one "
  287.                        "character."));
  288.               p++;
  289.               break;
  290.             }
  291.           else if (*p == '\\')
  292.             {
  293.               const char *s, *o;

  294.               s = o = ++p;
  295.               char_count += c_parse_escape (&s, NULL);
  296.               p += s - o;
  297.             }
  298.           else
  299.             {
  300.               p++;
  301.               char_count++;
  302.             }
  303.         }

  304.       set_token (tok, tok_start, p);
  305.       return 1;
  306.     }
  307.   else
  308.     return 0;
  309. }


  310. /* If the text starting at P going up to (but not including) END
  311.    starts with a string literal, set *TOK to point to that string
  312.    literal, and return 1.  Otherwise, return zero.  Signal an error if
  313.    it contains a malformed or incomplete string literal.  */
  314. static int
  315. get_string_literal (struct macro_buffer *tok, char *p, char *end)
  316. {
  317.   if ((p + 1 <= end
  318.        && *p == '"')
  319.       || (p + 2 <= end
  320.           && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
  321.           && p[1] == '"'))
  322.     {
  323.       char *tok_start = p;

  324.       if (*p == '"')
  325.         p++;
  326.       else if (*p == 'L' || *p == 'u' || *p == 'U')
  327.         p += 2;
  328.       else
  329.         gdb_assert_not_reached ("unexpected string literal");

  330.       for (;;)
  331.         {
  332.           if (p >= end)
  333.             error (_("Unterminated string in expression."));
  334.           else if (*p == '"')
  335.             {
  336.               p++;
  337.               break;
  338.             }
  339.           else if (*p == '\n')
  340.             error (_("Newline characters may not appear in string "
  341.                    "constants."));
  342.           else if (*p == '\\')
  343.             {
  344.               const char *s, *o;

  345.               s = o = ++p;
  346.               c_parse_escape (&s, NULL);
  347.               p += s - o;
  348.             }
  349.           else
  350.             p++;
  351.         }

  352.       set_token (tok, tok_start, p);
  353.       return 1;
  354.     }
  355.   else
  356.     return 0;
  357. }


  358. static int
  359. get_punctuator (struct macro_buffer *tok, char *p, char *end)
  360. {
  361.   /* Here, speed is much less important than correctness and clarity.  */

  362.   /* ISO/IEC 9899:1999 (E)  Section 6.4.6  Paragraph 1.
  363.      Note that this table is ordered in a special way.  A punctuator
  364.      which is a prefix of another punctuator must appear after its
  365.      "extension".  Otherwise, the wrong token will be returned.  */
  366.   static const char * const punctuators[] = {
  367.     "[", "]", "(", ")", "{", "}", "?", ";", ",", "~",
  368.     "...", ".",
  369.     "->", "--", "-=", "-",
  370.     "++", "+=", "+",
  371.     "*=", "*",
  372.     "!=", "!",
  373.     "&&", "&=", "&",
  374.     "/=", "/",
  375.     "%>", "%:%:", "%:", "%=", "%",
  376.     "^=", "^",
  377.     "##", "#",
  378.     ":>", ":",
  379.     "||", "|=", "|",
  380.     "<<=", "<<", "<=", "<:", "<%", "<",
  381.     ">>=", ">>", ">=", ">",
  382.     "==", "=",
  383.     0
  384.   };

  385.   int i;

  386.   if (p + 1 <= end)
  387.     {
  388.       for (i = 0; punctuators[i]; i++)
  389.         {
  390.           const char *punctuator = punctuators[i];

  391.           if (p[0] == punctuator[0])
  392.             {
  393.               int len = strlen (punctuator);

  394.               if (p + len <= end
  395.                   && ! memcmp (p, punctuator, len))
  396.                 {
  397.                   set_token (tok, p, p + len);
  398.                   return 1;
  399.                 }
  400.             }
  401.         }
  402.     }

  403.   return 0;
  404. }


  405. /* Peel the next preprocessor token off of SRC, and put it in TOK.
  406.    Mutate TOK to refer to the first token in SRC, and mutate SRC to
  407.    refer to the text after that token.  SRC must be a shared buffer;
  408.    the resulting TOK will be shared, pointing into the same string SRC
  409.    does.  Initialize TOK's last_token field.  Return non-zero if we
  410.    succeed, or 0 if we didn't find any more tokens in SRC.  */
  411. static int
  412. get_token (struct macro_buffer *tok,
  413.            struct macro_buffer *src)
  414. {
  415.   char *p = src->text;
  416.   char *end = p + src->len;

  417.   gdb_assert (src->shared);

  418.   /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4:

  419.      preprocessing-token:
  420.          header-name
  421.          identifier
  422.          pp-number
  423.          character-constant
  424.          string-literal
  425.          punctuator
  426.          each non-white-space character that cannot be one of the above

  427.      We don't have to deal with header-name tokens, since those can
  428.      only occur after a #include, which we will never see.  */

  429.   while (p < end)
  430.     if (macro_is_whitespace (*p))
  431.       p++;
  432.     else if (get_comment (tok, p, end))
  433.       p += tok->len;
  434.     else if (get_pp_number (tok, p, end)
  435.              || get_character_constant (tok, p, end)
  436.              || get_string_literal (tok, p, end)
  437.              /* Note: the grammar in the standard seems to be
  438.                 ambiguous: L'x' can be either a wide character
  439.                 constant, or an identifier followed by a normal
  440.                 character constant.  By trying `get_identifier' after
  441.                 we try get_character_constant and get_string_literal,
  442.                 we give the wide character syntax precedence.  Now,
  443.                 since GDB doesn't handle wide character constants
  444.                 anyway, is this the right thing to do?  */
  445.              || get_identifier (tok, p, end)
  446.              || get_punctuator (tok, p, end))
  447.       {
  448.         /* How many characters did we consume, including whitespace?  */
  449.         int consumed = p - src->text + tok->len;

  450.         src->text += consumed;
  451.         src->len -= consumed;
  452.         return 1;
  453.       }
  454.     else
  455.       {
  456.         /* We have found a "non-whitespace character that cannot be
  457.            one of the above."  Make a token out of it.  */
  458.         int consumed;

  459.         set_token (tok, p, p + 1);
  460.         consumed = p - src->text + tok->len;
  461.         src->text += consumed;
  462.         src->len -= consumed;
  463.         return 1;
  464.       }

  465.   return 0;
  466. }



  467. /* Appending token strings, with and without splicing  */


  468. /* Append the macro buffer SRC to the end of DEST, and ensure that
  469.    doing so doesn't splice the token at the end of SRC with the token
  470.    at the beginning of DEST.  SRC and DEST must have their last_token
  471.    fields set.  Upon return, DEST's last_token field is set correctly.

  472.    For example:

  473.    If DEST is "(" and SRC is "y", then we can return with
  474.    DEST set to "(y" --- we've simply appended the two buffers.

  475.    However, if DEST is "x" and SRC is "y", then we must not return
  476.    with DEST set to "xy" --- that would splice the two tokens "x" and
  477.    "y" together to make a single token "xy".  However, it would be
  478.    fine to return with DEST set to "x y".  Similarly, "<" and "<" must
  479.    yield "< <", not "<<", etc.  */
  480. static void
  481. append_tokens_without_splicing (struct macro_buffer *dest,
  482.                                 struct macro_buffer *src)
  483. {
  484.   int original_dest_len = dest->len;
  485.   struct macro_buffer dest_tail, new_token;

  486.   gdb_assert (src->last_token != -1);
  487.   gdb_assert (dest->last_token != -1);

  488.   /* First, just try appending the two, and call get_token to see if
  489.      we got a splice.  */
  490.   appendmem (dest, src->text, src->len);

  491.   /* If DEST originally had no token abutting its end, then we can't
  492.      have spliced anything, so we're done.  */
  493.   if (dest->last_token == original_dest_len)
  494.     {
  495.       dest->last_token = original_dest_len + src->last_token;
  496.       return;
  497.     }

  498.   /* Set DEST_TAIL to point to the last token in DEST, followed by
  499.      all the stuff we just appended.  */
  500.   init_shared_buffer (&dest_tail,
  501.                       dest->text + dest->last_token,
  502.                       dest->len - dest->last_token);

  503.   /* Re-parse DEST's last token.  We know that DEST used to contain
  504.      at least one token, so if it doesn't contain any after the
  505.      append, then we must have spliced "/" and "*" or "/" and "/" to
  506.      make a comment start.  (Just for the record, I got this right
  507.      the first time.  This is not a bug fix.)  */
  508.   if (get_token (&new_token, &dest_tail)
  509.       && (new_token.text + new_token.len
  510.           == dest->text + original_dest_len))
  511.     {
  512.       /* No splice, so we're done.  */
  513.       dest->last_token = original_dest_len + src->last_token;
  514.       return;
  515.     }

  516.   /* Okay, a simple append caused a splice.  Let's chop dest back to
  517.      its original length and try again, but separate the texts with a
  518.      space.  */
  519.   dest->len = original_dest_len;
  520.   appendc (dest, ' ');
  521.   appendmem (dest, src->text, src->len);

  522.   init_shared_buffer (&dest_tail,
  523.                       dest->text + dest->last_token,
  524.                       dest->len - dest->last_token);

  525.   /* Try to re-parse DEST's last token, as above.  */
  526.   if (get_token (&new_token, &dest_tail)
  527.       && (new_token.text + new_token.len
  528.           == dest->text + original_dest_len))
  529.     {
  530.       /* No splice, so we're done.  */
  531.       dest->last_token = original_dest_len + 1 + src->last_token;
  532.       return;
  533.     }

  534.   /* As far as I know, there's no case where inserting a space isn't
  535.      enough to prevent a splice.  */
  536.   internal_error (__FILE__, __LINE__,
  537.                   _("unable to avoid splicing tokens during macro expansion"));
  538. }

  539. /* Stringify an argument, and insert it into DEST.  ARG is the text to
  540.    stringify; it is LEN bytes long.  */

  541. static void
  542. stringify (struct macro_buffer *dest, const char *arg, int len)
  543. {
  544.   /* Trim initial whitespace from ARG.  */
  545.   while (len > 0 && macro_is_whitespace (*arg))
  546.     {
  547.       ++arg;
  548.       --len;
  549.     }

  550.   /* Trim trailing whitespace from ARG.  */
  551.   while (len > 0 && macro_is_whitespace (arg[len - 1]))
  552.     --len;

  553.   /* Insert the string.  */
  554.   appendc (dest, '"');
  555.   while (len > 0)
  556.     {
  557.       /* We could try to handle strange cases here, like control
  558.          characters, but there doesn't seem to be much point.  */
  559.       if (macro_is_whitespace (*arg))
  560.         {
  561.           /* Replace a sequence of whitespace with a single space.  */
  562.           appendc (dest, ' ');
  563.           while (len > 1 && macro_is_whitespace (arg[1]))
  564.             {
  565.               ++arg;
  566.               --len;
  567.             }
  568.         }
  569.       else if (*arg == '\\' || *arg == '"')
  570.         {
  571.           appendc (dest, '\\');
  572.           appendc (dest, *arg);
  573.         }
  574.       else
  575.         appendc (dest, *arg);
  576.       ++arg;
  577.       --len;
  578.     }
  579.   appendc (dest, '"');
  580.   dest->last_token = dest->len;
  581. }

  582. /* See macroexp.h.  */

  583. char *
  584. macro_stringify (const char *str)
  585. {
  586.   struct macro_buffer buffer;
  587.   int len = strlen (str);

  588.   init_buffer (&buffer, len);
  589.   stringify (&buffer, str, len);
  590.   appendc (&buffer, '\0');

  591.   return free_buffer_return_text (&buffer);
  592. }


  593. /* Expanding macros!  */


  594. /* A singly-linked list of the names of the macros we are currently
  595.    expanding --- for detecting expansion loops.  */
  596. struct macro_name_list {
  597.   const char *name;
  598.   struct macro_name_list *next;
  599. };


  600. /* Return non-zero if we are currently expanding the macro named NAME,
  601.    according to LIST; otherwise, return zero.

  602.    You know, it would be possible to get rid of all the NO_LOOP
  603.    arguments to these functions by simply generating a new lookup
  604.    function and baton which refuses to find the definition for a
  605.    particular macro, and otherwise delegates the decision to another
  606.    function/baton pair.  But that makes the linked list of excluded
  607.    macros chained through untyped baton pointers, which will make it
  608.    harder to debug.  :(  */
  609. static int
  610. currently_rescanning (struct macro_name_list *list, const char *name)
  611. {
  612.   for (; list; list = list->next)
  613.     if (strcmp (name, list->name) == 0)
  614.       return 1;

  615.   return 0;
  616. }


  617. /* Gather the arguments to a macro expansion.

  618.    NAME is the name of the macro being invoked.  (It's only used for
  619.    printing error messages.)

  620.    Assume that SRC is the text of the macro invocation immediately
  621.    following the macro name.  For example, if we're processing the
  622.    text foo(bar, baz), then NAME would be foo and SRC will be (bar,
  623.    baz).

  624.    If SRC doesn't start with an open paren ( token at all, return
  625.    zero, leave SRC unchanged, and don't set *ARGC_P to anything.

  626.    If SRC doesn't contain a properly terminated argument list, then
  627.    raise an error.

  628.    For a variadic macro, NARGS holds the number of formal arguments to
  629.    the macro.  For a GNU-style variadic macro, this should be the
  630.    number of named arguments.  For a non-variadic macro, NARGS should
  631.    be -1.

  632.    Otherwise, return a pointer to the first element of an array of
  633.    macro buffers referring to the argument texts, and set *ARGC_P to
  634.    the number of arguments we found --- the number of elements in the
  635.    array.  The macro buffers share their text with SRC, and their
  636.    last_token fields are initialized.  The array is allocated with
  637.    xmalloc, and the caller is responsible for freeing it.

  638.    NOTE WELL: if SRC starts with a open paren ( token followed
  639.    immediately by a close paren ) token (e.g., the invocation looks
  640.    like "foo()"), we treat that as one argument, which happens to be
  641.    the empty list of tokens.  The caller should keep in mind that such
  642.    a sequence of tokens is a valid way to invoke one-parameter
  643.    function-like macros, but also a valid way to invoke zero-parameter
  644.    function-like macros.  Eeew.

  645.    Consume the tokens from SRC; after this call, SRC contains the text
  646.    following the invocation.  */

  647. static struct macro_buffer *
  648. gather_arguments (const char *name, struct macro_buffer *src,
  649.                   int nargs, int *argc_p)
  650. {
  651.   struct macro_buffer tok;
  652.   int args_len, args_size;
  653.   struct macro_buffer *args = NULL;
  654.   struct cleanup *back_to = make_cleanup (free_current_contents, &args);

  655.   /* Does SRC start with an opening paren token?  Read from a copy of
  656.      SRC, so SRC itself is unaffected if we don't find an opening
  657.      paren.  */
  658.   {
  659.     struct macro_buffer temp;

  660.     init_shared_buffer (&temp, src->text, src->len);

  661.     if (! get_token (&tok, &temp)
  662.         || tok.len != 1
  663.         || tok.text[0] != '(')
  664.       {
  665.         discard_cleanups (back_to);
  666.         return 0;
  667.       }
  668.   }

  669.   /* Consume SRC's opening paren.  */
  670.   get_token (&tok, src);

  671.   args_len = 0;
  672.   args_size = 6;
  673.   args = (struct macro_buffer *) xmalloc (sizeof (*args) * args_size);

  674.   for (;;)
  675.     {
  676.       struct macro_buffer *arg;
  677.       int depth;

  678.       /* Make sure we have room for the next argument.  */
  679.       if (args_len >= args_size)
  680.         {
  681.           args_size *= 2;
  682.           args = xrealloc (args, sizeof (*args) * args_size);
  683.         }

  684.       /* Initialize the next argument.  */
  685.       arg = &args[args_len++];
  686.       set_token (arg, src->text, src->text);

  687.       /* Gather the argument's tokens.  */
  688.       depth = 0;
  689.       for (;;)
  690.         {
  691.           if (! get_token (&tok, src))
  692.             error (_("Malformed argument list for macro `%s'."), name);

  693.           /* Is tok an opening paren?  */
  694.           if (tok.len == 1 && tok.text[0] == '(')
  695.             depth++;

  696.           /* Is tok is a closing paren?  */
  697.           else if (tok.len == 1 && tok.text[0] == ')')
  698.             {
  699.               /* If it's a closing paren at the top level, then that's
  700.                  the end of the argument list.  */
  701.               if (depth == 0)
  702.                 {
  703.                   /* In the varargs case, the last argument may be
  704.                      missing.  Add an empty argument in this case.  */
  705.                   if (nargs != -1 && args_len == nargs - 1)
  706.                     {
  707.                       /* Make sure we have room for the argument.  */
  708.                       if (args_len >= args_size)
  709.                         {
  710.                           args_size++;
  711.                           args = xrealloc (args, sizeof (*args) * args_size);
  712.                         }
  713.                       arg = &args[args_len++];
  714.                       set_token (arg, src->text, src->text);
  715.                     }

  716.                   discard_cleanups (back_to);
  717.                   *argc_p = args_len;
  718.                   return args;
  719.                 }

  720.               depth--;
  721.             }

  722.           /* If tok is a comma at top level, then that's the end of
  723.              the current argument.  However, if we are handling a
  724.              variadic macro and we are computing the last argument, we
  725.              want to include the comma and remaining tokens.  */
  726.           else if (tok.len == 1 && tok.text[0] == ',' && depth == 0
  727.                    && (nargs == -1 || args_len < nargs))
  728.             break;

  729.           /* Extend the current argument to enclose this token.  If
  730.              this is the current argument's first token, leave out any
  731.              leading whitespace, just for aesthetics.  */
  732.           if (arg->len == 0)
  733.             {
  734.               arg->text = tok.text;
  735.               arg->len = tok.len;
  736.               arg->last_token = 0;
  737.             }
  738.           else
  739.             {
  740.               arg->len = (tok.text + tok.len) - arg->text;
  741.               arg->last_token = tok.text - arg->text;
  742.             }
  743.         }
  744.     }
  745. }


  746. /* The `expand' and `substitute_args' functions both invoke `scan'
  747.    recursively, so we need a forward declaration somewhere.  */
  748. static void scan (struct macro_buffer *dest,
  749.                   struct macro_buffer *src,
  750.                   struct macro_name_list *no_loop,
  751.                   macro_lookup_ftype *lookup_func,
  752.                   void *lookup_baton);


  753. /* A helper function for substitute_args.

  754.    ARGV is a vector of all the arguments; ARGC is the number of
  755.    arguments.  IS_VARARGS is true if the macro being substituted is a
  756.    varargs macro; in this case VA_ARG_NAME is the name of the
  757.    "variable" argument.  VA_ARG_NAME is ignored if IS_VARARGS is
  758.    false.

  759.    If the token TOK is the name of a parameter, return the parameter's
  760.    index.  If TOK is not an argument, return -1.  */

  761. static int
  762. find_parameter (const struct macro_buffer *tok,
  763.                 int is_varargs, const struct macro_buffer *va_arg_name,
  764.                 int argc, const char * const *argv)
  765. {
  766.   int i;

  767.   if (! tok->is_identifier)
  768.     return -1;

  769.   for (i = 0; i < argc; ++i)
  770.     if (tok->len == strlen (argv[i])
  771.         && !memcmp (tok->text, argv[i], tok->len))
  772.       return i;

  773.   if (is_varargs && tok->len == va_arg_name->len
  774.       && ! memcmp (tok->text, va_arg_name->text, tok->len))
  775.     return argc - 1;

  776.   return -1;
  777. }

  778. /* Given the macro definition DEF, being invoked with the actual
  779.    arguments given by ARGC and ARGV, substitute the arguments into the
  780.    replacement list, and store the result in DEST.

  781.    IS_VARARGS should be true if DEF is a varargs macro.  In this case,
  782.    VA_ARG_NAME should be the name of the "variable" argument -- either
  783.    __VA_ARGS__ for c99-style varargs, or the final argument name, for
  784.    GNU-style varargs.  If IS_VARARGS is false, this parameter is
  785.    ignored.

  786.    If it is necessary to expand macro invocations in one of the
  787.    arguments, use LOOKUP_FUNC and LOOKUP_BATON to find the macro
  788.    definitions, and don't expand invocations of the macros listed in
  789.    NO_LOOP.  */

  790. static void
  791. substitute_args (struct macro_buffer *dest,
  792.                  struct macro_definition *def,
  793.                  int is_varargs, const struct macro_buffer *va_arg_name,
  794.                  int argc, struct macro_buffer *argv,
  795.                  struct macro_name_list *no_loop,
  796.                  macro_lookup_ftype *lookup_func,
  797.                  void *lookup_baton)
  798. {
  799.   /* A macro buffer for the macro's replacement list.  */
  800.   struct macro_buffer replacement_list;
  801.   /* The token we are currently considering.  */
  802.   struct macro_buffer tok;
  803.   /* The replacement list's pointer from just before TOK was lexed.  */
  804.   char *original_rl_start;
  805.   /* We have a single lookahead token to handle token splicing.  */
  806.   struct macro_buffer lookahead;
  807.   /* The lookahead token might not be valid.  */
  808.   int lookahead_valid;
  809.   /* The replacement list's pointer from just before LOOKAHEAD was
  810.      lexed.  */
  811.   char *lookahead_rl_start;

  812.   init_shared_buffer (&replacement_list, (char *) def->replacement,
  813.                       strlen (def->replacement));

  814.   gdb_assert (dest->len == 0);
  815.   dest->last_token = 0;

  816.   original_rl_start = replacement_list.text;
  817.   if (! get_token (&tok, &replacement_list))
  818.     return;
  819.   lookahead_rl_start = replacement_list.text;
  820.   lookahead_valid = get_token (&lookahead, &replacement_list);

  821.   for (;;)
  822.     {
  823.       /* Just for aesthetics.  If we skipped some whitespace, copy
  824.          that to DEST.  */
  825.       if (tok.text > original_rl_start)
  826.         {
  827.           appendmem (dest, original_rl_start, tok.text - original_rl_start);
  828.           dest->last_token = dest->len;
  829.         }

  830.       /* Is this token the stringification operator?  */
  831.       if (tok.len == 1
  832.           && tok.text[0] == '#')
  833.         {
  834.           int arg;

  835.           if (!lookahead_valid)
  836.             error (_("Stringification operator requires an argument."));

  837.           arg = find_parameter (&lookahead, is_varargs, va_arg_name,
  838.                                 def->argc, def->argv);
  839.           if (arg == -1)
  840.             error (_("Argument to stringification operator must name "
  841.                      "a macro parameter."));

  842.           stringify (dest, argv[arg].text, argv[arg].len);

  843.           /* Read one token and let the loop iteration code handle the
  844.              rest.  */
  845.           lookahead_rl_start = replacement_list.text;
  846.           lookahead_valid = get_token (&lookahead, &replacement_list);
  847.         }
  848.       /* Is this token the splicing operator?  */
  849.       else if (tok.len == 2
  850.                && tok.text[0] == '#'
  851.                && tok.text[1] == '#')
  852.         error (_("Stray splicing operator"));
  853.       /* Is the next token the splicing operator?  */
  854.       else if (lookahead_valid
  855.                && lookahead.len == 2
  856.                && lookahead.text[0] == '#'
  857.                && lookahead.text[1] == '#')
  858.         {
  859.           int finished = 0;
  860.           int prev_was_comma = 0;

  861.           /* Note that GCC warns if the result of splicing is not a
  862.              token.  In the debugger there doesn't seem to be much
  863.              benefit from doing this.  */

  864.           /* Insert the first token.  */
  865.           if (tok.len == 1 && tok.text[0] == ',')
  866.             prev_was_comma = 1;
  867.           else
  868.             {
  869.               int arg = find_parameter (&tok, is_varargs, va_arg_name,
  870.                                         def->argc, def->argv);

  871.               if (arg != -1)
  872.                 appendmem (dest, argv[arg].text, argv[arg].len);
  873.               else
  874.                 appendmem (dest, tok.text, tok.len);
  875.             }

  876.           /* Apply a possible sequence of ## operators.  */
  877.           for (;;)
  878.             {
  879.               if (! get_token (&tok, &replacement_list))
  880.                 error (_("Splicing operator at end of macro"));

  881.               /* Handle a comma before a ##.  If we are handling
  882.                  varargs, and the token on the right hand side is the
  883.                  varargs marker, and the final argument is empty or
  884.                  missing, then drop the comma.  This is a GNU
  885.                  extension.  There is one ambiguous case here,
  886.                  involving pedantic behavior with an empty argument,
  887.                  but we settle that in favor of GNU-style (GCC uses an
  888.                  option).  If we aren't dealing with varargs, we
  889.                  simply insert the comma.  */
  890.               if (prev_was_comma)
  891.                 {
  892.                   if (! (is_varargs
  893.                          && tok.len == va_arg_name->len
  894.                          && !memcmp (tok.text, va_arg_name->text, tok.len)
  895.                          && argv[argc - 1].len == 0))
  896.                     appendmem (dest, ",", 1);
  897.                   prev_was_comma = 0;
  898.                 }

  899.               /* Insert the token.  If it is a parameter, insert the
  900.                  argument.  If it is a comma, treat it specially.  */
  901.               if (tok.len == 1 && tok.text[0] == ',')
  902.                 prev_was_comma = 1;
  903.               else
  904.                 {
  905.                   int arg = find_parameter (&tok, is_varargs, va_arg_name,
  906.                                             def->argc, def->argv);

  907.                   if (arg != -1)
  908.                     appendmem (dest, argv[arg].text, argv[arg].len);
  909.                   else
  910.                     appendmem (dest, tok.text, tok.len);
  911.                 }

  912.               /* Now read another token.  If it is another splice, we
  913.                  loop.  */
  914.               original_rl_start = replacement_list.text;
  915.               if (! get_token (&tok, &replacement_list))
  916.                 {
  917.                   finished = 1;
  918.                   break;
  919.                 }

  920.               if (! (tok.len == 2
  921.                      && tok.text[0] == '#'
  922.                      && tok.text[1] == '#'))
  923.                 break;
  924.             }

  925.           if (prev_was_comma)
  926.             {
  927.               /* We saw a comma.  Insert it now.  */
  928.               appendmem (dest, ",", 1);
  929.             }

  930.           dest->last_token = dest->len;
  931.           if (finished)
  932.             lookahead_valid = 0;
  933.           else
  934.             {
  935.               /* Set up for the loop iterator.  */
  936.               lookahead = tok;
  937.               lookahead_rl_start = original_rl_start;
  938.               lookahead_valid = 1;
  939.             }
  940.         }
  941.       else
  942.         {
  943.           /* Is this token an identifier?  */
  944.           int substituted = 0;
  945.           int arg = find_parameter (&tok, is_varargs, va_arg_name,
  946.                                     def->argc, def->argv);

  947.           if (arg != -1)
  948.             {
  949.               struct macro_buffer arg_src;

  950.               /* Expand any macro invocations in the argument text,
  951.                  and append the result to dest.  Remember that scan
  952.                  mutates its source, so we need to scan a new buffer
  953.                  referring to the argument's text, not the argument
  954.                  itself.  */
  955.               init_shared_buffer (&arg_src, argv[arg].text, argv[arg].len);
  956.               scan (dest, &arg_src, no_loop, lookup_func, lookup_baton);
  957.               substituted = 1;
  958.             }

  959.           /* If it wasn't a parameter, then just copy it across.  */
  960.           if (! substituted)
  961.             append_tokens_without_splicing (dest, &tok);
  962.         }

  963.       if (! lookahead_valid)
  964.         break;

  965.       tok = lookahead;
  966.       original_rl_start = lookahead_rl_start;

  967.       lookahead_rl_start = replacement_list.text;
  968.       lookahead_valid = get_token (&lookahead, &replacement_list);
  969.     }
  970. }


  971. /* Expand a call to a macro named ID, whose definition is DEF.  Append
  972.    its expansion to DEST.  SRC is the input text following the ID
  973.    token.  We are currently rescanning the expansions of the macros
  974.    named in NO_LOOP; don't re-expand them.  Use LOOKUP_FUNC and
  975.    LOOKUP_BATON to find definitions for any nested macro references.

  976.    Return 1 if we decided to expand it, zero otherwise.  (If it's a
  977.    function-like macro name that isn't followed by an argument list,
  978.    we don't expand it.)  If we return zero, leave SRC unchanged.  */
  979. static int
  980. expand (const char *id,
  981.         struct macro_definition *def,
  982.         struct macro_buffer *dest,
  983.         struct macro_buffer *src,
  984.         struct macro_name_list *no_loop,
  985.         macro_lookup_ftype *lookup_func,
  986.         void *lookup_baton)
  987. {
  988.   struct macro_name_list new_no_loop;

  989.   /* Create a new node to be added to the front of the no-expand list.
  990.      This list is appropriate for re-scanning replacement lists, but
  991.      it is *not* appropriate for scanning macro arguments; invocations
  992.      of the macro whose arguments we are gathering *do* get expanded
  993.      there.  */
  994.   new_no_loop.name = id;
  995.   new_no_loop.next = no_loop;

  996.   /* What kind of macro are we expanding?  */
  997.   if (def->kind == macro_object_like)
  998.     {
  999.       struct macro_buffer replacement_list;

  1000.       init_shared_buffer (&replacement_list, (char *) def->replacement,
  1001.                           strlen (def->replacement));

  1002.       scan (dest, &replacement_list, &new_no_loop, lookup_func, lookup_baton);
  1003.       return 1;
  1004.     }
  1005.   else if (def->kind == macro_function_like)
  1006.     {
  1007.       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
  1008.       int argc = 0;
  1009.       struct macro_buffer *argv = NULL;
  1010.       struct macro_buffer substituted;
  1011.       struct macro_buffer substituted_src;
  1012.       struct macro_buffer va_arg_name = {0};
  1013.       int is_varargs = 0;

  1014.       if (def->argc >= 1)
  1015.         {
  1016.           if (strcmp (def->argv[def->argc - 1], "...") == 0)
  1017.             {
  1018.               /* In C99-style varargs, substitution is done using
  1019.                  __VA_ARGS__.  */
  1020.               init_shared_buffer (&va_arg_name, "__VA_ARGS__",
  1021.                                   strlen ("__VA_ARGS__"));
  1022.               is_varargs = 1;
  1023.             }
  1024.           else
  1025.             {
  1026.               int len = strlen (def->argv[def->argc - 1]);

  1027.               if (len > 3
  1028.                   && strcmp (def->argv[def->argc - 1] + len - 3, "...") == 0)
  1029.                 {
  1030.                   /* In GNU-style varargs, the name of the
  1031.                      substitution parameter is the name of the formal
  1032.                      argument without the "...".  */
  1033.                   init_shared_buffer (&va_arg_name,
  1034.                                       (char *) def->argv[def->argc - 1],
  1035.                                       len - 3);
  1036.                   is_varargs = 1;
  1037.                 }
  1038.             }
  1039.         }

  1040.       make_cleanup (free_current_contents, &argv);
  1041.       argv = gather_arguments (id, src, is_varargs ? def->argc : -1,
  1042.                                &argc);

  1043.       /* If we couldn't find any argument list, then we don't expand
  1044.          this macro.  */
  1045.       if (! argv)
  1046.         {
  1047.           do_cleanups (back_to);
  1048.           return 0;
  1049.         }

  1050.       /* Check that we're passing an acceptable number of arguments for
  1051.          this macro.  */
  1052.       if (argc != def->argc)
  1053.         {
  1054.           if (is_varargs && argc >= def->argc - 1)
  1055.             {
  1056.               /* Ok.  */
  1057.             }
  1058.           /* Remember that a sequence of tokens like "foo()" is a
  1059.              valid invocation of a macro expecting either zero or one
  1060.              arguments.  */
  1061.           else if (! (argc == 1
  1062.                       && argv[0].len == 0
  1063.                       && def->argc == 0))
  1064.             error (_("Wrong number of arguments to macro `%s' "
  1065.                    "(expected %d, got %d)."),
  1066.                    id, def->argc, argc);
  1067.         }

  1068.       /* Note that we don't expand macro invocations in the arguments
  1069.          yet --- we let subst_args take care of that.  Parameters that
  1070.          appear as operands of the stringifying operator "#" or the
  1071.          splicing operator "##" don't get macro references expanded,
  1072.          so we can't really tell whether it's appropriate to macro-
  1073.          expand an argument until we see how it's being used.  */
  1074.       init_buffer (&substituted, 0);
  1075.       make_cleanup (cleanup_macro_buffer, &substituted);
  1076.       substitute_args (&substituted, def, is_varargs, &va_arg_name,
  1077.                        argc, argv, no_loop, lookup_func, lookup_baton);

  1078.       /* Now `substituted' is the macro's replacement list, with all
  1079.          argument values substituted into it properly.  Re-scan it for
  1080.          macro references, but don't expand invocations of this macro.

  1081.          We create a new buffer, `substituted_src', which points into
  1082.          `substituted', and scan that.  We can't scan `substituted'
  1083.          itself, since the tokenization process moves the buffer's
  1084.          text pointer around, and we still need to be able to find
  1085.          `substituted's original text buffer after scanning it so we
  1086.          can free it.  */
  1087.       init_shared_buffer (&substituted_src, substituted.text, substituted.len);
  1088.       scan (dest, &substituted_src, &new_no_loop, lookup_func, lookup_baton);

  1089.       do_cleanups (back_to);

  1090.       return 1;
  1091.     }
  1092.   else
  1093.     internal_error (__FILE__, __LINE__, _("bad macro definition kind"));
  1094. }


  1095. /* If the single token in SRC_FIRST followed by the tokens in SRC_REST
  1096.    constitute a macro invokation not forbidden in NO_LOOP, append its
  1097.    expansion to DEST and return non-zero.  Otherwise, return zero, and
  1098.    leave DEST unchanged.

  1099.    SRC_FIRST and SRC_REST must be shared buffers; DEST must not be one.
  1100.    SRC_FIRST must be a string built by get_token.  */
  1101. static int
  1102. maybe_expand (struct macro_buffer *dest,
  1103.               struct macro_buffer *src_first,
  1104.               struct macro_buffer *src_rest,
  1105.               struct macro_name_list *no_loop,
  1106.               macro_lookup_ftype *lookup_func,
  1107.               void *lookup_baton)
  1108. {
  1109.   gdb_assert (src_first->shared);
  1110.   gdb_assert (src_rest->shared);
  1111.   gdb_assert (! dest->shared);

  1112.   /* Is this token an identifier?  */
  1113.   if (src_first->is_identifier)
  1114.     {
  1115.       /* Make a null-terminated copy of it, since that's what our
  1116.          lookup function expects.  */
  1117.       char *id = xmalloc (src_first->len + 1);
  1118.       struct cleanup *back_to = make_cleanup (xfree, id);

  1119.       memcpy (id, src_first->text, src_first->len);
  1120.       id[src_first->len] = 0;

  1121.       /* If we're currently re-scanning the result of expanding
  1122.          this macro, don't expand it again.  */
  1123.       if (! currently_rescanning (no_loop, id))
  1124.         {
  1125.           /* Does this identifier have a macro definition in scope?  */
  1126.           struct macro_definition *def = lookup_func (id, lookup_baton);

  1127.           if (def && expand (id, def, dest, src_rest, no_loop,
  1128.                              lookup_func, lookup_baton))
  1129.             {
  1130.               do_cleanups (back_to);
  1131.               return 1;
  1132.             }
  1133.         }

  1134.       do_cleanups (back_to);
  1135.     }

  1136.   return 0;
  1137. }


  1138. /* Expand macro references in SRC, appending the results to DEST.
  1139.    Assume we are re-scanning the result of expanding the macros named
  1140.    in NO_LOOP, and don't try to re-expand references to them.

  1141.    SRC must be a shared buffer; DEST must not be one.  */
  1142. static void
  1143. scan (struct macro_buffer *dest,
  1144.       struct macro_buffer *src,
  1145.       struct macro_name_list *no_loop,
  1146.       macro_lookup_ftype *lookup_func,
  1147.       void *lookup_baton)
  1148. {
  1149.   gdb_assert (src->shared);
  1150.   gdb_assert (! dest->shared);

  1151.   for (;;)
  1152.     {
  1153.       struct macro_buffer tok;
  1154.       char *original_src_start = src->text;

  1155.       /* Find the next token in SRC.  */
  1156.       if (! get_token (&tok, src))
  1157.         break;

  1158.       /* Just for aesthetics.  If we skipped some whitespace, copy
  1159.          that to DEST.  */
  1160.       if (tok.text > original_src_start)
  1161.         {
  1162.           appendmem (dest, original_src_start, tok.text - original_src_start);
  1163.           dest->last_token = dest->len;
  1164.         }

  1165.       if (! maybe_expand (dest, &tok, src, no_loop, lookup_func, lookup_baton))
  1166.         /* We didn't end up expanding tok as a macro reference, so
  1167.            simply append it to dest.  */
  1168.         append_tokens_without_splicing (dest, &tok);
  1169.     }

  1170.   /* Just for aesthetics.  If there was any trailing whitespace in
  1171.      src, copy it to dest.  */
  1172.   if (src->len)
  1173.     {
  1174.       appendmem (dest, src->text, src->len);
  1175.       dest->last_token = dest->len;
  1176.     }
  1177. }


  1178. char *
  1179. macro_expand (const char *source,
  1180.               macro_lookup_ftype *lookup_func,
  1181.               void *lookup_func_baton)
  1182. {
  1183.   struct macro_buffer src, dest;
  1184.   struct cleanup *back_to;

  1185.   init_shared_buffer (&src, (char *) source, strlen (source));

  1186.   init_buffer (&dest, 0);
  1187.   dest.last_token = 0;
  1188.   back_to = make_cleanup (cleanup_macro_buffer, &dest);

  1189.   scan (&dest, &src, 0, lookup_func, lookup_func_baton);

  1190.   appendc (&dest, '\0');

  1191.   discard_cleanups (back_to);
  1192.   return dest.text;
  1193. }


  1194. char *
  1195. macro_expand_once (const char *source,
  1196.                    macro_lookup_ftype *lookup_func,
  1197.                    void *lookup_func_baton)
  1198. {
  1199.   error (_("Expand-once not implemented yet."));
  1200. }


  1201. char *
  1202. macro_expand_next (const char **lexptr,
  1203.                    macro_lookup_ftype *lookup_func,
  1204.                    void *lookup_baton)
  1205. {
  1206.   struct macro_buffer src, dest, tok;
  1207.   struct cleanup *back_to;

  1208.   /* Set up SRC to refer to the input text, pointed to by *lexptr.  */
  1209.   init_shared_buffer (&src, (char *) *lexptr, strlen (*lexptr));

  1210.   /* Set up DEST to receive the expansion, if there is one.  */
  1211.   init_buffer (&dest, 0);
  1212.   dest.last_token = 0;
  1213.   back_to = make_cleanup (cleanup_macro_buffer, &dest);

  1214.   /* Get the text's first preprocessing token.  */
  1215.   if (! get_token (&tok, &src))
  1216.     {
  1217.       do_cleanups (back_to);
  1218.       return 0;
  1219.     }

  1220.   /* If it's a macro invocation, expand it.  */
  1221.   if (maybe_expand (&dest, &tok, &src, 0, lookup_func, lookup_baton))
  1222.     {
  1223.       /* It was a macro invocation!  Package up the expansion as a
  1224.          null-terminated string and return it.  Set *lexptr to the
  1225.          start of the next token in the input.  */
  1226.       appendc (&dest, '\0');
  1227.       discard_cleanups (back_to);
  1228.       *lexptr = src.text;
  1229.       return dest.text;
  1230.     }
  1231.   else
  1232.     {
  1233.       /* It wasn't a macro invocation.  */
  1234.       do_cleanups (back_to);
  1235.       return 0;
  1236.     }
  1237. }