Skip to content

Add an option for an explicit interface build without an additional Compile SubInstance #60556

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
merged 3 commits into from
Aug 16, 2022
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
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ WARNING(warning_cannot_find_locale_file,none,
"cannot find translations for '%0' at '%1': no such file", (StringRef, StringRef))
WARNING(warning_cannot_multithread_batch_mode,none,
"ignoring -num-threads argument; cannot multithread batch mode", ())
ERROR(error_cannot_ignore_interface_options_in_mode,none,
"'-ignore-interface-provided-options' only supported when building a module from interface ('-compile-module-from-interface')'", ())
ERROR(error_cannot_explicit_interface_build_in_mode,none,
"'-explicit-interface-module-build' only supported when building a module from interface ('-compile-module-from-interface')'", ())
ERROR(error_unsupported_option_argument,none,
"unsupported argument '%1' to option '%0'", (StringRef, StringRef))
ERROR(error_immediate_mode_missing_stdlib,none,
Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/ModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,13 @@ struct InterfaceSubContextDelegate {
StringRef interfacePath,
StringRef outputPath,
SourceLoc diagLoc,
bool ignoreInterfaceProvidedOptions,
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
ArrayRef<StringRef>,
ArrayRef<StringRef>, StringRef)> action) = 0;
virtual std::error_code runInSubCompilerInstance(StringRef moduleName,
StringRef interfacePath,
StringRef outputPath,
SourceLoc diagLoc,
bool ignoreInterfaceProvidedOptions,
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) = 0;

