llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: None (cmtice)

<details>
<summary>Changes</summary>

When handling anonymous structs, GetIndexOfChildMemberWithName needs to add the 
number of non-empty base classes to the child index, to get the actual correct 
index. It was not doing so. This fixes that.

---
Full diff: https://github.com/llvm/llvm-project/pull/138487.diff


4 Files Affected:

- (modified) lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp (+8-1) 
- (added) lldb/test/API/lang/cpp/type_lookup_anon_struct/Makefile (+3) 
- (added) 
lldb/test/API/lang/cpp/type_lookup_anon_struct/TestCppTypeLookupAnonStruct.py 
(+27) 
- (added) lldb/test/API/lang/cpp/type_lookup_anon_struct/main.cpp (+33) 


``````````diff
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp 
b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 1a2b3d4133e51..dd8d144510056 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -6743,7 +6743,14 @@ size_t TypeSystemClang::GetIndexOfChildMemberWithName(
           if (field_name.empty()) {
             CompilerType field_type = GetType(field->getType());
             std::vector<uint32_t> save_indices = child_indexes;
-            child_indexes.push_back(child_idx);
+            if (field_type.IsAnonymousType())
+              // We have to add on the number of non-empty base classes to this
+              // index!
+              child_indexes.push_back(
+                  child_idx +
+                  TypeSystemClang::GetNumBaseClasses(cxx_record_decl, true));
+            else
+              child_indexes.push_back(child_idx);
             if (field_type.GetIndexOfChildMemberWithName(
                     name, omit_empty_base_classes, child_indexes))
               return child_indexes.size();
diff --git a/lldb/test/API/lang/cpp/type_lookup_anon_struct/Makefile 
b/lldb/test/API/lang/cpp/type_lookup_anon_struct/Makefile
new file mode 100644
index 0000000000000..99998b20bcb05
--- /dev/null
+++ b/lldb/test/API/lang/cpp/type_lookup_anon_struct/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
diff --git 
a/lldb/test/API/lang/cpp/type_lookup_anon_struct/TestCppTypeLookupAnonStruct.py 
b/lldb/test/API/lang/cpp/type_lookup_anon_struct/TestCppTypeLookupAnonStruct.py
new file mode 100644
index 0000000000000..2295ecfe81ff7
--- /dev/null
+++ 
b/lldb/test/API/lang/cpp/type_lookup_anon_struct/TestCppTypeLookupAnonStruct.py
@@ -0,0 +1,27 @@
+"""
+Test that we properly print multiple types.
+"""
+
+import lldb
+import lldbsuite.test.lldbutil as lldbutil
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import decorators
+
+
+class TestTypeLookupAnonStruct(TestBase):
+    def test_lookup_anon_struct(self):
+        self.build()
+        lldbutil.run_to_source_breakpoint(
+            self, '// Set breakpoint here', lldb.SBFileSpec('main.cpp')
+        )
+
+        self.expect_var_path('unnamed_derived.y', value='2')
+        self.expect_var_path('unnamed_derived.z', value='13')
+        self.expect('frame variable "derb.x"', error=True,
+                    substrs=['"x" is not a member of "(DerivedB) derb"'])
+        self.expect('frame variable "derb.y"', error=True,
+                    substrs=['"y" is not a member of "(DerivedB) derb"'])
+        self.expect_var_path('derb.w', value='14')
+        self.expect_var_path('derb.k', value='15')
+        self.expect_var_path('derb.a.x', value='1')
+        self.expect_var_path('derb.a.y', value='2')
diff --git a/lldb/test/API/lang/cpp/type_lookup_anon_struct/main.cpp 
b/lldb/test/API/lang/cpp/type_lookup_anon_struct/main.cpp
new file mode 100644
index 0000000000000..18dd997ea9563
--- /dev/null
+++ b/lldb/test/API/lang/cpp/type_lookup_anon_struct/main.cpp
@@ -0,0 +1,33 @@
+int main(int argc, char** argv) {
+  struct A {
+    struct {
+      int x = 1;
+    };
+    int y = 2;
+  } a;
+
+   struct B {
+    // Anonymous struct inherits another struct.
+    struct : public A {
+      int z = 3;
+    };
+    int w = 4;
+    A a;
+  } b;
+
+   struct : public A {
+     struct {
+       int z = 13;
+     };
+   } unnamed_derived;
+
+   struct DerivedB : public B {
+     struct {
+       // `w` in anonymous struct overrides `w` from `B`.
+       int w = 14;
+       int k = 15;
+     };
+   } derb;
+
+   return 0; // Set breakpoint here
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/138487
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to