[flang][driver] Add support for `-module-suffix`
authorAndrzej Warzynski <andrzej.warzynski@arm.com>
Fri, 4 Jun 2021 12:58:03 +0000 (13:58 +0100)
committerAndrzej Warzynski <andrzej.warzynski@arm.com>
Fri, 4 Jun 2021 12:58:04 +0000 (13:58 +0100)
This option is supported in `f18`, but not yet available in `flang-new`.
It is required in order to call `flang-new` from the `flang` bash
script.

Differential Revision: https://reviews.llvm.org/D103613

clang/include/clang/Driver/Options.td
flang/include/flang/Frontend/CompilerInvocation.h
flang/lib/Frontend/CompilerInvocation.cpp
flang/test/Driver/driver-help.f90
flang/test/Driver/module-suffix.f90 [new file with mode: 0644]

index 4945538ec0f019f4b5ff454c520082febcb4615d..c2945fb0bf184d2f975933759c9cdbf9b9549786 100644 (file)
@@ -4522,6 +4522,9 @@ def fdebug_module_writer : Flag<["-"],"fdebug-module-writer">,
 def fget_symbols_sources : Flag<["-"], "fget-symbols-sources">, Group<Action_Group>,
   HelpText<"Dump symbols and their source code locations">;
 
+def module_suffix : Separate<["-"], "module-suffix">,  Group<f_Group>, MetaVarName<"<suffix>">,
+  HelpText<"Use <suffix> as the suffix for module files (the default value is `.mod`)">;
+
 }
 
 //===----------------------------------------------------------------------===//
index edd32917b175c86ff17e316868ed789a51e8acb4..f449a69dfa8a0ccab6cf979f773b0557b6aeeb20 100644 (file)
@@ -69,6 +69,8 @@ class CompilerInvocation : public CompilerInvocationBase {
   // of options.
   std::string moduleDir_ = ".";
 
+  std::string moduleFileSuffix_ = ".mod";
+
   bool debugModuleDir_ = false;
 
   bool warnAsErr_ = false;
@@ -97,6 +99,9 @@ public:
   std::string &moduleDir() { return moduleDir_; }
   const std::string &moduleDir() const { return moduleDir_; }
 
+  std::string &moduleFileSuffix() { return moduleFileSuffix_; }
+  const std::string &moduleFileSuffix() const { return moduleFileSuffix_; }
+
   bool &debugModuleDir() { return debugModuleDir_; }
   const bool &debugModuleDir() const { return debugModuleDir_; }
 
@@ -129,6 +134,10 @@ public:
   /// Useful setters
   void SetModuleDir(std::string &moduleDir) { moduleDir_ = moduleDir; }
 
+  void SetModuleFileSuffix(const char *moduleFileSuffix) {
+    moduleFileSuffix_ = std::string(moduleFileSuffix);
+  }
+
   void SetDebugModuleDir(bool flag) { debugModuleDir_ = flag; }
 
   void SetWarnAsErr(bool flag) { warnAsErr_ = flag; }
index 1f61e0e9ff30570a4c3b15db0dca283c1181e63e..1d18dda5e7eea817eea2987f1a13d6c3d9266435 100644 (file)
@@ -392,6 +392,12 @@ static bool parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
     res.SetDebugModuleDir(true);
   }
 
+  // -module-suffix
+  if (const auto *moduleSuffix =
+          args.getLastArg(clang::driver::options::OPT_module_suffix)) {
+    res.SetModuleFileSuffix(moduleSuffix->getValue());
+  }
+
   return diags.getNumErrors() == numErrorsBefore;
 }
 
@@ -639,5 +645,6 @@ void CompilerInvocation::setSemanticsOpts(
   semanticsContext_->set_moduleDirectory(moduleDir())
       .set_searchDirectories(fortranOptions.searchDirectories)
       .set_warnOnNonstandardUsage(enableConformanceChecks())
-      .set_warningsAreErrors(warnAsErr());
+      .set_warningsAreErrors(warnAsErr())
+      .set_moduleFileSuffix(moduleFileSuffix());
 }
index 9783751a2c9e7f8b94e5cc61353a20f981351aca..696c437a574b8a47b9e04c962e00824bdb2c61b2 100644 (file)
 ! HELP-FC1-NEXT: -help                  Display available options
 ! 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: -module-suffix <suffix> Use <suffix> as the suffix for module files (the default value is `.mod`)
 ! HELP-FC1-NEXT: -nocpp                 Disable predefined and command line preprocessor macros
 ! HELP-FC1-NEXT: -o <file>              Write output to <file>
 ! HELP-FC1-NEXT: -pedantic              Warn on language extensions
diff --git a/flang/test/Driver/module-suffix.f90 b/flang/test/Driver/module-suffix.f90
new file mode 100644 (file)
index 0000000..1516884
--- /dev/null
@@ -0,0 +1,16 @@
+! Tests `-module-suffix` frontend option
+
+!--------------------------
+! RUN lines
+!--------------------------
+! RUN: rm -rf %t && mkdir -p %t/dir-flang/
+! RUN: cd %t && %flang_fc1 -fsyntax-only -module-suffix .f18.mod -module-dir %t/dir-flang %s
+! RUN: ls %t/dir-flang/testmodule.f18.mod && not ls %t/dir-flang/testmodule.mod
+
+!--------------------------
+! INPUT
+!--------------------------
+module testmodule
+  type::t2
+  end type
+end