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

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 13 05:16:32 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
----------------
kevinlzh1108 wrote:

You're absolutely right — the single-file test was not a valid reduction. Within a single TU, Sema's mergeDeclAttributes() automatically copies the availability attribute from @interface to @class, so getMostRecentDecl()->attrs() already finds
   it. The test passed with or without the fix.

  I've replaced it with a Clang Modules test (clang/test/Modules/availability-redecl-weak.m) that reproduces the actual bug. The key insight is that it requires multiple availability attributes (e.g., macos before ios), because cross-PCM
  mergeInheritableAttributes uses getAttr<AvailabilityAttr>() which only copies the first one. So the @class ends up with only the inherited macos attr, losing ios — which is exactly what happens with UTType in practice.

  The test covers both import orders, and I've verified that the bug-triggering order (@import InterfaceMod → @import ForwardMod) produces strong linkage without the fix and extern_weak with it.


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


More information about the cfe-commits mailing list