Blame SOURCES/gcc11-stringify-__VA_OPT__.patch

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