[clang] [Clang] Fix isWeakImported() to traverse redeclaration chain for avai… (PR #181482)

Victor Chernyakin via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 8 12:40:01 PDT 2026


================
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -triple arm64-apple-ios12.0 -emit-llvm -o - %s | FileCheck %s
+
+// Test that isWeakImported() correctly traverses the redeclaration chain
+// to find availability attributes, even when a forward declaration (@class)
+// without availability attributes becomes the most recent declaration.
+
+// Case 1: @interface (with availability) first, then @class (without availability).
+// The @class becomes getMostRecentDecl(). Without the fix, isWeakImported()
+// would only check the @class's attributes and miss the availability attribute,
+// resulting in strong linkage instead of extern_weak.
+
+__attribute__((availability(ios,introduced=14.0)))
+ at interface WeakRedecl1
+ at end
+
+ at class WeakRedecl1;
+
+ at implementation WeakRedecl1 (TestCategory1)
+ at end
+
+// CHECK: @"OBJC_CLASS_$_WeakRedecl1" = extern_weak global
----------------
localspook wrote:

When I run stock clang 22.1.2 (i.e., one that doesn't include this fix) on the following `foo.mm`:
```cpp
__attribute__((availability(ios,introduced=14.0)))
@interface WeakRedecl1
@end

@class WeakRedecl1;

@implementation WeakRedecl1 (TestCategory1)
@end
```
```sh
clang++ foo.mm -target arm64-apple-ios12.0 -emit-llvm -S -o -
```
I already see `@"OBJC_CLASS_$_WeakRedecl1" = extern_weak global %struct._class_t` in the output. Are you sure this is a valid reduction? It looks like you may need to mimic the setup with modules in the original issue.




https://github.com/llvm/llvm-project/pull/181482


More information about the cfe-commits mailing list