Skip to content

[flang][Driver] Add support of -fd-lines-as-comments and -fd-lines-as-code flags #127605

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 1 commit into from
Mar 3, 2025

Conversation

JDPailleux
Copy link
Contributor

-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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels Feb 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Jean-Didier PAILLEUX (JDPailleux)

Changes

-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.


Full diff: https://github.com/llvm/llvm-project/pull/127605.diff

4 Files Affected:

  • (modified) clang/include/clang/Driver/Options.td (+6-2)
  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+2)
  • (modified) flang/lib/Frontend/CompilerInvocation.cpp (+26)
  • (added) flang/test/Driver/fd-lines-as.f90 (+29)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 5ad187926e710..ac0103cabda07 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6747,8 +6747,12 @@ defm backtrace : BooleanFFlag<"backtrace">, Group<gfortran_Group>;
 defm bounds_check : BooleanFFlag<"bounds-check">, Group<gfortran_Group>;
 defm check_array_temporaries : BooleanFFlag<"check-array-temporaries">, Group<gfortran_Group>;
 defm cray_pointer : BooleanFFlag<"cray-pointer">, Group<gfortran_Group>;
-defm d_lines_as_code : BooleanFFlag<"d-lines-as-code">, Group<gfortran_Group>;
-defm d_lines_as_comments : BooleanFFlag<"d-lines-as-comments">, Group<gfortran_Group>;
+defm d_lines_as_code : BooleanFFlag<"d-lines-as-code">,
+                       Group<gfortran_Group>,
+                       Visibility<[FlangOption, FC1Option]>;
+defm d_lines_as_comments : BooleanFFlag<"d-lines-as-comments">,
+                           Group<gfortran_Group>,
+                           Visibility<[FlangOption, FC1Option]>;
 defm dollar_ok : BooleanFFlag<"dollar-ok">, Group<gfortran_Group>;
 defm dump_fortran_optimized : BooleanFFlag<"dump-fortran-optimized">, Group<gfortran_Group>;
 defm dump_fortran_original : BooleanFFlag<"dump-fortran-original">, Group<gfortran_Group>;
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 9ad795edd724d..4c452491fa415 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -60,6 +60,8 @@ void Flang::addFortranDialectOptions(const ArgList &Args,
                             options::OPT_frealloc_lhs,
                             options::OPT_fno_realloc_lhs,
                             options::OPT_fsave_main_program,
+                            options::OPT_fd_lines_as_code,
+                            options::OPT_fd_lines_as_comments,
                             options::OPT_fno_save_main_program});
 }
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index f3d9432c62d3b..55283e1279f2f 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -957,6 +957,32 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
                              clang::DiagnosticsEngine &diags) {
   unsigned numErrorsBefore = diags.getNumErrors();
 
+  // -fd-lines-as-code
+  if (args.hasArg(clang::driver::options::OPT_fd_lines_as_code)) {
+    if (res.getFrontendOpts().fortranForm == FortranForm::FreeForm) {
+      const auto fdLinesAsWarning = diags.getCustomDiagID(
+          clang::DiagnosticsEngine::Warning,
+          "‘-fd-lines-as-code’ has no effect in free form.");
+      diags.Report(fdLinesAsWarning);
+    } else {
+      res.getFrontendOpts().features.Enable(
+          Fortran::common::LanguageFeature::OldDebugLines, true);
+    }
+  }
+
+  // -fd-lines-as-comments
+  if (args.hasArg(clang::driver::options::OPT_fd_lines_as_comments)) {
+    if (res.getFrontendOpts().fortranForm == FortranForm::FreeForm) {
+      const auto fdLinesAsWarning = diags.getCustomDiagID(
+          clang::DiagnosticsEngine::Warning,
+          "‘-fd-lines-as-comments’ has no effect in free form.");
+      diags.Report(fdLinesAsWarning);
+    } else {
+      res.getFrontendOpts().features.Enable(
+          Fortran::common::LanguageFeature::OldDebugLines, false);
+    }
+  }
+
   // -fdefault* family
   if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
     res.getDefaultKinds().set_defaultRealKind(8);
diff --git a/flang/test/Driver/fd-lines-as.f90 b/flang/test/Driver/fd-lines-as.f90
new file mode 100644
index 0000000000000..ea06a39378e28
--- /dev/null
+++ b/flang/test/Driver/fd-lines-as.f90
@@ -0,0 +1,29 @@
+! Ensure arguments -fd-lines-as-comments and -fd-lines-as-code as expected.
+
+!--------------------------
+! FLANG DRIVER (flang)
+!--------------------------
+! Default behavior is equivalent as -fd-lines-as-comments
+!--------------------------
+! RUN: %flang -fsyntax-only -ffixed-form %s 2>&1
+! RUN: %flang -fsyntax-only -ffixed-form -fd-lines-as-comments %s 2>&1
+! RUN: not %flang -fsyntax-only -ffixed-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=CODE
+! RUN: not %flang -fsyntax-only -ffree-form -fd-lines-as-comments %s 2>&1 | FileCheck %s --check-prefix=WARNING-COMMENTS
+! RUN: not %flang -fsyntax-only -ffree-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=WARNING-CODE
+
+!----------------------------------------
+! FRONTEND FLANG DRIVER (flang -fc1)
+!----------------------------------------
+! RUN: %flang_fc1 -fsyntax-only -ffixed-form %s 2>&1
+! RUN: %flang_fc1 -fsyntax-only -ffixed-form -fd-lines-as-comments %s 2>&1
+! RUN: not %flang_fc1 -fsyntax-only -ffixed-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=CODE
+! RUN: not %flang_fc1 -fsyntax-only -ffree-form -fd-lines-as-comments %s 2>&1 | FileCheck %s --check-prefix=WARNING-COMMENTS
+! RUN: not %flang_fc1 -fsyntax-only -ffree-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=WARNING-CODE
+
+! CODE: Semantic errors
+! WARNING-COMMENTS: warning: ‘-fd-lines-as-comments’ has no effect in free form.
+! WARNING-CODE: warning: ‘-fd-lines-as-code’ has no effect in free form.
+
+      program FixedForm
+d     end
+      end

@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-flang-driver

Author: Jean-Didier PAILLEUX (JDPailleux)

Changes

-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.


Full diff: https://github.com/llvm/llvm-project/pull/127605.diff

4 Files Affected:

  • (modified) clang/include/clang/Driver/Options.td (+6-2)
  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+2)
  • (modified) flang/lib/Frontend/CompilerInvocation.cpp (+26)
  • (added) flang/test/Driver/fd-lines-as.f90 (+29)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 5ad187926e710..ac0103cabda07 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6747,8 +6747,12 @@ defm backtrace : BooleanFFlag<"backtrace">, Group<gfortran_Group>;
 defm bounds_check : BooleanFFlag<"bounds-check">, Group<gfortran_Group>;
 defm check_array_temporaries : BooleanFFlag<"check-array-temporaries">, Group<gfortran_Group>;
 defm cray_pointer : BooleanFFlag<"cray-pointer">, Group<gfortran_Group>;
