Skip to content

[5.1] -build-module-from-parseable-interface: Don't make an extra ASTContext #25198

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 1 commit into from
Jun 7, 2019
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
1 change: 1 addition & 0 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ class CompilerInstance {
}

bool setUpInputs();
bool setUpASTContextIfNeeded();
Optional<unsigned> setUpCodeCompletionBuffer();

/// Set up all state in the CompilerInstance to process the given input file.
Expand Down
10 changes: 7 additions & 3 deletions include/swift/Frontend/ParseableInterfaceModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ namespace unittest {

namespace swift {

class LangOptions;
class SearchPathOptions;

/// A ModuleLoader that runs a subordinate \c CompilerInvocation and
/// \c CompilerInstance to convert .swiftinterface files to .swiftmodule
/// files on the fly, caching the resulting .swiftmodules in the module cache
Expand Down Expand Up @@ -162,10 +165,11 @@ class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
/// Unconditionally build \p InPath (a swiftinterface file) to \p OutPath (as
/// a swiftmodule file).
///
/// A simplified version of the core logic in #openModuleFiles, mostly for
/// testing purposes.
/// A simplified version of the core logic in #openModuleFiles.
static bool buildSwiftModuleFromSwiftInterface(
ASTContext &Ctx, StringRef CacheDir, StringRef PrebuiltCacheDir,
SourceManager &SourceMgr, DiagnosticEngine &Diags,
const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
StringRef CacheDir, StringRef PrebuiltCacheDir,
StringRef ModuleName, StringRef InPath, StringRef OutPath,
bool SerializeDependencyHashes, bool TrackSystemDependencies,
bool RemarkOnRebuildFromInterface);
Expand Down
36 changes: 27 additions & 9 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ void CompilerInstance::recordPrimarySourceFile(SourceFile *SF) {
recordPrimaryInputBuffer(SF->getBufferID().getValue());
}

bool CompilerInstance::setUpASTContextIfNeeded() {
if (Invocation.getFrontendOptions().RequestedAction ==
FrontendOptions::ActionType::CompileModuleFromInterface) {
// 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.
return false;
}

Context.reset(ASTContext::get(Invocation.getLangOptions(),
Invocation.getSearchPathOptions(), SourceMgr,
Diagnostics));
registerTypeCheckerRequestFunctions(Context->evaluator);

if (setUpModuleLoaders())
return true;

return false;
}

bool CompilerInstance::setup(const CompilerInvocation &Invok) {
Invocation = Invok;

Expand All @@ -196,20 +216,18 @@ bool CompilerInstance::setup(const CompilerInvocation &Invok) {
Invocation.getLangOptions().AttachCommentsToDecls = true;
}

Context.reset(ASTContext::get(Invocation.getLangOptions(),
Invocation.getSearchPathOptions(), SourceMgr,
Diagnostics));
registerTypeCheckerRequestFunctions(Context->evaluator);

if (setUpModuleLoaders())
return true;

assert(Lexer::isIdentifier(Invocation.getModuleName()));

if (isInSILMode())
Invocation.getLangOptions().EnableAccessControl = false;

return setUpInputs();
if (setUpInputs())
return true;

if (setUpASTContextIfNeeded())
return true;

return false;
}

static bool loadAndValidateVFSOverlay(
Expand Down
54 changes: 30 additions & 24 deletions lib/Frontend/ParseableInterfaceModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ static Optional<StringRef> getRelativeDepPath(StringRef DepPath,
/// output path.
/// \note Needs to be in the swift namespace so CompilerInvocation can see it.
class swift::ParseableInterfaceBuilder {
ASTContext &ctx;
llvm::vfs::FileSystem &fs;
DiagnosticEngine &diags;
const StringRef interfacePath;
Expand All @@ -307,10 +306,9 @@ class swift::ParseableInterfaceBuilder {
.setMainAndSupplementaryOutputs({MainOut}, {SOPs});
}

void configureSubInvocation() {
auto &SearchPathOpts = ctx.SearchPathOpts;
auto &LangOpts = ctx.LangOpts;

void configureSubInvocation(const SearchPathOptions &SearchPathOpts,
const LangOptions &LangOpts,
ClangModuleLoader *ClangLoader) {
// Start with a SubInvocation that copies various state from our
// invoking ASTContext.
subInvocation.setImportSearchPaths(SearchPathOpts.ImportSearchPaths);
Expand All @@ -329,7 +327,7 @@ class swift::ParseableInterfaceBuilder {
// Respect the detailed-record preprocessor setting of the parent context.
// This, and the "raw" clang module format it implicitly enables, are
// required by sourcekitd.
if (auto *ClangLoader = ctx.getClangModuleLoader()) {
if (ClangLoader) {
auto &Opts = ClangLoader->getClangInstance().getPreprocessorOpts();
if (Opts.DetailedRecord) {
subInvocation.getClangImporterOptions().DetailedPreprocessingRecord = true;
Expand Down Expand Up @@ -472,7 +470,10 @@ class swift::ParseableInterfaceBuilder {
}

public:
ParseableInterfaceBuilder(ASTContext &ctx,
ParseableInterfaceBuilder(SourceManager &sourceMgr, DiagnosticEngine &diags,
const SearchPathOptions &searchPathOpts,
const LangOptions &langOpts,
ClangModuleLoader *clangImporter,
StringRef interfacePath,
StringRef moduleName,
StringRef moduleCachePath,
Expand All @@ -482,14 +483,14 @@ class swift::ParseableInterfaceBuilder {
bool remarkOnRebuildFromInterface = false,
SourceLoc diagnosticLoc = SourceLoc(),
DependencyTracker *tracker = nullptr)
: ctx(ctx), fs(*ctx.SourceMgr.getFileSystem()), diags(ctx.Diags),
interfacePath(interfacePath), moduleName(moduleName),
moduleCachePath(moduleCachePath), prebuiltCachePath(prebuiltCachePath),
serializeDependencyHashes(serializeDependencyHashes),
trackSystemDependencies(trackSystemDependencies),
remarkOnRebuildFromInterface(remarkOnRebuildFromInterface),
diagnosticLoc(diagnosticLoc), dependencyTracker(tracker) {
configureSubInvocation();
: fs(*sourceMgr.getFileSystem()), diags(diags),
interfacePath(interfacePath), moduleName(moduleName),
moduleCachePath(moduleCachePath), prebuiltCachePath(prebuiltCachePath),
serializeDependencyHashes(serializeDependencyHashes),
trackSystemDependencies(trackSystemDependencies),
remarkOnRebuildFromInterface(remarkOnRebuildFromInterface),
diagnosticLoc(diagnosticLoc), dependencyTracker(tracker) {
configureSubInvocation(searchPathOpts, langOpts, clangImporter);
}

const CompilerInvocation &getSubInvocation() const {
Expand Down Expand Up @@ -1236,9 +1237,11 @@ class ParseableInterfaceModuleLoaderImpl {
// Set up a builder if we need to build the module. It'll also set up
// the subinvocation we'll need to use to compute the cache paths.
ParseableInterfaceBuilder builder(
ctx, interfacePath, moduleName, cacheDir, prebuiltCacheDir,
/*serializeDependencyHashes*/false, trackSystemDependencies,
remarkOnRebuildFromInterface, diagnosticLoc, dependencyTracker);
ctx.SourceMgr, ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
ctx.getClangModuleLoader(), interfacePath, moduleName, cacheDir,
prebuiltCacheDir, /*serializeDependencyHashes*/false,
trackSystemDependencies, remarkOnRebuildFromInterface, diagnosticLoc,
dependencyTracker);
auto &subInvocation = builder.getSubInvocation();

// Compute the output path if we're loading or emitting a cached module.
Expand Down Expand Up @@ -1373,12 +1376,15 @@ std::error_code ParseableInterfaceModuleLoader::findModuleFilesInDirectory(


bool ParseableInterfaceModuleLoader::buildSwiftModuleFromSwiftInterface(
ASTContext &Ctx, StringRef CacheDir, StringRef PrebuiltCacheDir,
StringRef ModuleName, StringRef InPath, StringRef OutPath,
bool SerializeDependencyHashes, bool TrackSystemDependencies,
bool RemarkOnRebuildFromInterface) {
ParseableInterfaceBuilder builder(Ctx, InPath, ModuleName,
CacheDir, PrebuiltCacheDir,
SourceManager &SourceMgr, DiagnosticEngine &Diags,
const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
StringRef CacheDir, StringRef PrebuiltCacheDir,
StringRef ModuleName, StringRef InPath, StringRef OutPath,
bool SerializeDependencyHashes, bool TrackSystemDependencies,
bool RemarkOnRebuildFromInterface) {
ParseableInterfaceBuilder builder(SourceMgr, Diags, SearchPathOpts, LangOpts,
/*clangImporter*/nullptr, InPath,
ModuleName, CacheDir, PrebuiltCacheDir,
SerializeDependencyHashes,
TrackSystemDependencies,
RemarkOnRebuildFromInterface);
Expand Down
4 changes: 3 additions & 1 deletion lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,9 @@ static bool buildModuleFromParseableInterface(CompilerInvocation &Invocation,
StringRef InputPath = FEOpts.InputsAndOutputs.getFilenameOfFirstInput();
StringRef PrebuiltCachePath = FEOpts.PrebuiltModuleCachePath;
return ParseableInterfaceModuleLoader::buildSwiftModuleFromSwiftInterface(
Instance.getASTContext(), Invocation.getClangModuleCachePath(),
Instance.getSourceMgr(), Instance.getDiags(),
Invocation.getSearchPathOptions(), Invocation.getLangOptions(),
Invocation.getClangModuleCachePath(),
PrebuiltCachePath, Invocation.getModuleName(), InputPath,
Invocation.getOutputFilename(),
FEOpts.SerializeModuleInterfaceDependencyHashes,
Expand Down
13 changes: 13 additions & 0 deletions test/ParseableInterface/NoWrongSDKWarning.swiftinterface
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// swift-interface-format-version: 1.0
// swift-module-flags: -target x86_64-apple-macos10.9

// Deliberately pass the wrong target at the command line and see what happens.
// RUN: %empty-directory(%t)
// RUN: %swift -sdk %sdk -target arm64-apple-ios10 -build-module-from-parseable-interface %s -o %t/NoWrongSDKWarning.swiftmodule 2>&1 | %FileCheck -allow-empty %s

// REQUIRES: OS=macosx

public func empty()

// CHECK-NOT: warning:
// CHECK-NOT: error: