https://github.com/28arnab updated https://github.com/llvm/llvm-project/pull/243855
>From 763c32bfce00a0c87d67a6239988ebc28cde029e Mon Sep 17 00:00:00 2001 From: Arnab Sarkar <[email protected]> Date: Wed, 18 Nov 2026 19:11:42 +0530 Subject: [PATCH] [Clang][Sema] Propagate completed array type to tentative definitions At translation unit termination (Sema::ActOnEndOfTranslationUnit), tentative array definitions that were completed by later extern declarations (e.g., `extern int b[3];`) were still checking the type of the initial declaration rather than querying the most recent redeclaration. This caused completed tentative arrays to incorrectly fall back to a size of 1 and emit an erroneous warning. This fix checks `VD->getMostRecentDecl()` during tentative definition processing and updates the variable's type if a later redeclaration has completed the array type. Signed-off-by: Arnab Sarkar <[email protected]> Assisted-By: AI --- clang/lib/Sema/Sema.cpp | 6 ++++++ clang/test/CodeGen/tentative-decls.c | 4 ++++ clang/test/Sema/tentative-decls.c | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index ebc8b85cf8a628..ece3421cf104af 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -1515,6 +1515,12 @@ void Sema::ActOnEndOfTranslationUnit() { if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second) continue; + // If a later redeclaration (e.g. extern int b[3]) completed the type, + // update the tentative definition to use that completed type. + VarDecl *MostRecent = VD->getMostRecentDecl(); + if (MostRecent != VD && !MostRecent->getType()->isIncompleteArrayType()) + VD->setType(MostRecent->getType()); + if (const IncompleteArrayType *ArrayT = Context.getAsIncompleteArrayType(VD->getType())) { // Set the length of the array to 1 (C99 6.9.2p5). diff --git a/clang/test/CodeGen/tentative-decls.c b/clang/test/CodeGen/tentative-decls.c index 9453f15dbc619d..7ee4e1e49a506e 100644 --- a/clang/test/CodeGen/tentative-decls.c +++ b/clang/test/CodeGen/tentative-decls.c @@ -36,3 +36,7 @@ static int c5[]; static int func(void) { return c5[0]; } int callfunc(void) { return func(); } +// GH243855 +// CHECK-DAG: @d = {{(dso_local )?}}global [3 x {{.*}}] zeroinitializer +int d[]; +extern int d[3]; diff --git a/clang/test/Sema/tentative-decls.c b/clang/test/Sema/tentative-decls.c index 94d21bdbf94daf..6b532e903b5d27 100644 --- a/clang/test/Sema/tentative-decls.c +++ b/clang/test/Sema/tentative-decls.c @@ -63,3 +63,7 @@ static int b0; static int a0[] = { 4 }; static int b0 = 5; + +// GH243855 +int d[]; +extern int d[3]; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
