[clang] bcc8811 - [clang] Emit an error if variable ends up with incomplete array type

via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 29 06:56:13 PDT 2023


Author: Podchishchaeva, Mariya
Date: 2023-08-29T06:50:32-07:00
New Revision: bcc881161aeb56ea5a38858fe0007e59f21042c4

URL: https://github.com/llvm/llvm-project/commit/bcc881161aeb56ea5a38858fe0007e59f21042c4
DIFF: https://github.com/llvm/llvm-project/commit/bcc881161aeb56ea5a38858fe0007e59f21042c4.diff

LOG: [clang] Emit an error if variable ends up with incomplete array type

This adds an error if variable with incomplete type has initializer with
incomplete type, so it is not possible to deduce array size from
initializer.

Fixes https://github.com/llvm/llvm-project/issues/37257

Reviewed By: aaron.ballman, shafik

Differential Revision: https://reviews.llvm.org/D158615

Added: 
    clang/test/SemaCXX/gh37257.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index deb53303a21b44..9cd95005d7e059 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -189,6 +189,9 @@ Bug Fixes in This Version
   (`#64876 <https://github.com/llvm/llvm-project/issues/64876>`_)
 - Fixed an assertion if a function has cleanups and fatal erors.
   (`#48974 <https://github.com/llvm/llvm-project/issues/48974>`_)
+- Clang now emits an error if it is not possible to deduce array size for a
+  variable with incomplete array type.
+  (`#37257 <https://github.com/llvm/llvm-project/issues/37257>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 664af4ccf4c635..18355b484975af 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13445,6 +13445,18 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
     IsParenListInit = !InitSeq.steps().empty() &&
                       InitSeq.step_begin()->Kind ==
                           InitializationSequence::SK_ParenthesizedListInit;
+    QualType VDeclType = VDecl->getType();
+    if (Init && !Init->getType().isNull() &&
+        !Init->getType()->isDependentType() && !VDeclType->isDependentType() &&
+        Context.getAsIncompleteArrayType(VDeclType) &&
+        Context.getAsIncompleteArrayType(Init->getType())) {
+      // Bail out if it is not possible to deduce array size from the
+      // initializer.
+      Diag(VDecl->getLocation(), diag::err_typecheck_decl_incomplete_type)
+          << VDeclType;
+      VDecl->setInvalidDecl();
+      return;
+    }
   }
 
   // Check for self-references within variable initializers.

diff  --git a/clang/test/SemaCXX/gh37257.cpp b/clang/test/SemaCXX/gh37257.cpp
new file mode 100644
index 00000000000000..ebcd32d18f69da
--- /dev/null
+++ b/clang/test/SemaCXX/gh37257.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template<class T>
+T&& create();
+
+template<class T, class... Args>
+void test() {
+  T t(create<Args>()...); // expected-error{{variable has incomplete type 'int[]'}}
+  (void) t;
+}
+
+struct A;
+
+int main() {
+  test<int[]>(); // expected-note {{in instantiation of function template specialization 'test<int[]>' requested here}}
+}


        


More information about the cfe-commits mailing list