Hi!

This is another case which changed from compile time undefined behavior
to ill-formed, diagnostic required.  Now, we warn on this, so pedantically
that is good enough, maybe all we need is a testcase, but the following
patch changes it to a pedwarn for C++26.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Or do you prefer a warning everywhere and just add a testcase?

2025-08-02  Jakub Jelinek  <ja...@redhat.com>

        PR preprocessor/120778
        * macro.cc (stringify_arg): For C++26 emit a pedarn instead of warning
        for \ at the end of stringification.

        * g++.dg/DRs/dr2578.C: New test.

--- libcpp/macro.cc.jj  2025-08-01 15:55:51.131693172 +0200
+++ libcpp/macro.cc     2025-08-01 17:49:10.717143946 +0200
@@ -1003,7 +1003,10 @@ stringify_arg (cpp_reader *pfile, const
   /* Ignore the final \ of invalid string literals.  */
   if (backslash_count & 1)
     {
-      cpp_error (pfile, CPP_DL_WARNING,
+      cpp_error (pfile,
+                CPP_OPTION (pfile, cplusplus)
+                && CPP_OPTION (pfile, lang) >= CLK_GNUCXX26
+                ? CPP_DL_PEDWARN : CPP_DL_WARNING,
                 "invalid string literal, ignoring final %<\\%>");
       dest--;
     }
--- gcc/testsuite/g++.dg/DRs/dr2578.C.jj        2025-08-01 18:03:58.297808256 
+0200
+++ gcc/testsuite/g++.dg/DRs/dr2578.C   2025-08-01 18:13:01.174877937 +0200
@@ -0,0 +1,10 @@
+// DR 2578 - Undefined behavior when creating an invalid string literal via 
stringizing
+// { dg-do preprocess }
+// { dg-options "-pedantic-errors" }
+
+#define A(a) #a
+#define B(a) A(a)
+#define C \\
+
+const char *x = B(C);  // { dg-warning "invalid string literal, ignoring final 
'\\\\'" "" { target c++23_down } }
+// { dg-error "invalid string literal, ignoring final '\\\\'" "" { target 
c++26 }  .-1 }

        Jakub

Reply via email to