Skip to content

Commit 7dd220f

Browse files
authored
Merge pull request #32754 from artemcm/CanImportExplicitly
[Explicit Module Builds] Add canImport functionality to the ExplicitSwiftModuleLoader
2 parents f8ee94d + f361b25 commit 7dd220f

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
@@ -1616,6 +1616,17 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
16161616
return std::error_code();
16171617
}
16181618

1619+
bool ExplicitSwiftModuleLoader::canImportModule(
1620+
Located<Identifier> mID) {
1621+
StringRef moduleName = mID.Item.str();
1622+
auto it = Impl.ExplicitModuleMap.find(moduleName);
1623+
// If no provided explicit module matches the name, then it cannot be imported.
1624+
if (it == Impl.ExplicitModuleMap.end()) {
1625+
return false;
1626+
}
1627+
return true;
1628+
}
1629+
16191630
void ExplicitSwiftModuleLoader::collectVisibleTopLevelModuleNames(
16201631
SmallVectorImpl<Identifier> &names) const {
16211632
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)