[libcamera-devel] meson: Ignore -Wredundant-move with gcc-13 and newer
diff mbox series

Message ID 20230501025711.25157-1-laurent.pinchart@ideasonboard.com
State Accepted
Commit af7d6a4c2df6c836ecc704ea44b39ea07418d832
Headers show
Series
  • [libcamera-devel] meson: Ignore -Wredundant-move with gcc-13 and newer
Related show

Commit Message

Laurent Pinchart May 1, 2023, 2:57 a.m. UTC
Starting from 13.1, gcc implements the C++23 version of automatic move
from local variables in return statements (see
https://en.cppreference.com/w/cpp/language/return). As a result, some
previously required explicit `std::move()` in return statements generate
warnings. This is the case when a function returns an object whose type
is a class derived from the class type the function returns:

struct U { };
struct T : U { };

U f()
{
	T t;
	return t;
}

Up to C++20, the automatic move from local variables selects the move
constructor of class U, which is not the move constructor of the
expression. Overload resolution is then performed a second time, with t
considered as an lvalue. An explicit `std::move(t)` is needed in the
return statement to select the U move constructor.

Starting from C++23, `t` is treated as an xvalue, and the U move
constructor is selected without the need for an explicit `std::move(t)`.
An explicit `std:move()` then generates a redundant-move warning, as in
the valueOrTuple() function in src/py/libcamera/py_helpers.cpp.

Omitting the `std::move()` silences the warning, but selects the copy
constructor of U with older gcc versions and with clang, which
negatively impacts performance.

The easiest fix is to disable the warning. With -Wpessimizing-move
enabled, the compiler will still warn of pessimizing moves, only the
redundant but not pessimizing moves will be ignored.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 meson.build | 15 +++++++++++++++
 1 file changed, 15 insertions(+)


base-commit: 701b201cf387055e5c0a1c9549e2dbbf4988f092

Comments

Kieran Bingham May 1, 2023, 10:16 a.m. UTC | #1
Quoting Laurent Pinchart via libcamera-devel (2023-05-01 03:57:11)
> Starting from 13.1, gcc implements the C++23 version of automatic move
> from local variables in return statements (see
> https://en.cppreference.com/w/cpp/language/return). As a result, some
> previously required explicit `std::move()` in return statements generate
> warnings. This is the case when a function returns an object whose type
> is a class derived from the class type the function returns:
> 
> struct U { };
> struct T : U { };
> 
> U f()
> {
>         T t;
>         return t;
> }
> 
> Up to C++20, the automatic move from local variables selects the move
> constructor of class U, which is not the move constructor of the
> expression. Overload resolution is then performed a second time, with t
> considered as an lvalue. An explicit `std::move(t)` is needed in the
> return statement to select the U move constructor.
> 
> Starting from C++23, `t` is treated as an xvalue, and the U move
> constructor is selected without the need for an explicit `std::move(t)`.
> An explicit `std:move()` then generates a redundant-move warning, as in
> the valueOrTuple() function in src/py/libcamera/py_helpers.cpp.
> 
> Omitting the `std::move()` silences the warning, but selects the copy
> constructor of U with older gcc versions and with clang, which
> negatively impacts performance.
> 
> The easiest fix is to disable the warning. With -Wpessimizing-move
> enabled, the compiler will still warn of pessimizing moves, only the
> redundant but not pessimizing moves will be ignored.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Looks good to me. I'll collect this for v0.0.5 and test with the other
GCC-13 fix.


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  meson.build | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/meson.build b/meson.build
> index 8628e6acebee..6e363a906c69 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -136,6 +136,21 @@ if cc.get_id() == 'gcc'
>          ]
>      endif
>  
> +    # gcc 13 implements the C++23 version of automatic move from local
> +    # variables in return statements (see
> +    # https://en.cppreference.com/w/cpp/language/return). As a result, some
> +    # previously required explicit std::move() in return statements generate
> +    # warnings. Those moves can't be removed as older compiler versions could
> +    # use copy constructors instead of move constructors. The easiest fix is to
> +    # disable the warning. With -Wpessimizing-move enabled, the compiler will
> +    # still warn of pessimizing moves, only the redundant but not pessimizing
> +    # moves will be ignored.
> +    if cc.version().version_compare('>=13')
> +        cpp_arguments += [
> +            '-Wno-redundant-move',
> +        ]
> +    endif
> +
>      # gcc 7.1 introduced processor-specific ABI breakages related to parameter
>      # passing on ARM platforms. This generates a large number of messages
>      # during compilation. Silence them.
> 
> base-commit: 701b201cf387055e5c0a1c9549e2dbbf4988f092
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/meson.build b/meson.build
index 8628e6acebee..6e363a906c69 100644
--- a/meson.build
+++ b/meson.build
@@ -136,6 +136,21 @@  if cc.get_id() == 'gcc'
         ]
     endif
 
+    # gcc 13 implements the C++23 version of automatic move from local
+    # variables in return statements (see
+    # https://en.cppreference.com/w/cpp/language/return). As a result, some
+    # previously required explicit std::move() in return statements generate
+    # warnings. Those moves can't be removed as older compiler versions could
+    # use copy constructors instead of move constructors. The easiest fix is to
+    # disable the warning. With -Wpessimizing-move enabled, the compiler will
+    # still warn of pessimizing moves, only the redundant but not pessimizing
+    # moves will be ignored.
+    if cc.version().version_compare('>=13')
+        cpp_arguments += [
+            '-Wno-redundant-move',
+        ]
+    endif
+
     # gcc 7.1 introduced processor-specific ABI breakages related to parameter
     # passing on ARM platforms. This generates a large number of messages
     # during compilation. Silence them.