4bc2c8
c++: Add C++20 #__VA_OPT__ support
4bc2c8
4bc2c8
The following patch implements C++20 # __VA_OPT__ (...) support.
4bc2c8
Testcases cover what I came up with myself and what LLVM has for #__VA_OPT__
4bc2c8
in its testsuite and the string literals are identical between the two
4bc2c8
compilers on the va-opt-5.c testcase.
4bc2c8
4bc2c8
2021-08-17  Jakub Jelinek  <jakub@redhat.com>
4bc2c8
4bc2c8
libcpp/
4bc2c8
	* macro.c (vaopt_state): Add m_stringify member.
4bc2c8
	(vaopt_state::vaopt_state): Initialize it.
4bc2c8
	(vaopt_state::update): Overwrite it.
4bc2c8
	(vaopt_state::stringify): New method.
4bc2c8
	(stringify_arg): Replace arg argument with first, count arguments
4bc2c8
	and add va_opt argument.  Use first instead of arg->first and
4bc2c8
	count instead of arg->count, for va_opt add paste_tokens handling.
4bc2c8
	(paste_tokens): Fix up len calculation.  Don't spell rhs twice,
4bc2c8
	instead use %.*s to supply lhs and rhs spelling lengths.  Don't call
4bc2c8
	_cpp_backup_tokens here.
4bc2c8
	(paste_all_tokens): Call it here instead.
4bc2c8
	(replace_args): Adjust stringify_arg caller.  For vaopt_state::END
4bc2c8
	if stringify is true handle __VA_OPT__ stringification.
4bc2c8
	(create_iso_definition): Handle # __VA_OPT__ similarly to # macro_arg.
4bc2c8
gcc/testsuite/
4bc2c8
	* c-c++-common/cpp/va-opt-5.c: New test.
4bc2c8
	* c-c++-common/cpp/va-opt-6.c: New test.
4bc2c8
4bc2c8
--- libcpp/macro.c
4bc2c8
+++ libcpp/macro.c
4bc2c8
@@ -118,6 +118,7 @@ class vaopt_state {
4bc2c8
     m_arg (arg),
4bc2c8
     m_variadic (is_variadic),
4bc2c8
     m_last_was_paste (false),
4bc2c8
+    m_stringify (false),
4bc2c8
     m_state (0),
4bc2c8
     m_paste_location (0),
4bc2c8
     m_location (0),
4bc2c8
@@ -145,6 +146,7 @@ class vaopt_state {
4bc2c8
 	  }
4bc2c8
 	++m_state;
4bc2c8
 	m_location = token->src_loc;
4bc2c8
+	m_stringify = (token->flags & STRINGIFY_ARG) != 0;
4bc2c8
 	return BEGIN;
4bc2c8
       }
4bc2c8
     else if (m_state == 1)
4bc2c8
@@ -234,6 +236,12 @@ class vaopt_state {
4bc2c8
     return m_state == 0;
4bc2c8
   }
4bc2c8
 
4bc2c8
+  /* Return true for # __VA_OPT__.  */
4bc2c8
+  bool stringify () const
4bc2c8
+  {
4bc2c8
+    return m_stringify;
4bc2c8
+  }
4bc2c8
+
4bc2c8
  private:
4bc2c8
 
4bc2c8
   /* The cpp_reader.  */
