@@ -1473,6 +1473,33 @@ bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
14731473 return false ;
14741474}
14751475
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+
14761503Compilation *Driver::BuildCompilation (ArrayRef<const char *> ArgList) {
14771504 llvm::PrettyStackTraceString CrashInfo (" Compilation construction" );
14781505
@@ -1836,6 +1863,26 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
18361863 else
18371864 BuildActions (*C, C->getArgs (), Inputs, C->getActions ());
18381865
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+
18391886 if (CCCPrintPhases) {
18401887 PrintActions (*C);
18411888 return C;
@@ -4320,33 +4367,6 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
43204367 }
43214368}
43224369
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-
43504370void Driver::BuildActions (Compilation &C, DerivedArgList &Args,
43514371 const InputList &Inputs, ActionList &Actions) const {
43524372 llvm::PrettyStackTraceString CrashInfo (" Building compilation actions" );
@@ -4358,33 +4378,6 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
43584378
43594379 handleArguments (C, Args, Inputs, Actions);
43604380
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-
43884381 bool UseNewOffloadingDriver =
43894382 C.isOffloadingHostKind (Action::OFK_OpenMP) ||
43904383 C.isOffloadingHostKind (Action::OFK_SYCL) ||
@@ -4680,12 +4673,6 @@ void Driver::BuildDefaultActions(Compilation &C, DerivedArgList &Args,
46804673 Args.ClaimAllArgs (options::OPT_cl_ignored_Group);
46814674}
46824675
4683- void Driver::BuildDriverManagedModuleBuildActions (
4684- Compilation &C, llvm::opt::DerivedArgList &Args, const InputList &Inputs,
4685- ActionList &Actions) const {
4686- Diags.Report (diag::remark_performing_driver_managed_module_build);
4687- }
4688-
46894676// / Returns the canonical name for the offloading architecture when using a HIP
46904677// / or CUDA architecture.
46914678static StringRef getCanonicalArchString (Compilation &C,
0 commit comments