Skip to content

Reland [clang][modules-driver] Add scanner to detect C++20 module presence #147630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,13 @@ def err_drv_reduced_module_output_overrided : Warning<
"please consider use '-fmodule-output=' to specify the output file for reduced BMI explicitly">,
InGroup<DiagGroup<"reduced-bmi-output-overrided">>;

def remark_found_cxx20_module_usage : Remark<
"found C++20 module usage in file '%0'">,
InGroup<ModulesDriver>;
def remark_performing_driver_managed_module_build : Remark<
"performing driver managed module build">,
InGroup<ModulesDriver>;

def warn_drv_delayed_template_parsing_after_cxx20 : Warning<
"-fdelayed-template-parsing is deprecated after C++20">,
InGroup<DiagGroup<"delayed-template-parsing-in-cxx20">>;
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ def ModuleConflict : DiagGroup<"module-conflict">;
def ModuleFileExtension : DiagGroup<"module-file-extension">;
def ModuleIncludeDirectiveTranslation : DiagGroup<"module-include-translation">;
def ModuleMap : DiagGroup<"module-map">;
def ModulesDriver : DiagGroup<"modules-driver">;
def RoundTripCC1Args : DiagGroup<"round-trip-cc1-args">;
def NewlineEOF : DiagGroup<"newline-eof">;
def Nullability : DiagGroup<"nullability">;
Expand Down
32 changes: 32 additions & 0 deletions clang/include/clang/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ class Driver {

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

/// BuildDefaultActions - Constructs the list of actions to perform
/// for the provided arguments, which are only done for a single architecture.
///
/// \param C - The compilation that is being built.
/// \param Args - The input arguments.
/// \param Actions - The list to store the resulting actions onto.
void BuildDefaultActions(Compilation &C, llvm::opt::DerivedArgList &Args,
const InputList &Inputs, ActionList &Actions) const;

/// BuildDriverManagedModuleBuildActions - Performs a dependency
/// scan and constructs the list of actions to perform for dependency order
/// and the provided arguments. This is only done for a single a architecture.
///
/// \param C - The compilation that is being built.
/// \param Args - The input arguments.
/// \param Actions - The list to store the resulting actions onto.
void BuildDriverManagedModuleBuildActions(Compilation &C,
llvm::opt::DerivedArgList &Args,
const InputList &Inputs,
ActionList &Actions) const;

/// Scans the leading lines of the C++ source inputs to detect C++20 module
/// usage.
///
/// \returns True if module usage is detected, false otherwise, or an error on
/// read failure.
llvm::ErrorOr<bool>
ScanInputsForCXX20ModulesUsage(const InputList &Inputs) const;

/// Retrieves a ToolChain for a particular \p Target triple.
///
/// Will cache ToolChains for the life of the driver object, and create them
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3262,6 +3262,13 @@ def modules_reduced_bmi : Flag<["-"], "fmodules-reduced-bmi">,
HelpText<"Generate the reduced BMI">,
MarshallingInfoFlag<FrontendOpts<"GenReducedBMI">>;

def fmodules_driver : Flag<["-"], "fmodules-driver">,
Group<f_Group>, Visibility<[ClangOption]>,
HelpText<"Enable support for driver managed module builds (experimental)">;
def fno_modules_driver : Flag<["-"], "fno-modules-driver">,
Group<f_Group>, Visibility<[ClangOption]>,
HelpText<"Disable support for driver managed module builds (experimental)">;

def experimental_modules_reduced_bmi : Flag<["-"], "fexperimental-modules-reduced-bmi">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>, Alias<modules_reduced_bmi>;

Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Lex/DependencyDirectivesScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ void printDependencyDirectivesAsSource(
ArrayRef<dependency_directives_scan::Directive> Directives,
llvm::raw_ostream &OS);

/// Scan an input source buffer for C++20 named module usage.
///
/// \param Source The input source buffer.
///
/// \returns true if any C++20 named modules related directive was found.
bool scanInputForCXX20ModulesUsage(StringRef Source);

/// Functor that returns the dependency directives for a given file.
class DependencyDirectivesGetter {
public:
Expand Down
70 changes: 70 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include "clang/Driver/Tool.h"
#include "clang/Driver/ToolChain.h"
#include "clang/Driver/Types.h"
#include "clang/Lex/DependencyDirectivesScanner.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -4290,6 +4291,12 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
YcArg = nullptr;
}

if (Args.hasArgNoClaim(options::OPT_fmodules_driver))
// TODO: Check against all incompatible -fmodules-driver arguments
if (!ModulesModeCXX20) {
Args.eraseArg(options::OPT_fmodules_driver);
}

Arg *FinalPhaseArg;
phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);

Expand Down Expand Up @@ -4416,6 +4423,35 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
}
}

