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

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