Blame SOURCES/gcc11-stringify-__VA_OPT__.patch

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