A naïve Fortran user would think that if the thing being stringified contains a quote but not an apostrophe, it should use quotes as the delimiter.
i.e. CPP_STRINGIFY(key .eq. "x") should produce 'key .eq. "x"' It's not wrong to blindly double the quotes, though. Just better to use apostrophes in some cases. Cheers, -- ..................Malcolm Cohen, NAG Oxford/Tokyo. -----Original Message----- From: Jerry D <[email protected]> Sent: Thursday, June 11, 2026 8:36 AM To: Jason Merrill <[email protected]>; Malcolm Cohen <[email protected]> Cc: gfortran <[email protected]> Subject: Fwd: [PATCH] PR125533 fortran/libcpp: CPP stringify operator emits \" for double-quotes in Fortran mode Jason and Malcolm, Can either of you review the attached patch. It touches on libcpp and fixes a sneaky glitch for gfortran. Much appreciated, Jerry -------- Forwarded Message -------- Subject: [PATCH] PR125533 fortran/libcpp: CPP stringify operator emits \" for double-quotes in Fortran mode Date: Fri, 5 Jun 2026 12:47:12 -0700 From: Jerry D <[email protected]> To: gfortran <[email protected]>, gcc-patches <[email protected]> CC: Jakub Jelinek <[email protected]> See attached patch. This one was a surprise and a subtle problem. See the explanation in the commit message. Since this touches on libcpp I am not sure who else should review this. Regression tested on x86_64. OK for mainline and backport to 16 later. Regards, Jerry --- The traditional-mode CPP stringification operator escaped double-quote characters in macro argument tokens with a backslash (e.g. CPP_STRINGIFY(key .eq. "x") expanded to "key .eq. \"x\""). This is correct for C but not valid Fortran: Fortran represents an embedded double-quote with a doubled quote (""), not a backslash-quote (\"). The backslash-escaped output was rejected by the Fortran scanner with a spurious syntax error. Fix: add a new flag fortran_string_escaping to struct cpp_options. When set, traditional.cc emits a doubled quote instead of a backslash-quote when escaping a double-quote character inside a stringified argument, and suppresses the extra backslash before a literal backslash in that context. Set the flag in gfc_cpp_post_options, which configures the CPP reader for Fortran. PR fortran/125533 Assisted by: Claude Sonnet 4.6 gcc/fortran/ChangeLog: * cpp.cc (gfc_cpp_post_options): Set fortran_string_escaping in cpp_options so traditional-mode stringification uses doubled-quote escaping instead of backslash-quote. gcc/testsuite/ChangeLog: * gfortran.dg/cpp_stringify_quote_1.F90: New test. libcpp/ChangeLog: * include/cpplib.h (cpp_options): Add fortran_string_escaping flag. * traditional.cc (replace_args_and_push): When fortran_string_escaping is set, escape double-quote with a doubled quote rather than a backslash; suppress the extra backslash before a backslash inside a quoted argument. ---
