SDL: add HAVE_GCC_DIAGNOSTIC_PRAGMA to SDL_internal.h, use it

From 5730eb67f05bd73adfacc4c59753a0a6c7db957d Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Thu, 23 Nov 2023 11:33:20 +0300
Subject: [PATCH] add HAVE_GCC_DIAGNOSTIC_PRAGMA to SDL_internal.h, use it

---
 src/SDL_internal.h                                 |  4 ++++
 src/SDL_log.c                                      |  4 ++--
 src/hidapi/windows/hid.c                           | 12 ++++++------
 src/hidapi/windows/hidapi_descriptor_reconstruct.h |  4 ++++
 src/render/direct3d11/SDL_render_d3d11.c           |  4 ++--
 src/render/direct3d12/SDL_render_d3d12.c           |  4 ++--
 src/render/ps2/SDL_render_ps2.c                    |  6 ++++++
 src/video/ps2/SDL_ps2video.h                       |  6 ++++++
 src/video/x11/SDL_x11sym.h                         |  4 ++--
 9 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/src/SDL_internal.h b/src/SDL_internal.h
index b0f2c296f64f..8c5463a924fb 100644
--- a/src/SDL_internal.h
+++ b/src/SDL_internal.h
@@ -40,6 +40,10 @@
 #define SDL_VARIABLE_LENGTH_ARRAY
 #endif
 
+#if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) || defined(__clang__)
+#define HAVE_GCC_DIAGNOSTIC_PRAGMA 1
+#endif
+
 #define SDL_MAX_SMALL_ALLOC_STACKSIZE          128
 #define SDL_small_alloc(type, count, pisstack) ((*(pisstack) = ((sizeof(type) * (count)) < SDL_MAX_SMALL_ALLOC_STACKSIZE)), (*(pisstack) ? SDL_stack_alloc(type, count) : (type *)SDL_malloc(sizeof(type) * (count))))
 #define SDL_small_free(ptr, isstack) \
diff --git a/src/SDL_log.c b/src/SDL_log.c
index a9ac740c4278..89cc380c863b 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -65,7 +65,7 @@ static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
 static void *SDL_log_userdata = NULL;
 static SDL_Mutex *log_function_mutex = NULL;
 
-#ifdef __GNUC__
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-variable"
 #endif
@@ -80,7 +80,7 @@ static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
     "CRITICAL"
 };
 
-#ifdef __GNUC__
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic pop
 #endif
 
diff --git a/src/hidapi/windows/hid.c b/src/hidapi/windows/hid.c
index e8b6ee987014..3a90a3db37cf 100644
--- a/src/hidapi/windows/hid.c
+++ b/src/hidapi/windows/hid.c
@@ -120,7 +120,7 @@ static void free_library_handles()
 	cfgmgr32_lib_handle = NULL;
 }
 
-#if defined(__GNUC__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 # pragma GCC diagnostic push
 # pragma GCC diagnostic ignored "-Wcast-function-type"
 #endif
@@ -170,7 +170,7 @@ static int lookup_functions()
 	return -1;
 }
 
-#if defined(__GNUC__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 # pragma GCC diagnostic pop
 #endif
 
@@ -325,7 +325,7 @@ static void register_winapi_error_to_buffer(wchar_t **error_buffer, const WCHAR
 #endif
 }
 
-#if defined(__GNUC__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 # pragma GCC diagnostic push
 # pragma GCC diagnostic ignored "-Warray-bounds"
 #endif
@@ -355,7 +355,7 @@ static void register_string_error_to_buffer(wchar_t **error_buffer, const WCHAR
 #endif /* HIDAPI_USING_SDL_RUNTIME */
 }
 
-#if defined(__GNUC__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 # pragma GCC diagnostic pop
 #endif
 
@@ -1462,7 +1462,7 @@ int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned c
 	return hid_get_report(dev, IOCTL_HID_GET_INPUT_REPORT, data, length);
 }
 
-#if defined(__GNUC__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 # pragma GCC diagnostic push
 # pragma GCC diagnostic ignored "-Wcast-function-type"
 #endif
@@ -1486,7 +1486,7 @@ void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev)
 	}
 	free_hid_device(dev);
 }
-#if defined(__GNUC__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 # pragma GCC diagnostic pop
 #endif
 
