Blame SOURCES/gcc11-stringify-__VA_OPT__-2.patch

e60d6e
libcpp: Fix up #__VA_OPT__ handling [PR103415]
e60d6e
e60d6e
stringify_arg uses pfile->u_buff to create the string literal.
e60d6e
Unfortunately, paste_tokens -> _cpp_lex_direct -> lex_number -> _cpp_unaligned_alloc
e60d6e
can in some cases use pfile->u_buff too, which results in losing everything
e60d6e
prepared for the string literal until the token pasting.
e60d6e
e60d6e
The following patch fixes that by not calling paste_token during the
e60d6e
construction of the string literal, but doing that before.  All the tokens
e60d6e
we are processing have been pushed into a token buffer using
e60d6e
tokens_buff_add_token so it is fine if we paste some of them in that buffer
e60d6e
(successful pasting creates a new token in that buffer), move following
e60d6e
tokens if any to make it contiguous, pop (throw away) the extra tokens at
e60d6e
the end and then do stringify_arg.
e60d6e
e60d6e
Also, paste_tokens now copies over PREV_WHITE and PREV_FALLTHROUGH flags
e60d6e
from the original lhs token to the replacement token.  Copying that way
e60d6e
the PREV_WHITE flag is needed for the #__VA_OPT__ handling and copying
e60d6e
over PREV_FALLTHROUGH fixes the new Wimplicit-fallthrough-38.c test.
e60d6e
e60d6e
2021-12-01  Jakub Jelinek  <jakub@redhat.com>
e60d6e
e60d6e
	PR preprocessor/103415
e60d6e
libcpp/
e60d6e
	* macro.c (stringify_arg): Remove va_opt argument and va_opt handling.
e60d6e
	(paste_tokens): On successful paste or in PREV_WHITE and
e60d6e
	PREV_FALLTHROUGH flags from the *plhs token to the new token.
e60d6e
	(replace_args): Adjust stringify_arg callers.  For #__VA_OPT__,
e60d6e
	perform token pasting in a separate loop before stringify_arg call.
e60d6e
gcc/testsuite/
e60d6e
	* c-c++-common/cpp/va-opt-8.c: New test.
e60d6e
	* c-c++-common/Wimplicit-fallthrough-38.c: New test.
e60d6e
e60d6e
--- libcpp/macro.c.jj
e60d6e
+++ libcpp/macro.c
e60d6e
@@ -295,7 +295,7 @@ static cpp_context *next_context (cpp_re
e60d6e
 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
e60d6e
 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
e60d6e
 static const cpp_token *stringify_arg (cpp_reader *, const cpp_token **,
e60d6e
-				       unsigned int, bool);
e60d6e
+				       unsigned int);
e60d6e
 static void paste_all_tokens (cpp_reader *, const cpp_token *);
e60d6e
 static bool paste_tokens (cpp_reader *, location_t,
e60d6e
 			  const cpp_token **, const cpp_token *);
