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