Skip to content

Commit 6666f98

Browse files
committed
[Bridging PCH] Handle mixed PCH + implicit bridging header imports.
This happens fairly regularly in unit testing scenarios.
1 parent b0222c3 commit 6666f98

7 files changed

+64
-3
lines changed

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ clang::NamedDecl *SwiftLookupTable::resolveContext(StringRef unresolvedName) {
322322
}
323323

324324
void SwiftLookupTable::addCategory(clang::ObjCCategoryDecl *category) {
325-
assert(!Reader && "Cannot modify a lookup table stored on disk");
325+
// Force deserialization to occur before appending.
326+
(void) categories();
326327

327328
// Add the category.
328329
Categories.push_back(category);
@@ -467,8 +468,6 @@ bool SwiftLookupTable::addLocalEntry(SingleEntry newEntry,
467468
void SwiftLookupTable::addEntry(DeclName name, SingleEntry newEntry,
468469
EffectiveClangContext effectiveContext,
469470
const clang::Preprocessor *PP) {
470-
assert(!Reader && "Cannot modify a lookup table stored on disk");
471-
472471
// Translate the context.
473472
auto contextOpt = translateContext(effectiveContext);
474473
if (!contextOpt) {
@@ -484,6 +483,9 @@ void SwiftLookupTable::addEntry(DeclName name, SingleEntry newEntry,
484483
return;
485484
}
486485

486+
// Populate cache from reader if necessary.
487+
findOrCreate(name.getBaseName().str());
488+
487489
auto context = *contextOpt;
488490

489491
// If this is a global imported as a member, record is as such.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
static inline int app_function(int x) {
2+
return x + 27;
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Foundation
2+
3+
public func AppFunc() -> Int32 {
4+
return app_function(10)
5+
}
6+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import "app-bridging-header-to-pch.h"
2+
3+
static inline int unit_test_function(int x) {
4+
return x + 28;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
static inline int unit_test_function(int x) {
2+
return x + 28;
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// REQUIRES: objc_interop
2+
// RUN: rm -rf %t && mkdir -p %t/tmp
3+
// RUN: %target-swiftc_driver -emit-module -import-objc-header %S/Inputs/app-bridging-header-to-pch.h -module-name App -emit-module-path %t/App.swiftmodule %S/Inputs/app-that-uses-pch-bridging-header.swift
4+
// RUN: llvm-bcanalyzer -dump %t/App.swiftmodule | %FileCheck %s
5+
// CHECK: IMPORTED_HEADER{{.*}}Inputs/app-bridging-header-to-pch.h
6+
7+
// Should get no warnings when we PCH-in the chained unit-test bridging header (thereby suppressing implicit import)
8+
// RUN: %target-swiftc_driver -D UNIT_TESTS -typecheck -Xfrontend -verify -import-objc-header %S/Inputs/chained-unit-test-bridging-header-to-pch.h -I %S/Inputs -I %t %s
9+
10+
// Should get no warnings when we PCH-in the app bridging header (thereby suppressing implicit import)
11+
// RUN: %target-swiftc_driver -typecheck -Xfrontend -verify -import-objc-header %S/Inputs/app-bridging-header-to-pch.h -I %t %s
12+
13+
import App
14+
15+
func test_all() {
16+
#if UNIT_TESTS
17+
let _ = unit_test_function(AppFunc())
18+
#else
19+
let _ = app_function(AppFunc())
20+
#endif
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// REQUIRES: objc_interop
2+
// RUN: rm -rf %t && mkdir -p %t/tmp
3+
// RUN: %target-swiftc_driver -emit-module -import-objc-header %S/Inputs/app-bridging-header-to-pch.h -module-name App -emit-module-path %t/App.swiftmodule %S/Inputs/app-that-uses-pch-bridging-header.swift
4+
// RUN: llvm-bcanalyzer -dump %t/App.swiftmodule | %FileCheck %s
5+
// CHECK: IMPORTED_HEADER{{.*}}Inputs/app-bridging-header-to-pch.h
6+
7+
// Should get a warning when we PCH-in the unit test header and then implicitly import the app header.
8+
// RUN: %target-swiftc_driver -D UNIT_TESTS -typecheck -Xfrontend -verify -import-objc-header %S/Inputs/unit-test-bridging-header-to-pch.h -I %t %s
9+
10+
// Should get a warning when skip the unit test header entirely and implicitly import the app header.
11+
// RUN: %target-swiftc_driver -typecheck -Xfrontend -verify -I %t %s
12+
13+
import App // expected-warning{{implicit import of bridging header 'app-bridging-header-to-pch.h' via module 'App' is deprecated and will be removed in a later version of Swift}}
14+
15+
func test_all() {
16+
#if UNIT_TESTS
17+
let _ = unit_test_function(AppFunc())
18+
#else
19+
let _ = app_function(AppFunc())
20+
#endif
21+
}

0 commit comments

Comments
 (0)