Skip to content

Commit 42db1b1

Browse files
committed
[cxx-interop] Refactor: do not rely on Clang module importer being available
This makes sure that we can emit a pch from a C++ header that uses `CF_OPTIONS`. rdar://112225263 (cherry picked from commit 6a2f10a)
1 parent 9ccbca6 commit 42db1b1

File tree

6 files changed

+27
-20
lines changed

6 files changed

+27
-20
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,6 @@ class ClangModuleLoader : public ModuleLoader {
306306
virtual EffectiveClangContext getEffectiveClangContext(
307307
const NominalTypeDecl *nominal) = 0;
308308

309-
virtual const clang::TypedefType *
310-
getTypeDefForCXXCFOptionsDefinition(const clang::Decl *candidateDecl) = 0;
311-
312309
virtual SourceLoc importSourceLocation(clang::SourceLocation loc) = 0;
313310
};
314311

include/swift/ClangImporter/ClangImporter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ class ClangImporter final : public ClangModuleLoader {
592592
/// Enable the symbolic import experimental feature for the given callback.
593593
void withSymbolicFeatureEnabled(llvm::function_ref<void(void)> callback);
594594

595-
const clang::TypedefType *getTypeDefForCXXCFOptionsDefinition(
596-
const clang::Decl *candidateDecl) override;
595+
static const clang::TypedefType *getTypedefForCXXCFOptionsDefinition(
596+
const clang::Decl *candidateDecl, const ASTContext &ctx);
597597

598598
SourceLoc importSourceLocation(clang::SourceLocation loc) override;
599599
};

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,8 +2551,8 @@ ASTMangler::getTypeDefForCXXCFOptionsDefinition(const ValueDecl *decl) {
25512551
if (!clangDecl)
25522552
return nullptr;
25532553

2554-
const auto &clangModuleLoader = decl->getASTContext().getClangModuleLoader();
2555-
return clangModuleLoader->getTypeDefForCXXCFOptionsDefinition(clangDecl);
2554+
auto &ctx = decl->getASTContext();
2555+
return ClangImporter::getTypedefForCXXCFOptionsDefinition(clangDecl, ctx);
25562556
}
25572557

25582558
const clang::NamedDecl *

lib/ClangImporter/ClangImporter.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6958,26 +6958,23 @@ void ClangImporter::withSymbolicFeatureEnabled(
69586958
oldImportSymbolicCXXDecls.get());
69596959
}
69606960

6961-
const clang::TypedefType *ClangImporter::getTypeDefForCXXCFOptionsDefinition(
6962-
const clang::Decl *candidateDecl) {
6963-
6964-
if (!Impl.SwiftContext.LangOpts.EnableCXXInterop)
6961+
const clang::TypedefType *ClangImporter::getTypedefForCXXCFOptionsDefinition(
6962+
const clang::Decl *candidateDecl, const ASTContext &ctx) {
6963+
if (!ctx.LangOpts.EnableCXXInterop)
69656964
return nullptr;
69666965

69676966
auto enumDecl = dyn_cast<clang::EnumDecl>(candidateDecl);
69686967
if (!enumDecl)
69696968
return nullptr;
6970-
69716969
if (!enumDecl->getDeclName().isEmpty())
69726970
return nullptr;
69736971

69746972
const clang::ElaboratedType *elaboratedType =
6975-
dyn_cast<clang::ElaboratedType>(enumDecl->getIntegerType().getTypePtr());
6973+
enumDecl->getIntegerType()->getAs<clang::ElaboratedType>();
69766974
if (auto typedefType =
69776975
elaboratedType
69786976
? dyn_cast<clang::TypedefType>(elaboratedType->desugar())
6979-
: dyn_cast<clang::TypedefType>(
6980-
enumDecl->getIntegerType().getTypePtr())) {
6977+
: enumDecl->getIntegerType()->getAs<clang::TypedefType>()) {
69816978
auto enumExtensibilityAttr =
69826979
elaboratedType
69836980
? enumDecl->getAttr<clang::EnumExtensibilityAttr>()
@@ -6990,8 +6987,13 @@ const clang::TypedefType *ClangImporter::getTypeDefForCXXCFOptionsDefinition(
69906987
enumExtensibilityAttr->getExtensibility() ==
69916988
clang::EnumExtensibilityAttr::Open &&
69926989
hasFlagEnumAttr) {
6993-
return Impl.isUnavailableInSwift(typedefType->getDecl()) ? typedefType
6994-
: nullptr;
6990+
// Make sure the typedef is marked as unavailable in Swift.
6991+
auto typedefDecl = typedefType->getDecl();
6992+
for (auto *attr :
6993+
typedefDecl->specific_attrs<clang::AvailabilityAttr>()) {
6994+
if (attr->getPlatform()->getName() == "swift")
6995+
return typedefType;
6996+
}
69956997
}
69966998
}
69976999

lib/ClangImporter/ImportType.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,9 +2652,8 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
26522652
if (declIter != declsInContext.end()) {
26532653
if (auto enumDecl = dyn_cast<clang::EnumDecl>(*declIter)) {
26542654
if (auto cfOptionsTy =
2655-
nameImporter.getContext()
2656-
.getClangModuleLoader()
2657-
->getTypeDefForCXXCFOptionsDefinition(enumDecl)) {
2655+
ClangImporter::getTypedefForCXXCFOptionsDefinition(
2656+
enumDecl, nameImporter.getContext())) {
26582657
if (cfOptionsTy->getDecl() == typedefDecl) {
26592658
auto enumName = typedefDecl->getName();
26602659
ArgumentAttrs argumentAttrs(DefaultArgumentKind::None, true,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t/pch
3+
14
// RUN: %target-typecheck-verify-swift -verify-ignore-unknown -I %S/Inputs -enable-objc-interop -enable-experimental-cxx-interop
5+
6+
// RUN: %target-swift-frontend -emit-pch -enable-objc-interop -enable-experimental-cxx-interop -o %t/pch/customNSOptions.pch %S/Inputs/customNSOptions.h
7+
// RUN: %target-typecheck-verify-swift -D BRIDGING_HEADER -I %S/Inputs -import-objc-header %t/pch/customNSOptions.pch -enable-objc-interop -enable-experimental-cxx-interop %s
8+
29
// REQUIRES: objc_interop
310

11+
#if !BRIDGING_HEADER
412
import CustomNSOptions
13+
#endif
514

615
let flags1: MyControlFlags = []
716
let flags2: MyControlFlags = [.first]

0 commit comments

Comments
 (0)