llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Mallarapu Ujwal (ujwal-224)

<details>
<summary>Changes</summary>

* 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-&gt;getMostRecentDecl()-&gt;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.

---
Full diff: https://github.com/llvm/llvm-project/pull/243008.diff


3 Files Affected:

- (modified) clang/lib/Sema/Sema.cpp (+14-8) 
- (modified) clang/test/CodeGen/tentative-decls.c (+10) 
- (modified) clang/test/Sema/extern-redecl.c (+11) 


``````````diff
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[];

``````````

</details>


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

Reply via email to