Skip to content

Commit

Permalink
[ModuleInterface] Propagate dependencies to outer DependencyTracker f…
Browse files Browse the repository at this point in the history
…or use in .d files.
  • Loading branch information
graydon committed Nov 8, 2018
1 parent c174359 commit 40f30ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion include/swift/AST/ModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class DependencyTracker {

/// \brief Abstract interface that loads named modules into the AST.
class ModuleLoader {
DependencyTracker * const dependencyTracker;
virtual void anchor();

protected:
DependencyTracker * const dependencyTracker;
ModuleLoader(DependencyTracker *tracker) : dependencyTracker(tracker) {}

void addDependency(StringRef file, bool IsSystem=false) {
Expand Down
20 changes: 14 additions & 6 deletions lib/Frontend/ParseableInterfaceSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ static bool
swiftModuleIsUpToDate(clang::vfs::FileSystem &FS,
StringRef ModuleCachePath,
StringRef OutPath,
DiagnosticEngine &Diags) {
DiagnosticEngine &Diags,
DependencyTracker *OuterTracker) {

auto OutBuf = FS.getBufferForFile(OutPath);
if (!OutBuf)
Expand All @@ -188,6 +189,8 @@ swiftModuleIsUpToDate(clang::vfs::FileSystem &FS,
return false;

for (auto In : AllDeps) {
if (OuterTracker)
OuterTracker->addDependency(In.Path, /*IsSystem=*/false);
auto DepBuf = getBufferOfDependency(FS, OutPath, In.Path, Diags);
if (!DepBuf ||
DepBuf->getBufferSize() != In.Size ||
Expand Down Expand Up @@ -217,13 +220,16 @@ collectDepsForSerialization(clang::vfs::FileSystem &FS,
CompilerInstance &SubInstance,
StringRef InPath, StringRef ModuleCachePath,
SmallVectorImpl<FileDependency> &Deps,
DiagnosticEngine &Diags) {
DiagnosticEngine &Diags,
DependencyTracker *OuterTracker) {
auto DTDeps = SubInstance.getDependencyTracker()->getDependencies();
SmallVector<StringRef, 16> InitialDepNames(DTDeps.begin(), DTDeps.end());
InitialDepNames.push_back(InPath);
llvm::StringSet<> AllDepNames;
for (auto const &DepName : InitialDepNames) {
AllDepNames.insert(DepName);
if (OuterTracker)
OuterTracker->addDependency(DepName, /*IsSystem=*/false);
auto DepBuf = getBufferOfDependency(FS, InPath, DepName, Diags);
if (!DepBuf) {
return true;
Expand Down Expand Up @@ -254,6 +260,8 @@ collectDepsForSerialization(clang::vfs::FileSystem &FS,
if (AllDepNames.count(SubDep.Path) == 0) {
AllDepNames.insert(SubDep.Path);
Deps.push_back(SubDep);
if (OuterTracker)
OuterTracker->addDependency(SubDep.Path, /*IsSystem=*/false);
}
}
}
Expand All @@ -264,7 +272,7 @@ collectDepsForSerialization(clang::vfs::FileSystem &FS,
static bool buildSwiftModuleFromSwiftInterface(
clang::vfs::FileSystem &FS, DiagnosticEngine &Diags,
CompilerInvocation &SubInvocation, StringRef InPath, StringRef OutPath,
StringRef ModuleCachePath) {
StringRef ModuleCachePath, DependencyTracker *OuterTracker) {
bool SubError = false;
bool RunSuccess = llvm::CrashRecoveryContext().RunSafelyOnThread([&] {

Expand Down Expand Up @@ -327,7 +335,7 @@ static bool buildSwiftModuleFromSwiftInterface(
SerializationOpts.SerializeAllSIL = true;
SmallVector<FileDependency, 16> Deps;
if (collectDepsForSerialization(FS, SubInstance, InPath, ModuleCachePath,
Deps, Diags)) {
Deps, Diags, OuterTracker)) {
SubError = true;
return;
}
Expand Down Expand Up @@ -368,9 +376,9 @@ std::error_code ParseableInterfaceModuleLoader::openModuleFiles(
configureSubInvocationAndOutputPaths(SubInvocation, InPath, OutPath);

// Evaluate if we need to run this sub-invocation, and if so run it.
if (!swiftModuleIsUpToDate(FS, CacheDir, OutPath, Diags)) {
if (!swiftModuleIsUpToDate(FS, CacheDir, OutPath, Diags, dependencyTracker)) {
if (buildSwiftModuleFromSwiftInterface(FS, Diags, SubInvocation, InPath,
OutPath, CacheDir))
OutPath, CacheDir, dependencyTracker))
return std::make_error_code(std::errc::invalid_argument);
}

Expand Down
13 changes: 11 additions & 2 deletions test/ParseableInterface/ModuleCache/module-cache-init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,26 @@
//
// Phase 3: build TestModule into a .swiftmodule explicitly us OtherModule via OtherModule.swiftinterface, creating OtherModule-*.swiftmodule along the way.
//
// RUN: %target-swift-frontend -I %t -module-cache-path %t/modulecache -enable-parseable-module-interface -emit-module -o %t/TestModule.swiftmodule -module-name TestModule %s
// RUN: %target-swift-frontend -I %t -module-cache-path %t/modulecache -enable-parseable-module-interface -emit-module -emit-dependencies -emit-dependencies-path %t/TestModule.d -o %t/TestModule.swiftmodule -module-name TestModule %s
// RUN: test -f %t/TestModule.swiftmodule
// RUN: test -f %t/modulecache/OtherModule-*.swiftmodule
// RUN: test -f %t/TestModule.d
// RUN: llvm-bcanalyzer -dump %t/modulecache/OtherModule-*.swiftmodule | %FileCheck %s -check-prefix=CHECK-OTHERMODULE
// RUN: %FileCheck %s -check-prefix=CHECK-DEPENDS <%t/TestModule.d
// CHECK-OTHERMODULE: {{MODULE_NAME.*blob data = 'OtherModule'}}
// CHECK-OTHERMODULE: {{FILE_DEPENDENCY.*Swift.swiftmodule'}}
// CHECK-OTHERMODULE: {{FILE_DEPENDENCY.*SwiftOnoneSupport.swiftmodule'}}
// CHECK-OTHERMODULE: {{FILE_DEPENDENCY.*LeafModule-.*.swiftmodule'}}
// CHECK-OTHERMODULE: {{FILE_DEPENDENCY.*LeafModule.swiftinterface'}}
// CHECK-OTHERMODULE: {{FILE_DEPENDENCY.*LeafModule-.*.swiftmodule'}}
// CHECK-OTHERMODULE: {{FILE_DEPENDENCY.*OtherModule.swiftinterface'}}
// CHECK-OTHERMODULE: FUNC_DECL
// CHECK-DEPENDS: TestModule.swiftmodule :
// CHECK-DEPENDS-SAME: LeafModule.swiftinterface
// CHECK-DEPENDS-SAME: OtherModule.swiftinterface
// CHECK-DEPENDS-SAME: {{LeafModule-[^ ]+.swiftmodule}}
// CHECK-DEPENDS-SAME: {{OtherModule-[^ ]+.swiftmodule}}
// CHECK-DEPENDS-SAME: Swift.swiftmodule
// CHECK-DEPENDS-SAME: SwiftOnoneSupport.swiftmodule

import OtherModule

Expand Down

0 comments on commit 40f30ce

Please sign in to comment.