From e8f2b9a1c7d7b152e5fd85e40421fc6460a6f86d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 2 Feb 2026 11:08:08 +0800 Subject: [PATCH] Common: uses `_Countof` in ARRAY_SIZE definition when available --- CMakeLists.txt | 9 +++++++++ src/common/arrayUtils.h | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3357fef61..28778aacb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1343,6 +1343,15 @@ add_library(libfastfetch OBJECT ${LIBFASTFETCH_SRC} ) +include(CheckCSourceCompiles) +check_c_source_compiles("int main(void){int arr[1];return _Countof(arr);}" COMPILER_SUPPORTS_COUNT_OF) +if(COMPILER_SUPPORTS_COUNT_OF) + message(STATUS "_Countof is supported by the compiler") + target_compile_definitions(libfastfetch PUBLIC FF_SUPPORTS_COUNT_OF=1) +else() + message(STATUS "_Countof is NOT supported by the compiler") +endif() + if(yyjson_FOUND) target_compile_definitions(libfastfetch PUBLIC FF_USE_SYSTEM_YYJSON=1) target_link_libraries(libfastfetch PUBLIC yyjson::yyjson) diff --git a/src/common/arrayUtils.h b/src/common/arrayUtils.h index ad251ba1e..83a49d6aa 100644 --- a/src/common/arrayUtils.h +++ b/src/common/arrayUtils.h @@ -3,7 +3,9 @@ #include #ifdef __has_builtin - #if __has_builtin(__is_array) + #if !__cplusplus && FF_SUPPORTS_COUNT_OF + #define ARRAY_SIZE(x) _Countof(x) + #elif __has_builtin(__is_array) #define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) #elif __has_builtin(__builtin_types_compatible_p) #define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); (uint32_t) (sizeof(x) / sizeof(*(x))); })