Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -4277,6 +4277,8 @@ HelpText<"Dump the parse tree">; def fdebug_dump_provenance : Flag<["-"], "fdebug-dump-provenance">, Group, HelpText<"Dump provenance">; +def fdebug_module_writer : Flag<["-"],"fdebug-module-writer">, + HelpText<"Enable debug messages while writing module files">; } Index: clang/lib/Driver/ToolChains/Flang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Flang.cpp +++ clang/lib/Driver/ToolChains/Flang.cpp @@ -39,7 +39,7 @@ } void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { - Args.AddAllArgs(CmdArgs, options::OPT_module_dir); + Args.AddAllArgs(CmdArgs, {options::OPT_module_dir,options::OPT_fdebug_module_writer}); } void Flang::ConstructJob(Compilation &C, const JobAction &JA, Index: flang/include/flang/Frontend/CompilerInvocation.h =================================================================== --- flang/include/flang/Frontend/CompilerInvocation.h +++ flang/include/flang/Frontend/CompilerInvocation.h @@ -69,6 +69,8 @@ // of options. std::string moduleDir_ = "."; + bool debugModuleDir_ = false; + public: CompilerInvocation() = default; @@ -88,6 +90,9 @@ std::string &moduleDir() { return moduleDir_; } const std::string &moduleDir() const { return moduleDir_; } + bool &debugModuleDir() { return debugModuleDir_; } + const bool &debugModuleDir() const { return debugModuleDir_; } + /// Create a compiler invocation from a list of input options. /// \returns true on success. /// \returns false if an error was encountered while parsing the arguments @@ -96,6 +101,15 @@ llvm::ArrayRef commandLineArgs, clang::DiagnosticsEngine &diags); + /// Useful setters + void SetModuleDir(std::string &moduleDir) { + moduleDir_ = moduleDir; + } + + void SetDebugModuleDir(bool flag) { + debugModuleDir_ = flag; + } + /// 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: flang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- flang/lib/Frontend/CompilerInvocation.cpp +++ flang/lib/Frontend/CompilerInvocation.cpp @@ -292,9 +292,10 @@ /// Parses all semantic related arguments and populates the variables /// options accordingly. -static void parseSemaArgs(std::string &moduleDir, llvm::opt::ArgList &args, +static void parseSemaArgs(CompilerInvocation &res, llvm::opt::ArgList &args, clang::DiagnosticsEngine &diags) { + // -J/module-dir option auto moduleDirList = args.getAllArgValues(clang::driver::options::OPT_module_dir); // User can only specify -J/-module-dir once @@ -306,7 +307,12 @@ diags.Report(diagID); } if (moduleDirList.size() == 1) - moduleDir = moduleDirList[0]; + res.SetModuleDir(moduleDirList[0]); + + // -fdebug-module-writer option + if (args.hasArg(clang::driver::options::OPT_fdebug_module_writer)) { + res.SetDebugModuleDir(true); + } } bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res, @@ -339,7 +345,7 @@ // Parse the preprocessor args parsePreprocessorArgs(res.preprocessorOpts(), args); // Parse semantic args - parseSemaArgs(res.moduleDir(), args, diags); + parseSemaArgs(res, args, diags); return success; } @@ -451,7 +457,6 @@ *(new Fortran::common::IntrinsicTypeDefaultKinds()), fortranOptions.features, allCookedSources); - auto &moduleDirJ = moduleDir(); - semanticsContext_->set_moduleDirectory(moduleDirJ) + semanticsContext_->set_moduleDirectory(moduleDir()) .set_searchDirectories(fortranOptions.searchDirectories); } Index: flang/lib/Frontend/FrontendActions.cpp =================================================================== --- flang/lib/Frontend/FrontendActions.cpp +++ flang/lib/Frontend/FrontendActions.cpp @@ -116,7 +116,7 @@ // Prepare semantics setSemantics(std::make_unique( ci.invocation().semanticsContext(), parseTree, - ci.parsing().cooked().AsCharBlock())); + ci.parsing().cooked().AsCharBlock(), ci.invocation().debugModuleDir())); auto &semantics = this->semantics(); // Run semantic checks Index: flang/test/Flang-Driver/driver-help.f90 =================================================================== --- flang/test/Flang-Driver/driver-help.f90 +++ flang/test/Flang-Driver/driver-help.f90 @@ -59,6 +59,7 @@ ! HELP-FC1-NEXT: -fdebug-dump-parse-tree Dump the parse tree ! HELP-FC1-NEXT: -fdebug-dump-provenance Dump provenance ! HELP-FC1-NEXT: -fdebug-dump-symbols Dump symbols after the semantic analysis +! HELP-FC1-NEXT: -fdebug-module-writer Enables showing debug messages while writing module files. ! HELP-FC1-NEXT: -fdebug-unparse-with-symbols ! HELP-FC1-NEXT: Unparse and stop. ! HELP-FC1-NEXT: -fdebug-unparse Unparse and stop. Index: flang/test/Semantics/mod-file-rewriter.f90 =================================================================== --- flang/test/Semantics/mod-file-rewriter.f90 +++ flang/test/Semantics/mod-file-rewriter.f90 @@ -1,8 +1,8 @@ ! RUN: rm -fr %t && mkdir %t && cd %t -! RUN: %f18 -fsyntax-only -fdebug-module-writer %s 2>&1 | FileCheck %s --check-prefix CHECK_CHANGED -! RUN: %f18 -fsyntax-only -fdebug-module-writer %s 2>&1 | FileCheck %s --check-prefix CHECK_UNCHANGED -! RUN: %f18 -fsyntax-only -fdebug-module-writer %p/Inputs/mod-file-unchanged.f90 2>&1 | FileCheck %s --check-prefix CHECK_UNCHANGED -! RUN: %f18 -fsyntax-only -fdebug-module-writer %p/Inputs/mod-file-changed.f90 2>&1 | FileCheck %s --check-prefix CHECK_CHANGED +! RUN: %flang_fc1 -fsyntax-only -fdebug-module-writer %s 2>&1 | FileCheck %s --check-prefix CHECK_CHANGED +! RUN: %flang_fc1 -fsyntax-only -fdebug-module-writer %s 2>&1 | FileCheck %s --check-prefix CHECK_UNCHANGED +! RUN: %flang_fc1 -fsyntax-only -fdebug-module-writer %p/Inputs/mod-file-unchanged.f90 2>&1 | FileCheck %s --check-prefix CHECK_UNCHANGED +! RUN: %flang_fc1 -fsyntax-only -fdebug-module-writer %p/Inputs/mod-file-changed.f90 2>&1 | FileCheck %s --check-prefix CHECK_CHANGED module m real :: x(10)