From fc41f7917531693680b5baf71ffe38c47333fe84 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 6 Apr 2026 15:55:33 +0200 Subject: Make the project build with Microsoft's broken C compiler. MSVC is not fully C11-compliant, even when compiling with /std:c11. Some changes were needed to make the codebase compatible. Notably, the notation a[static N] and a[n] for function parameters of array type is not supported, so that had to be hidden behind a macro. Atomic types are also an experimental feature, apparently, but at least they work with the correct compiler flag. One thing that MSVC does well, however, is warning on integer conversions on /W4 level. I am not sure if Clang and GCC have something similar, so I took this chance to fix some of these. --- src/utils/compilers.h | 17 ++++++++++++++--- src/utils/dbg_log.h | 3 +-- src/utils/math.h | 6 +++--- src/utils/wrapthread.h | 4 ++-- 4 files changed, 20 insertions(+), 10 deletions(-) (limited to 'src/utils') diff --git a/src/utils/compilers.h b/src/utils/compilers.h index a7d6df5..1b175bb 100644 --- a/src/utils/compilers.h +++ b/src/utils/compilers.h @@ -1,13 +1,24 @@ #if defined(__GNUC__) -#define unused __attribute__((unused)) +#define UNUSED __attribute__((unused)) +#define SIZE(x) static (x) +#define NON_NULL SIZE(1) #elif defined(__clang__) -#define unused __attribute__((unused)) +#define UNUSED __attribute__((unused)) +#define SIZE(x) static (x) +#define NON_NULL SIZE(1) #else -#define unused +/* +For example MSVC, which is not fully C11 compliant (e.g. it does not support +a[static N] notation for array parameters). +*/ + +#define UNUSED +#define SIZE(x) +#define NON_NULL #endif diff --git a/src/utils/dbg_log.h b/src/utils/dbg_log.h index d737ff2..08d42a3 100644 --- a/src/utils/dbg_log.h +++ b/src/utils/dbg_log.h @@ -7,8 +7,7 @@ void write_wrapper(void (*)(const char *, void *), const char *, ...); void write_wrapper(void (*write)(const char *, void *), const char *str, ...) { - static const size_t len = 1000; - char message[len]; + char message[1000]; va_list args; va_start(args, str); diff --git a/src/utils/math.h b/src/utils/math.h index f2176b6..0c56861 100644 --- a/src/utils/math.h +++ b/src/utils/math.h @@ -73,10 +73,10 @@ indextoperm(uint64_t p, size_t n, uint8_t *r) /* Find k-th unused number */ for (j = 0, c = 0; c <= k; j++) - c += 1 - ((used & (1<> j); + c += UINT64_C(1) - ((used & (UINT64_C(1)<> j); - r[i] = j-1; - used |= 1 << (j-1); + r[i] = (uint8_t)(j-1); + used |= UINT64_C(1) << (j-1); p %= factorial[n-i-1]; } diff --git a/src/utils/wrapthread.h b/src/utils/wrapthread.h index 4ac72a6..9578293 100644 --- a/src/utils/wrapthread.h +++ b/src/utils/wrapthread.h @@ -61,8 +61,8 @@ #define wrapthread_return_t int #define wrapthread_return_val 0 - #define wrapthread_define_var_thread_t(x) unused char x - #define wrapthread_define_var_mutex_t(x) unused char x + #define wrapthread_define_var_thread_t(x) UNUSED char x + #define wrapthread_define_var_mutex_t(x) UNUSED char x #define wrapthread_define_struct_thread_t(x) char x #define wrapthread_define_struct_mutex_t(x) char x -- cgit v1.3