Skip to content

Commit b19606c

Browse files
committed
[Scan dependencies] Find overlays associated with imported Clang modules.
1 parent 33cdd61 commit b19606c

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

lib/FrontendTool/ScanDependencies.cpp

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,36 @@
2121
#include "swift/Basic/Defer.h"
2222
#include "swift/Basic/LLVM.h"
2323
#include "swift/Basic/STLExtras.h"
24-
#include "swift/ClangImporter/ClangImporter.h"
2524
#include "swift/Frontend/FrontendOptions.h"
2625
#include "clang/Basic/Module.h"
2726
#include "llvm/ADT/SetVector.h"
2827
#include "llvm/ADT/StringMap.h"
2928
#include "llvm/ADT/StringRef.h"
29+
#include "llvm/ADT/StringSet.h"
3030
#include "llvm/Support/FileSystem.h"
3131
#include <set>
3232

3333
using namespace swift;
3434

35+
/// Find all of the imported Clang modules starting with the given module name.
36+
static void findAllImportedClangModules(ASTContext &ctx, StringRef moduleName,
37+
ModuleDependenciesCache &cache,
38+
std::vector<std::string> &allModules,
39+
llvm::StringSet<> &knownModules) {
40+
if (!knownModules.insert(moduleName).second)
41+
return;
42+
allModules.push_back(moduleName);
43+
44+
auto dependencies = cache.findDependencies(
45+
moduleName, ModuleDependenciesKind::Clang);
46+
if (!dependencies)
47+
return;
48+
49+
for (const auto &dep : dependencies->getModuleDependencies()) {
50+
findAllImportedClangModules(ctx, dep, cache, allModules, knownModules);
51+
}
52+
}
53+
3554
/// Resolve the direct dependencies of the given module.
3655
static std::vector<ModuleDependencyID> resolveDirectDependencies(
3756
ASTContext &ctx, ModuleDependencyID module,
@@ -55,7 +74,25 @@ static std::vector<ModuleDependencyID> resolveDirectDependencies(
5574
// For a Swift module, look for overlays for each of the Clang modules it
5675
// depends on.
5776
if (isSwift) {
58-
// FIXME: Implement this.
77+
// Find all of the Clang modules this Swift module depends on.
78+
std::vector<std::string> allClangModules;
79+
llvm::StringSet<> knownModules;
80+
for (const auto &dep : result) {
81+
if (dep.second != ModuleDependenciesKind::Clang)
82+
continue;
83+
84+
findAllImportedClangModules(ctx, dep.first, cache, allClangModules,
85+
knownModules);
86+
}
87+
88+
// Look for overlays for each of the Clang modules.
89+
for (const auto &clangDep : allClangModules) {
90+
if (auto found = ctx.getModuleDependencies(
91+
clangDep, /*onlyClangModule=*/false, cache)) {
92+
if (found->getKind() == ModuleDependenciesKind::Swift)
93+
result.push_back({clangDep, found->getKind()});
94+
}
95+
}
5996
}
6097

6198
return result;
@@ -317,7 +354,7 @@ bool swift::scanDependencies(ASTContext &Context, ModuleDecl *mainModule,
317354
writeJSON(out, Context, cache, allModules.getArrayRef());
318355

319356
// This process succeeds regardless of whether any errors occurred.
320-
// FIXME: We shouldn't need this, but it's masking bugs on our scanning
357+
// FIXME: We shouldn't need this, but it's masking bugs in our scanning
321358
// logic where we don't create a fresh context when scanning Swift interfaces
322359
// that includes their own command-line flags.
323360
Context.Diags.resetHadAnyError();

test/ScanDependencies/module_deps.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ import E
4444
// CHECK: "moduleInterfacePath"
4545
// CHECK-SAME: E.swiftinterface
4646

47+
/// --------Swift module A
48+
// CHECK-LABEL: "modulePath": "A.swiftmodule",
49+
50+
// CHECK: directDependencies
51+
// CHECK-NEXT: {
52+
// CHECK-NEXT: "clang": "A"
53+
// CHECK-NEXT: }
54+
4755
/// --------Clang module B
4856
// CHECK-LABEL: "modulePath": "B.pcm"
4957

@@ -63,13 +71,5 @@ import E
6371
// CHECK-NEXT: {
6472
// CHECK-NEXT: "clang": "SwiftShims"
6573

66-
/// --------Swift module A
67-
// CHECK-LABEL: "modulePath": "A.swiftmodule",
68-
69-
// CHECK: directDependencies
70-
// CHECK-NEXT: {
71-
// CHECK-NEXT: "clang": "A"
72-
// CHECK-NEXT: }
73-
7474
/// --------Clang module SwiftShims
7575
// CHECK-LABEL: "modulePath": "SwiftShims.pcm",

0 commit comments

Comments
 (0)