Skip to content

Commit f361b25

Browse files
committed
[Explicit Module Builds] Add canImport functionality to the ExplicitSwiftModuleLoader
It needs to check against the provided ExplicitModuleMap instead of looking into search paths.
1 parent 5a8fdaf commit f361b25

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class ExplicitSwiftModuleLoader: public SerializedModuleLoaderBase {
141141
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
142142
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer) override;
143143

144+
bool canImportModule(Located<Identifier> mID) override;
145+
144146
bool isCached(StringRef DepPath) override { return false; };
145147

146148
struct Implementation;

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,17 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
15141514
return std::error_code();
15151515
}
15161516

1517+
bool ExplicitSwiftModuleLoader::canImportModule(
1518+
Located<Identifier> mID) {
1519+
StringRef moduleName = mID.Item.str();
1520+
auto it = Impl.ExplicitModuleMap.find(moduleName);
1521+
// If no provided explicit module matches the name, then it cannot be imported.
1522+
if (it == Impl.ExplicitModuleMap.end()) {
1523+
return false;
1524+
}
1525+
return true;
1526+
}
1527+
15171528
void ExplicitSwiftModuleLoader::collectVisibleTopLevelModuleNames(
15181529
SmallVectorImpl<Identifier> &names) const {
15191530
for (auto &entry: Impl.ExplicitModuleMap) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir -p %t/clang-module-cache
3+
// RUN: mkdir -p %t/inputs
4+
// RUN: echo "public func foo() {}" >> %t/foo.swift
5+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/inputs/Foo.swiftmodule -emit-module-doc-path %t/inputs/Foo.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/inputs/Foo.swiftsourceinfo -module-cache-path %t.module-cache %t/foo.swift -module-name Foo
6+
7+
// RUN: echo "[{" > %/t/inputs/map.json
8+
// RUN: echo "\"moduleName\": \"Foo\"," >> %/t/inputs/map.json
9+
// RUN: echo "\"modulePath\": \"%/t/inputs/Foo.swiftmodule\"," >> %/t/inputs/map.json
10+
// RUN: echo "\"docPath\": \"%/t/inputs/Foo.swiftdoc\"," >> %/t/inputs/map.json
11+
// RUN: echo "\"sourceInfoPath\": \"%/t/inputs/Foo.swiftsourceinfo\"" >> %/t/inputs/map.json
12+
// RUN: echo "}]" >> %/t/inputs/map.json
13+
14+
// RUN: %target-swift-frontend -typecheck %s -explicit-swift-module-map-file %t/inputs/map.json -disable-implicit-swift-modules
15+
#if canImport(Foo)
16+
import Foo
17+
#endif

0 commit comments

Comments
 (0)