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