tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, rsmith.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When calling `hasArrayFiller()` on a `APValue` that's not an array, the two 
functions called inside `hasArrayFiller()` would both run into their 
`isArray()` assertions.

This triggered for me with the included test case in `ExprConstant.cpp`:

  if (!Result.hasArrayFiller())
    return Success;

adding a `!Result.isArray() ||` there would also work of course but I think the 
version in this patch is safer.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128248

Files:
  clang/include/clang/AST/APValue.h
  clang/test/SemaCXX/constexpr-array-init.cpp


Index: clang/test/SemaCXX/constexpr-array-init.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/constexpr-array-init.cpp
@@ -0,0 +1,19 @@
+
+// RUN: %clang_cc1 -std=c++20 -verify %s
+
+struct Foo {
+  int a; // expected-note {{subobject declared here}}
+  constexpr Foo()
+      : a(get_int()) {
+  }
+
+  constexpr int get_int() {
+    return 5;
+  }
+};
+
+
+static constexpr Foo bar[2][1] = { // expected-error {{constexpr variable 
'bar' must be initialized by a constant expression}} \
+                                   // expected-note {{subobject of type 'int' 
is not initialized}}
+    {{}},
+};
Index: clang/include/clang/AST/APValue.h
===================================================================
--- clang/include/clang/AST/APValue.h
+++ clang/include/clang/AST/APValue.h
@@ -508,6 +508,8 @@
     return const_cast<APValue*>(this)->getArrayInitializedElt(I);
   }
   bool hasArrayFiller() const {
+    if (!isArray())
+      return false;
     return getArrayInitializedElts() != getArraySize();
   }
   APValue &getArrayFiller() {


Index: clang/test/SemaCXX/constexpr-array-init.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/constexpr-array-init.cpp
@@ -0,0 +1,19 @@
+
+// RUN: %clang_cc1 -std=c++20 -verify %s
+
+struct Foo {
+  int a; // expected-note {{subobject declared here}}
+  constexpr Foo()
+      : a(get_int()) {
+  }
+
+  constexpr int get_int() {
+    return 5;
+  }
+};
+
+
+static constexpr Foo bar[2][1] = { // expected-error {{constexpr variable 'bar' must be initialized by a constant expression}} \
+                                   // expected-note {{subobject of type 'int' is not initialized}}
+    {{}},
+};
Index: clang/include/clang/AST/APValue.h
===================================================================
--- clang/include/clang/AST/APValue.h
+++ clang/include/clang/AST/APValue.h
@@ -508,6 +508,8 @@
     return const_cast<APValue*>(this)->getArrayInitializedElt(I);
   }
   bool hasArrayFiller() const {
+    if (!isArray())
+      return false;
     return getArrayInitializedElts() != getArraySize();
   }
   APValue &getArrayFiller() {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to