Skip to content

Commit 58d7080

Browse files
inaki-amatriafrederik-h
authored andcommitted
[flang] Align -x language modes with gfortran (llvm#130268)
This PR addresses some of the issues described in llvm#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`.
1 parent ca8ef6f commit 58d7080

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,13 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
817817

818818
// 'flang -E' always produces output that is suitable for use as fixed form
819819
// Fortran. However it is only valid free form source if the original is also
820-
// free form.
821-
if (InputType == types::TY_PP_Fortran &&
820+
// free form. Ensure this logic does not incorrectly assume fixed-form for
821+
// cases where it shouldn't, such as `flang -x f95 foo.f90`.
822+
bool isAtemporaryPreprocessedFile =
823+
Input.isFilename() &&
824+
llvm::sys::path::extension(Input.getFilename())
825+
.ends_with(types::getTypeTempSuffix(InputType, /*CLStyle=*/false));
826+
if (InputType == types::TY_PP_Fortran && isAtemporaryPreprocessedFile &&
822827
!Args.getLastArg(options::OPT_ffixed_form, options::OPT_ffree_form))
823828
CmdArgs.push_back("-ffixed-form");
824829

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,12 @@ static void parsePreprocessorArgs(Fortran::frontend::PreprocessorOptions &opts,
863863
(currentArg->getOption().matches(clang::driver::options::OPT_cpp))
864864
? PPMacrosFlag::Include
865865
: PPMacrosFlag::Exclude;
866+
// Enable -cpp based on -x unless explicitly disabled with -nocpp
867+
if (opts.macrosFlag != PPMacrosFlag::Exclude)
868+
if (const auto *dashX = args.getLastArg(clang::driver::options::OPT_x))
869+
opts.macrosFlag = llvm::StringSwitch<PPMacrosFlag>(dashX->getValue())
870+
.Case("f95-cpp-input", PPMacrosFlag::Include)
871+
.Default(opts.macrosFlag);
866872

867873
opts.noReformat = args.hasArg(clang::driver::options::OPT_fno_reformat);
868874
opts.preprocessIncludeLines =
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
program main
2+
print *, __FILE__, __LINE__
3+
end
4+
5+
! This test verifies that `flang`'s `-x` options behave like `gfortran`'s.
6+
! Specifically:
7+
! - `-x f95` should process the file based on its extension unless overridden.
8+
! - `-x f95-cpp-input` should behave like `-x f95` but with preprocessing
9+
! (`-cpp`) enabled unless overridden.
10+
11+
! ---
12+
! Ensure the file is treated as fixed-form unless explicitly set otherwise
13+
! ---
14+
! RUN: not %flang -Werror -fsyntax-only -x f95 -cpp %s 2>&1 | FileCheck --check-prefix=SCAN-ERROR %s
15+
! RUN: not %flang -Werror -fsyntax-only -x f95-cpp-input %s 2>&1 | FileCheck --check-prefix=SCAN-ERROR %s
16+
17+
! SCAN-ERROR: error: Could not scan
18+
19+
! RUN: %flang -Werror -fsyntax-only -x f95 -cpp -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SCAN-ERROR --allow-empty %s
20+
! RUN: %flang -Werror -fsyntax-only -x f95-cpp-input -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SCAN-ERROR --allow-empty %s
21+
22+
! NO-SCAN-ERROR-NOT: error
23+
24+
! ---
25+
! Ensure `-cpp` is not enabled by default unless explicitly requested
26+
! ---
27+
! RUN: not %flang -Werror -fsyntax-only -x f95 -ffree-form %s 2>&1 | FileCheck --check-prefix=SEMA-ERROR %s
28+
! RUN: not %flang -Werror -fsyntax-only -x f95-cpp-input -nocpp -ffree-form %s 2>&1 | FileCheck --check-prefix=SEMA-ERROR %s
29+
30+
! SEMA-ERROR: error: Semantic errors
31+
32+
! RUN: %flang -Werror -fsyntax-only -x f95 -cpp -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SEMA-ERROR --allow-empty %s
33+
! RUN: %flang -Werror -fsyntax-only -x f95-cpp-input -ffree-form %s 2>&1 | FileCheck --check-prefix=NO-SEMA-ERROR --allow-empty %s
34+
35+
! NO-SEMA-ERROR-NOT: error
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
! This test verifies that using `-x f95` does not cause the driver to assume
2+
! this file is in fixed-form.
3+
4+
program main
5+
print *, "Hello, World!"
6+
end
7+
8+
! RUN: %flang -### -x f95 %s 2>&1 | FileCheck --check-prefix=PRINT-PHASES %s
9+
! PRINT-PHASES-NOT: -ffixed-form
10+
11+
! RUN: %flang -Werror -fsyntax-only -x f95 %s 2>&1 | FileCheck --check-prefix=COMPILE --allow-empty %s
12+
! COMPILE-NOT: error

flang/test/Driver/input-from-stdin/input-from-stdin.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
! Input type is implicit
77
! RUN: cat %s | %flang -E -cpp - | FileCheck %s --check-prefix=PP-NOT-DEFINED
88
! RUN: cat %s | %flang -DNEW -E -cpp - | FileCheck %s --check-prefix=PP-DEFINED
9-
! RUN: cat %s | %flang -DNEW -E - | FileCheck %s --check-prefix=PP-NOT-DEFINED
9+
! RUN: cat %s | %flang -DNEW -E - | FileCheck %s --check-prefix=PP-DEFINED
1010
! RUN: cat %s | %flang -DNEW -E -nocpp - | FileCheck %s --check-prefix=PP-NOT-DEFINED
1111

1212
! Input type is explicit

0 commit comments

Comments
 (0)