Skip to content

Commit e8f2b9a

Browse files
committed
Common: uses _Countof in ARRAY_SIZE definition when available
1 parent 0686d1d commit e8f2b9a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,15 @@ add_library(libfastfetch OBJECT
13431343
${LIBFASTFETCH_SRC}
13441344
)
13451345

1346+
include(CheckCSourceCompiles)
1347+
check_c_source_compiles("int main(void){int arr[1];return _Countof(arr);}" COMPILER_SUPPORTS_COUNT_OF)
1348+
if(COMPILER_SUPPORTS_COUNT_OF)
1349+
message(STATUS "_Countof is supported by the compiler")
1350+
target_compile_definitions(libfastfetch PUBLIC FF_SUPPORTS_COUNT_OF=1)
1351+
else()
1352+
message(STATUS "_Countof is NOT supported by the compiler")
1353+
endif()
1354+
13461355
if(yyjson_FOUND)
13471356
target_compile_definitions(libfastfetch PUBLIC FF_USE_SYSTEM_YYJSON=1)
13481357
target_link_libraries(libfastfetch PUBLIC yyjson::yyjson)

src/common/arrayUtils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <assert.h>
44

55
#ifdef __has_builtin
6-
#if __has_builtin(__is_array)
6+
#if !__cplusplus && FF_SUPPORTS_COUNT_OF
7+
#define ARRAY_SIZE(x) _Countof(x)
8+
#elif __has_builtin(__is_array)
79
#define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); })
810
#elif __has_builtin(__builtin_types_compatible_p)
911
#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))); })

0 commit comments

Comments
 (0)