c++: tweak for -Wrange-loop-construct [PR116731]

Message ID 20240918183517.517366-1-polacek@redhat.com
State Committed
Commit 6ac4e2f4b2ca9980670e7d3815a9140730df1005
Headers
Series c++: tweak for -Wrange-loop-construct [PR116731] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed

Commit Message

Marek Polacek Sept. 18, 2024, 6:35 p.m. UTC
  Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
This PR reports that the warning would be better off using a check
for trivially constructible rather than trivially copyable.

LLVM accepted a similar fix:
https://github.com/llvm/llvm-project/issues/47355

	PR c++/116731

gcc/cp/ChangeLog:

	* parser.cc (warn_for_range_copy): Check if TYPE is trivially
	constructible, not copyable.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wrange-loop-construct3.C: New test.
---
 gcc/cp/parser.cc                              |  8 ++-
 .../g++.dg/warn/Wrange-loop-construct3.C      | 57 +++++++++++++++++++
 2 files changed, 62 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C


base-commit: cc62b2c3da118f08f71d2ae9c08bafb55b35767a
  

Comments

Jason Merrill Sept. 26, 2024, 3:34 p.m. UTC | #1
On 9/18/24 2:35 PM, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> -- >8 --
> This PR reports that the warning would be better off using a check
> for trivially constructible rather than trivially copyable.
> 
> LLVM accepted a similar fix:
> https://github.com/llvm/llvm-project/issues/47355
> 
> 	PR c++/116731
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (warn_for_range_copy): Check if TYPE is trivially
> 	constructible, not copyable.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Wrange-loop-construct3.C: New test.
> ---
>   gcc/cp/parser.cc                              |  8 ++-
>   .../g++.dg/warn/Wrange-loop-construct3.C      | 57 +++++++++++++++++++
>   2 files changed, 62 insertions(+), 3 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index 4dd9474cf60..140457ba224 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -14394,11 +14394,13 @@ warn_for_range_copy (tree decl, tree expr)
>     else if (!CP_TYPE_CONST_P (type))
>       return;
>   
> -  /* Since small trivially copyable types are cheap to copy, we suppress the
> -     warning for them.  64B is a common size of a cache line.  */
> +  /* Since small trivially constructible types are cheap to construct, we
> +     suppress the warning for them.  64B is a common size of a cache line.  */
> +  tree vec = make_tree_vec (1);
> +  TREE_VEC_ELT (vec, 0) = TREE_TYPE (expr);
>     if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
>         || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
> -	  && trivially_copyable_p (type)))
> +	  && is_trivially_xible (INIT_EXPR, type, vec)))
>       return;
>   
>     /* If we can initialize a reference directly, suggest that to avoid the
> diff --git a/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C b/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C
> new file mode 100644
> index 00000000000..3d9d0c9088e
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C
> @@ -0,0 +1,57 @@
> +// PR c++/116731
> +// { dg-do compile { target c++11 } }
> +// { dg-options "-Wrange-loop-construct" }
> +
> +void
> +f0 ()
> +{
> +  struct S {
> +    char a[64];
> +    S& operator=(const S&) { return *this; };
> +  };
> +
> +  S arr[8];
> +  for (const auto r : arr)
> +    (void) r;
> +}
> +
> +void
> +f1 ()
> +{
> +  struct S {
> +    char a[65];
> +    S& operator=(const S&) { return *this; };
> +  };
> +
> +  S arr[8];
> +  for (const auto r : arr) // { dg-warning "creates a copy" }
> +    (void) r;
> +}
> +
> +void
> +f2 ()
> +{
> +  struct S {
> +    char a[64];
> +    S& operator=(const S&) { return *this; };
> +    ~S() { }
> +  };
> +
> +  S arr[8];
> +  for (const auto r : arr) // { dg-warning "creates a copy" }
> +    (void) r;
> +}
> +
> +void
> +f3 ()
> +{
> +  struct S {
> +    char a[65];
> +    S& operator=(const S&) { return *this; };
> +    ~S() { }
> +  };
> +
> +  S arr[8];
> +  for (const auto r : arr) // { dg-warning "creates a copy" }
> +    (void) r;
> +}
> 
> base-commit: cc62b2c3da118f08f71d2ae9c08bafb55b35767a
  

Patch

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 4dd9474cf60..140457ba224 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -14394,11 +14394,13 @@  warn_for_range_copy (tree decl, tree expr)
   else if (!CP_TYPE_CONST_P (type))
     return;
 
-  /* Since small trivially copyable types are cheap to copy, we suppress the
-     warning for them.  64B is a common size of a cache line.  */
+  /* Since small trivially constructible types are cheap to construct, we
+     suppress the warning for them.  64B is a common size of a cache line.  */
+  tree vec = make_tree_vec (1);
+  TREE_VEC_ELT (vec, 0) = TREE_TYPE (expr);
   if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
       || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
-	  && trivially_copyable_p (type)))
+	  && is_trivially_xible (INIT_EXPR, type, vec)))
     return;
 
   /* If we can initialize a reference directly, suggest that to avoid the
diff --git a/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C b/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C
new file mode 100644
index 00000000000..3d9d0c9088e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C
@@ -0,0 +1,57 @@ 
+// PR c++/116731
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wrange-loop-construct" }
+
+void
+f0 ()
+{
+  struct S {
+    char a[64];
+    S& operator=(const S&) { return *this; };
+  };
+
+  S arr[8];
+  for (const auto r : arr)
+    (void) r;
+}
+
+void
+f1 ()
+{
+  struct S {
+    char a[65];
+    S& operator=(const S&) { return *this; };
+  };
+
+  S arr[8];
+  for (const auto r : arr) // { dg-warning "creates a copy" }
+    (void) r;
+}
+
+void
+f2 ()
+{
+  struct S {
+    char a[64];
+    S& operator=(const S&) { return *this; };
+    ~S() { }
+  };
+
+  S arr[8];
+  for (const auto r : arr) // { dg-warning "creates a copy" }
+    (void) r;
+}
+
+void
+f3 ()
+{
+  struct S {
+    char a[65];
+    S& operator=(const S&) { return *this; };
+    ~S() { }
+  };
+
+  S arr[8];
+  for (const auto r : arr) // { dg-warning "creates a copy" }
+    (void) r;
+}