SDL: testautomation: avoid format related warnings by using a few pragma's

From 274ec025819a24eeaa13cca2b4869fbdef1dc876 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 6 Oct 2022 00:31:00 +0200
Subject: [PATCH] testautomation: avoid format related warnings by using a few
 pragma's

ci: enable -Werror to a few platforms
---
 test/CMakeLists.txt            | 19 ++++++++
 test/configure                 | 84 ++++++++++++++++++++++++++++++++++
 test/configure.ac              | 36 +++++++++++++++
 test/testautomation_platform.c | 11 ++++-
 test/testautomation_stdlib.c   | 29 +++++++++++-
 5 files changed, 177 insertions(+), 2 deletions(-)

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 133c7975297f..d7c19f175c34 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -160,6 +160,25 @@ add_executable(controllermap controllermap.c testutils.c)
 add_executable(testvulkan testvulkan.c)
 add_executable(testoffscreen testoffscreen.c)
 
+cmake_push_check_state(RESET)
+
+check_c_compiler_flag(-Wformat-overflow HAVE_WFORMAT_OVERFLOW)
+if(HAVE_WFORMAT_OVERFLOW)
+    target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT_OVERFLOW)
+endif()
+
+check_c_compiler_flag(-Wformat HAVE_WFORMAT)
+if(HAVE_WFORMAT)
+    target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT)
+endif()
+
+check_c_compiler_flag(-Wformat-extra-args HAVE_WFORMAT_EXTRA_ARGS)
+if(HAVE_WFORMAT_EXTRA_ARGS)
+    target_compile_definitions(testautomation PRIVATE HAVE_WFORMAT_EXTRA_ARGS)
+endif()
+
+cmake_pop_check_state()
+
 SET(ALL_TESTS
     checkkeys
     checkkeysthreads
diff --git a/test/configure b/test/configure
index 9c1e90a86378..8b7afb01ef08 100755
--- a/test/configure
+++ b/test/configure
@@ -4664,6 +4664,90 @@ if test x$have_opengl = xyes; then
     OPENGL_TARGETS="TARGETS"
 fi
 
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for GCC -Wformat" >&5
+printf %s "checking for GCC -Wformat... " >&6; }
+have_wformat=no
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -Wformat"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+int x = 0;
+int
+main (void)
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"
+then :
+  have_wformat=yes
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $have_wformat" >&5
+printf "%s\n" "$have_wformat" >&6; }
+CFLAGS="$save_CFLAGS"
+if test x$have_wformat = xyes; then
+    CFLAGS="$CFLAGS -DHAVE_WFORMAT"
+fi
+
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for GCC -Wformat-overflow" >&5
+printf %s "checking for GCC -Wformat-overflow... " >&6; }
+have_wformat_overflow=no
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -Wformat-overflow"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+int x = 0;
+int
+main (void)
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"
+then :
+  have_wformat_overflow=yes
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $have_wformat_overflow" >&5
+printf "%s\n" "$have_wformat_overflow" >&6; }
+CFLAGS="$save_CFLAGS"
+if test x$have_wformat_overflow = xyes; then
+    CFLAGS="$CFLAGS -DHAVE_WFORMAT_OVERFLOW"
+fi
+
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for GCC -Wformat-extra-args" >&5
+printf %s "checking for GCC -Wformat-extra-args... " >&6; }
+have_wformat_extra_args=no
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -Wformat-extra-args"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+int x = 0;
+int
+main (void)
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"
+then :
+  have_wformat_extra_args=yes
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $have_wformat_extra_args" >&5
+printf "%s\n" "$have_wformat_extra_args" >&6; }
+CFLAGS="$save_CFLAGS"
+if test x$have_wformat_extra_args = xyes; then
+    CFLAGS="$CFLAGS -DHAVE_WFORMAT_EXTRA_ARGS"
+fi
+
 # Check whether --enable-werror was given.
 if test ${enable_werror+y}
 then :
diff --git a/test/configure.ac b/test/configure.ac
index d31565dfdc7d..e0dd1c463896 100644
--- a/test/configure.ac
+++ b/test/configure.ac
@@ -184,6 +184,42 @@ if test x$have_opengl = xyes; then
     OPENGL_TARGETS="TARGETS"
 fi
 
