Proposed patch for PR 92613:

With -fpreprocessed, gfortran skips the C
preprocessor, avoiding spurious diagnostics about characters in Fortran
comments.
>From 26fa99e5c6d2bc0c528df4d5c37c099aeddf807f Mon Sep 17 00:00:00 2001
From: Christopher Albert <[email protected]>
Date: Thu, 4 Dec 2025 14:08:10 +0100
Subject: [PATCH] fortran: Fix bogus warning with -cpp -fpreprocessed [PR92613]

With -fpreprocessed, gfortran skips the C preprocessor, avoiding
spurious diagnostics about characters in Fortran comments.

	PR fortran/92613

gcc/fortran/ChangeLog:

	* cpp.cc (gfc_cpp_preprocess): Skip libcpp for -fpreprocessed.
	Error out for -fpreprocessed -E.
	* f95-lang.cc (gfc_init): Skip libcpp init for -fpreprocessed.
	* scanner.cc (gfc_new_file): Load source directly if preprocessed.
	* invoke.texi (Preprocessing Options): Document -fpreprocessed.

gcc/testsuite/ChangeLog:

	* gfortran.dg/pr92613.f90: New test.
	* gfortran.dg/pr92613_2.f90: New test for -E error.

Signed-off-by: Christopher Albert <[email protected]>
---
 gcc/fortran/cpp.cc                      |  7 +++++++
 gcc/fortran/f95-lang.cc                 |  4 ++--
 gcc/fortran/invoke.texi                 |  4 ++++
 gcc/fortran/scanner.cc                  | 10 +++++++---
 gcc/testsuite/gfortran.dg/pr92613.f90   | 13 +++++++++++++
 gcc/testsuite/gfortran.dg/pr92613_2.f90 | 11 +++++++++++
 6 files changed, 44 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr92613.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pr92613_2.f90

diff --git a/gcc/fortran/cpp.cc b/gcc/fortran/cpp.cc
index 15ecc7dd5fa..b88b541b919 100644
--- a/gcc/fortran/cpp.cc
+++ b/gcc/fortran/cpp.cc
@@ -679,6 +679,13 @@ gfc_cpp_preprocess (const char *source_file)
   if (!gfc_cpp_enabled ())
     return false;
 
+  if (gfc_option.flag_preprocessed)
+    {
+      if (gfc_cpp_preprocess_only ())
+	gfc_fatal_error ("%<-E%> is not supported with %<-fpreprocessed%>");
+      return false;
+    }
+
   cpp_change_file (cpp_in, LC_RENAME, source_file);
 
   if (cpp_option->traditional)
diff --git a/gcc/fortran/f95-lang.cc b/gcc/fortran/f95-lang.cc
index 06ffc67357b..cf91be36ba3 100644
--- a/gcc/fortran/f95-lang.cc
+++ b/gcc/fortran/f95-lang.cc
@@ -274,7 +274,7 @@ gfc_be_parse_file (void)
 static bool
 gfc_init (void)
 {
-  if (!gfc_cpp_enabled ())
+  if (!gfc_cpp_enabled () || gfc_option.flag_preprocessed)
     {
       linemap_add (line_table, LC_ENTER, false, gfc_source_file, 1);
       linemap_add (line_table, LC_RENAME, false, special_fname_builtin (), 0);
@@ -285,7 +285,7 @@ gfc_init (void)
   gfc_init_decl_processing ();
   gfc_static_ctors = NULL_TREE;
 
-  if (gfc_cpp_enabled ())
+  if (gfc_cpp_enabled () && !gfc_option.flag_preprocessed)
     gfc_cpp_init ();
 
   gfc_init_1 ();
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index a65f2d1cc34..ef09996937d 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -647,6 +647,10 @@ invoke the preprocessor on any file, use @option{-cpp}, to disable
 preprocessing on files where the preprocessor is run automatically, use
 @option{-nocpp}.
 
+When compiling a preprocessed file, use @option{-fpreprocessed}
+(@pxref{Preprocessor Options,,Options Controlling the Preprocessor,gcc,
+Using the GNU Compiler Collection (GCC)}).  This skips the C preprocessor.
+
 If a preprocessed file includes another file with the Fortran @code{INCLUDE}
 statement, the included file is not preprocessed.  To preprocess included
 files, use the equivalent preprocessor statement @code{#include}.
diff --git a/gcc/fortran/scanner.cc b/gcc/fortran/scanner.cc
index cd9c5283515..2507de666f6 100644
--- a/gcc/fortran/scanner.cc
+++ b/gcc/fortran/scanner.cc
@@ -2775,9 +2775,13 @@ gfc_new_file (void)
 
   if (gfc_cpp_enabled ())
     {
-      gfc_cpp_preprocess (gfc_source_file);
-      if (!gfc_cpp_preprocess_only ())
-	load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
+      if (gfc_cpp_preprocess (gfc_source_file))
+	{
+	  if (!gfc_cpp_preprocess_only ())
+	    load_file (gfc_cpp_temporary_file (), gfc_source_file, true);
+	}
+      else
+	load_file (gfc_source_file, NULL, true);
     }
   else
     load_file (gfc_source_file, NULL, true);
diff --git a/gcc/testsuite/gfortran.dg/pr92613.f90 b/gcc/testsuite/gfortran.dg/pr92613.f90
new file mode 100644
index 00000000000..78b462385d1
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr92613.f90
@@ -0,0 +1,13 @@
+# 1 "<test>"
+# 1 "<built-in>"
+# 1 "<command-line>"
+# 1 "<test>"
+! PR fortran/92613
+! { dg-do compile }
+! { dg-options "-cpp -fpreprocessed" }
+program test
+  implicit none
+  write(6,*) 'hello'
+! Comment with apostrophe: it's good!
+! Comment with double quote: "quoted"
+end program
diff --git a/gcc/testsuite/gfortran.dg/pr92613_2.f90 b/gcc/testsuite/gfortran.dg/pr92613_2.f90
new file mode 100644
index 00000000000..8706a1dc8e6
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr92613_2.f90
@@ -0,0 +1,11 @@
+# 1 "<test>"
+# 1 "<built-in>"
+# 1 "<command-line>"
+# 1 "<test>"
+! PR fortran/92613
+! { dg-do preprocess }
+! { dg-options "-cpp -fpreprocessed" }
+! { dg-error ".-E. is not supported with .-fpreprocessed." "" { target *-*-* } 0 }
+! { dg-prune-output "compilation terminated" }
+program test
+end program
-- 
2.52.0

Reply via email to