This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] C++: fix-it hints suggesting accessors for private fields
- From: David Malcolm <dmalcolm at redhat dot com>
- To: Nathan Sidwell <nathan at acm dot org>, gcc-patches at gcc dot gnu dot org
- Date: Tue, 25 Apr 2017 11:58:47 -0400
- Subject: Re: [PATCH] C++: fix-it hints suggesting accessors for private fields
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dmalcolm at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A552580515
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A552580515
- References: <1493064385-51828-1-git-send-email-dmalcolm@redhat.com> <14a3b4be-6812-09c2-98a8-f812b164fc83@acm.org> <f8f1f6a7-1171-5d94-4857-18531a89c6ac@acm.org>
On Tue, 2017-04-25 at 07:49 -0400, Nathan Sidwell wrote:
> On 04/25/2017 07:46 AM, Nathan Sidwell wrote:
> > On 04/24/2017 04:06 PM, David Malcolm wrote:
> >
> > > test.cc:12:13: note: field ‘int foo::m_field’ can be accessed via
> > > ‘int
> > > foo::get_field() const’
> > > return f->m_field;
> > > ^~~~~~~
> > > get_field()
> > >
> > > Assuming that an IDE can offer to apply fix-it hints, this should
> > > make it easier to handle refactorings where one makes a field
> > > private and adds a getter.
> > >
> > > It also helps by letting the user know that a getter exists, and
> > > the name of the getter ("is it "field", "get_field", etc?").
> >
> > Neat!
> >
> > >
> > > OK for trunk?
> > >
> > > gcc/cp/ChangeLog:
> > > * call.c (maybe_suggest_accessor): New function.
> > > (enforce_access): Call maybe_suggest_accessor for
> > > inaccessible
> > > decls.
> > > * cp-tree.h (locate_field_accessor): New decl.
> > > * search.c (matches_code_and_type_p): New function.
> > > (field_access_p): New function.
> > > (direct_accessor_p): New function.
> > > (reference_accessor_p): New function.
> > > (field_accessor_p): New function.
> > > (dfs_locate_field_accessor_pre): New function.
> > > (locate_field_accessor): New function.
> >
> > ok.
>
> Oh, what if the field is being accessed for modification or
> lvalueness?
> Must the accessor return T, or can it return 'T cv &'? I.e. does it
> need to look for setters too?
There's an example of doing so in the patch in:
g++.dg/other/accessor-fixits-2.C
(see direct_accessor_p vs reference_accessor_p in the patch for how
this is handled).
The patch doesn't take account the context of how the returned field is
used, e.g. whether as an rvalue vs in a modification/lvalue way; it
just looks for a method of the form
{ return FIELD; }
for the correct field, favoring returning T to returning T&.
Is there a way to get at the "style" of access from
call.c:enforce_access? (to determine if T vs T& would be a better
suggestion)
Otherwise, is the patch OK as-is?
A case the patch doesn't provide a hint for yet is for static data; e.g. for:
return foo::s_singleton;
(as in g++.dg/other/accessor-fixits-3.C in the patch); would that be OK
to leave as a follow-up?
Thanks
Dave