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