Skip to content

Commit 370d34f

Browse files
authored
[flang][Driver] Add support of -fd-lines-as-comments and -fd-lines-as-code flags (#127605)
`-fd-lines-as-code` and `-fd-lines-as-comments` enables treatment for lines beginning with `d` or `D` in fixed form sources. Using these options in free form has no effect. If the `-fd-lines-as-code` option is given they are treated as if the first column contained a blank. If the `-fd-lines-as-comments` option is given, they are treated as comment lines.
1 parent a557861 commit 370d34f

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6777,8 +6777,16 @@ defm backtrace : BooleanFFlag<"backtrace">, Group<gfortran_Group>;
67776777
defm bounds_check : BooleanFFlag<"bounds-check">, Group<gfortran_Group>;
67786778
defm check_array_temporaries : BooleanFFlag<"check-array-temporaries">, Group<gfortran_Group>;
67796779
defm cray_pointer : BooleanFFlag<"cray-pointer">, Group<gfortran_Group>;
6780-
defm d_lines_as_code : BooleanFFlag<"d-lines-as-code">, Group<gfortran_Group>;
6781-
defm d_lines_as_comments : BooleanFFlag<"d-lines-as-comments">, Group<gfortran_Group>;
6780+
defm d_lines_as_code : BooleanFFlag<"d-lines-as-code">,
6781+
HelpText<"Treat fixed form lines with 'd' or 'D' in the "
6782+
"first column as blank.">,
6783+
Group<f_Group>,
6784+
Visibility<[FlangOption, FC1Option]>;
6785+
defm d_lines_as_comments : BooleanFFlag<"d-lines-as-comments">,
6786+
HelpText<"Treat fixed form lines with 'd' or 'D' in "
6787+
"the first column as comments.">,
6788+
Group<f_Group>,
6789+
Visibility<[FlangOption, FC1Option]>;
67826790
defm dollar_ok : BooleanFFlag<"dollar-ok">, Group<gfortran_Group>;
67836791
defm dump_fortran_optimized : BooleanFFlag<"dump-fortran-optimized">, Group<gfortran_Group>;
67846792
defm dump_fortran_original : BooleanFFlag<"dump-fortran-original">, Group<gfortran_Group>;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void Flang::addFortranDialectOptions(const ArgList &Args,
6060
options::OPT_frealloc_lhs,
6161
options::OPT_fno_realloc_lhs,
6262
options::OPT_fsave_main_program,
63+
options::OPT_fd_lines_as_code,
64+
options::OPT_fd_lines_as_comments,
6365
options::OPT_fno_save_main_program});
6466
}
6567

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,32 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
961961
clang::DiagnosticsEngine &diags) {
962962
unsigned numErrorsBefore = diags.getNumErrors();
963963

964+
// -fd-lines-as-code
965+
if (args.hasArg(clang::driver::options::OPT_fd_lines_as_code)) {
966+
if (res.getFrontendOpts().fortranForm == FortranForm::FreeForm) {
967+
const unsigned fdLinesAsWarning = diags.getCustomDiagID(
968+
clang::DiagnosticsEngine::Warning,
969+
"‘-fd-lines-as-code’ has no effect in free form.");
970+
diags.Report(fdLinesAsWarning);
971+
} else {
972+
res.getFrontendOpts().features.Enable(
973+
Fortran::common::LanguageFeature::OldDebugLines, true);
974+
}
975+
}
976+
977+
// -fd-lines-as-comments
978+
if (args.hasArg(clang::driver::options::OPT_fd_lines_as_comments)) {
979+
if (res.getFrontendOpts().fortranForm == FortranForm::FreeForm) {
980+
const unsigned fdLinesAsWarning = diags.getCustomDiagID(
981+
clang::DiagnosticsEngine::Warning,
982+
"‘-fd-lines-as-comments’ has no effect in free form.");
983+
diags.Report(fdLinesAsWarning);
984+
} else {
985+
res.getFrontendOpts().features.Enable(
986+
Fortran::common::LanguageFeature::OldDebugLines, false);
987+
}
988+
}
989+
964990
// -fdefault* family
965991
if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
966992
res.getDefaultKinds().set_defaultRealKind(8);

flang/test/Driver/fd-lines-as.f90

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! Ensure arguments -fd-lines-as-comments and -fd-lines-as-code as expected.
2+
3+
!--------------------------
4+
! FLANG DRIVER (flang)
5+
!--------------------------
6+
! Default behavior is equivalent as -fd-lines-as-comments
7+
!--------------------------
8+
! RUN: %flang -fsyntax-only -ffixed-form %s 2>&1
9+
! RUN: %flang -fsyntax-only -ffixed-form -fd-lines-as-comments %s 2>&1
10+
! RUN: not %flang -fsyntax-only -ffixed-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=CODE
11+
! RUN: not %flang -fsyntax-only -ffree-form -fd-lines-as-comments %s 2>&1 | FileCheck %s --check-prefix=WARNING-COMMENTS
12+
! RUN: not %flang -fsyntax-only -ffree-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=WARNING-CODE
13+
14+
!----------------------------------------
15+
! FRONTEND FLANG DRIVER (flang -fc1)
16+
!----------------------------------------
17+
! RUN: %flang_fc1 -fsyntax-only -ffixed-form %s 2>&1
18+
! RUN: %flang_fc1 -fsyntax-only -ffixed-form -fd-lines-as-comments %s 2>&1
19+
! RUN: not %flang_fc1 -fsyntax-only -ffixed-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=CODE
20+
! RUN: not %flang_fc1 -fsyntax-only -ffree-form -fd-lines-as-comments %s 2>&1 | FileCheck %s --check-prefix=WARNING-COMMENTS
21+
! RUN: not %flang_fc1 -fsyntax-only -ffree-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=WARNING-CODE
22+
23+
! CODE: Semantic errors
24+
! WARNING-COMMENTS: warning: ‘-fd-lines-as-comments’ has no effect in free form.
25+
! WARNING-CODE: warning: ‘-fd-lines-as-code’ has no effect in free form.
26+
27+
program FixedForm
28+
d end
29+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
! Ensure arguments -fd-lines-as-comments and -fd-lines-as-code display the correct output with -E.
2+
3+
!--------------------------
4+
! Default behavior is equivalent as -fd-lines-as-comments
5+
!--------------------------
6+
! RUN: %flang -E -ffixed-form %s 2>&1 | FileCheck %s --check-prefix=COMMENT
7+
! RUN: %flang -E -ffixed-form -fd-lines-as-comments %s 2>&1 | FileCheck %s --check-prefix=COMMENT
8+
! RUN: %flang -E -ffixed-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=CODE
9+
10+
program FixedForm
11+
d end
12+
D end
13+
end
14+
15+
! COMMENT: program FixedForm
16+
! COMMENT: end
17+
! COMMENT-NOT: end
18+
! COMMENT-NOT: end
19+
20+
! CODE: program FixedForm
21+
! CODE: end
22+
! CODE: end
23+
! CODE: end

0 commit comments

Comments
 (0)