c++: Implement C++23 P2334R1 - #elifdef/#elifndef
Commit Message
Hi!
This patch implements C++23 P2334R1, which is easy because Joseph has done
all the hard work for C2X already.
Unlike the C N2645 paper, the C++ P2334R1 contains one important addition
(but not in the normative text):
"While this is a new preprocessor feature and cannot be treated as a defect
report, implementations that support older versions of the standard are
encouraged to implement this feature in the older language modes as well
as C++23."
so there are different variants how to implement it.
One is in the patch below, ignores that sentence and only implements it
for -std=c++23/-std=gnu++23 like it is only implemented for -std=c23.
Another option would be to implement it also in the older GNU modes but
not in the C/CXX modes (but it would be strange if we did that just for
C++ and not for C).
Yet another option is to enable it unconditionally.
And yet another option would be to enable it unconditionally but emit
a warning (or pedwarn) when it is seen.
Note, when it is enabled for the older language modes, as Joseph wrote
in the c11-elifdef-1.c testcase, it can result e.g. in rejecting previously
valid code:
#define A
#undef B
#if 0
#elifdef A
#error "#elifdef A applied"
#endif
#if 0
#elifndef B
#error "#elifndef B applied"
#endif
Note, seems clang went the enable it unconditionally in all standard
versions of both C and C++, no warnings or anything whatsoever, so
essentially treated it as a DR that changed behavior of e.g. the above code.
2021-10-05 Jakub Jelinek <jakub@redhat.com>
libcpp/
* init.c (lang_defaults): Implement P2334R1, enable elifdef for
-std=c++23 and -std=gnu++23.
gcc/testsuite/
* g++.dg/cpp/elifdef-1.C: New test.
* g++.dg/cpp/elifdef-2.C: New test.
* g++.dg/cpp/elifdef-3.C: New test.
Jakub
Comments
On Tue, Oct 05, 2021 at 10:35:12AM +0200, Jakub Jelinek wrote:
> Hi!
>
> This patch implements C++23 P2334R1, which is easy because Joseph has done
> all the hard work for C2X already.
> Unlike the C N2645 paper, the C++ P2334R1 contains one important addition
> (but not in the normative text):
> "While this is a new preprocessor feature and cannot be treated as a defect
> report, implementations that support older versions of the standard are
> encouraged to implement this feature in the older language modes as well
> as C++23."
> so there are different variants how to implement it.
> One is in the patch below, ignores that sentence and only implements it
> for -std=c++23/-std=gnu++23 like it is only implemented for -std=c23.
> Another option would be to implement it also in the older GNU modes but
> not in the C/CXX modes (but it would be strange if we did that just for
> C++ and not for C).
> Yet another option is to enable it unconditionally.
This would work for me, but...
> And yet another option would be to enable it unconditionally but emit
> a warning (or pedwarn) when it is seen.
...frankly, I'd prefer this last option: enable it unconditionally but emit
a pedwarn when -Wpedantic. I think let's see what Joseph and Jason think
before you change your patch though; don't want to add more work for you.
> Note, when it is enabled for the older language modes, as Joseph wrote
> in the c11-elifdef-1.c testcase, it can result e.g. in rejecting previously
> valid code:
> #define A
> #undef B
> #if 0
> #elifdef A
> #error "#elifdef A applied"
> #endif
> #if 0
> #elifndef B
> #error "#elifndef B applied"
> #endif
> Note, seems clang went the enable it unconditionally in all standard
> versions of both C and C++, no warnings or anything whatsoever, so
> essentially treated it as a DR that changed behavior of e.g. the above code.
>
> 2021-10-05 Jakub Jelinek <jakub@redhat.com>
>
> libcpp/
> * init.c (lang_defaults): Implement P2334R1, enable elifdef for
> -std=c++23 and -std=gnu++23.
> gcc/testsuite/
> * g++.dg/cpp/elifdef-1.C: New test.
> * g++.dg/cpp/elifdef-2.C: New test.
> * g++.dg/cpp/elifdef-3.C: New test.
>
> --- libcpp/init.c.jj 2021-09-02 10:01:15.954715595 +0200
> +++ libcpp/init.c 2021-10-05 09:55:15.010620700 +0200
> @@ -122,8 +122,8 @@ static const struct lang_flags lang_defa
> /* CXX17 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0 },
> /* GNUCXX20 */ { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0 },
> /* CXX20 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0 },
> - /* GNUCXX23 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
> - /* CXX23 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
> + /* GNUCXX23 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 },
> + /* CXX23 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 },
> /* ASM */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> };
>
> --- gcc/testsuite/g++.dg/cpp/elifdef-1.C.jj 2021-10-05 10:00:41.410057024 +0200
> +++ gcc/testsuite/g++.dg/cpp/elifdef-1.C 2021-10-05 10:00:33.110173069 +0200
> @@ -0,0 +1,3 @@
> +// { dg-do preprocess { target { ! c++23 } } }
> +
> +#include "../../gcc.dg/cpp/c11-elifdef-1.c"
> --- gcc/testsuite/g++.dg/cpp/elifdef-2.C.jj 2021-10-05 10:01:30.345372808 +0200
> +++ gcc/testsuite/g++.dg/cpp/elifdef-2.C 2021-10-05 10:03:36.560608083 +0200
> @@ -0,0 +1,4 @@
> +// P2334R1
> +// { dg-do preprocess { target c++23 } }
> +
> +#include "../../gcc.dg/cpp/c2x-elifdef-1.c"
> --- gcc/testsuite/g++.dg/cpp/elifdef-3.C.jj 2021-10-05 10:01:36.029293338 +0200
> +++ gcc/testsuite/g++.dg/cpp/elifdef-3.C 2021-10-05 10:03:48.896435601 +0200
> @@ -0,0 +1,4 @@
> +// P2334R1
> +// { dg-do preprocess { target c++23 } }
> +
> +#include "../../gcc.dg/cpp/c2x-elifdef-1.c"
>
> Jakub
>
Marek
On Tue, 5 Oct 2021, Jakub Jelinek via Gcc-patches wrote:
> One is in the patch below, ignores that sentence and only implements it
> for -std=c++23/-std=gnu++23 like it is only implemented for -std=c23.
> Another option would be to implement it also in the older GNU modes but
> not in the C/CXX modes (but it would be strange if we did that just for
> C++ and not for C).
> Yet another option is to enable it unconditionally.
> And yet another option would be to enable it unconditionally but emit
> a warning (or pedwarn) when it is seen.
> Note, when it is enabled for the older language modes, as Joseph wrote
> in the c11-elifdef-1.c testcase, it can result e.g. in rejecting previously
> valid code:
It would probably be reasonable to enable it in older GNU modes for C as
well as C++ if desired (and, in that case, emit a pedwarn-if-pedantic when
it's acted on) - cases where it affects compatibility should be rare.
Enabling with a pedwarn in strict modes is problematic because it changes
semantics of valid code where it was inside #if 0, however. It doesn't
make sense at all to me to think of a new feature like this (one with no
prior art in C mentioned in the WG14 proposal) as a defect fix.
Any normal directive - i.e. one that has no effect on the preprocessor #if
structure and so is ignored inside #if 0 for all language versions - can
more reasonably be enabled for all language versions with a pedwarn when
used for old versions. (In particular, that will be appropriate for
#warning, where the "don't pedwarn in C2X modes" part needs implementing
after N2686 was accepted at the August / September WG14 meeting - I don't
know if C++ is doing anything with #warning.)
@@ -122,8 +122,8 @@ static const struct lang_flags lang_defa
/* CXX17 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0 },
/* GNUCXX20 */ { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0 },
/* CXX20 */ { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0 },
- /* GNUCXX23 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
- /* CXX23 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
+ /* GNUCXX23 */ { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 },
+ /* CXX23 */ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 },
/* ASM */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
@@ -0,0 +1,3 @@
+// { dg-do preprocess { target { ! c++23 } } }
+
+#include "../../gcc.dg/cpp/c11-elifdef-1.c"
@@ -0,0 +1,4 @@
+// P2334R1
+// { dg-do preprocess { target c++23 } }
+
+#include "../../gcc.dg/cpp/c2x-elifdef-1.c"
@@ -0,0 +1,4 @@
+// P2334R1
+// { dg-do preprocess { target c++23 } }
+
+#include "../../gcc.dg/cpp/c2x-elifdef-1.c"