Skip to content

Commit 94d8004

Browse files
[BridgingHeader] Implicit import bridging header from CAS module
The binary module built from a CAS build will have the embeded bridging header info with 0 modTime. Allow a regular build to import such a module with the same behavior as if the module is built from a regular build. rdar://126221616
1 parent abfe5e3 commit 94d8004

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,8 +1717,13 @@ bool ClangImporter::importHeader(StringRef header, ModuleDecl *adapter,
17171717
StringRef cachedContents, SourceLoc diagLoc) {
17181718
clang::FileManager &fileManager = Impl.Instance->getFileManager();
17191719
auto headerFile = fileManager.getFile(header, /*OpenFile=*/true);
1720+
// Prefer importing the header directly if the header content matches by
1721+
// checking size and mod time. This allows correct import if some no-modular
1722+
// headers are already imported into clang importer. If mod time is zero, then
1723+
// the module should be built from CAS and there is no mod time to verify.
17201724
if (headerFile && (*headerFile)->getSize() == expectedSize &&
1721-
(*headerFile)->getModificationTime() == expectedModTime) {
1725+
(expectedModTime == 0 ||
1726+
(*headerFile)->getModificationTime() == expectedModTime)) {
17221727
return importBridgingHeader(header, adapter, diagLoc, false, true);
17231728
}
17241729

test/CAS/bridging-header.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,28 @@
6262
// RUN: %target-swift-frontend -cache-compile-job -module-name Test -O -cas-path %t/cas @%t/MyApp.cmd %t/test.swift \
6363
// RUN: -emit-module -o %t/Test.swiftmodule
6464

65+
/// Importing binary module with bridging header built from CAS from a regluar build.
66+
/// This should succeed even it is also importing a bridging header that shares same header dependencies (with proper header guard).
6567
// RUN: %target-swift-frontend -typecheck -module-name User -swift-version 5 \
6668
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import \
6769
// RUN: -Xcc -fmodule-map-file=%t/a.modulemap -Xcc -fmodule-map-file=%t/b.modulemap \
68-
// RUN: -I %t %t/user.swift
70+
// RUN: -I %t %t/user.swift -import-objc-header %t/Bridging2.h
71+
72+
/// Importing binary module with bridging header built from CAS from a cached build. This should work without additional bridging header deps.
73+
// RUN: %target-swift-frontend -scan-dependencies -module-name User -module-cache-path %t/clang-module-cache -O \
74+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import \
75+
// RUN: %t/user.swift -o %t/deps2.json -swift-version 5 -cache-compile-job -cas-path %t/cas \
76+
// RUN: -Xcc -fmodule-map-file=%t/a.modulemap -Xcc -fmodule-map-file=%t/b.modulemap -I %t
77+
78+
// RUN: %{python} %S/Inputs/GenerateExplicitModuleMap.py %t/deps2.json > %t/map2.json
79+
// RUN: llvm-cas --cas %t/cas --make-blob --data %t/map2.json > %t/map2.casid
80+
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps2.json User > %t/User.cmd
81+
// RUN: %target-swift-frontend -cache-compile-job -module-name User -O -cas-path %t/cas \
82+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -disable-implicit-swift-modules \
83+
// RUN: -explicit-swift-module-map-file @%t/map2.casid @%t/User.cmd %t/user.swift \
84+
// RUN: -emit-module -o %t/User.swiftmodule
6985

7086
//--- test.swift
71-
import B
7287
public func test() {
7388
b()
7489
}
@@ -82,14 +97,31 @@ func user() {
8297
test()
8398
}
8499

100+
extension A {
101+
public func testA() {}
102+
}
103+
104+
85105
//--- Bridging.h
86106
#include "Foo.h"
107+
#include "Foo2.h"
108+
109+
//--- Bridging2.h
110+
#include "Foo.h"
111+
#include "Foo2.h"
87112

88113
//--- Foo.h
89114
#import "a.h"
90115

116+
//--- Foo2.h
117+
#pragma once
118+
int Foo = 0;
119+
91120
//--- a.h
92121
#include "b.h"
122+
struct A {
123+
int a;
124+
};
93125

94126
//--- b.h
95127
void b(void);

0 commit comments

Comments
 (0)