diff --git a/src/hidapi/windows/hidapi_descriptor_reconstruct.h b/src/hidapi/windows/hidapi_descriptor_reconstruct.h
index 5f0b54aa0f14..6f3e37f744c4 100644
--- a/src/hidapi/windows/hidapi_descriptor_reconstruct.h
+++ b/src/hidapi/windows/hidapi_descriptor_reconstruct.h
@@ -217,11 +217,15 @@ typedef struct hidp_preparsed_data_ {
 	// MINGW fails with: Flexible array member in union not supported
 	// Solution: https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
 	union {
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wpedantic"
+#endif
 		hid_pp_cap caps[0];
 		hid_pp_link_collection_node LinkCollectionArray[0];
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic pop
+#endif
 	};
 #else
 	union {
diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c
index 2263f883276c..103eb21715f8 100644
--- a/src/render/direct3d11/SDL_render_d3d11.c
+++ b/src/render/direct3d11/SDL_render_d3d11.c
@@ -175,7 +175,7 @@ typedef struct
  * linker errors in WinRT/UWP builds.)
  */
 
-#ifdef __GNUC__
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-const-variable"
 #endif
@@ -190,7 +190,7 @@ static const GUID SDL_IID_ID3D11Device1 = { 0xa04bfb29, 0x08ef, 0x43d6, { 0xa4,
 static const GUID SDL_IID_ID3D11DeviceContext1 = { 0xbb2c6faa, 0xb5fb, 0x4082, { 0x8e, 0x6b, 0x38, 0x8b, 0x8c, 0xfa, 0x90, 0xe1 } };
 /*static const GUID SDL_IID_ID3D11Debug = { 0x79cf2233, 0x7536, 0x4948, { 0x9d, 0x36, 0x1e, 0x46, 0x92, 0xdc, 0x57, 0x60 } };*/
 
-#ifdef __GNUC__
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic pop
 #endif
 
diff --git a/src/render/direct3d12/SDL_render_d3d12.c b/src/render/direct3d12/SDL_render_d3d12.c
index c241c3bc9cbc..dee07a489874 100644
--- a/src/render/direct3d12/SDL_render_d3d12.c
+++ b/src/render/direct3d12/SDL_render_d3d12.c
@@ -235,7 +235,7 @@ typedef struct
 
 /* Define D3D GUIDs here so we don't have to include uuid.lib. */
 
-#if defined(__GNUC__) || defined(__clang__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-const-variable"
 #endif
@@ -260,7 +260,7 @@ static const GUID SDL_IID_ID3D12PipelineState = { 0x765a30f3, 0xf624, 0x4c6f, {
 static const GUID SDL_IID_ID3D12Heap = { 0x6b3b2502, 0x6e51, 0x45b3, { 0x90, 0xee, 0x98, 0x84, 0x26, 0x5e, 0x8d, 0xf3 } };
 static const GUID SDL_IID_ID3D12InfoQueue = { 0x0742a90b, 0xc387, 0x483f, { 0xb9, 0x46, 0x30, 0xa7, 0xe4, 0xe6, 0x14, 0x58 } };
 
-#if defined(__GNUC__) || defined(__clang__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic pop
 #endif
 
diff --git a/src/render/ps2/SDL_render_ps2.c b/src/render/ps2/SDL_render_ps2.c
index 467d2745c048..9d02c3e768ab 100644
--- a/src/render/ps2/SDL_render_ps2.c
+++ b/src/render/ps2/SDL_render_ps2.c
@@ -30,10 +30,16 @@
 #include <dmaKit.h>
 #include <gsToolkit.h>
 
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
+#endif
+
 #include <gsInline.h>
+
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic pop
+#endif
 
 /* turn black GS Screen */
 #define GS_BLACK GS_SETREG_RGBA(0x00, 0x00, 0x00, 0x80)
diff --git a/src/video/ps2/SDL_ps2video.h b/src/video/ps2/SDL_ps2video.h
index 33563ea88c82..ef255736f3fe 100644
--- a/src/video/ps2/SDL_ps2video.h
+++ b/src/video/ps2/SDL_ps2video.h
@@ -32,9 +32,15 @@
 
 #include <gsToolkit.h>
 
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
+#endif
+
 #include <gsInline.h>
+
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic pop
+#endif
 
 #endif /* SDL_ps2video_h_ */
diff --git a/src/video/x11/SDL_x11sym.h b/src/video/x11/SDL_x11sym.h
index 67f74d4b0f0a..001633a5e268 100644
--- a/src/video/x11/SDL_x11sym.h
+++ b/src/video/x11/SDL_x11sym.h
@@ -194,7 +194,7 @@ SDL_X11_SYM(Bool,XkbSetDetectableAutoRepeat,(Display* a, Bool b, Bool* c),(a,b,c
 #endif
 
 /* XKeycodeToKeysym is a deprecated function */
-#if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) || defined(__clang__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 #endif
@@ -203,7 +203,7 @@ SDL_X11_SYM(KeySym,XKeycodeToKeysym,(Display* a,unsigned int b,int c),(a,b,c),re
 #else
 SDL_X11_SYM(KeySym,XKeycodeToKeysym,(Display* a,KeyCode b,int c),(a,b,c),return)
 #endif
-#if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) || defined(__clang__)
+#ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
 #pragma GCC diagnostic pop
 #endif