Skip to content

Commit fd3f732

Browse files
authored
Merge pull request swiftlang#33687 from artemcm/DepScannerOrder
[DependencyScanner] Change the scanner order to resolve placeholders first
2 parents 8b5fb43 + 67eca9b commit fd3f732

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

include/swift/Serialization/ModuleDependencyScanner.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ namespace swift {
1919
/// for the purpose of determining dependencies, but does not attempt to
2020
/// load the module files.
2121
class ModuleDependencyScanner : public SerializedModuleLoaderBase {
22+
public:
23+
enum ScannerKind {
24+
MDS_plain,
25+
MDS_placeholder
26+
};
27+
28+
private:
29+
/// The kind of scanner this is (LLVM-style RTTI)
30+
const ScannerKind kind;
31+
2232
/// The module we're scanning dependencies of.
2333
Identifier moduleName;
2434

@@ -36,10 +46,11 @@ namespace swift {
3646
ModuleDependencyScanner(
3747
ASTContext &ctx, ModuleLoadingMode LoadMode, Identifier moduleName,
3848
InterfaceSubContextDelegate &astDelegate,
39-
ModuleDependenciesKind dependencyKind = ModuleDependenciesKind::Swift)
49+
ModuleDependenciesKind dependencyKind = ModuleDependenciesKind::Swift,
50+
ScannerKind kind = MDS_plain)
4051
: SerializedModuleLoaderBase(ctx, nullptr, LoadMode,
4152
/*IgnoreSwiftSourceInfoFile=*/true),
42-
moduleName(moduleName), astDelegate(astDelegate),
53+
kind(kind), moduleName(moduleName), astDelegate(astDelegate),
4354
dependencyKind(dependencyKind) {}
4455

4556
std::error_code findModuleFilesInDirectory(
@@ -55,6 +66,11 @@ namespace swift {
5566
SmallVectorImpl<Identifier> &names) const override {
5667
llvm_unreachable("Not used");
5768
}
69+
70+
ScannerKind getKind() const { return kind; }
71+
static bool classof(const ModuleDependencyScanner *MDS) {
72+
return MDS->getKind() == MDS_plain;
73+
}
5874
};
5975

6076
/// A ModuleLoader that loads placeholder dependency module stubs specified in
@@ -90,7 +106,8 @@ namespace swift {
90106
StringRef PlaceholderDependencyModuleMap,
91107
InterfaceSubContextDelegate &astDelegate)
92108
: ModuleDependencyScanner(ctx, LoadMode, moduleName, astDelegate,
93-
ModuleDependenciesKind::SwiftPlaceholder) {
109+
ModuleDependenciesKind::SwiftPlaceholder,
110+
MDS_placeholder) {
94111

95112
// FIXME: Find a better place for this map to live, to avoid
96113
// doing the parsing on every module.
@@ -106,5 +123,9 @@ namespace swift {
106123
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
107124
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
108125
bool IsFramework) override;
126+
127+
static bool classof(const ModuleDependencyScanner *MDS) {
128+
return MDS->getKind() == MDS_placeholder;
129+
}
109130
};
110131
}

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,19 @@ Optional<ModuleDependencies> SerializedModuleLoaderBase::getModuleDependencies(
176176
auto moduleId = Ctx.getIdentifier(moduleName);
177177
// Instantiate dependency scanning "loaders".
178178
SmallVector<std::unique_ptr<ModuleDependencyScanner>, 2> scanners;
179-
scanners.push_back(std::make_unique<ModuleDependencyScanner>(
180-
Ctx, LoadMode, moduleId, delegate));
179+
// Placeholder dependencies must be resolved first, to prevent the ModuleDependencyScanner
180+
// from first discovering artifacts of a previous build. Such artifacts are captured
181+
// as compiledModuleCandidates in the dependency graph of the placeholder dependency module
182+
// itself.
181183
scanners.push_back(std::make_unique<PlaceholderSwiftModuleScanner>(
182184
Ctx, LoadMode, moduleId, Ctx.SearchPathOpts.PlaceholderDependencyModuleMap,
183185
delegate));
186+
scanners.push_back(std::make_unique<ModuleDependencyScanner>(
187+
Ctx, LoadMode, moduleId, delegate));
184188

185189
// Check whether there is a module with this name that we can import.
190+
assert(isa<PlaceholderSwiftModuleScanner>(scanners[0].get()) &&
191+
"Expected PlaceholderSwiftModuleScanner as the first dependency scanner loader.");
186192
for (auto &scanner : scanners) {
187193
if (scanner->canImportModule({moduleId, SourceLoc()})) {
188194
// Record the dependencies.

test/ScanDependencies/can_import_placeholder.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@
88
// RUN: echo "\"docPath\": \"%/t/inputs/SomeExternalModule.swiftdoc\"," >> %/t/inputs/map.json
99
// RUN: echo "\"sourceInfoPath\": \"%/t/inputs/SomeExternalModule.swiftsourceinfo\"," >> %/t/inputs/map.json
1010
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
11-
// RUN: echo "}," >> %/t/inputs/map.json
12-
// RUN: echo "{" >> %/t/inputs/map.json
13-
// RUN: echo "\"moduleName\": \"Swift\"," >> %/t/inputs/map.json
14-
// RUN: echo "\"modulePath\": \"%/stdlib_module\"," >> %/t/inputs/map.json
15-
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
16-
// RUN: echo "}," >> %/t/inputs/map.json
17-
// RUN: echo "{" >> %/t/inputs/map.json
18-
// RUN: echo "\"moduleName\": \"SwiftOnoneSupport\"," >> %/t/inputs/map.json
19-
// RUN: echo "\"modulePath\": \"%/ononesupport_module\"," >> %/t/inputs/map.json
20-
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
2111
// RUN: echo "}]" >> %/t/inputs/map.json
2212

2313
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -placeholder-dependency-module-map-file %t/inputs/map.json -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -emit-dependencies -emit-dependencies-path %t/deps.d -import-objc-header %S/Inputs/CHeaders/Bridging.h -swift-version 4

test/ScanDependencies/module_deps_external.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@
88
// RUN: echo "\"docPath\": \"%/t/inputs/SomeExternalModule.swiftdoc\"," >> %/t/inputs/map.json
99
// RUN: echo "\"sourceInfoPath\": \"%/t/inputs/SomeExternalModule.swiftsourceinfo\"," >> %/t/inputs/map.json
1010
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
11-
// RUN: echo "}," >> %/t/inputs/map.json
12-
// RUN: echo "{" >> %/t/inputs/map.json
13-
// RUN: echo "\"moduleName\": \"Swift\"," >> %/t/inputs/map.json
14-
// RUN: echo "\"modulePath\": \"%/stdlib_module\"," >> %/t/inputs/map.json
15-
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
16-
// RUN: echo "}," >> %/t/inputs/map.json
17-
// RUN: echo "{" >> %/t/inputs/map.json
18-
// RUN: echo "\"moduleName\": \"SwiftOnoneSupport\"," >> %/t/inputs/map.json
19-
// RUN: echo "\"modulePath\": \"%/ononesupport_module\"," >> %/t/inputs/map.json
20-
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
2111
// RUN: echo "}]" >> %/t/inputs/map.json
2212

2313
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -placeholder-dependency-module-map-file %t/inputs/map.json -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -emit-dependencies -emit-dependencies-path %t/deps.d -import-objc-header %S/Inputs/CHeaders/Bridging.h -swift-version 4

0 commit comments

Comments
 (0)