Skip to content

Commit 0f45437

Browse files
authored
-build-module-from-parseable-interface: Don't make an extra ASTContext (#25185)
This mode is supposed to get all its configuration information from the switftinterface being read in, but that means that the ASTContext and ClangImporter that get created by default may not be a sensible configuration (for example, a mismatched target and SDK, which Clang emits a warning about). Avoid this by just not creating the ASTContext if it's already been determined that the frontend is building a module from a parseable interface.
1 parent b8276f8 commit 0f45437

File tree

6 files changed

+81
-37
lines changed

6 files changed

+81
-37
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ class CompilerInstance {
535535
}
536536

537537
bool setUpInputs();
538+
bool setUpASTContextIfNeeded();
538539
Optional<unsigned> setUpCodeCompletionBuffer();
539540

540541
/// Set up all state in the CompilerInstance to process the given input file.

include/swift/Frontend/ParseableInterfaceModuleLoader.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ namespace unittest {
116116

117117
namespace swift {
118118

119+
class LangOptions;
120+
class SearchPathOptions;
121+
119122
/// A ModuleLoader that runs a subordinate \c CompilerInvocation and
120123
/// \c CompilerInstance to convert .swiftinterface files to .swiftmodule
121124
/// files on the fly, caching the resulting .swiftmodules in the module cache
@@ -162,10 +165,11 @@ class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
162165
/// Unconditionally build \p InPath (a swiftinterface file) to \p OutPath (as
163166
/// a swiftmodule file).
164167
///
165-
/// A simplified version of the core logic in #openModuleFiles, mostly for
166-
/// testing purposes.
168+
/// A simplified version of the core logic in #openModuleFiles.
167169
static bool buildSwiftModuleFromSwiftInterface(
168-
ASTContext &Ctx, StringRef CacheDir, StringRef PrebuiltCacheDir,
170+
SourceManager &SourceMgr, DiagnosticEngine &Diags,
171+
const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
172+
StringRef CacheDir, StringRef PrebuiltCacheDir,
169173
StringRef ModuleName, StringRef InPath, StringRef OutPath,
170174
bool SerializeDependencyHashes, bool TrackSystemDependencies,
171175
bool RemarkOnRebuildFromInterface);

lib/Frontend/Frontend.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ void CompilerInstance::recordPrimarySourceFile(SourceFile *SF) {
175175
recordPrimaryInputBuffer(SF->getBufferID().getValue());
176176
}
177177

178+
bool CompilerInstance::setUpASTContextIfNeeded() {
179+
if (Invocation.getFrontendOptions().RequestedAction ==
180+
FrontendOptions::ActionType::CompileModuleFromInterface) {
181+
// Compiling a module interface from source uses its own CompilerInstance
182+
// with options read from the input file. Don't bother setting up an
183+
// ASTContext at this level.
184+
return false;
185+
}
186+
187+
Context.reset(ASTContext::get(Invocation.getLangOptions(),
188+
Invocation.getSearchPathOptions(), SourceMgr,
189+
Diagnostics));
190+
registerTypeCheckerRequestFunctions(Context->evaluator);
191+
192+
if (setUpModuleLoaders())
193+
return true;
194+
195+
return false;
196+
}
197+
178198
bool CompilerInstance::setup(const CompilerInvocation &Invok) {
179199
Invocation = Invok;
180200

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

199-
Context.reset(ASTContext::get(Invocation.getLangOptions(),
200-
Invocation.getSearchPathOptions(), SourceMgr,
201-
Diagnostics));
202-
registerTypeCheckerRequestFunctions(Context->evaluator);
203-
204-
if (setUpModuleLoaders())
205-
return true;
206-
207219
assert(Lexer::isIdentifier(Invocation.getModuleName()));
208220

209221
if (isInSILMode())
210222
Invocation.getLangOptions().EnableAccessControl = false;
211223

212-
return setUpInputs();
224+
if (setUpInputs())
225+
return true;
226+
227+
if (setUpASTContextIfNeeded())
228+
return true;
229+
230+
return false;
213231
}
214232

215233
static bool loadAndValidateVFSOverlay(

lib/Frontend/ParseableInterfaceModuleLoader.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ static Optional<StringRef> getRelativeDepPath(StringRef DepPath,
280280
/// output path.
281281
/// \note Needs to be in the swift namespace so CompilerInvocation can see it.
282282
class swift::ParseableInterfaceBuilder {
283-
ASTContext &ctx;
284283
llvm::vfs::FileSystem &fs;
285284
DiagnosticEngine &diags;
286285
const StringRef interfacePath;
@@ -307,10 +306,9 @@ class swift::ParseableInterfaceBuilder {
307306
.setMainAndSupplementaryOutputs({MainOut}, {SOPs});
308307
}
309308

310-
void configureSubInvocation() {
311-
auto &SearchPathOpts = ctx.SearchPathOpts;
312-
auto &LangOpts = ctx.LangOpts;
313-
309+
void configureSubInvocation(const SearchPathOptions &SearchPathOpts,
310+
const LangOptions &LangOpts,
311+
ClangModuleLoader *ClangLoader) {
314312
// Start with a SubInvocation that copies various state from our
315313
// invoking ASTContext.
316314
subInvocation.setImportSearchPaths(SearchPathOpts.ImportSearchPaths);
@@ -329,7 +327,7 @@ class swift::ParseableInterfaceBuilder {
329327
// Respect the detailed-record preprocessor setting of the parent context.
330328
// This, and the "raw" clang module format it implicitly enables, are
331329
// required by sourcekitd.
332-
if (auto *ClangLoader = ctx.getClangModuleLoader()) {
330+
if (ClangLoader) {
333331
auto &Opts = ClangLoader->getClangInstance().getPreprocessorOpts();
334332
if (Opts.DetailedRecord) {
335333
subInvocation.getClangImporterOptions().DetailedPreprocessingRecord = true;
@@ -472,7 +470,10 @@ class swift::ParseableInterfaceBuilder {
472470
}
473471

474472
public:
475-
ParseableInterfaceBuilder(ASTContext &ctx,
473+
ParseableInterfaceBuilder(SourceManager &sourceMgr, DiagnosticEngine &diags,
474+
const SearchPathOptions &searchPathOpts,
475+
const LangOptions &langOpts,
476+
ClangModuleLoader *clangImporter,
476477
StringRef interfacePath,
477478
StringRef moduleName,
478479
StringRef moduleCachePath,
@@ -482,14 +483,14 @@ class swift::ParseableInterfaceBuilder {
482483
bool remarkOnRebuildFromInterface = false,
483484
SourceLoc diagnosticLoc = SourceLoc(),
484485
DependencyTracker *tracker = nullptr)
485-
: ctx(ctx), fs(*ctx.SourceMgr.getFileSystem()), diags(ctx.Diags),
486-
interfacePath(interfacePath), moduleName(moduleName),
487-
moduleCachePath(moduleCachePath), prebuiltCachePath(prebuiltCachePath),
488-
serializeDependencyHashes(serializeDependencyHashes),
489-
trackSystemDependencies(trackSystemDependencies),
490-
remarkOnRebuildFromInterface(remarkOnRebuildFromInterface),
491-
diagnosticLoc(diagnosticLoc), dependencyTracker(tracker) {
492-
configureSubInvocation();
486+
: fs(*sourceMgr.getFileSystem()), diags(diags),
487+
interfacePath(interfacePath), moduleName(moduleName),
488+
moduleCachePath(moduleCachePath), prebuiltCachePath(prebuiltCachePath),
489+
serializeDependencyHashes(serializeDependencyHashes),
490+
trackSystemDependencies(trackSystemDependencies),
491+
remarkOnRebuildFromInterface(remarkOnRebuildFromInterface),
492+
diagnosticLoc(diagnosticLoc), dependencyTracker(tracker) {
493+
configureSubInvocation(searchPathOpts, langOpts, clangImporter);
493494
}
494495

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

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

13741377

13751378
bool ParseableInterfaceModuleLoader::buildSwiftModuleFromSwiftInterface(
1376-
ASTContext &Ctx, StringRef CacheDir, StringRef PrebuiltCacheDir,
1377-
StringRef ModuleName, StringRef InPath, StringRef OutPath,
1378-
bool SerializeDependencyHashes, bool TrackSystemDependencies,
1379-
bool RemarkOnRebuildFromInterface) {
1380-
ParseableInterfaceBuilder builder(Ctx, InPath, ModuleName,
1381-
CacheDir, PrebuiltCacheDir,
1379+
SourceManager &SourceMgr, DiagnosticEngine &Diags,
1380+
const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
1381+
StringRef CacheDir, StringRef PrebuiltCacheDir,
1382+
StringRef ModuleName, StringRef InPath, StringRef OutPath,
1383+
bool SerializeDependencyHashes, bool TrackSystemDependencies,
1384+
bool RemarkOnRebuildFromInterface) {
1385+
ParseableInterfaceBuilder builder(SourceMgr, Diags, SearchPathOpts, LangOpts,
1386+
/*clangImporter*/nullptr, InPath,
1387+
ModuleName, CacheDir, PrebuiltCacheDir,
13821388
SerializeDependencyHashes,
13831389
TrackSystemDependencies,
13841390
RemarkOnRebuildFromInterface);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,9 @@ static bool buildModuleFromParseableInterface(CompilerInvocation &Invocation,
597597
StringRef InputPath = FEOpts.InputsAndOutputs.getFilenameOfFirstInput();
598598
StringRef PrebuiltCachePath = FEOpts.PrebuiltModuleCachePath;
599599
return ParseableInterfaceModuleLoader::buildSwiftModuleFromSwiftInterface(
600-
Instance.getASTContext(), Invocation.getClangModuleCachePath(),
600+
Instance.getSourceMgr(), Instance.getDiags(),
601+
Invocation.getSearchPathOptions(), Invocation.getLangOptions(),
602+
Invocation.getClangModuleCachePath(),
601603
PrebuiltCachePath, Invocation.getModuleName(), InputPath,
602604
Invocation.getOutputFilename(),
603605
FEOpts.SerializeModuleInterfaceDependencyHashes,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -target x86_64-apple-macos10.9
3+
4+
// Deliberately pass the wrong target at the command line and see what happens.
5+
// RUN: %empty-directory(%t)
6+
// RUN: %swift -sdk %sdk -target arm64-apple-ios10 -build-module-from-parseable-interface %s -o %t/NoWrongSDKWarning.swiftmodule 2>&1 | %FileCheck -allow-empty %s
7+
8+
// REQUIRES: OS=macosx
9+
10+
public func empty()
11+
12+
// CHECK-NOT: warning:
13+
// CHECK-NOT: error:

0 commit comments

Comments
 (0)