-defm d_lines_as_code : BooleanFFlag<"d-lines-as-code">, Group<gfortran_Group>;
-defm d_lines_as_comments : BooleanFFlag<"d-lines-as-comments">, Group<gfortran_Group>;
+defm d_lines_as_code : BooleanFFlag<"d-lines-as-code">,
+                       Group<gfortran_Group>,
+                       Visibility<[FlangOption, FC1Option]>;
+defm d_lines_as_comments : BooleanFFlag<"d-lines-as-comments">,
+                           Group<gfortran_Group>,
+                           Visibility<[FlangOption, FC1Option]>;
 defm dollar_ok : BooleanFFlag<"dollar-ok">, Group<gfortran_Group>;
 defm dump_fortran_optimized : BooleanFFlag<"dump-fortran-optimized">, Group<gfortran_Group>;
 defm dump_fortran_original : BooleanFFlag<"dump-fortran-original">, Group<gfortran_Group>;
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 9ad795edd724d..4c452491fa415 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -60,6 +60,8 @@ void Flang::addFortranDialectOptions(const ArgList &Args,
                             options::OPT_frealloc_lhs,
                             options::OPT_fno_realloc_lhs,
                             options::OPT_fsave_main_program,
+                            options::OPT_fd_lines_as_code,
+                            options::OPT_fd_lines_as_comments,
                             options::OPT_fno_save_main_program});
 }
 
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index f3d9432c62d3b..55283e1279f2f 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -957,6 +957,32 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
                              clang::DiagnosticsEngine &diags) {
   unsigned numErrorsBefore = diags.getNumErrors();
 
+  // -fd-lines-as-code
+  if (args.hasArg(clang::driver::options::OPT_fd_lines_as_code)) {
+    if (res.getFrontendOpts().fortranForm == FortranForm::FreeForm) {
+      const auto fdLinesAsWarning = diags.getCustomDiagID(
+          clang::DiagnosticsEngine::Warning,
+          "‘-fd-lines-as-code’ has no effect in free form.");
+      diags.Report(fdLinesAsWarning);
+    } else {
+      res.getFrontendOpts().features.Enable(
+          Fortran::common::LanguageFeature::OldDebugLines, true);
+    }
+  }
+
+  // -fd-lines-as-comments
+  if (args.hasArg(clang::driver::options::OPT_fd_lines_as_comments)) {
+    if (res.getFrontendOpts().fortranForm == FortranForm::FreeForm) {
+      const auto fdLinesAsWarning = diags.getCustomDiagID(
+          clang::DiagnosticsEngine::Warning,
+          "‘-fd-lines-as-comments’ has no effect in free form.");
+      diags.Report(fdLinesAsWarning);
+    } else {
+      res.getFrontendOpts().features.Enable(
+          Fortran::common::LanguageFeature::OldDebugLines, false);
+    }
+  }
+
   // -fdefault* family
   if (args.hasArg(clang::driver::options::OPT_fdefault_real_8)) {
     res.getDefaultKinds().set_defaultRealKind(8);
diff --git a/flang/test/Driver/fd-lines-as.f90 b/flang/test/Driver/fd-lines-as.f90
new file mode 100644
index 0000000000000..ea06a39378e28
--- /dev/null
+++ b/flang/test/Driver/fd-lines-as.f90
@@ -0,0 +1,29 @@
+! Ensure arguments -fd-lines-as-comments and -fd-lines-as-code as expected.
+
+!--------------------------
+! FLANG DRIVER (flang)
+!--------------------------
+! Default behavior is equivalent as -fd-lines-as-comments
+!--------------------------
+! RUN: %flang -fsyntax-only -ffixed-form %s 2>&1
+! RUN: %flang -fsyntax-only -ffixed-form -fd-lines-as-comments %s 2>&1
+! RUN: not %flang -fsyntax-only -ffixed-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=CODE
+! RUN: not %flang -fsyntax-only -ffree-form -fd-lines-as-comments %s 2>&1 | FileCheck %s --check-prefix=WARNING-COMMENTS
+! RUN: not %flang -fsyntax-only -ffree-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=WARNING-CODE
+
+!----------------------------------------
+! FRONTEND FLANG DRIVER (flang -fc1)
+!----------------------------------------
+! RUN: %flang_fc1 -fsyntax-only -ffixed-form %s 2>&1
+! RUN: %flang_fc1 -fsyntax-only -ffixed-form -fd-lines-as-comments %s 2>&1
+! RUN: not %flang_fc1 -fsyntax-only -ffixed-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=CODE
+! RUN: not %flang_fc1 -fsyntax-only -ffree-form -fd-lines-as-comments %s 2>&1 | FileCheck %s --check-prefix=WARNING-COMMENTS
+! RUN: not %flang_fc1 -fsyntax-only -ffree-form -fd-lines-as-code %s 2>&1 | FileCheck %s --check-prefix=WARNING-CODE
+
+! CODE: Semantic errors
+! WARNING-COMMENTS: warning: ‘-fd-lines-as-comments’ has no effect in free form.
+! WARNING-CODE: warning: ‘-fd-lines-as-code’ has no effect in free form.
+
+      program FixedForm
+d     end
+      end

@kiranchandramohan
Copy link
Contributor

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.

Do the tests added in this patch check this behaviour?

Comment on lines 6750 to 6759
defm d_lines_as_code : BooleanFFlag<"d-lines-as-code">,
Group<gfortran_Group>,
Visibility<[FlangOption, FC1Option]>;
defm d_lines_as_comments : BooleanFFlag<"d-lines-as-comments">,
Group<gfortran_Group>,
Visibility<[FlangOption, FC1Option]>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add help messages for these options?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

File updated with help messages.

@JDPailleux JDPailleux force-pushed the jdp/flang/fd-lines-as-flags branch from dafaaa9 to 2adb56d Compare February 18, 2025 13:43
@JDPailleux
Copy link
Contributor Author

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.

Do the tests added in this patch check this behaviour?

Yes, if the source file is compiled with -fd-lines-as-comments, then line started with 'd' will be ignored as it is a comment and no error will be thrown with a duplicate end statement. If -fd-lines-as-code is provided, the character 'd' will be replaced by a blank and so the program can't compile because there's an error due to the present of 2 end statements.

Comment on lines 6758 to 6759
HelpText<"In fixed form, with lines beginning with 'd' or 'D', the lines "
"will be treated as a comment.">,
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider rephrasing

Suggested change
HelpText<"In fixed form, with lines beginning with 'd' or 'D', the lines "
"will be treated as a comment.">,
HelpText<"In fixed form, lines beginning with 'd' or 'D' are"
"treated as comment lines">,

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another suggestion:
"Treat fixed form lines with 'd' or 'D' in the first column as comments."

I think the 'd'/'D' is required to be in the first column.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Help text updated

Comment on lines 6752 to 6753
HelpText<"In fixed form, with lines beginning with 'd' or 'D', the first "
"column will be treated as if it contained a blank.">,
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider rephrasing

Suggested change
HelpText<"In fixed form, with lines beginning with 'd' or 'D', the first "
"column will be treated as if it contained a blank.">,
HelpText<"In fixed form, lines beginning with 'd' or 'D' are treated "
"as if the first column contained a blank.">,

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another suggestion:
"Treat 'd' or 'D' in the first column of fixed form lines as a blank."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Help text updated

@JDPailleux JDPailleux force-pushed the jdp/flang/fd-lines-as-flags branch from 2adb56d to 7029f71 Compare February 19, 2025 13:41
Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

Thanks, Jean-Didier

@kiranchandramohan
Copy link
Contributor

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.

Do the tests added in this patch check this behaviour?

Yes, if the source file is compiled with -fd-lines-as-comments, then line started with 'd' will be ignored as it is a comment and no error will be thrown with a duplicate end statement. If -fd-lines-as-code is provided, the character 'd' will be replaced by a blank and so the program can't compile because there's an error due to the present of 2 end statements.

Thanks. I wondered whether there is a better way to test this (by unparse or inspecting the output of -E). If possible do so, otherwise it is alrite.

Comment on lines 6750 to 6759
defm d_lines_as_code : BooleanFFlag<"d-lines-as-code">,
HelpText<"Treat fixed form lines with 'd' or 'D' in the "
"first column as blank.">,
Group<gfortran_Group>,
Visibility<[FlangOption, FC1Option]>;
defm d_lines_as_comments : BooleanFFlag<"d-lines-as-comments">,
HelpText<"Treat fixed form lines with 'd' or 'D' in "
"the first column as comments.">,
Group<gfortran_Group>,
Visibility<[FlangOption, FC1Option]>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we move these to the f_Group as done in other patches (#120165)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch Kiran!

It looks like the gfortran_Group was created in 2013, well before flang. I assume that his was from back when clang would silently pass unsupported files to gfortran. The patch you referenced does not seem to have moved a known option to f_Group. But it does seem like the right thing to do.

Do we want to eventually move all supported gfortran flags to f_Group? If so, we can start with this and move the others over in time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Move to f_Group done.
Why keep gfortran_group unless it's to specify that a flag of this group is not supported by flang?

Copy link
Contributor

Choose a reason for hiding this comment

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

gfortran_group was probably used once in llvm to drive gcc/gfortran. I don't think anyone uses it these days.

@JDPailleux JDPailleux force-pushed the jdp/flang/fd-lines-as-flags branch from 7029f71 to 75c8eb0 Compare February 21, 2025 09:15
@JDPailleux
Copy link
Contributor Author

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.

Do the tests added in this patch check this behaviour?

Yes, if the source file is compiled with -fd-lines-as-comments, then line started with 'd' will be ignored as it is a comment and no error will be thrown with a duplicate end statement. If -fd-lines-as-code is provided, the character 'd' will be replaced by a blank and so the program can't compile because there's an error due to the present of 2 end statements.

Thanks. I wondered whether there is a better way to test this (by unparse or inspecting the output of -E). If possible do so, otherwise it is alrite.

Test added by inspecting the output of -E.

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

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

LG.

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

Thanks for seeing this through.

@JDPailleux JDPailleux closed this Mar 3, 2025
@JDPailleux JDPailleux reopened this Mar 3, 2025
@JDPailleux
Copy link
Contributor Author

Hi, thanks you for the approvals !
Can anyone merge this PR ? Thank in advance

@tblah tblah merged commit 370d34f into llvm:main Mar 3, 2025
22 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…-code flags (llvm#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants