[PATCH] PR125533 fortran/libcpp: CPP stringify operator emits \" for double-quotes in Fortran mode

Jerry D jvdelisle2@gmail.com
Sat Jun 13 23:51:05 GMT 2026


On 6/11/26 8:58 AM, malcolm@nag-j.co.jp wrote:
> 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.
> 

I appreciate the feedback. The patch is safe and follows a principle of least 
surprise. In this case, before the patch, all code appeared to be fortran 
conforming and gave the appearance that there was a problem somewhere else.

I need to know if the patch is approved or not? If not, is there a concern that 
I not understand and if so, what?

Regards,

Jerry

> Cheers,
> --
> ..................Malcolm Cohen, NAG Oxford/Tokyo.
> 
> -----Original Message-----
> From: Jerry D <jvdelisle2@gmail.com>
> Sent: Thursday, June 11, 2026 8:36 AM
> To: Jason Merrill <jason@redhat.com>; Malcolm Cohen <malcolm@nag-j.co.jp>
> Cc: gfortran <fortran@gcc.gnu.org>
> 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 <jvdelisle2@gmail.com>
> To: gfortran <fortran@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>
> CC: Jakub Jelinek <jakub@redhat.com>
> 
> 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.
> ---
> 



More information about the Fortran mailing list