Skip to content

Commit 5c9b737

Browse files
committed
DependenciesScanner: prefer private Swift module interfaces if present
rdar://67257185
1 parent c0bc8ec commit 5c9b737

File tree

6 files changed

+80
-52
lines changed

6 files changed

+80
-52
lines changed

include/swift/AST/ModuleLoader.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/SetVector.h"
2525
#include "llvm/ADT/TinyPtrVector.h"
2626
#include "swift/AST/ModuleDependencies.h"
27+
#include <system_error>
2728

2829
namespace llvm {
2930
class FileCollector;
@@ -103,17 +104,18 @@ struct SubCompilerInstanceInfo {
103104

104105
/// Abstract interface to run an action in a sub ASTContext.
105106
struct InterfaceSubContextDelegate {
106-
virtual bool runInSubContext(StringRef moduleName,
107-
StringRef interfacePath,
108-
StringRef outputPath,
109-
SourceLoc diagLoc,
110-
llvm::function_ref<bool(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
111-
ArrayRef<StringRef>, StringRef)> action) = 0;
112-
virtual bool runInSubCompilerInstance(StringRef moduleName,
113-
StringRef interfacePath,
114-
StringRef outputPath,
115-
SourceLoc diagLoc,
116-
llvm::function_ref<bool(SubCompilerInstanceInfo&)> action) = 0;
107+
virtual std::error_code runInSubContext(StringRef moduleName,
108+
StringRef interfacePath,
109+
StringRef outputPath,
110+
SourceLoc diagLoc,
111+
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
112+
ArrayRef<StringRef>,
113+
ArrayRef<StringRef>, StringRef)> action) = 0;
114+
virtual std::error_code runInSubCompilerInstance(StringRef moduleName,
115+
StringRef interfacePath,
116+
StringRef outputPath,
117+
SourceLoc diagLoc,
118+
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) = 0;
117119

118120
virtual ~InterfaceSubContextDelegate() = default;
119121
};

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,18 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
415415
StringRef prebuiltCachePath,
416416
bool serializeDependencyHashes,
417417
bool trackSystemDependencies);
418-
bool runInSubContext(StringRef moduleName,
419-
StringRef interfacePath,
420-
StringRef outputPath,
421-
SourceLoc diagLoc,
422-
llvm::function_ref<bool(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
423-
ArrayRef<StringRef>, StringRef)> action) override;
424-
bool runInSubCompilerInstance(StringRef moduleName,
425-
StringRef interfacePath,
426-
StringRef outputPath,
427-
SourceLoc diagLoc,
428-
llvm::function_ref<bool(SubCompilerInstanceInfo&)> action) override;
418+
std::error_code runInSubContext(StringRef moduleName,
419+
StringRef interfacePath,
420+
StringRef outputPath,
421+
SourceLoc diagLoc,
422+
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
423+
ArrayRef<StringRef>, ArrayRef<StringRef>,
424+
StringRef)> action) override;
425+
std::error_code runInSubCompilerInstance(StringRef moduleName,
426+
StringRef interfacePath,
427+
StringRef outputPath,
428+
SourceLoc diagLoc,
429+
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) override;
429430

430431
~InterfaceSubContextDelegateImpl() = default;
431432

lib/Frontend/ModuleInterfaceBuilder.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
161161
llvm::RestorePrettyStackState(savedInnerPrettyStackState);
162162
};
163163

164-
SubError = subASTDelegate.runInSubCompilerInstance(moduleName,
165-
interfacePath,
166-
OutPath,
167-
diagnosticLoc,
164+
SubError = (bool)subASTDelegate.runInSubCompilerInstance(moduleName,
165+
interfacePath,
166+
OutPath,
167+
diagnosticLoc,
168168
[&](SubCompilerInstanceInfo &info) {
169169
auto &SubInstance = *info.Instance;
170170
auto subInvocation = SubInstance.getInvocation();
@@ -173,7 +173,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
173173
.getModuleInterfaceLoader())->tryEmitForwardingModule(moduleName,
174174
interfacePath,
175175
CompiledCandidates, OutPath)) {
176-
return false;
176+
return std::error_code();
177177
}
178178
FrontendOptions &FEOpts = subInvocation.getFrontendOptions();
179179
const auto &InputInfo = FEOpts.InputsAndOutputs.firstInput();
@@ -208,7 +208,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
208208
SubInstance.performSema();
209209
if (SubInstance.getASTContext().hadError()) {
210210
LLVM_DEBUG(llvm::dbgs() << "encountered errors\n");
211-
return true;
211+
return std::make_error_code(std::errc::not_supported);
212212
}
213213

214214
SILOptions &SILOpts = subInvocation.getSILOptions();
@@ -217,7 +217,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
217217
auto SILMod = performASTLowering(Mod, TC, SILOpts);
218218
if (!SILMod) {
219219
LLVM_DEBUG(llvm::dbgs() << "SILGen did not produce a module\n");
220-
return true;
220+
return std::make_error_code(std::errc::not_supported);
221221
}
222222

223223
// Setup the callbacks for serialization, which can occur during the
@@ -237,7 +237,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
237237
SmallVector<FileDependency, 16> Deps;
238238
bool serializeHashes = FEOpts.SerializeModuleInterfaceDependencyHashes;
239239
if (collectDepsForSerialization(SubInstance, Deps, serializeHashes)) {
240-
return true;
240+
return std::make_error_code(std::errc::not_supported);
241241
}
242242
if (ShouldSerializeDeps)
243243
SerializationOpts.Dependencies = Deps;
@@ -253,9 +253,12 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
253253
LLVM_DEBUG(llvm::dbgs() << "Running SIL processing passes\n");
254254
if (SubInstance.performSILProcessing(SILMod.get())) {
255255
LLVM_DEBUG(llvm::dbgs() << "encountered errors\n");
256-
return true;
256+
return std::make_error_code(std::errc::not_supported);
257+
}
258+
if (SubInstance.getDiags().hadAnyError()) {
259+
return std::make_error_code(std::errc::not_supported);
257260
}
258-
return SubInstance.getDiags().hadAnyError();
261+
return std::error_code();
259262
});
260263
});
261264
return !RunSuccess || SubError;

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,11 +1393,12 @@ InterfaceSubContextDelegateImpl::getCacheHash(StringRef useInterfacePath) {
13931393
return llvm::APInt(64, H).toString(36, /*Signed=*/false);
13941394
}
13951395

