public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94695] New: Implement -Wrange-loop-analysis
@ 2020-04-21 14:37 mpolacek at gcc dot gnu.org
  2020-04-21 14:37 ` [Bug c++/94695] " mpolacek at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-04-21 14:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94695

            Bug ID: 94695
           Summary: Implement -Wrange-loop-analysis
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mpolacek at gcc dot gnu.org
  Target Milestone: ---

This options contains a sub-option -Wrange-loop-construct which warns about:

#include <string>
#include <vector>

int main()
{
    std::vector<std::string> lol;
    for (const auto it : lol) { }
}

<source>:7:21: warning: loop variable 'it' creates a copy from type 'const
std::__cxx11::basic_string<char>' [-Wrange-loop-construct]

    for (const auto it : lol) { }

                    ^

<source>:7:10: note: use reference type 'const std::__cxx11::basic_string<char>
&' to prevent copying

    for (const auto it : lol) { }

         ^~~~~~~~~~~~~~~

                    &

1 warning generated.

We should consider adding something like that, too, in GCC 11.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/94695] Implement -Wrange-loop-analysis
  2020-04-21 14:37 [Bug c++/94695] New: Implement -Wrange-loop-analysis mpolacek at gcc dot gnu.org
@ 2020-04-21 14:37 ` mpolacek at gcc dot gnu.org
  2020-09-22 16:28 ` mpolacek at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-04-21 14:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94695

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
           Severity|normal                      |enhancement

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/94695] Implement -Wrange-loop-analysis
  2020-04-21 14:37 [Bug c++/94695] New: Implement -Wrange-loop-analysis mpolacek at gcc dot gnu.org
  2020-04-21 14:37 ` [Bug c++/94695] " mpolacek at gcc dot gnu.org
@ 2020-09-22 16:28 ` mpolacek at gcc dot gnu.org
  2020-09-29 23:03 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-09-22 16:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94695

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-09-22
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/94695] Implement -Wrange-loop-analysis
  2020-04-21 14:37 [Bug c++/94695] New: Implement -Wrange-loop-analysis mpolacek at gcc dot gnu.org
  2020-04-21 14:37 ` [Bug c++/94695] " mpolacek at gcc dot gnu.org
  2020-09-22 16:28 ` mpolacek at gcc dot gnu.org
@ 2020-09-29 23:03 ` cvs-commit at gcc dot gnu.org
  2020-09-29 23:05 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-29 23:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94695

--- Comment #1 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:969baf03acd8124345617cea125b148568c7370a

commit r11-3539-g969baf03acd8124345617cea125b148568c7370a
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Sep 24 14:30:50 2020 -0400

    c++: Implement -Wrange-loop-construct [PR94695]

    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) {  }
          |                   ^
          |                   &

    As per Clang, this warning is suppressed for trivially copyable types
    whose size does not exceed 64B.  The tricky part of the patch was how
    to figure out if using a reference would have prevented a copy.  To
    that point, I'm using the new function called ref_conv_binds_directly_p.

    This warning is enabled by -Wall.  Further warnings of similar nature
    should follow soon.

    gcc/c-family/ChangeLog:

            PR c++/94695
            * c.opt (Wrange-loop-construct): New option.

    gcc/cp/ChangeLog:

            PR c++/94695
            * call.c (ref_conv_binds_directly_p): New function.
            * cp-tree.h (ref_conv_binds_directly_p): Declare.
            * parser.c (warn_for_range_copy): New function.
            (cp_convert_range_for): Call it.

    gcc/ChangeLog:

            PR c++/94695
            * doc/invoke.texi: Document -Wrange-loop-construct.

    gcc/testsuite/ChangeLog:

            PR c++/94695
            * g++.dg/warn/Wrange-loop-construct.C: New test.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/94695] Implement -Wrange-loop-analysis
  2020-04-21 14:37 [Bug c++/94695] New: Implement -Wrange-loop-analysis mpolacek at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2020-09-29 23:03 ` cvs-commit at gcc dot gnu.org
@ 2020-09-29 23:05 ` mpolacek at gcc dot gnu.org
  2020-11-21 21:28 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-09-29 23:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94695

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
First part of the warning is now implemented.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/94695] Implement -Wrange-loop-analysis
  2020-04-21 14:37 [Bug c++/94695] New: Implement -Wrange-loop-analysis mpolacek at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2020-09-29 23:05 ` mpolacek at gcc dot gnu.org
@ 2020-11-21 21:28 ` cvs-commit at gcc dot gnu.org
  2020-11-21 21:30 ` mpolacek at gcc dot gnu.org
  2021-05-10 10:03 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-11-21 21:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94695

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:c51e31a06f2c740c55852a683aa7ffdc20417362

commit r11-5234-gc51e31a06f2c740c55852a683aa7ffdc20417362
Author: Marek Polacek <polacek@redhat.com>
Date:   Mon Nov 9 21:15:33 2020 -0500

    c++: Extend -Wrange-loop-construct for binding-to-temp [PR94695]

    This patch finishes the second half of -Wrange-loop-construct I promised
    to implement: it warns when a loop variable in a range-based for-loop is
    initialized with a value of a different type resulting in a copy.  For
    instance:

      int arr[10];
      for (const double &x : arr) { ... }

    where in every iteration we have to create and destroy a temporary value
    of type double, to which we bind the reference.  This could negatively
    impact performance.

    As per Clang, this doesn't warn when the range returns a copy, hence the
    glvalue_p check.

    gcc/ChangeLog:

            PR c++/94695
            * doc/invoke.texi: Update the -Wrange-loop-construct description.

    gcc/cp/ChangeLog:

            PR c++/94695
            * parser.c (warn_for_range_copy): Warn when the loop variable is
            initialized with a value of a different type resulting in a copy.

    gcc/testsuite/ChangeLog:

            PR c++/94695
            * g++.dg/warn/Wrange-loop-construct2.C: New test.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/94695] Implement -Wrange-loop-analysis
  2020-04-21 14:37 [Bug c++/94695] New: Implement -Wrange-loop-analysis mpolacek at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2020-11-21 21:28 ` cvs-commit at gcc dot gnu.org
@ 2020-11-21 21:30 ` mpolacek at gcc dot gnu.org
  2021-05-10 10:03 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-11-21 21:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94695

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Implemented in GCC 11.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/94695] Implement -Wrange-loop-analysis
  2020-04-21 14:37 [Bug c++/94695] New: Implement -Wrange-loop-analysis mpolacek at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2020-11-21 21:30 ` mpolacek at gcc dot gnu.org
@ 2021-05-10 10:03 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-10 10:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94695

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |antoshkka at gmail dot com

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
*** Bug 80542 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-05-10 10:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-21 14:37 [Bug c++/94695] New: Implement -Wrange-loop-analysis mpolacek at gcc dot gnu.org
2020-04-21 14:37 ` [Bug c++/94695] " mpolacek at gcc dot gnu.org
2020-09-22 16:28 ` mpolacek at gcc dot gnu.org
2020-09-29 23:03 ` cvs-commit at gcc dot gnu.org
2020-09-29 23:05 ` mpolacek at gcc dot gnu.org
2020-11-21 21:28 ` cvs-commit at gcc dot gnu.org
2020-11-21 21:30 ` mpolacek at gcc dot gnu.org
2021-05-10 10:03 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).