xref: /dflybsd-src/contrib/gdb-7/gdb/macroexp.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* C preprocessor macro expansion for GDB.
2*ef5ccd6cSJohn Marino    Copyright (C) 2002-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert    Contributed by Red Hat, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "gdb_obstack.h"
225796c8dcSSimon Schubert #include "bcache.h"
235796c8dcSSimon Schubert #include "macrotab.h"
245796c8dcSSimon Schubert #include "macroexp.h"
255796c8dcSSimon Schubert #include "gdb_assert.h"
265796c8dcSSimon Schubert #include "c-lang.h"
275796c8dcSSimon Schubert 
285796c8dcSSimon Schubert 
295796c8dcSSimon Schubert 
305796c8dcSSimon Schubert /* A resizeable, substringable string type.  */
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert /* A string type that we can resize, quickly append to, and use to
345796c8dcSSimon Schubert    refer to substrings of other strings.  */
355796c8dcSSimon Schubert struct macro_buffer
365796c8dcSSimon Schubert {
375796c8dcSSimon Schubert   /* An array of characters.  The first LEN bytes are the real text,
385796c8dcSSimon Schubert      but there are SIZE bytes allocated to the array.  If SIZE is
395796c8dcSSimon Schubert      zero, then this doesn't point to a malloc'ed block.  If SHARED is
405796c8dcSSimon Schubert      non-zero, then this buffer is actually a pointer into some larger
415796c8dcSSimon Schubert      string, and we shouldn't append characters to it, etc.  Because
425796c8dcSSimon Schubert      of sharing, we can't assume in general that the text is
435796c8dcSSimon Schubert      null-terminated.  */
445796c8dcSSimon Schubert   char *text;
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert   /* The number of characters in the string.  */
475796c8dcSSimon Schubert   int len;
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert   /* The number of characters allocated to the string.  If SHARED is
505796c8dcSSimon Schubert      non-zero, this is meaningless; in this case, we set it to zero so
515796c8dcSSimon Schubert      that any "do we have room to append something?" tests will fail,
525796c8dcSSimon Schubert      so we don't always have to check SHARED before using this field.  */
535796c8dcSSimon Schubert   int size;
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert   /* Zero if TEXT can be safely realloc'ed (i.e., it's its own malloc
565796c8dcSSimon Schubert      block).  Non-zero if TEXT is actually pointing into the middle of
575796c8dcSSimon Schubert      some other block, and we shouldn't reallocate it.  */
585796c8dcSSimon Schubert   int shared;
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert   /* For detecting token splicing.
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert      This is the index in TEXT of the first character of the token
635796c8dcSSimon Schubert      that abuts the end of TEXT.  If TEXT contains no tokens, then we
645796c8dcSSimon Schubert      set this equal to LEN.  If TEXT ends in whitespace, then there is
655796c8dcSSimon Schubert      no token abutting the end of TEXT (it's just whitespace), and
665796c8dcSSimon Schubert      again, we set this equal to LEN.  We set this to -1 if we don't
675796c8dcSSimon Schubert      know the nature of TEXT.  */
685796c8dcSSimon Schubert   int last_token;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert   /* If this buffer is holding the result from get_token, then this
715796c8dcSSimon Schubert      is non-zero if it is an identifier token, zero otherwise.  */
725796c8dcSSimon Schubert   int is_identifier;
735796c8dcSSimon Schubert };
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert 
765796c8dcSSimon Schubert /* Set the macro buffer *B to the empty string, guessing that its
775796c8dcSSimon Schubert    final contents will fit in N bytes.  (It'll get resized if it
785796c8dcSSimon Schubert    doesn't, so the guess doesn't have to be right.)  Allocate the
795796c8dcSSimon Schubert    initial storage with xmalloc.  */
805796c8dcSSimon Schubert static void
init_buffer(struct macro_buffer * b,int n)815796c8dcSSimon Schubert init_buffer (struct macro_buffer *b, int n)
825796c8dcSSimon Schubert {
835796c8dcSSimon Schubert   b->size = n;
845796c8dcSSimon Schubert   if (n > 0)
855796c8dcSSimon Schubert     b->text = (char *) xmalloc (n);
865796c8dcSSimon Schubert   else
875796c8dcSSimon Schubert     b->text = NULL;
885796c8dcSSimon Schubert   b->len = 0;
895796c8dcSSimon Schubert   b->shared = 0;
905796c8dcSSimon Schubert   b->last_token = -1;
915796c8dcSSimon Schubert }
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert /* Set the macro buffer *BUF to refer to the LEN bytes at ADDR, as a
955796c8dcSSimon Schubert    shared substring.  */
965796c8dcSSimon Schubert static void
init_shared_buffer(struct macro_buffer * buf,char * addr,int len)975796c8dcSSimon Schubert init_shared_buffer (struct macro_buffer *buf, char *addr, int len)
985796c8dcSSimon Schubert {
995796c8dcSSimon Schubert   buf->text = addr;
1005796c8dcSSimon Schubert   buf->len = len;
1015796c8dcSSimon Schubert   buf->shared = 1;
1025796c8dcSSimon Schubert   buf->size = 0;
1035796c8dcSSimon Schubert   buf->last_token = -1;
1045796c8dcSSimon Schubert }
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert /* Free the text of the buffer B.  Raise an error if B is shared.  */
1085796c8dcSSimon Schubert static void
free_buffer(struct macro_buffer * b)1095796c8dcSSimon Schubert free_buffer (struct macro_buffer *b)
1105796c8dcSSimon Schubert {
1115796c8dcSSimon Schubert   gdb_assert (! b->shared);
1125796c8dcSSimon Schubert   if (b->size)
1135796c8dcSSimon Schubert     xfree (b->text);
1145796c8dcSSimon Schubert }
1155796c8dcSSimon Schubert 
116*ef5ccd6cSJohn Marino /* Like free_buffer, but return the text as an xstrdup()d string.
117*ef5ccd6cSJohn Marino    This only exists to try to make the API relatively clean.  */
118*ef5ccd6cSJohn Marino 
119*ef5ccd6cSJohn Marino static char *
free_buffer_return_text(struct macro_buffer * b)120*ef5ccd6cSJohn Marino free_buffer_return_text (struct macro_buffer *b)
121*ef5ccd6cSJohn Marino {
122*ef5ccd6cSJohn Marino   gdb_assert (! b->shared);
123*ef5ccd6cSJohn Marino   gdb_assert (b->size);
124*ef5ccd6cSJohn Marino   /* Nothing to do.  */
125*ef5ccd6cSJohn Marino   return b->text;
126*ef5ccd6cSJohn Marino }
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert /* A cleanup function for macro buffers.  */
1295796c8dcSSimon Schubert static void
cleanup_macro_buffer(void * untyped_buf)1305796c8dcSSimon Schubert cleanup_macro_buffer (void *untyped_buf)
1315796c8dcSSimon Schubert {
1325796c8dcSSimon Schubert   free_buffer ((struct macro_buffer *) untyped_buf);
1335796c8dcSSimon Schubert }
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert /* Resize the buffer B to be at least N bytes long.  Raise an error if
1375796c8dcSSimon Schubert    B shouldn't be resized.  */
1385796c8dcSSimon Schubert static void
resize_buffer(struct macro_buffer * b,int n)1395796c8dcSSimon Schubert resize_buffer (struct macro_buffer *b, int n)
1405796c8dcSSimon Schubert {
1415796c8dcSSimon Schubert   /* We shouldn't be trying to resize shared strings.  */
1425796c8dcSSimon Schubert   gdb_assert (! b->shared);
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert   if (b->size == 0)
1455796c8dcSSimon Schubert     b->size = n;
1465796c8dcSSimon Schubert   else
1475796c8dcSSimon Schubert     while (b->size <= n)
1485796c8dcSSimon Schubert       b->size *= 2;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   b->text = xrealloc (b->text, b->size);
1515796c8dcSSimon Schubert }
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert /* Append the character C to the buffer B.  */
1555796c8dcSSimon Schubert static void
appendc(struct macro_buffer * b,int c)1565796c8dcSSimon Schubert appendc (struct macro_buffer *b, int c)
1575796c8dcSSimon Schubert {
1585796c8dcSSimon Schubert   int new_len = b->len + 1;
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert   if (new_len > b->size)
1615796c8dcSSimon Schubert     resize_buffer (b, new_len);
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert   b->text[b->len] = c;
1645796c8dcSSimon Schubert   b->len = new_len;
1655796c8dcSSimon Schubert }
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert /* Append the LEN bytes at ADDR to the buffer B.  */
1695796c8dcSSimon Schubert static void
appendmem(struct macro_buffer * b,char * addr,int len)1705796c8dcSSimon Schubert appendmem (struct macro_buffer *b, char *addr, int len)
1715796c8dcSSimon Schubert {
1725796c8dcSSimon Schubert   int new_len = b->len + len;
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert   if (new_len > b->size)
1755796c8dcSSimon Schubert     resize_buffer (b, new_len);
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert   memcpy (b->text + b->len, addr, len);
1785796c8dcSSimon Schubert   b->len = new_len;
1795796c8dcSSimon Schubert }
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert /* Recognizing preprocessor tokens.  */
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert int
macro_is_whitespace(int c)1875796c8dcSSimon Schubert macro_is_whitespace (int c)
1885796c8dcSSimon Schubert {
1895796c8dcSSimon Schubert   return (c == ' '
1905796c8dcSSimon Schubert           || c == '\t'
1915796c8dcSSimon Schubert           || c == '\n'
1925796c8dcSSimon Schubert           || c == '\v'
1935796c8dcSSimon Schubert           || c == '\f');
1945796c8dcSSimon Schubert }
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert int
macro_is_digit(int c)1985796c8dcSSimon Schubert macro_is_digit (int c)
1995796c8dcSSimon Schubert {
2005796c8dcSSimon Schubert   return ('0' <= c && c <= '9');
2015796c8dcSSimon Schubert }
2025796c8dcSSimon Schubert 
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert int
macro_is_identifier_nondigit(int c)2055796c8dcSSimon Schubert macro_is_identifier_nondigit (int c)
2065796c8dcSSimon Schubert {
2075796c8dcSSimon Schubert   return (c == '_'
2085796c8dcSSimon Schubert           || ('a' <= c && c <= 'z')
2095796c8dcSSimon Schubert           || ('A' <= c && c <= 'Z'));
2105796c8dcSSimon Schubert }
2115796c8dcSSimon Schubert 
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert static void
set_token(struct macro_buffer * tok,char * start,char * end)2145796c8dcSSimon Schubert set_token (struct macro_buffer *tok, char *start, char *end)
2155796c8dcSSimon Schubert {
2165796c8dcSSimon Schubert   init_shared_buffer (tok, start, end - start);
2175796c8dcSSimon Schubert   tok->last_token = 0;
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert   /* Presumed; get_identifier may overwrite this.  */
2205796c8dcSSimon Schubert   tok->is_identifier = 0;
2215796c8dcSSimon Schubert }
2225796c8dcSSimon Schubert 
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert static int
get_comment(struct macro_buffer * tok,char * p,char * end)2255796c8dcSSimon Schubert get_comment (struct macro_buffer *tok, char *p, char *end)
2265796c8dcSSimon Schubert {
2275796c8dcSSimon Schubert   if (p + 2 > end)
2285796c8dcSSimon Schubert     return 0;
2295796c8dcSSimon Schubert   else if (p[0] == '/'
2305796c8dcSSimon Schubert            && p[1] == '*')
2315796c8dcSSimon Schubert     {
2325796c8dcSSimon Schubert       char *tok_start = p;
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert       p += 2;
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert       for (; p < end; p++)
2375796c8dcSSimon Schubert         if (p + 2 <= end
2385796c8dcSSimon Schubert             && p[0] == '*'
2395796c8dcSSimon Schubert             && p[1] == '/')
2405796c8dcSSimon Schubert           {
2415796c8dcSSimon Schubert             p += 2;
2425796c8dcSSimon Schubert             set_token (tok, tok_start, p);
2435796c8dcSSimon Schubert             return 1;
2445796c8dcSSimon Schubert           }
2455796c8dcSSimon Schubert 
2465796c8dcSSimon Schubert       error (_("Unterminated comment in macro expansion."));
2475796c8dcSSimon Schubert     }
2485796c8dcSSimon Schubert   else if (p[0] == '/'
2495796c8dcSSimon Schubert            && p[1] == '/')
2505796c8dcSSimon Schubert     {
2515796c8dcSSimon Schubert       char *tok_start = p;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert       p += 2;
2545796c8dcSSimon Schubert       for (; p < end; p++)
2555796c8dcSSimon Schubert         if (*p == '\n')
2565796c8dcSSimon Schubert           break;
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert       set_token (tok, tok_start, p);
2595796c8dcSSimon Schubert       return 1;
2605796c8dcSSimon Schubert     }
2615796c8dcSSimon Schubert   else
2625796c8dcSSimon Schubert     return 0;
2635796c8dcSSimon Schubert }
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert 
2665796c8dcSSimon Schubert static int
get_identifier(struct macro_buffer * tok,char * p,char * end)2675796c8dcSSimon Schubert get_identifier (struct macro_buffer *tok, char *p, char *end)
2685796c8dcSSimon Schubert {
2695796c8dcSSimon Schubert   if (p < end
2705796c8dcSSimon Schubert       && macro_is_identifier_nondigit (*p))
2715796c8dcSSimon Schubert     {
2725796c8dcSSimon Schubert       char *tok_start = p;
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert       while (p < end
2755796c8dcSSimon Schubert              && (macro_is_identifier_nondigit (*p)
2765796c8dcSSimon Schubert                  || macro_is_digit (*p)))
2775796c8dcSSimon Schubert         p++;
2785796c8dcSSimon Schubert 
2795796c8dcSSimon Schubert       set_token (tok, tok_start, p);
2805796c8dcSSimon Schubert       tok->is_identifier = 1;
2815796c8dcSSimon Schubert       return 1;
2825796c8dcSSimon Schubert     }
2835796c8dcSSimon Schubert   else
2845796c8dcSSimon Schubert     return 0;
2855796c8dcSSimon Schubert }
2865796c8dcSSimon Schubert 
2875796c8dcSSimon Schubert 
2885796c8dcSSimon Schubert static int
get_pp_number(struct macro_buffer * tok,char * p,char * end)2895796c8dcSSimon Schubert get_pp_number (struct macro_buffer *tok, char *p, char *end)
2905796c8dcSSimon Schubert {
2915796c8dcSSimon Schubert   if (p < end
2925796c8dcSSimon Schubert       && (macro_is_digit (*p)
2935796c8dcSSimon Schubert           || (*p == '.'
2945796c8dcSSimon Schubert 	      && p + 2 <= end
2955796c8dcSSimon Schubert 	      && macro_is_digit (p[1]))))
2965796c8dcSSimon Schubert     {
2975796c8dcSSimon Schubert       char *tok_start = p;
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert       while (p < end)
3005796c8dcSSimon Schubert         {
3015796c8dcSSimon Schubert 	  if (p + 2 <= end
3025796c8dcSSimon Schubert 	      && strchr ("eEpP", *p)
3035796c8dcSSimon Schubert 	      && (p[1] == '+' || p[1] == '-'))
3045796c8dcSSimon Schubert             p += 2;
3055796c8dcSSimon Schubert           else if (macro_is_digit (*p)
3065796c8dcSSimon Schubert 		   || macro_is_identifier_nondigit (*p)
3075796c8dcSSimon Schubert 		   || *p == '.')
3085796c8dcSSimon Schubert             p++;
3095796c8dcSSimon Schubert           else
3105796c8dcSSimon Schubert             break;
3115796c8dcSSimon Schubert         }
3125796c8dcSSimon Schubert 
3135796c8dcSSimon Schubert       set_token (tok, tok_start, p);
3145796c8dcSSimon Schubert       return 1;
3155796c8dcSSimon Schubert     }
3165796c8dcSSimon Schubert   else
3175796c8dcSSimon Schubert     return 0;
3185796c8dcSSimon Schubert }
3195796c8dcSSimon Schubert 
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert /* If the text starting at P going up to (but not including) END
3235796c8dcSSimon Schubert    starts with a character constant, set *TOK to point to that
3245796c8dcSSimon Schubert    character constant, and return 1.  Otherwise, return zero.
3255796c8dcSSimon Schubert    Signal an error if it contains a malformed or incomplete character
3265796c8dcSSimon Schubert    constant.  */
3275796c8dcSSimon Schubert static int
get_character_constant(struct macro_buffer * tok,char * p,char * end)3285796c8dcSSimon Schubert get_character_constant (struct macro_buffer *tok, char *p, char *end)
3295796c8dcSSimon Schubert {
3305796c8dcSSimon Schubert   /* ISO/IEC 9899:1999 (E)  Section 6.4.4.4  paragraph 1
3315796c8dcSSimon Schubert      But of course, what really matters is that we handle it the same
3325796c8dcSSimon Schubert      way GDB's C/C++ lexer does.  So we call parse_escape in utils.c
3335796c8dcSSimon Schubert      to handle escape sequences.  */
3345796c8dcSSimon Schubert   if ((p + 1 <= end && *p == '\'')
3355796c8dcSSimon Schubert       || (p + 2 <= end
3365796c8dcSSimon Schubert 	  && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
3375796c8dcSSimon Schubert 	  && p[1] == '\''))
3385796c8dcSSimon Schubert     {
3395796c8dcSSimon Schubert       char *tok_start = p;
3405796c8dcSSimon Schubert       int char_count = 0;
3415796c8dcSSimon Schubert 
3425796c8dcSSimon Schubert       if (*p == '\'')
3435796c8dcSSimon Schubert         p++;
3445796c8dcSSimon Schubert       else if (*p == 'L' || *p == 'u' || *p == 'U')
3455796c8dcSSimon Schubert         p += 2;
3465796c8dcSSimon Schubert       else
347c50c785cSJohn Marino         gdb_assert_not_reached ("unexpected character constant");
3485796c8dcSSimon Schubert 
3495796c8dcSSimon Schubert       for (;;)
3505796c8dcSSimon Schubert         {
3515796c8dcSSimon Schubert           if (p >= end)
3525796c8dcSSimon Schubert             error (_("Unmatched single quote."));
3535796c8dcSSimon Schubert           else if (*p == '\'')
3545796c8dcSSimon Schubert             {
3555796c8dcSSimon Schubert               if (!char_count)
3565796c8dcSSimon Schubert                 error (_("A character constant must contain at least one "
3575796c8dcSSimon Schubert                        "character."));
3585796c8dcSSimon Schubert               p++;
3595796c8dcSSimon Schubert               break;
3605796c8dcSSimon Schubert             }
3615796c8dcSSimon Schubert           else if (*p == '\\')
3625796c8dcSSimon Schubert             {
3635796c8dcSSimon Schubert               p++;
3645796c8dcSSimon Schubert 	      char_count += c_parse_escape (&p, NULL);
3655796c8dcSSimon Schubert             }
3665796c8dcSSimon Schubert           else
3675796c8dcSSimon Schubert 	    {
3685796c8dcSSimon Schubert 	      p++;
3695796c8dcSSimon Schubert 	      char_count++;
3705796c8dcSSimon Schubert 	    }
3715796c8dcSSimon Schubert         }
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert       set_token (tok, tok_start, p);
3745796c8dcSSimon Schubert       return 1;
3755796c8dcSSimon Schubert     }
3765796c8dcSSimon Schubert   else
3775796c8dcSSimon Schubert     return 0;
3785796c8dcSSimon Schubert }
3795796c8dcSSimon Schubert 
3805796c8dcSSimon Schubert 
3815796c8dcSSimon Schubert /* If the text starting at P going up to (but not including) END
3825796c8dcSSimon Schubert    starts with a string literal, set *TOK to point to that string
3835796c8dcSSimon Schubert    literal, and return 1.  Otherwise, return zero.  Signal an error if
3845796c8dcSSimon Schubert    it contains a malformed or incomplete string literal.  */
3855796c8dcSSimon Schubert static int
get_string_literal(struct macro_buffer * tok,char * p,char * end)3865796c8dcSSimon Schubert get_string_literal (struct macro_buffer *tok, char *p, char *end)
3875796c8dcSSimon Schubert {
3885796c8dcSSimon Schubert   if ((p + 1 <= end
3895796c8dcSSimon Schubert        && *p == '"')
3905796c8dcSSimon Schubert       || (p + 2 <= end
3915796c8dcSSimon Schubert           && (p[0] == 'L' || p[0] == 'u' || p[0] == 'U')
3925796c8dcSSimon Schubert           && p[1] == '"'))
3935796c8dcSSimon Schubert     {
3945796c8dcSSimon Schubert       char *tok_start = p;
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert       if (*p == '"')
3975796c8dcSSimon Schubert         p++;
3985796c8dcSSimon Schubert       else if (*p == 'L' || *p == 'u' || *p == 'U')
3995796c8dcSSimon Schubert         p += 2;
4005796c8dcSSimon Schubert       else
401c50c785cSJohn Marino         gdb_assert_not_reached ("unexpected string literal");
4025796c8dcSSimon Schubert 
4035796c8dcSSimon Schubert       for (;;)
4045796c8dcSSimon Schubert         {
4055796c8dcSSimon Schubert           if (p >= end)
4065796c8dcSSimon Schubert             error (_("Unterminated string in expression."));
4075796c8dcSSimon Schubert           else if (*p == '"')
4085796c8dcSSimon Schubert             {
4095796c8dcSSimon Schubert               p++;
4105796c8dcSSimon Schubert               break;
4115796c8dcSSimon Schubert             }
4125796c8dcSSimon Schubert           else if (*p == '\n')
4135796c8dcSSimon Schubert             error (_("Newline characters may not appear in string "
4145796c8dcSSimon Schubert                    "constants."));
4155796c8dcSSimon Schubert           else if (*p == '\\')
4165796c8dcSSimon Schubert             {
4175796c8dcSSimon Schubert               p++;
4185796c8dcSSimon Schubert               c_parse_escape (&p, NULL);
4195796c8dcSSimon Schubert             }
4205796c8dcSSimon Schubert           else
4215796c8dcSSimon Schubert             p++;
4225796c8dcSSimon Schubert         }
4235796c8dcSSimon Schubert 
4245796c8dcSSimon Schubert       set_token (tok, tok_start, p);
4255796c8dcSSimon Schubert       return 1;
4265796c8dcSSimon Schubert     }
4275796c8dcSSimon Schubert   else
4285796c8dcSSimon Schubert     return 0;
4295796c8dcSSimon Schubert }
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert 
4325796c8dcSSimon Schubert static int
get_punctuator(struct macro_buffer * tok,char * p,char * end)4335796c8dcSSimon Schubert get_punctuator (struct macro_buffer *tok, char *p, char *end)
4345796c8dcSSimon Schubert {
4355796c8dcSSimon Schubert   /* Here, speed is much less important than correctness and clarity.  */
4365796c8dcSSimon Schubert 
4375796c8dcSSimon Schubert   /* ISO/IEC 9899:1999 (E)  Section 6.4.6  Paragraph 1.
4385796c8dcSSimon Schubert      Note that this table is ordered in a special way.  A punctuator
4395796c8dcSSimon Schubert      which is a prefix of another punctuator must appear after its
4405796c8dcSSimon Schubert      "extension".  Otherwise, the wrong token will be returned.  */
4415796c8dcSSimon Schubert   static const char * const punctuators[] = {
4425796c8dcSSimon Schubert     "[", "]", "(", ")", "{", "}", "?", ";", ",", "~",
4435796c8dcSSimon Schubert     "...", ".",
4445796c8dcSSimon Schubert     "->", "--", "-=", "-",
4455796c8dcSSimon Schubert     "++", "+=", "+",
4465796c8dcSSimon Schubert     "*=", "*",
4475796c8dcSSimon Schubert     "!=", "!",
4485796c8dcSSimon Schubert     "&&", "&=", "&",
4495796c8dcSSimon Schubert     "/=", "/",
4505796c8dcSSimon Schubert     "%>", "%:%:", "%:", "%=", "%",
4515796c8dcSSimon Schubert     "^=", "^",
4525796c8dcSSimon Schubert     "##", "#",
4535796c8dcSSimon Schubert     ":>", ":",
4545796c8dcSSimon Schubert     "||", "|=", "|",
4555796c8dcSSimon Schubert     "<<=", "<<", "<=", "<:", "<%", "<",
4565796c8dcSSimon Schubert     ">>=", ">>", ">=", ">",
4575796c8dcSSimon Schubert     "==", "=",
4585796c8dcSSimon Schubert     0
4595796c8dcSSimon Schubert   };
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert   int i;
4625796c8dcSSimon Schubert 
4635796c8dcSSimon Schubert   if (p + 1 <= end)
4645796c8dcSSimon Schubert     {
4655796c8dcSSimon Schubert       for (i = 0; punctuators[i]; i++)
4665796c8dcSSimon Schubert         {
4675796c8dcSSimon Schubert           const char *punctuator = punctuators[i];
4685796c8dcSSimon Schubert 
4695796c8dcSSimon Schubert           if (p[0] == punctuator[0])
4705796c8dcSSimon Schubert             {
4715796c8dcSSimon Schubert               int len = strlen (punctuator);
4725796c8dcSSimon Schubert 
4735796c8dcSSimon Schubert               if (p + len <= end
4745796c8dcSSimon Schubert                   && ! memcmp (p, punctuator, len))
4755796c8dcSSimon Schubert                 {
4765796c8dcSSimon Schubert                   set_token (tok, p, p + len);
4775796c8dcSSimon Schubert                   return 1;
4785796c8dcSSimon Schubert                 }
4795796c8dcSSimon Schubert             }
4805796c8dcSSimon Schubert         }
4815796c8dcSSimon Schubert     }
4825796c8dcSSimon Schubert 
4835796c8dcSSimon Schubert   return 0;
4845796c8dcSSimon Schubert }
4855796c8dcSSimon Schubert 
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert /* Peel the next preprocessor token off of SRC, and put it in TOK.
4885796c8dcSSimon Schubert    Mutate TOK to refer to the first token in SRC, and mutate SRC to
4895796c8dcSSimon Schubert    refer to the text after that token.  SRC must be a shared buffer;
4905796c8dcSSimon Schubert    the resulting TOK will be shared, pointing into the same string SRC
4915796c8dcSSimon Schubert    does.  Initialize TOK's last_token field.  Return non-zero if we
4925796c8dcSSimon Schubert    succeed, or 0 if we didn't find any more tokens in SRC.  */
4935796c8dcSSimon Schubert static int
get_token(struct macro_buffer * tok,struct macro_buffer * src)4945796c8dcSSimon Schubert get_token (struct macro_buffer *tok,
4955796c8dcSSimon Schubert            struct macro_buffer *src)
4965796c8dcSSimon Schubert {
4975796c8dcSSimon Schubert   char *p = src->text;
4985796c8dcSSimon Schubert   char *end = p + src->len;
4995796c8dcSSimon Schubert 
5005796c8dcSSimon Schubert   gdb_assert (src->shared);
5015796c8dcSSimon Schubert 
5025796c8dcSSimon Schubert   /* From the ISO C standard, ISO/IEC 9899:1999 (E), section 6.4:
5035796c8dcSSimon Schubert 
5045796c8dcSSimon Schubert      preprocessing-token:
5055796c8dcSSimon Schubert          header-name
5065796c8dcSSimon Schubert          identifier
5075796c8dcSSimon Schubert          pp-number
5085796c8dcSSimon Schubert          character-constant
5095796c8dcSSimon Schubert          string-literal
5105796c8dcSSimon Schubert          punctuator
5115796c8dcSSimon Schubert          each non-white-space character that cannot be one of the above
5125796c8dcSSimon Schubert 
5135796c8dcSSimon Schubert      We don't have to deal with header-name tokens, since those can
5145796c8dcSSimon Schubert      only occur after a #include, which we will never see.  */
5155796c8dcSSimon Schubert 
5165796c8dcSSimon Schubert   while (p < end)
5175796c8dcSSimon Schubert     if (macro_is_whitespace (*p))
5185796c8dcSSimon Schubert       p++;
5195796c8dcSSimon Schubert     else if (get_comment (tok, p, end))
5205796c8dcSSimon Schubert       p += tok->len;
5215796c8dcSSimon Schubert     else if (get_pp_number (tok, p, end)
5225796c8dcSSimon Schubert              || get_character_constant (tok, p, end)
5235796c8dcSSimon Schubert              || get_string_literal (tok, p, end)
5245796c8dcSSimon Schubert              /* Note: the grammar in the standard seems to be
5255796c8dcSSimon Schubert                 ambiguous: L'x' can be either a wide character
5265796c8dcSSimon Schubert                 constant, or an identifier followed by a normal
5275796c8dcSSimon Schubert                 character constant.  By trying `get_identifier' after
5285796c8dcSSimon Schubert                 we try get_character_constant and get_string_literal,
5295796c8dcSSimon Schubert                 we give the wide character syntax precedence.  Now,
5305796c8dcSSimon Schubert                 since GDB doesn't handle wide character constants
5315796c8dcSSimon Schubert                 anyway, is this the right thing to do?  */
5325796c8dcSSimon Schubert              || get_identifier (tok, p, end)
5335796c8dcSSimon Schubert              || get_punctuator (tok, p, end))
5345796c8dcSSimon Schubert       {
5355796c8dcSSimon Schubert         /* How many characters did we consume, including whitespace?  */
5365796c8dcSSimon Schubert         int consumed = p - src->text + tok->len;
537cf7f2e2dSJohn Marino 
5385796c8dcSSimon Schubert         src->text += consumed;
5395796c8dcSSimon Schubert         src->len -= consumed;
5405796c8dcSSimon Schubert         return 1;
5415796c8dcSSimon Schubert       }
5425796c8dcSSimon Schubert     else
5435796c8dcSSimon Schubert       {
5445796c8dcSSimon Schubert         /* We have found a "non-whitespace character that cannot be
5455796c8dcSSimon Schubert            one of the above."  Make a token out of it.  */
5465796c8dcSSimon Schubert         int consumed;
5475796c8dcSSimon Schubert 
5485796c8dcSSimon Schubert         set_token (tok, p, p + 1);
5495796c8dcSSimon Schubert         consumed = p - src->text + tok->len;
5505796c8dcSSimon Schubert         src->text += consumed;
5515796c8dcSSimon Schubert         src->len -= consumed;
5525796c8dcSSimon Schubert         return 1;
5535796c8dcSSimon Schubert       }
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert   return 0;
5565796c8dcSSimon Schubert }
5575796c8dcSSimon Schubert 
5585796c8dcSSimon Schubert 
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert /* Appending token strings, with and without splicing  */
5615796c8dcSSimon Schubert 
5625796c8dcSSimon Schubert 
5635796c8dcSSimon Schubert /* Append the macro buffer SRC to the end of DEST, and ensure that
5645796c8dcSSimon Schubert    doing so doesn't splice the token at the end of SRC with the token
5655796c8dcSSimon Schubert    at the beginning of DEST.  SRC and DEST must have their last_token
5665796c8dcSSimon Schubert    fields set.  Upon return, DEST's last_token field is set correctly.
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert    For example:
5695796c8dcSSimon Schubert 
5705796c8dcSSimon Schubert    If DEST is "(" and SRC is "y", then we can return with
5715796c8dcSSimon Schubert    DEST set to "(y" --- we've simply appended the two buffers.
5725796c8dcSSimon Schubert 
5735796c8dcSSimon Schubert    However, if DEST is "x" and SRC is "y", then we must not return
5745796c8dcSSimon Schubert    with DEST set to "xy" --- that would splice the two tokens "x" and
5755796c8dcSSimon Schubert    "y" together to make a single token "xy".  However, it would be
5765796c8dcSSimon Schubert    fine to return with DEST set to "x y".  Similarly, "<" and "<" must
5775796c8dcSSimon Schubert    yield "< <", not "<<", etc.  */
5785796c8dcSSimon Schubert static void
append_tokens_without_splicing(struct macro_buffer * dest,struct macro_buffer * src)5795796c8dcSSimon Schubert append_tokens_without_splicing (struct macro_buffer *dest,
5805796c8dcSSimon Schubert                                 struct macro_buffer *src)
5815796c8dcSSimon Schubert {
5825796c8dcSSimon Schubert   int original_dest_len = dest->len;
5835796c8dcSSimon Schubert   struct macro_buffer dest_tail, new_token;
5845796c8dcSSimon Schubert 
5855796c8dcSSimon Schubert   gdb_assert (src->last_token != -1);
5865796c8dcSSimon Schubert   gdb_assert (dest->last_token != -1);
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert   /* First, just try appending the two, and call get_token to see if
5895796c8dcSSimon Schubert      we got a splice.  */
5905796c8dcSSimon Schubert   appendmem (dest, src->text, src->len);
5915796c8dcSSimon Schubert 
5925796c8dcSSimon Schubert   /* If DEST originally had no token abutting its end, then we can't
5935796c8dcSSimon Schubert      have spliced anything, so we're done.  */
5945796c8dcSSimon Schubert   if (dest->last_token == original_dest_len)
5955796c8dcSSimon Schubert     {
5965796c8dcSSimon Schubert       dest->last_token = original_dest_len + src->last_token;
5975796c8dcSSimon Schubert       return;
5985796c8dcSSimon Schubert     }
5995796c8dcSSimon Schubert 
6005796c8dcSSimon Schubert   /* Set DEST_TAIL to point to the last token in DEST, followed by
6015796c8dcSSimon Schubert      all the stuff we just appended.  */
6025796c8dcSSimon Schubert   init_shared_buffer (&dest_tail,
6035796c8dcSSimon Schubert                       dest->text + dest->last_token,
6045796c8dcSSimon Schubert                       dest->len - dest->last_token);
6055796c8dcSSimon Schubert 
6065796c8dcSSimon Schubert   /* Re-parse DEST's last token.  We know that DEST used to contain
6075796c8dcSSimon Schubert      at least one token, so if it doesn't contain any after the
6085796c8dcSSimon Schubert      append, then we must have spliced "/" and "*" or "/" and "/" to
6095796c8dcSSimon Schubert      make a comment start.  (Just for the record, I got this right
6105796c8dcSSimon Schubert      the first time.  This is not a bug fix.)  */
6115796c8dcSSimon Schubert   if (get_token (&new_token, &dest_tail)
6125796c8dcSSimon Schubert       && (new_token.text + new_token.len
6135796c8dcSSimon Schubert           == dest->text + original_dest_len))
6145796c8dcSSimon Schubert     {
6155796c8dcSSimon Schubert       /* No splice, so we're done.  */
6165796c8dcSSimon Schubert       dest->last_token = original_dest_len + src->last_token;
6175796c8dcSSimon Schubert       return;
6185796c8dcSSimon Schubert     }
6195796c8dcSSimon Schubert 
6205796c8dcSSimon Schubert   /* Okay, a simple append caused a splice.  Let's chop dest back to
6215796c8dcSSimon Schubert      its original length and try again, but separate the texts with a
6225796c8dcSSimon Schubert      space.  */
6235796c8dcSSimon Schubert   dest->len = original_dest_len;
6245796c8dcSSimon Schubert   appendc (dest, ' ');
6255796c8dcSSimon Schubert   appendmem (dest, src->text, src->len);
6265796c8dcSSimon Schubert 
6275796c8dcSSimon Schubert   init_shared_buffer (&dest_tail,
6285796c8dcSSimon Schubert                       dest->text + dest->last_token,
6295796c8dcSSimon Schubert                       dest->len - dest->last_token);
6305796c8dcSSimon Schubert 
6315796c8dcSSimon Schubert   /* Try to re-parse DEST's last token, as above.  */
6325796c8dcSSimon Schubert   if (get_token (&new_token, &dest_tail)
6335796c8dcSSimon Schubert       && (new_token.text + new_token.len
6345796c8dcSSimon Schubert           == dest->text + original_dest_len))
6355796c8dcSSimon Schubert     {
6365796c8dcSSimon Schubert       /* No splice, so we're done.  */
6375796c8dcSSimon Schubert       dest->last_token = original_dest_len + 1 + src->last_token;
6385796c8dcSSimon Schubert       return;
6395796c8dcSSimon Schubert     }
6405796c8dcSSimon Schubert 
6415796c8dcSSimon Schubert   /* As far as I know, there's no case where inserting a space isn't
6425796c8dcSSimon Schubert      enough to prevent a splice.  */
6435796c8dcSSimon Schubert   internal_error (__FILE__, __LINE__,
6445796c8dcSSimon Schubert                   _("unable to avoid splicing tokens during macro expansion"));
6455796c8dcSSimon Schubert }
6465796c8dcSSimon Schubert 
6475796c8dcSSimon Schubert /* Stringify an argument, and insert it into DEST.  ARG is the text to
6485796c8dcSSimon Schubert    stringify; it is LEN bytes long.  */
6495796c8dcSSimon Schubert 
6505796c8dcSSimon Schubert static void
stringify(struct macro_buffer * dest,const char * arg,int len)651*ef5ccd6cSJohn Marino stringify (struct macro_buffer *dest, const char *arg, int len)
6525796c8dcSSimon Schubert {
6535796c8dcSSimon Schubert   /* Trim initial whitespace from ARG.  */
6545796c8dcSSimon Schubert   while (len > 0 && macro_is_whitespace (*arg))
6555796c8dcSSimon Schubert     {
6565796c8dcSSimon Schubert       ++arg;
6575796c8dcSSimon Schubert       --len;
6585796c8dcSSimon Schubert     }
6595796c8dcSSimon Schubert 
6605796c8dcSSimon Schubert   /* Trim trailing whitespace from ARG.  */
6615796c8dcSSimon Schubert   while (len > 0 && macro_is_whitespace (arg[len - 1]))
6625796c8dcSSimon Schubert     --len;
6635796c8dcSSimon Schubert 
6645796c8dcSSimon Schubert   /* Insert the string.  */
6655796c8dcSSimon Schubert   appendc (dest, '"');
6665796c8dcSSimon Schubert   while (len > 0)
6675796c8dcSSimon Schubert     {
6685796c8dcSSimon Schubert       /* We could try to handle strange cases here, like control
6695796c8dcSSimon Schubert 	 characters, but there doesn't seem to be much point.  */
6705796c8dcSSimon Schubert       if (macro_is_whitespace (*arg))
6715796c8dcSSimon Schubert 	{
6725796c8dcSSimon Schubert 	  /* Replace a sequence of whitespace with a single space.  */
6735796c8dcSSimon Schubert 	  appendc (dest, ' ');
6745796c8dcSSimon Schubert 	  while (len > 1 && macro_is_whitespace (arg[1]))
6755796c8dcSSimon Schubert 	    {
6765796c8dcSSimon Schubert 	      ++arg;
6775796c8dcSSimon Schubert 	      --len;
6785796c8dcSSimon Schubert 	    }
6795796c8dcSSimon Schubert 	}
6805796c8dcSSimon Schubert       else if (*arg == '\\' || *arg == '"')
6815796c8dcSSimon Schubert 	{
6825796c8dcSSimon Schubert 	  appendc (dest, '\\');
6835796c8dcSSimon Schubert 	  appendc (dest, *arg);
6845796c8dcSSimon Schubert 	}
6855796c8dcSSimon Schubert       else
6865796c8dcSSimon Schubert 	appendc (dest, *arg);
6875796c8dcSSimon Schubert       ++arg;
6885796c8dcSSimon Schubert       --len;
6895796c8dcSSimon Schubert     }
6905796c8dcSSimon Schubert   appendc (dest, '"');
6915796c8dcSSimon Schubert   dest->last_token = dest->len;
6925796c8dcSSimon Schubert }
6935796c8dcSSimon Schubert 
694*ef5ccd6cSJohn Marino /* See macroexp.h.  */
695*ef5ccd6cSJohn Marino 
696*ef5ccd6cSJohn Marino char *
macro_stringify(const char * str)697*ef5ccd6cSJohn Marino macro_stringify (const char *str)
698*ef5ccd6cSJohn Marino {
699*ef5ccd6cSJohn Marino   struct macro_buffer buffer;
700*ef5ccd6cSJohn Marino   int len = strlen (str);
701*ef5ccd6cSJohn Marino 
702*ef5ccd6cSJohn Marino   init_buffer (&buffer, len);
703*ef5ccd6cSJohn Marino   stringify (&buffer, str, len);
704*ef5ccd6cSJohn Marino   appendc (&buffer, '\0');
705*ef5ccd6cSJohn Marino 
706*ef5ccd6cSJohn Marino   return free_buffer_return_text (&buffer);
707*ef5ccd6cSJohn Marino }
708*ef5ccd6cSJohn Marino 
7095796c8dcSSimon Schubert 
7105796c8dcSSimon Schubert /* Expanding macros!  */
7115796c8dcSSimon Schubert 
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert /* A singly-linked list of the names of the macros we are currently
7145796c8dcSSimon Schubert    expanding --- for detecting expansion loops.  */
7155796c8dcSSimon Schubert struct macro_name_list {
7165796c8dcSSimon Schubert   const char *name;
7175796c8dcSSimon Schubert   struct macro_name_list *next;
7185796c8dcSSimon Schubert };
7195796c8dcSSimon Schubert 
7205796c8dcSSimon Schubert 
7215796c8dcSSimon Schubert /* Return non-zero if we are currently expanding the macro named NAME,
7225796c8dcSSimon Schubert    according to LIST; otherwise, return zero.
7235796c8dcSSimon Schubert 
7245796c8dcSSimon Schubert    You know, it would be possible to get rid of all the NO_LOOP
7255796c8dcSSimon Schubert    arguments to these functions by simply generating a new lookup
7265796c8dcSSimon Schubert    function and baton which refuses to find the definition for a
7275796c8dcSSimon Schubert    particular macro, and otherwise delegates the decision to another
7285796c8dcSSimon Schubert    function/baton pair.  But that makes the linked list of excluded
7295796c8dcSSimon Schubert    macros chained through untyped baton pointers, which will make it
7305796c8dcSSimon Schubert    harder to debug.  :(  */
7315796c8dcSSimon Schubert static int
currently_rescanning(struct macro_name_list * list,const char * name)7325796c8dcSSimon Schubert currently_rescanning (struct macro_name_list *list, const char *name)
7335796c8dcSSimon Schubert {
7345796c8dcSSimon Schubert   for (; list; list = list->next)
7355796c8dcSSimon Schubert     if (strcmp (name, list->name) == 0)
7365796c8dcSSimon Schubert       return 1;
7375796c8dcSSimon Schubert 
7385796c8dcSSimon Schubert   return 0;
7395796c8dcSSimon Schubert }
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert 
7425796c8dcSSimon Schubert /* Gather the arguments to a macro expansion.
7435796c8dcSSimon Schubert 
7445796c8dcSSimon Schubert    NAME is the name of the macro being invoked.  (It's only used for
7455796c8dcSSimon Schubert    printing error messages.)
7465796c8dcSSimon Schubert 
7475796c8dcSSimon Schubert    Assume that SRC is the text of the macro invocation immediately
7485796c8dcSSimon Schubert    following the macro name.  For example, if we're processing the
7495796c8dcSSimon Schubert    text foo(bar, baz), then NAME would be foo and SRC will be (bar,
7505796c8dcSSimon Schubert    baz).
7515796c8dcSSimon Schubert 
7525796c8dcSSimon Schubert    If SRC doesn't start with an open paren ( token at all, return
7535796c8dcSSimon Schubert    zero, leave SRC unchanged, and don't set *ARGC_P to anything.
7545796c8dcSSimon Schubert 
7555796c8dcSSimon Schubert    If SRC doesn't contain a properly terminated argument list, then
7565796c8dcSSimon Schubert    raise an error.
7575796c8dcSSimon Schubert 
7585796c8dcSSimon Schubert    For a variadic macro, NARGS holds the number of formal arguments to
7595796c8dcSSimon Schubert    the macro.  For a GNU-style variadic macro, this should be the
7605796c8dcSSimon Schubert    number of named arguments.  For a non-variadic macro, NARGS should
7615796c8dcSSimon Schubert    be -1.
7625796c8dcSSimon Schubert 
7635796c8dcSSimon Schubert    Otherwise, return a pointer to the first element of an array of
7645796c8dcSSimon Schubert    macro buffers referring to the argument texts, and set *ARGC_P to
7655796c8dcSSimon Schubert    the number of arguments we found --- the number of elements in the
7665796c8dcSSimon Schubert    array.  The macro buffers share their text with SRC, and their
7675796c8dcSSimon Schubert    last_token fields are initialized.  The array is allocated with
7685796c8dcSSimon Schubert    xmalloc, and the caller is responsible for freeing it.
7695796c8dcSSimon Schubert 
7705796c8dcSSimon Schubert    NOTE WELL: if SRC starts with a open paren ( token followed
7715796c8dcSSimon Schubert    immediately by a close paren ) token (e.g., the invocation looks
7725796c8dcSSimon Schubert    like "foo()"), we treat that as one argument, which happens to be
7735796c8dcSSimon Schubert    the empty list of tokens.  The caller should keep in mind that such
7745796c8dcSSimon Schubert    a sequence of tokens is a valid way to invoke one-parameter
7755796c8dcSSimon Schubert    function-like macros, but also a valid way to invoke zero-parameter
7765796c8dcSSimon Schubert    function-like macros.  Eeew.
7775796c8dcSSimon Schubert 
7785796c8dcSSimon Schubert    Consume the tokens from SRC; after this call, SRC contains the text
7795796c8dcSSimon Schubert    following the invocation.  */
7805796c8dcSSimon Schubert 
7815796c8dcSSimon Schubert static struct macro_buffer *
gather_arguments(const char * name,struct macro_buffer * src,int nargs,int * argc_p)7825796c8dcSSimon Schubert gather_arguments (const char *name, struct macro_buffer *src,
7835796c8dcSSimon Schubert 		  int nargs, int *argc_p)
7845796c8dcSSimon Schubert {
7855796c8dcSSimon Schubert   struct macro_buffer tok;
7865796c8dcSSimon Schubert   int args_len, args_size;
7875796c8dcSSimon Schubert   struct macro_buffer *args = NULL;
7885796c8dcSSimon Schubert   struct cleanup *back_to = make_cleanup (free_current_contents, &args);
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert   /* Does SRC start with an opening paren token?  Read from a copy of
7915796c8dcSSimon Schubert      SRC, so SRC itself is unaffected if we don't find an opening
7925796c8dcSSimon Schubert      paren.  */
7935796c8dcSSimon Schubert   {
7945796c8dcSSimon Schubert     struct macro_buffer temp;
795cf7f2e2dSJohn Marino 
7965796c8dcSSimon Schubert     init_shared_buffer (&temp, src->text, src->len);
7975796c8dcSSimon Schubert 
7985796c8dcSSimon Schubert     if (! get_token (&tok, &temp)
7995796c8dcSSimon Schubert         || tok.len != 1
8005796c8dcSSimon Schubert         || tok.text[0] != '(')
8015796c8dcSSimon Schubert       {
8025796c8dcSSimon Schubert         discard_cleanups (back_to);
8035796c8dcSSimon Schubert         return 0;
8045796c8dcSSimon Schubert       }
8055796c8dcSSimon Schubert   }
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert   /* Consume SRC's opening paren.  */
8085796c8dcSSimon Schubert   get_token (&tok, src);
8095796c8dcSSimon Schubert 
8105796c8dcSSimon Schubert   args_len = 0;
8115796c8dcSSimon Schubert   args_size = 6;
8125796c8dcSSimon Schubert   args = (struct macro_buffer *) xmalloc (sizeof (*args) * args_size);
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert   for (;;)
8155796c8dcSSimon Schubert     {
8165796c8dcSSimon Schubert       struct macro_buffer *arg;
8175796c8dcSSimon Schubert       int depth;
8185796c8dcSSimon Schubert 
8195796c8dcSSimon Schubert       /* Make sure we have room for the next argument.  */
8205796c8dcSSimon Schubert       if (args_len >= args_size)
8215796c8dcSSimon Schubert         {
8225796c8dcSSimon Schubert           args_size *= 2;
8235796c8dcSSimon Schubert           args = xrealloc (args, sizeof (*args) * args_size);
8245796c8dcSSimon Schubert         }
8255796c8dcSSimon Schubert 
8265796c8dcSSimon Schubert       /* Initialize the next argument.  */
8275796c8dcSSimon Schubert       arg = &args[args_len++];
8285796c8dcSSimon Schubert       set_token (arg, src->text, src->text);
8295796c8dcSSimon Schubert 
8305796c8dcSSimon Schubert       /* Gather the argument's tokens.  */
8315796c8dcSSimon Schubert       depth = 0;
8325796c8dcSSimon Schubert       for (;;)
8335796c8dcSSimon Schubert         {
8345796c8dcSSimon Schubert           if (! get_token (&tok, src))
8355796c8dcSSimon Schubert             error (_("Malformed argument list for macro `%s'."), name);
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert           /* Is tok an opening paren?  */
8385796c8dcSSimon Schubert           if (tok.len == 1 && tok.text[0] == '(')
8395796c8dcSSimon Schubert             depth++;
8405796c8dcSSimon Schubert 
8415796c8dcSSimon Schubert           /* Is tok is a closing paren?  */
8425796c8dcSSimon Schubert           else if (tok.len == 1 && tok.text[0] == ')')
8435796c8dcSSimon Schubert             {
8445796c8dcSSimon Schubert               /* If it's a closing paren at the top level, then that's
8455796c8dcSSimon Schubert                  the end of the argument list.  */
8465796c8dcSSimon Schubert               if (depth == 0)
8475796c8dcSSimon Schubert                 {
8485796c8dcSSimon Schubert 		  /* In the varargs case, the last argument may be
8495796c8dcSSimon Schubert 		     missing.  Add an empty argument in this case.  */
8505796c8dcSSimon Schubert 		  if (nargs != -1 && args_len == nargs - 1)
8515796c8dcSSimon Schubert 		    {
8525796c8dcSSimon Schubert 		      /* Make sure we have room for the argument.  */
8535796c8dcSSimon Schubert 		      if (args_len >= args_size)
8545796c8dcSSimon Schubert 			{
8555796c8dcSSimon Schubert 			  args_size++;
8565796c8dcSSimon Schubert 			  args = xrealloc (args, sizeof (*args) * args_size);
8575796c8dcSSimon Schubert 			}
8585796c8dcSSimon Schubert 		      arg = &args[args_len++];
8595796c8dcSSimon Schubert 		      set_token (arg, src->text, src->text);
8605796c8dcSSimon Schubert 		    }
8615796c8dcSSimon Schubert 
8625796c8dcSSimon Schubert                   discard_cleanups (back_to);
8635796c8dcSSimon Schubert                   *argc_p = args_len;
8645796c8dcSSimon Schubert                   return args;
8655796c8dcSSimon Schubert                 }
8665796c8dcSSimon Schubert 
8675796c8dcSSimon Schubert               depth--;
8685796c8dcSSimon Schubert             }
8695796c8dcSSimon Schubert 
8705796c8dcSSimon Schubert           /* If tok is a comma at top level, then that's the end of
8715796c8dcSSimon Schubert              the current argument.  However, if we are handling a
8725796c8dcSSimon Schubert              variadic macro and we are computing the last argument, we
8735796c8dcSSimon Schubert              want to include the comma and remaining tokens.  */
8745796c8dcSSimon Schubert           else if (tok.len == 1 && tok.text[0] == ',' && depth == 0
8755796c8dcSSimon Schubert 		   && (nargs == -1 || args_len < nargs))
8765796c8dcSSimon Schubert             break;
8775796c8dcSSimon Schubert 
8785796c8dcSSimon Schubert           /* Extend the current argument to enclose this token.  If
8795796c8dcSSimon Schubert              this is the current argument's first token, leave out any
8805796c8dcSSimon Schubert              leading whitespace, just for aesthetics.  */
8815796c8dcSSimon Schubert           if (arg->len == 0)
8825796c8dcSSimon Schubert             {
8835796c8dcSSimon Schubert               arg->text = tok.text;
8845796c8dcSSimon Schubert               arg->len = tok.len;
8855796c8dcSSimon Schubert               arg->last_token = 0;
8865796c8dcSSimon Schubert             }
8875796c8dcSSimon Schubert           else
8885796c8dcSSimon Schubert             {
8895796c8dcSSimon Schubert               arg->len = (tok.text + tok.len) - arg->text;
8905796c8dcSSimon Schubert               arg->last_token = tok.text - arg->text;
8915796c8dcSSimon Schubert             }
8925796c8dcSSimon Schubert         }
8935796c8dcSSimon Schubert     }
8945796c8dcSSimon Schubert }
8955796c8dcSSimon Schubert 
8965796c8dcSSimon Schubert 
8975796c8dcSSimon Schubert /* The `expand' and `substitute_args' functions both invoke `scan'
8985796c8dcSSimon Schubert    recursively, so we need a forward declaration somewhere.  */
8995796c8dcSSimon Schubert static void scan (struct macro_buffer *dest,
9005796c8dcSSimon Schubert                   struct macro_buffer *src,
9015796c8dcSSimon Schubert                   struct macro_name_list *no_loop,
9025796c8dcSSimon Schubert                   macro_lookup_ftype *lookup_func,
9035796c8dcSSimon Schubert                   void *lookup_baton);
9045796c8dcSSimon Schubert 
9055796c8dcSSimon Schubert 
9065796c8dcSSimon Schubert /* A helper function for substitute_args.
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert    ARGV is a vector of all the arguments; ARGC is the number of
9095796c8dcSSimon Schubert    arguments.  IS_VARARGS is true if the macro being substituted is a
9105796c8dcSSimon Schubert    varargs macro; in this case VA_ARG_NAME is the name of the
9115796c8dcSSimon Schubert    "variable" argument.  VA_ARG_NAME is ignored if IS_VARARGS is
9125796c8dcSSimon Schubert    false.
9135796c8dcSSimon Schubert 
9145796c8dcSSimon Schubert    If the token TOK is the name of a parameter, return the parameter's
9155796c8dcSSimon Schubert    index.  If TOK is not an argument, return -1.  */
9165796c8dcSSimon Schubert 
9175796c8dcSSimon Schubert static int
find_parameter(const struct macro_buffer * tok,int is_varargs,const struct macro_buffer * va_arg_name,int argc,const char * const * argv)9185796c8dcSSimon Schubert find_parameter (const struct macro_buffer *tok,
9195796c8dcSSimon Schubert 		int is_varargs, const struct macro_buffer *va_arg_name,
9205796c8dcSSimon Schubert 		int argc, const char * const *argv)
9215796c8dcSSimon Schubert {
9225796c8dcSSimon Schubert   int i;
9235796c8dcSSimon Schubert 
9245796c8dcSSimon Schubert   if (! tok->is_identifier)
9255796c8dcSSimon Schubert     return -1;
9265796c8dcSSimon Schubert 
9275796c8dcSSimon Schubert   for (i = 0; i < argc; ++i)
928c50c785cSJohn Marino     if (tok->len == strlen (argv[i])
929c50c785cSJohn Marino 	&& !memcmp (tok->text, argv[i], tok->len))
9305796c8dcSSimon Schubert       return i;
9315796c8dcSSimon Schubert 
9325796c8dcSSimon Schubert   if (is_varargs && tok->len == va_arg_name->len
9335796c8dcSSimon Schubert       && ! memcmp (tok->text, va_arg_name->text, tok->len))
9345796c8dcSSimon Schubert     return argc - 1;
9355796c8dcSSimon Schubert 
9365796c8dcSSimon Schubert   return -1;
9375796c8dcSSimon Schubert }
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert /* Given the macro definition DEF, being invoked with the actual
9405796c8dcSSimon Schubert    arguments given by ARGC and ARGV, substitute the arguments into the
9415796c8dcSSimon Schubert    replacement list, and store the result in DEST.
9425796c8dcSSimon Schubert 
9435796c8dcSSimon Schubert    IS_VARARGS should be true if DEF is a varargs macro.  In this case,
9445796c8dcSSimon Schubert    VA_ARG_NAME should be the name of the "variable" argument -- either
9455796c8dcSSimon Schubert    __VA_ARGS__ for c99-style varargs, or the final argument name, for
9465796c8dcSSimon Schubert    GNU-style varargs.  If IS_VARARGS is false, this parameter is
9475796c8dcSSimon Schubert    ignored.
9485796c8dcSSimon Schubert 
9495796c8dcSSimon Schubert    If it is necessary to expand macro invocations in one of the
9505796c8dcSSimon Schubert    arguments, use LOOKUP_FUNC and LOOKUP_BATON to find the macro
9515796c8dcSSimon Schubert    definitions, and don't expand invocations of the macros listed in
9525796c8dcSSimon Schubert    NO_LOOP.  */
9535796c8dcSSimon Schubert 
9545796c8dcSSimon Schubert static void
substitute_args(struct macro_buffer * dest,struct macro_definition * def,int is_varargs,const struct macro_buffer * va_arg_name,int argc,struct macro_buffer * argv,struct macro_name_list * no_loop,macro_lookup_ftype * lookup_func,void * lookup_baton)9555796c8dcSSimon Schubert substitute_args (struct macro_buffer *dest,
9565796c8dcSSimon Schubert                  struct macro_definition *def,
9575796c8dcSSimon Schubert 		 int is_varargs, const struct macro_buffer *va_arg_name,
9585796c8dcSSimon Schubert                  int argc, struct macro_buffer *argv,
9595796c8dcSSimon Schubert                  struct macro_name_list *no_loop,
9605796c8dcSSimon Schubert                  macro_lookup_ftype *lookup_func,
9615796c8dcSSimon Schubert                  void *lookup_baton)
9625796c8dcSSimon Schubert {
9635796c8dcSSimon Schubert   /* A macro buffer for the macro's replacement list.  */
9645796c8dcSSimon Schubert   struct macro_buffer replacement_list;
9655796c8dcSSimon Schubert   /* The token we are currently considering.  */
9665796c8dcSSimon Schubert   struct macro_buffer tok;
9675796c8dcSSimon Schubert   /* The replacement list's pointer from just before TOK was lexed.  */
9685796c8dcSSimon Schubert   char *original_rl_start;
9695796c8dcSSimon Schubert   /* We have a single lookahead token to handle token splicing.  */
9705796c8dcSSimon Schubert   struct macro_buffer lookahead;
9715796c8dcSSimon Schubert   /* The lookahead token might not be valid.  */
9725796c8dcSSimon Schubert   int lookahead_valid;
9735796c8dcSSimon Schubert   /* The replacement list's pointer from just before LOOKAHEAD was
9745796c8dcSSimon Schubert      lexed.  */
9755796c8dcSSimon Schubert   char *lookahead_rl_start;
9765796c8dcSSimon Schubert 
9775796c8dcSSimon Schubert   init_shared_buffer (&replacement_list, (char *) def->replacement,
9785796c8dcSSimon Schubert                       strlen (def->replacement));
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert   gdb_assert (dest->len == 0);
9815796c8dcSSimon Schubert   dest->last_token = 0;
9825796c8dcSSimon Schubert 
9835796c8dcSSimon Schubert   original_rl_start = replacement_list.text;
9845796c8dcSSimon Schubert   if (! get_token (&tok, &replacement_list))
9855796c8dcSSimon Schubert     return;
9865796c8dcSSimon Schubert   lookahead_rl_start = replacement_list.text;
9875796c8dcSSimon Schubert   lookahead_valid = get_token (&lookahead, &replacement_list);
9885796c8dcSSimon Schubert 
9895796c8dcSSimon Schubert   for (;;)
9905796c8dcSSimon Schubert     {
9915796c8dcSSimon Schubert       /* Just for aesthetics.  If we skipped some whitespace, copy
9925796c8dcSSimon Schubert          that to DEST.  */
9935796c8dcSSimon Schubert       if (tok.text > original_rl_start)
9945796c8dcSSimon Schubert         {
9955796c8dcSSimon Schubert           appendmem (dest, original_rl_start, tok.text - original_rl_start);
9965796c8dcSSimon Schubert           dest->last_token = dest->len;
9975796c8dcSSimon Schubert         }
9985796c8dcSSimon Schubert 
9995796c8dcSSimon Schubert       /* Is this token the stringification operator?  */
10005796c8dcSSimon Schubert       if (tok.len == 1
10015796c8dcSSimon Schubert           && tok.text[0] == '#')
10025796c8dcSSimon Schubert 	{
10035796c8dcSSimon Schubert 	  int arg;
10045796c8dcSSimon Schubert 
10055796c8dcSSimon Schubert 	  if (!lookahead_valid)
10065796c8dcSSimon Schubert 	    error (_("Stringification operator requires an argument."));
10075796c8dcSSimon Schubert 
10085796c8dcSSimon Schubert 	  arg = find_parameter (&lookahead, is_varargs, va_arg_name,
10095796c8dcSSimon Schubert 				def->argc, def->argv);
10105796c8dcSSimon Schubert 	  if (arg == -1)
10115796c8dcSSimon Schubert 	    error (_("Argument to stringification operator must name "
10125796c8dcSSimon Schubert 		     "a macro parameter."));
10135796c8dcSSimon Schubert 
10145796c8dcSSimon Schubert 	  stringify (dest, argv[arg].text, argv[arg].len);
10155796c8dcSSimon Schubert 
10165796c8dcSSimon Schubert 	  /* Read one token and let the loop iteration code handle the
10175796c8dcSSimon Schubert 	     rest.  */
10185796c8dcSSimon Schubert 	  lookahead_rl_start = replacement_list.text;
10195796c8dcSSimon Schubert 	  lookahead_valid = get_token (&lookahead, &replacement_list);
10205796c8dcSSimon Schubert 	}
10215796c8dcSSimon Schubert       /* Is this token the splicing operator?  */
10225796c8dcSSimon Schubert       else if (tok.len == 2
10235796c8dcSSimon Schubert 	       && tok.text[0] == '#'
10245796c8dcSSimon Schubert 	       && tok.text[1] == '#')
10255796c8dcSSimon Schubert 	error (_("Stray splicing operator"));
10265796c8dcSSimon Schubert       /* Is the next token the splicing operator?  */
10275796c8dcSSimon Schubert       else if (lookahead_valid
10285796c8dcSSimon Schubert 	       && lookahead.len == 2
10295796c8dcSSimon Schubert 	       && lookahead.text[0] == '#'
10305796c8dcSSimon Schubert 	       && lookahead.text[1] == '#')
10315796c8dcSSimon Schubert 	{
1032cf7f2e2dSJohn Marino 	  int finished = 0;
10335796c8dcSSimon Schubert 	  int prev_was_comma = 0;
10345796c8dcSSimon Schubert 
10355796c8dcSSimon Schubert 	  /* Note that GCC warns if the result of splicing is not a
10365796c8dcSSimon Schubert 	     token.  In the debugger there doesn't seem to be much
10375796c8dcSSimon Schubert 	     benefit from doing this.  */
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert 	  /* Insert the first token.  */
10405796c8dcSSimon Schubert 	  if (tok.len == 1 && tok.text[0] == ',')
10415796c8dcSSimon Schubert 	    prev_was_comma = 1;
10425796c8dcSSimon Schubert 	  else
10435796c8dcSSimon Schubert 	    {
10445796c8dcSSimon Schubert 	      int arg = find_parameter (&tok, is_varargs, va_arg_name,
10455796c8dcSSimon Schubert 					def->argc, def->argv);
1046cf7f2e2dSJohn Marino 
10475796c8dcSSimon Schubert 	      if (arg != -1)
10485796c8dcSSimon Schubert 		appendmem (dest, argv[arg].text, argv[arg].len);
10495796c8dcSSimon Schubert 	      else
10505796c8dcSSimon Schubert 		appendmem (dest, tok.text, tok.len);
10515796c8dcSSimon Schubert 	    }
10525796c8dcSSimon Schubert 
10535796c8dcSSimon Schubert 	  /* Apply a possible sequence of ## operators.  */
10545796c8dcSSimon Schubert 	  for (;;)
10555796c8dcSSimon Schubert 	    {
10565796c8dcSSimon Schubert 	      if (! get_token (&tok, &replacement_list))
10575796c8dcSSimon Schubert 		error (_("Splicing operator at end of macro"));
10585796c8dcSSimon Schubert 
10595796c8dcSSimon Schubert 	      /* Handle a comma before a ##.  If we are handling
10605796c8dcSSimon Schubert 		 varargs, and the token on the right hand side is the
10615796c8dcSSimon Schubert 		 varargs marker, and the final argument is empty or
10625796c8dcSSimon Schubert 		 missing, then drop the comma.  This is a GNU
10635796c8dcSSimon Schubert 		 extension.  There is one ambiguous case here,
10645796c8dcSSimon Schubert 		 involving pedantic behavior with an empty argument,
10655796c8dcSSimon Schubert 		 but we settle that in favor of GNU-style (GCC uses an
10665796c8dcSSimon Schubert 		 option).  If we aren't dealing with varargs, we
10675796c8dcSSimon Schubert 		 simply insert the comma.  */
10685796c8dcSSimon Schubert 	      if (prev_was_comma)
10695796c8dcSSimon Schubert 		{
10705796c8dcSSimon Schubert 		  if (! (is_varargs
10715796c8dcSSimon Schubert 			 && tok.len == va_arg_name->len
10725796c8dcSSimon Schubert 			 && !memcmp (tok.text, va_arg_name->text, tok.len)
10735796c8dcSSimon Schubert 			 && argv[argc - 1].len == 0))
10745796c8dcSSimon Schubert 		    appendmem (dest, ",", 1);
10755796c8dcSSimon Schubert 		  prev_was_comma = 0;
10765796c8dcSSimon Schubert 		}
10775796c8dcSSimon Schubert 
10785796c8dcSSimon Schubert 	      /* Insert the token.  If it is a parameter, insert the
10795796c8dcSSimon Schubert 		 argument.  If it is a comma, treat it specially.  */
10805796c8dcSSimon Schubert 	      if (tok.len == 1 && tok.text[0] == ',')
10815796c8dcSSimon Schubert 		prev_was_comma = 1;
10825796c8dcSSimon Schubert 	      else
10835796c8dcSSimon Schubert 		{
10845796c8dcSSimon Schubert 		  int arg = find_parameter (&tok, is_varargs, va_arg_name,
10855796c8dcSSimon Schubert 					    def->argc, def->argv);
1086cf7f2e2dSJohn Marino 
10875796c8dcSSimon Schubert 		  if (arg != -1)
10885796c8dcSSimon Schubert 		    appendmem (dest, argv[arg].text, argv[arg].len);
10895796c8dcSSimon Schubert 		  else
10905796c8dcSSimon Schubert 		    appendmem (dest, tok.text, tok.len);
10915796c8dcSSimon Schubert 		}
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert 	      /* Now read another token.  If it is another splice, we
10945796c8dcSSimon Schubert 		 loop.  */
10955796c8dcSSimon Schubert 	      original_rl_start = replacement_list.text;
10965796c8dcSSimon Schubert 	      if (! get_token (&tok, &replacement_list))
10975796c8dcSSimon Schubert 		{
10985796c8dcSSimon Schubert 		  finished = 1;
10995796c8dcSSimon Schubert 		  break;
11005796c8dcSSimon Schubert 		}
11015796c8dcSSimon Schubert 
11025796c8dcSSimon Schubert 	      if (! (tok.len == 2
11035796c8dcSSimon Schubert 		     && tok.text[0] == '#'
11045796c8dcSSimon Schubert 		     && tok.text[1] == '#'))
11055796c8dcSSimon Schubert 		break;
11065796c8dcSSimon Schubert 	    }
11075796c8dcSSimon Schubert 
11085796c8dcSSimon Schubert 	  if (prev_was_comma)
11095796c8dcSSimon Schubert 	    {
11105796c8dcSSimon Schubert 	      /* We saw a comma.  Insert it now.  */
11115796c8dcSSimon Schubert 	      appendmem (dest, ",", 1);
11125796c8dcSSimon Schubert 	    }
11135796c8dcSSimon Schubert 
11145796c8dcSSimon Schubert           dest->last_token = dest->len;
11155796c8dcSSimon Schubert 	  if (finished)
11165796c8dcSSimon Schubert 	    lookahead_valid = 0;
11175796c8dcSSimon Schubert 	  else
11185796c8dcSSimon Schubert 	    {
11195796c8dcSSimon Schubert 	      /* Set up for the loop iterator.  */
11205796c8dcSSimon Schubert 	      lookahead = tok;
11215796c8dcSSimon Schubert 	      lookahead_rl_start = original_rl_start;
11225796c8dcSSimon Schubert 	      lookahead_valid = 1;
11235796c8dcSSimon Schubert 	    }
11245796c8dcSSimon Schubert 	}
11255796c8dcSSimon Schubert       else
11265796c8dcSSimon Schubert 	{
11275796c8dcSSimon Schubert 	  /* Is this token an identifier?  */
11285796c8dcSSimon Schubert 	  int substituted = 0;
11295796c8dcSSimon Schubert 	  int arg = find_parameter (&tok, is_varargs, va_arg_name,
11305796c8dcSSimon Schubert 				    def->argc, def->argv);
11315796c8dcSSimon Schubert 
11325796c8dcSSimon Schubert 	  if (arg != -1)
11335796c8dcSSimon Schubert 	    {
11345796c8dcSSimon Schubert 	      struct macro_buffer arg_src;
11355796c8dcSSimon Schubert 
11365796c8dcSSimon Schubert 	      /* Expand any macro invocations in the argument text,
11375796c8dcSSimon Schubert 		 and append the result to dest.  Remember that scan
11385796c8dcSSimon Schubert 		 mutates its source, so we need to scan a new buffer
11395796c8dcSSimon Schubert 		 referring to the argument's text, not the argument
11405796c8dcSSimon Schubert 		 itself.  */
11415796c8dcSSimon Schubert 	      init_shared_buffer (&arg_src, argv[arg].text, argv[arg].len);
11425796c8dcSSimon Schubert 	      scan (dest, &arg_src, no_loop, lookup_func, lookup_baton);
11435796c8dcSSimon Schubert 	      substituted = 1;
11445796c8dcSSimon Schubert 	    }
11455796c8dcSSimon Schubert 
11465796c8dcSSimon Schubert 	  /* If it wasn't a parameter, then just copy it across.  */
11475796c8dcSSimon Schubert 	  if (! substituted)
11485796c8dcSSimon Schubert 	    append_tokens_without_splicing (dest, &tok);
11495796c8dcSSimon Schubert 	}
11505796c8dcSSimon Schubert 
11515796c8dcSSimon Schubert       if (! lookahead_valid)
11525796c8dcSSimon Schubert 	break;
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert       tok = lookahead;
11555796c8dcSSimon Schubert       original_rl_start = lookahead_rl_start;
11565796c8dcSSimon Schubert 
11575796c8dcSSimon Schubert       lookahead_rl_start = replacement_list.text;
11585796c8dcSSimon Schubert       lookahead_valid = get_token (&lookahead, &replacement_list);
11595796c8dcSSimon Schubert     }
11605796c8dcSSimon Schubert }
11615796c8dcSSimon Schubert 
11625796c8dcSSimon Schubert 
11635796c8dcSSimon Schubert /* Expand a call to a macro named ID, whose definition is DEF.  Append
11645796c8dcSSimon Schubert    its expansion to DEST.  SRC is the input text following the ID
11655796c8dcSSimon Schubert    token.  We are currently rescanning the expansions of the macros
11665796c8dcSSimon Schubert    named in NO_LOOP; don't re-expand them.  Use LOOKUP_FUNC and
11675796c8dcSSimon Schubert    LOOKUP_BATON to find definitions for any nested macro references.
11685796c8dcSSimon Schubert 
11695796c8dcSSimon Schubert    Return 1 if we decided to expand it, zero otherwise.  (If it's a
11705796c8dcSSimon Schubert    function-like macro name that isn't followed by an argument list,
11715796c8dcSSimon Schubert    we don't expand it.)  If we return zero, leave SRC unchanged.  */
11725796c8dcSSimon Schubert static int
expand(const char * id,struct macro_definition * def,struct macro_buffer * dest,struct macro_buffer * src,struct macro_name_list * no_loop,macro_lookup_ftype * lookup_func,void * lookup_baton)11735796c8dcSSimon Schubert expand (const char *id,
11745796c8dcSSimon Schubert         struct macro_definition *def,
11755796c8dcSSimon Schubert         struct macro_buffer *dest,
11765796c8dcSSimon Schubert         struct macro_buffer *src,
11775796c8dcSSimon Schubert         struct macro_name_list *no_loop,
11785796c8dcSSimon Schubert         macro_lookup_ftype *lookup_func,
11795796c8dcSSimon Schubert         void *lookup_baton)
11805796c8dcSSimon Schubert {
11815796c8dcSSimon Schubert   struct macro_name_list new_no_loop;
11825796c8dcSSimon Schubert 
11835796c8dcSSimon Schubert   /* Create a new node to be added to the front of the no-expand list.
11845796c8dcSSimon Schubert      This list is appropriate for re-scanning replacement lists, but
11855796c8dcSSimon Schubert      it is *not* appropriate for scanning macro arguments; invocations
11865796c8dcSSimon Schubert      of the macro whose arguments we are gathering *do* get expanded
11875796c8dcSSimon Schubert      there.  */
11885796c8dcSSimon Schubert   new_no_loop.name = id;
11895796c8dcSSimon Schubert   new_no_loop.next = no_loop;
11905796c8dcSSimon Schubert 
11915796c8dcSSimon Schubert   /* What kind of macro are we expanding?  */
11925796c8dcSSimon Schubert   if (def->kind == macro_object_like)
11935796c8dcSSimon Schubert     {
11945796c8dcSSimon Schubert       struct macro_buffer replacement_list;
11955796c8dcSSimon Schubert 
11965796c8dcSSimon Schubert       init_shared_buffer (&replacement_list, (char *) def->replacement,
11975796c8dcSSimon Schubert                           strlen (def->replacement));
11985796c8dcSSimon Schubert 
11995796c8dcSSimon Schubert       scan (dest, &replacement_list, &new_no_loop, lookup_func, lookup_baton);
12005796c8dcSSimon Schubert       return 1;
12015796c8dcSSimon Schubert     }
12025796c8dcSSimon Schubert   else if (def->kind == macro_function_like)
12035796c8dcSSimon Schubert     {
12045796c8dcSSimon Schubert       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
12055796c8dcSSimon Schubert       int argc = 0;
12065796c8dcSSimon Schubert       struct macro_buffer *argv = NULL;
12075796c8dcSSimon Schubert       struct macro_buffer substituted;
12085796c8dcSSimon Schubert       struct macro_buffer substituted_src;
1209c50c785cSJohn Marino       struct macro_buffer va_arg_name = {0};
12105796c8dcSSimon Schubert       int is_varargs = 0;
12115796c8dcSSimon Schubert 
12125796c8dcSSimon Schubert       if (def->argc >= 1)
12135796c8dcSSimon Schubert 	{
12145796c8dcSSimon Schubert 	  if (strcmp (def->argv[def->argc - 1], "...") == 0)
12155796c8dcSSimon Schubert 	    {
12165796c8dcSSimon Schubert 	      /* In C99-style varargs, substitution is done using
12175796c8dcSSimon Schubert 		 __VA_ARGS__.  */
12185796c8dcSSimon Schubert 	      init_shared_buffer (&va_arg_name, "__VA_ARGS__",
12195796c8dcSSimon Schubert 				  strlen ("__VA_ARGS__"));
12205796c8dcSSimon Schubert 	      is_varargs = 1;
12215796c8dcSSimon Schubert 	    }
12225796c8dcSSimon Schubert 	  else
12235796c8dcSSimon Schubert 	    {
12245796c8dcSSimon Schubert 	      int len = strlen (def->argv[def->argc - 1]);
1225cf7f2e2dSJohn Marino 
12265796c8dcSSimon Schubert 	      if (len > 3
12275796c8dcSSimon Schubert 		  && strcmp (def->argv[def->argc - 1] + len - 3, "...") == 0)
12285796c8dcSSimon Schubert 		{
12295796c8dcSSimon Schubert 		  /* In GNU-style varargs, the name of the
12305796c8dcSSimon Schubert 		     substitution parameter is the name of the formal
12315796c8dcSSimon Schubert 		     argument without the "...".  */
12325796c8dcSSimon Schubert 		  init_shared_buffer (&va_arg_name,
12335796c8dcSSimon Schubert 				      (char *) def->argv[def->argc - 1],
12345796c8dcSSimon Schubert 				      len - 3);
12355796c8dcSSimon Schubert 		  is_varargs = 1;
12365796c8dcSSimon Schubert 		}
12375796c8dcSSimon Schubert 	    }
12385796c8dcSSimon Schubert 	}
12395796c8dcSSimon Schubert 
12405796c8dcSSimon Schubert       make_cleanup (free_current_contents, &argv);
12415796c8dcSSimon Schubert       argv = gather_arguments (id, src, is_varargs ? def->argc : -1,
12425796c8dcSSimon Schubert 			       &argc);
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert       /* If we couldn't find any argument list, then we don't expand
12455796c8dcSSimon Schubert          this macro.  */
12465796c8dcSSimon Schubert       if (! argv)
12475796c8dcSSimon Schubert         {
12485796c8dcSSimon Schubert           do_cleanups (back_to);
12495796c8dcSSimon Schubert           return 0;
12505796c8dcSSimon Schubert         }
12515796c8dcSSimon Schubert 
12525796c8dcSSimon Schubert       /* Check that we're passing an acceptable number of arguments for
12535796c8dcSSimon Schubert          this macro.  */
12545796c8dcSSimon Schubert       if (argc != def->argc)
12555796c8dcSSimon Schubert         {
12565796c8dcSSimon Schubert 	  if (is_varargs && argc >= def->argc - 1)
12575796c8dcSSimon Schubert 	    {
12585796c8dcSSimon Schubert 	      /* Ok.  */
12595796c8dcSSimon Schubert 	    }
12605796c8dcSSimon Schubert           /* Remember that a sequence of tokens like "foo()" is a
12615796c8dcSSimon Schubert              valid invocation of a macro expecting either zero or one
12625796c8dcSSimon Schubert              arguments.  */
12635796c8dcSSimon Schubert           else if (! (argc == 1
12645796c8dcSSimon Schubert 		      && argv[0].len == 0
12655796c8dcSSimon Schubert 		      && def->argc == 0))
12665796c8dcSSimon Schubert             error (_("Wrong number of arguments to macro `%s' "
12675796c8dcSSimon Schubert                    "(expected %d, got %d)."),
12685796c8dcSSimon Schubert                    id, def->argc, argc);
12695796c8dcSSimon Schubert         }
12705796c8dcSSimon Schubert 
12715796c8dcSSimon Schubert       /* Note that we don't expand macro invocations in the arguments
12725796c8dcSSimon Schubert          yet --- we let subst_args take care of that.  Parameters that
12735796c8dcSSimon Schubert          appear as operands of the stringifying operator "#" or the
12745796c8dcSSimon Schubert          splicing operator "##" don't get macro references expanded,
12755796c8dcSSimon Schubert          so we can't really tell whether it's appropriate to macro-
12765796c8dcSSimon Schubert          expand an argument until we see how it's being used.  */
12775796c8dcSSimon Schubert       init_buffer (&substituted, 0);
12785796c8dcSSimon Schubert       make_cleanup (cleanup_macro_buffer, &substituted);
12795796c8dcSSimon Schubert       substitute_args (&substituted, def, is_varargs, &va_arg_name,
12805796c8dcSSimon Schubert 		       argc, argv, no_loop, lookup_func, lookup_baton);
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert       /* Now `substituted' is the macro's replacement list, with all
12835796c8dcSSimon Schubert          argument values substituted into it properly.  Re-scan it for
12845796c8dcSSimon Schubert          macro references, but don't expand invocations of this macro.
12855796c8dcSSimon Schubert 
12865796c8dcSSimon Schubert          We create a new buffer, `substituted_src', which points into
12875796c8dcSSimon Schubert          `substituted', and scan that.  We can't scan `substituted'
12885796c8dcSSimon Schubert          itself, since the tokenization process moves the buffer's
12895796c8dcSSimon Schubert          text pointer around, and we still need to be able to find
12905796c8dcSSimon Schubert          `substituted's original text buffer after scanning it so we
12915796c8dcSSimon Schubert          can free it.  */
12925796c8dcSSimon Schubert       init_shared_buffer (&substituted_src, substituted.text, substituted.len);
12935796c8dcSSimon Schubert       scan (dest, &substituted_src, &new_no_loop, lookup_func, lookup_baton);
12945796c8dcSSimon Schubert 
12955796c8dcSSimon Schubert       do_cleanups (back_to);
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert       return 1;
12985796c8dcSSimon Schubert     }
12995796c8dcSSimon Schubert   else
13005796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__, _("bad macro definition kind"));
13015796c8dcSSimon Schubert }
13025796c8dcSSimon Schubert 
13035796c8dcSSimon Schubert 
13045796c8dcSSimon Schubert /* If the single token in SRC_FIRST followed by the tokens in SRC_REST
13055796c8dcSSimon Schubert    constitute a macro invokation not forbidden in NO_LOOP, append its
13065796c8dcSSimon Schubert    expansion to DEST and return non-zero.  Otherwise, return zero, and
13075796c8dcSSimon Schubert    leave DEST unchanged.
13085796c8dcSSimon Schubert 
13095796c8dcSSimon Schubert    SRC_FIRST and SRC_REST must be shared buffers; DEST must not be one.
13105796c8dcSSimon Schubert    SRC_FIRST must be a string built by get_token.  */
13115796c8dcSSimon Schubert static int
maybe_expand(struct macro_buffer * dest,struct macro_buffer * src_first,struct macro_buffer * src_rest,struct macro_name_list * no_loop,macro_lookup_ftype * lookup_func,void * lookup_baton)13125796c8dcSSimon Schubert maybe_expand (struct macro_buffer *dest,
13135796c8dcSSimon Schubert               struct macro_buffer *src_first,
13145796c8dcSSimon Schubert               struct macro_buffer *src_rest,
13155796c8dcSSimon Schubert               struct macro_name_list *no_loop,
13165796c8dcSSimon Schubert               macro_lookup_ftype *lookup_func,
13175796c8dcSSimon Schubert               void *lookup_baton)
13185796c8dcSSimon Schubert {
13195796c8dcSSimon Schubert   gdb_assert (src_first->shared);
13205796c8dcSSimon Schubert   gdb_assert (src_rest->shared);
13215796c8dcSSimon Schubert   gdb_assert (! dest->shared);
13225796c8dcSSimon Schubert 
13235796c8dcSSimon Schubert   /* Is this token an identifier?  */
13245796c8dcSSimon Schubert   if (src_first->is_identifier)
13255796c8dcSSimon Schubert     {
13265796c8dcSSimon Schubert       /* Make a null-terminated copy of it, since that's what our
13275796c8dcSSimon Schubert          lookup function expects.  */
13285796c8dcSSimon Schubert       char *id = xmalloc (src_first->len + 1);
13295796c8dcSSimon Schubert       struct cleanup *back_to = make_cleanup (xfree, id);
1330cf7f2e2dSJohn Marino 
13315796c8dcSSimon Schubert       memcpy (id, src_first->text, src_first->len);
13325796c8dcSSimon Schubert       id[src_first->len] = 0;
13335796c8dcSSimon Schubert 
13345796c8dcSSimon Schubert       /* If we're currently re-scanning the result of expanding
13355796c8dcSSimon Schubert          this macro, don't expand it again.  */
13365796c8dcSSimon Schubert       if (! currently_rescanning (no_loop, id))
13375796c8dcSSimon Schubert         {
13385796c8dcSSimon Schubert           /* Does this identifier have a macro definition in scope?  */
13395796c8dcSSimon Schubert           struct macro_definition *def = lookup_func (id, lookup_baton);
13405796c8dcSSimon Schubert 
13415796c8dcSSimon Schubert           if (def && expand (id, def, dest, src_rest, no_loop,
13425796c8dcSSimon Schubert                              lookup_func, lookup_baton))
13435796c8dcSSimon Schubert             {
13445796c8dcSSimon Schubert               do_cleanups (back_to);
13455796c8dcSSimon Schubert               return 1;
13465796c8dcSSimon Schubert             }
13475796c8dcSSimon Schubert         }
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert       do_cleanups (back_to);
13505796c8dcSSimon Schubert     }
13515796c8dcSSimon Schubert 
13525796c8dcSSimon Schubert   return 0;
13535796c8dcSSimon Schubert }
13545796c8dcSSimon Schubert 
13555796c8dcSSimon Schubert 
13565796c8dcSSimon Schubert /* Expand macro references in SRC, appending the results to DEST.
13575796c8dcSSimon Schubert    Assume we are re-scanning the result of expanding the macros named
13585796c8dcSSimon Schubert    in NO_LOOP, and don't try to re-expand references to them.
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert    SRC must be a shared buffer; DEST must not be one.  */
13615796c8dcSSimon Schubert static void
scan(struct macro_buffer * dest,struct macro_buffer * src,struct macro_name_list * no_loop,macro_lookup_ftype * lookup_func,void * lookup_baton)13625796c8dcSSimon Schubert scan (struct macro_buffer *dest,
13635796c8dcSSimon Schubert       struct macro_buffer *src,
13645796c8dcSSimon Schubert       struct macro_name_list *no_loop,
13655796c8dcSSimon Schubert       macro_lookup_ftype *lookup_func,
13665796c8dcSSimon Schubert       void *lookup_baton)
13675796c8dcSSimon Schubert {
13685796c8dcSSimon Schubert   gdb_assert (src->shared);
13695796c8dcSSimon Schubert   gdb_assert (! dest->shared);
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert   for (;;)
13725796c8dcSSimon Schubert     {
13735796c8dcSSimon Schubert       struct macro_buffer tok;
13745796c8dcSSimon Schubert       char *original_src_start = src->text;
13755796c8dcSSimon Schubert 
13765796c8dcSSimon Schubert       /* Find the next token in SRC.  */
13775796c8dcSSimon Schubert       if (! get_token (&tok, src))
13785796c8dcSSimon Schubert         break;
13795796c8dcSSimon Schubert 
13805796c8dcSSimon Schubert       /* Just for aesthetics.  If we skipped some whitespace, copy
13815796c8dcSSimon Schubert          that to DEST.  */
13825796c8dcSSimon Schubert       if (tok.text > original_src_start)
13835796c8dcSSimon Schubert         {
13845796c8dcSSimon Schubert           appendmem (dest, original_src_start, tok.text - original_src_start);
13855796c8dcSSimon Schubert           dest->last_token = dest->len;
13865796c8dcSSimon Schubert         }
13875796c8dcSSimon Schubert 
13885796c8dcSSimon Schubert       if (! maybe_expand (dest, &tok, src, no_loop, lookup_func, lookup_baton))
13895796c8dcSSimon Schubert         /* We didn't end up expanding tok as a macro reference, so
13905796c8dcSSimon Schubert            simply append it to dest.  */
13915796c8dcSSimon Schubert         append_tokens_without_splicing (dest, &tok);
13925796c8dcSSimon Schubert     }
13935796c8dcSSimon Schubert 
13945796c8dcSSimon Schubert   /* Just for aesthetics.  If there was any trailing whitespace in
13955796c8dcSSimon Schubert      src, copy it to dest.  */
13965796c8dcSSimon Schubert   if (src->len)
13975796c8dcSSimon Schubert     {
13985796c8dcSSimon Schubert       appendmem (dest, src->text, src->len);
13995796c8dcSSimon Schubert       dest->last_token = dest->len;
14005796c8dcSSimon Schubert     }
14015796c8dcSSimon Schubert }
14025796c8dcSSimon Schubert 
14035796c8dcSSimon Schubert 
14045796c8dcSSimon Schubert char *
macro_expand(const char * source,macro_lookup_ftype * lookup_func,void * lookup_func_baton)14055796c8dcSSimon Schubert macro_expand (const char *source,
14065796c8dcSSimon Schubert               macro_lookup_ftype *lookup_func,
14075796c8dcSSimon Schubert               void *lookup_func_baton)
14085796c8dcSSimon Schubert {
14095796c8dcSSimon Schubert   struct macro_buffer src, dest;
14105796c8dcSSimon Schubert   struct cleanup *back_to;
14115796c8dcSSimon Schubert 
14125796c8dcSSimon Schubert   init_shared_buffer (&src, (char *) source, strlen (source));
14135796c8dcSSimon Schubert 
14145796c8dcSSimon Schubert   init_buffer (&dest, 0);
14155796c8dcSSimon Schubert   dest.last_token = 0;
14165796c8dcSSimon Schubert   back_to = make_cleanup (cleanup_macro_buffer, &dest);
14175796c8dcSSimon Schubert 
14185796c8dcSSimon Schubert   scan (&dest, &src, 0, lookup_func, lookup_func_baton);
14195796c8dcSSimon Schubert 
14205796c8dcSSimon Schubert   appendc (&dest, '\0');
14215796c8dcSSimon Schubert 
14225796c8dcSSimon Schubert   discard_cleanups (back_to);
14235796c8dcSSimon Schubert   return dest.text;
14245796c8dcSSimon Schubert }
14255796c8dcSSimon Schubert 
14265796c8dcSSimon Schubert 
14275796c8dcSSimon Schubert char *
macro_expand_once(const char * source,macro_lookup_ftype * lookup_func,void * lookup_func_baton)14285796c8dcSSimon Schubert macro_expand_once (const char *source,
14295796c8dcSSimon Schubert                    macro_lookup_ftype *lookup_func,
14305796c8dcSSimon Schubert                    void *lookup_func_baton)
14315796c8dcSSimon Schubert {
14325796c8dcSSimon Schubert   error (_("Expand-once not implemented yet."));
14335796c8dcSSimon Schubert }
14345796c8dcSSimon Schubert 
14355796c8dcSSimon Schubert 
14365796c8dcSSimon Schubert char *
macro_expand_next(char ** lexptr,macro_lookup_ftype * lookup_func,void * lookup_baton)14375796c8dcSSimon Schubert macro_expand_next (char **lexptr,
14385796c8dcSSimon Schubert                    macro_lookup_ftype *lookup_func,
14395796c8dcSSimon Schubert                    void *lookup_baton)
14405796c8dcSSimon Schubert {
14415796c8dcSSimon Schubert   struct macro_buffer src, dest, tok;
14425796c8dcSSimon Schubert   struct cleanup *back_to;
14435796c8dcSSimon Schubert 
14445796c8dcSSimon Schubert   /* Set up SRC to refer to the input text, pointed to by *lexptr.  */
14455796c8dcSSimon Schubert   init_shared_buffer (&src, *lexptr, strlen (*lexptr));
14465796c8dcSSimon Schubert 
14475796c8dcSSimon Schubert   /* Set up DEST to receive the expansion, if there is one.  */
14485796c8dcSSimon Schubert   init_buffer (&dest, 0);
14495796c8dcSSimon Schubert   dest.last_token = 0;
14505796c8dcSSimon Schubert   back_to = make_cleanup (cleanup_macro_buffer, &dest);
14515796c8dcSSimon Schubert 
14525796c8dcSSimon Schubert   /* Get the text's first preprocessing token.  */
14535796c8dcSSimon Schubert   if (! get_token (&tok, &src))
14545796c8dcSSimon Schubert     {
14555796c8dcSSimon Schubert       do_cleanups (back_to);
14565796c8dcSSimon Schubert       return 0;
14575796c8dcSSimon Schubert     }
14585796c8dcSSimon Schubert 
14595796c8dcSSimon Schubert   /* If it's a macro invocation, expand it.  */
14605796c8dcSSimon Schubert   if (maybe_expand (&dest, &tok, &src, 0, lookup_func, lookup_baton))
14615796c8dcSSimon Schubert     {
14625796c8dcSSimon Schubert       /* It was a macro invocation!  Package up the expansion as a
14635796c8dcSSimon Schubert          null-terminated string and return it.  Set *lexptr to the
14645796c8dcSSimon Schubert          start of the next token in the input.  */
14655796c8dcSSimon Schubert       appendc (&dest, '\0');
14665796c8dcSSimon Schubert       discard_cleanups (back_to);
14675796c8dcSSimon Schubert       *lexptr = src.text;
14685796c8dcSSimon Schubert       return dest.text;
14695796c8dcSSimon Schubert     }
14705796c8dcSSimon Schubert   else
14715796c8dcSSimon Schubert     {
14725796c8dcSSimon Schubert       /* It wasn't a macro invocation.  */
14735796c8dcSSimon Schubert       do_cleanups (back_to);
14745796c8dcSSimon Schubert       return 0;
14755796c8dcSSimon Schubert     }
14765796c8dcSSimon Schubert }
1477