From 2c5bd8e9fb16584cdd1d090b9a5a63dd9140b61b Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Thu, 7 Aug 2025 08:51:00 +0200 Subject: [PATCH] c++: Add testcase for CWG2576 [PR120778] Another part of the C++26 P2843R3, this time in https://eel.is/c++draft/cpp.include#7 changing former (compile time) undefined behavior to IFNDR. With IFNDR we wouldn't have to test anything I guess, but this still adds the cases from the 3.4 section of the paper plus related tests. The paper wonders about implementation divergence between most compilers and gcc in the case of glueing tokens together with <> around from multiple macros, GCC doesn't add CPP_PADDING macros in that case (and even if it would, it would ignore them through get_token_no_padding). But I think this behavior is allowed by https://eel.is/c++draft/cpp.include#7.sentence-3 and changing it now after 25+ years of such behavior might break real-world stuff, especially making it really hard to construct the <> includes from some name and adding < and > from some other macro around that. We handle #define A #include A like clang++ and try to open 'cstd def' file. But not adding spaces for all the padding means handling even stuff where nothing else can help, while for cstd def coming from different macros one can use token pasting to combine them together, e.g. < and following identifier or identifier followed by . or . followed by identifier or identifier followed by > can't be pasted together. 2025-08-07 Jakub Jelinek PR preprocessor/120778 * g++.dg/DRs/dr2576.C: New test. --- gcc/testsuite/g++.dg/DRs/dr2576.C | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 gcc/testsuite/g++.dg/DRs/dr2576.C diff --git a/gcc/testsuite/g++.dg/DRs/dr2576.C b/gcc/testsuite/g++.dg/DRs/dr2576.C new file mode 100644 index 00000000000..ed53a082033 --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2576.C @@ -0,0 +1,47 @@ +// DR 2576 - Undefined behavior with macro-expanded #include directives +// { dg-do preprocess } +// { dg-options "-pedantic-errors" } + +#define A +#include A +#define B "cstddef" +#include B +#define C(x) #x +#define D(x) C(x) +#include D(cstddef) +#include "cstddef" "" // { dg-error "extra tokens at end of '#include' directive" } +#include "cstddef"".h" // { dg-error "extra tokens at end of '#include' directive" } +#include // { dg-error "'#include' expects '\"FILENAME\"' or ''" } +#include E // { dg-error "'#include' expects '\"FILENAME\"' or ''" } +#include ' character" "" { target *-*-* } .-1 } +#include "cstddef + // { dg-error "missing terminating \" character" "" { target *-*-* } .-1 } + // { dg-error "'#include' expects '\"FILENAME\"' or ''" "" { target *-*-* } .-2 } +#define F cstddef +#include F // { dg-error "'#include' expects '\"FILENAME\"' or ''" } +// There is implementation divergence on the following cases (G H through M N) +// between e.g. GCC and clang++. clang++ fails on trying to include ' cstddef' +// and 'cstd def' and 'stddef .h' and 'cstddef ' headers. +// https://eel.is/c++draft/cpp.include#7.sentence-3 makes the whitespace +// handling implementation defined and the way GCC handles it can allow +// certain use cases which aren't otherwise possible. One can still +// insert spaces into the <> filenames if it is from the same macro. +#define G < +#define H cstddef> +#include G H +#define I +#include I J +#define K +#include K L +#define M +#include M N +#define O +#include O // { dg-error "extra tokens at end of '#include' directive" } +#define P "cstddef" "" +#include P // { dg-error "extra tokens at end of '#include' directive" } +#define Q "cstddef"".h" +#include Q // { dg-error "extra tokens at end of '#include' directive" } -- 2.47.2