Skip to content

[flang] Align -x language modes with gfortran #130268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions clang/lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,8 +817,13 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,

// '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 &&
// free form. Ensure this logic does not incorrectly assume fixed-form for
// cases where it shouldn't, such as `flang -x f95 foo.f90`.
bool isAtemporaryPreprocessedFile =
Input.isFilename() &&
llvm::sys::path::extension(Input.getFilename())
.ends_with(types::getTypeTempSuffix(InputType, /*CLStyle=*/false));
if (InputType == types::TY_PP_Fortran && isAtemporaryPreprocessedFile &&
Copy link
Member

@DavidTruby DavidTruby Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only just spotted this, but this requirement that isAtemporaryPreprocessedFile appears to have changed the default for all files to -cpp instead of -nocpp. I don't think this patch intended that change?

E.g. files with the .f90 suffix are now treated as if -cpp were passed, which I don't think is the intended behaviour.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I thought it was this part but I actually think it is the other change 😓

!Args.getLastArg(options::OPT_ffixed_form, options::OPT_ffree_form))
CmdArgs.push_back("-ffixed-form");

Expand Down
6 changes: 6 additions & 0 deletions flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

@DavidTruby DavidTruby Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we concluded that f95-cpp-input should mean that it has already been preprocessed, and so does not need preprocessing again, and fixed that elsewhere. So this should be PPMacrosFlag::Exclude since -x f95-cpp-input is what we pass for .f90 files, and that's where the issue is coming from.

Edit: Ah, no, we agreed the opposite 😅 , and we are passing the wrong -x for .f90 files. Sorry, I find this all a bit confusing....

.Default(opts.macrosFlag);

opts.noReformat = args.hasArg(clang::driver::options::OPT_fno_reformat);
opts.preprocessIncludeLines =
Expand Down
35 changes: 35 additions & 0 deletions flang/test/Driver/dash-x-f95-cpp-input.f
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions flang/test/Driver/dash-x-f95-do-not-assume-fixed-form.f90
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion flang/test/Driver/input-from-stdin/input-from-stdin.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down