Skip to content

Commit 2d31fc8

Browse files
authored
Reland [clang][modules-driver] Add scanner to detect C++20 module presence (#147630)
This patch is part of a series to natively support C++20 module usage from the Clang driver (without requiring an external build system). This introduces a new scanner that detects C++20 module usage in source files without using the preprocessor or lexer. For now, it is enabled only with the `-fmodules-driver` flag and serves solely diagnostic purposes. In the future, the scanner will be enabled for any (modules-driver compatible) compilation with two or more inputs, and will help the driver determine whether to implicitly enable the modules driver. Since the scanner adds very little overhead, we are also exploring enabling it for compilations with only a single input. This approach could allow us to detect `import std` usage in a single-file compilation, which would then activate the modules driver. For performance measurements on this, see https://github.com/naveen-seth/llvm-dev-cxx-modules-check-benchmark. RFC: https://discourse.llvm.org/t/rfc-modules-support-simple-c-20-modules-use-from-the-clang-driver-without-a-build-system This patch relands commit ded1426. The CI failure is resolved by removing the compatibility warning for using the `-fmodules-driver` flag with pre-C++20 standards, which also better aligns its behavior with other features/flags supported only in newer standards.
1 parent e47d5eb commit 2d31fc8

File tree

8 files changed

+365
-0
lines changed

8 files changed

+365
-0
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,13 @@ def err_drv_reduced_module_output_overrided : Warning<
577577
"please consider use '-fmodule-output=' to specify the output file for reduced BMI explicitly">,
578578
InGroup<DiagGroup<"reduced-bmi-output-overrided">>;
579579

580+
def remark_found_cxx20_module_usage : Remark<
581+
"found C++20 module usage in file '%0'">,
582+
InGroup<ModulesDriver>;
583+
def remark_performing_driver_managed_module_build : Remark<
584+
"performing driver managed module build">,
585+
InGroup<ModulesDriver>;
586+
580587
def warn_drv_delayed_template_parsing_after_cxx20 : Warning<
581588
"-fdelayed-template-parsing is deprecated after C++20">,
582589
InGroup<DiagGroup<"delayed-template-parsing-in-cxx20">>;

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ def ModuleConflict : DiagGroup<"module-conflict">;
628628
def ModuleFileExtension : DiagGroup<"module-file-extension">;
629629
def ModuleIncludeDirectiveTranslation : DiagGroup<"module-include-translation">;
630630
def ModuleMap : DiagGroup<"module-map">;
631+
def ModulesDriver : DiagGroup<"modules-driver">;
631632
def RoundTripCC1Args : DiagGroup<"round-trip-cc1-args">;
632633
def NewlineEOF : DiagGroup<"newline-eof">;
633634
def Nullability : DiagGroup<"nullability">;

clang/include/clang/Driver/Driver.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ class Driver {
504504

505505
/// BuildActions - Construct the list of actions to perform for the
506506
/// given arguments, which are only done for a single architecture.
507+
/// If the compilation is an explicit module build, delegates to
508+
/// BuildDriverManagedModuleBuildActions. Otherwise, BuildDefaultActions is
509+
/// used.
507510
///
508511
/// \param C - The compilation that is being built.
509512
/// \param Args - The input arguments.
@@ -789,6 +792,35 @@ class Driver {
789792
/// compilation based on which -f(no-)?lto(=.*)? option occurs last.
790793
void setLTOMode(const llvm::opt::ArgList &Args);
791794

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

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3263,6 +3263,13 @@ defm modules_reduced_bmi : BoolOption<"f", "modules-reduced-bmi",
32633263
PosFlag<SetTrue, [], [ClangOption, CC1Option],
32643264
"Generate the reduced BMI">>;
32653265

3266+
def fmodules_driver : Flag<["-"], "fmodules-driver">,
3267+
Group<f_Group>, Visibility<[ClangOption]>,
3268+
HelpText<"Enable support for driver managed module builds (experimental)">;
3269+
def fno_modules_driver : Flag<["-"], "fno-modules-driver">,
3270+
Group<f_Group>, Visibility<[ClangOption]>,
3271+
HelpText<"Disable support for driver managed module builds (experimental)">;
3272+
32663273
def experimental_modules_reduced_bmi : Flag<["-"], "fexperimental-modules-reduced-bmi">,
32673274
Group<f_Group>, Visibility<[ClangOption, CC1Option]>, Alias<fmodules_reduced_bmi>;
32683275

clang/include/clang/Lex/DependencyDirectivesScanner.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ void printDependencyDirectivesAsSource(
136136
ArrayRef<dependency_directives_scan::Directive> Directives,
137137
llvm::raw_ostream &OS);
138138

139+
/// Scan an input source buffer for C++20 named module usage.
140+
///
141+
/// \param Source The input source buffer.
142+
///
143+
/// \returns true if any C++20 named modules related directive was found.
144+
bool scanInputForCXX20ModulesUsage(StringRef Source);
145+
139146
/// Functor that returns the dependency directives for a given file.
140147
class DependencyDirectivesGetter {
141148
public:

clang/lib/Driver/Driver.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
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/StringExtras.h"
@@ -4290,6 +4291,12 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
42904291
YcArg = nullptr;
42914292
}
42924293

4294+
if (Args.hasArgNoClaim(options::OPT_fmodules_driver))
4295+
// TODO: Check against all incompatible -fmodules-driver arguments
4296+
if (!ModulesModeCXX20) {
4297+
Args.eraseArg(options::OPT_fmodules_driver);
4298+
}
4299+
42934300
Arg *FinalPhaseArg;
42944301
phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
42954302

@@ -4416,6 +4423,35 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
44164423
}
44174424
}
44184425

4426+
static bool hasCXXModuleInputType(const Driver::InputList &Inputs) {
4427+
const auto IsTypeCXXModule = [](const auto &Input) -> bool {
4428+
const auto TypeID = Input.first;
4429+
return (TypeID == types::TY_CXXModule);
4430+
};
4431+
return llvm::any_of(Inputs, IsTypeCXXModule);
4432+
}
4433+
4434+
llvm::ErrorOr<bool>
4435+
Driver::ScanInputsForCXX20ModulesUsage(const InputList &Inputs) const {
4436+
const auto CXXInputs = llvm::make_filter_range(
4437+
Inputs, [](const auto &Input) { return types::isCXX(Input.first); });
4438+
4439+
for (const auto &Input : CXXInputs) {
4440+
StringRef Filename = Input.second->getSpelling();
4441+
auto ErrOrBuffer = VFS->getBufferForFile(Filename);
4442+
if (!ErrOrBuffer)
4443+
return ErrOrBuffer.getError();
4444+
const auto Buffer = std::move(*ErrOrBuffer);
4445+
4446+
if (scanInputForCXX20ModulesUsage(Buffer->getBuffer())) {
4447+
Diags.Report(diag::remark_found_cxx20_module_usage) << Filename;
4448+
return true;
4449+
}
4450+
}
4451+
4452+
return false;
4453+
}
4454+
44194455
void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
44204456
const InputList &Inputs, ActionList &Actions) const {
44214457
llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
@@ -4427,6 +4463,33 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
44274463

44284464
handleArguments(C, Args, Inputs, Actions);
44294465

4466+
if (Args.hasFlag(options::OPT_fmodules_driver,
4467+
options::OPT_fno_modules_driver, false)) {
4468+
// TODO: Move the logic for implicitly enabling explicit-module-builds out
4469+
// of -fmodules-driver once it is no longer experimental.
4470+
// Currently, this serves diagnostic purposes only.
4471+
bool UsesCXXModules = hasCXXModuleInputType(Inputs);
4472+
if (!UsesCXXModules) {
4473+
const auto ErrOrScanResult = ScanInputsForCXX20ModulesUsage(Inputs);
4474+
if (!ErrOrScanResult) {
4475+
Diags.Report(diag::err_cannot_open_file)
4476+
<< ErrOrScanResult.getError().message();
4477+
return;
4478+
}
4479+
UsesCXXModules = *ErrOrScanResult;
4480+
}
4481+
if (UsesCXXModules)
4482+
BuildDriverManagedModuleBuildActions(C, Args, Inputs, Actions);
4483+
return;
4484+
}
4485+
4486+
BuildDefaultActions(C, Args, Inputs, Actions);
4487+
}
4488+
4489+
void Driver::BuildDefaultActions(Compilation &C, DerivedArgList &Args,
4490+
const InputList &Inputs,
4491+
ActionList &Actions) const {
4492+
44304493
bool UseNewOffloadingDriver =
44314494
C.isOffloadingHostKind(Action::OFK_OpenMP) ||
44324495
C.isOffloadingHostKind(Action::OFK_SYCL) ||
@@ -4710,6 +4773,13 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
47104773
Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
47114774
}
47124775

