]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fortran: Fix bogus warning with -cpp -fpreprocessed [PR92613]
authorChristopher Albert <albert@tugraz.at>
Thu, 4 Dec 2025 13:08:10 +0000 (14:08 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 19 Dec 2025 17:29:36 +0000 (18:29 +0100)
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>
gcc/fortran/cpp.cc
gcc/fortran/f95-lang.cc
gcc/fortran/invoke.texi
gcc/fortran/scanner.cc
gcc/testsuite/gfortran.dg/pr92613.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/pr92613_2.f90 [new file with mode: 0644]

index 109dc587e7bc75ba880b8e815d1fbe1735364cb2..f946fae9e1bd6cb5602319aed4969a41a1f0ae51 100644 (file)
@@ -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)
index 06ffc67357bdd0b897a5e4d0b4b9fd10f26abd4c..cf91be36ba32400bb16c0703a4ef0f0e1bbb98c8 100644 (file)
@@ -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 ();
index d0a3b7aeedeaaadca48163af5993886e8f937a49..ccfa9516370a7df7c79b0999fd9708d6dc45cdff 100644 (file)
@@ -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}.
index cd9c528351566d82c6a88558ac3de2c710eb6199..2507de666f6f2df100a69169b02acc5087bac898 100644 (file)
@@ -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 (file)
index 0000000..78b4623
--- /dev/null
@@ -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 (file)
index 0000000..8706a1d
--- /dev/null
@@ -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