[gcc r16-6285] fortran: Fix bogus warning with -cpp -fpreprocessed [PR92613]
Harald Anlauf
anlauf@gcc.gnu.org
Fri Dec 19 17:30:02 GMT 2025
https://gcc.gnu.org/g:15ffee4e129937c07190bc2ce059470bbd8068ae
commit r16-6285-g15ffee4e129937c07190bc2ce059470bbd8068ae
Author: Christopher Albert <albert@tugraz.at>
Date: Thu Dec 4 14:08:10 2025 +0100
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 <albert@tugraz.at>
Diff:
---
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(-)
diff --git a/gcc/fortran/cpp.cc b/gcc/fortran/cpp.cc
index 109dc587e7bc..f946fae9e1bd 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 06ffc67357bd..cf91be36ba32 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 d0a3b7aeedea..ccfa9516370a 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 cd9c52835156..2507de666f6f 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 000000000000..78b462385d15
--- /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 000000000000..8706a1dc8e65
--- /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
More information about the Gcc-cvs
mailing list