c++: Add C++20 #__VA_OPT__ support
authorJakub Jelinek <jakub@redhat.com>
Tue, 17 Aug 2021 07:25:56 +0000 (09:25 +0200)
committerJakub Jelinek <jakub@redhat.com>
Tue, 17 Aug 2021 07:27:57 +0000 (09:27 +0200)
commitd56599979211266b2f7b7535311205dd758353ac
tree2841f0061f3afc3a036c6609b32e7915dbb3eee6
parent0215b3559e55f39f38e10984a804c53907f7491c
c++: Add C++20 #__VA_OPT__ support

The following patch implements C++20 # __VA_OPT__ (...) support.
Testcases cover what I came up with myself and what LLVM has for #__VA_OPT__
in its testsuite and the string literals are identical between the two
compilers on the va-opt-5.c testcase.

2021-08-17  Jakub Jelinek  <jakub@redhat.com>

libcpp/
* macro.c (vaopt_state): Add m_stringify member.
(vaopt_state::vaopt_state): Initialize it.
(vaopt_state::update): Overwrite it.
(vaopt_state::stringify): New method.
(stringify_arg): Replace arg argument with first, count arguments
and add va_opt argument.  Use first instead of arg->first and
count instead of arg->count, for va_opt add paste_tokens handling.
(paste_tokens): Fix up len calculation.  Don't spell rhs twice,
instead use %.*s to supply lhs and rhs spelling lengths.  Don't call
_cpp_backup_tokens here.
(paste_all_tokens): Call it here instead.
(replace_args): Adjust stringify_arg caller.  For vaopt_state::END
if stringify is true handle __VA_OPT__ stringification.
(create_iso_definition): Handle # __VA_OPT__ similarly to # macro_arg.
gcc/testsuite/
* c-c++-common/cpp/va-opt-5.c: New test.
* c-c++-common/cpp/va-opt-6.c: New test.
gcc/testsuite/c-c++-common/cpp/va-opt-5.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/cpp/va-opt-6.c [new file with mode: 0644]
libcpp/macro.c