[clang] 056042d - [clang][Interp] Fix initializing fields after base class members

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 3 06:58:31 PDT 2023


Author: Timm Bäder
Date: 2023-04-03T15:55:11+02:00
New Revision: 056042d21b72a86653f88719c0b54b07e35d2144

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

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

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index e5c9b2637b85..ee5dd73159a6 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1384,6 +1384,7 @@ bool ByteCodeExprGen<Emitter>::visitRecordInitializer(const Expr *Initializer) {
 
         if (!this->emitPopPtr(Initializer))
           return false;
+        ++InitIndex;
       } else {
         // Initializer for a direct base class.
         if (const Record::Base *B = R->getBase(Init->getType())) {
@@ -1395,6 +1396,8 @@ bool ByteCodeExprGen<Emitter>::visitRecordInitializer(const Expr *Initializer) {
 
           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
@@ -1407,9 +1410,9 @@ bool ByteCodeExprGen<Emitter>::visitRecordInitializer(const Expr *Initializer) {
 
           if (!this->emitPopPtr(Initializer))
             return false;
+          ++InitIndex;
         }
       }
-      ++InitIndex;
     }
 
     return true;

diff  --git a/clang/test/AST/Interp/cxx20.cpp b/clang/test/AST/Interp/cxx20.cpp
index 6cb106adf59c..37be7a41bf75 100644
--- a/clang/test/AST/Interp/cxx20.cpp
+++ b/clang/test/AST/Interp/cxx20.cpp
@@ -583,3 +583,20 @@ namespace Destructors {
   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};
+  static_assert(c.a == 1 && c.b == 2 && c.c == 3);
+}


        


More information about the cfe-commits mailing list