https://github.com/ujwal-224 created https://github.com/llvm/llvm-project/pull/243008
* When a tentative array definition (int a[];) is completed by a later extern declaration (extern int a[10];), Clang erroneously clamped its storage size to 1 ([1 x i32]) and emitted a warning. * Sema::ActOnEndOfTranslationUnit() to inspect VD->getMostRecentDecl()->getType() so the tentative definition adopts the completed composite type without warning. * Tests added to clang/test/Sema/extern-redecl.c and clang/test/CodeGen/tentative-decls.c. >From 5f0381b7b7c9b6b9b35951f29c4f2915e4b56fb3 Mon Sep 17 00:00:00 2001 From: ujwal-224 <[email protected]> Date: Mon, 16 Nov 2026 14:58:26 +0530 Subject: [PATCH] fix storage size for tentative array --- clang/lib/Sema/Sema.cpp | 22 ++++++++++++++-------- clang/test/CodeGen/tentative-decls.c | 10 ++++++++++ clang/test/Sema/extern-redecl.c | 11 +++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index fcfcc706d2f9d3..a2c9e4e539e920 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -1514,14 +1514,20 @@ void Sema::ActOnEndOfTranslationUnit() { if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second) continue; - if (const IncompleteArrayType *ArrayT - = Context.getAsIncompleteArrayType(VD->getType())) { - // Set the length of the array to 1 (C99 6.9.2p5). - Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); - llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); - QualType T = Context.getConstantArrayType( - ArrayT->getElementType(), One, nullptr, ArraySizeModifier::Normal, 0); - VD->setType(T); + if (const IncompleteArrayType *ArrayT = + Context.getAsIncompleteArrayType(VD->getType())) { + QualType MostRecentType = VD->getMostRecentDecl()->getType(); + if (!MostRecentType->isIncompleteArrayType()) { + VD->setType(MostRecentType); + } else { + // Set the length of the array to 1 (C99 6.9.2p5). + Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); + llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); + QualType T = + Context.getConstantArrayType(ArrayT->getElementType(), One, nullptr, + ArraySizeModifier::Normal, 0); + VD->setType(T); + } } else if (RequireCompleteType(VD->getLocation(), VD->getType(), diag::err_tentative_def_incomplete_type)) VD->setInvalidDecl(); diff --git a/clang/test/CodeGen/tentative-decls.c b/clang/test/CodeGen/tentative-decls.c index 9453f15dbc619d..cdbb5dded90f88 100644 --- a/clang/test/CodeGen/tentative-decls.c +++ b/clang/test/CodeGen/tentative-decls.c @@ -36,3 +36,13 @@ static int c5[]; static int func(void) { return c5[0]; } int callfunc(void) { return func(); } +// GH242842: Check that tentative array completed by later extern gets the completed size. +// CHECK-DAG: @gh242842_a = {{(dso_local )?}}global [10 x {{.*}}] zeroinitializer +int gh242842_a[]; +extern int gh242842_a[10]; + +// CHECK-DAG: @gh242842_b = internal global [10 x {{.*}}] zeroinitializer +static int gh242842_b[]; +extern int gh242842_b[10]; +int *use_gh242842_b(void) { return gh242842_b; } + diff --git a/clang/test/Sema/extern-redecl.c b/clang/test/Sema/extern-redecl.c index 3a9720986137ca..57848ed7fdad3f 100644 --- a/clang/test/Sema/extern-redecl.c +++ b/clang/test/Sema/extern-redecl.c @@ -84,3 +84,14 @@ void test6(void) { } } } + +// GH242842: tentative array completed by later extern should not warn. +int gh242842_a[]; +extern int gh242842_a[10]; + +static int gh242842_b[]; +extern int gh242842_b[10]; + +int gh242842_c[]; +extern int gh242842_c[10]; +extern int gh242842_c[]; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
