commit 5463b750d970f7b7a68acafd296fd1afbabdd6ca
parent 92c21e9863cf2eade09ee9256f7ece8fd4da21e6
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Mon, 17 Nov 2025 10:24:59 +0100
Implemented mhd_ASSUME() compiler helper
Diffstat:
2 files changed, 113 insertions(+), 0 deletions(-)
diff --git a/configure.ac b/configure.ac
@@ -1514,6 +1514,43 @@ AS_IF([test "x${mhd_cv_cc_kwd_unreachable}" != "xnone"],
)
]
)
+
+AC_CACHE_CHECK([for 'assume' hint keywords supported by $CC],[mhd_cv_cc_kwd_assume],
+ [
+ CFLAGS="${CFLAGS_ac} ${user_CFLAGS} ${errattr_CFLAGS}"
+ mhd_cv_cc_kwd_assume="none"
+ for keyword_chk in '__attribute__((assume(statement)))' '__builtin_assume(statement)' '[[assume(statement)]]' '__assume(statement)'
+ do
+ AC_LINK_IFELSE([
+ AC_LANG_SOURCE([[
+
+#define MACRO_ASSUME(statement) ${keyword_chk}
+
+static int zero_or_n(int cnd, int val)
+{
+ MACRO_ASSUME(cnd >= 0);
+ MACRO_ASSUME(cnd <= 1);
+ return cnd * val;
+}
+
+int main(int argc, char *const *argv)
+{
+ (void) argv;
+ return zero_or_n(0 > argc, 2);
+}
+ ]]
+ )
+ ],
+ [mhd_cv_cc_kwd_assume="$keyword_chk"]
+ )
+ test "x${mhd_cv_cc_kwd_assume}" != "xnone" && break
+ done
+ CFLAGS="${CFLAGS_ac} ${user_CFLAGS}"
+ ]
+)
+AS_IF([test "x${mhd_cv_cc_kwd_assume}" != "xnone"],
+ [AC_DEFINE_UNQUOTED([MHD_ASSUME_KEYWORD(statement)],[$mhd_cv_cc_kwd_assume],[Define to keyword supported to indicate a hard promise])]
+)
ac_c_werror_flag="$SAVE_ac_c_werror_flag"
AS_UNSET([SAVE_ac_c_werror_flag])
diff --git a/src/mhd2/mhd_assume.h b/src/mhd2/mhd_assume.h
@@ -0,0 +1,76 @@
+/* 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) 2025 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_assume.h
+ * @brief The definition of the mhd_ASSUME() macro
+ * @author Karlson2k (Evgeny Grin)
+ */
+
+#ifndef MHD_ASSUME_H
+#define MHD_ASSUME_H 1
+
+#include "mhd_sys_options.h"
+
+#if ! defined(NDEBUG)
+# include "mhd_assert.h"
+#elif ! defined(MHD_ASSUME_KEYWORD) && defined(MHD_UNREACHABLE_KEYWORD)
+# include "mhd_unreachable.h"
+#endif
+
+/**
+ * mhd_UNREACHABLE() should be used in locations where it is known in advance
+ * that the code must be not reachable.
+ * It should give compiler a hint to exclude some code paths from the final
+ * binary.
+ */
+#ifdef NDEBUG
+# ifdef MHD_ASSUME_KEYWORD
+# define mhd_ASSUME(statement) MHD_ASSUME_KEYWORD ((statement))
+# elif defined(MHD_UNREACHABLE_KEYWORD)
+# define mhd_ASSUME(statement) \
+ do { if (! (statement)) mhd_UNREACHABLE (); } while (0)
+# else
+# define mhd_ASSUME(statement) ((void) 0)
+# endif
+#else
+# define mhd_ASSUME(statement) \
+ mhd_assert ((statement) && "This statement must be always 'true'")
+#endif
+
+#endif /* ! MHD_ASSUME_H */