https://gcc.gnu.org/g:ab7c16990fca6b456156895d8d0d5e1a37476d38
commit r16-2776-gab7c16990fca6b456156895d8d0d5e1a37476d38 Author: Jakub Jelinek <ja...@redhat.com> Date: Tue Aug 5 08:21:55 2025 +0200 libcpp: Add testcase for CWG2579 [PR120778] Another easy part from the paper. Part of the CWG2579 has been already done in an earlier paper (with test commits by Marek) and the remaining part is implemented correctly, we diagnose as error when token pasting doesn't form a valid token. Except that message pasting """" and """" does not give a valid preprocessing token looked weird and so I've updated the message to use %< and %> instead of \" quoting. 2025-08-05 Jakub Jelinek <ja...@redhat.com> PR preprocessor/120778 * macro.cc (paste_tokens): Use %< and %> instead of \" in diagnostics around %.*s. * g++.dg/DRs/dr2579.C: New test. * c-c++-common/cpp/va-opt-6.c: Expect ' rather than \" around tokens in incorrect pasting diagnostics. * gcc.dg/c23-attr-syntax-6.c: Likewise. * gcc.dg/cpp/paste12.c: Likewise. * gcc.dg/cpp/paste12-2.c: Likewise. * gcc.dg/cpp/paste14.c: Likewise. * gcc.dg/cpp/paste14-2.c: Likewise. Diff: --- gcc/testsuite/c-c++-common/cpp/va-opt-6.c | 10 +++++----- gcc/testsuite/g++.dg/DRs/dr2579.C | 9 +++++++++ gcc/testsuite/gcc.dg/c23-attr-syntax-6.c | 4 ++-- gcc/testsuite/gcc.dg/cpp/paste12-2.c | 2 +- gcc/testsuite/gcc.dg/cpp/paste12.c | 2 +- gcc/testsuite/gcc.dg/cpp/paste14-2.c | 4 ++-- gcc/testsuite/gcc.dg/cpp/paste14.c | 4 ++-- libcpp/macro.cc | 2 +- 8 files changed, 23 insertions(+), 14 deletions(-) diff --git a/gcc/testsuite/c-c++-common/cpp/va-opt-6.c b/gcc/testsuite/c-c++-common/cpp/va-opt-6.c index 8a7761b39def..9a924318cb59 100644 --- a/gcc/testsuite/c-c++-common/cpp/va-opt-6.c +++ b/gcc/testsuite/c-c++-common/cpp/va-opt-6.c @@ -3,15 +3,15 @@ /* { dg-options "-std=c++20" { target c++ } } */ #define a "" -#define b(...) a ## #__VA_OPT__(1) /* { dg-error "pasting \"a\" and \"\"\"\" does not give a valid preprocessing token" } */ -#define c(...) a ## #__VA_OPT__(1) /* { dg-error "pasting \"a\" and \"\"1\"\" does not give a valid preprocessing token" } */ +#define b(...) a ## #__VA_OPT__(1) /* { dg-error "pasting 'a' and '\"\"' does not give a valid preprocessing token" } */ +#define c(...) a ## #__VA_OPT__(1) /* { dg-error "pasting 'a' and '\"1\"' does not give a valid preprocessing token" } */ #define d(...) #__VA_OPT__(1) ## ! #define e(...) #__VA_OPT__(1) ## ! #define f(...) #__VA_OPT__(. ## !) #define g(...) #__VA_OPT__(. ## !) b() c(1) -d( ) /* { dg-error "pasting \"\"\"\" and \"!\" does not give a valid preprocessing token" } */ -e( 1 ) /* { dg-error "pasting \"\"1\"\" and \"!\" does not give a valid preprocessing token" } */ +d( ) /* { dg-error "pasting '\"\"' and '!' does not give a valid preprocessing token" } */ +e( 1 ) /* { dg-error "pasting '\"1\"' and '!' does not give a valid preprocessing token" } */ f() -g(0) /* { dg-error "pasting \".\" and \"!\" does not give a valid preprocessing token" } */ +g(0) /* { dg-error "pasting '.' and '!' does not give a valid preprocessing token" } */ diff --git a/gcc/testsuite/g++.dg/DRs/dr2579.C b/gcc/testsuite/g++.dg/DRs/dr2579.C new file mode 100644 index 000000000000..dda3643826df --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2579.C @@ -0,0 +1,9 @@ +// DR 2579 - Undefined behavior when token pasting does not create a preprocessing token +// { dg-do preprocess } +// { dg-options "-pedantic-errors" } + +#define A(a, b) a ## b +A(5,6) +A(-,32) // { dg-error "pasting '-' and '32' does not give a valid preprocessing token" } +A("","") // { dg-error "pasting '\"\"' and '\"\"' does not give a valid preprocessing token" } +A(\,u0393) diff --git a/gcc/testsuite/gcc.dg/c23-attr-syntax-6.c b/gcc/testsuite/gcc.dg/c23-attr-syntax-6.c index f8c5b0facff9..4e6b80b76395 100644 --- a/gcc/testsuite/gcc.dg/c23-attr-syntax-6.c +++ b/gcc/testsuite/gcc.dg/c23-attr-syntax-6.c @@ -45,7 +45,7 @@ typedef int [[__extension__ __extension__]] b2; /* { dg-error {'extension' attri typedef int [[__extension__ unknown_attribute]] b3; /* { dg-error {'unknown_attribute' attribute ignored} } */ typedef int [[__extension__ gnu:vector_size(4)]] b4; /* { dg-error {expected '\]' before ':'} } */ /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-1 } */ -typedef int [[__extension__ gnu JOIN2(:,:) vector_size (4)]] b5; /* { dg-error {pasting ":" and ":" does not give a valid preprocessing token} } */ +typedef int [[__extension__ gnu JOIN2(:,:) vector_size (4)]] b5; /* { dg-error {pasting ':' and ':' does not give a valid preprocessing token} } */ /* { dg-error {expected '\]' before ':'} "" { target *-*-* } .-1 } */ /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-2 } */ typedef int [[__extension__ gnu : : vector_size (4)]] b6; /* { dg-error {expected '\]' before ':'} } */ @@ -81,7 +81,7 @@ typedef int [[gnu :: vector_size (4)]] b18; /* { dg-error {attributes before C23 typedef int [[gnu FOO vector_size (4)]] b19; /* { dg-error {attributes before C23} } */ typedef int [[gnu :: vector_size (sizeof (void (*)(...)))]] b20; /* { dg-error {attributes before C23} } */ /* { dg-error {requires a named argument before} "" { target *-*-* } .-1 } */ -typedef int [[gnu JOIN2(:,:) vector_size (4)]] b21; /* { dg-error {pasting ":" and ":" does not give a valid preprocessing token} } */ +typedef int [[gnu JOIN2(:,:) vector_size (4)]] b21; /* { dg-error {pasting ':' and ':' does not give a valid preprocessing token} } */ /* { dg-error {expected '\]' before ':'} "" { target *-*-* } .-1 } */ /* { dg-error {'gnu' attribute ignored} "" { target *-*-* } .-2 } */ /* { dg-error {attributes before C23} "" { target *-*-* } .-3 } */ diff --git a/gcc/testsuite/gcc.dg/cpp/paste12-2.c b/gcc/testsuite/gcc.dg/cpp/paste12-2.c index 6e2e4f1057d7..f46645a96136 100644 --- a/gcc/testsuite/gcc.dg/cpp/paste12-2.c +++ b/gcc/testsuite/gcc.dg/cpp/paste12-2.c @@ -6,6 +6,6 @@ /* Test correct diagnostics when pasting in #include. Source: PR preprocessor/6780. */ -#define inc2(a,b) <##a.b> /* { dg-error "pasting \"<\" and \"stdio\" does not" } */ +#define inc2(a,b) <##a.b> /* { dg-error "pasting '<' and 'stdio' does not" } */ #define INC(X) inc2(X,h) #include INC(stdio) diff --git a/gcc/testsuite/gcc.dg/cpp/paste12.c b/gcc/testsuite/gcc.dg/cpp/paste12.c index 3e0f7b959222..f6b369695f80 100644 --- a/gcc/testsuite/gcc.dg/cpp/paste12.c +++ b/gcc/testsuite/gcc.dg/cpp/paste12.c @@ -8,4 +8,4 @@ #define inc2(a,b) <##a.b> #define INC(X) inc2(X,h) -#include INC(stdio) /* { dg-error "pasting \"<\" and \"stdio\" does not" } */ +#include INC(stdio) /* { dg-error "pasting '<' and 'stdio' does not" } */ diff --git a/gcc/testsuite/gcc.dg/cpp/paste14-2.c b/gcc/testsuite/gcc.dg/cpp/paste14-2.c index 3b23ada3d215..bb5199949855 100644 --- a/gcc/testsuite/gcc.dg/cpp/paste14-2.c +++ b/gcc/testsuite/gcc.dg/cpp/paste14-2.c @@ -4,8 +4,8 @@ { dg-do preprocess } */ -#define foo - ## >> /* { dg-error "pasting \"-\" and \">>\"" } */ +#define foo - ## >> /* { dg-error "pasting '-' and '>>'" } */ foo -#define bar = ## == /* { dg-error "pasting \"=\" and \"==\"" } */ +#define bar = ## == /* { dg-error "pasting '=' and '=='" } */ bar diff --git a/gcc/testsuite/gcc.dg/cpp/paste14.c b/gcc/testsuite/gcc.dg/cpp/paste14.c index 043d5e5804ff..d60b3287bf00 100644 --- a/gcc/testsuite/gcc.dg/cpp/paste14.c +++ b/gcc/testsuite/gcc.dg/cpp/paste14.c @@ -5,6 +5,6 @@ */ #define foo - ## >> -foo /* { dg-error "pasting \"-\" and \">>\"" } */ +foo /* { dg-error "pasting '-' and '>>'" } */ #define bar = ## == -bar /* { dg-error "pasting \"=\" and \"==\"" } */ +bar /* { dg-error "pasting '=' and '=='" } */ diff --git a/libcpp/macro.cc b/libcpp/macro.cc index d869b0239350..158c8215e522 100644 --- a/libcpp/macro.cc +++ b/libcpp/macro.cc @@ -1071,7 +1071,7 @@ paste_tokens (cpp_reader *pfile, location_t location, /* Mandatory error for all apart from assembler. */ if (CPP_OPTION (pfile, lang) != CLK_ASM) cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, - "pasting \"%.*s\" and \"%.*s\" does not give " + "pasting %<%.*s%> and %<%.*s%> does not give " "a valid preprocessing token", (int) (lhsend - buf), buf, (int) (end - rhsstart), rhsstart);