[PATCH] D24312: [CodeGen] Fix an assert in EmitNullConstant

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 7 13:16:53 PDT 2016


ahatanak created this revision.
ahatanak added a reviewer: rjmccall.
ahatanak added a subscriber: cfe-commits.

r235815 changed CGRecordLowering::accumulateBases to ignore non-virtual bases of size 0, which prevented adding those non-virtual bases to CGRecordLayout's NonVirtualBases. EmitNullConstant calls CGRecordLayout::getNonVirtualBaseLLVMFieldNo to get the field number of the base, which causes an assert. This patch fixes the bug.

https://reviews.llvm.org/D24312

Files:
  lib/CodeGen/CGExprConstant.cpp
  test/CodeGenCXX/empty-classes.cpp

Index: test/CodeGenCXX/empty-classes.cpp
===================================================================
--- test/CodeGenCXX/empty-classes.cpp
+++ test/CodeGenCXX/empty-classes.cpp
@@ -96,3 +96,24 @@
   // Type checked at the top of the file.
   B b;
 };
+
+// This test used to crash when CGRecordLayout::getNonVirtualBaseLLVMFieldNo was called.
+namespace record_layout {
+struct X0 {
+  int x[0];
+};
+
+template<typename>
+struct X2 : X0 {
+};
+
+template<typename>
+struct X3 : X2<int> {
+  X3() : X2<int>() {}
+};
+
+
+void test0() {
+  X3<int>();
+}
+}
Index: lib/CodeGen/CGExprConstant.cpp
===================================================================
--- lib/CodeGen/CGExprConstant.cpp
+++ lib/CodeGen/CGExprConstant.cpp
@@ -1532,7 +1532,8 @@
       cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
 
     // Ignore empty bases.
-    if (base->isEmpty())
+    if (base->isEmpty() ||
+        CGM.getContext().getASTRecordLayout(base).getNonVirtualSize().isZero())
       continue;
     
     unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24312.70589.patch
Type: text/x-patch
Size: 1094 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160907/c4d5411d/attachment.bin>


More information about the cfe-commits mailing list