Skip to content

Commit 89de991

Browse files
committed
Reland Reland [clang][modules-driver] Add scanner to detect C++20 module presence (llvm#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 the reland (2d31fc8) for commit ded1426. The previous reland failed because the link dependency on clangLex was missing. Following the RFC linked below, the issue has now been addressed by properly linking against clangLex. https://discourse.llvm.org/t/rfc-driver-link-the-driver-against-clangdependencyscanning-clangast-clangfrontend-clangserialization-and-clanglex
1 parent f24c50a commit 89de991

File tree

9 files changed

+366
-0
lines changed

9 files changed

+366
-0
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

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

584+
def remark_found_cxx20_module_usage : Remark<
585+
"found C++20 module usage in file '%0'">,
586+
InGroup<ModulesDriver>;
587+
def remark_performing_driver_managed_module_build : Remark<
588+
"performing driver managed module build">,
589+
InGroup<ModulesDriver>;
590+
584591
def warn_drv_delayed_template_parsing_after_cxx20 : Warning<
585592
"-fdelayed-template-parsing is deprecated after C++20">,
586593
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
@@ -512,6 +512,9 @@ 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.
515518
///
516519
/// \param C - The compilation that is being built.
517520
/// \param Args - The input arguments.
@@ -796,6 +799,35 @@ class Driver {
796799
/// compilation based on which -f(no-)?lto(=.*)? option occurs last.
797800
void setLTOMode(const llvm::opt::ArgList &Args);
798801

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+
823+
/// Scans the leading lines of the C++ source inputs to detect C++20 module
824+
/// usage.
825+
///
826+
/// \returns True if module usage is detected, false otherwise, or an error on
827+
/// read failure.
828+
llvm::ErrorOr<bool>
829+
ScanInputsForCXX20ModulesUsage(const InputList &Inputs) const;
830+
799831
/// Retrieves a ToolChain for a particular \p Target triple.
800832
///
801833
/// 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
@@ -3296,6 +3296,13 @@ defm modules_reduced_bmi : BoolOption<"f", "modules-reduced-bmi",
32963296
PosFlag<SetTrue, [], [ClangOption, CC1Option],
32973297
"Generate the reduced BMI">>;
32983298

3299+
def fmodules_driver : Flag<["-"], "fmodules-driver">,
3300+
Group<f_Group>, Visibility<[ClangOption]>,
3301+
HelpText<"Enable support for driver managed module builds (experimental)">;
3302+
def fno_modules_driver : Flag<["-"], "fno-modules-driver">,
3303+
Group<f_Group>, Visibility<[ClangOption]>,
3304+
HelpText<"Disable support for driver managed module builds (experimental)">;
3305+
32993306
def experimental_modules_reduced_bmi : Flag<["-"], "fexperimental-modules-reduced-bmi">,
33003307
Group<f_Group>, Visibility<[ClangOption, CC1Option]>, Alias<fmodules_reduced_bmi>;
33013308

clang/include/clang/Lex/DependencyDirectivesScanner.h

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

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

clang/lib/Driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@ add_clang_library(clangDriver
9898

9999
LINK_LIBS
100100
clangBasic
101+
clangLex
101102
${system_libs}
102103
)

clang/lib/Driver/Driver.cpp

Lines changed: 69 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/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,35 @@ 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+
4336+
for (const auto &Input : CXXInputs) {
4337+
StringRef Filename = Input.second->getSpelling();
4338+
auto ErrOrBuffer = VFS->getBufferForFile(Filename);
4339+
if (!ErrOrBuffer)
4340+
return ErrOrBuffer.getError();
4341+
const auto Buffer = std::move(*ErrOrBuffer);
4342+
4343+
if (scanInputForCXX20ModulesUsage(Buffer->getBuffer())) {
4344+
Diags.Report(diag::remark_found_cxx20_module_usage) << Filename;
4345+
return true;
4346+
}
4347+
}
4348+
4349+
return false;
4350+
}
4351+
43174352
void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
43184353
const InputList &Inputs, ActionList &Actions) const {
43194354
llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
@@ -4325,6 +4360,33 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
43254360

43264361
handleArguments(C, Args, Inputs, Actions);
43274362

4363+
if (Args.hasFlag(options::OPT_fmodules_driver,
4364+
options::OPT_fno_modules_driver, false)) {
4365+
// TODO: Move the logic for implicitly enabling explicit-module-builds out
4366+
// of -fmodules-driver once it is no longer experimental.
4367+
// Currently, this serves diagnostic purposes only.
4368+
bool UsesCXXModules = hasCXXModuleInputType(Inputs);
4369+
if (!UsesCXXModules) {
4370+
const auto ErrOrScanResult = ScanInputsForCXX20ModulesUsage(Inputs);
4371+
if (!ErrOrScanResult) {
4372+
Diags.Report(diag::err_cannot_open_file)
4373+
<< ErrOrScanResult.getError().message();
4374+
return;
4375+
}
4376+
UsesCXXModules = *ErrOrScanResult;
4377+
}
4378+
if (UsesCXXModules)
4379+
BuildDriverManagedModuleBuildActions(C, Args, Inputs, Actions);
4380+
return;
4381+
}
4382+
4383+
BuildDefaultActions(C, Args, Inputs, Actions);
4384+
}
4385+
4386+
void Driver::BuildDefaultActions(Compilation &C, DerivedArgList &Args,
4387+
const InputList &Inputs,
4388+
ActionList &Actions) const {
4389+
43284390
bool UseNewOffloadingDriver =
43294391
C.isOffloadingHostKind(Action::OFK_OpenMP) ||
43304392
C.isOffloadingHostKind(Action::OFK_SYCL) ||
@@ -4608,6 +4670,13 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
46084670
Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
46094671
}
46104672

4673+
void Driver::BuildDriverManagedModuleBuildActions(
4674+
Compilation &C, llvm::opt::DerivedArgList &Args, const InputList &Inputs,
4675+
ActionList &Actions) const {
4676+
Diags.Report(diag::remark_performing_driver_managed_module_build);
4677+
return;
4678+
}
4679+
46114680
/// Returns the canonical name for the offloading architecture when using a HIP
46124681
/// or CUDA architecture.
46134682
static StringRef getCanonicalArchString(Compilation &C,

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 50 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,51 @@ 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+
S.TheLexer.setParsingPreprocessorDirective(true);
1113+
if (S.lexModule(First, End))
1114+
return false;
1115+
auto IsCXXNamedModuleDirective = [](const DirectiveWithTokens &D) {
1116+
switch (D.Kind) {
1117+
case dependency_directives_scan::cxx_module_decl:
1118+
case dependency_directives_scan::cxx_import_decl:
1119+
case dependency_directives_scan::cxx_export_module_decl:
1120+
case dependency_directives_scan::cxx_export_import_decl:
1121+
return true;
1122+
default:
1123+
return false;
1124+
}
1125+
};
1126+
return llvm::any_of(S.DirsWithToks, IsCXXNamedModuleDirective);
1127+
}

0 commit comments

Comments
 (0)