[clang] [ObjCDirectPreconditionThunk] Use canonical declaration to find visibility of hand-written accessors (PR #242130)

Peter Rong via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 17:55:24 PST 2026


https://github.com/DataCorrupted created https://github.com/llvm/llvm-project/pull/242130

With `-fobjc-direct-precondition-thunk`, a direct method takes its explicit visibility attribute, and an accessor falls back to its property's. The property was found with `findPropertyDecl()`, which works well for implicitly declared accessors. But a hand-written one isn't marked, so it stayed hidden even when its property had visibility("default"), and a client in another linkage unit failed to link against its symbol.

Find the property through the method's canonical declaration, which is marked. Check the method's own attribute first, so an attribute on the accessor wins over its property's. Before, a synthesized accessor took its property's attribute, or the lack of one, and ignored its own.

A readonly property redeclared readwrite in a class extension now gives its setter the redeclaration's attribute, not the public property's. The getter still takes the public property's.

>From ebbea1b8f10e691862d987aa7a255b1c5e98cca5 Mon Sep 17 00:00:00 2001
From: Peter Rong <PeterRong at meta.com>
Date: Thu, 12 Nov 2026 17:43:38 -0800
Subject: [PATCH] [ObjCDirectPreconditionThunk] Fix visibility of hand-written
 accessors

With -fobjc-direct-precondition-thunk, a direct method takes its
explicit visibility attribute, and an accessor falls back to its
property's. The property was found with findPropertyDecl(), which only
works on methods marked as accessors: synthesized stubs and implicit
declarations. A hand-written accessor isn't marked, so it stayed hidden
even when its property had visibility("default"), and a client in
another linkage unit failed to link against its ]D symbol.

Find the property through the method's canonical declaration, which is
marked. Check the method's own attribute first, so an attribute on the
accessor wins over its property's. Before, a synthesized accessor took
its property's attribute, or the lack of one, and ignored its own.

A readonly property redeclared readwrite in a class extension now gives
its setter the redeclaration's attribute, not the public property's.
The getter still takes the public property's.
---
 clang/lib/CodeGen/CGObjC.cpp                  | 18 ++--
 .../expose-direct-method-visibility-linkage.m | 94 +++++++++++++++++++
 2 files changed, 104 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp
