https://github.com/DataCorrupted updated https://github.com/llvm/llvm-project/pull/242130
>From ebbea1b8f10e691862d987aa7a255b1c5e98cca5 Mon Sep 17 00:00:00 2001 From: Peter Rong <[email protected]> Date: Thu, 12 Nov 2026 17:43:38 -0800 Subject: [PATCH 1/2] [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 cb738d060c24a9..4408b637ab8576 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 dd15f960d114be..b3a251aee43ee2 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 +@property (nonatomic, direct, readonly) int computed __attribute__((visibility("default"))); +@property (nonatomic, direct, readonly) int computedHidden; +@property (nonatomic, direct) int handWrittenSetter __attribute__((visibility("default"))); +// Class properties are never synthesized +@property (class, nonatomic, direct, readonly) int classValue __attribute__((visibility("default"))); + +// An attribute on the accessor wins over the property's +@property (nonatomic, direct, readonly) int getterDeclared; +- (int)getterDeclared __attribute__((objc_direct, visibility("default"))); +@property (nonatomic, direct, readonly) int definitionExported; +@property (nonatomic, direct, readonly) int definitionHidden __attribute__((visibility("default"))); + +// Exported readonly property, redeclared readwrite in foo.m +@property (nonatomic, direct, readonly) int counter __attribute__((visibility("default"))); +@end + +@interface Foo (Cat) +// Category properties are never synthesized +@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. +@interface Foo () +@property (nonatomic, direct, readwrite) int counter; +@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" + +@end + +@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" >From de6dc2e29909f4b55ae5aaec1f4395293864fefd Mon Sep 17 00:00:00 2001 From: Peter Rong <[email protected]> Date: Thu, 19 Nov 2026 13:10:44 -0800 Subject: [PATCH 2/2] update test --- .../expose-direct-method-visibility-linkage.m | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/clang/test/CodeGenObjC/expose-direct-method-visibility-linkage.m b/clang/test/CodeGenObjC/expose-direct-method-visibility-linkage.m index b3a251aee43ee2..9edd0b77f9a67e 100644 --- a/clang/test/CodeGenObjC/expose-direct-method-visibility-linkage.m +++ b/clang/test/CodeGenObjC/expose-direct-method-visibility-linkage.m @@ -46,6 +46,8 @@ - (int)getterDeclared __attribute__((objc_direct, visibility("default"))); // Exported readonly property, redeclared readwrite in foo.m @property (nonatomic, direct, readonly) int counter __attribute__((visibility("default"))); +// Hidden readonly property, redeclared readwrite and exported in foo.m +@property (nonatomic, direct, readonly) int counterHidden; @end @interface Foo (Cat) @@ -59,10 +61,11 @@ @interface Foo (Cat) // 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. +// The setter takes the visibility of the readwrite redeclaration, not that of +// the readonly property in foo.h. The getter takes the readonly property's. @interface Foo () @property (nonatomic, direct, readwrite) int counter; +@property (nonatomic, direct, readwrite) int counterHidden __attribute__((visibility("default"))); @end @implementation Foo @@ -154,6 +157,10 @@ - (int)definitionHidden __attribute__((visibility("hidden"))) { // FOO_M-LABEL: define dso_local i32 @"-[Foo counter]D" // FOO_M-LABEL: define hidden void @"-[Foo setCounter:]D" +// The private setter is exported, although the readonly property is hidden +// FOO_M-LABEL: define hidden i32 @"-[Foo counterHidden]D" +// FOO_M-LABEL: define dso_local void @"-[Foo setCounterHidden:]D" + @end @implementation Foo (Cat) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
