[clang] [Clang][ObjC] Add optionality to property attribute strings. (PR #66507)

Alastair Houghton via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 15 05:50:09 PDT 2023


https://github.com/al45tair created https://github.com/llvm/llvm-project/pull/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

>From 3514013089dcf9fa798e9ba5812b34df497a493b Mon Sep 17 00:00:00 2001
From: Alastair Houghton <ahoughton at apple.com>
Date: Fri, 15 Sep 2023 13:46:19 +0100
Subject: [PATCH] [Clang][ObjC] Add optionality to property attribute strings.

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
---
 clang/lib/AST/ASTContext.cpp                     |  4 ++++
 clang/test/CodeGenObjC/objc-asm-attribute-test.m | 10 +++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 4b1d9e86797b778..57aaa05b1d81ddb 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -7890,6 +7890,7 @@ ASTContext::getObjCPropertyImplDeclForPropertyDecl(
 /// kPropertyWeak = 'W'              // 'weak' property
 /// kPropertyStrong = 'P'            // property GC'able
 /// kPropertyNonAtomic = 'N'         // property non-atomic
+/// kPropertyOptional = '?'          // property optional
 /// };
 /// @endcode
 std::string
@@ -7915,6 +7916,9 @@ ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
   // closely resembles encoding of ivars.
   getObjCEncodingForPropertyType(PD->getType(), S);
 
+  if (PD->isOptional())
+    S += ",?";
+
   if (PD->isReadOnly()) {
     S += ",R";
     if (PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy)
diff --git a/clang/test/CodeGenObjC/objc-asm-attribute-test.m b/clang/test/CodeGenObjC/objc-asm-attribute-test.m
index 876370115bfc424..e57e42535f67274 100644
--- a/clang/test/CodeGenObjC/objc-asm-attribute-test.m
+++ b/clang/test/CodeGenObjC/objc-asm-attribute-test.m
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -emit-llvm -triple x86_64-apple-darwin %s -o - | FileCheck %s
+// RUN: %clang_cc1 -Wno-objc-root-class -emit-llvm -triple x86_64-apple-darwin %s -o - | FileCheck %s
 
 __attribute__((objc_runtime_name("MySecretNamespace.Protocol")))
 @protocol Protocol
@@ -10,6 +10,10 @@ + (void) ClsMethodP;
 @protocol Protocol2
 - (void) MethodP2;
 + (void) ClsMethodP2;
+
+ at optional
+ at property(retain) id optionalProp;
+
 @end
 
 __attribute__((objc_runtime_name("MySecretNamespace.Protocol3")))
@@ -57,6 +61,10 @@ id Test16877359(void) {
 // 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"
+// CHECK-NEXT: @OBJC_PROP_NAME_ATTR_.11 = private unnamed_addr constant [7 x i8] c"T@,?,&\00"
+// CHECK: @"_OBJC_$_PROP_LIST_MySecretNamespace.Protocol2" ={{.*}} [%struct._prop_t { ptr @OBJC_PROP_NAME_ATTR_, ptr @OBJC_PROP_NAME_ATTR_.11 }]
+
 // 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"



More information about the cfe-commits mailing list