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.129.124]) by sourceware.org (Postfix) with ESMTPS id 694593858D28 for ; Mon, 22 Nov 2021 18:00:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 694593858D28 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-217-dOiw9Nq2NImflwl4QIknsQ-1; Mon, 22 Nov 2021 13:00:14 -0500 X-MC-Unique: dOiw9Nq2NImflwl4QIknsQ-1 Received: by mail-wm1-f70.google.com with SMTP id 201-20020a1c04d2000000b003335bf8075fso7153229wme.0 for ; Mon, 22 Nov 2021 10:00:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=88sNS6x6HNdsK3Xk/XEv/4+lFlWI8D8G6Eo/f1e57IM=; b=RdrK/v7RR0Lk5ZMlTqcIBHhFy8XQa8TSKgkGPejZ9n1t2t/1ZHuExHzQMqv1UwAaQg 1Z+dMwDaVvzDapt7mi1l3jFhmrDEghE+Y9MrZnVOPonmezwexTm+KAb0m3Kxh53H5bqb eCoySUWV2EtKsXxpBqyLQ91hPExzlcJ1Q/KQUDvvGGqCI8XVXsIalyYmR45sdM6WdJn2 oSakPePCavLDygpkybQIVfPpqfFft1VDA3+ZF4ScW5iZy8wXgf38OmdK/1Y9nTwxmaz8 RL3hcKZb2bRmw4wxi1QfGQ1EW6A+KmJx28EEDm95HMXjBAfk0Y5oX/maZijqP2Ki2sDE hDFg== X-Gm-Message-State: AOAM533JYnvuno+hFzdSgTj/sNn/c1MHpwzauSUEbmtptYjHWG3l2/2a iJpk23/lMs9FfCATd1wRwgUpKAWtb42R9OORR4m2WHm8O1OCzjUlwrySdQ8v3aiRu49LWQwjgv9 SWbj9ym4Nsl6Ni19+e5n0Ig== X-Received: by 2002:a05:600c:4154:: with SMTP id h20mr30885418wmm.189.1637604013187; Mon, 22 Nov 2021 10:00:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJwE+R2KCT+YF3CF1oKwZCQ2g5v2JcpKZhKVTefiZU3jtEn/FhfX1Qzw62bxgObun+VmCUub2A== X-Received: by 2002:a05:600c:4154:: with SMTP id h20mr30885368wmm.189.1637604012823; Mon, 22 Nov 2021 10:00:12 -0800 (PST) Received: from localhost (host86-166-129-255.range86-166.btcentralplus.com. [86.166.129.255]) by smtp.gmail.com with ESMTPSA id o1sm9574639wrn.63.2021.11.22.10.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Nov 2021 10:00:12 -0800 (PST) Date: Mon, 22 Nov 2021 18:00:11 +0000 From: Andrew Burgess To: Bruno Larsen Cc: gdb-patches@sourceware.org Subject: Re: [PATCH v2] PR gdb/28480: Improve ambiguous member detection Message-ID: <20211122180011.GG2514@redhat.com> References: <20211108182722.29510-1-blarsen@redhat.com> MIME-Version: 1.0 In-Reply-To: <20211108182722.29510-1-blarsen@redhat.com> X-Operating-System: Linux/5.8.18-100.fc31.x86_64 (x86_64) X-Uptime: 16:46:56 up 3 days, 5:45, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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: Mon, 22 Nov 2021 18:00:18 -0000 * Bruno Larsen via Gdb-patches [2021-11-08 15:27:22 -0300]: > 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: You've use "boffset" a few times in this description. I think it would be clearer to just say "offset", or to expand to (I guess) "byte offset". The 'boffset' comes from the code, and, ideally, I shouldn't need to be familiar with variable names to understand the commit message. > > 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. Thanks for working on this. I had some formatting feedback, and also I had some questions on some of the comments, more details below. > --- > gdb/testsuite/gdb.cp/ambiguous.cc | 19 +++++++++++++++++++ > gdb/testsuite/gdb.cp/ambiguous.exp | 10 ++++++++++ > gdb/valops.c | 27 +++++++++++++++++++++++++++ > 3 files changed, 56 insertions(+) > > diff --git a/gdb/testsuite/gdb.cp/ambiguous.cc b/gdb/testsuite/gdb.cp/ambiguous.cc > index a55686547f2..af2198dcfbc 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,17 @@ public: > int y; > }; > > +#if !defined (__GNUC__) || __GNUC__ > 7 > +# define NO_UNIQUE_ADDRESS [[no_unique_address]] > +#else > +# define NO_UNIQUE_ADDRESS > +#endif > + > +class A4 { > +public: > + NO_UNIQUE_ADDRESS empty x; > +}; > + > class X : public A1, public A2 { > public: > int z; > @@ -77,6 +89,10 @@ public: > int jva1v; > }; > > +class JE : public A1, public A4 { > +public: > +}; > + > int main() > { > A1 a1; > @@ -92,6 +108,7 @@ int main() > JVA1 jva1; > JVA2 jva2; > JVA1V jva1v; > + JE je; > > int i; > > @@ -173,5 +190,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..a2a7b02b113 100644 > --- a/gdb/testsuite/gdb.cp/ambiguous.exp > +++ b/gdb/testsuite/gdb.cp/ambiguous.exp > @@ -264,3 +264,13 @@ 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}" > + > +# C++20 introduced a way to have ambiguous fields with the same boffset. Same request for the use of "boffset" here. > +# This class explicitly tests for that. > +# if this is tested with a compiler that can't handle [[no_unique_address]] > +# the code should still correctly identify the ambiguity because of > +# different boffsets. > +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..2989a93df1a 100644 > --- a/gdb/valops.c > +++ b/gdb/valops.c > @@ -1962,6 +1962,33 @@ 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 currently (2021) impossible. */ This comment should be moved inside the "{ ... }" block. I found this comment difficult to understand. When you say "...when the types are different", I guess this is referring to the path check below maybe? In which case I wonder if we can find a different way to phrase this, rather than "different types" ... "paths to the two fields are different" maybe? Additional the whole final sentence just leaves me confused, it seems to hint that there is a situation not covered by this code "This is not necessarily complete...", but also that there is no such situation "... is currently impossible". I wonder if you are saying that should we ever have two fields of the same name, in the same class, that occur at the same address, then this code wouldn't cover that case? But that seems a pretty weird thing to worry about, so I assume I'm not understand you correctly. Could you rephrase the last part please? > + { The indentation of this is wrong, it should be indented with two tabs. > + bool ambiguous = false, insert = true; > + for (const found_field& field: m_fields) { I think the GDB style for this would be: for (const found_field &field : m_fields) the '{' should be on the next line, and indented from the 'for'. > + if(field.path.back () != m_struct_path.back ()) > + { New blocks get two additional spaces for indentation, which will mean reindenting all this if/else code. > + /* 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}); > + } Single statement if blocks don't get '{ ... }' around them. > + } > } > } > } > -- > 2.27.0 > Thanks, Andrew