Skip to content

Commit 41364ab

Browse files
Merge pull request swiftlang#66000 from cachemeifyoucan/eng/PR-109411245-release-5.9
[ObjcHeader] Fix objc header generation when pch is explicited passed
2 parents 6227e4d + b7039d2 commit 41364ab

File tree

9 files changed

+68
-6
lines changed

9 files changed

+68
-6
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ class ClangImporter final : public ClangModuleLoader {
404404
/// if we need to persist a PCH for later reuse.
405405
bool canReadPCH(StringRef PCHFilename);
406406

407+
/// Reads the original source file name from PCH.
408+
std::string getOriginalSourceFile(StringRef PCHFilename);
409+
407410
/// Makes a temporary replica of the ClangImporter's CompilerInstance, reads a
408411
/// module map into the replica and emits a PCM file for one of the modules it
409412
/// declares. Delegates to clang for everything except construction of the

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ class CompilerInstance {
616616
/// file.
617617
SourceFile *getIDEInspectionFile() const;
618618

619+
/// Retrieve the printing path for bridging header.
620+
std::string getBridgingHeaderPath() const;
621+
619622
private:
620623
/// Set up the file system by loading and validating all VFS overlay YAML
621624
/// files. If the process of validating VFS files failed, or the overlay

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,12 @@ bool ClangImporter::canReadPCH(StringRef PCHFilename) {
913913
llvm_unreachable("unhandled result");
914914
}
915915

916+
std::string ClangImporter::getOriginalSourceFile(StringRef PCHFilename) {
917+
return clang::ASTReader::getOriginalSourceFile(
918+
PCHFilename.str(), Impl.Instance->getFileManager(),
919+
Impl.Instance->getPCHContainerReader(), Impl.Instance->getDiagnostics());
920+
}
921+
916922
Optional<std::string>
917923
ClangImporter::getPCHFilename(const ClangImporterOptions &ImporterOptions,
918924
StringRef SwiftPCHHash, bool &isExplicit) {

lib/Frontend/Frontend.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,26 @@ SourceFile *CompilerInstance::getIDEInspectionFile() const {
703703
return evaluateOrDefault(eval, IDEInspectionFileRequest{mod}, nullptr);
704704
}
705705

706+
static inline bool isPCHFilenameExtension(StringRef path) {
707+
return llvm::sys::path::extension(path)
708+
.endswith(file_types::getExtension(file_types::TY_PCH));
709+
}
710+
711+
std::string CompilerInstance::getBridgingHeaderPath() const {
712+
const FrontendOptions &opts = Invocation.getFrontendOptions();
713+
if (!isPCHFilenameExtension(opts.ImplicitObjCHeaderPath))
714+
return opts.ImplicitObjCHeaderPath;
715+
716+
auto clangImporter =
717+
static_cast<ClangImporter *>(getASTContext().getClangModuleLoader());
718+
719+
// No clang importer created. Report error?
720+
if (!clangImporter)
721+
return std::string();
722+
723+
return clangImporter->getOriginalSourceFile(opts.ImplicitObjCHeaderPath);
724+
}
725+
706726
bool CompilerInstance::setUpInputs() {
707727
// Adds to InputSourceCodeBufferIDs, so may need to happen before the
708728
// per-input setup.

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -911,17 +911,14 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
911911

912912
if ((!Context.hadError() || opts.AllowModuleWithCompilerErrors) &&
913913
opts.InputsAndOutputs.hasClangHeaderOutputPath()) {
914-
std::string BridgingHeaderPathForPrint;
915-
if (!opts.ImplicitObjCHeaderPath.empty()) {
914+
std::string BridgingHeaderPathForPrint = Instance.getBridgingHeaderPath();
915+
if (!BridgingHeaderPathForPrint.empty()) {
916916
if (opts.BridgingHeaderDirForPrint.has_value()) {
917917
// User specified preferred directory for including, use that dir.
918918
llvm::SmallString<32> Buffer(*opts.BridgingHeaderDirForPrint);
919919
llvm::sys::path::append(Buffer,
920-
llvm::sys::path::filename(opts.ImplicitObjCHeaderPath));
920+
llvm::sys::path::filename(BridgingHeaderPathForPrint));
921921
BridgingHeaderPathForPrint = (std::string)Buffer;
922-
} else {
923-
// By default, include the given bridging header path directly.
924-
BridgingHeaderPathForPrint = opts.ImplicitObjCHeaderPath;
925922
}
926923
}
927924
hadAnyError |= printAsClangHeaderIfNeeded(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import Foundation;
2+
3+
@protocol TestProto
4+
@property id strongProp;
5+
@end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#import <Foundation.h>
2+
3+
@interface ObjCClass : NSObject
4+
5+
- (nullable id)swiftMethod;
6+
7+
@end
8+

test/PrintAsObjC/Inputs/custom-modules/module.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,8 @@ module objc_implementation {
6363
header "objc_implementation/objc_implementation.h"
6464
export *
6565
}
66+
67+
module bridging_header {
68+
header "bridging_header/bridging_header.h"
69+
export *
70+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// REQUIRES: objc_interop
2+
3+
// RUN: %empty-directory(%t)
4+
5+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-pch -o %t/bridging-header.pch %S/Inputs/bridging_header-Bridging-Header.h
6+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -I %S/Inputs/custom-modules -import-objc-header %t/bridging-header.pch -import-underlying-module -o %t %s -disable-objc-attr-requires-foundation-module -emit-objc-header-path %t/bridging_header-Swift.h
7+
8+
// RUN: %FileCheck %s --input-file %t/bridging_header-Swift.h
9+
10+
// CHECK: bridging_header-Bridging-Header.h
11+
// CHECK-NOT: bridging-header.pch
12+
13+
@objc class Test : NSObject, TestProto {
14+
var strongProp: Any?
15+
}

0 commit comments

Comments
 (0)