1396-
bool InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
1397-
StringRef interfacePath,
1398-
StringRef outputPath,
1399-
SourceLoc diagLoc,
1400-
llvm::function_ref<bool(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
1396+
std::error_code
1397+
InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
1398+
StringRef interfacePath,
1399+
StringRef outputPath,
1400+
SourceLoc diagLoc,
1401+
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
14011402
ArrayRef<StringRef>, StringRef)> action) {
14021403
return runInSubCompilerInstance(moduleName, interfacePath, outputPath, diagLoc,
14031404
[&](SubCompilerInstanceInfo &info){
@@ -1409,11 +1410,12 @@ bool InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
14091410
});
14101411
}
14111412

1412-
bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
1413-
StringRef interfacePath,
1414-
StringRef outputPath,
1415-
SourceLoc diagLoc,
1416-
llvm::function_ref<bool(SubCompilerInstanceInfo&)> action) {
1413+
std::error_code
1414+
InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
1415+
StringRef interfacePath,
1416+
StringRef outputPath,
1417+
SourceLoc diagLoc,
1418+
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) {
14171419
// We are about to mess up the compiler invocation by using the compiler
14181420
// arguments in the textual interface file. So copy to use a new compiler
14191421
// invocation.
@@ -1457,12 +1459,12 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
14571459
CompilerVersion,
14581460
interfacePath,
14591461
diagLoc)) {
1460-
return true;
1462+
return std::make_error_code(std::errc::not_supported);
14611463
}
14621464
// Insert arguments collected from the interface file.
14631465
BuildArgs.insert(BuildArgs.end(), SubArgs.begin(), SubArgs.end());
14641466
if (subInvocation.parseArgs(SubArgs, Diags)) {
1465-
return true;
1467+
return std::make_error_code(std::errc::not_supported);
14661468
}
14671469
CompilerInstance subInstance;
14681470
SubCompilerInstanceInfo info;
@@ -1474,7 +1476,7 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
14741476
ForwardingDiagnosticConsumer FDC(Diags);
14751477
subInstance.addDiagnosticConsumer(&FDC);
14761478
if (subInstance.setup(subInvocation)) {
1477-
return true;
1479+
return std::make_error_code(std::errc::not_supported);
14781480
}
14791481
info.BuildArguments = BuildArgs;
14801482
info.Hash = CacheHash;

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ class ModuleDependencyScanner : public SerializedModuleLoaderBase {
8181
}
8282
}
8383
assert(fs.exists(InPath));
84+
// Use the private interface file if exits.
85+
auto PrivateInPath =
86+
BaseName.getName(file_types::TY_PrivateSwiftModuleInterfaceFile);
87+
if (fs.exists(PrivateInPath)) {
88+
InPath = PrivateInPath;
89+
}
8490
auto dependencies = scanInterfaceFile(InPath, IsFramework);
8591
if (dependencies) {
8692
this->dependencies = std::move(dependencies.get());
@@ -182,9 +188,8 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
182188
llvm::SmallString<32> modulePath = moduleName.str();
183189
llvm::sys::path::replace_extension(modulePath, newExt);
184190
Optional<ModuleDependencies> Result;
185-
std::error_code code;
186-
187-
auto hasError = astDelegate.runInSubContext(moduleName.str(),
191+
std::error_code code =
192+
astDelegate.runInSubContext(moduleName.str(),
188193
moduleInterfacePath.str(),
189194
StringRef(),
190195
SourceLoc(),
@@ -205,8 +210,7 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
205210
auto &fs = *Ctx.SourceMgr.getFileSystem();
206211
auto interfaceBuf = fs.getBufferForFile(moduleInterfacePath);
207212
if (!interfaceBuf) {
208-
code = interfaceBuf.getError();
209-
return true;
213+
return interfaceBuf.getError();
210214
}
211215

212216
// Create a source file.
@@ -225,10 +229,10 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
225229
for (auto name: imInfo.ModuleNames) {
226230
Result->addModuleDependency(name.str(), &alreadyAddedModules);
227231
}
228-
return false;
232+
return std::error_code();
229233
});
230234

231-
if (hasError) {
235+
if (code) {
232236
return code;
233237
}
234238
return *Result;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir -p %t/clang-module-cache
3+
// RUN: echo "// swift-interface-format-version: 1.0" > %t/Foo.swiftinterface
4+
// RUN: echo "// swift-module-flags: -module-name Foo" >> %t/Foo.swiftinterface
5+
// RUN: echo "public func foo() {}" >> %t/Foo.swiftinterface
6+
7+
// RUN: cp %t/Foo.swiftinterface %t/Foo.private.swiftinterface
8+
9+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -o %t/deps.json -I %t -disable-implicit-swift-modules -Xcc -Xclang -Xcc -fno-implicit-modules
10+
11+
// Check the contents of the JSON output
12+
// RUN: %FileCheck %s < %t/deps.json
13+
14+
import Foo
15+
16+
// CHECK: "moduleInterfacePath": "{{.*}}Foo.private.swiftinterface",

0 commit comments

Comments
 (0)