Skip to content

Commit 06f10b8

Browse files
authored
[Clang][ObjC] Add optionality to property attribute strings. (#66507)
Add a new attribute, "?", to the property attribute string for properties of protocols that are declared @optional. (Previously https://reviews.llvm.org/D135273) rdar://100463524
1 parent 7fe8e0a commit 06f10b8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7890,6 +7890,7 @@ ASTContext::getObjCPropertyImplDeclForPropertyDecl(
78907890
/// kPropertyWeak = 'W' // 'weak' property
78917891
/// kPropertyStrong = 'P' // property GC'able
78927892
/// kPropertyNonAtomic = 'N' // property non-atomic
7893+
/// kPropertyOptional = '?' // property optional
78937894
/// };
78947895
/// @endcode
78957896
std::string
@@ -7915,6 +7916,9 @@ ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
79157916
// closely resembles encoding of ivars.
79167917
getObjCEncodingForPropertyType(PD->getType(), S);
79177918

7919+
if (PD->isOptional())
7920+
S += ",?";
7921+
79187922
if (PD->isReadOnly()) {
79197923
S += ",R";
79207924
if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy)

clang/test/CodeGenObjC/objc-asm-attribute-test.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -emit-llvm -triple x86_64-apple-darwin %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -Wno-objc-root-class -emit-llvm -triple x86_64-apple-darwin %s -o - | FileCheck %s
22

33
__attribute__((objc_runtime_name("MySecretNamespace.Protocol")))
44
@protocol Protocol
@@ -10,6 +10,10 @@ + (void) ClsMethodP;
1010
@protocol Protocol2
1111
- (void) MethodP2;
1212
+ (void) ClsMethodP2;
13+
14+
@optional
15+
@property(retain) id optionalProp;
16+
1317
@end
1418

1519
__attribute__((objc_runtime_name("MySecretNamespace.Protocol3")))
@@ -57,6 +61,10 @@ id Test16877359(void) {
5761
// CHECK: @"OBJC_CLASS_$_MySecretNamespace.Message" ={{.*}} global %struct._class_t
5862
// CHECK: @"OBJC_METACLASS_$_MySecretNamespace.Message" ={{.*}} global %struct._class_t
5963

64+
// CHECK: @OBJC_PROP_NAME_ATTR_ = private unnamed_addr constant [13 x i8] c"optionalProp\00"
65+
// CHECK-NEXT: @OBJC_PROP_NAME_ATTR_.11 = private unnamed_addr constant [7 x i8] c"T@,?,&\00"
66+
// CHECK: @"_OBJC_$_PROP_LIST_MySecretNamespace.Protocol2" ={{.*}} [%struct._prop_t { ptr @OBJC_PROP_NAME_ATTR_, ptr @OBJC_PROP_NAME_ATTR_.11 }]
67+
6068
// CHECK: private unnamed_addr constant [42 x i8] c"T@\22MySecretNamespace.Message\22,&,V_msgProp\00"
6169
// CHECK: private unnamed_addr constant [76 x i8] c"T@\22MySecretNamespace.Message<MySecretNamespace.Protocol3>\22,&,V_msgProtoProp\00"
6270
// CHECK: private unnamed_addr constant [50 x i8] c"T@\22<MySecretNamespace.Protocol3>\22,&,V_idProtoProp\00"

0 commit comments

Comments
 (0)