index cb738d060c24a..4408b637ab857 100644
--- a/clang/lib/CodeGen/CGObjC.cpp
+++ b/clang/lib/CodeGen/CGObjC.cpp
@@ -782,16 +782,18 @@ void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
     // Default hidden visibility
     Fn->setVisibility(llvm::Function::HiddenVisibility);
     if (CGM.isObjCDirectPreconditionThunkEnabled()) {
-      // However, if we expose the symbol, and the decl (property or method)
-      // have visibility attribute set ...
-      const NamedDecl *Decl = OMD;
-      if (const auto *PD = OMD->findPropertyDecl()) {
-        Decl = PD;
+      // However, if we expose the symbol, respect an explicit visibility
+      // attribute. If the method is an accessor and the property also sets
+      // the attribute, prefer the method's, as it's the more specific one.
+      auto V = OMD->getExplicitVisibility(NamedDecl::VisibilityForValue);
+      if (!V) {
+        // Find the property through the method's declaration: a hand-written
+        // accessor isn't marked as an accessor, but its declaration is.
+        if (const auto *PD = OMD->getCanonicalDecl()->findPropertyDecl())
+          V = PD->getExplicitVisibility(NamedDecl::VisibilityForValue);
       }
-      // ... then respect source level visibility setting
-      if (auto V = Decl->getExplicitVisibility(NamedDecl::VisibilityForValue)) {
+      if (V)
         Fn->setVisibility(CGM.GetLLVMVisibility(*V));
-      }
     }
     CGM.SetLLVMFunctionAttributes(OMD, FI, Fn, /*IsThunk=*/false);
     CGM.SetLLVMFunctionAttributesForDefinition(OMD, Fn);
diff --git a/clang/test/CodeGenObjC/expose-direct-method-visibility-linkage.m b/clang/test/CodeGenObjC/expose-direct-method-visibility-linkage.m
index dd15f960d114b..b3a251aee43ee 100644
--- a/clang/test/CodeGenObjC/expose-direct-method-visibility-linkage.m
+++ b/clang/test/CodeGenObjC/expose-direct-method-visibility-linkage.m
@@ -30,6 +30,27 @@ + (int)classMethod:(int)x __attribute__((objc_direct));
 // Explicit default visibility (should be exported)
 - (int)exportedInstanceMethod:(int)x __attribute__((objc_direct, visibility("default")));
 + (int)exportedClassMethod:(int)x __attribute__((objc_direct, visibility("default")));
+
+// Hand-written accessors take the property's visibility, like synthesized ones
+ at property (nonatomic, direct, readonly) int computed __attribute__((visibility("default")));
+ at property (nonatomic, direct, readonly) int computedHidden;
+ at property (nonatomic, direct) int handWrittenSetter __attribute__((visibility("default")));
+// Class properties are never synthesized
+ at property (class, nonatomic, direct, readonly) int classValue __attribute__((visibility("default")));
+
+// An attribute on the accessor wins over the property's
+ at property (nonatomic, direct, readonly) int getterDeclared;
+- (int)getterDeclared __attribute__((objc_direct, visibility("default")));
+ at property (nonatomic, direct, readonly) int definitionExported;
+ at property (nonatomic, direct, readonly) int definitionHidden __attribute__((visibility("default")));
+
+// Exported readonly property, redeclared readwrite in foo.m
+ at property (nonatomic, direct, readonly) int counter __attribute__((visibility("default")));
+ at end
+
+ at interface Foo (Cat)
+// Category properties are never synthesized
+ at property (nonatomic, direct, readonly) int categoryValue __attribute__((visibility("default")));
 @end
 
 //--- foo.m
@@ -38,6 +59,12 @@ + (int)exportedClassMethod:(int)x __attribute__((objc_direct, visibility("defaul
 // FOO_M-NOT: @{{.*}}_thunk
 #import "foo.h"
 
+// The setter takes the visibility of this redeclaration, which has no
+// attribute, not that of the exported readonly property in foo.h.
+ at interface Foo ()
+ at property (nonatomic, direct, readwrite) int counter;
+ at end
+
 @implementation Foo
 
 // FOO_M-LABEL: define internal ptr @"\01-[Foo initWithprivateValue:exportedValue:]"
@@ -79,12 +106,63 @@ + (int)exportedClassMethod:(int)x {
   return x * 5;
 }
 
+// Hand-written accessors take the property's visibility
+// FOO_M-LABEL: define dso_local i32 @"-[Foo computed]D"
+- (int)computed {
+  return 1;
+}
+
+// FOO_M-LABEL: define hidden i32 @"-[Foo computedHidden]D"
+- (int)computedHidden {
+  return 2;
+}
+
+// FOO_M-LABEL: define dso_local void @"-[Foo setHandWrittenSetter:]D"
+- (void)setHandWrittenSetter:(int)x {
+  _handWrittenSetter = x;
+}
+
+// FOO_M-LABEL: define dso_local i32 @"+[Foo classValue]D"
++ (int)classValue {
+  return 3;
+}
+
+// An attribute on the definition wins over the property's
+// FOO_M-LABEL: define dso_local i32 @"-[Foo definitionExported]D"
+- (int)definitionExported __attribute__((visibility("default"))) {
+  return 4;
+}
+
+// FOO_M-LABEL: define hidden i32 @"-[Foo definitionHidden]D"
+- (int)definitionHidden __attribute__((visibility("hidden"))) {
+  return 5;
+}
+
 // Hidden property getter and setter (default visibility)
 // FOO_M-LABEL: define hidden void @"-[Foo setPrivateValue:]D"
 
 // Exported property getter and setter (explicit default visibility)
 // FOO_M-LABEL: define dso_local void @"-[Foo setExportedValue:]D"
 
+// Synthesized getter of a property with a hand-written setter
+// FOO_M-LABEL: define dso_local i32 @"-[Foo handWrittenSetter]D"
+
+// Synthesized getter whose declaration has its own attribute
+// FOO_M-LABEL: define dso_local i32 @"-[Foo getterDeclared]D"
+
+// The private setter is hidden, although the readonly property is exported
+// FOO_M-LABEL: define dso_local i32 @"-[Foo counter]D"
+// FOO_M-LABEL: define hidden void @"-[Foo setCounter:]D"
+
+ at end
+
+ at implementation Foo (Cat)
+
+// FOO_M-LABEL: define dso_local i32 @"-[Foo categoryValue]D"
+- (int)categoryValue {
+  return 6;
+}
+
 @end
 
 //--- main.m
@@ -109,6 +187,18 @@ int main() {
     // MAIN_M: call i32 @"+[Foo exportedClassMethod:]D_thunk"
     printf("Exported class: %d\n", [Foo exportedClassMethod:10]);
 
+    // MAIN_M: call i32 @"-[Foo computed]D_thunk"
+    printf("Computed: %d\n", [obj computed]);
+
+    // MAIN_M: call void @"-[Foo setHandWrittenSetter:]D_thunk"
+    [obj setHandWrittenSetter:40];
+
+    // MAIN_M: call i32 @"+[Foo classValue]D_thunk"
+    printf("Class value: %d\n", [Foo classValue]);
+
+    // MAIN_M: call i32 @"-[Foo categoryValue]D_thunk"
+    printf("Category value: %d\n", [obj categoryValue]);
+
     return 0;
 }
 
@@ -117,3 +207,7 @@ int main() {
 // MAIN_M-LABEL: define linkonce_odr hidden i32 @"-[Foo exportedValue]D_thunk"
 // MAIN_M-LABEL: define linkonce_odr hidden i32 @"-[Foo exportedInstanceMethod:]D_thunk"
 // MAIN_M-LABEL: define linkonce_odr hidden i32 @"+[Foo exportedClassMethod:]D_thunk"
+// MAIN_M-LABEL: define linkonce_odr hidden i32 @"-[Foo computed]D_thunk"
+// MAIN_M-LABEL: define linkonce_odr hidden void @"-[Foo setHandWrittenSetter:]D_thunk"
+// MAIN_M-LABEL: define linkonce_odr hidden i32 @"+[Foo classValue]D_thunk"
+// MAIN_M-LABEL: define linkonce_odr hidden i32 @"-[Foo categoryValue]D_thunk"



More information about the cfe-commits mailing list