Skip to content

Commit 9968ef5

Browse files
committed
[flang] Preserve fixed form in fc1 -x value
Fixes an issue introduced by 9fb2db1 [flang] Retain spaces when preprocessing fixed-form source Where flang -fc1 fails to parse preprocessor output because it now remains in fixed form.
1 parent 6f16a8b commit 9968ef5

File tree

6 files changed

+58
-12
lines changed

6 files changed

+58
-12
lines changed

clang/include/clang/Driver/Types.def

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ TYPE("assembler-with-cpp", Asm, PP_Asm, "S", phases
8888
// modules when Flang needs to emit pre-processed files. Therefore, the
8989
// `PP_TYPE` is set to `PP_Fortran` so that the driver is fine with
9090
// "pre-processing a pre-processed file".
91-
TYPE("f95", PP_Fortran, PP_Fortran, "i", phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
92-
TYPE("f95-cpp-input", Fortran, PP_Fortran, nullptr, phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
91+
TYPE("f95", PP_Fortran, PP_Fortran, "i", phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
92+
TYPE("f95-cpp-input", Fortran, PP_Fortran, nullptr, phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
93+
TYPE("f95-fixed", PP_Fortran_Fixed, PP_Fortran_Fixed, "i", phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
94+
TYPE("f95-fixed-cpp-input", Fortran_Fixed, PP_Fortran_Fixed, nullptr, phases::Preprocess, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
95+
9396
TYPE("java", Java, INVALID, nullptr, phases::Compile, phases::Backend, phases::Assemble, phases::Link)
9497

9598
// LLVM IR/LTO types. We define separate types for IR and LTO because LTO

clang/lib/Driver/ToolChain.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,10 +1037,14 @@ types::ID ToolChain::LookupTypeForExtension(StringRef Ext) const {
10371037
types::ID id = types::lookupTypeForExtension(Ext);
10381038

10391039
// Flang always runs the preprocessor and has no notion of "preprocessed
1040-
// fortran". Here, TY_PP_Fortran is coerced to TY_Fortran to avoid treating
1041-
// them differently.
1042-
if (D.IsFlangMode() && id == types::TY_PP_Fortran)
1043-
id = types::TY_Fortran;
1040+
// fortran". Here, TY_PP_Fortran[_Fixed] is coerced to TY_Fortran[_Fixed] to
1041+
// avoid treating them differently.
1042+
if (D.IsFlangMode()) {
1043+
if (id == types::TY_PP_Fortran)
1044+
id = types::TY_Fortran;
1045+
else if (id == types::TY_PP_Fortran_Fixed)
1046+
id = types::TY_Fortran_Fixed;
1047+
}
10441048

10451049
return id;
10461050
}

clang/lib/Driver/Types.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ bool types::isAcceptedByFlang(ID Id) {
162162
return false;
163163

164164
case TY_Fortran:
165+
case TY_Fortran_Fixed:
165166
case TY_PP_Fortran:
167+
case TY_PP_Fortran_Fixed:
166168
return true;
167169
case TY_LLVM_IR:
168170
case TY_LLVM_BC:
@@ -300,8 +302,8 @@ types::ID types::lookupTypeForExtension(llvm::StringRef Ext) {
300302
return llvm::StringSwitch<types::ID>(Ext)
301303
.Case("c", TY_C)
302304
.Case("C", TY_CXX)
303-
.Case("F", TY_Fortran)
304-
.Case("f", TY_PP_Fortran)
305+
.Case("F", TY_Fortran_Fixed)
306+
.Case("f", TY_PP_Fortran_Fixed)
305307
.Case("h", TY_CHeader)
306308
.Case("H", TY_CXXHeader)
307309
.Case("i", TY_PP_C)
@@ -344,10 +346,10 @@ types::ID types::lookupTypeForExtension(llvm::StringRef Ext) {
344346
.Case("f90", TY_PP_Fortran)
345347
.Case("F95", TY_Fortran)
346348
.Case("f95", TY_PP_Fortran)
347-
.Case("for", TY_PP_Fortran)
348-
.Case("FOR", TY_PP_Fortran)
349-
.Case("fpp", TY_Fortran)
350-
.Case("FPP", TY_Fortran)
349+
.Case("for", TY_PP_Fortran_Fixed)
350+
.Case("FOR", TY_PP_Fortran_Fixed)
351+
.Case("fpp", TY_Fortran_Fixed)
352+
.Case("FPP", TY_Fortran_Fixed)
351353
.Case("gch", TY_PCH)
352354
.Case("hip", TY_HIP)
353355
.Case("hipi", TY_PP_HIP)

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
639639

640640
// Get the input kind (from the value passed via `-x`)
641641
InputKind dashX(Language::Unknown);
642+
FortranForm dashXForm = FortranForm::Unknown;
642643
if (const llvm::opt::Arg *a =
643644
args.getLastArg(clang::driver::options::OPT_x)) {
644645
llvm::StringRef xValue = a->getValue();
@@ -648,6 +649,8 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
648649
// pre-processed inputs.
649650
.Case("f95", Language::Fortran)
650651
.Case("f95-cpp-input", Language::Fortran)
652+
.Case("f95-fixed", Language::Fortran)
653+
.Case("f95-fixed-cpp-input", Language::Fortran)
651654
// CUDA Fortran
652655
.Case("cuda", Language::Fortran)
653656
.Default(Language::Unknown);
@@ -663,6 +666,13 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
663666
if (dashX.isUnknown())
664667
diags.Report(clang::diag::err_drv_invalid_value)
665668
<< a->getAsString(args) << a->getValue();
669+
670+
if (dashX.getLanguage() == Language::Fortran) {
671+
if (xValue.starts_with("f95-fixed"))
672+
dashXForm = FortranForm::FixedForm;
673+
else
674+
dashXForm = FortranForm::FreeForm;
675+
}
666676
}
667677

668678
// Collect the input files and save them in our instance of FrontendOptions.
@@ -694,6 +704,8 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args,
694704
arg->getOption().matches(clang::driver::options::OPT_ffixed_form)
695705
? FortranForm::FixedForm
696706
: FortranForm::FreeForm;
707+
} else if (dashXForm != FortranForm::Unknown) {
708+
opts.fortranForm = dashXForm;
697709
}
698710

699711
// Set fixedFormColumns based on -ffixed-line-length=<value>

flang/test/Driver/x-lang.f

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
!RUN: %flang -save-temps -### %S/Inputs/free-form-test.f90 2>&1 | FileCheck %s --check-prefix=FREE
2+
!RUN: %flang -save-temps -### %S/Inputs/fixed-form-test.f 2>&1 | FileCheck %s --check-prefix=FIXED
3+
4+
FREE: "-fc1" {{.*}} "-o" "free-form-test.i" {{.*}} "-x" "f95-cpp-input" "{{.*}}/free-form-test.f90"
5+
FREE-NEXT: "-fc1" {{.*}} "-o" "free-form-test.bc" {{.*}} "-x" "f95" "free-form-test.i"
6+
7+
FIXED: "-fc1" {{.*}} "-o" "fixed-form-test.i" {{.*}} "-x" "f95-fixed-cpp-input" "{{.*}}/fixed-form-test.f"
8+
FIXED-NEXT: "-fc1" {{.*}} "-o" "fixed-form-test.bc" {{.*}} "-x" "f95-fixed" "fixed-form-test.i"

flang/test/Parser/x-f95-fixed.f

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
subroutine foo(a, b)
2+
if ( (a .eq. 0) .and.(b. eq. 1)) then
3+
4+
print *, "foo"
5+
end if
6+
end subroutine
7+
8+
! RUN: %flang_fc1 -fsyntax-only "-x" "f95-fixed" %s 2>&1 | FileCheck %s --allow-empty --check-prefix=F95-FIXED
9+
! F95-FIXED-NOT: Could not parse {{.*}}x-f95-fixed.f
10+
! F95-FIXED-NOT: error
11+
! F95-FIXED-NOT: warning
12+
13+
! RUN: not %flang_fc1 -fsyntax-only "-x" "f95" %s 2>&1 | FileCheck %s --check-prefix=F95 --strict-whitespace
14+
! F95: error: Could not parse {{.*}}x-f95-fixed.f
15+
! F95: {{.*}}x-f95-fixed.f:2:31: error: expected ')'
16+
! F95: if ( (a .eq. 0) .and.(b. eq. 1)) then
17+
! F95: {{^([ ]{32})}}^

0 commit comments

Comments
 (0)