From ce3e9136a80c6d34a8bbfb512b4072128cc34792 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 17 Jun 2023 16:04:13 +0100 Subject: [PATCH] adding compile time assertion support whenever possible and convert some runtime which deserve to be evaluated earlier. --- src/client/refresh/gl3/gl3_main.c | 2 +- src/client/refresh/gl3/gl3_mesh.c | 2 +- src/common/header/shared.h | 5 +++++ src/common/shared/shared.c | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/client/refresh/gl3/gl3_main.c b/src/client/refresh/gl3/gl3_main.c index 65ed4864..7c119e1c 100644 --- a/src/client/refresh/gl3/gl3_main.c +++ b/src/client/refresh/gl3/gl3_main.c @@ -1011,7 +1011,7 @@ GL3_DrawParticles(void) GLfloat dist; GLfloat color[4]; } part_vtx; - assert(sizeof(part_vtx)==9*sizeof(float)); // remember to update GL3_SurfInit() if this changes! + YQ2_STATIC_ASSERT(sizeof(part_vtx)==9*sizeof(float), "invalid part_vtx size"); // remember to update GL3_SurfInit() if this changes! // Don't try to draw particles if there aren't any. if (numParticles == 0) diff --git a/src/client/refresh/gl3/gl3_mesh.c b/src/client/refresh/gl3/gl3_mesh.c index 0d2a371e..e9e73f85 100644 --- a/src/client/refresh/gl3/gl3_mesh.c +++ b/src/client/refresh/gl3/gl3_mesh.c @@ -189,7 +189,7 @@ DrawAliasFrameLerp(dmdl_t *paliashdr, entity_t* entity, vec3_t shadelight) LerpVerts(colorOnly, paliashdr->num_xyz, v, ov, verts, lerp, move, frontv, backv); - assert(sizeof(gl3_alias_vtx_t) == 9*sizeof(GLfloat)); + YQ2_STATIC_ASSERT(sizeof(gl3_alias_vtx_t) == 9*sizeof(GLfloat), "invalid gl3_alias_vtx_t size"); // all the triangle fans and triangle strips of this model will be converted to // just triangles: the vertices stay the same and are batched in vtxBuf, diff --git a/src/common/header/shared.h b/src/common/header/shared.h index a66f7872..4edb171b 100644 --- a/src/common/header/shared.h +++ b/src/common/header/shared.h @@ -59,6 +59,7 @@ typedef unsigned char byte; #define YQ2_ALIGNAS_TYPE(TYPE) _Alignas(TYPE) // must be used as prefix (YQ2_ATTR_NORETURN void bla();)! #define YQ2_ATTR_NORETURN _Noreturn + #define YQ2_STATIC_ASSERT(C, M) _Static_assert((C), M) #if defined(__GNUC__) #define YQ2_ATTR_MALLOC __attribute__ ((__malloc__)) #define YQ2_ATTR_INLINE __attribute__((always_inline)) inline @@ -77,6 +78,8 @@ typedef unsigned char byte; #define YQ2_ATTR_NORETURN __attribute__ ((noreturn)) #define YQ2_ATTR_MALLOC __attribute__ ((__malloc__)) #define YQ2_ATTR_INLINE __attribute__((always_inline)) inline + // GCC supports this extension since 4.6 + #define YQ2_STATIC_ASSERT(C, M) _Static_assert((C), M) #elif defined(_MSC_VER) // Note: We prefer VS2019 16.8 or newer in C11 mode (/std:c11), // then the __STDC_VERSION__ >= 201112L case above is used @@ -95,6 +98,7 @@ typedef unsigned char byte; #define YQ2_ATTR_NORETURN __declspec(noreturn) #define YQ2_ATTR_MALLOC __declspec(restrict) #define YQ2_ATTR_INLINE __forceinline + #define YQ2_STATIC_ASSERT(C, M) assert((C) && M) #else #warning "Please add a case for your compiler here to align correctly" #define YQ2_ALIGNAS_SIZE(SIZE) @@ -102,6 +106,7 @@ typedef unsigned char byte; #define YQ2_ATTR_NORETURN #define YQ2_ATTR_MALLOC #define YQ2_ATTR_INLINE inline + #define YQ2_STATIC_ASSERT(C, M) assert((C) && M) #endif #if defined(__GNUC__) diff --git a/src/common/shared/shared.c b/src/common/shared/shared.c index b60aae23..0b02a6e6 100644 --- a/src/common/shared/shared.c +++ b/src/common/shared/shared.c @@ -868,7 +868,7 @@ Swap_Init(void) { byte swaptest[2] = {1, 0}; short swapTestShort; - assert(sizeof(short) == 2); + YQ2_STATIC_ASSERT(sizeof(short) == 2, "invalid short size"); memcpy(&swapTestShort, swaptest, 2); /* set the byte swapping variables in a portable manner */