4776+
void Driver::BuildDriverManagedModuleBuildActions(
4777+
Compilation &C, llvm::opt::DerivedArgList &Args, const InputList &Inputs,
4778+
ActionList &Actions) const {
4779+
Diags.Report(diag::remark_performing_driver_managed_module_build);
4780+
return;
4781+
}
4782+
47134783
/// Returns the canonical name for the offloading architecture when using a HIP
47144784
/// or CUDA architecture.
47154785
static StringRef getCanonicalArchString(Compilation &C,

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ struct Scanner {
8383
/// \returns True on error.
8484
bool scan(SmallVectorImpl<Directive> &Directives);
8585

86+
friend bool clang::scanInputForCXX20ModulesUsage(StringRef Source);
87+
8688
private:
8789
/// Lexes next token and advances \p First and the \p Lexer.
8890
[[nodiscard]] dependency_directives_scan::Token &
@@ -1075,3 +1077,50 @@ void clang::printDependencyDirectivesAsSource(
10751077
}
10761078
}
10771079
}
1080+
1081+
static void skipUntilMaybeCXX20ModuleDirective(const char *&First,
1082+
const char *const End) {
1083+
assert(First <= End);
1084+
while (First != End) {
1085+
if (*First == '#') {
1086+
++First;
1087+
skipToNewlineRaw(First, End);
1088+
}
1089+
skipWhitespace(First, End);
1090+
if (const auto Len = isEOL(First, End)) {
1091+
First += Len;
1092+
continue;
1093+
}
1094+
break;
1095+
}
1096+
}
1097+
1098+
bool clang::scanInputForCXX20ModulesUsage(StringRef Source) {
1099+
const char *First = Source.begin();
1100+
const char *const End = Source.end();
1101+
skipUntilMaybeCXX20ModuleDirective(First, End);
1102+
if (First == End)
1103+
return false;
1104+
1105+
// Check if the next token can even be a module directive before creating a
1106+
// full lexer.
1107+
if (!(*First == 'i' || *First == 'e' || *First == 'm'))
1108+
return false;
1109+
1110+
llvm::SmallVector<dependency_directives_scan::Token> Tokens;
1111+
Scanner S(StringRef(First, End - First), Tokens, nullptr, SourceLocation());
1112+
if (S.lexModule(First, End))
1113+
return false;
1114+
auto IsCXXNamedModuleDirective = [](const DirectiveWithTokens &D) {
1115+
switch (D.Kind) {
1116+
case dependency_directives_scan::cxx_module_decl:
1117+
case dependency_directives_scan::cxx_import_decl:
1118+
case dependency_directives_scan::cxx_export_module_decl:
1119+
case dependency_directives_scan::cxx_export_import_decl:
1120+
return true;
1121+
default:
1122+
return false;
1123+
}
1124+
};
1125+
return llvm::any_of(S.DirsWithToks, IsCXXNamedModuleDirective);
1126+
}

0 commit comments

Comments
 (0)