Skip to content

Commit e40a275

Browse files
committed
[Clang importer] Ensure that we check a bridging PCH in a source file context.
When checking whether we can load a bridging precompiled header (PCH), we end up parsing module maps. If Clang emits a warning or error while parsing the module maps we encounter, the Swift compiler would crash because we have not properly established the invariants of Clang's diagnostic engine. Perform a pairsed set of BeginSourceFile/EndSourceFile calls on the Clang diagnostic consumer to set up the appropriate state. Fixes rdar://problem/57626886.
1 parent 9cbc761 commit e40a275

File tree

7 files changed

+27
-0
lines changed

7 files changed

+27
-0
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ bool ClangImporter::canReadPCH(StringRef PCHFilename) {
800800
auto FID = clangSrcMgr.createFileID(
801801
llvm::make_unique<ZeroFilledMemoryBuffer>(1, "<main>"));
802802
clangSrcMgr.setMainFileID(FID);
803+
auto &diagConsumer = CI.getDiagnosticClient();
804+
diagConsumer.BeginSourceFile(CI.getLangOpts());
805+
SWIFT_DEFER {
806+
diagConsumer.EndSourceFile();
807+
};
803808

804809
// Pass in TU_Complete, which is the default mode for the Preprocessor
805810
// constructor and the right one for reading a PCH.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* No content */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
framework module PrivateWarning {
2+
header "PrivateWarning.h"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
framework module PrivateWarning.Private {
2+
header "PrivateWarning_Private.h"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* No content */
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import PrivateWarning;
2+
@import PrivateWarning.Private;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: rm -f %t.*
2+
3+
// Build a bridging PCH for involving a module map that contains a warning
4+
// RUN: %target-swift-frontend -F %S/Inputs/ModuleMapWarning -emit-pch %S/Inputs/ModuleMapWarning/bridging-pch.h -pch-output-dir %t/pch 2> %t.stderr
5+
// RUN: %FileCheck %s < %t.stderr
6+
7+
// CHECK: module.private.modulemap:1:33: warning: private submodule 'PrivateWarning.Private' in private module map, expected top-level module
8+
9+
// Check that loading that bridging PCH Does not crash the compiler.
10+
// RUN: %target-swift-frontend -F %S/Inputs/ModuleMapWarning -import-objc-header %S/Inputs/ModuleMapWarning/bridging-pch.h -pch-output-dir %t/pch -typecheck %s
11+
12+

0 commit comments

Comments
 (0)