https://github.com/keepyixiao updated 
https://github.com/llvm/llvm-project/pull/223124

>From bf043645f8ef9ff38bc53b705368363c23d1d534 Mon Sep 17 00:00:00 2001
From: nudt_yixiao <[email protected]>
Date: Wed, 23 Sep 2026 10:39:13 +0800
Subject: [PATCH] [Clang] Avoid assertion failure for initialized extern
 aliases

Alias attributes are processed before variable initializers are attached, so
an initialized extern variable can retain an AliasAttr and reach a late
assertion.

Diagnose and drop the AliasAttr when attaching the initializer. Add a
regression test and release note.
---
 clang/docs/ReleaseNotes.md           |  4 ++++
 clang/lib/Sema/SemaDecl.cpp          | 12 ++++++++++++
 clang/test/Sema/alias-redefinition.c |  2 ++
 clang/test/SemaCXX/attr-weakref.cpp  |  3 +++
 4 files changed, 21 insertions(+)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 59b5c14c59242c..1ba85521baeb39 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -536,6 +536,10 @@ features cannot lower the translation-unit ABI level;
   written after the declarator-id, where it appertains to the declared entity
   rather than to a declarator chunk. (#GH196982, #GH111463)
 
+- Fixed an assertion failure when the `alias` attribute was applied to an
+  `extern` variable with an initializer. Clang now correctly diagnoses that
+  such a declaration is a definition and cannot also be an alias.
+
 #### Bug Fixes to C++ Support
 
 - Fixed false-positive module ODR diagnostics when a type is found through a
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a9047f61a8bf5e..4e4f799be523eb 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14093,6 +14093,18 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr 
*Init, bool DirectInit) {
     return;
   }
 
+  // handleAliasAttr() runs before the initializer is attached to the VarDecl,
+  // so it cannot determine that an extern declaration with an initializer is a
+  // definition. Diagnose the conflict now that an initializer is present and
+  // remove the attribute before the post-initialization checks.
+  if (const auto *Attr = VDecl->getAttr<AliasAttr>()) {
+    if (!VDecl->isInvalidDecl()) {
+      Diag(Attr->getLocation(), diag::err_alias_is_definition) << VDecl << 0;
+      VDecl->setInvalidDecl();
+    }
+    VDecl->dropAttr<AliasAttr>();
+  }
+
   if (VDecl->isInvalidDecl()) {
     ExprResult Recovery =
         CreateRecoveryExpr(Init->getBeginLoc(), Init->getEndLoc(), {Init});
diff --git a/clang/test/Sema/alias-redefinition.c 
b/clang/test/Sema/alias-redefinition.c
index 526b67d9be7f29..1b81d926505db4 100644
--- a/clang/test/Sema/alias-redefinition.c
+++ b/clang/test/Sema/alias-redefinition.c
@@ -24,6 +24,8 @@ void __attribute((alias("f5"))) fun5(void) {} // 
expected-error {{definition 'fu
 
 int var1 __attribute((alias("v1"))); // expected-error {{definition 'var1' 
cannot also be an alias}}
 static int var2 __attribute((alias("v2"))) = 2; // expected-error {{definition 
'var2' cannot also be an alias}}
+extern int var_with_extern_initializer __attribute__((alias(""))) = 42; // 
expected-error {{definition 'var_with_extern_initializer' cannot also be an 
alias}}
+extern int var_with_extern_initializer1 __attribute__((alias("v1"))) = 42; // 
expected-error {{definition 'var_with_extern_initializer1' cannot also be an 
alias}}
 
 extern int var3 __attribute__((alias("C"))); // expected-note{{previous 
definition is here}}
 int var3 = 3; // expected-error{{redefinition of 'var3'}}
diff --git a/clang/test/SemaCXX/attr-weakref.cpp 
b/clang/test/SemaCXX/attr-weakref.cpp
index 4e1834096ebede..f3b0f9cac9d6b4 100644
--- a/clang/test/SemaCXX/attr-weakref.cpp
+++ b/clang/test/SemaCXX/attr-weakref.cpp
@@ -36,3 +36,6 @@ int a10() __attribute__((weakref ("foo")));
 static int v __attribute__((weakref(a1), alias("foo"))); // expected-error 
{{expected string literal as argument of 'weakref' attribute}}
 
 __attribute__((weakref ("foo"))) auto a11 = 1; // expected-error {{weakref 
declaration must have internal linkage}}
+// expected-error@-1 {{definition 'a11' cannot also be an alias}}
+
+static int a12 __attribute__((weakref("foo"))) = 1; // expected-error 
{{definition 'a12' cannot also be an alias}}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to