6666#include " clang/Driver/Tool.h"
6767#include " clang/Driver/ToolChain.h"
6868#include " clang/Driver/Types.h"
69+ #include " clang/Lex/DependencyDirectivesScanner.h"
6970#include " llvm/ADT/ArrayRef.h"
7071#include " llvm/ADT/STLExtras.h"
7172#include " llvm/ADT/SmallSet.h"
@@ -4188,6 +4189,11 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
41884189 YcArg = nullptr ;
41894190 }
41904191
4192+ if (Args.hasArgNoClaim (options::OPT_fmodules_driver))
4193+ // TODO: Check against all incompatible -fmodules-driver arguments
4194+ if (!ModulesModeCXX20 && !Args.hasArgNoClaim (options::OPT_fmodules))
4195+ Args.eraseArg (options::OPT_fmodules_driver);
4196+
41914197 Arg *FinalPhaseArg;
41924198 phases::ID FinalPhase = getFinalPhase (Args, &FinalPhaseArg);
41934199
@@ -4314,6 +4320,33 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
43144320 }
43154321}
43164322
4323+ static bool hasCXXModuleInputType (const Driver::InputList &Inputs) {
4324+ const auto IsTypeCXXModule = [](const auto &Input) -> bool {
4325+ const auto TypeID = Input.first ;
4326+ return (TypeID == types::TY_CXXModule);
4327+ };
4328+ return llvm::any_of (Inputs, IsTypeCXXModule);
4329+ }
4330+
4331+ llvm::ErrorOr<bool >
4332+ Driver::ScanInputsForCXX20ModulesUsage (const InputList &Inputs) const {
4333+ const auto CXXInputs = llvm::make_filter_range (
4334+ Inputs, [](const auto &Input) { return types::isCXX (Input.first ); });
4335+ for (const auto &Input : CXXInputs) {
4336+ StringRef Filename = Input.second ->getSpelling ();
4337+ auto ErrOrBuffer = VFS->getBufferForFile (Filename);
4338+ if (!ErrOrBuffer)
4339+ return ErrOrBuffer.getError ();
4340+ const auto Buffer = std::move (*ErrOrBuffer);
4341+
4342+ if (scanInputForCXX20ModulesUsage (Buffer->getBuffer ())) {
4343+ Diags.Report (diag::remark_found_cxx20_module_usage) << Filename;
4344+ return true ;
4345+ }
4346+ }
4347+ return false ;
4348+ }
4349+
43174350void Driver::BuildActions (Compilation &C, DerivedArgList &Args,
43184351 const InputList &Inputs, ActionList &Actions) const {
43194352 llvm::PrettyStackTraceString CrashInfo (" Building compilation actions" );
@@ -4325,6 +4358,33 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
43254358
43264359 handleArguments (C, Args, Inputs, Actions);
43274360
4361+ if (Args.hasFlag (options::OPT_fmodules_driver,
4362+ options::OPT_fno_modules_driver, false )) {
4363+ // TODO: Move the logic for implicitly enabling explicit-module-builds out
4364+ // of -fmodules-driver once it is no longer experimental.
4365+ // Currently, this serves diagnostic purposes only.
4366+ bool UsesCXXModules = hasCXXModuleInputType (Inputs);
4367+ if (!UsesCXXModules) {
4368+ const auto ErrOrScanResult = ScanInputsForCXX20ModulesUsage (Inputs);
4369+ if (!ErrOrScanResult) {
4370+ Diags.Report (diag::err_cannot_open_file)
4371+ << ErrOrScanResult.getError ().message ();
4372+ return ;
4373+ }
4374+ UsesCXXModules = *ErrOrScanResult;
4375+ }
4376+ if (UsesCXXModules || Args.hasArg (options::OPT_fmodules))
4377+ BuildDriverManagedModuleBuildActions (C, Args, Inputs, Actions);
4378+ return ;
4379+ }
4380+
4381+ BuildDefaultActions (C, Args, Inputs, Actions);
4382+ }
4383+
4384+ void Driver::BuildDefaultActions (Compilation &C, DerivedArgList &Args,
4385+ const InputList &Inputs,
4386+ ActionList &Actions) const {
4387+
43284388 bool UseNewOffloadingDriver =
43294389 C.isOffloadingHostKind (Action::OFK_OpenMP) ||
43304390 C.isOffloadingHostKind (Action::OFK_SYCL) ||
@@ -4608,6 +4668,13 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
46084668 Args.ClaimAllArgs (options::OPT_cl_ignored_Group);
46094669}
46104670
4671+ void Driver::BuildDriverManagedModuleBuildActions (
4672+ Compilation &C, llvm::opt::DerivedArgList &Args, const InputList &Inputs,
4673+ ActionList &Actions) const {
4674+ Diags.Report (diag::remark_performing_driver_managed_module_build);
4675+ return ;
4676+ }
4677+
46114678// / Returns the canonical name for the offloading architecture when using a HIP
46124679// / or CUDA architecture.
46134680static StringRef getCanonicalArchString (Compilation &C,
0 commit comments