[PATCH] D145860: [clang][Interp] Fix initializing fields after base class members

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 11 22:46:50 PST 2023


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

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145860

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/test/AST/Interp/cxx20.cpp


Index: clang/test/AST/Interp/cxx20.cpp
===================================================================
--- clang/test/AST/Interp/cxx20.cpp
+++ clang/test/AST/Interp/cxx20.cpp
@@ -583,3 +583,19 @@
   constexpr Outer O;
   static_assert(O.bar() == 12);
 }
+
+namespace BaseAndFieldInit {
+  struct A {
+    int a;
+  };
+
+  struct B : A {
+    int b;
+  };
+
+  struct C : B {
+    int c;
+  };
+
+  constexpr C c = {1,2,3};
+}
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1410,6 +1410,7 @@
 
         if (!this->emitPopPtr(Initializer))
           return false;
+        ++InitIndex;
       } else {
         // Initializer for a direct base class.
         if (const Record::Base *B = R->getBase(Init->getType())) {
@@ -1421,6 +1422,8 @@
 
           if (!this->emitPopPtr(Initializer))
             return false;
+          // Base initializers don't increase InitIndex, since they don't count
+          // into the Record's fields.
         } else {
           const Record::Field *FieldToInit = R->getField(InitIndex);
           // Non-primitive case. Get a pointer to the field-to-initialize
@@ -1433,9 +1436,9 @@
 
           if (!this->emitPopPtr(Initializer))
             return false;
+          ++InitIndex;
         }
       }
-      ++InitIndex;
     }
 
     return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145860.504408.patch
Type: text/x-patch
Size: 1457 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230312/f5523173/attachment.bin>


More information about the cfe-commits mailing list