[PATCH] D135273: [Clang][ObjC] Add optionality to property attribute strings.

Alastair Houghton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 5 08:33:55 PDT 2022


al45tair created this revision.
al45tair added reviewers: ahatanak, rjmccall, theraven.
Herald added a project: All.
al45tair requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Add a new attribute, "?", to the property attribute string for properties of protocols that are declared `@optional`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135273

Files:
  clang/lib/AST/ASTContext.cpp
  clang/test/CodeGenObjC/objc-asm-attribute-test.m


Index: clang/test/CodeGenObjC/objc-asm-attribute-test.m
===================================================================
--- clang/test/CodeGenObjC/objc-asm-attribute-test.m
+++ clang/test/CodeGenObjC/objc-asm-attribute-test.m
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -no-opaque-pointers -emit-llvm -triple x86_64-apple-darwin %s -o - | FileCheck %s
+// RUN: %clang_cc1 -no-opaque-pointers -Wno-objc-root-class -emit-llvm -triple x86_64-apple-darwin %s -o - | FileCheck %s
 // rdar://16462586
 
 __attribute__((objc_runtime_name("MySecretNamespace.Protocol")))
@@ -11,10 +11,17 @@
 @protocol Protocol2
 - (void) MethodP2;
 + (void) ClsMethodP2;
+
+ at optional
+ at property(retain) id optionalProp;
+
 @end
 
+ at class Message;
+
 __attribute__((objc_runtime_name("MySecretNamespace.Protocol3")))
 @protocol Protocol3
+
 @end
 
 __attribute__((objc_runtime_name("MySecretNamespace.Message")))
@@ -56,9 +63,13 @@
 }
 
 // CHECK: @"OBJC_IVAR_$_MySecretNamespace.Message.MyIVAR" ={{.*}} global i64 0
+
 // CHECK: @"OBJC_CLASS_$_MySecretNamespace.Message" ={{.*}} global %struct._class_t
 // CHECK: @"OBJC_METACLASS_$_MySecretNamespace.Message" ={{.*}} global %struct._class_t
 
+// CHECK: @OBJC_PROP_NAME_ATTR_ = private unnamed_addr constant [13 x i8] c"optionalProp\00", section "__TEXT,__objc_methname,cstring_literals", align 1
+// CHECK-NEXT: @OBJC_PROP_NAME_ATTR_.11 = private unnamed_addr constant [7 x i8] c"T@,?,&\00", section "__TEXT,__objc_methname,cstring_literals", align 1
+
 // CHECK: private unnamed_addr constant [42 x i8] c"T@\22MySecretNamespace.Message\22,&,V_msgProp\00"
 // CHECK: private unnamed_addr constant [76 x i8] c"T@\22MySecretNamespace.Message<MySecretNamespace.Protocol3>\22,&,V_msgProtoProp\00"
 // CHECK: private unnamed_addr constant [50 x i8] c"T@\22<MySecretNamespace.Protocol3>\22,&,V_idProtoProp\00"
Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -7828,6 +7828,7 @@
 /// kPropertyWeak = 'W'              // 'weak' property
 /// kPropertyStrong = 'P'            // property GC'able
 /// kPropertyNonAtomic = 'N'         // property non-atomic
+/// kPropertyOptional = '?'          // property optional
 /// };
 /// @endcode
 std::string
@@ -7853,6 +7854,10 @@
   // closely resembles encoding of ivars.
   getObjCEncodingForPropertyType(PD->getType(), S);
 
+  if (PD->isOptional()) {
+    S += ",?";
+  }
+
   if (PD->isReadOnly()) {
     S += ",R";
     if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135273.465415.patch
Type: text/x-patch
Size: 2577 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221005/6c51a816/attachment-0001.bin>


More information about the cfe-commits mailing list