static bool hasCXXModuleInputType(const Driver::InputList &Inputs) {
const auto IsTypeCXXModule = [](const auto &Input) -> bool {
const auto TypeID = Input.first;
return (TypeID == types::TY_CXXModule);
};
return llvm::any_of(Inputs, IsTypeCXXModule);
}

llvm::ErrorOr<bool>
Driver::ScanInputsForCXX20ModulesUsage(const InputList &Inputs) const {
const auto CXXInputs = llvm::make_filter_range(
Inputs, [](const auto &Input) { return types::isCXX(Input.first); });

for (const auto &Input : CXXInputs) {
StringRef Filename = Input.second->getSpelling();
auto ErrOrBuffer = VFS->getBufferForFile(Filename);
if (!ErrOrBuffer)
return ErrOrBuffer.getError();
const auto Buffer = std::move(*ErrOrBuffer);

if (scanInputForCXX20ModulesUsage(Buffer->getBuffer())) {
Diags.Report(diag::remark_found_cxx20_module_usage) << Filename;
return true;
}
}

return false;
}

void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
const InputList &Inputs, ActionList &Actions) const {
llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
Expand All @@ -4427,6 +4463,33 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,

handleArguments(C, Args, Inputs, Actions);

if (Args.hasFlag(options::OPT_fmodules_driver,
options::OPT_fno_modules_driver, false)) {
// TODO: Move the logic for implicitly enabling explicit-module-builds out
// of -fmodules-driver once it is no longer experimental.
// Currently, this serves diagnostic purposes only.
bool UsesCXXModules = hasCXXModuleInputType(Inputs);
if (!UsesCXXModules) {
const auto ErrOrScanResult = ScanInputsForCXX20ModulesUsage(Inputs);
if (!ErrOrScanResult) {
Diags.Report(diag::err_cannot_open_file)
<< ErrOrScanResult.getError().message();
return;
}
UsesCXXModules = *ErrOrScanResult;
}
if (UsesCXXModules)
BuildDriverManagedModuleBuildActions(C, Args, Inputs, Actions);
return;
}

BuildDefaultActions(C, Args, Inputs, Actions);
}

void Driver::BuildDefaultActions(Compilation &C, DerivedArgList &Args,
const InputList &Inputs,
ActionList &Actions) const {

bool UseNewOffloadingDriver =
C.isOffloadingHostKind(Action::OFK_OpenMP) ||
C.isOffloadingHostKind(Action::OFK_SYCL) ||
Expand Down Expand Up @@ -4710,6 +4773,13 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
}

void Driver::BuildDriverManagedModuleBuildActions(
Compilation &C, llvm::opt::DerivedArgList &Args, const InputList &Inputs,
ActionList &Actions) const {
Diags.Report(diag::remark_performing_driver_managed_module_build);
return;
}

/// Returns the canonical name for the offloading architecture when using a HIP
/// or CUDA architecture.
static StringRef getCanonicalArchString(Compilation &C,
Expand Down
49 changes: 49 additions & 0 deletions clang/lib/Lex/DependencyDirectivesScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ struct Scanner {
/// \returns True on error.
bool scan(SmallVectorImpl<Directive> &Directives);

friend bool clang::scanInputForCXX20ModulesUsage(StringRef Source);

private:
/// Lexes next token and advances \p First and the \p Lexer.
[[nodiscard]] dependency_directives_scan::Token &
Expand Down Expand Up @@ -1075,3 +1077,50 @@ void clang::printDependencyDirectivesAsSource(
}
}
}

static void skipUntilMaybeCXX20ModuleDirective(const char *&First,
const char *const End) {
assert(First <= End);
while (First != End) {
if (*First == '#') {
++First;
skipToNewlineRaw(First, End);
}
skipWhitespace(First, End);
if (const auto Len = isEOL(First, End)) {
First += Len;
continue;
}
break;
}
}

bool clang::scanInputForCXX20ModulesUsage(StringRef Source) {
const char *First = Source.begin();
const char *const End = Source.end();
skipUntilMaybeCXX20ModuleDirective(First, End);
if (First == End)
return false;

// Check if the next token can even be a module directive before creating a
// full lexer.
if (!(*First == 'i' || *First == 'e' || *First == 'm'))
return false;

llvm::SmallVector<dependency_directives_scan::Token> Tokens;
Scanner S(StringRef(First, End - First), Tokens, nullptr, SourceLocation());
if (S.lexModule(First, End))
return false;
auto IsCXXNamedModuleDirective = [](const DirectiveWithTokens &D) {
switch (D.Kind) {
case dependency_directives_scan::cxx_module_decl:
case dependency_directives_scan::cxx_import_decl:
case dependency_directives_scan::cxx_export_module_decl:
case dependency_directives_scan::cxx_export_import_decl:
return true;
default:
return false;
}
};
return llvm::any_of(S.DirsWithToks, IsCXXNamedModuleDirective);
}
Loading