Skip to content

Commit e04512f

Browse files
authored
Merge pull request #71161 from artemcm/IncrementalExplicitHeaderFixes
[Explicit Module Builds] Register Clang module dependencies' input `.h` files with the dependency tracker
2 parents 3c91572 + 5fdb695 commit e04512f

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() {
@@ -2252,6 +2252,22 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
22522252
(void) namelookup::getAllImports(result);
22532253
}
22542254

2255+
// Register '.h' inputs of each Clang module dependency with
2256+
// the dependency tracker. In implicit builds such dependencies are registered
2257+
// during the on-demand construction of Clang module. In Explicit Module
2258+
// Builds, since we load pre-built PCMs directly, we do not get to do so. So
2259+
// instead, manually register all `.h` inputs of Clang module dependnecies.
2260+
if (SwiftDependencyTracker &&
2261+
!Instance->getInvocation().getLangOpts().ImplicitModules) {
2262+
auto *moduleFile = Instance->getASTReader()->getModuleManager().lookup(
2263+
clangModule->getASTFile());
2264+
Instance->getASTReader()->visitInputFileInfos(
2265+
*moduleFile, /*IncludeSystem=*/true,
2266+
[&](const clang::serialization::InputFileInfo &IFI, bool isSystem) {
2267+
SwiftDependencyTracker->addDependency(IFI.Filename, isSystem);
2268+
});
2269+
}
2270+
22552271
if (clangModule->isSubModule()) {
22562272
finishLoadingClangModule(clangModule->getTopLevelModule(), importLoc);
22572273
} else {
@@ -2452,7 +2468,8 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
24522468
}
24532469

24542470
ClangImporter::Implementation::Implementation(
2455-
ASTContext &ctx, DWARFImporterDelegate *dwarfImporterDelegate)
2471+
ASTContext &ctx, DependencyTracker *dependencyTracker,
2472+
DWARFImporterDelegate *dwarfImporterDelegate)
24562473
: SwiftContext(ctx), ImportForwardDeclarations(
24572474
ctx.ClangImporterOpts.ImportForwardDeclarations),
24582475
DisableSwiftBridgeAttr(ctx.ClangImporterOpts.DisableSwiftBridgeAttr),
@@ -2468,6 +2485,7 @@ ClangImporter::Implementation::Implementation(
24682485
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),
24692486
platformAvailability(ctx.LangOpts), nameImporter(),
24702487
DisableSourceImport(ctx.ClangImporterOpts.DisableSourceImport),
2488+
SwiftDependencyTracker(dependencyTracker),
24712489
DWARFImporter(dwarfImporterDelegate) {}
24722490

24732491
ClangImporter::Implementation::~Implementation() {

lib/ClangImporter/ImporterImpl.h

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

432432
public:
433-
Implementation(ASTContext &ctx,
433+
Implementation(ASTContext &ctx, DependencyTracker *dependencyTracker,
434434
DWARFImporterDelegate *dwarfImporterDelegate);
435435
~Implementation();
436436

@@ -840,6 +840,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
840840
/// DWARFImporter delegate.
841841
bool DisableSourceImport;
842842

843+
/// File dependency tracker, if installed.
844+
DependencyTracker *SwiftDependencyTracker = nullptr;
845+
843846
/// The DWARF importer delegate, if installed.
844847
DWARFImporterDelegate *DWARFImporter = nullptr;
845848

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ static bool buildModuleFromInterface(CompilerInstance &Instance) {
418418
Instance, Invocation.getClangModuleCachePath(),
419419
FEOpts.BackupModuleInterfaceDir, PrebuiltCachePath, ABIPath, InputPath,
420420
Invocation.getOutputFilename(),
421-
FEOpts.SerializeModuleInterfaceDependencyHashes,
421+
/* shouldSerializeDeps */ true,
422422
Invocation.getSearchPathOptions().CandidateCompiledModules);
423423
else
424424
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)