From 1f770d97b4338846dd75006bbcf229c1952780f6 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 10 Apr 2023 19:03:53 +0300 Subject: [PATCH 39/39] Add fc__unreachable() See osdn #47835 Signed-off-by: Marko Lindqvist --- configure.ac | 1 + gen_headers/meson_freeciv_config.h.in | 3 +++ m4/compiler.m4 | 11 +++++++++++ meson.build | 7 +++++++ utility/support.h | 9 +++++++++ 5 files changed, 31 insertions(+) diff --git a/configure.ac b/configure.ac index 70e6810c10..c971e39b42 100644 --- a/configure.ac +++ b/configure.ac @@ -804,6 +804,7 @@ FC_CXX20_CAPTURE_THIS _CFLAGS="$CFLAGS" CFLAGS="$CFLAGS $WERROR_C_TEST_FLAGS" FC_SIZE_T_FORMAT +FC_BUILTIN_UNREACHABLE CFLAGS="$_CFLAGS" dnl BeOS-specific settings diff --git a/gen_headers/meson_freeciv_config.h.in b/gen_headers/meson_freeciv_config.h.in index 7e231d7737..466ff08827 100644 --- a/gen_headers/meson_freeciv_config.h.in +++ b/gen_headers/meson_freeciv_config.h.in @@ -39,6 +39,9 @@ /* C++20 capture this supported */ #mesondefine FREECIV_HAVE_CXX20_CAPTURE_THIS +/* __builtin_unreachable() available */ +#mesondefine FREECIV_HAVE_UNREACHABLE + /* Use pthreads as thread implementation */ #mesondefine FREECIV_HAVE_PTHREAD diff --git a/m4/compiler.m4 b/m4/compiler.m4 index bc813977d9..873c79beda 100644 --- a/m4/compiler.m4 +++ b/m4/compiler.m4 @@ -167,3 +167,14 @@ AC_DEFUN([FC_SIZE_T_FORMAT], AC_MSG_RESULT([$SIZE_T_PRINTF]) ]) + +# Check for __builtin_unreachable() +AC_DEFUN([FC_BUILTIN_UNREACHABLE], +[ + AC_MSG_CHECKING([__builtin_unreachable()]) + + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], +[[__builtin_unreachable();]])],[ + AC_DEFINE([FREECIV_HAVE_UNREACHABLE], [1], [__builtin_unreachable() available]) + AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])]) +]) diff --git a/meson.build b/meson.build index 59d0129d83..d58036002e 100644 --- a/meson.build +++ b/meson.build @@ -633,6 +633,13 @@ int main(void) { size_t var = 0; fr("''' + format + '''", var); return 0; }''', endif endforeach +if c_compiler.compiles('''int main(void) { __builtin_unreachable(); return 0; }''', + include_directories: include_directories(cross_inc_path), + name: '__builtin_unreachable()', + args: ['-Werror', '-Wall']) + pub_conf_data.set('FREECIV_HAVE_UNREACHABLE', 1) +endif + if get_option('audio') or get_option('clients').contains('sdl2') if host_system == 'windows' sdl2main_dep = [c_compiler.find_library('mingw32', dirs: cross_lib_path), diff --git a/utility/support.h b/utility/support.h index b112ad93d8..49f2c5fe9c 100644 --- a/utility/support.h +++ b/utility/support.h @@ -123,6 +123,15 @@ extern "C" { #define fc__noreturn #endif +#ifdef FREECIV_HAVE_UNREACHABLE +#define fc__unreachable(_cond_) \ + if (_cond_) { \ + __builtin_unreachable(); \ + } +#else /* FREECIV_HAVE_UNREACHABLE */ +#define fc__unreachable(_cond_) fc_assert(!(_cond_)) +#endif /* FREECIV_HAVE_UNREACHABLE */ + #ifdef FREECIV_MSWINDOWS typedef long int fc_errno; #else -- 2.39.2