Home | History | Annotate | Line # | Download | only in gdb
macroexp.c revision 1.10
      1 /* C preprocessor macro expansion for GDB.
      2    Copyright (C) 2002-2023 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 "gdbsupport/gdb_obstack.h"
     22 #include "macrotab.h"
     23 #include "macroexp.h"
     24 #include "macroscope.h"
     25 #include "c-lang.h"
     26 
     27 
     28 
     29 
     31 /* A string type that we can use to refer to substrings of other
     32    strings.  */
     33 
     34 struct shared_macro_buffer
     35 {
     36   /* An array of characters.  This buffer is a pointer into some
     37      larger string and thus we can't assume in that the text is
     38      null-terminated.  */
     39   const char *text;
     40 
     41   /* The number of characters in the string.  */
     42   int len;
     43 
     44   /* For detecting token splicing.
     45 
     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 = -1;
     53 
     54   /* If this buffer is holding the result from get_token, then this
     55      is non-zero if it is an identifier token, zero otherwise.  */
     56   int is_identifier = 0;
     57 
     58   shared_macro_buffer ()
     59     : text (NULL),
     60       len (0)
     61   {
     62   }
     63 
     64   /* Set the macro buffer to refer to the LEN bytes at ADDR, as a
     65      shared substring.  */
     66   shared_macro_buffer (const char *addr, int len)
     67   {
     68     set_shared (addr, len);
     69   }
     70 
     71   /* Set the macro buffer to refer to the LEN bytes at ADDR, as a
     72      shared substring.  */
     73   void set_shared (const char *addr, int len_)
     74   {
     75     text = addr;
     76     len = len_;
     77   }
     78 };
     79 
     80 /* A string type that we can resize and quickly append to.  */
     81 
     82 struct growable_macro_buffer
     83 {
     84   /* An array of characters.  The first LEN bytes are the real text,
     85      but there are SIZE bytes allocated to the array.  */
     86   char *text;
     87 
     88   /* The number of characters in the string.  */
     89   int len;
     90 
     91   /* The number of characters allocated to the string.  */
     92   int size;
     93 
     94   /* For detecting token splicing.
     95 
     96      This is the index in TEXT of the first character of the token
     97      that abuts the end of TEXT.  If TEXT contains no tokens, then we
     98      set this equal to LEN.  If TEXT ends in whitespace, then there is
     99      no token abutting the end of TEXT (it's just whitespace), and
    100      again, we set this equal to LEN.  We set this to -1 if we don't
    101      know the nature of TEXT.  */
    102   int last_token = -1;
    103 
    104   /* Set the macro buffer to the empty string, guessing that its
    105      final contents will fit in N bytes.  (It'll get resized if it
    106      doesn't, so the guess doesn't have to be right.)  Allocate the
    107      initial storage with xmalloc.  */
    108   explicit growable_macro_buffer (int n)
    109     : len (0),
    110       size (n)
    111   {
    112     if (n > 0)
    113       text = (char *) xmalloc (n);
    114     else
    115       text = NULL;
    116   }
    117 
    118   DISABLE_COPY_AND_ASSIGN (growable_macro_buffer);
    119 
    120   ~growable_macro_buffer ()
    121   {
    122     xfree (text);
    123   }
    124 
    125   /* Release the text of the buffer to the caller.  */
    126   gdb::unique_xmalloc_ptr<char> release ()
    127   {
    128     gdb_assert (size);
    129     char *result = text;
    130     text = NULL;
    131     return gdb::unique_xmalloc_ptr<char> (result);
    132   }
    133 
    134   /* Resize the buffer to be at least N bytes long.  */
    135   void resize_buffer (int n)
    136   {
    137     if (size == 0)
    138       size = n;
    139     else
    140       while (size <= n)
    141 	size *= 2;
    142 
    143     text = (char *) xrealloc (text, size);
    144   }
    145 
    146   /* Append the character C to the buffer.  */
    147   void appendc (int c)
    148   {
    149     int new_len = len + 1;
    150 
    151     if (new_len > size)
    152       resize_buffer (new_len);
    153 
    154     text[len] = c;
    155     len = new_len;
    156   }
    157 
    158   /* Append the COUNT bytes at ADDR to the buffer.  */
    159   void appendmem (const char *addr, int count)
    160   {
    161     int new_len = len + count;
    162 
    163     if (new_len > size)
    164       resize_buffer (new_len);
    165 
    166     memcpy (text + len, addr, count);
    167     len = new_len;
    168   }
    169 };
    170 
    171 
    172 
    173 /* Recognizing preprocessor tokens.  */
    175 
    176 
    177 int
    178 macro_is_whitespace (int c)
    179 {
    180   return (c == ' '
    181 	  || c == '\t'
    182 	  || c == '\n'
    183 	  || c == '\v'
    184 	  || c == '\f');
    185 }
    186 
    187 
    188 int
    189 macro_is_digit (int c)
    190 {
    191   return ('0' <= c && c <= '9');
    192 }
    193 
    194 
    195 int
    196 macro_is_identifier_nondigit (int c)
    197 {
    198   return (c == '_'
    199 	  || ('a' <= c && c <= 'z')
    200 	  || ('A' <= c && c <= 'Z'));
    201 }
    202 
    203 
    204 static void
    205 set_token (shared_macro_buffer *tok, const char *start, const char *end)
    206 {
    207   tok->set_shared (start, end - start);
    208   tok->last_token = 0;
    209 
    210   /* Presumed; get_identifier may overwrite this.  */
    211   tok->is_identifier = 0;
    212 }
    213 
    214 
    215 static int
    216 get_comment (shared_macro_buffer *tok, const char *p, const char *end)
    217 {
    218   if (p + 2 > end)
    219     return 0;
    220   else if (p[0] == '/'
    221 	   && p[1] == '*')
    222     {
    223       const char *tok_start = p;
    224 
    225       p += 2;
    226 
    227       for (; p < end; p++)
    228 	if (p + 2 <= end
    229 	    && p[0] == '*'
    230 	    && p[1] == '/')
    231 	  {
    232 	    p += 2;
    233 	    set_token (tok, tok_start, p);
    234 	    return 1;
    235 	  }
    236 
    237       error (_("Unterminated comment in macro expansion."));
    238     }
    239   else if (p[0] == '/'
    240 	   && p[1] == '/')
    241     {
    242       const char *tok_start = p;
    243 
    244       p += 2;
    245       for (; p < end; p++)
    246 	if (*p == '\n')
    247 	  break;
    248 
    249       set_token (tok, tok_start, p);
    250       return 1;
    251     }
    252   else
    253     return 0;
    254 }
    255 
    256 
    257 static int
    258 get_identifier (shared_macro_buffer *tok, const char *p, const char *end)
    259 {
    260   if (p < end
    261       && macro_is_identifier_nondigit (*p))
    262     {
    263       const char *tok_start = p;
    264 
    265       while (p < end
    266 	     && (macro_is_identifier_nondigit (*p)
    267 		 || macro_is_digit (*p)))
    268 	p++;
    269 
    270       set_token (tok, tok_start, p);
    271       tok->is_identifier = 1;
    272       return 1;
    273     }
    274   else
    275     return 0;
    276 }
    277 
    278 
    279 static int
    280 get_pp_number (shared_macro_buffer *tok, const char *p, const char *end)
    281 {
    282   if (p < end
    283       && (macro_is_digit (*p)
    284 	  || (*p == '.'
    285 	      && p + 2 <= end
    286 	      && macro_is_digit (p[1]))))
    287     {
    288       const char *tok_start = p;
    289 
    290       while (p < end)
    291 	{
    292 	  if (p + 2 <= end
    293 	      && strchr ("eEpP", *p)
    294 	      && (p[1] == '+' || p[1] == '-'))
    295 	    p += 2;
    296 	  else if (macro_is_digit (*p)
    297 		   || macro_is_identifier_nondigit (*p)
    298 		   || *p == '.')
    299 	    p++;
    300 	  else
    301 	    break;
    302 	}
    303 
    304       set_token (tok, tok_start, p);
    305       return 1;
    306     }
    307   else
    308     return 0;
    309 }
    310 
    311 
    312 
    313 /* If the text starting at P going up to (but not including) END
    314    starts with a character constant, set *TOK to point to that
    315    character constant, and return 1.  Otherwise, return zero.
    316    Signal an error if it contains a malformed or incomplete character
    317    constant.  */
    318 static int
    319 get_character_constant (shared_macro_buffer *tok,
    320 			const char *p, const char *end)
    321 {
    322   /* ISO/IEC 9899:1999 (E)  Section 6.4.4.4  paragraph 1
    323      But of course, what really matters is that we handle it the same
    324      way GDB's C/C++ lexer does.  So we call parse_escape in utils.c
    325      to handle escape sequences.  */
    326   if ((p + 1 <= end && *p == '\'')
    327       || (p + 2 <= end
    328 	  && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
    329 	  && p[1] == '\''))
    330     {
    331       const char *tok_start = p;
    332       int char_count = 0;
    333 
    334       if (*p == '\'')
    335 	p++;
    336       else if (*p == 'L' || *p == 'u' || *p == 'U')
    337 	p += 2;
    338       else
    339 	gdb_assert_not_reached ("unexpected character constant");
    340 
    341       for (;;)
    342 	{
    343 	  if (p >= end)
    344 	    error (_("Unmatched single quote."));
    345 	  else if (*p == '\'')
    346 	    {
    347 	      if (!char_count)
    348 		error (_("A character constant must contain at least one "
    349 		       "character."));
    350 	      p++;
    351 	      break;
    352 	    }
    353 	  else if (*p == '\\')
    354 	    {
    355 	      const char *s, *o;
    356 
    357 	      s = o = ++p;
    358 	      char_count += c_parse_escape (&s, NULL);
    359 	      p += s - o;
    360 	    }
    361 	  else
    362 	    {
    363 	      p++;
    364 	      char_count++;
    365 	    }
    366 	}
    367 
    368       set_token (tok, tok_start, p);
    369       return 1;
    370     }
    371   else
    372     return 0;
    373 }
    374 
    375 
    376 /* If the text starting at P going up to (but not including) END
    377    starts with a string literal, set *TOK to point to that string
    378    literal, and return 1.  Otherwise, return zero.  Signal an error if
    379    it contains a malformed or incomplete string literal.  */
    380 static int
    381 get_string_literal (shared_macro_buffer *tok, const char *p, const char *end)
    382 {
    383   if ((p + 1 <= end
    384        && *p == '"')
    385       || (p + 2 <= end
    386 	  && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
    387 	  && p[1] == '"'))
    388     {
    389       const char *tok_start = p;
    390 
    391       if (*p == '"')
    392 	p++;
    393       else if (*p == 'L' || *p == 'u' || *p == 'U')
    394 	p += 2;
    395       else
    396 	gdb_assert_not_reached ("unexpected string literal");
    397 
    398       for (;;)
    399 	{
    400 	  if (p >= end)
    401 	    error (_("Unterminated string in expression."));
    402 	  else if (*p == '"')
    403 	    {
    404 	      p++;
    405 	      break;
    406 	    }
    407 	  else if (*p == '\n')
    408 	    error (_("Newline characters may not appear in string "
    409 		   "constants."));
    410 	  else if (*p == '\\')
    411 	    {
    412 	      const char *s, *o;
    413 
    414 	      s = o = ++p;
    415 	      c_parse_escape (&s, NULL);
    416 	      p += s - o;
    417 	    }
    418 	  else
    419 	    p++;
    420 	}
    421 
    422       set_token (tok, tok_start, p);
    423       return 1;
    424     }
    425   else
    426     return 0;
    427 }
    428 
    429 
    430 static int
    431 get_punctuator (shared_macro_buffer *tok, const char *p, const char *end)
    432 {
    433   /* Here, speed is much less important than correctness and clarity.  */
    434 
    435   /* ISO/IEC 9899:1999 (E)  Section 6.4.6  Paragraph 1.
    436      Note that this table is ordered in a special way.  A punctuator
    437      which is a prefix of another punctuator must appear after its
    438      "extension".  Otherwise, the wrong token will be returned.  */
    439   static const char * const punctuators[] = {
    440     "[", "]", "(", ")", "{", "}", "?", ";", ",", "~",
    441     "...", ".",
    442     "->", "--", "-=", "-",
    443     "++", "+=", "+",
    444     "*=", "*",
    445     "!=", "!",
    446     "&&", "&=", "&",
    447     "/=", "/",
    448     "%>", "%:%:", "%:", "%=", "%",
    449     "^=", "^",
    450     "##", "#",
    451     ":>", ":",
    452     "||", "|=", "|",
    453     "<<=", "<<", "<=", "<:", "<%", "<",
    454     ">>=", ">>", ">=", ">",
    455     "==", "=",
    456     0
    457   };
    458 
    459   int i;
    460 
    461   if (p + 1 <= end)
    462     {
    463       for (i = 0; punctuators[i]; i++)
    464 	{
    465 	  const char *punctuator = punctuators[i];
    466 
    467 	  if (p[0] == punctuator[0])
    468 	    {
    469 	      int len = strlen (punctuator);
    470 
    471 	      if (p + len <= end
    472 		  && ! memcmp (p, punctuator, len))
    473 		{
    474 		  set_token (tok, p, p + len);
    475 		  return 1;
    476 		}
    477 	    }
    478 	}
    479     }
    480 
    481   return 0;
    482 }
    483 
    484 
    485 /* Peel the next preprocessor token off of SRC, and put it in TOK.
    486    Mutate TOK to refer to the first token in SRC, and mutate SRC to
    487    refer to the text after that token.  The resulting TOK will point
    488    into the same string SRC does.  Initialize TOK's last_token field.
    489    Return non-zero if we succeed, or 0 if we didn't find any more
    490    tokens in SRC.  */
    491 
    492 static int
    493 get_token (shared_macro_buffer *tok, shared_macro_buffer *src)
    494 {
    495   const char *p = src->text;
    496   const char *end = p + src->len;
    497 
    498   /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4:
    499 
    500      preprocessing-token:
    501 	 header-name
    502 	 identifier
    503 	 pp-number
    504 	 character-constant
    505 	 string-literal
    506 	 punctuator
    507 	 each non-white-space character that cannot be one of the above
    508 
    509      We don't have to deal with header-name tokens, since those can
    510      only occur after a #include, which we will never see.  */
    511 
    512   while (p < end)
    513     if (macro_is_whitespace (*p))
    514       p++;
    515     else if (get_comment (tok, p, end))
    516       p += tok->len;
    517     else if (get_pp_number (tok, p, end)
    518 	     || get_character_constant (tok, p, end)
    519 	     || get_string_literal (tok, p, end)
    520 	     /* Note: the grammar in the standard seems to be
    521 		ambiguous: L'x' can be either a wide character
    522 		constant, or an identifier followed by a normal
    523 		character constant.  By trying `get_identifier' after
    524 		we try get_character_constant and get_string_literal,
    525 		we give the wide character syntax precedence.  Now,
    526 		since GDB doesn't handle wide character constants
    527 		anyway, is this the right thing to do?  */
    528 	     || get_identifier (tok, p, end)
    529 	     || get_punctuator (tok, p, end))
    530       {
    531 	/* How many characters did we consume, including whitespace?  */
    532 	int consumed = p - src->text + tok->len;
    533 
    534 	src->text += consumed;
    535 	src->len -= consumed;
    536 	return 1;
    537       }
    538     else
    539       {
    540 	/* We have found a "non-whitespace character that cannot be
    541 	   one of the above."  Make a token out of it.  */
    542 	int consumed;
    543 
    544 	set_token (tok, p, p + 1);
    545 	consumed = p - src->text + tok->len;
    546 	src->text += consumed;
    547 	src->len -= consumed;
    548 	return 1;
    549       }
    550 
    551   return 0;
    552 }
    553 
    554 
    555 
    556 /* Appending token strings, with and without splicing  */
    558 
    559 
    560 /* Append the macro buffer SRC to the end of DEST, and ensure that
    561    doing so doesn't splice the token at the end of SRC with the token
    562    at the beginning of DEST.  SRC and DEST must have their last_token
    563    fields set.  Upon return, DEST's last_token field is set correctly.
    564 
    565    For example:
    566 
    567    If DEST is "(" and SRC is "y", then we can return with
    568    DEST set to "(y" --- we've simply appended the two buffers.
    569 
    570    However, if DEST is "x" and SRC is "y", then we must not return
    571    with DEST set to "xy" --- that would splice the two tokens "x" and
    572    "y" together to make a single token "xy".  However, it would be
    573    fine to return with DEST set to "x y".  Similarly, "<" and "<" must
    574    yield "< <", not "<<", etc.  */
    575 static void
    576 append_tokens_without_splicing (growable_macro_buffer *dest,
    577                                 shared_macro_buffer *src)
    578 {
    579   int original_dest_len = dest->len;
    580   shared_macro_buffer dest_tail, new_token;
    581 
    582   gdb_assert (src->last_token != -1);
    583   gdb_assert (dest->last_token != -1);
    584 
    585   /* First, just try appending the two, and call get_token to see if
    586      we got a splice.  */
    587   dest->appendmem (src->text, src->len);
    588 
    589   /* If DEST originally had no token abutting its end, then we can't
    590      have spliced anything, so we're done.  */
    591   if (dest->last_token == original_dest_len)
    592     {
    593       dest->last_token = original_dest_len + src->last_token;
    594       return;
    595     }
    596 
    597   /* Set DEST_TAIL to point to the last token in DEST, followed by
    598      all the stuff we just appended.  */
    599   dest_tail.set_shared (dest->text + dest->last_token,
    600 			dest->len - dest->last_token);
    601 
    602   /* Re-parse DEST's last token.  We know that DEST used to contain
    603      at least one token, so if it doesn't contain any after the
    604      append, then we must have spliced "/" and "*" or "/" and "/" to
    605      make a comment start.  (Just for the record, I got this right
    606      the first time.  This is not a bug fix.)  */
    607   if (get_token (&new_token, &dest_tail)
    608       && (new_token.text + new_token.len
    609 	  == dest->text + original_dest_len))
    610     {
    611       /* No splice, so we're done.  */
    612       dest->last_token = original_dest_len + src->last_token;
    613       return;
    614     }
    615 
    616   /* Okay, a simple append caused a splice.  Let's chop dest back to
    617      its original length and try again, but separate the texts with a
    618      space.  */
    619   dest->len = original_dest_len;
    620   dest->appendc (' ');
    621   dest->appendmem (src->text, src->len);
    622 
    623   dest_tail.set_shared (dest->text + dest->last_token,
    624 			dest->len - dest->last_token);
    625 
    626   /* Try to re-parse DEST's last token, as above.  */
    627   if (get_token (&new_token, &dest_tail)
    628       && (new_token.text + new_token.len
    629 	  == dest->text + original_dest_len))
    630     {
    631       /* No splice, so we're done.  */
    632       dest->last_token = original_dest_len + 1 + src->last_token;
    633       return;
    634     }
    635 
    636   /* As far as I know, there's no case where inserting a space isn't
    637      enough to prevent a splice.  */
    638   internal_error (_("unable to avoid splicing tokens during macro expansion"));
    639 }
    640 
    641 /* Stringify an argument, and insert it into DEST.  ARG is the text to
    642    stringify; it is LEN bytes long.  */
    643 
    644 static void
    645 stringify (growable_macro_buffer *dest, const char *arg, int len)
    646 {
    647   /* Trim initial whitespace from ARG.  */
    648   while (len > 0 && macro_is_whitespace (*arg))
    649     {
    650       ++arg;
    651       --len;
    652     }
    653 
    654   /* Trim trailing whitespace from ARG.  */
    655   while (len > 0 && macro_is_whitespace (arg[len - 1]))
    656     --len;
    657 
    658   /* Insert the string.  */
    659   dest->appendc ('"');
    660   while (len > 0)
    661     {
    662       /* We could try to handle strange cases here, like control
    663 	 characters, but there doesn't seem to be much point.  */
    664       if (macro_is_whitespace (*arg))
    665 	{
    666 	  /* Replace a sequence of whitespace with a single space.  */
    667 	  dest->appendc (' ');
    668 	  while (len > 1 && macro_is_whitespace (arg[1]))
    669 	    {
    670 	      ++arg;
    671 	      --len;
    672 	    }
    673 	}
    674       else if (*arg == '\\' || *arg == '"')
    675 	{
    676 	  dest->appendc ('\\');
    677 	  dest->appendc (*arg);
    678 	}
    679       else
    680 	dest->appendc (*arg);
    681       ++arg;
    682       --len;
    683     }
    684   dest->appendc ('"');
    685   dest->last_token = dest->len;
    686 }
    687 
    688 /* See macroexp.h.  */
    689 
    690 gdb::unique_xmalloc_ptr<char>
    691 macro_stringify (const char *str)
    692 {
    693   int len = strlen (str);
    694   growable_macro_buffer buffer (len);
    695 
    696   stringify (&buffer, str, len);
    697   buffer.appendc ('\0');
    698 
    699   return buffer.release ();
    700 }
    701 
    702 
    703 /* Expanding macros!  */
    705 
    706 
    707 /* A singly-linked list of the names of the macros we are currently
    708    expanding --- for detecting expansion loops.  */
    709 struct macro_name_list {
    710   const char *name;
    711   struct macro_name_list *next;
    712 };
    713 
    714 
    715 /* Return non-zero if we are currently expanding the macro named NAME,
    716    according to LIST; otherwise, return zero.
    717 
    718    You know, it would be possible to get rid of all the NO_LOOP
    719    arguments to these functions by simply generating a new lookup
    720    function and baton which refuses to find the definition for a
    721    particular macro, and otherwise delegates the decision to another
    722    function/baton pair.  But that makes the linked list of excluded
    723    macros chained through untyped baton pointers, which will make it
    724    harder to debug.  :(  */
    725 static int
    726 currently_rescanning (struct macro_name_list *list, const char *name)
    727 {
    728   for (; list; list = list->next)
    729     if (strcmp (name, list->name) == 0)
    730       return 1;
    731 
    732   return 0;
    733 }
    734 
    735 
    736 /* Gather the arguments to a macro expansion.
    737 
    738    NAME is the name of the macro being invoked.  (It's only used for
    739    printing error messages.)
    740 
    741    Assume that SRC is the text of the macro invocation immediately
    742    following the macro name.  For example, if we're processing the
    743    text foo(bar, baz), then NAME would be foo and SRC will be (bar,
    744    baz).
    745 
    746    If SRC doesn't start with an open paren ( token at all, return
    747    false, leave SRC unchanged, and don't set *ARGS_PTR to anything.
    748 
    749    If SRC doesn't contain a properly terminated argument list, then
    750    raise an error.
    751 
    752    For a variadic macro, NARGS holds the number of formal arguments to
    753    the macro.  For a GNU-style variadic macro, this should be the
    754    number of named arguments.  For a non-variadic macro, NARGS should
    755    be -1.
    756 
    757    Otherwise, return true and set *ARGS_PTR to a vector of macro
    758    buffers referring to the argument texts.  The macro buffers share
    759    their text with SRC, and their last_token fields are initialized.
    760 
    761    NOTE WELL: if SRC starts with a open paren ( token followed
    762    immediately by a close paren ) token (e.g., the invocation looks
    763    like "foo()"), we treat that as one argument, which happens to be
    764    the empty list of tokens.  The caller should keep in mind that such
    765    a sequence of tokens is a valid way to invoke one-parameter
    766    function-like macros, but also a valid way to invoke zero-parameter
    767    function-like macros.  Eeew.
    768 
    769    Consume the tokens from SRC; after this call, SRC contains the text
    770    following the invocation.  */
    771 
    772 static bool
    773 gather_arguments (const char *name, shared_macro_buffer *src, int nargs,
    774 		  std::vector<shared_macro_buffer> *args_ptr)
    775 {
    776   shared_macro_buffer tok;
    777   std::vector<shared_macro_buffer> args;
    778 
    779   /* Does SRC start with an opening paren token?  Read from a copy of
    780      SRC, so SRC itself is unaffected if we don't find an opening
    781      paren.  */
    782   {
    783     shared_macro_buffer temp (src->text, src->len);
    784 
    785     if (! get_token (&tok, &temp)
    786 	|| tok.len != 1
    787 	|| tok.text[0] != '(')
    788       return false;
    789   }
    790 
    791   /* Consume SRC's opening paren.  */
    792   get_token (&tok, src);
    793 
    794   for (;;)
    795     {
    796       shared_macro_buffer *arg;
    797       int depth;
    798 
    799       /* Initialize the next argument.  */
    800       args.emplace_back ();
    801       arg = &args.back ();
    802       set_token (arg, src->text, src->text);
    803 
    804       /* Gather the argument's tokens.  */
    805       depth = 0;
    806       for (;;)
    807 	{
    808 	  if (! get_token (&tok, src))
    809 	    error (_("Malformed argument list for macro `%s'."), name);
    810 
    811 	  /* Is tok an opening paren?  */
    812 	  if (tok.len == 1 && tok.text[0] == '(')
    813 	    depth++;
    814 
    815 	  /* Is tok is a closing paren?  */
    816 	  else if (tok.len == 1 && tok.text[0] == ')')
    817 	    {
    818 	      /* If it's a closing paren at the top level, then that's
    819 		 the end of the argument list.  */
    820 	      if (depth == 0)
    821 		{
    822 		  /* In the varargs case, the last argument may be
    823 		     missing.  Add an empty argument in this case.  */
    824 		  if (nargs != -1 && args.size () == nargs - 1)
    825 		    {
    826 		      args.emplace_back ();
    827 		      arg = &args.back ();
    828 		      set_token (arg, src->text, src->text);
    829 		    }
    830 
    831 		  *args_ptr = std::move (args);
    832 		  return true;
    833 		}
    834 
    835 	      depth--;
    836 	    }
    837 
    838 	  /* If tok is a comma at top level, then that's the end of
    839 	     the current argument.  However, if we are handling a
    840 	     variadic macro and we are computing the last argument, we
    841 	     want to include the comma and remaining tokens.  */
    842 	  else if (tok.len == 1 && tok.text[0] == ',' && depth == 0
    843 		   && (nargs == -1 || args.size () < nargs))
    844 	    break;
    845 
    846 	  /* Extend the current argument to enclose this token.  If
    847 	     this is the current argument's first token, leave out any
    848 	     leading whitespace, just for aesthetics.  */
    849 	  if (arg->len == 0)
    850 	    {
    851 	      arg->text = tok.text;
    852 	      arg->len = tok.len;
    853 	      arg->last_token = 0;
    854 	    }
    855 	  else
    856 	    {
    857 	      arg->len = (tok.text + tok.len) - arg->text;
    858 	      arg->last_token = tok.text - arg->text;
    859 	    }
    860 	}
    861     }
    862 }
    863 
    864 
    865 /* The `expand' and `substitute_args' functions both invoke `scan'
    866    recursively, so we need a forward declaration somewhere.  */
    867 static void scan (growable_macro_buffer *dest,
    868 		  shared_macro_buffer *src,
    869 		  struct macro_name_list *no_loop,
    870 		  const macro_scope &scope);
    871 
    872 /* A helper function for substitute_args.
    873 
    874    ARGV is a vector of all the arguments; ARGC is the number of
    875    arguments.  IS_VARARGS is true if the macro being substituted is a
    876    varargs macro; in this case VA_ARG_NAME is the name of the
    877    "variable" argument.  VA_ARG_NAME is ignored if IS_VARARGS is
    878    false.
    879 
    880    If the token TOK is the name of a parameter, return the parameter's
    881    index.  If TOK is not an argument, return -1.  */
    882 
    883 static int
    884 find_parameter (const shared_macro_buffer *tok,
    885 		int is_varargs, const shared_macro_buffer *va_arg_name,
    886 		int argc, const char * const *argv)
    887 {
    888   int i;
    889 
    890   if (! tok->is_identifier)
    891     return -1;
    892 
    893   for (i = 0; i < argc; ++i)
    894     if (tok->len == strlen (argv[i])
    895 	&& !memcmp (tok->text, argv[i], tok->len))
    896       return i;
    897 
    898   if (is_varargs && tok->len == va_arg_name->len
    899       && ! memcmp (tok->text, va_arg_name->text, tok->len))
    900     return argc - 1;
    901 
    902   return -1;
    903 }
    904 
    905 /* Helper function for substitute_args that gets the next token and
    906    updates the passed-in state variables.  */
    907 
    908 static void
    909 get_next_token_for_substitution (shared_macro_buffer *replacement_list,
    910 				 shared_macro_buffer *token,
    911 				 const char **start,
    912 				 shared_macro_buffer *lookahead,
    913 				 const char **lookahead_start,
    914 				 int *lookahead_valid,
    915 				 bool *keep_going)
    916 {
    917   if (!*lookahead_valid)
    918     *keep_going = false;
    919   else
    920     {
    921       *keep_going = true;
    922       *token = *lookahead;
    923       *start = *lookahead_start;
    924       *lookahead_start = replacement_list->text;
    925       *lookahead_valid = get_token (lookahead, replacement_list);
    926     }
    927 }
    928 
    929 /* Given the macro definition DEF, being invoked with the actual
    930    arguments given by ARGV, substitute the arguments into the
    931    replacement list, and store the result in DEST.
    932 
    933    IS_VARARGS should be true if DEF is a varargs macro.  In this case,
    934    VA_ARG_NAME should be the name of the "variable" argument -- either
    935    __VA_ARGS__ for c99-style varargs, or the final argument name, for
    936    GNU-style varargs.  If IS_VARARGS is false, this parameter is
    937    ignored.
    938 
    939    If it is necessary to expand macro invocations in one of the
    940    arguments, use LOOKUP_FUNC and LOOKUP_BATON to find the macro
    941    definitions, and don't expand invocations of the macros listed in
    942    NO_LOOP.  */
    943 
    944 static void
    945 substitute_args (growable_macro_buffer *dest,
    946 		 struct macro_definition *def,
    947 		 int is_varargs, const shared_macro_buffer *va_arg_name,
    948 		 const std::vector<shared_macro_buffer> &argv,
    949 		 struct macro_name_list *no_loop,
    950 		 const macro_scope &scope)
    951 {
    952   /* The token we are currently considering.  */
    953   shared_macro_buffer tok;
    954   /* The replacement list's pointer from just before TOK was lexed.  */
    955   const char *original_rl_start;
    956   /* We have a single lookahead token to handle token splicing.  */
    957   shared_macro_buffer lookahead;
    958   /* The lookahead token might not be valid.  */
    959   int lookahead_valid;
    960   /* The replacement list's pointer from just before LOOKAHEAD was
    961      lexed.  */
    962   const char *lookahead_rl_start;
    963 
    964   /* A macro buffer for the macro's replacement list.  */
    965   shared_macro_buffer replacement_list (def->replacement,
    966 					strlen (def->replacement));
    967 
    968   gdb_assert (dest->len == 0);
    969   dest->last_token = 0;
    970 
    971   original_rl_start = replacement_list.text;
    972   if (! get_token (&tok, &replacement_list))
    973     return;
    974   lookahead_rl_start = replacement_list.text;
    975   lookahead_valid = get_token (&lookahead, &replacement_list);
    976 
    977   /* __VA_OPT__ state variable.  The states are:
    978      0 - nothing happening
    979      1 - saw __VA_OPT__
    980      >= 2 in __VA_OPT__, the value encodes the parenthesis depth.  */
    981   unsigned vaopt_state = 0;
    982 
    983   for (bool keep_going = true;
    984        keep_going;
    985        get_next_token_for_substitution (&replacement_list,
    986 					&tok,
    987 					&original_rl_start,
    988 					&lookahead,
    989 					&lookahead_rl_start,
    990 					&lookahead_valid,
    991 					&keep_going))
    992     {
    993       bool token_is_vaopt = (tok.len == 10
    994 			     && startswith (tok.text, "__VA_OPT__"));
    995 
    996       if (vaopt_state > 0)
    997 	{
    998 	  if (token_is_vaopt)
    999 	    error (_("__VA_OPT__ cannot appear inside __VA_OPT__"));
   1000 	  else if (tok.len == 1 && tok.text[0] == '(')
   1001 	    {
   1002 	      ++vaopt_state;
   1003 	      /* We just entered __VA_OPT__, so don't emit this
   1004 		 token.  */
   1005 	      continue;
   1006 	    }
   1007 	  else if (vaopt_state == 1)
   1008 	    error (_("__VA_OPT__ must be followed by an open parenthesis"));
   1009 	  else if (tok.len == 1 && tok.text[0] == ')')
   1010 	    {
   1011 	      --vaopt_state;
   1012 	      if (vaopt_state == 1)
   1013 		{
   1014 		  /* Done with __VA_OPT__.  */
   1015 		  vaopt_state = 0;
   1016 		  /* Don't emit.  */
   1017 		  continue;
   1018 		}
   1019 	    }
   1020 
   1021 	  /* If __VA_ARGS__ is empty, then drop the contents of
   1022 	     __VA_OPT__.  */
   1023 	  if (argv.back ().len == 0)
   1024 	    continue;
   1025 	}
   1026       else if (token_is_vaopt)
   1027 	{
   1028 	  if (!is_varargs)
   1029 	    error (_("__VA_OPT__ is only valid in a variadic macro"));
   1030 	  vaopt_state = 1;
   1031 	  /* Don't emit this token.  */
   1032 	  continue;
   1033 	}
   1034 
   1035       /* Just for aesthetics.  If we skipped some whitespace, copy
   1036 	 that to DEST.  */
   1037       if (tok.text > original_rl_start)
   1038 	{
   1039 	  dest->appendmem (original_rl_start, tok.text - original_rl_start);
   1040 	  dest->last_token = dest->len;
   1041 	}
   1042 
   1043       /* Is this token the stringification operator?  */
   1044       if (tok.len == 1
   1045 	  && tok.text[0] == '#')
   1046 	{
   1047 	  int arg;
   1048 
   1049 	  if (!lookahead_valid)
   1050 	    error (_("Stringification operator requires an argument."));
   1051 
   1052 	  arg = find_parameter (&lookahead, is_varargs, va_arg_name,
   1053 				def->argc, def->argv);
   1054 	  if (arg == -1)
   1055 	    error (_("Argument to stringification operator must name "
   1056 		     "a macro parameter."));
   1057 
   1058 	  stringify (dest, argv[arg].text, argv[arg].len);
   1059 
   1060 	  /* Read one token and let the loop iteration code handle the
   1061 	     rest.  */
   1062 	  lookahead_rl_start = replacement_list.text;
   1063 	  lookahead_valid = get_token (&lookahead, &replacement_list);
   1064 	}
   1065       /* Is this token the splicing operator?  */
   1066       else if (tok.len == 2
   1067 	       && tok.text[0] == '#'
   1068 	       && tok.text[1] == '#')
   1069 	error (_("Stray splicing operator"));
   1070       /* Is the next token the splicing operator?  */
   1071       else if (lookahead_valid
   1072 	       && lookahead.len == 2
   1073 	       && lookahead.text[0] == '#'
   1074 	       && lookahead.text[1] == '#')
   1075 	{
   1076 	  int finished = 0;
   1077 	  int prev_was_comma = 0;
   1078 
   1079 	  /* Note that GCC warns if the result of splicing is not a
   1080 	     token.  In the debugger there doesn't seem to be much
   1081 	     benefit from doing this.  */
   1082 
   1083 	  /* Insert the first token.  */
   1084 	  if (tok.len == 1 && tok.text[0] == ',')
   1085 	    prev_was_comma = 1;
   1086 	  else
   1087 	    {
   1088 	      int arg = find_parameter (&tok, is_varargs, va_arg_name,
   1089 					def->argc, def->argv);
   1090 
   1091 	      if (arg != -1)
   1092 		dest->appendmem (argv[arg].text, argv[arg].len);
   1093 	      else
   1094 		dest->appendmem (tok.text, tok.len);
   1095 	    }
   1096 
   1097 	  /* Apply a possible sequence of ## operators.  */
   1098 	  for (;;)
   1099 	    {
   1100 	      if (! get_token (&tok, &replacement_list))
   1101 		error (_("Splicing operator at end of macro"));
   1102 
   1103 	      /* Handle a comma before a ##.  If we are handling
   1104 		 varargs, and the token on the right hand side is the
   1105 		 varargs marker, and the final argument is empty or
   1106 		 missing, then drop the comma.  This is a GNU
   1107 		 extension.  There is one ambiguous case here,
   1108 		 involving pedantic behavior with an empty argument,
   1109 		 but we settle that in favor of GNU-style (GCC uses an
   1110 		 option).  If we aren't dealing with varargs, we
   1111 		 simply insert the comma.  */
   1112 	      if (prev_was_comma)
   1113 		{
   1114 		  if (! (is_varargs
   1115 			 && tok.len == va_arg_name->len
   1116 			 && !memcmp (tok.text, va_arg_name->text, tok.len)
   1117 			 && argv.back ().len == 0))
   1118 		    dest->appendmem (",", 1);
   1119 		  prev_was_comma = 0;
   1120 		}
   1121 
   1122 	      /* Insert the token.  If it is a parameter, insert the
   1123 		 argument.  If it is a comma, treat it specially.  */
   1124 	      if (tok.len == 1 && tok.text[0] == ',')
   1125 		prev_was_comma = 1;
   1126 	      else
   1127 		{
   1128 		  int arg = find_parameter (&tok, is_varargs, va_arg_name,
   1129 					    def->argc, def->argv);
   1130 
   1131 		  if (arg != -1)
   1132 		    dest->appendmem (argv[arg].text, argv[arg].len);
   1133 		  else
   1134 		    dest->appendmem (tok.text, tok.len);
   1135 		}
   1136 
   1137 	      /* Now read another token.  If it is another splice, we
   1138 		 loop.  */
   1139 	      original_rl_start = replacement_list.text;
   1140 	      if (! get_token (&tok, &replacement_list))
   1141 		{
   1142 		  finished = 1;
   1143 		  break;
   1144 		}
   1145 
   1146 	      if (! (tok.len == 2
   1147 		     && tok.text[0] == '#'
   1148 		     && tok.text[1] == '#'))
   1149 		break;
   1150 	    }
   1151 
   1152 	  if (prev_was_comma)
   1153 	    {
   1154 	      /* We saw a comma.  Insert it now.  */
   1155 	      dest->appendmem (",", 1);
   1156 	    }
   1157 
   1158 	  dest->last_token = dest->len;
   1159 	  if (finished)
   1160 	    lookahead_valid = 0;
   1161 	  else
   1162 	    {
   1163 	      /* Set up for the loop iterator.  */
   1164 	      lookahead = tok;
   1165 	      lookahead_rl_start = original_rl_start;
   1166 	      lookahead_valid = 1;
   1167 	    }
   1168 	}
   1169       else
   1170 	{
   1171 	  /* Is this token an identifier?  */
   1172 	  int substituted = 0;
   1173 	  int arg = find_parameter (&tok, is_varargs, va_arg_name,
   1174 				    def->argc, def->argv);
   1175 
   1176 	  if (arg != -1)
   1177 	    {
   1178 	      /* Expand any macro invocations in the argument text,
   1179 		 and append the result to dest.  Remember that scan
   1180 		 mutates its source, so we need to scan a new buffer
   1181 		 referring to the argument's text, not the argument
   1182 		 itself.  */
   1183 	      shared_macro_buffer arg_src (argv[arg].text, argv[arg].len);
   1184 	      scan (dest, &arg_src, no_loop, scope);
   1185 	      substituted = 1;
   1186 	    }
   1187 
   1188 	  /* If it wasn't a parameter, then just copy it across.  */
   1189 	  if (! substituted)
   1190 	    append_tokens_without_splicing (dest, &tok);
   1191 	}
   1192     }
   1193 
   1194   if (vaopt_state > 0)
   1195     error (_("Unterminated __VA_OPT__"));
   1196 }
   1197 
   1198 
   1199 /* Expand a call to a macro named ID, whose definition is DEF.  Append
   1200    its expansion to DEST.  SRC is the input text following the ID
   1201    token.  We are currently rescanning the expansions of the macros
   1202    named in NO_LOOP; don't re-expand them.  Use LOOKUP_FUNC and
   1203    LOOKUP_BATON to find definitions for any nested macro references.
   1204 
   1205    Return 1 if we decided to expand it, zero otherwise.  (If it's a
   1206    function-like macro name that isn't followed by an argument list,
   1207    we don't expand it.)  If we return zero, leave SRC unchanged.  */
   1208 static int
   1209 expand (const char *id,
   1210 	struct macro_definition *def,
   1211 	growable_macro_buffer *dest,
   1212 	shared_macro_buffer *src,
   1213 	struct macro_name_list *no_loop,
   1214 	const macro_scope &scope)
   1215 {
   1216   struct macro_name_list new_no_loop;
   1217 
   1218   /* Create a new node to be added to the front of the no-expand list.
   1219      This list is appropriate for re-scanning replacement lists, but
   1220      it is *not* appropriate for scanning macro arguments; invocations
   1221      of the macro whose arguments we are gathering *do* get expanded
   1222      there.  */
   1223   new_no_loop.name = id;
   1224   new_no_loop.next = no_loop;
   1225 
   1226   /* What kind of macro are we expanding?  */
   1227   if (def->kind == macro_object_like)
   1228     {
   1229       shared_macro_buffer replacement_list (def->replacement,
   1230 					    strlen (def->replacement));
   1231 
   1232       scan (dest, &replacement_list, &new_no_loop, scope);
   1233       return 1;
   1234     }
   1235   else if (def->kind == macro_function_like)
   1236     {
   1237       shared_macro_buffer va_arg_name;
   1238       int is_varargs = 0;
   1239 
   1240       if (def->argc >= 1)
   1241 	{
   1242 	  if (strcmp (def->argv[def->argc - 1], "...") == 0)
   1243 	    {
   1244 	      /* In C99-style varargs, substitution is done using
   1245 		 __VA_ARGS__.  */
   1246 	      va_arg_name.set_shared ("__VA_ARGS__", strlen ("__VA_ARGS__"));
   1247 	      is_varargs = 1;
   1248 	    }
   1249 	  else
   1250 	    {
   1251 	      int len = strlen (def->argv[def->argc - 1]);
   1252 
   1253 	      if (len > 3
   1254 		  && strcmp (def->argv[def->argc - 1] + len - 3, "...") == 0)
   1255 		{
   1256 		  /* In GNU-style varargs, the name of the
   1257 		     substitution parameter is the name of the formal
   1258 		     argument without the "...".  */
   1259 		  va_arg_name.set_shared (def->argv[def->argc - 1], len - 3);
   1260 		  is_varargs = 1;
   1261 		}
   1262 	    }
   1263 	}
   1264 
   1265       std::vector<shared_macro_buffer> argv;
   1266       /* If we couldn't find any argument list, then we don't expand
   1267 	 this macro.  */
   1268       if (!gather_arguments (id, src, is_varargs ? def->argc : -1,
   1269 			     &argv))
   1270 	return 0;
   1271 
   1272       /* Check that we're passing an acceptable number of arguments for
   1273 	 this macro.  */
   1274       if (argv.size () != def->argc)
   1275 	{
   1276 	  if (is_varargs && argv.size () >= def->argc - 1)
   1277 	    {
   1278 	      /* Ok.  */
   1279 	    }
   1280 	  /* Remember that a sequence of tokens like "foo()" is a
   1281 	     valid invocation of a macro expecting either zero or one
   1282 	     arguments.  */
   1283 	  else if (! (argv.size () == 1
   1284 		      && argv[0].len == 0
   1285 		      && def->argc == 0))
   1286 	    error (_("Wrong number of arguments to macro `%s' "
   1287 		   "(expected %d, got %d)."),
   1288 		   id, def->argc, int (argv.size ()));
   1289 	}
   1290 
   1291       /* Note that we don't expand macro invocations in the arguments
   1292 	 yet --- we let subst_args take care of that.  Parameters that
   1293 	 appear as operands of the stringifying operator "#" or the
   1294 	 splicing operator "##" don't get macro references expanded,
   1295 	 so we can't really tell whether it's appropriate to macro-
   1296 	 expand an argument until we see how it's being used.  */
   1297       growable_macro_buffer substituted (0);
   1298       substitute_args (&substituted, def, is_varargs, &va_arg_name,
   1299 		       argv, no_loop, scope);
   1300 
   1301       /* Now `substituted' is the macro's replacement list, with all
   1302 	 argument values substituted into it properly.	Re-scan it for
   1303 	 macro references, but don't expand invocations of this macro.
   1304 
   1305 	 We create a new buffer, `substituted_src', which points into
   1306 	 `substituted', and scan that.	We can't scan `substituted'
   1307 	 itself, since the tokenization process moves the buffer's
   1308 	 text pointer around, and we still need to be able to find
   1309 	 `substituted's original text buffer after scanning it so we
   1310 	 can free it.  */
   1311       shared_macro_buffer substituted_src (substituted.text, substituted.len);
   1312       scan (dest, &substituted_src, &new_no_loop, scope);
   1313 
   1314       return 1;
   1315     }
   1316   else
   1317     internal_error (_("bad macro definition kind"));
   1318 }
   1319 
   1320 
   1321 /* If the single token in SRC_FIRST followed by the tokens in SRC_REST
   1322    constitute a macro invocation not forbidden in NO_LOOP, append its
   1323    expansion to DEST and return non-zero.  Otherwise, return zero, and
   1324    leave DEST unchanged.
   1325 
   1326    SRC_FIRST must be a string built by get_token.  */
   1327 static int
   1328 maybe_expand (growable_macro_buffer *dest,
   1329 	      shared_macro_buffer *src_first,
   1330 	      shared_macro_buffer *src_rest,
   1331 	      struct macro_name_list *no_loop,
   1332 	      const macro_scope &scope)
   1333 {
   1334   /* Is this token an identifier?  */
   1335   if (src_first->is_identifier)
   1336     {
   1337       /* Make a null-terminated copy of it, since that's what our
   1338 	 lookup function expects.  */
   1339       std::string id (src_first->text, src_first->len);
   1340 
   1341       /* If we're currently re-scanning the result of expanding
   1342 	 this macro, don't expand it again.  */
   1343       if (! currently_rescanning (no_loop, id.c_str ()))
   1344 	{
   1345 	  /* Does this identifier have a macro definition in scope?  */
   1346 	  macro_definition *def = standard_macro_lookup (id.c_str (), scope);
   1347 
   1348 	  if (def && expand (id.c_str (), def, dest, src_rest, no_loop, scope))
   1349 	    return 1;
   1350 	}
   1351     }
   1352 
   1353   return 0;
   1354 }
   1355 
   1356 
   1357 /* Expand macro references in SRC, appending the results to DEST.
   1358    Assume we are re-scanning the result of expanding the macros named
   1359    in NO_LOOP, and don't try to re-expand references to them.  */
   1360 
   1361 static void
   1362 scan (growable_macro_buffer *dest,
   1363       shared_macro_buffer *src,
   1364       struct macro_name_list *no_loop,
   1365       const macro_scope &scope)
   1366 {
   1367 
   1368   for (;;)
   1369     {
   1370       shared_macro_buffer tok;
   1371       const char *original_src_start = src->text;
   1372 
   1373       /* Find the next token in SRC.  */
   1374       if (! get_token (&tok, src))
   1375 	break;
   1376 
   1377       /* Just for aesthetics.  If we skipped some whitespace, copy
   1378 	 that to DEST.  */
   1379       if (tok.text > original_src_start)
   1380 	{
   1381 	  dest->appendmem (original_src_start, tok.text - original_src_start);
   1382 	  dest->last_token = dest->len;
   1383 	}
   1384 
   1385       if (! maybe_expand (dest, &tok, src, no_loop, scope))
   1386 	/* We didn't end up expanding tok as a macro reference, so
   1387 	   simply append it to dest.  */
   1388 	append_tokens_without_splicing (dest, &tok);
   1389     }
   1390 
   1391   /* Just for aesthetics.  If there was any trailing whitespace in
   1392      src, copy it to dest.  */
   1393   if (src->len)
   1394     {
   1395       dest->appendmem (src->text, src->len);
   1396       dest->last_token = dest->len;
   1397     }
   1398 }
   1399 
   1400 
   1401 gdb::unique_xmalloc_ptr<char>
   1402 macro_expand (const char *source, const macro_scope &scope)
   1403 {
   1404   shared_macro_buffer src (source, strlen (source));
   1405 
   1406   growable_macro_buffer dest (0);
   1407   dest.last_token = 0;
   1408 
   1409   scan (&dest, &src, 0, scope);
   1410 
   1411   dest.appendc ('\0');
   1412 
   1413   return dest.release ();
   1414 }
   1415 
   1416 
   1417 gdb::unique_xmalloc_ptr<char>
   1418 macro_expand_once (const char *source, const macro_scope &scope)
   1419 {
   1420   error (_("Expand-once not implemented yet."));
   1421 }
   1422 
   1423 gdb::unique_xmalloc_ptr<char>
   1424 macro_expand_next (const char **lexptr, const macro_scope &scope)
   1425 {
   1426   shared_macro_buffer tok;
   1427 
   1428   /* Set up SRC to refer to the input text, pointed to by *lexptr.  */
   1429   shared_macro_buffer src (*lexptr, strlen (*lexptr));
   1430 
   1431   /* Set up DEST to receive the expansion, if there is one.  */
   1432   growable_macro_buffer dest (0);
   1433   dest.last_token = 0;
   1434 
   1435   /* Get the text's first preprocessing token.  */
   1436   if (! get_token (&tok, &src))
   1437     return nullptr;
   1438 
   1439   /* If it's a macro invocation, expand it.  */
   1440   if (maybe_expand (&dest, &tok, &src, 0, scope))
   1441     {
   1442       /* It was a macro invocation!  Package up the expansion as a
   1443 	 null-terminated string and return it.  Set *lexptr to the
   1444 	 start of the next token in the input.  */
   1445       dest.appendc ('\0');
   1446       *lexptr = src.text;
   1447       return dest.release ();
   1448     }
   1449   else
   1450     {
   1451       /* It wasn't a macro invocation.  */
   1452       return nullptr;
   1453     }
   1454 }
   1455