+AC_MSG_CHECKING(for GCC -Wformat)
+have_wformat=no
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -Wformat"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[int x = 0;]],[])],
+    [have_wformat=yes], [])
+AC_MSG_RESULT($have_wformat)
+CFLAGS="$save_CFLAGS"
+if test x$have_wformat = xyes; then
+    CFLAGS="$CFLAGS -DHAVE_WFORMAT"
+fi
+
+AC_MSG_CHECKING(for GCC -Wformat-overflow)
+have_wformat_overflow=no
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -Wformat-overflow"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[int x = 0;]],[])],
+    [have_wformat_overflow=yes], [])
+AC_MSG_RESULT($have_wformat_overflow)
+CFLAGS="$save_CFLAGS"
+if test x$have_wformat_overflow = xyes; then
+    CFLAGS="$CFLAGS -DHAVE_WFORMAT_OVERFLOW"
+fi
+
+AC_MSG_CHECKING(for GCC -Wformat-extra-args)
+have_wformat_extra_args=no
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -Wformat-extra-args"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[int x = 0;]],[])],
+    [have_wformat_extra_args=yes], [])
+AC_MSG_RESULT($have_wformat_extra_args)
+CFLAGS="$save_CFLAGS"
+if test x$have_wformat_extra_args = xyes; then
+    CFLAGS="$CFLAGS -DHAVE_WFORMAT_EXTRA_ARGS"
+fi
+
 AC_ARG_ENABLE(werror,
 [AS_HELP_STRING([--enable-werror], [treat warnings as errors [default=no]])],
               enable_werror=$enableval, enable_werror=no)
diff --git a/test/testautomation_platform.c b/test/testautomation_platform.c
index 5eb37b332a0e..0b37dd9314ec 100644
--- a/test/testautomation_platform.c
+++ b/test/testautomation_platform.c
@@ -376,6 +376,11 @@ int platform_testSetErrorEmptyInput(void *arg)
    return TEST_COMPLETED;
 }
 
+#if defined(HAVE_WFORMAT_OVERFLOW)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-overflow"
+#endif
+
 /* !
  * \brief Tests SDL_SetError with invalid input
  * \sa
@@ -451,7 +456,7 @@ int platform_testSetErrorInvalidInput(void *arg)
              probeError,
              lastError);
    }
-   
+
    /* Clean up */
    SDL_ClearError();
    SDLTest_AssertPass("SDL_ClearError()");
@@ -459,6 +464,10 @@ int platform_testSetErrorInvalidInput(void *arg)
    return TEST_COMPLETED;
 }
 
+#if defined(HAVE_WFORMAT_OVERFLOW)
+#pragma GCC diagnostic pop
+#endif
+
 /* !
  * \brief Tests SDL_GetPowerInfo
  * \sa
diff --git a/test/testautomation_stdlib.c b/test/testautomation_stdlib.c
index a89ec1e0b05a..7abad3625355 100644
--- a/test/testautomation_stdlib.c
+++ b/test/testautomation_stdlib.c
@@ -7,7 +7,6 @@
 #include "SDL.h"
 #include "SDL_test.h"
 
-
 /* Test case functions */
 
 /**
@@ -36,6 +35,16 @@ stdlib_strlcpy(void *arg)
   return TEST_COMPLETED;
 }
 
+#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
+#pragma GCC diagnostic push
+#if defined(HAVE_WFORMAT)
+#pragma GCC diagnostic ignored "-Wformat"
+#endif
+#if defined(HAVE_WFORMAT_EXTRA_ARGS)
+#pragma GCC diagnostic ignored "-Wformat-extra-args"
+#endif
+#endif
+
 /**
  * @brief Call to SDL_snprintf
  */
@@ -159,6 +168,10 @@ stdlib_snprintf(void *arg)
   return TEST_COMPLETED;
 }
 
+#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
+#pragma GCC diagnostic pop
+#endif
+
 /**
  * @brief Call to SDL_getenv and SDL_setenv
  */
@@ -293,6 +306,16 @@ stdlib_getsetenv(void *arg)
   return TEST_COMPLETED;
 }
 
+#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
+#pragma GCC diagnostic push
+#if defined(HAVE_WFORMAT)
+#pragma GCC diagnostic ignored "-Wformat"
+#endif
+#if defined(HAVE_WFORMAT_EXTRA_ARGS)
+#pragma GCC diagnostic ignored "-Wformat-extra-args"
+#endif
+#endif
+
 /**
  * @brief Call to SDL_sscanf
  */
@@ -375,6 +398,10 @@ stdlib_sscanf(void *arg)
   return TEST_COMPLETED;
 }
 
+#if defined(HAVE_WFORMAT) || defined(HAVE_WFORMAT_EXTRA_ARGS)
+#pragma GCC diagnostic pop
+#endif
+
 #if defined(_WIN64)
 # define SIZE_FORMAT "I64u"
 #elif defined(__WIN32__)