Skip to content

Commit c27b506

Browse files
committed
Merge pull request #260 from kballard/objc-new-style-nullable
Use new-style _Nullable keywords in obj-c header
2 parents 1decce2 + 6636946 commit c27b506

File tree

8 files changed

+172
-160
lines changed

8 files changed

+172
-160
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -655,23 +655,35 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
655655
if (!kind)
656656
return;
657657

658-
if (printKind == NullabilityPrintKind::After)
659-
os << ' ';
660-
661-
if (printKind != NullabilityPrintKind::ContextSensitive)
662-
os << "__";
663-
664-
switch (*kind) {
665-
case OTK_None:
666-
os << "nonnull";
667-
break;
668-
669-
case OTK_Optional:
670-
os << "nullable";
658+
switch (printKind) {
659+
case NullabilityPrintKind::ContextSensitive:
660+
switch (*kind) {
661+
case OTK_None:
662+
os << "nonnull";
663+
break;
664+
case OTK_Optional:
665+
os << "nullable";
666+
break;
667+
case OTK_ImplicitlyUnwrappedOptional:
668+
os << "null_unspecified";
669+
break;
670+
}
671671
break;
672-
673-
case OTK_ImplicitlyUnwrappedOptional:
674-
os << "null_unspecified";
672+
case NullabilityPrintKind::After:
673+
os << ' ';
674+
[[clang::fallthrough]];
675+
case NullabilityPrintKind::Before:
676+
switch (*kind) {
677+
case OTK_None:
678+
os << "_Nonnull";
679+
break;
680+
case OTK_Optional:
681+
os << "_Nullable";
682+
break;
683+
case OTK_ImplicitlyUnwrappedOptional:
684+
os << "_Null_unspecified";
685+
break;
686+
}
675687
break;
676688
}
677689

@@ -805,7 +817,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
805817
auto *clangTypeDecl = cast<clang::TypeDecl>(alias->getClangDecl());
806818
os << clangTypeDecl->getName();
807819

808-
// Print proper nullability for CF types, but __null_unspecified for
820+
// Print proper nullability for CF types, but _Null_unspecified for
809821
// all other non-object Clang pointer types.
810822
if (aliasTy->hasReferenceSemantics() ||
811823
isClangObjectPointerType(clangTypeDecl)) {
@@ -1527,7 +1539,7 @@ class ModuleWriter {
15271539
return elem->getName().str() == "Domain";
15281540
});
15291541
if (!hasDomainCase) {
1530-
os << "static NSString * __nonnull const " << ED->getName()
1542+
os << "static NSString * _Nonnull const " << ED->getName()
15311543
<< "Domain = @\"" << M.getName() << "." << ED->getName() << "\";\n";
15321544
}
15331545
}

test/PrintAsObjC/blocks.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ typealias MyTuple = (a: Int, b: AnyObject?)
1515
typealias MyInt = Int
1616

