[PATCH] libcamera: base: Don't redefine __nodiscard

Laurent Pinchart laurent.pinchart at ideasonboard.com
Fri Jan 3 15:53:18 CET 2025


Hi Mattijs,

Thank you for the patch.

On Fri, Jan 03, 2025 at 03:02:50PM +0100, Mattijs Korpershoek wrote:
> Some libc implementations (like bionic) already define the __nodiscard
> macro [1].
> 
> Building with bionic results in compiler error since libcamera redefines
> the __nodiscard macro.
> 
> Don't redefine __nodiscard if it's already defined.

bionic defines it as __attribute__((__warn_unused_result__)) which
(hopefully) does the exact same thing as [[nodiscard]]. Someone else
could define it differently though. I suppose the risk is low ?

> [1] https://android-review.googlesource.com/c/platform/bionic/+/3254860
> 
> Signed-off-by: Mattijs Korpershoek <mkorpershoek at baylibre.com>
> ---
> Hi, it's been a while. I've found a (trivial) build issue when
> building against recent bionic versions.
> 
> I hope this is the right fix. Thanks for your review!

It's been nearly four years since we introduced __nodiscard. I wonder if
we could require applications to use C++17 or newer now, and use
[[nodiscard]] directly.

> ---
>  include/libcamera/base/compiler.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/libcamera/base/compiler.h b/include/libcamera/base/compiler.h
> index fda8fdfdc543f86c5554e38ef790c00d72d60389..946e20d835b216446e4099b6ab6865638d9aaf26 100644
> --- a/include/libcamera/base/compiler.h
> +++ b/include/libcamera/base/compiler.h
> @@ -7,8 +7,10 @@
>  
>  #pragma once
>  
> +#ifndef __nodiscard
>  #if __cplusplus >= 201703L
>  #define __nodiscard		[[nodiscard]]
>  #else
>  #define __nodiscard
>  #endif
> +#endif
> 
> ---
> base-commit: 35ed4b91291d9f3d08e4b51acfb51163e65df8f8
> change-id: 20250103-nodiscard-redef-9158e8fdc3f5

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list