arnamoy10 created this revision.
arnamoy10 added reviewers: awarzynski, sscalpone, clementval, tskeith.
Herald added subscribers: jansvoboda11, dang.
arnamoy10 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Add support for the following Fortran dialect options:

-std=2018

It also adds one test cases.  Currently only `2018` is allowed as the standard.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97119

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/CompilerInvocation.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/test/Flang-Driver/driver-help-hidden.f90
  flang/test/Flang-Driver/driver-help.f90
  flang/test/Flang-Driver/std2018.f90

Index: flang/test/Flang-Driver/std2018.f90
===================================================================
--- /dev/null
+++ flang/test/Flang-Driver/std2018.f90
@@ -0,0 +1,33 @@
+! Ensure argument -std=2018 works as expected.
+
+!--------------------------
+! FLANG DRIVER (flang-new)
+!--------------------------
+! RUN: %flang-new -fsyntax-only -std=2018 %s  2>&1 | FileCheck %s --check-prefix=GIVEN
+! RUN: not %flang-new -fsyntax-only -std=90 %s  2>&1 | FileCheck %s --check-prefix=WRONG
+
+!-----------------------------------------
+! FRONTEND FLANG DRIVER (flang-new -fc1)
+!-----------------------------------------
+!  RUN: %flang-new -fc1 -fsyntax-only -std=2018 %s  2>&1 | FileCheck %s --check-prefix=GIVEN
+!  RUN: not %flang-new -fc1 -fsyntax-only -std=90 %s  2>&1 | FileCheck %s --check-prefix=WRONG
+
+!-----------------------------------------
+! EXPECTED OUTPUT WITH
+!-----------------------------------------
+! GIVEN: A DO loop should terminate with an END DO or CONTINUE
+
+!-----------------------------------------
+! EXPECTED OUTPUT WITH WRONG
+!-----------------------------------------
+! WRONG: Only -std=2018 is allowed currently.
+
+subroutine foo2()
+    do 01 m=1,2
+      select case (m)
+      case default
+        print*, "default", m
+      case (1)
+        print*, "start"
+01    end select
+end subroutine
Index: flang/test/Flang-Driver/driver-help.f90
===================================================================
--- flang/test/Flang-Driver/driver-help.f90
+++ flang/test/Flang-Driver/driver-help.f90
@@ -38,6 +38,7 @@
 ! HELP-NEXT: -I <dir>               Add directory to the end of the list of include search paths
 ! HELP-NEXT: -module-dir <dir>      Put MODULE files in <dir>
 ! HELP-NEXT: -o <file>              Write output to <file>
+! HELP-NEXT: -std=<value>           Language standard to compile for
 ! HELP-NEXT: -U <macro>             Undefine macro <macro>
 ! HELP-NEXT: --version              Print version information
 
@@ -64,6 +65,7 @@
 ! HELP-FC1-NEXT: -I <dir>               Add directory to the end of the list of include search paths
 ! HELP-FC1-NEXT: -module-dir <dir>      Put MODULE files in <dir>
 ! HELP-FC1-NEXT: -o <file>              Write output to <file>
+! HELP-FC1-NEXT: -std=<value>           Language standard to compile for
 ! HELP-FC1-NEXT: -U <macro>             Undefine macro <macro>
 ! HELP-FC1-NEXT: --version              Print version information
 
Index: flang/test/Flang-Driver/driver-help-hidden.f90
===================================================================
--- flang/test/Flang-Driver/driver-help-hidden.f90
+++ flang/test/Flang-Driver/driver-help-hidden.f90
@@ -38,6 +38,7 @@
 ! CHECK-NEXT: -I <dir>               Add directory to the end of the list of include search paths
 ! CHECK-NEXT: -module-dir <dir>      Put MODULE files in <dir>
 ! CHECK-NEXT: -o <file> Write output to <file>
+! CHECK-NEXT: -std=<value>           Language standard to compile for
 ! CHECK-NEXT: -test-io  Run the InputOuputTest action. Use for development and testing only.
 ! CHECK-NEXT: -U <macro>             Undefine macro <macro>
 ! CHECK-NEXT: --version Print version information
Index: flang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- flang/lib/Frontend/CompilerInvocation.cpp
+++ flang/lib/Frontend/CompilerInvocation.cpp
@@ -286,6 +286,21 @@
     res.frontendOpts().features_.Enable(
         Fortran::common::LanguageFeature::OpenMP);
   }
+
+  //-std=2018
+  if (args.hasArg(clang::driver::options::OPT_std_EQ)) {
+    auto standard = args.getLastArgValue(clang::driver::options::OPT_std_EQ);
+    // We only allow 2018 as the given standard
+    if (standard.equals("2018")) {
+      res.SetStandard();
+    }
+    else {
+      const unsigned diagID =
+          diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
+              "Only -std=2018 is allowed currently.");
+      diags.Report(diagID);
+    }
+  }
   return;
 }
 
@@ -423,6 +438,11 @@
   // directories
   if (moduleDirJ.compare(".") != 0)
     fortranOptions.searchDirectories.emplace_back(moduleDirJ);
+
+  // Set the standard
+  if (standard_) {
+    fortranOptions.features.WarnOnAllNonstandard();
+  }
 }
 
 void CompilerInvocation::setSemanticsOpts(
Index: flang/include/flang/Frontend/CompilerInvocation.h
===================================================================
--- flang/include/flang/Frontend/CompilerInvocation.h
+++ flang/include/flang/Frontend/CompilerInvocation.h
@@ -71,6 +71,7 @@
 
   // Fortran Dialect options
   Fortran::common::IntrinsicTypeDefaultKinds defaultKinds_;
+  bool standard_ = false;
 
 public:
   CompilerInvocation() = default;
@@ -106,6 +107,10 @@
       llvm::ArrayRef<const char *> commandLineArgs,
       clang::DiagnosticsEngine &diags);
 
+  // Enables the std=2018 option
+  void SetStandard() {
+    standard_ = true;
+  }
   /// Set the Fortran options to predifined defaults. These defaults are
   /// consistend with f18/f18.cpp.
   // TODO: We should map frontendOpts_ to parserOpts_ instead. For that, we
Index: clang/lib/Driver/ToolChains/Flang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Flang.cpp
+++ clang/lib/Driver/ToolChains/Flang.cpp
@@ -26,7 +26,8 @@
                    options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp,
                    options::OPT_fopenacc, options::OPT_fdefault_real_8,
                    options::OPT_fdefault_integer_8,
-                   options::OPT_fdefault_double_8, options::OPT_flarge_sizes});
+                   options::OPT_fdefault_double_8, options::OPT_flarge_sizes,
+                   options::OPT_std_EQ});
 }
 
 void Flang::AddPreprocessingOptions(const ArgList &Args,
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -3548,7 +3548,7 @@
 def static_libstdcxx : Flag<["-"], "static-libstdc++">;
 def static : Flag<["-", "--"], "static">, Group<Link_Group>, Flags<[NoArgumentUnused]>;
 def std_default_EQ : Joined<["-"], "std-default=">;
-def std_EQ : Joined<["-", "--"], "std=">, Flags<[CC1Option]>,
+def std_EQ : Joined<["-", "--"], "std=">, Flags<[CC1Option,FlangOption,FC1Option]>,
   Group<CompileOnly_Group>, HelpText<"Language standard to compile for">,
   ValuesCode<[{
     const char *Values =
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to