Skip to content

Commit 6a2f10a

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
1 parent f7c2257 commit 6a2f10a

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
@@ -603,8 +603,8 @@ class ClangImporter final : public ClangModuleLoader {
603603
/// Enable the symbolic import experimental feature for the given callback.
604604
void withSymbolicFeatureEnabled(llvm::function_ref<void(void)> callback);
605605

606-
const clang::TypedefType *getTypeDefForCXXCFOptionsDefinition(
607-
const clang::Decl *candidateDecl) override;
606+
static const clang::TypedefType *getTypedefForCXXCFOptionsDefinition(
607+
const clang::Decl *candidateDecl, const ASTContext &ctx);
608608

609609
SourceLoc importSourceLocation(clang::SourceLocation loc) override;
610610
};

lib/AST/ASTMangler.cpp

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

2544-
const auto &clangModuleLoader = decl->getASTContext().getClangModuleLoader();
2545-
return clangModuleLoader->getTypeDefForCXXCFOptionsDefinition(clangDecl);
2544+
auto &ctx = decl->getASTContext();
2545+
return ClangImporter::getTypedefForCXXCFOptionsDefinition(clangDecl, ctx);
25462546
}
25472547

25482548
const clang::NamedDecl *

lib/ClangImporter/ClangImporter.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7022,26 +7022,23 @@ void ClangImporter::withSymbolicFeatureEnabled(
70227022
oldImportSymbolicCXXDecls.get());
70237023
}
70247024

7025-
const clang::TypedefType *ClangImporter::getTypeDefForCXXCFOptionsDefinition(
7026-
const clang::Decl *candidateDecl) {
7027-
7028-
if (!Impl.SwiftContext.LangOpts.EnableCXXInterop)
7025+
const clang::TypedefType *ClangImporter::getTypedefForCXXCFOptionsDefinition(
7026+
const clang::Decl *candidateDecl, const ASTContext &ctx) {
7027+
if (!ctx.LangOpts.EnableCXXInterop)
70297028
return nullptr;
70307029

70317030
auto enumDecl = dyn_cast<clang::EnumDecl>(candidateDecl);
70327031
if (!enumDecl)
70337032
return nullptr;
7034-
70357033
if (!enumDecl->getDeclName().isEmpty())
70367034
return nullptr;
70377035

70387036
const clang::ElaboratedType *elaboratedType =
7039-
dyn_cast<clang::ElaboratedType>(enumDecl->getIntegerType().getTypePtr());
7037+
enumDecl->getIntegerType()->getAs<clang::ElaboratedType>();
70407038
if (auto typedefType =
70417039
elaboratedType
70427040
? dyn_cast<clang::TypedefType>(elaboratedType->desugar())
7043-
: dyn_cast<clang::TypedefType>(
7044-
enumDecl->getIntegerType().getTypePtr())) {
7041+
: enumDecl->getIntegerType()->getAs<clang::TypedefType>()) {
70457042
auto enumExtensibilityAttr =
70467043
elaboratedType
70477044
? enumDecl->getAttr<clang::EnumExtensibilityAttr>()
@@ -7054,8 +7051,13 @@ const clang::TypedefType *ClangImporter::getTypeDefForCXXCFOptionsDefinition(
70547051
enumExtensibilityAttr->getExtensibility() ==
70557052
clang::EnumExtensibilityAttr::Open &&
70567053
hasFlagEnumAttr) {
7057-
return Impl.isUnavailableInSwift(typedefType->getDecl()) ? typedefType
7058-
: nullptr;
7054+
// Make sure the typedef is marked as unavailable in Swift.
7055+
auto typedefDecl = typedefType->getDecl();
7056+
for (auto *attr :
7057+
typedefDecl->specific_attrs<clang::AvailabilityAttr>()) {
7058+
if (attr->getPlatform()->getName() == "swift")
7059+
return typedefType;
7060+
}
70597061
}
70607062
}
70617063

lib/ClangImporter/ImportType.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,9 +2656,8 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
26562656
if (declIter != declsInContext.end()) {
26572657
if (auto enumDecl = dyn_cast<clang::EnumDecl>(*declIter)) {
26582658
if (auto cfOptionsTy =
2659-
nameImporter.getContext()
2660-
.getClangModuleLoader()
2661-
->getTypeDefForCXXCFOptionsDefinition(enumDecl)) {
2659+
ClangImporter::getTypedefForCXXCFOptionsDefinition(
2660+
enumDecl, nameImporter.getContext())) {
26622661
if (cfOptionsTy->getDecl() == typedefDecl) {
26632662
auto enumName = typedefDecl->getName();
26642663
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)