4bc2c8
@@ -247,6 +255,8 @@ class vaopt_state {
4bc2c8
   /* If true, the previous token was ##.  This is used to detect when
4bc2c8
      a paste occurs at the end of the sequence.  */
4bc2c8
   bool m_last_was_paste;
4bc2c8
+  /* True for #__VA_OPT__.  */
4bc2c8
+  bool m_stringify;
4bc2c8
 
4bc2c8
   /* The state variable:
4bc2c8
      0 means not parsing
8af166
@@ -284,7 +294,8 @@ static _cpp_buff *collect_args (cpp_read
4bc2c8
 static cpp_context *next_context (cpp_reader *);
4bc2c8
 static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
4bc2c8
 static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
4bc2c8
-static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
4bc2c8
+static const cpp_token *stringify_arg (cpp_reader *, const cpp_token **,
4bc2c8
+				       unsigned int, bool);
4bc2c8
 static void paste_all_tokens (cpp_reader *, const cpp_token *);
4bc2c8
 static bool paste_tokens (cpp_reader *, location_t,
4bc2c8
 			  const cpp_token **, const cpp_token *);
8af166
@@ -812,10 +823,11 @@ cpp_quote_string (uchar *dest, const uch
4bc2c8
   return dest;
4bc2c8
 }
4bc2c8
 
4bc2c8
-/* Convert a token sequence ARG to a single string token according to
4bc2c8
-   the rules of the ISO C #-operator.  */
4bc2c8
+/* Convert a token sequence FIRST to FIRST+COUNT-1 to a single string token
4bc2c8
+   according to the rules of the ISO C #-operator.  */
4bc2c8
 static const cpp_token *
4bc2c8
-stringify_arg (cpp_reader *pfile, macro_arg *arg)
4bc2c8
+stringify_arg (cpp_reader *pfile, const cpp_token **first, unsigned int count,
4bc2c8
+	       bool va_opt)
4bc2c8
 {
4bc2c8
   unsigned char *dest;
4bc2c8
   unsigned int i, escape_it, backslash_count = 0;
8af166
@@ -828,9 +840,27 @@ stringify_arg (cpp_reader *pfile, macro_
4bc2c8
   *dest++ = '"';
4bc2c8
 
4bc2c8
   /* Loop, reading in the argument's tokens.  */
4bc2c8
-  for (i = 0; i < arg->count; i++)
4bc2c8
+  for (i = 0; i < count; i++)
4bc2c8
     {
4bc2c8
-      const cpp_token *token = arg->first[i];
4bc2c8
+      const cpp_token *token = first[i];
4bc2c8
+
4bc2c8
+      if (va_opt && (token->flags & PASTE_LEFT))
4bc2c8
+	{
4bc2c8
+	  location_t virt_loc = pfile->invocation_location;
4bc2c8
+	  const cpp_token *rhs;
4bc2c8
+	  do
4bc2c8
+	    {
4bc2c8
+	      if (i == count)
4bc2c8
+		abort ();
4bc2c8
+	      rhs = first[++i];
4bc2c8
+	      if (!paste_tokens (pfile, virt_loc, &token, rhs))
4bc2c8
+		{
4bc2c8
+		  --i;
4bc2c8
+		  break;
4bc2c8
+		}
4bc2c8
+	    }
4bc2c8
+	  while (rhs->flags & PASTE_LEFT);
4bc2c8
+	}
4bc2c8
 
4bc2c8
       if (token->type == CPP_PADDING)
4bc2c8
 	{
8af166
@@ -917,7 +947,7 @@ paste_tokens (cpp_reader *pfile, locatio
4bc2c8
   cpp_token *lhs;
4bc2c8
   unsigned int len;
4bc2c8
 
4bc2c8
-  len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
4bc2c8
+  len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 2;
4bc2c8
   buf = (unsigned char *) alloca (len);
4bc2c8
   end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
4bc2c8
 
8af166
@@ -943,8 +973,10 @@ paste_tokens (cpp_reader *pfile, locatio
4bc2c8
       location_t saved_loc = lhs->src_loc;
4bc2c8
 
4bc2c8
       _cpp_pop_buffer (pfile);
4bc2c8
-      _cpp_backup_tokens (pfile, 1);
4bc2c8
-      *lhsend = '\0';
4bc2c8
+
4bc2c8
+      unsigned char *rhsstart = lhsend;
4bc2c8
+      if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
4bc2c8
+	rhsstart++;
4bc2c8
 
4bc2c8
       /* We have to remove the PASTE_LEFT flag from the old lhs, but
4bc2c8
 	 we want to keep the new location.  */
8af166
@@ -956,8 +988,10 @@ paste_tokens (cpp_reader *pfile, locatio
4bc2c8
       /* Mandatory error for all apart from assembler.  */
4bc2c8
       if (CPP_OPTION (pfile, lang) != CLK_ASM)
4bc2c8
 	cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
4bc2c8
-	 "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
4bc2c8
-		   buf, cpp_token_as_text (pfile, rhs));
4bc2c8
+			     "pasting \"%.*s\" and \"%.*s\" does not give "
4bc2c8
+			     "a valid preprocessing token",
4bc2c8
+			     (int) (lhsend - buf), buf,
4bc2c8
+			     (int) (end - rhsstart), rhsstart);
4bc2c8
       return false;
4bc2c8
     }
4bc2c8
 
8af166
@@ -1033,7 +1067,10 @@ paste_all_tokens (cpp_reader *pfile, con
4bc2c8
 	    abort ();
4bc2c8
 	}
4bc2c8
       if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
4bc2c8
-	break;
4bc2c8
+	{
4bc2c8
+	  _cpp_backup_tokens (pfile, 1);
4bc2c8
+	  break;
4bc2c8
+	}
4bc2c8
     }
4bc2c8
   while (rhs->flags & PASTE_LEFT);
4bc2c8
 
8af166
@@ -1900,7 +1937,8 @@ replace_args (cpp_reader *pfile, cpp_has
4bc2c8
 	if (src->flags & STRINGIFY_ARG)
4bc2c8
 	  {
4bc2c8
 	    if (!arg->stringified)
4bc2c8
-	      arg->stringified = stringify_arg (pfile, arg);
4bc2c8
+	      arg->stringified = stringify_arg (pfile, arg->first, arg->count,
4bc2c8
+						false);
4bc2c8
 	  }
4bc2c8
 	else if ((src->flags & PASTE_LEFT)
4bc2c8
 		 || (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
8af166
@@ -2023,6 +2061,24 @@ replace_args (cpp_reader *pfile, cpp_has
4bc2c8
 		  paste_flag = tokens_buff_last_token_ptr (buff);
4bc2c8
 		}
4bc2c8
 
4bc2c8
+	      if (vaopt_tracker.stringify ())
4bc2c8
+		{
4bc2c8
+		  unsigned int count
4bc2c8
+		    = start ? paste_flag - start : tokens_buff_count (buff);
4bc2c8
+		  const cpp_token *t
4bc2c8
+		    = stringify_arg (pfile,
4bc2c8
+				     start ? start + 1
4bc2c8
+				     : (const cpp_token **) (buff->base),
4bc2c8
+				     count, true);
4bc2c8
+		  while (count--)
4bc2c8
+		    tokens_buff_remove_last_token (buff);
4bc2c8
+		  if (src->flags & PASTE_LEFT)
4bc2c8
+		    copy_paste_flag (pfile, &t, src);
4bc2c8
+		  tokens_buff_add_token (buff, virt_locs,
4bc2c8
+					 t, t->src_loc, t->src_loc,
4bc2c8
+					 NULL, 0);
8af166
+		  continue;
4bc2c8
+		}
8af166
 	      if (start && paste_flag == start && (*start)->flags & PASTE_LEFT)
8af166
 		/* If __VA_OPT__ expands to nothing (either because __VA_ARGS__
8af166
 		   is empty or because it is __VA_OPT__() ), drop PASTE_LEFT
8af166
@@ -3584,7 +3640,10 @@ create_iso_definition (cpp_reader *pfile
4bc2c8
 	 function-like macros when lexing the subsequent token.  */
4bc2c8
       if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
4bc2c8
 	{
4bc2c8
-	  if (token->type == CPP_MACRO_ARG)
4bc2c8
+	  if (token->type == CPP_MACRO_ARG
4bc2c8
+	      || (macro->variadic
4bc2c8
+		  && token->type == CPP_NAME
4bc2c8
+		  && token->val.node.node == pfile->spec_nodes.n__VA_OPT__))
4bc2c8
 	    {
4bc2c8
 	      if (token->flags & PREV_WHITE)
4bc2c8
 		token->flags |= SP_PREV_WHITE;
4bc2c8
--- gcc/testsuite/c-c++-common/cpp/va-opt-5.c
4bc2c8
+++ gcc/testsuite/c-c++-common/cpp/va-opt-5.c
4bc2c8
@@ -0,0 +1,67 @@
4bc2c8
+/* { dg-do run } */
4bc2c8
+/* { dg-options "-std=gnu99" { target c } } */
4bc2c8
+/* { dg-options "-std=c++20" { target c++ } } */
4bc2c8
+
4bc2c8
+#define lparen (
4bc2c8
+#define a0 fooa0
4bc2c8
+#define a1  fooa1 a0
4bc2c8
+#define a2  fooa2 a1
4bc2c8
+#define a3  fooa3 a2
4bc2c8
+#define a() b lparen )
4bc2c8
+#define b() c lparen )
4bc2c8
+#define c() d lparen )
4bc2c8
+#define g h
4bc2c8
+#define i(j) j
4bc2c8
+#define f(...) #__VA_OPT__(g i(0))
4bc2c8
+#define k(x,...) # __VA_OPT__(x) #x #__VA_OPT__(__VA_ARGS__)
4bc2c8
+#define l(x,...) #__VA_OPT__(a1 x)
4bc2c8
+#define m(x,...) "a()" #__VA_OPT__(a3 __VA_ARGS__ x ## __VA_ARGS__ ## x ## c a3) "a()"
4bc2c8
+#define n(x,...) = #__VA_OPT__(a3 __VA_ARGS__ x ## __VA_ARGS__ ## x ## c a3) #x #__VA_OPT__(a0 __VA_ARGS__ x ## __VA_ARGS__ ## x ## c a0) ;
4bc2c8
+#define o(x, ...) #__VA_OPT__(x##x x##x)
4bc2c8
+#define p(x, ...) #__VA_OPT__(_Pragma ("foobar"))
4bc2c8
+#define q(...) #__VA_OPT__(/* foo */x/* bar */)
4bc2c8
+const char *v1 = f();
4bc2c8
+const char *v2 = f(123);
4bc2c8
+const char *v3 = k(1);
4bc2c8
+const char *v4 = k(1, 2, 3 );
4bc2c8
+const char *v5 = l(a());
4bc2c8
+const char *v6 = l(a1 a(), 1);
4bc2c8
+const char *v7 = m();
4bc2c8
+const char *v8 = m(,);
4bc2c8
+const char *v9 = m(,a3);
4bc2c8
+const char *v10 = m(a3,a(),a0);
4bc2c8
+const char *v11 n()
4bc2c8
+const char *v12 n(,)
4bc2c8
+const char *v13 n(,a0)
4bc2c8
+const char *v14 n(a0, a(),a0)
4bc2c8
+const char *v15 = o(, 0);
4bc2c8
+const char *v16 = p(0);
4bc2c8
+const char *v17 = p(0, 1);
4bc2c8
+const char *v18 = q();
4bc2c8
+const char *v19 = q(1);
4bc2c8
+
4bc2c8
+int
4bc2c8
+main ()
4bc2c8
+{
4bc2c8
+  if (__builtin_strcmp (v1, "")
4bc2c8
+      || __builtin_strcmp (v2, "g i(0)")
4bc2c8
+      || __builtin_strcmp (v3, "1")
4bc2c8
+      || __builtin_strcmp (v4, "112, 3")
4bc2c8
+      || __builtin_strcmp (v5, "")
4bc2c8
+      || __builtin_strcmp (v6, "a1 fooa1 fooa0 b ( )")
4bc2c8
+      || __builtin_strcmp (v7, "a()a()")
4bc2c8
+      || __builtin_strcmp (v8, "a()a()")
4bc2c8
+      || __builtin_strcmp (v9, "a()a3 fooa3 fooa2 fooa1 fooa0 a3c a3a()")
4bc2c8
+      || __builtin_strcmp (v10, "a()a3 b ( ),fooa0 a3a(),a0a3c a3a()")
4bc2c8
+      || __builtin_strcmp (v11, "")
4bc2c8
+      || __builtin_strcmp (v12, "")
4bc2c8
+      || __builtin_strcmp (v13, "a3 fooa0 a0c a3a0 fooa0 a0c a0")
4bc2c8
+      || __builtin_strcmp (v14, "a3 b ( ),fooa0 a0a(),a0a0c a3a0a0 b ( ),fooa0 a0a(),a0a0c a0")
4bc2c8
+      || __builtin_strcmp (v15, "")
4bc2c8
+      || __builtin_strcmp (v16, "")
4bc2c8
+      || __builtin_strcmp (v17, "_Pragma (\"foobar\")")
4bc2c8
+      || __builtin_strcmp (v18, "")
4bc2c8
+      || __builtin_strcmp (v19, "x"))
4bc2c8
+    __builtin_abort ();
4bc2c8
+  return 0;
4bc2c8
+}
4bc2c8
--- gcc/testsuite/c-c++-common/cpp/va-opt-6.c
4bc2c8
+++ gcc/testsuite/c-c++-common/cpp/va-opt-6.c
4bc2c8
@@ -0,0 +1,17 @@
4bc2c8
+/* { dg-do preprocess } */
4bc2c8
+/* { dg-options "-std=gnu99" { target c } } */
4bc2c8
+/* { dg-options "-std=c++20" { target c++ } } */
4bc2c8
+
4bc2c8
+#define a ""
4bc2c8
+#define b(...) a ## #__VA_OPT__(1)	/* { dg-error "pasting \"a\" and \"\"\"\" does not give a valid preprocessing token" } */
4bc2c8
+#define c(...) a ## #__VA_OPT__(1)	/* { dg-error "pasting \"a\" and \"\"1\"\" does not give a valid preprocessing token" } */
4bc2c8
+#define d(...) #__VA_OPT__(1) ## !
4bc2c8
+#define e(...) #__VA_OPT__(1) ## !
4bc2c8
+#define f(...) #__VA_OPT__(. ## !)
4bc2c8
+#define g(...) #__VA_OPT__(. ## !)
4bc2c8
+b()
4bc2c8
+c(1)
4bc2c8
+d(   )		/* { dg-error "pasting \"\"\"\" and \"!\" does not give a valid preprocessing token" } */
4bc2c8
+e(  1 )		/* { dg-error "pasting \"\"1\"\" and \"!\" does not give a valid preprocessing token" } */
4bc2c8
+f()
4bc2c8
+g(0)		/* { dg-error "pasting \".\" and \"!\" does not give a valid preprocessing token" } */