From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A92453857810 for ; Thu, 4 Nov 2021 21:05:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A92453857810 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-244-jzFSKZVuNZKSqAWQ_P1aOg-1; Thu, 04 Nov 2021 17:05:19 -0400 X-MC-Unique: jzFSKZVuNZKSqAWQ_P1aOg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DBC8E8042E4 for ; Thu, 4 Nov 2021 21:05:18 +0000 (UTC) Received: from blarsen.com (ovpn-116-55.gru2.redhat.com [10.97.116.55]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 332FB57CD3; Thu, 4 Nov 2021 21:05:16 +0000 (UTC) From: Bruno Larsen To: gdb-patches@sourceware.org Subject: [PATCH] PR gdb/28480: Improve ambiguous member detection Date: Thu, 4 Nov 2021 18:04:57 -0300 Message-Id: <20211104210457.35512-1-blarsen@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Nov 2021 21:05:23 -0000 Basic ambiguity detection assumes that when 2 fields with the same name have the same boffset, 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 boffset) 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 boffset. The testcase gdb.cp/ambiguous.exp was also changed to test for this behavior. --- gdb/testsuite/gdb.cp/ambiguous.cc | 13 +++++++++++++ gdb/testsuite/gdb.cp/ambiguous.exp | 7 +++++++ gdb/valops.c | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/gdb/testsuite/gdb.cp/ambiguous.cc b/gdb/testsuite/gdb.cp/ambiguous.cc index a55686547f2..b2be7297b28 100644 --- a/gdb/testsuite/gdb.cp/ambiguous.cc +++ b/gdb/testsuite/gdb.cp/ambiguous.cc @@ -1,3 +1,4 @@ +class empty { }; class A1 { public: @@ -17,6 +18,11 @@ public: int y; }; +class A4 { +public: + [[no_unique_address]] empty x; +}; + class X : public A1, public A2 { public: int z; @@ -77,6 +83,10 @@ public: int jva1v; }; +class JE : public A1, public A4 { +public: +}; + int main() { A1 a1; @@ -92,6 +102,7 @@ int main() JVA1 jva1; JVA2 jva2; JVA1V jva1v; + JE je; int i; @@ -173,5 +184,7 @@ int main() jva1v.i = 4; jva1v.jva1v = 5; + je.A1::x = 1; + return 0; /* set breakpoint here */ } diff --git a/gdb/testsuite/gdb.cp/ambiguous.exp b/gdb/testsuite/gdb.cp/ambiguous.exp index 008898c5818..68b82d45b68 100644 --- a/gdb/testsuite/gdb.cp/ambiguous.exp +++ b/gdb/testsuite/gdb.cp/ambiguous.exp @@ -264,3 +264,10 @@ gdb_test "print (A1)(KV)jva1" " = \{x = 3, y = 4\}" # JVA1V is derived from A1; A1 is a virtual base indirectly # and also directly; must not report ambiguity when a JVA1V is cast to an A1. gdb_test "print (A1)jva1v" " = {x = 1, y = 2}" + +#unique_ptr is a weird edge-case that interacts differently with the +#ambiguity detection, so we should test it directly +test_ambiguous "je.x" "x" "JE" { + "'int A1::x' (JE -> A1)" + "'empty A4::x' (JE -> A4)" +} diff --git a/gdb/valops.c b/gdb/valops.c index 9787cdbb513..75b732af62b 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -1962,6 +1962,32 @@ 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 + /* Some fields may occupy the same space and still be ambiguous. + This happens when [[no_unique_address]] is used by a member + of the class. We assume that this only happens when the types are + different. This is not necessarily complete, but a situation where + this assumption is incorrect is unlikely*/ + { + bool ambiguous = false, insert = true; + for(auto finds: m_fields){ + if(finds.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; + } + } + if(ambiguous && insert){ + m_fields.push_back({m_struct_path, v}); + } + } } } } -- 2.27.0