mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[ObjcHeader] Fix objc header generation when pch is explicited passed
When swift-frontend is explicitly passed the pch file as bridging header on command-line through `-import-objc-header`, it needs to print the original source file name if needed to the generated objc header. rdar://109411245
This commit is contained in:
@@ -403,6 +403,9 @@ public:
|
||||
/// 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
|
||||
|
||||
@@ -667,6 +667,9 @@ public:
|
||||
/// 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
|
||||
|
||||
@@ -915,6 +915,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) {
|
||||
|
||||
@@ -818,6 +818,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.
|
||||
|
||||
@@ -912,17 +912,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(
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
@import Foundation;
|
||||
|
||||
@protocol TestProto
|
||||
@property id strongProp;
|
||||
@end
|
||||
@@ -0,0 +1,8 @@
|
||||
#import <Foundation.h>
|
||||
|
||||
@interface ObjCClass : NSObject
|
||||
|
||||
- (nullable id)swiftMethod;
|
||||
|
||||
@end
|
||||
|
||||
@@ -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
test/PrintAsObjC/bridging_header.swift
Normal file
15
test/PrintAsObjC/bridging_header.swift
Normal file
@@ -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?
|
||||
}
|
||||
Reference in New Issue
Block a user