Skip to content

Commit 9760106

Browse files
committed
[clang][modules-driver] Move logic to enable -fmodules-driver (NFC)
This patch is part of a series to support driver-managed module builds for C++ named modules and Clang modules. Commit 9403c2d introduced the entry point for the driver-managed module build logic and assumed that it would live in the BuildActions phase of the driver. That proved unnecessary: the logic can be fully implemented in the BuildJobs phase. This reverts changes to BuildActions in preparation for the upcoming patches.
1 parent be2f020 commit 9760106

File tree

2 files changed

+47
-85
lines changed

2 files changed

+47
-85
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,6 @@ class Driver {
512512

513513
/// BuildActions - Construct the list of actions to perform for the
514514
/// given arguments, which are only done for a single architecture.
515-
/// If the compilation is an explicit module build, delegates to
516-
/// BuildDriverManagedModuleBuildActions. Otherwise, BuildDefaultActions is
517-
/// used.
518515
///
519516
/// \param C - The compilation that is being built.
520517
/// \param Args - The input arguments.
@@ -799,35 +796,13 @@ class Driver {
799796
/// compilation based on which -f(no-)?lto(=.*)? option occurs last.
800797
void setLTOMode(const llvm::opt::ArgList &Args);
801798

802-
/// BuildDefaultActions - Constructs the list of actions to perform
803-
/// for the provided arguments, which are only done for a single architecture.
804-
///
805-
/// \param C - The compilation that is being built.
806-
/// \param Args - The input arguments.
807-
/// \param Actions - The list to store the resulting actions onto.
808-
void BuildDefaultActions(Compilation &C, llvm::opt::DerivedArgList &Args,
809-
const InputList &Inputs, ActionList &Actions) const;
810-
811-
/// BuildDriverManagedModuleBuildActions - Performs a dependency
812-
/// scan and constructs the list of actions to perform for dependency order
813-
/// and the provided arguments. This is only done for a single a architecture.
814-
///
815-
/// \param C - The compilation that is being built.
816-
/// \param Args - The input arguments.
817-
/// \param Actions - The list to store the resulting actions onto.
818-
void BuildDriverManagedModuleBuildActions(Compilation &C,
819-
llvm::opt::DerivedArgList &Args,
820-
const InputList &Inputs,
821-
ActionList &Actions) const;
822-
823799
/// Scans the leading lines of the C++ source inputs to detect C++20 module
824800
/// usage.
825801
///
826802
/// \returns True if module usage is detected, false otherwise, or an error on
827803
/// read failure.
828804
llvm::ErrorOr<bool>
829805
ScanInputsForCXX20ModulesUsage(const InputList &Inputs) const;
830-
831806
/// Retrieves a ToolChain for a particular \p Target triple.
832807
///
833808
/// Will cache ToolChains for the life of the driver object, and create them

clang/lib/Driver/Driver.cpp

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
14761503
Compilation *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-
43504370
void 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.
46914678
static StringRef getCanonicalArchString(Compilation &C,

0 commit comments

Comments
 (0)