Skip to content

[ObjcHeader] Fix objc header generation when pch is explicited passed #66000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ class ClangImporter final : public ClangModuleLoader {
/// if we need to persist a PCH for later reuse.
bool canReadPCH(StringRef PCHFilename);

/// Reads the original source file name from PCH.
std::string getOriginalSourceFile(StringRef PCHFilename);

/// Makes a temporary replica of the ClangImporter's CompilerInstance, reads a
/// module map into the replica and emits a PCM file for one of the modules it
/// declares. Delegates to clang for everything except construction of the
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ class CompilerInstance {
/// file.
SourceFile *getIDEInspectionFile() const;

/// Retrieve the printing path for bridging header.
std::string getBridgingHeaderPath() const;

private:
/// Set up the file system by loading and validating all VFS overlay YAML
/// files. If the process of validating VFS files failed, or the overlay
Expand Down
6 changes: 6 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,12 @@ bool ClangImporter::canReadPCH(StringRef PCHFilename) {
llvm_unreachable("unhandled result");
}

std::string ClangImporter::getOriginalSourceFile(StringRef PCHFilename) {
return clang::ASTReader::getOriginalSourceFile(
PCHFilename.str(), Impl.Instance->getFileManager(),
Impl.Instance->getPCHContainerReader(), Impl.Instance->getDiagnostics());
}

Optional<std::string>
ClangImporter::getPCHFilename(const ClangImporterOptions &ImporterOptions,
StringRef SwiftPCHHash, bool &isExplicit) {
Expand Down
20 changes: 20 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,26 @@ SourceFile *CompilerInstance::getIDEInspectionFile() const {
return evaluateOrDefault(eval, IDEInspectionFileRequest{mod}, nullptr);
}

static inline bool isPCHFilenameExtension(StringRef path) {
return llvm::sys::path::extension(path)
.endswith(file_types::getExtension(file_types::TY_PCH));
}

std::string CompilerInstance::getBridgingHeaderPath() const {
const FrontendOptions &opts = Invocation.getFrontendOptions();
if (!isPCHFilenameExtension(opts.ImplicitObjCHeaderPath))
return opts.ImplicitObjCHeaderPath;

auto clangImporter =
static_cast<ClangImporter *>(getASTContext().getClangModuleLoader());

// No clang importer created. Report error?
if (!clangImporter)
return std::string();

return clangImporter->getOriginalSourceFile(opts.ImplicitObjCHeaderPath);
}

bool CompilerInstance::setUpInputs() {
// Adds to InputSourceCodeBufferIDs, so may need to happen before the
// per-input setup.
Expand Down
9 changes: 3 additions & 6 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,17 +911,14 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(

if ((!Context.hadError() || opts.AllowModuleWithCompilerErrors) &&
opts.InputsAndOutputs.hasClangHeaderOutputPath()) {
std::string BridgingHeaderPathForPrint;
if (!opts.ImplicitObjCHeaderPath.empty()) {
std::string BridgingHeaderPathForPrint = Instance.getBridgingHeaderPath();
if (!BridgingHeaderPathForPrint.empty()) {
if (opts.BridgingHeaderDirForPrint.has_value()) {
// User specified preferred directory for including, use that dir.
llvm::SmallString<32> Buffer(*opts.BridgingHeaderDirForPrint);
llvm::sys::path::append(Buffer,
llvm::sys::path::filename(opts.ImplicitObjCHeaderPath));
llvm::sys::path::filename(BridgingHeaderPathForPrint));
BridgingHeaderPathForPrint = (std::string)Buffer;
} else {
// By default, include the given bridging header path directly.
BridgingHeaderPathForPrint = opts.ImplicitObjCHeaderPath;
}
}
hadAnyError |= printAsClangHeaderIfNeeded(
Expand Down
5 changes: 5 additions & 0 deletions test/PrintAsObjC/Inputs/bridging_header-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import Foundation;

@protocol TestProto
@property id strongProp;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation.h>

@interface ObjCClass : NSObject

- (nullable id)swiftMethod;

@end

5 changes: 5 additions & 0 deletions test/PrintAsObjC/Inputs/custom-modules/module.map
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ module objc_implementation {
header "objc_implementation/objc_implementation.h"
export *
}

module bridging_header {
header "bridging_header/bridging_header.h"
export *
}
15 changes: 15 additions & 0 deletions test/PrintAsObjC/bridging_header.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// REQUIRES: objc_interop

// RUN: %empty-directory(%t)

// 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
// 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

// RUN: %FileCheck %s --input-file %t/bridging_header-Swift.h

// CHECK: bridging_header-Bridging-Header.h
// CHECK-NOT: bridging-header.pch

@objc class Test : NSObject, TestProto {
var strongProp: Any?
}