[clang] [Clang] Fix isWeakImported() to traverse redeclaration chain for avai… (PR #181482)
via cfe-commits
cfe-commits at lists.llvm.org
Sat May 2 01:49:43 PDT 2026
https://github.com/kevinlzh1108 updated https://github.com/llvm/llvm-project/pull/181482
>From c39d655751fd643f325289445faac19a0b537fc3 Mon Sep 17 00:00:00 2001
From: kevinlzh1108 <kevinlzh1108 at gmail.com>
Date: Mon, 20 Apr 2026 15:15:26 +0800
Subject: [PATCH] [Clang] Narrow fix to mergeInheritableAttributes only, per
review feedback
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Drop the DeclBase.cpp isWeakImported() redecls() traversal — the Sema
path (mergeDeclAttributes) already propagates all AvailabilityAttrs
correctly via specific_attrs<InheritableAttr>(). The root cause is
isolated to mergeInheritableAttributes() using getAttr<AvailabilityAttr>()
which only returns the first platform attr.
Extend the existing clang/test/Modules/decl-attr-merge.mm with a
multi-platform case (macOS + iOS) and an iOS triple RUN line that
triggers the bug without the fix.
---
clang/lib/Serialization/ASTReaderDecl.cpp | 15 ++++++++-----
clang/test/Modules/decl-attr-merge.mm | 26 +++++++++++++++++++----
2 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index 9033ea55bc5e2..664544f7809b3 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -3620,11 +3620,16 @@ void mergeInheritableAttributes(ASTReader &Reader, Decl *D, Decl *Previous) {
D->addAttr(NewAttr);
}
- const auto *AA = Previous->getAttr<AvailabilityAttr>();
- if (AA && !D->hasAttr<AvailabilityAttr>()) {
- NewAttr = AA->clone(Context);
- NewAttr->setInherited(true);
- D->addAttr(NewAttr);
+ // getAttr<AvailabilityAttr>() only returns the first match, but a
+ // declaration can carry multiple AvailabilityAttrs (one per platform,
+ // e.g. macos + ios). Iterate over all of them so every platform's
+ // availability is propagated to the new redeclaration.
+ if (!D->hasAttr<AvailabilityAttr>()) {
+ for (const auto *AA : Previous->specific_attrs<AvailabilityAttr>()) {
+ NewAttr = AA->clone(Context);
+ NewAttr->setInherited(true);
+ D->addAttr(NewAttr);
+ }
}
}
} // namespace
diff --git a/clang/test/Modules/decl-attr-merge.mm b/clang/test/Modules/decl-attr-merge.mm
index 57f3f93df480e..62a2dde557972 100644
--- a/clang/test/Modules/decl-attr-merge.mm
+++ b/clang/test/Modules/decl-attr-merge.mm
@@ -1,13 +1,21 @@
// RUN: rm -rf %t.dir
// RUN: split-file %s %t.dir
+// macOS: single-platform availability worked even before the fix.
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps \
-// RUN: -fmodules-cache-path=%t.dir/cache -triple x86_64-apple-macosx10.11.0 \
-// RUN: -I%t.dir/headers %t.dir/main.m -emit-llvm -o %t.dir/main.ll
-// RUN: cat %t.dir/main.ll | FileCheck %s
+// RUN: -fmodules-cache-path=%t.dir/mcache -triple x86_64-apple-macosx10.11.0 \
+// RUN: -I%t.dir/headers %t.dir/main-macos.m -emit-llvm -o - | FileCheck %s
+// iOS: the @interface carries two AvailabilityAttrs (macOS + iOS).
+// Without the fix, mergeInheritableAttributes used getAttr<AvailabilityAttr>()
+// which only copied the first (macOS); the iOS attr was lost on the @class
+// redeclaration, causing isWeakImported() to return false (strong linkage).
+// RUN: %clang_cc1 -fmodules -fimplicit-module-maps \
+// RUN: -fmodules-cache-path=%t.dir/icache -triple arm64-apple-ios12.0 \
+// RUN: -I%t.dir/headers %t.dir/main-ios.m -emit-llvm -o - | FileCheck %s
//--- headers/a.h
__attribute__((availability(macos,introduced=10.16)))
+__attribute__((availability(ios,introduced=14.0)))
@interface INIntent
- (instancetype)self;
@end
@@ -26,7 +34,7 @@ - (instancetype)self;
header "b.h"
}
-//--- main.m
+//--- main-macos.m
#import <a.h>
#import <b.h> // NOTE: Non attributed decl imported after one with attrs.
@@ -39,3 +47,13 @@ int main() {
}
// CHECK: @"OBJC_CLASS_$_INIntent" = extern_weak
+
+//--- main-ios.m
+
+#import <a.h>
+#import <b.h>
+
+ at implementation INIntent (Testing)
+ at end
+
+// CHECK: @"OBJC_CLASS_$_INIntent" = extern_weak
More information about the cfe-commits
mailing list