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 BBD083858D28 for ; Tue, 5 Sep 2023 20:52:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BBD083858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693947137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YzHFw9XzHt/yM8sbnBKG/zEDs6GZZ4d3pjxJPrj+yRc=; b=MBMz57Q/7IUTUj2gnzbWQ4Bldv0xjhr8Gmg5aS15AeGRJqEs8GfPoPMAuWsOF1AQaUKhR2 tNu6lZiGywGXoCNPz+cJh6NvxfNxPIykrJZPuAd4RPUuthji0ZlGkH5Ckr+s8GiFvXuizt uf/mui9Ul4mn7DWsx+F7MMxmHi4Jl4w= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-113-rGscxbZnPj-XHszqTjkNqg-1; Tue, 05 Sep 2023 16:52:15 -0400 X-MC-Unique: rGscxbZnPj-XHszqTjkNqg-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-414c43a48fdso26983971cf.3 for ; Tue, 05 Sep 2023 13:52:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693947135; x=1694551935; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YzHFw9XzHt/yM8sbnBKG/zEDs6GZZ4d3pjxJPrj+yRc=; b=lsXVSzAR9iNoWg9XLi7PPBPqAP8BYe/opfjhCWWTM/UlYa+paiKNLhUyG9llfdn2so Uyf/Ja7HQL+t42xYpcc9nl/dhC1vQCc4IqTHsTQK0wBaY+XPGgXkVV49EdhEnFYYN1xq tpZlWjjmgnyiL/CrACZR8j8O58XIWra1mP/e3Pjq91bjKBeibvkQsNwqJlRXi337FftB Wrn+n4mSX0CQsdF2O5etnZuehqzid42XvS7MnuZyA2JkTP/qjv2UPKP6OF+5tkdrbYdj hJ1KU0toAJlCmS63ZUTc3r9L3AzDlQKOFqSnvqWtCotXEt+xsu2NbLoyn9zJlNxbhEu+ m/9w== X-Gm-Message-State: AOJu0YyUY8wVeprDsE2PfCFESufbHbixpCpfhuxaypyxeqNld6bv+FrJ 8/n03UERgtmF/stez3/+XoUfN22eFe3znFMQLvqC5AAN1inXZr+0X8gqvTZmZdM/+j5IMGFxT08 UqPI1zpMJNSMLvyk5QA== X-Received: by 2002:ac8:59ca:0:b0:3fd:ecdc:1e0f with SMTP id f10-20020ac859ca000000b003fdecdc1e0fmr20843609qtf.17.1693947135422; Tue, 05 Sep 2023 13:52:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEPSv1K2TM9AqYMQFdpz4SKLlwErZCaPgqtPBYhiXS+xxoj7psQpMuhY0IZf8AtLbYrttbX3g== X-Received: by 2002:ac8:59ca:0:b0:3fd:ecdc:1e0f with SMTP id f10-20020ac859ca000000b003fdecdc1e0fmr20843592qtf.17.1693947135113; Tue, 05 Sep 2023 13:52:15 -0700 (PDT) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id p15-20020ac8740f000000b003fde3d63d22sm4628082qtq.69.2023.09.05.13.52.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 Sep 2023 13:52:14 -0700 (PDT) Message-ID: <1a0f7dcf-3aa5-57f1-da86-0adf34b1d50b@redhat.com> Date: Tue, 5 Sep 2023 16:52:13 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: [PATCH] c++: Additional warning for name-hiding [PR12341] To: priour.be@gmail.com, gcc-patches@gcc.gnu.org Cc: benjamin priour References: <20230904171858.2660517-1-vultkayn@gcc.gnu.org> From: Jason Merrill In-Reply-To: <20230904171858.2660517-1-vultkayn@gcc.gnu.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 9/4/23 13:18, priour.be@gmail.com wrote: > From: benjamin priour > > Hi, > > This patch was the first I wrote and had been > at that time returned to me because ill-formatted. > > Getting busy with other things, I forgot about it. > I've now fixed the formatting. > > Succesfully regstrapped on x86_64-linux-gnu off trunk > a7d052b3200c7928d903a0242b8cfd75d131e374. > Is it OK for trunk ? > > Thanks, > Benjamin. > > Patch below. > --- > > Add a new warning for name-hiding. When a class's field > is named similarly to one inherited, a warning should > be issued. > This new warning is controlled by the existing Wshadow. > > gcc/cp/ChangeLog: > > PR c++/12341 > * search.cc (lookup_member): > New optional parameter to preempt processing the > inheritance tree deeper than necessary. > (lookup_field): Likewise. > (dfs_walk_all): Likewise. > * cp-tree.h: Update the above declarations. > * class.cc: (warn_name_hiding): New function. > (finish_struct_1): Call warn_name_hiding if -Wshadow. > > gcc/testsuite/ChangeLog: > > PR c++/12341 > * g++.dg/pr12341-1.C: New file. > * g++.dg/pr12341-2.C: New file. > > Signed-off-by: benjamin priour > --- > gcc/cp/class.cc | 75 ++++++++++++++++++++++++++++++++ > gcc/cp/cp-tree.h | 9 ++-- > gcc/cp/search.cc | 28 ++++++++---- > gcc/testsuite/g++.dg/pr12341-1.C | 65 +++++++++++++++++++++++++++ > gcc/testsuite/g++.dg/pr12341-2.C | 34 +++++++++++++++ > 5 files changed, 200 insertions(+), 11 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/pr12341-1.C > create mode 100644 gcc/testsuite/g++.dg/pr12341-2.C > > diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc > index 778759237dc..b1c59c392a0 100644 > --- a/gcc/cp/class.cc > +++ b/gcc/cp/class.cc > @@ -3080,6 +3080,79 @@ warn_hidden (tree t) > } > } > > +/* Warn about non-static fields name hiding. */ > + > +static void > +warn_name_hiding (tree t) > +{ > + if (is_empty_class (t) || CLASSTYPE_NEARLY_EMPTY_P (t)) > + return; > + > + for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) > + { > + /* Skip if field is not an user-defined non-static data member. */ > + if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) > + continue; > + > + unsigned j; > + tree name = DECL_NAME (field); > + /* Skip if field is anonymous. */ > + if (!name || !identifier_p (name)) > + continue; > + > + auto_vec base_vardecls; > + tree binfo; > + tree base_binfo; > + /* Iterate through all of the base classes looking for possibly > + shadowed non-static data members. */ > + for (binfo = TYPE_BINFO (t), j = 0; > + BINFO_BASE_ITERATE (binfo, j, base_binfo); j++) Rather than iterate through the bases here, maybe add a mode to lookup_member/lookup_field_r that skips the most derived type, e.g. by adding that as a flag in lookup_field_info? Probably instead of the once_suffices stuff? > + if (base_vardecl) > + { > + auto_diagnostic_group d; > + if (warning_at (location_of (field), OPT_Wshadow, > + "%qD might shadow %qD", field, base_vardecl)) Why "might"? We can give a correct answer, we shouldn't settle for an approximation. Jason