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

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