From fe526a5e195dd5f4ca8f0779ef0dd678b7c41f3e Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Thu, 1 Aug 2024 08:15:20 -0700 Subject: [PATCH] Require C23 and test __STDC_VERSION__ >= 202000L Fix issue #2352. --- CMakeLists.txt | 9 +++++++-- include/avif/avif.h | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 61ba84173e..aa2e7de575 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,13 +33,17 @@ include(FetchContent) include(FindPkgConfig) include(AvifExternalProjectUtils) -option(AVIF_ENABLE_NODISCARD "Add [[nodiscard]] to some functions. CMake must be at least 3.21 to request C23." OFF) +option(AVIF_ENABLE_NODISCARD "Add [[nodiscard]] to some functions. CMake must be at least 3.21 to force C23." OFF) # Set C99 as the default -if(AVIF_ENABLE_NODISCARD AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.21.0) +if(AVIF_ENABLE_NODISCARD) # [[nodiscard]] requires C23. The supported value 23 for CMAKE_C_STANDARD # was added in CMake version 3.21. + if(CMAKE_VERSION VERSION_LESS 3.21.0) + message(FATAL_ERROR "CMake must be at least 3.21 to force C23, bailing out") + endif() set(CMAKE_C_STANDARD 23) + set(CMAKE_C_STANDARD_REQUIRED ON) else() set(CMAKE_C_STANDARD 99) endif() @@ -584,6 +588,7 @@ if(AVIF_LIB_USE_CXX OR (AVIF_BUILD_APPS AND AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP) O if(AVIF_ENABLE_NODISCARD) # [[nodiscard]] requires C++17. set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_STANDARD_REQUIRED ON) else() set(CMAKE_CXX_STANDARD 14) endif() diff --git a/include/avif/avif.h b/include/avif/avif.h index 91b719d138..fe5dbd67d0 100644 --- a/include/avif/avif.h +++ b/include/avif/avif.h @@ -48,7 +48,9 @@ extern "C" { #define AVIF_API #endif // defined(AVIF_DLL) -#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) +// [[nodiscard]] requires C++17 and C23. The -std=c2x and -std=gnu2x options of +// recent versions of GCC and Clang define __STDC_VERSION__ as 202000L. +#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L) #define AVIF_NODISCARD [[nodiscard]] #else // Starting with 3.9, clang allows defining the warn_unused_result attribute for enums.