1717
// CHECK-LABEL: @interface Callbacks
18-
// CHECK-NEXT: - (void (^ __nonnull)(void))voidBlocks:(void (^ __nonnull)(void))input;
19-
// CHECK-NEXT: - (void)manyArguments:(void (^ __nonnull)(float, float, double, double))input;
20-
// CHECK-NEXT: - (void)blockTakesBlock:(void (^ __nonnull)(void (^ __nonnull)(void)))input;
21-
// CHECK-NEXT: - (void)blockReturnsBlock:(void (^ __nonnull (^ __nonnull)(void))(void))input;
22-
// CHECK-NEXT: - (void)blockTakesAndReturnsBlock:(uint8_t (^ __nonnull (^ __nonnull)(uint16_t (^ __nonnull)(int16_t)))(int8_t))input;
23-
// CHECK-NEXT: - (void)blockTakesTwoBlocksAndReturnsBlock:(uint8_t (^ __nonnull (^ __nonnull)(uint16_t (^ __nonnull)(int16_t), uint32_t (^ __nonnull)(int32_t)))(int8_t))input;
24-
// CHECK-NEXT: - (void (^ __nullable)(NSObject * __nonnull))returnsBlockWithInput;
25-
// CHECK-NEXT: - (void (^ __nullable)(NSObject * __nonnull))returnsBlockWithParenthesizedInput;
26-
// CHECK-NEXT: - (void (^ __nullable)(NSObject * __nonnull, NSObject * __nonnull))returnsBlockWithTwoInputs;
27-
// CHECK-NEXT: - (void)blockWithTypealias:(NSInteger (^ __nonnull)(NSInteger, id __nullable))input;
28-
// CHECK-NEXT: - (void)blockWithSimpleTypealias:(NSInteger (^ __nonnull)(NSInteger))input;
29-
// CHECK-NEXT: - (NSInteger (* __nonnull)(NSInteger))functionPointers:(NSInteger (* __nonnull)(NSInteger))input;
30-
// CHECK-NEXT: - (void)functionPointerTakesAndReturnsFunctionPointer:(NSInteger (* __nonnull (^ __nonnull (* __nonnull)(NSInteger))(NSInteger))(NSInteger))input;
31-
// CHECK-NEXT: @property (nonatomic, copy) NSInteger (^ __nullable savedBlock)(NSInteger);
32-
// CHECK-NEXT: @property (nonatomic) NSInteger (* __nonnull savedFunctionPointer)(NSInteger);
33-
// CHECK-NEXT: @property (nonatomic) NSInteger (* __nullable savedFunctionPointer2)(NSInteger);
18+
// CHECK-NEXT: - (void (^ _Nonnull)(void))voidBlocks:(void (^ _Nonnull)(void))input;
19+
// CHECK-NEXT: - (void)manyArguments:(void (^ _Nonnull)(float, float, double, double))input;
20+
// CHECK-NEXT: - (void)blockTakesBlock:(void (^ _Nonnull)(void (^ _Nonnull)(void)))input;
21+
// CHECK-NEXT: - (void)blockReturnsBlock:(void (^ _Nonnull (^ _Nonnull)(void))(void))input;
22+
// CHECK-NEXT: - (void)blockTakesAndReturnsBlock:(uint8_t (^ _Nonnull (^ _Nonnull)(uint16_t (^ _Nonnull)(int16_t)))(int8_t))input;
23+
// CHECK-NEXT: - (void)blockTakesTwoBlocksAndReturnsBlock:(uint8_t (^ _Nonnull (^ _Nonnull)(uint16_t (^ _Nonnull)(int16_t), uint32_t (^ _Nonnull)(int32_t)))(int8_t))input;
24+
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithInput;
25+
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithParenthesizedInput;
26+
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull, NSObject * _Nonnull))returnsBlockWithTwoInputs;
27+
// CHECK-NEXT: - (void)blockWithTypealias:(NSInteger (^ _Nonnull)(NSInteger, id _Nullable))input;
28+
// CHECK-NEXT: - (void)blockWithSimpleTypealias:(NSInteger (^ _Nonnull)(NSInteger))input;
29+
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointers:(NSInteger (* _Nonnull)(NSInteger))input;
30+
// CHECK-NEXT: - (void)functionPointerTakesAndReturnsFunctionPointer:(NSInteger (* _Nonnull (^ _Nonnull (* _Nonnull)(NSInteger))(NSInteger))(NSInteger))input;
31+
// CHECK-NEXT: @property (nonatomic, copy) NSInteger (^ _Nullable savedBlock)(NSInteger);
32+
// CHECK-NEXT: @property (nonatomic) NSInteger (* _Nonnull savedFunctionPointer)(NSInteger);
33+
// CHECK-NEXT: @property (nonatomic) NSInteger (* _Nullable savedFunctionPointer2)(NSInteger);
3434
// CHECK-NEXT: init
3535
// CHECK-NEXT: @end
3636
@objc class Callbacks {

0 commit comments

Comments
 (0)