Skip to content

Commit

Permalink
Require C23 and test __STDC_VERSION__ >= 202000L
Browse files Browse the repository at this point in the history
Fix #2352.
  • Loading branch information
wantehchang committed Aug 1, 2024
1 parent a4654aa commit 0c616d2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -585,6 +589,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()
Expand Down
9 changes: 8 additions & 1 deletion include/avif/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ 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.
//
// If the -std=c2x or -std=gnu2x option is specified, __STDC_VERSION__ is
// * 202000L in GCC 13.2.0, Clang 16.0.6, and Apple Clang 15.0.0, or
// * 202311L in Clang 19.0.0git.
// If the /std:clatest option is specified, __STDC_VERSION__ is
// * 202312L in Microsoft Visual Studio 17.10.5.
#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.
Expand Down

0 comments on commit 0c616d2

Please sign in to comment.