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