diff options
author | Marek Polacek <polacek@redhat.com> | 2020-11-09 21:15:33 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2020-11-21 16:20:16 -0500 |
commit | c51e31a06f2c740c55852a683aa7ffdc20417362 (patch) | |
tree | 8e9427d27fc773b8f550807538315fc9bc9e6160 /gcc/cp/parser.c | |
parent | 6f20c42cc162ac3725584547ab4933bae4c78665 (diff) | |
download | gcc-c51e31a06f2c740c55852a683aa7ffdc20417362.zip gcc-c51e31a06f2c740c55852a683aa7ffdc20417362.tar.gz gcc-c51e31a06f2c740c55852a683aa7ffdc20417362.tar.bz2 |
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.
Diffstat (limited to 'gcc/cp/parser.c')
-rw-r--r-- | gcc/cp/parser.c | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index cea7cb0..8802124 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -12679,8 +12679,15 @@ do_range_for_auto_deduction (tree decl, tree range_expr) for (const auto &x : range) - if this version doesn't make a copy. DECL is the RANGE_DECL; EXPR is the - *__for_begin expression. + if this version doesn't make a copy. + + This function also warns when the loop variable is initialized with + a value of a different type resulting in a copy: + + int arr[10]; + for (const double &x : arr) + + DECL is the RANGE_DECL; EXPR is the *__for_begin expression. This function is never called when processing_template_decl is on. */ static void @@ -12698,7 +12705,22 @@ warn_for_range_copy (tree decl, tree expr) if (TYPE_REF_P (type)) { - /* TODO: Implement reference warnings. */ + if (glvalue_p (expr) && !ref_conv_binds_directly_p (type, expr)) + { + auto_diagnostic_group d; + if (warning_at (loc, OPT_Wrange_loop_construct, + "loop variable %qD of type %qT binds to a temporary " + "constructed from type %qT", decl, type, + TREE_TYPE (expr))) + { + tree ref = cp_build_qualified_type (TREE_TYPE (expr), + TYPE_QUAL_CONST); + ref = cp_build_reference_type (ref, /*rval*/false); + inform (loc, "use non-reference type %qT to make the copy " + "explicit or %qT to prevent copying", + non_reference (type), ref); + } + } return; } else if (!CP_TYPE_CONST_P (type)) |