[flang-commits] [clang] [flang] [flang] Align `-x f95[-cpp-input]` language modes with `gfortran` (PR #127986)

IƱaki Amatria Barral via flang-commits flang-commits at lists.llvm.org
Thu Feb 20 02:23:06 PST 2025


https://github.com/inaki-amatria created https://github.com/llvm/llvm-project/pull/127986

This PR may solve some of the issues described in: https://github.com/llvm/llvm-project/issues/127617.

The reason to revert https://github.com/llvm/llvm-project/commit/81d82cac8c4cbd006bf991fd7380de2d72858d1c is detailed in the associated issue.

>From e76692d226487b4b5c6c18caedd2ee5d9a86d42b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?I=C3=B1aki=20Amatria=20Barral?= <inaki.amatria at appentra.com>
Date: Thu, 20 Feb 2025 10:40:22 +0100
Subject: [PATCH 1/2] Revert "[flang] Treat pre-processed input as fixed
 (#117563)"

This reverts commit 81d82cac8c4cbd006bf991fd7380de2d72858d1c.
---
 clang/lib/Driver/ToolChains/Flang.cpp |  7 -------
 flang/test/Driver/pp-fixed-form.f90   | 19 -------------------
 2 files changed, 26 deletions(-)
 delete mode 100644 flang/test/Driver/pp-fixed-form.f90

diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 9ad795edd724d..bb308c45eb64b 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -803,13 +803,6 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
 
   addFortranDialectOptions(Args, CmdArgs);
 
-  // 'flang -E' always produces output that is suitable for use as fixed form
-  // Fortran. However it is only valid free form source if the original is also
-  // free form.
-  if (InputType == types::TY_PP_Fortran &&
-      !Args.getLastArg(options::OPT_ffixed_form, options::OPT_ffree_form))
-    CmdArgs.push_back("-ffixed-form");
-
   handleColorDiagnosticsArgs(D, Args, CmdArgs);
 
   // LTO mode is parsed by the Clang driver library.
diff --git a/flang/test/Driver/pp-fixed-form.f90 b/flang/test/Driver/pp-fixed-form.f90
deleted file mode 100644
index 4695da78763ae..0000000000000
--- a/flang/test/Driver/pp-fixed-form.f90
+++ /dev/null
@@ -1,19 +0,0 @@
-!RUN: %flang -save-temps -### %S/Inputs/free-form-test.f90  2>&1 | FileCheck %s --check-prefix=FREE
-FREE:       "-fc1" {{.*}} "-o" "free-form-test.i" {{.*}} "-x" "f95-cpp-input" "{{.*}}/free-form-test.f90"
-FREE-NEXT:  "-fc1" {{.*}} "-ffixed-form" {{.*}} "-x" "f95" "free-form-test.i"
-
-!RUN: %flang -save-temps -### %S/Inputs/fixed-form-test.f  2>&1 | FileCheck %s --check-prefix=FIXED
-FIXED:      "-fc1" {{.*}} "-o" "fixed-form-test.i" {{.*}} "-x" "f95-cpp-input" "{{.*}}/fixed-form-test.f"
-FIXED-NEXT: "-fc1" {{.*}} "-ffixed-form" {{.*}} "-x" "f95" "fixed-form-test.i"
-
-!RUN: %flang -save-temps -### -ffree-form %S/Inputs/free-form-test.f90  2>&1 | FileCheck %s --check-prefix=FREE-FLAG
-FREE-FLAG:           "-fc1" {{.*}} "-o" "free-form-test.i" {{.*}} "-x" "f95-cpp-input" "{{.*}}/free-form-test.f90"
-FREE-FLAG-NEXT:      "-fc1" {{.*}} "-emit-llvm-bc" "-ffree-form"
-FREE-FLAG-NOT:       "-ffixed-form"
-FREE-FLAG-SAME:      "-x" "f95" "free-form-test.i"
-
-!RUN: %flang -save-temps -### -ffixed-form %S/Inputs/fixed-form-test.f  2>&1 | FileCheck %s --check-prefix=FIXED-FLAG
-FIXED-FLAG:          "-fc1" {{.*}} "-o" "fixed-form-test.i" {{.*}} "-x" "f95-cpp-input" "{{.*}}/fixed-form-test.f"
-FIXED-FLAG-NEXT:     "-fc1" {{.*}} "-emit-llvm-bc" "-ffixed-form"
-FIXED-FLAG-NOT:      "-ffixed-form"
-FIXED-FLAG-SAME:     "-x" "f95" "fixed-form-test.i"

>From a989adb98ba03742c0615a526d630b4a225503e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?I=C3=B1aki=20Amatria=20Barral?= <inaki.amatria at appentra.com>
Date: Thu, 20 Feb 2025 11:14:19 +0100
Subject: [PATCH 2/2] [flang] Align `-x f95-cpp-input` with `gfortran`

---
 flang/lib/Frontend/CompilerInvocation.cpp     |  6 ++++
 flang/test/Driver/dash-x-f95.f                | 35 +++++++++++++++++++
 .../input-from-stdin/input-from-stdin.f90     |  2 +-
 3 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Driver/dash-x-f95.f

diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index f3d9432c62d3b..1b68dee8b169d 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -859,6 +859,12 @@ static void parsePreprocessorArgs(Fortran::frontend::PreprocessorOptions &opts,
         (currentArg->getOption().matches(clang::driver::options::OPT_cpp))
             ? PPMacrosFlag::Include
             : PPMacrosFlag::Exclude;
+  // Enable -cpp based on -x unless explicitly disabled with -nocpp
+  if (const auto *dashX = args.getLastArg(clang::driver::options::OPT_x);
+      dashX && opts.macrosFlag != PPMacrosFlag::Exclude)
+    opts.macrosFlag = llvm::StringSwitch<PPMacrosFlag>(dashX->getValue())
+                          .Case("f95-cpp-input", PPMacrosFlag::Include)
+                          .Default(opts.macrosFlag);
 
   opts.noReformat = args.hasArg(clang::driver::options::OPT_fno_reformat);
   opts.preprocessIncludeLines =
diff --git a/flang/test/Driver/dash-x-f95.f b/flang/test/Driver/dash-x-f95.f
new file mode 100644
index 0000000000000..ae9a426a9d956
--- /dev/null
+++ b/flang/test/Driver/dash-x-f95.f
@@ -0,0 +1,35 @@
+program main
+  print *, __FILE__, __LINE__
+end
+
+! This test verifies that `flang`'s `-x` options behave like `gfortran`'s.
+! Specifically:
+! - `-x f95` should process the file based on its extension unless overridden.
+! - `-x f95-cpp-input` should behave like `-x f95` but with preprocessing
+!   (`-cpp`) enabled unless overridden.
+
+! ---
+! Ensure the file is treated as fixed-form unless explicitly set otherwise
+! ---
+! RUN: not %flang -Werror -x f95 -cpp -c %s 2>&1 | FileCheck --check-prefix=SCAN-ERROR %s
+! RUN: not %flang -Werror -x f95-cpp-input -c %s 2>&1 | FileCheck --check-prefix=SCAN-ERROR %s
+
+! SCAN-ERROR: error
+
+! RUN: %flang -Werror -x f95 -cpp -ffree-form -c %s 2>&1 | FileCheck --check-prefix=NO-SCAN-ERROR --allow-empty %s
+! RUN: %flang -Werror -x f95-cpp-input -ffree-form -c %s 2>&1 | FileCheck --check-prefix=NO-SCAN-ERROR --allow-empty %s
+
+! NO-SCAN-ERROR-NOT: error
+
+! ---
+! Ensure `-cpp` is not enabled by default unless explicitly requested
+! ---
+! RUN: not %flang -Werror -x f95 -ffree-form -c %s 2>&1 | FileCheck --check-prefix=SEMA-ERROR %s
+! RUN: not %flang -Werror -x f95-cpp-input -nocpp -ffree-form -c %s 2>&1 | FileCheck --check-prefix=SEMA-ERROR %s
+
+! SEMA-ERROR: error
+
+! RUN: %flang -Werror -x f95 -cpp -ffree-form -c %s 2>&1 | FileCheck --check-prefix=NO-SEMA-ERROR --allow-empty %s
+! RUN: %flang -Werror -x f95-cpp-input -ffree-form -c %s 2>&1 | FileCheck --check-prefix=NO-SEMA-ERROR --allow-empty %s
+
+! NO-SEMA-ERROR-NOT: error
diff --git a/flang/test/Driver/input-from-stdin/input-from-stdin.f90 b/flang/test/Driver/input-from-stdin/input-from-stdin.f90
index 285f0751b35d8..1fcc0340a64ba 100644
--- a/flang/test/Driver/input-from-stdin/input-from-stdin.f90
+++ b/flang/test/Driver/input-from-stdin/input-from-stdin.f90
@@ -6,7 +6,7 @@
 ! Input type is implicit
 ! RUN: cat %s | %flang -E -cpp - | FileCheck %s --check-prefix=PP-NOT-DEFINED
 ! RUN: cat %s | %flang -DNEW -E -cpp - | FileCheck %s --check-prefix=PP-DEFINED
-! RUN: cat %s | %flang -DNEW -E - | FileCheck %s --check-prefix=PP-NOT-DEFINED
+! RUN: cat %s | %flang -DNEW -E - | FileCheck %s --check-prefix=PP-DEFINED
 ! RUN: cat %s | %flang -DNEW -E -nocpp - | FileCheck %s --check-prefix=PP-NOT-DEFINED
 
 ! Input type is explicit



More information about the flang-commits mailing list