aboutsummaryrefslogtreecommitdiff
path: root/flang
diff options
context:
space:
mode:
authorIƱaki Amatria Barral <140811900+inaki-amatria@users.noreply.github.com>2025-03-12 16:45:33 +0100
committerGitHub <noreply@github.com>2025-03-12 16:45:33 +0100
commitbdbe8fa1f3dcde77f7e0741ea7fa757ce092a420 (patch)
tree29d2a6638c535315896fb0b5d5e9d4ff5e5eaeca /flang
parent1db978cd781314a15277ea23a11ebd58604685d7 (diff)
downloadllvm-bdbe8fa1f3dcde77f7e0741ea7fa757ce092a420.zip
llvm-bdbe8fa1f3dcde77f7e0741ea7fa757ce092a420.tar.gz
llvm-bdbe8fa1f3dcde77f7e0741ea7fa757ce092a420.tar.bz2
[flang] Align `-x` language modes with `gfortran` (#130268)
This PR addresses some of the issues described in https://github.com/llvm/llvm-project/issues/127617. Key changes: - Stop assuming fixed-form for `-x f95` unless the input is a `.i` file. This change ensures compatibility with `-save-temps` workflows while preventing unintended fixed-form assumptions. - Ensure `-x f95-cpp-input` enables `-cpp` by default, aligning Flang's behavior with `gfortran`.
Diffstat (limited to 'flang')
-rw-r--r--flang/lib/Frontend/CompilerInvocation.cpp6
-rw-r--r--flang/test/Driver/dash-x-f95-cpp-input.f35
-rw-r--r--flang/test/Driver/dash-x-f95-do-not-assume-fixed-form.f9012
-rw-r--r--flang/test/Driver/input-from-stdin/input-from-stdin.f902
4 files changed, 54 insertions, 1 deletions
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 8b07a50..1537122 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -863,6 +863,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 (opts.macrosFlag != PPMacrosFlag::Exclude)
+ if (const auto *dashX = args.getLastArg(clang::driver::options::OPT_x))
+ 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-cpp-input.f b/flang/test/Driver/dash-x-f95-cpp-input.f
new file mode 100644
index 0000000..7c4689c6
--- /dev/null
+++ b/flang/test/Driver/dash-x-f95-cpp-input.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 -fsyntax-only -x f95 -cpp %s 2>&1 | FileCheck --check-prefix=SCAN-ERROR %s
+! RUN: not %flang -Werror -fsyntax-only -x f95-cpp-input %s 2>&1 | FileCheck --check-prefix=SCAN-ERROR %s
+
+! SCAN-ERROR: error: Could not scan
+
+! RUN: %flang -Werror -fsyntax-only -x f95 -cpp -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SCAN-ERROR --allow-empty %s
+! RUN: %flang -Werror -fsyntax-only -x f95-cpp-input -ffree-form %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 -fsyntax-only -x f95 -ffree-form %s 2>&1 | FileCheck --check-prefix=SEMA-ERROR %s
+! RUN: not %flang -Werror -fsyntax-only -x f95-cpp-input -nocpp -ffree-form %s 2>&1 | FileCheck --check-prefix=SEMA-ERROR %s
+
+! SEMA-ERROR: error: Semantic errors
+
+! RUN: %flang -Werror -fsyntax-only -x f95 -cpp -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SEMA-ERROR --allow-empty %s
+! RUN: %flang -Werror -fsyntax-only -x f95-cpp-input -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SEMA-ERROR --allow-empty %s
+
+! NO-SEMA-ERROR-NOT: error
diff --git a/flang/test/Driver/dash-x-f95-do-not-assume-fixed-form.f90 b/flang/test/Driver/dash-x-f95-do-not-assume-fixed-form.f90
new file mode 100644
index 0000000..d6faa4b
--- /dev/null
+++ b/flang/test/Driver/dash-x-f95-do-not-assume-fixed-form.f90
@@ -0,0 +1,12 @@
+! This test verifies that using `-x f95` does not cause the driver to assume
+! this file is in fixed-form.
+
+program main
+ print *, "Hello, World!"
+end
+
+! RUN: %flang -### -x f95 %s 2>&1 | FileCheck --check-prefix=PRINT-PHASES %s
+! PRINT-PHASES-NOT: -ffixed-form
+
+! RUN: %flang -Werror -fsyntax-only -x f95 %s 2>&1 | FileCheck --check-prefix=COMPILE --allow-empty %s
+! COMPILE-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 285f075..1fcc034 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