[clang] [Clang] Avoid assertion failure for initialized extern aliases (PR #223124)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 11 22:57:16 PDT 2026
https://github.com/keepyixiao created https://github.com/llvm/llvm-project/pull/223124
An extern variable with an initializer is both a definition and externally visible. When such a declaration has an alias attribute, the late alias validation incorrectly assumes that the variable is not externally visible and triggers an assertion failure.
Remove the invalid external-visibility assertion and allow the existing diagnostic to report that a definition cannot also be an alias.
Add a Sema regression test covering an initialized extern variable with an empty alias target.
Fixes https://github.com/llvm/llvm-project/issues/204762
>From bb1a8a339bbf28d6f468b28913b23c6f20ab5f2f Mon Sep 17 00:00:00 2001
From: yixiao <yixiao at hygon.cn>
Date: Sat, 12 Sep 2026 13:50:35 +0800
Subject: [PATCH] [Clang] Avoid assertion failure for initialized extern
aliases
An extern variable with an initializer is both a definition and externally
visible. When such a declaration has an alias attribute, the late alias
validation incorrectly assumes that the variable is not externally visible
and triggers an assertion failure.
Remove the invalid external-visibility assertion and allow the existing
diagnostic to report that a definition cannot also be an alias.
Add a Sema regression test covering an initialized extern variable with an
empty alias target.
---
clang/lib/Sema/SemaDecl.cpp | 2 +-
clang/test/Sema/alias-redefinition.c | 2 ++
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a9047f61a8bf5..34a5b72b8b64e 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7180,7 +7180,7 @@ static void checkAliasAttr(Sema &S, NamedDecl &ND) {
if (VD->hasInit()) {
if (const auto *Attr = VD->getAttr<AliasAttr>()) {
assert(VD->isThisDeclarationADefinition() &&
- !VD->isExternallyVisible() && "Broken AliasAttr handled late!");
+ "Broken AliasAttr handled late!");
S.Diag(Attr->getLocation(), diag::err_alias_is_definition) << VD << 0;
VD->dropAttr<AliasAttr>();
}
diff --git a/clang/test/Sema/alias-redefinition.c b/clang/test/Sema/alias-redefinition.c
index 526b67d9be7f2..4cf9fa26aca78 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-warning {{'extern' variable has an initializer}}
+// expected-error at -1 {{definition 'var_with_extern_initializer' 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'}}
More information about the cfe-commits
mailing list