Skip to content

Commit 5fdb695

Browse files
committed
[Explicit Module Builds] Register Clang module dependencies' input .h files with the dependency tracker
Prior to emission of `.d` and `.swiftdeps` outputs. In implicit builds such dependencies are registered during the construction of the corresponding Clang module by the ClangImporter's built-in Compiler Instance. In explicit builds, since we load pre-built PCMs directly, we do not get to do so. So instead, manually register all `.h` inputs of Clang module dependnecies. Resolves rdar://121354886
1 parent a48dc87 commit 5fdb695

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ ClangImporter::ClangImporter(ASTContext &ctx,
433433
DependencyTracker *tracker,
434434
DWARFImporterDelegate *dwarfImporterDelegate)
435435
: ClangModuleLoader(tracker),
436-
Impl(*new Implementation(ctx, dwarfImporterDelegate)) {
436+
Impl(*new Implementation(ctx, tracker, dwarfImporterDelegate)) {
437437
}
438438

439439
ClangImporter::~ClangImporter() {
@@ -2228,6 +2228,22 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
22282228
(void) namelookup::getAllImports(result);
22292229
}
22302230

2231+
// Register '.h' inputs of each Clang module dependency with
2232+
// the dependency tracker. In implicit builds such dependencies are registered
2233+
// during the on-demand construction of Clang module. In Explicit Module
2234+
// Builds, since we load pre-built PCMs directly, we do not get to do so. So
2235+
// instead, manually register all `.h` inputs of Clang module dependnecies.
2236+
if (SwiftDependencyTracker &&
2237+
!Instance->getInvocation().getLangOpts().ImplicitModules) {
2238+
auto *moduleFile = Instance->getASTReader()->getModuleManager().lookup(
2239+
clangModule->getASTFile());
2240+
Instance->getASTReader()->visitInputFileInfos(
2241+
*moduleFile, /*IncludeSystem=*/true,
2242+
[&](const clang::serialization::InputFileInfo &IFI, bool isSystem) {
2243+
SwiftDependencyTracker->addDependency(IFI.Filename, isSystem);
2244+
});
2245+
}
2246+
22312247
if (clangModule->isSubModule()) {
22322248
finishLoadingClangModule(clangModule->getTopLevelModule(), importLoc);
22332249
} else {
@@ -2428,7 +2444,8 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
24282444
}
24292445

24302446
ClangImporter::Implementation::Implementation(
2431-
ASTContext &ctx, DWARFImporterDelegate *dwarfImporterDelegate)
2447+
ASTContext &ctx, DependencyTracker *dependencyTracker,
2448+
DWARFImporterDelegate *dwarfImporterDelegate)
24322449
: SwiftContext(ctx), ImportForwardDeclarations(
24332450
ctx.ClangImporterOpts.ImportForwardDeclarations),
24342451
DisableSwiftBridgeAttr(ctx.ClangImporterOpts.DisableSwiftBridgeAttr),
@@ -2444,6 +2461,7 @@ ClangImporter::Implementation::Implementation(
24442461
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),
24452462
platformAvailability(ctx.LangOpts), nameImporter(),
24462463
DisableSourceImport(ctx.ClangImporterOpts.DisableSourceImport),
2464+
SwiftDependencyTracker(dependencyTracker),
24472465
DWARFImporter(dwarfImporterDelegate) {}
24482466

