Răsfoiți Sursa

stdinc: use _Countof in SDL_arraysize

_Countof is a compiler intrinsics that errors when using a pointer argument
Anonymous Maarten 1 săptămână în urmă
părinte
comite
f0b89704e9
2 a modificat fișierele cu 36 adăugiri și 1 ștergeri
  1. 24 0
      include/SDL3/SDL_begin_code.h
  2. 12 1
      include/SDL3/SDL_stdinc.h

+ 24 - 0
include/SDL3/SDL_begin_code.h

@@ -281,6 +281,22 @@
  */
 #define SDL_HAS_BUILTIN(x) __has_builtin(x)
 
+/**
+ * Check if the compiler supports a given extension.
+ *
+ * This allows preprocessor checks for things that otherwise might fail to
+ * compile.
+ *
+ * Supported by virtually all clang versions and more-recent GCCs. Use this
+ * instead of checking the clang version if possible.
+ *
+ * On compilers without has_extension support, this is defined to 0 (always
+ * false).
+ *
+ * \since This macro is available since SDL 3.2.0.
+ */
+#define SDL_HAS_EXTENSION(x) __has_extension(x)
+
 /**
  * A macro to specify data alignment.
  *
@@ -344,6 +360,14 @@
 #endif
 #endif
 
+#ifndef SDL_HAS_EXTENSION
+#ifdef __has_extension
+#define SDL_HAS_EXTENSION(x) __has_extension(x)
+#else
+#define SDL_HAS_EXTENSION(x) 0
+#endif
+#endif
+
 #ifndef SDL_DEPRECATED
 #  if defined(__GNUC__) && (__GNUC__ >= 4)  /* technically, this arrived in gcc 3.1, but oh well. */
 #    define SDL_DEPRECATED __attribute__((deprecated))

+ 12 - 1
include/SDL3/SDL_stdinc.h

@@ -52,6 +52,8 @@
 #include <string.h>
 #include <wchar.h>
 
+#include <SDL3/SDL_begin_code.h>
+
 /* Most everything except Visual Studio 2008 and earlier has stdint.h now */
 #if defined(_MSC_VER) && (_MSC_VER < 1600)
 typedef signed __int8 int8_t;
@@ -237,6 +239,8 @@ void *alloca(size_t);
        typedef int SDL_compile_time_assert_ ## name[(x) * 2 - 1]
 #endif
 
+#ifdef SDL_WIKI_DOCUMENTATION_SECTION
+
 /**
  * The number of elements in a static array.
  *
@@ -249,7 +253,15 @@ void *alloca(size_t);
  *
  * \since This macro is available since SDL 3.2.0.
  */
+#define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))  /* or `_Countof(array)` on recent gcc and clang */
+
+#else
+#if (defined(__GNUC__) && __GNUC__ >= 16) || SDL_HAS_EXTENSION(c_countof)
+#define SDL_arraysize(array) _Countof(array)
+#else
 #define SDL_arraysize(array) (sizeof(array)/sizeof(array[0]))
+#endif
+#endif
 
 /**
  * Macro useful for building other macros with strings in them.
@@ -1209,7 +1221,6 @@ SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int));
 #endif /* DOXYGEN_SHOULD_IGNORE_THIS */
 /** \endcond */
 
-#include <SDL3/SDL_begin_code.h>
 /* Set up for C function definitions, even when using C++ */
 #ifdef __cplusplus
 extern "C" {