aboutsummaryrefslogtreecommitdiff
path: root/gdb/valops.c
diff options
context:
space:
mode:
authorBruno Larsen <blarsen@redhat.com>2021-10-29 17:56:28 -0300
committerBruno Larsen <blarsen@redhat.com>2021-11-25 09:56:03 -0300
commita41ad3474ceacba39e11c7478154c0e553784a01 (patch)
treedb9de693ebbb1b1596b4df4a12d3f229f4d250b9 /gdb/valops.c
parent423e91d347cf6050ac17c7098cad6cbc15e5e50f (diff)
downloadgdb-a41ad3474ceacba39e11c7478154c0e553784a01.zip
gdb-a41ad3474ceacba39e11c7478154c0e553784a01.tar.gz
gdb-a41ad3474ceacba39e11c7478154c0e553784a01.tar.bz2
PR gdb/28480: Improve ambiguous member detection
Basic ambiguity detection assumes that when 2 fields with the same name have the same byte offset, it must be an unambiguous request. This is not always correct. Consider the following code: class empty { }; class A { public: [[no_unique_address]] empty e; }; class B { public: int e; }; class C: public A, public B { }; if we tried to use c.e in code, the compiler would warn of an ambiguity, however, since A::e does not demand an unique address, it gets the same address (and thus byte offset) of the members, making A::e and B::e have the same address. however, "print c.e" would fail to report the ambiguity, and would instead print it as an empty class (first path found). The new code solves this by checking for other found_fields that have different m_struct_path.back() (final class that the member was found in), despite having the same byte offset. The testcase gdb.cp/ambiguous.exp was also changed to test for this behavior.
Diffstat (limited to 'gdb/valops.c')
-rw-r--r--gdb/valops.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/gdb/valops.c b/gdb/valops.c
index 9787cdb..c552e82 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -1962,6 +1962,34 @@ struct_field_searcher::update_result (struct value *v, LONGEST boffset)
space. */
if (m_fields.empty () || m_last_boffset != boffset)
m_fields.push_back ({m_struct_path, v});
+ else
+ {
+ /*Fields can occupy the same space and have the same name (be
+ ambiguous). This can happen when fields in two different base
+ classes are marked [[no_unique_address]] and have the same name.
+ The C++ standard says that such fields can only occupy the same
+ space if they are of different type, but we don't rely on that in
+ the following code. */
+ bool ambiguous = false, insert = true;
+ for (const found_field &field: m_fields)
+ {
+ if(field.path.back () != m_struct_path.back ())
+ {
+ /* Same boffset points to members of different classes.
+ We have found an ambiguity and should record it. */
+ ambiguous = true;
+ }
+ else
+ {
+ /* We don't need to insert this value again, because a
+ non-ambiguous path already leads to it. */
+ insert = false;
+ break;
+ }
+ }
+ if (ambiguous && insert)
+ m_fields.push_back ({m_struct_path, v});
+ }
}
}
}