24492467
ClangImporter::Implementation::~Implementation() {

lib/ClangImporter/ImporterImpl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
416416
using Version = importer::ImportNameVersion;
417417

418418
public:
419-
Implementation(ASTContext &ctx,
419+
Implementation(ASTContext &ctx, DependencyTracker *dependencyTracker,
420420
DWARFImporterDelegate *dwarfImporterDelegate);
421421
~Implementation();
422422

@@ -826,6 +826,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
826826
/// DWARFImporter delegate.
827827
bool DisableSourceImport;
828828

829+
/// File dependency tracker, if installed.
830+
DependencyTracker *SwiftDependencyTracker = nullptr;
831+
829832
/// The DWARF importer delegate, if installed.
830833
DWARFImporterDelegate *DWARFImporter = nullptr;
831834

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ static bool buildModuleFromInterface(CompilerInstance &Instance) {
417417
Instance, Invocation.getClangModuleCachePath(),
418418
FEOpts.BackupModuleInterfaceDir, PrebuiltCachePath, ABIPath, InputPath,
419419
Invocation.getOutputFilename(),
420-
FEOpts.SerializeModuleInterfaceDependencyHashes,
420+
/* shouldSerializeDeps */ true,
421421
Invocation.getSearchPathOptions().CandidateCompiledModules);
422422
else
423423
return ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// UNSUPPORTED: OS=windows-msvc
2+
// RUN: %empty-directory(%t)
3+
// RUN: mkdir -p %t/inputs
4+
5+
// RUN: split-file %s %t
6+
// RUN: sed -e "s|INPUTSDIR|%/t/inputs|g" %t/map.json.template > %t/map.json.template1
7+
// RUN: sed -e "s|STDLIBMOD|%/stdlib_module|g" %t/map.json.template1 > %t/map.json.template2
8+
// RUN: sed -e "s|ONONEMOD|%/ononesupport_module|g" %t/map.json.template2 > %t/map.json.template3
9+
// RUN: sed -e "s|CHEADERSDIR|%/S/Inputs/CHeaders|g" %t/map.json.template3 > %t/map.json.template4
10+
// RUN: sed -e "s|SWIFTLIBDIR|%swift-lib-dir|g" %t/map.json.template4 > %t/map.json
11+
12+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/inputs/A.swiftmodule -emit-module-doc-path %t/inputs/A.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/inputs/A.swiftsourceinfo -import-underlying-module -I%S/Inputs/CHeaders -module-cache-path %t.module-cache %t/A.swift -module-name A -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import
13+
// RUN: %target-swift-emit-pcm -module-name A -o %t/inputs/A.pcm %S/Inputs/CHeaders/module.modulemap
14+
// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift-lib-dir/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm
15+
16+
// RUN: %target-swift-frontend -emit-module-path %t/Foo.swiftmodule -disable-implicit-swift-modules -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -module-cache-path %t.module-cache -explicit-swift-module-map-file %t/map.json -primary-file %t/test.swift -emit-dependencies-path %t/test.d -emit-reference-dependencies-path %t/test.swiftdeps -o %t/test.o
17+
18+
// Verify that make-style dependencies contain header files of transitive clang module dependency
19+
// RUN: cat %t/test.d | %FileCheck %s -check-prefix=MAKEDEP-CHECK
20+
// MAKEDEP-CHECK: {{.*}}test{{/|\\}}ScanDependencies{{/|\\}}Inputs{{/|\\}}CHeaders{{/|\\}}A.h
21+
22+
// Verify that Swift reference dependencies contain header files of transitive clang module dependency
23+
// RUN: llvm-bcanalyzer --dump %t/test.swiftdeps | %FileCheck %s -check-prefix=SWIFTDEPS-CHECK
24+
// SWIFTDEPS-CHECK: <IDENTIFIER_NODE abbrevid=8/> blob data = '{{.*}}test{{/|\\}}ScanDependencies{{/|\\}}Inputs{{/|\\}}CHeaders{{/|\\}}A.h'
25+
26+
27+
//--- A.swift
28+
public func anotherFuncA() {}
29+
30+
//--- map.json.template
31+
[
32+
{
33+
"moduleName": "A",
34+
"modulePath": "INPUTSDIR/A.swiftmodule",
35+
"docPath": "INPUTSDIR/A.swiftdoc",
36+
"sourceInfoPath": "INPUTSDIR/A.swiftsourceinfo",
37+
"isFramework": false,
38+
},
39+
{
40+
"moduleName": "A",
41+
"clangModulePath": "INPUTSDIR/A.pcm",
42+
"clangModuleMapPath": "CHEADERSDIR/module.modulemap"
43+
},
44+
{
45+
"moduleName": "Swift",
46+
"modulePath": "STDLIBMOD",
47+
"isFramework": false
48+
},
49+
{
50+
"moduleName": "SwiftOnoneSupport",
51+
"modulePath": "ONONEMOD",
52+
"isFramework": false
53+
},
54+
{
55+
"moduleName": "SwiftShims",
56+
"isFramework": false,
57+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
58+
"clangModulePath": "INPUTSDIR/SwiftShims.pcm"
59+
}]
60+
61+
//--- test.swift
62+
import A
63+
64+
func callA() {
65+
funcA()
66+
anotherFuncA()
67+
}

0 commit comments

Comments
 (0)