[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:49 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: nudt_yixiao (keepyixiao)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/223124.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaDecl.cpp (+1-1)
- (modified) clang/test/Sema/alias-redefinition.c (+2)
``````````diff
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'}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/223124
More information about the cfe-commits
mailing list