https://github.com/ujwal-224 updated 
https://github.com/llvm/llvm-project/pull/243008

>From 87318990af726782c9ea6c82c35059c4d4827808 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              | 32 +++++++++++++++++++++-------
 clang/test/CodeGen/tentative-decls.c | 10 +++++++++
 clang/test/Sema/extern-redecl.c      | 17 +++++++++++++++
 3 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index fcfcc706d2f9d3..6b264afc6bb6ba 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1514,14 +1514,30 @@ 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())) {
+      VarDecl *MostRecentFileDecl = nullptr;
+      for (VarDecl *Prev = VD->getMostRecentDecl(); Prev;
+           Prev = Prev->getPreviousDecl()) {
+        if (Prev->isFileVarDecl()) {
+          MostRecentFileDecl = Prev;
+          break;
+        }
+      }
+      QualType MostRecentType =
+          MostRecentFileDecl ? MostRecentFileDecl->getType() : QualType();
+      if (!MostRecentType.isNull() &&
+          !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..59420bc615e287 100644
--- a/clang/test/Sema/extern-redecl.c
+++ b/clang/test/Sema/extern-redecl.c
@@ -84,3 +84,20 @@ 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[];
+
+// Block-scope extern does not complete file-scope tentative definition.
+static int gh242842_d[]; // expected-warning {{tentative array definition 
assumed to have one element}}
+void gh242842_fn(void) {
+  extern int gh242842_d[10];
+}

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

Reply via email to