Skip to content

Commit

Permalink
improved static_assert compat
Browse files Browse the repository at this point in the history
Use static_assert instead of _Static_assert, which is deprecated in c23.

- compat using static_assert macro defined in assert.h since c11 (until 23)
- dismiss for C prior to C11, but just for MSVS <=2015 (otherwise assume
it as an error, eg. missing /std with MSVC)
  • Loading branch information
MartinPulec committed Jan 30, 2025
1 parent e7206f0 commit cfd2091
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ BraceWrapping:
ColumnLimit: 120
IndentWidth: 4
PointerAlignment: Left
IndentPPDirectives: BeforeHash
ReflowComments: true
SpacesInParens: Custom
SpacesInParensOptions:
Expand Down
12 changes: 11 additions & 1 deletion src/gpujpeg_common_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,20 @@
#include "../libgpujpeg/gpujpeg_type.h"
#include "gpujpeg_util.h"

// static_assert compat
#if __STDC_VERSION__ < 202311L
#include <assert.h> // static_assert compat macro in C11-C17
#elif __STDC_VERSION__ < 201112L
#if defined _MSC_VER && _MSC_VER <= 1900
#define static_assert(cond, msg)
#else
#error "compiler is not supporting C11 - perhaps an error?"
#endif
#endif

// VS 2015 compat
#if defined _MSC_VER && _MSC_VER <= 1900
#define __func__ ""
#define _Static_assert(cond, msg)
#endif // VS <=2015

/** Contants */
Expand Down
2 changes: 1 addition & 1 deletion src/gpujpeg_huffman_cpu_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ gpujpeg_huffman_cpu_decoder_value_from_category(int category, int offset)
#pragma GCC diagnostic ignored "-Wshift-negative-value"
#pragma GCC diagnostic ignored "-Wpedantic"
#endif // defined __GNUC_
_Static_assert((-1)<<1 == -2, "Implementation defined behavior doesn't work as assumed.");
static_assert((-1)<<1 == -2, "Implementation defined behavior doesn't work as assumed.");
//start[i] is the starting value in this category; surely it is below zero
// entry n is (-1 << n) + 1
static const int start[16] = {
Expand Down
4 changes: 2 additions & 2 deletions src/gpujpeg_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ static const char *array_serialize(int comp_count, const uint8_t *comp_id) {
* checks component ID to determine if JPEG is in YCbCr (component IDs 1, 2, 3) or RGB (component IDs 'R', 'G', 'B')
*/
static enum gpujpeg_color_space gpujpeg_reader_process_cid(int comp_count, uint8_t *comp_id, enum gpujpeg_color_space header_color_space) {
_Static_assert(GPUJPEG_MAX_COMPONENT_COUNT >= 3, "An array of at least 3 components expected");
static_assert(GPUJPEG_MAX_COMPONENT_COUNT >= 3, "An array of at least 3 components expected");
static const uint8_t ycbcr_ids[] = { 1, 2, 3 };
static const uint8_t rgb_ids[] = { 'R', 'G', 'B' };
static const uint8_t bg_rgb_ids[] = { 'r', 'g', 'b' }; // big gamut sRGB (see ILG libjpeg - seemingly handled as above)
Expand Down Expand Up @@ -1468,7 +1468,7 @@ adjust_pixel_format(struct gpujpeg_parameters * param, struct gpujpeg_image_para
static void
adjust_format(struct gpujpeg_parameters* param, struct gpujpeg_image_parameters* param_image)
{
_Static_assert(GPUJPEG_PIXFMT_AUTODETECT < 0, "enum gpujpeg_pixel_format type should be signed");
static_assert(GPUJPEG_PIXFMT_AUTODETECT < 0, "enum gpujpeg_pixel_format type should be signed");
if ( param_image->color_space == GPUJPEG_CS_DEFAULT ) {
if ( param_image->pixel_format == GPUJPEG_U8 ||
(param_image->pixel_format <= GPUJPEG_PIXFMT_AUTODETECT && param->comp_count == 1) ) {
Expand Down
32 changes: 16 additions & 16 deletions src/gpujpeg_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,14 @@ gpujpeg_table_huffman_encoder_init(struct gpujpeg_table_huffman_encoder* table,
{
assert(comp_type == GPUJPEG_COMPONENT_LUMINANCE || comp_type == GPUJPEG_COMPONENT_CHROMINANCE);
assert(huff_type == GPUJPEG_HUFFMAN_DC || huff_type == GPUJPEG_HUFFMAN_AC);
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_dc_bits), "table buffer too small");
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_dc_value), "table buffer too small");
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_ac_bits), "table buffer too small");
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_ac_value), "table buffer too small");
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_dc_bits), "table buffer too small");
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_dc_value), "table buffer too small");
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_ac_bits), "table buffer too small");
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_ac_value), "table buffer too small");
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_dc_bits), "table buffer too small");
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_dc_value), "table buffer too small");
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_ac_bits), "table buffer too small");
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_ac_value), "table buffer too small");
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_dc_bits), "table buffer too small");
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_dc_value), "table buffer too small");
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_ac_bits), "table buffer too small");
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_ac_value), "table buffer too small");

if ( comp_type == GPUJPEG_COMPONENT_LUMINANCE ) {
if ( huff_type == GPUJPEG_HUFFMAN_DC ) {
Expand Down Expand Up @@ -348,14 +348,14 @@ gpujpeg_table_huffman_decoder_init(struct gpujpeg_table_huffman_decoder* table,
{
assert(comp_type == GPUJPEG_COMPONENT_LUMINANCE || comp_type == GPUJPEG_COMPONENT_CHROMINANCE);
assert(huff_type == GPUJPEG_HUFFMAN_DC || huff_type == GPUJPEG_HUFFMAN_AC);
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_dc_bits), "table buffer too small");
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_dc_value), "table buffer too small");
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_ac_bits), "table buffer too small");
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_ac_value), "table buffer too small");
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_dc_bits), "table buffer too small");
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_dc_value), "table buffer too small");
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_ac_bits), "table buffer too small");
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_ac_value), "table buffer too small");
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_dc_bits), "table buffer too small");
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_dc_value), "table buffer too small");
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_ac_bits), "table buffer too small");
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_ac_value), "table buffer too small");
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_dc_bits), "table buffer too small");
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_dc_value), "table buffer too small");
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_ac_bits), "table buffer too small");
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_ac_value), "table buffer too small");

if ( comp_type == GPUJPEG_COMPONENT_LUMINANCE ) {
if ( huff_type == GPUJPEG_HUFFMAN_DC ) {
Expand Down

0 comments on commit cfd2091

Please sign in to comment.