6060#include " clang/Driver/Compilation.h"
6161#include " clang/Driver/InputInfo.h"
6262#include " clang/Driver/Job.h"
63+ #include " clang/Driver/ModulesDriver.h"
6364#include " clang/Driver/Options.h"
6465#include " clang/Driver/Phases.h"
6566#include " clang/Driver/SanitizerArgs.h"
@@ -1473,33 +1474,6 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
14731474 return false ;
14741475}
14751476
1476- static bool hasCXXModuleInputType (const Driver::InputList &Inputs) {
1477- const auto IsTypeCXXModule = [](const auto &Input) -> bool {
1478- const auto TypeID = Input.first ;
1479- return (TypeID == types::TY_CXXModule);
1480- };
1481- return llvm::any_of (Inputs, IsTypeCXXModule);
1482- }
1483-
1484- llvm::ErrorOr<bool >
1485- Driver::ScanInputsForCXX20ModulesUsage (const InputList &Inputs) const {
1486- const auto CXXInputs = llvm::make_filter_range (
1487- Inputs, [](const auto &Input) { return types::isCXX (Input.first ); });
1488- for (const auto &Input : CXXInputs) {
1489- StringRef Filename = Input.second ->getSpelling ();
1490- auto ErrOrBuffer = VFS->getBufferForFile (Filename);
1491- if (!ErrOrBuffer)
1492- return ErrOrBuffer.getError ();
1493- const auto Buffer = std::move (*ErrOrBuffer);
1494-
1495- if (scanInputForCXX20ModulesUsage (Buffer->getBuffer ())) {
1496- Diags.Report (diag::remark_found_cxx20_module_usage) << Filename;
1497- return true ;
1498- }
1499- }
1500- return false ;
1501- }
1502-
15031477Compilation *Driver::BuildCompilation (ArrayRef<const char *> ArgList) {
15041478 llvm::PrettyStackTraceString CrashInfo (" Compilation construction" );
15051479
@@ -1853,6 +1827,18 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
18531827 }
18541828 }
18551829
1830+ if (C->getArgs ().hasFlag (options::OPT_fmodules_driver,
1831+ options::OPT_fno_modules_driver, false )) {
1832+ // The detection logic for this is kept here only for diagnostics until
1833+ // is enabled by default.
1834+ modules::shouldEnableModulesDriver (Inputs, getVFS (), Diags);
1835+ Diags.Report (diag::remark_performing_driver_managed_module_build);
1836+ if (C->getArgs ().hasFlag (options::OPT_fimplicit_import_std,
1837+ options::OPT_fno_implicit_import_std, true )) {
1838+ modules::ensureNamedModuleStdLibraryInputs (*C, Inputs);
1839+ }
1840+ }
1841+
18561842 // Populate the tool chains for the offloading devices, if any.
18571843 CreateOffloadingDeviceToolChains (*C, Inputs);
18581844
@@ -1863,26 +1849,6 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
18631849 else
18641850 BuildActions (*C, C->getArgs (), Inputs, C->getActions ());
18651851
1866- if (C->getArgs ().hasFlag (options::OPT_fmodules_driver,
1867- options::OPT_fno_modules_driver, false )) {
1868- // TODO: When -fmodules-driver is no longer experimental, it should be
1869- // enabled by default only if both conditions are met: (1) there are two or
1870- // more C++ source inputs; and (2) at least one input uses C++20 named
1871- // modules.
1872- // The detection logic for this is kept here only for diagnostics until
1873- // is enabled by default.
1874- bool UsesCXXModules = hasCXXModuleInputType (Inputs);
1875- if (!UsesCXXModules) {
1876- const auto ErrOrScanResult = ScanInputsForCXX20ModulesUsage (Inputs);
1877- if (!ErrOrScanResult) {
1878- Diags.Report (diag::err_cannot_open_file)
1879- << ErrOrScanResult.getError ().message ();
1880- }
1881- UsesCXXModules = *ErrOrScanResult;
1882- }
1883- Diags.Report (diag::remark_performing_driver_managed_module_build);
1884- }
1885-
18861852 if (CCCPrintPhases) {
18871853 PrintActions (*C);
18881854 return C;
@@ -4236,10 +4202,20 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
42364202 YcArg = nullptr ;
42374203 }
42384204
4239- if (Args.hasArgNoClaim (options::OPT_fmodules_driver))
4240- // TODO: Check against all incompatible -fmodules-driver arguments
4241- if (!ModulesModeCXX20 && !Args.hasArgNoClaim (options::OPT_fmodules))
4242- Args.eraseArg (options::OPT_fmodules_driver);
4205+ if (Args.hasArgNoClaim (options::OPT_fmodules_driver)) {
4206+ // HACK: This should be only added for the Standard library jobs, explicitly
4207+ // created by the modules driver.
4208+ MakeInputArg (Args, getOpts (),
4209+ Args.MakeArgString (" -Wno-reserved-module-identifier" ));
4210+ if (Args.hasArg (options::OPT_fmodules)) {
4211+ Args.eraseArg (options::OPT_fmodules);
4212+ Arg *Arg = Args.MakeSeparateArg (
4213+ nullptr , getOpts ().getOption (options::OPT_fimplicit_modules),
4214+ Args.MakeArgString ((" -fimplicit-modules" )));
4215+ Arg->claim ();
4216+ Args.append (Arg);
4217+ }
4218+ }
42434219
42444220 Arg *FinalPhaseArg;
42454221 phases::ID FinalPhase = getFinalPhase (Args, &FinalPhaseArg);
@@ -5427,6 +5403,12 @@ void Driver::BuildJobs(Compilation &C) const {
54275403 }
54285404 }
54295405 }
5406+ if (C.getArgs ().hasFlag (options::OPT_fmodules_driver,
5407+ options::OPT_fno_modules_driver, false )) {
5408+ auto Success = modules::performDriverModuleBuild (C, C.getDriver ().Diags );
5409+ if (!Success)
5410+ return ;
5411+ }
54305412}
54315413
54325414namespace {
0 commit comments