From 6cc285a9d07d53099ce2e318466a56bb6b1e0606 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Cristian=20Rodr=C3=ADguez?= Date: Mon, 24 Nov 2025 17:04:08 -0300 Subject: [PATCH] include: implement ARRAY_SIZE with compiler _Countof if supported C2Y has _Countof operator for this. GCC has an stdcountof.h hedaer and a countof definition Clang implements _Countof and needs __has_extension check --- include/c.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/c.h b/include/c.h index f7cd08fd8..0126591f2 100644 --- a/include/c.h +++ b/include/c.h @@ -133,6 +133,10 @@ C23 : https://en.cppreference.com/w/c/language/attributes/fallthrough #define __has_feature(x) 0 #endif +#ifndef __has_extension + #define __has_extension __has_feature +#endif + /* * Function attributes */ @@ -173,6 +177,15 @@ C23 : https://en.cppreference.com/w/c/language/attributes/fallthrough #define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); })) #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) +#if __has_include() +#include +#define ARRAY_SIZE(arr) countof(arr) +#endif + +#if !defined(ARRAY_SIZE) && __has_extension(c_countof) +#define ARRAY_SIZE(arr) _Countof(arr) +#endif + #ifndef ARRAY_SIZE # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) #endif -- 2.47.3