From 7c1ba29177e14f314aa0675f9366113e514226ae Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 9 Dec 2021 03:49:59 +0200 Subject: [PATCH 06/43] Autotools: Add configure check for printf() format specifier for size_t See osdn #43357 Signed-off-by: Marko Lindqvist --- configure.ac | 9 +++++++++ m4/compiler.m4 | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/configure.ac b/configure.ac index cc506d43eb..b98fbc71b7 100644 --- a/configure.ac +++ b/configure.ac @@ -680,6 +680,10 @@ FC_DEBUG dnl check profiling FC_GPROF +dnl Detect flags to be used in configure tests that should fail on warnings +WERROR_TEST_FLAGS="" +FC_C_FLAGS([-Werror -Wall], [], [WERROR_TEST_FLAGS]) + FC_C99_VARIADIC_MACROS FC_C99_VARIABLE_ARRAYS FC_C99_INITIALIZERS @@ -695,6 +699,11 @@ FC_STATIC_STRLEN FC_CXX11_STATIC_ASSERT FC_CXX11_NULLPTR +_CFLAGS="$CFLAGS" +CFLAGS="$CFLAGS $WERROR_TEST_FLAGS" +FC_SIZE_T_FORMAT +CFLAGS="$_CFLAGS" + dnl BeOS-specific settings if test x`$UNAME -s` = xBeOS ; then AC_DEFINE([FREECIV_SOCKET_ZERO_NOT_STDIN], [1], [BeOS-specific setting]) diff --git a/m4/compiler.m4 b/m4/compiler.m4 index 110afd803c..bf0b1d4115 100644 --- a/m4/compiler.m4 +++ b/m4/compiler.m4 @@ -127,3 +127,43 @@ elif test "x$qt_ver" = "xQt6" && test "x$HAVE_CXX17" = "x" ; then cxx_works=no fi ]) + +# Test suitability of a single printf() format specifier for +# size_t variables. Helper for FC_SIZE_T_FORMAT +# +# $1 - Format string +AC_DEFUN([_FC_SIZE_T_FORMAT_TEST], +[ +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include +#if defined(__GNUC__) +void fr(const char *form, ...) + __attribute__((__format__(__printf__, 1, 2))); +#else +#define fr(_a_,_b_) printf(_a_,_b_) +#endif +]], +[[ +size_t var = 0; +fr("$1", var);]])], [SIZE_T_PRINTF="$1"]) +]) + +# Find out proper printf() format specifier for size_t variables +AC_DEFUN([FC_SIZE_T_FORMAT], +[ + AC_MSG_CHECKING([format specifier for size_t]) + + for fmt in "%zu" "%ld" "%lld" "%I64d" + do + if test "x$SIZE_T_PRINTF" = "x" ; then + _FC_SIZE_T_FORMAT_TEST([${fmt}]) + fi + done + + if test "x$SIZE_T_PRINTF" = "x" ; then + AC_MSG_ERROR([Cannot find correct printf format specifier for size_t]) + fi + + AC_DEFINE_UNQUOTED([SIZE_T_PRINTF], ["$SIZE_T_PRINTF"], [Format specifier for size_t]) + + AC_MSG_RESULT([$SIZE_T_PRINTF]) +]) -- 2.34.1