Re: [PATCH] PR125533 fortran/libcpp: CPP stringify operator emits \" for double-quotes in Fortran mode
Harald Anlauf <[email protected]> Sun, 7 Jun 2026 20:44:47 +0200
| Newsgroups | gmane.comp.gcc.patches,gmane.comp.gcc.fortran |
|---|---|
| Message-ID | <[email protected]> |
Hi Jerry,
the Fortran parts are trivially OK, so someone else knowing libcpp
should approve it.
Thanks,
Harald
Am 05.06.26 um 9:47 PM schrieb Jerry D:
> 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.
> ---