Skip to content

Commit 7b0eff8

Browse files
committed
[ClangImporter] Skip importing values for ObjCBool declarations
1 parent e71ede1 commit 7b0eff8

File tree

7 files changed

+48
-1
lines changed

7 files changed

+48
-1
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
10941094
/// on macOS or Foundation on Linux.
10951095
bool isCGFloat();
10961096

1097+
/// Check if this is a ObjCBool type from the Objective-C module.
1098+
bool isObjCBool();
1099+
10971100
/// Check if this is a std.string type from C++.
10981101
bool isCxxString();
10991102

lib/AST/Type.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,19 @@ bool TypeBase::isCGFloat() {
12811281
NTD->getName().is("CGFloat");
12821282
}
12831283

1284+
bool TypeBase::isObjCBool() {
1285+
auto *NTD = getAnyNominal();
1286+
if (!NTD)
1287+
return false;
1288+
1289+
auto *DC = NTD->getDeclContext();
1290+
if (!DC->isModuleScopeContext())
1291+
return false;
1292+
1293+
auto *module = DC->getParentModule();
1294+
return module->getName().is("ObjectiveC") && NTD->getName().is("ObjCBool");
1295+
}
1296+
12841297
bool TypeBase::isCxxString() {
12851298
auto *nominal = getAnyNominal();
12861299
if (!nominal)

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4572,8 +4572,11 @@ namespace {
45724572
// CGFloats is special cased in the importer, and needs more handling.
45734573
bool isCGFloat = (type && type->isCGFloat()) ||
45744574
(type && synthesizer.isCGFloat(type));
4575+
// Do not attempts to import ObjCBool values, for similar reasons.
4576+
bool isObjCBool = (type && type->isObjCBool()) ||
4577+
(type && synthesizer.isObjCBool(type));
45754578

4576-
if (type && !isCGFloat) {
4579+
if (type && !isCGFloat && !isObjCBool) {
45774580
auto convertKind = ConstantConvertKind::None;
45784581
// Request conversions on enums, and swift_wrapper((enum/struct))
45794582
// types

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ Type SwiftDeclSynthesizer::getConstantLiteralType(
195195
}
196196
}
197197

198+
// This method is exposed on SwiftDeclSynthesizer to keep code that accesses
199+
// RawTypes together.
198200
bool SwiftDeclSynthesizer::isCGFloat(Type type) {
199201
auto found = ImporterImpl.RawTypes.find(type->getAnyNominal());
200202
if (found == ImporterImpl.RawTypes.end()) {
@@ -205,6 +207,18 @@ bool SwiftDeclSynthesizer::isCGFloat(Type type) {
205207
return importTy->isCGFloat();
206208
}
207209

210+
// This method is exposed on SwiftDeclSynthesizer to keep code that accesses
211+
// RawTypes together.
212+
bool SwiftDeclSynthesizer::isObjCBool(Type type) {
213+
auto found = ImporterImpl.RawTypes.find(type->getAnyNominal());
214+
if (found == ImporterImpl.RawTypes.end()) {
215+
return false;
216+
}
217+
218+
Type importTy = found->second;
219+
return importTy->isObjCBool();
220+
}
221+
208222
ValueDecl *SwiftDeclSynthesizer::createConstant(Identifier name,
209223
DeclContext *dc, Type type,
210224
const clang::APValue &value,

lib/ClangImporter/SwiftDeclSynthesizer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ class SwiftDeclSynthesizer {
354354

355355
bool isCGFloat(Type type);
356356

357+
bool isObjCBool(Type type);
358+
357359
private:
358360
Type getConstantLiteralType(Type type, ConstantConvertKind convertKind);
359361

test/ClangImporter/const_values_objc.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ static const MyFloatType MyFloatTypeValue1 = 10;
3131
static const MyFloatType MyFloatTypeValue2 = 20;
3232
static const MyFloatType MyFloatTypeValue3 = 30;
3333

34+
static const BOOL MyBoolConstantValue1 = YES;
35+
static const BOOL MyBoolConstantValue2 = NO;
36+
3437
//--- main.swift
3538
func foo() {
3639
print(MyClass.value)
3740
print(myFloatConstValue)
3841
print(MyFloatType.value1)
3942
print(MyFloatType.value2)
4043
print(MyFloatType.value3)
44+
print(MyBoolConstantValue1)
45+
print(MyBoolConstantValue2)
4146
}
4247

4348
// CHECK: // static MyClass.value.getter
@@ -57,3 +62,8 @@ func foo() {
5762
// CHECK-NOT: // static MyFloatType.value1.getter
5863
// CHECK-NOT: // static MyFloatType.value2.getter
5964
// CHECK-NOT: // static MyFloatType.value3.getter
65+
66+
// ObjCBools are not imported:
67+
68+
// CHECK-NOT: // MyBoolConstantValue1.getter
69+
// CHECK-NOT: // MyBoolConstantValue2.getter

test/Inputs/clang-importer-sdk/usr/include/objc/objc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ typedef int NSInteger;
1313
#endif
1414

1515
typedef __typeof__(__objc_yes) BOOL;
16+
#define YES __objc_yes
17+
#define NO __objc_no
1618

1719
typedef struct objc_selector *SEL;
1820
SEL sel_registerName(const char *str);

0 commit comments

Comments
 (0)