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

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