Skip to content

Commit cfd2091

Browse files
committed
improved static_assert compat
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)
1 parent e7206f0 commit cfd2091

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ BraceWrapping:
99
ColumnLimit: 120
1010
IndentWidth: 4
1111
PointerAlignment: Left
12+
IndentPPDirectives: BeforeHash
1213
ReflowComments: true
1314
SpacesInParens: Custom
1415
SpacesInParensOptions:

src/gpujpeg_common_internal.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,20 @@
4343
#include "../libgpujpeg/gpujpeg_type.h"
4444
#include "gpujpeg_util.h"
4545

46+
// static_assert compat
47+
#if __STDC_VERSION__ < 202311L
48+
#include <assert.h> // static_assert compat macro in C11-C17
49+
#elif __STDC_VERSION__ < 201112L
50+
#if defined _MSC_VER && _MSC_VER <= 1900
51+
#define static_assert(cond, msg)
52+
#else
53+
#error "compiler is not supporting C11 - perhaps an error?"
54+
#endif
55+
#endif
56+
4657
// VS 2015 compat
4758
#if defined _MSC_VER && _MSC_VER <= 1900
4859
#define __func__ ""
49-
#define _Static_assert(cond, msg)
5060
#endif // VS <=2015
5161

5262
/** Contants */

src/gpujpeg_huffman_cpu_decoder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ gpujpeg_huffman_cpu_decoder_value_from_category(int category, int offset)
187187
#pragma GCC diagnostic ignored "-Wshift-negative-value"
188188
#pragma GCC diagnostic ignored "-Wpedantic"
189189
#endif // defined __GNUC_
190-
_Static_assert((-1)<<1 == -2, "Implementation defined behavior doesn't work as assumed.");
190+
static_assert((-1)<<1 == -2, "Implementation defined behavior doesn't work as assumed.");
191191
//start[i] is the starting value in this category; surely it is below zero
192192
// entry n is (-1 << n) + 1
193193
static const int start[16] = {

src/gpujpeg_reader.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ static const char *array_serialize(int comp_count, const uint8_t *comp_id) {
695695
* checks component ID to determine if JPEG is in YCbCr (component IDs 1, 2, 3) or RGB (component IDs 'R', 'G', 'B')
696696
*/
697697
static enum gpujpeg_color_space gpujpeg_reader_process_cid(int comp_count, uint8_t *comp_id, enum gpujpeg_color_space header_color_space) {
698-
_Static_assert(GPUJPEG_MAX_COMPONENT_COUNT >= 3, "An array of at least 3 components expected");
698+
static_assert(GPUJPEG_MAX_COMPONENT_COUNT >= 3, "An array of at least 3 components expected");
699699
static const uint8_t ycbcr_ids[] = { 1, 2, 3 };
700700
static const uint8_t rgb_ids[] = { 'R', 'G', 'B' };
701701
static const uint8_t bg_rgb_ids[] = { 'r', 'g', 'b' }; // big gamut sRGB (see ILG libjpeg - seemingly handled as above)
@@ -1468,7 +1468,7 @@ adjust_pixel_format(struct gpujpeg_parameters * param, struct gpujpeg_image_para
14681468
static void
14691469
adjust_format(struct gpujpeg_parameters* param, struct gpujpeg_image_parameters* param_image)
14701470
{
1471-
_Static_assert(GPUJPEG_PIXFMT_AUTODETECT < 0, "enum gpujpeg_pixel_format type should be signed");
1471+
static_assert(GPUJPEG_PIXFMT_AUTODETECT < 0, "enum gpujpeg_pixel_format type should be signed");
14721472
if ( param_image->color_space == GPUJPEG_CS_DEFAULT ) {
14731473
if ( param_image->pixel_format == GPUJPEG_U8 ||
14741474
(param_image->pixel_format <= GPUJPEG_PIXFMT_AUTODETECT && param->comp_count == 1) ) {

src/gpujpeg_table.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,14 @@ gpujpeg_table_huffman_encoder_init(struct gpujpeg_table_huffman_encoder* table,
311311
{
312312
assert(comp_type == GPUJPEG_COMPONENT_LUMINANCE || comp_type == GPUJPEG_COMPONENT_CHROMINANCE);
313313
assert(huff_type == GPUJPEG_HUFFMAN_DC || huff_type == GPUJPEG_HUFFMAN_AC);
314-
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_dc_bits), "table buffer too small");
315-
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_dc_value), "table buffer too small");
316-
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_ac_bits), "table buffer too small");
317-
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_ac_value), "table buffer too small");
318-
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_dc_bits), "table buffer too small");
319-
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_dc_value), "table buffer too small");
320-
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_ac_bits), "table buffer too small");
321-
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_ac_value), "table buffer too small");
314+
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_dc_bits), "table buffer too small");
315+
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_dc_value), "table buffer too small");
316+
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_ac_bits), "table buffer too small");
317+
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_ac_value), "table buffer too small");
318+
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_dc_bits), "table buffer too small");
319+
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_dc_value), "table buffer too small");
320+
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_ac_bits), "table buffer too small");
321+
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_ac_value), "table buffer too small");
322322

323323
if ( comp_type == GPUJPEG_COMPONENT_LUMINANCE ) {
324324
if ( huff_type == GPUJPEG_HUFFMAN_DC ) {
@@ -348,14 +348,14 @@ gpujpeg_table_huffman_decoder_init(struct gpujpeg_table_huffman_decoder* table,
348348
{
349349
assert(comp_type == GPUJPEG_COMPONENT_LUMINANCE || comp_type == GPUJPEG_COMPONENT_CHROMINANCE);
350350
assert(huff_type == GPUJPEG_HUFFMAN_DC || huff_type == GPUJPEG_HUFFMAN_AC);
351-
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_dc_bits), "table buffer too small");
352-
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_dc_value), "table buffer too small");
353-
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_ac_bits), "table buffer too small");
354-
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_ac_value), "table buffer too small");
355-
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_dc_bits), "table buffer too small");
356-
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_dc_value), "table buffer too small");
357-
_Static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_ac_bits), "table buffer too small");
358-
_Static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_ac_value), "table buffer too small");
351+
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_dc_bits), "table buffer too small");
352+
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_dc_value), "table buffer too small");
353+
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_y_ac_bits), "table buffer too small");
354+
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_y_ac_value), "table buffer too small");
355+
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_dc_bits), "table buffer too small");
356+
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_dc_value), "table buffer too small");
357+
static_assert(sizeof(table->bits) >= sizeof(gpujpeg_table_huffman_cbcr_ac_bits), "table buffer too small");
358+
static_assert(sizeof(table->huffval) >= sizeof(gpujpeg_table_huffman_cbcr_ac_value), "table buffer too small");
359359

360360
if ( comp_type == GPUJPEG_COMPONENT_LUMINANCE ) {
361361
if ( huff_type == GPUJPEG_HUFFMAN_DC ) {

0 commit comments

Comments
 (0)