virtual ~InterfaceSubContextDelegate() = default;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class FrontendOptions {

/// If building a module from interface, ignore compiler flags
/// specified in the swiftinterface.
bool IgnoreInterfaceProvidedOptions = false;
bool ExplicitInterfaceBuild = false;

/// The module for which we should verify all of the generic signatures.
std::string VerifyGenericSignaturesInModule;
Expand Down
47 changes: 34 additions & 13 deletions include/swift/Frontend/ModuleInterfaceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,13 @@ struct ModuleInterfaceLoaderOptions {
bool disableImplicitSwiftModule = false;
bool disableBuildingInterface = false;
bool downgradeInterfaceVerificationError = false;
bool ignoreInterfaceProvidedOptions = false;
std::string mainExecutablePath;
ModuleInterfaceLoaderOptions(const FrontendOptions &Opts):
remarkOnRebuildFromInterface(Opts.RemarkOnRebuildFromModuleInterface),
disableInterfaceLock(Opts.DisableInterfaceFileLock),
disableImplicitSwiftModule(Opts.DisableImplicitModules),
disableBuildingInterface(Opts.DisableBuildingInterface),
downgradeInterfaceVerificationError(Opts.DowngradeInterfaceVerificationError),
ignoreInterfaceProvidedOptions(Opts.IgnoreInterfaceProvidedOptions),
mainExecutablePath(Opts.MainExecutablePath)
{
switch (Opts.RequestedAction) {
Expand Down Expand Up @@ -442,6 +440,21 @@ class ModuleInterfaceLoader : public SerializedModuleLoaderBase {
bool SerializeDependencyHashes,
bool TrackSystemDependencies, ModuleInterfaceLoaderOptions Opts,
RequireOSSAModules_t RequireOSSAModules);

/// Unconditionally build \p InPath (a swiftinterface file) to \p OutPath (as
/// a swiftmodule file).
///
/// Unlike the above `buildSwiftModuleFromSwiftInterface`, this method
/// bypasses the instantiation of a `CompilerInstance` from the compiler
/// configuration flags in the interface and instead directly uses the
/// supplied \p Instance
static bool buildExplicitSwiftModuleFromSwiftInterface(
CompilerInstance &Instance, const StringRef moduleCachePath,
const StringRef backupInterfaceDir, const StringRef prebuiltCachePath,
const StringRef ABIDescriptorPath, StringRef interfacePath,
StringRef outputPath, bool ShouldSerializeDeps,
ArrayRef<std::string> CompiledCandidates,
DependencyTracker *tracker = nullptr);
};

struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
Expand All @@ -460,13 +473,8 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
InFlightDiagnostic diagnose(StringRef interfacePath,
SourceLoc diagnosticLoc,
Diag<ArgTypes...> ID,
typename detail::PassArgument<ArgTypes>::type... Args) {
SourceLoc loc = diagnosticLoc;
if (diagnosticLoc.isInvalid()) {
// Diagnose this inside the interface file, if possible.
loc = SM.getLocFromExternalSource(interfacePath, 1, 1);
}
return Diags->diagnose(loc, ID, std::move(Args)...);
typename detail::PassArgument<ArgTypes>::type... Args) {
return InterfaceSubContextDelegateImpl::diagnose(interfacePath, diagnosticLoc, SM, Diags, ID, std::move(Args)...);
}
void
inheritOptionsForBuildingInterface(const SearchPathOptions &SearchPathOpts,
Expand All @@ -476,8 +484,7 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
SmallVectorImpl<const char *> &SubArgs,
std::string &CompilerVersion,
StringRef interfacePath,
SourceLoc diagnosticLoc,
bool ignoreInterfaceProvidedOptions);
SourceLoc diagnosticLoc);
public:
InterfaceSubContextDelegateImpl(
SourceManager &SM, DiagnosticEngine *Diags,
Expand All @@ -488,19 +495,33 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
StringRef backupModuleInterfaceDir,
bool serializeDependencyHashes, bool trackSystemDependencies,
RequireOSSAModules_t requireOSSAModules);

template<typename ...ArgTypes>
static InFlightDiagnostic diagnose(StringRef interfacePath,
SourceLoc diagnosticLoc,
SourceManager &SM,
DiagnosticEngine *Diags,
Diag<ArgTypes...> ID,
typename detail::PassArgument<ArgTypes>::type... Args) {
SourceLoc loc = diagnosticLoc;
if (diagnosticLoc.isInvalid()) {
// Diagnose this inside the interface file, if possible.
loc = SM.getLocFromExternalSource(interfacePath, 1, 1);
}
return Diags->diagnose(loc, ID, std::move(Args)...);
}

std::error_code runInSubContext(StringRef moduleName,
StringRef interfacePath,
StringRef outputPath,
SourceLoc diagLoc,
bool ignoreInterfaceProvidedOptions,
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
ArrayRef<StringRef>, ArrayRef<StringRef>,
StringRef)> action) override;
std::error_code runInSubCompilerInstance(StringRef moduleName,
StringRef interfacePath,
StringRef outputPath,
SourceLoc diagLoc,
bool ignoreInterfaceProvidedOptions,
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) override;

~InterfaceSubContextDelegateImpl() = default;
Expand Down
6 changes: 3 additions & 3 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -877,9 +877,9 @@ def compile_module_from_interface :
HelpText<"Treat the (single) input as a swiftinterface and produce a module">,
ModeOpt;

def ignore_interface_provided_options :
Flag<["-"], "ignore-interface-provided-options">,
HelpText<"Ignore all module flags specified in the swiftinterface being built">;
def explicit_interface_module_build :
Flag<["-"], "explicit-interface-module-build">,
HelpText<"Use the specified command-line to build the module from interface, instead of flags specified in the interface">;

def build_module_from_parseable_interface :
Flag<["-"], "build-module-from-parseable-interface">,
Expand Down
10 changes: 7 additions & 3 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bool ArgsToFrontendOptionsConverter::convert(
Opts.EnablePrivateImports |= Args.hasArg(OPT_enable_private_imports);
Opts.EnableLibraryEvolution |= Args.hasArg(OPT_enable_library_evolution);
Opts.FrontendParseableOutput |= Args.hasArg(OPT_frontend_parseable_output);
Opts.IgnoreInterfaceProvidedOptions |= Args.hasArg(OPT_ignore_interface_provided_options);
Opts.ExplicitInterfaceBuild |= Args.hasArg(OPT_explicit_interface_module_build);

// FIXME: Remove this flag
Opts.EnableLibraryEvolution |= Args.hasArg(OPT_enable_resilience);
Expand All @@ -93,6 +93,10 @@ bool ArgsToFrontendOptionsConverter::convert(
if (Args.hasArg(OPT_track_system_dependencies)) {
Opts.IntermoduleDependencyTracking =
IntermoduleDepTrackingMode::IncludeSystem;
} else if (Args.hasArg(OPT_explicit_interface_module_build)) {
// Always track at least the non-system dependencies for interface building.
Opts.IntermoduleDependencyTracking =
IntermoduleDepTrackingMode::ExcludeSystem;
}

if (const Arg *A = Args.getLastArg(OPT_bad_file_descriptor_retry_count)) {
Expand Down Expand Up @@ -622,9 +626,9 @@ bool ArgsToFrontendOptionsConverter::
bool ArgsToFrontendOptionsConverter::checkBuildFromInterfaceOnlyOptions()
const {
if (Opts.RequestedAction != FrontendOptions::ActionType::CompileModuleFromInterface &&
Opts.IgnoreInterfaceProvidedOptions) {
Opts.ExplicitInterfaceBuild) {
Diags.diagnose(SourceLoc(),
diag::error_cannot_ignore_interface_options_in_mode);
diag::error_cannot_explicit_interface_build_in_mode);
return true;
}
return false;
Expand Down
5 changes: 3 additions & 2 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@ void CompilerInstance::recordPrimaryInputBuffer(unsigned BufID) {
}

bool CompilerInstance::setUpASTContextIfNeeded() {
if (Invocation.getFrontendOptions().RequestedAction ==
if ((Invocation.getFrontendOptions().RequestedAction ==
FrontendOptions::ActionType::CompileModuleFromInterface ||
Invocation.getFrontendOptions().RequestedAction ==
FrontendOptions::ActionType::TypecheckModuleFromInterface) {
FrontendOptions::ActionType::TypecheckModuleFromInterface) &&
!Invocation.getFrontendOptions().ExplicitInterfaceBuild) {
// Compiling a module interface from source uses its own CompilerInstance
// with options read from the input file. Don't bother setting up an
// ASTContext at this level.
Expand Down
Loading