commit 1d6f9c1662e0b7fefdcd64d2c5a560147b65a8f4
parent 94a3ed7c6df8fb853f3ba7e485039645b7d0147d
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Tue, 25 Aug 2026 13:36:51 +0200
Implemented mhd_STATIC_ASSERT_STMT() macro
Diffstat:
3 files changed, 116 insertions(+), 0 deletions(-)
diff --git a/configure.ac b/configure.ac
@@ -1863,6 +1863,53 @@ AS_IF([test "x${mhd_cv_cc_kwd_assume}" != "xnone"],
)
ac_c_werror_flag=""
+AC_CACHE_CHECK([for 'static_assert' keywords supported by $CC],[mhd_cv_cc_kwn_static_assert],
+ [
+ mhd_cv_cc_kwn_static_assert="none"
+ for keyword_chk in ['static_assert(expr,msg)' '_Static_assert(expr,msg)' '__static_assert(expr,msg)' \
+ '__extension__ _Static_assert(expr,msg)' \
+ 'static_assert(expr)' '_Static_assert(expr)' '__static_assert(expr)']
+ do
+ ac_c_werror_flag="yes"
+ AC_COMPILE_IFELSE([
+ AC_LANG_SOURCE(
+ [[
+#define MHD_ST_ASRT(expr,msg) ${keyword_chk}
+
+void test_func(void);
+
+void test_func(void)
+{ MHD_ST_ASRT(!0, "Must not fail"); }
+ ]]
+ )
+ ],
+ [
+ ac_c_werror_flag=""
+ AC_COMPILE_IFELSE([
+ AC_LANG_SOURCE(
+ [[
+#define MHD_ST_ASRT(expr,msg) ${keyword_chk}
+
+void test_func(void);
+
+void test_func(void)
+{ MHD_ST_ASRT(!!0, "Must fail"); }
+ ]]
+ )
+ ],
+ [],[mhd_cv_cc_kwn_static_assert="${keyword_chk}"]
+ )
+ ]
+ )
+ test "x${mhd_cv_cc_kwn_static_assert}" != "xnone" && break
+ done
+ ac_c_werror_flag=""
+ ]
+)
+AS_IF([test "x${mhd_cv_cc_kwn_static_assert}" != "xnone"],
+ [AC_DEFINE_UNQUOTED([MHD_ST_ASSERT_KEYWORD(expr,msg)],[${mhd_cv_cc_kwn_static_assert}],[Define to keyword supported to check a condition at compile time])]
+)
+
# Check for 'fallthrough' keywords
save_CFLAGS_ac="${CFLAGS_ac}"
CFLAGS="${user_CFLAGS}"
diff --git a/src/mhd2/Makefile.am b/src/mhd2/Makefile.am
@@ -36,6 +36,7 @@ libmicrohttpd2_la_SOURCES = \
sys_w32_ver.h \
mhd_align.h mhd_bithelpers.h mhd_byteorder.h \
mhd_assert.h mhd_assume.h mhd_unreachable.h \
+ mhd_static_assert.h \
mhd_constexpr.h mhd_predict.h \
mhd_cntnr_ptr.h mhd_arr_num_elems.h \
mhd_tristate.h mhd_status_code_int.h \
diff --git a/src/mhd2/mhd_static_assert.h b/src/mhd2/mhd_static_assert.h
@@ -0,0 +1,68 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
+/*
+ This file is part of GNU libmicrohttpd.
+ Copyright (C) 2026 Evgeny Grin (Karlson2k)
+
+ GNU libmicrohttpd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ GNU libmicrohttpd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ Alternatively, you can redistribute GNU libmicrohttpd and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version, together
+ with the eCos exception, as follows:
+
+ As a special exception, if other files instantiate templates or
+ use macros or inline functions from this file, or you compile this
+ file and link it with other works to produce a work based on this
+ file, this file does not by itself cause the resulting work to be
+ covered by the GNU General Public License. However the source code
+ for this file must still be made available in accordance with
+ section (3) of the GNU General Public License v2.
+
+ This exception does not invalidate any other reasons why a work
+ based on this file might be covered by the GNU General Public
+ License.
+
+ You should have received copies of the GNU Lesser General Public
+ License and the GNU General Public License along with this library;
+ if not, see <https://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file src/mhd2/mhd_static_assert.h
+ * @brief The definition of the mhd_STATIC_ASSERT_STMT() macro
+ * @author Karlson2k (Evgeny Grin)
+ */
+
+#ifndef MHD_STATIC_ASSERT_H
+#define MHD_STATIC_ASSERT_H 1
+
+#include "mhd_sys_options.h"
+
+
+/**
+ * Fail the compilation if @a check is 'false'.
+ * This macro is a statement, it can be used inside functions only.
+ * @param check the integer constant expression to check
+ * @param msg the string literal describing the failed check
+ */
+#ifdef MHD_ST_ASSERT_KEYWORD
+# define mhd_STATIC_ASSERT_STMT(check, msg) \
+ do { MHD_ST_ASSERT_KEYWORD ((check), msg); } while (0)
+#else /* ! MHD_ST_ASSERT_KEYWORD */
+# define mhd_STATIC_ASSERT_STMT(check, msg) \
+ do { switch (0) \
+ { case 0: break; \
+ case !!(check): (void)(msg); break; \
+ default: break; } } while (0)
+#endif /* ! MHD_ST_ASSERT_KEYWORD */
+
+#endif /* ! MHD_STATIC_ASSERT_H */