e60d6e
@@ -826,8 +826,7 @@ cpp_quote_string (uchar *dest, const uch
e60d6e
 /* Convert a token sequence FIRST to FIRST+COUNT-1 to a single string token
e60d6e
    according to the rules of the ISO C #-operator.  */
e60d6e
 static const cpp_token *
e60d6e
-stringify_arg (cpp_reader *pfile, const cpp_token **first, unsigned int count,
e60d6e
-	       bool va_opt)
e60d6e
+stringify_arg (cpp_reader *pfile, const cpp_token **first, unsigned int count)
e60d6e
 {
e60d6e
   unsigned char *dest;
e60d6e
   unsigned int i, escape_it, backslash_count = 0;
e60d6e
@@ -844,24 +843,6 @@ stringify_arg (cpp_reader *pfile, const
e60d6e
     {
e60d6e
       const cpp_token *token = first[i];
e60d6e
 
e60d6e
-      if (va_opt && (token->flags & PASTE_LEFT))
e60d6e
-	{
e60d6e
-	  location_t virt_loc = pfile->invocation_location;
e60d6e
-	  const cpp_token *rhs;
e60d6e
-	  do
e60d6e
-	    {
e60d6e
-	      if (i == count)
e60d6e
-		abort ();
e60d6e
-	      rhs = first[++i];
e60d6e
-	      if (!paste_tokens (pfile, virt_loc, &token, rhs))
e60d6e
-		{
e60d6e
-		  --i;
e60d6e
-		  break;
e60d6e
-		}
e60d6e
-	    }
e60d6e
-	  while (rhs->flags & PASTE_LEFT);
e60d6e
-	}
e60d6e
-
e60d6e
       if (token->type == CPP_PADDING)
e60d6e
 	{
e60d6e
 	  if (source == NULL
e60d6e
@@ -995,6 +976,7 @@ paste_tokens (cpp_reader *pfile, locatio
e60d6e
       return false;
e60d6e
     }
e60d6e
 
e60d6e
+  lhs->flags |= (*plhs)->flags & (PREV_WHITE | PREV_FALLTHROUGH);
e60d6e
   *plhs = lhs;
e60d6e
   _cpp_pop_buffer (pfile);
e60d6e
   return true;
e60d6e
@@ -1937,8 +1919,7 @@ replace_args (cpp_reader *pfile, cpp_has
e60d6e
 	if (src->flags & STRINGIFY_ARG)
e60d6e
 	  {
e60d6e
 	    if (!arg->stringified)
e60d6e
-	      arg->stringified = stringify_arg (pfile, arg->first, arg->count,
e60d6e
-						false);
e60d6e
+	      arg->stringified = stringify_arg (pfile, arg->first, arg->count);
e60d6e
 	  }
e60d6e
 	else if ((src->flags & PASTE_LEFT)
e60d6e
 		 || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
e60d6e
@@ -2065,11 +2046,46 @@ replace_args (cpp_reader *pfile, cpp_has
e60d6e
 		{
e60d6e
 		  unsigned int count
e60d6e
 		    = start ? paste_flag - start : tokens_buff_count (buff);
e60d6e
-		  const cpp_token *t
e60d6e
-		    = stringify_arg (pfile,
e60d6e
-				     start ? start + 1
e60d6e
-				     : (const cpp_token **) (buff->base),
e60d6e
-				     count, true);
e60d6e
+		  const cpp_token **first
e60d6e
+		    = start ? start + 1
e60d6e
+			    : (const cpp_token **) (buff->base);
e60d6e
+		  unsigned int i, j;
e60d6e
+
e60d6e
+		  /* Paste any tokens that need to be pasted before calling
e60d6e
+		     stringify_arg, because stringify_arg uses pfile->u_buff
e60d6e
+		     which paste_tokens can use as well.  */
e60d6e
+		  for (i = 0, j = 0; i < count; i++, j++)
e60d6e
+		    {
e60d6e
+		      const cpp_token *token = first[i];
e60d6e
+
e60d6e
+		      if (token->flags & PASTE_LEFT)
e60d6e
+			{
e60d6e
+			  location_t virt_loc = pfile->invocation_location;
e60d6e
+			  const cpp_token *rhs;
e60d6e
+			  do
e60d6e
+			    {
e60d6e
+			      if (i == count)
e60d6e
+				abort ();
e60d6e
+			      rhs = first[++i];
e60d6e
+			      if (!paste_tokens (pfile, virt_loc, &token, rhs))
e60d6e
+				{
e60d6e
+				  --i;
e60d6e
+				  break;
e60d6e
+				}
e60d6e
+			    }
e60d6e
+			  while (rhs->flags & PASTE_LEFT);
e60d6e
+			}
e60d6e
+
e60d6e
+		      first[j] = token;
e60d6e
+		    }
e60d6e
+		  if (j != i)
e60d6e
+		    {
e60d6e
+		      while (i-- != j)
e60d6e
+			tokens_buff_remove_last_token (buff);
e60d6e
+		      count = j;
e60d6e
+		    }
e60d6e
+
e60d6e
+		  const cpp_token *t = stringify_arg (pfile, first, count);
e60d6e
 		  while (count--)
e60d6e
 		    tokens_buff_remove_last_token (buff);
e60d6e
 		  if (src->flags & PASTE_LEFT)
e60d6e
--- gcc/testsuite/c-c++-common/cpp/va-opt-8.c.jj
e60d6e
+++ gcc/testsuite/c-c++-common/cpp/va-opt-8.c
e60d6e
@@ -0,0 +1,18 @@
e60d6e
+/* PR preprocessor/103415 */
e60d6e
+/* { dg-do run } */
e60d6e
+/* { dg-options "-std=gnu99" { target c } } */
e60d6e
+/* { dg-options "-std=c++20" { target c++ } } */
e60d6e
+
e60d6e
+#define n(x, ...) = #__VA_OPT__(x##3)
e60d6e
+#define o(x, ...) #__VA_OPT__(x##__VA_ARGS__##9)
e60d6e
+const char *c n(1 2, 4);
e60d6e
+const char *d = o(5  6, 7	8);
e60d6e
+
e60d6e
+int
e60d6e
+main ()
e60d6e
+{
e60d6e
+  if (__builtin_strcmp (c, "1 23")
e60d6e
+      || __builtin_strcmp (d, "5 67 89"))
e60d6e
+    __builtin_abort ();
e60d6e
+  return 0;
e60d6e
+}
e60d6e
--- gcc/testsuite/c-c++-common/Wimplicit-fallthrough-38.c.jj
e60d6e
+++ gcc/testsuite/c-c++-common/Wimplicit-fallthrough-38.c
e60d6e
@@ -0,0 +1,24 @@
e60d6e
+/* { dg-do compile } */
e60d6e
+/* { dg-options "-Wimplicit-fallthrough=3" } */
e60d6e
+
e60d6e
+#define FOO \
e60d6e
+int				\
e60d6e
+foo (int a)			\
e60d6e
+{				\
e60d6e
+  switch (a)			\
e60d6e
+    {				\
e60d6e
+    case 1:			\
e60d6e
+      ++a;			\
e60d6e
+      /* FALLTHRU */		\
e60d6e
+    case 2:			\
e60d6e
+      ++a;			\
e60d6e
+      /* FALLTHRU */		\
e60d6e
+    ca##se 3:			\
e60d6e
+      ++a;			\
e60d6e
+    default:			\
e60d6e
+      break;			\
e60d6e
+    }				\
e60d6e
+  return a;			\
e60d6e
+}
e60d6e
+
e60d6e
+FOO