On Mon, Sep 28, 2020 at 03:05:55PM -0400, Jason Merrill via Gcc-patches wrote:
> On 9/28/20 12:30 PM, Marek Polacek wrote:
> > On Sat, Sep 26, 2020 at 01:22:41AM -0400, Jason Merrill wrote:
> > > > +bool
> > > > +ref_conv_binds_directly_p (tree type, tree expr)
> > > > +{
> > > > +  gcc_assert (TYPE_REF_P (type));
> > > > +  conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
> > > > +                                         /*c_cast_p=*/false,
> > > > +                                         LOOKUP_IMPLICIT, tf_none);
> > > > +  return conv && !conv_binds_ref_to_prvalue (conv);
> > > 
> > > You probably want to free any allocated conversions, like in
> > > can_convert_arg.
> > 
> > I ought to free them, indeed.  Apologies for missing that.  Fixed:
> > 
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > -- >8 --
> > This new warning can be used to prevent expensive copies inside range-based
> > for-loops, for instance:
> > 
> >    struct S { char arr[128]; };
> >    void fn () {
> >      S arr[5];
> >      for (const auto x : arr) {  }
> >    }
> > 
> > where auto deduces to S and then we copy the big S in every iteration.
> > Using "const auto &x" would not incur such a copy.  With this patch the
> > compiler will warn:
> > 
> > q.C:4:19: warning: loop variable 'x' creates a copy from type 'const S' 
> > [-Wrange-loop-construct]
> >      4 |   for (const auto x : arr) {  }
> >        |                   ^
> > q.C:4:19: note: use reference type 'const S&' to prevent copying
> >      4 |   for (const auto x : arr) {  }
> 
> It's unfortunate that we seem to suggest the unnecessary change from auto to
> S here.  Maybe just say "reference type" without printing the type?

Yeah, I wish there was a way to avoid it.  But I don't think we have
a TREE_TYPE bit that says that a type was deduced from auto/decltype(auto).

I'll just avoid printing the type...

> > +  auto_diagnostic_group d;
> > +  if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wrange_loop_construct,
> 
> Why not use 'loc' here?

Fixed.

> OK however you want to resolve these comments.

Pushed, thanks.

Marek

Reply via email to