headers: crt: Improve _Pragma support and add helper macros

Previously, __MINGW_PRAGMA_PARAM only resolves to _Pragma under GCC 4.4
and later, which means Clang and other compilers that support _Pragma
resolves it to empty, thus it was seldom used before.

This commit improves the detection of _Pragma support by first checking
for C++11 and C99 support, and then checking for GCC 3.0 and later,
which is the first documented version that supports _Pragma. Since Clang
always pretends to be GCC 4.2, and most other compilers also pretend to
be GCC 3.0+, __MINGW_PRAGMA_PARAM can now be safely used in most cases.

Also added _CRT_BEGIN_C_HEADER and _CRT_END_C_HEADER macros similar to
those in MSVC's corecrt.h, so that we no longer have to write #ifdef
__cplusplus in every header.

Signed-off-by: Zhongteng Gui <dragon-archer@outlook.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
diff --git a/mingw-w64-headers/crt/_mingw.h.in b/mingw-w64-headers/crt/_mingw.h.in
index e7c4790..771b4bf 100644
--- a/mingw-w64-headers/crt/_mingw.h.in
+++ b/mingw-w64-headers/crt/_mingw.h.in
@@ -214,12 +214,27 @@
 #define __MINGW_ATTRIB_NO_OPTIMIZE
 #endif
 
-#if __MINGW_GNUC_PREREQ (4, 4)
-#define __MINGW_PRAGMA_PARAM(x) _Pragma (#x)
+/**
+ * ISO C++11 & ISO C99 requires _Pragma to be implemented.
+ * GCC supports _Pragma since 3.0 (https://gcc.gnu.org/pipermail/gcc/2002-March/078934.html)
+ * Clang at least since 3.0 supports _Pragma, and pretends to be GCC 4.2, so no special handling is needed.
+ *
+ * Fallback to _Pragma instead of empty, in hope that some compilers may actually support it, but don't define __GNUC__.
+ * This also avoids silent ABI breakage.
+ *
+ * NOTE: Clang and MSVC expands macros inside #pragma, _Pragma, and __pragma, but GCC does not.
+ * This is why we need a helper macro to force the expansion.
+ */
+#if (defined(__cplusplus) && __cplusplus >= 201103L) \
+    || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \
+    || __MINGW_GNUC_PREREQ (3, 0)
+#define __MINGW_PRAGMA_PARAM_IMPL(x) _Pragma (#x)
+#define __MINGW_PRAGMA_PARAM(x) __MINGW_PRAGMA_PARAM_IMPL(x)
 #elif __MINGW_MSC_PREREQ (13, 1)
 #define __MINGW_PRAGMA_PARAM(x) __pragma (x)
 #else
-#define __MINGW_PRAGMA_PARAM(x)
+#define __MINGW_PRAGMA_PARAM_IMPL(x) _Pragma (#x)
+#define __MINGW_PRAGMA_PARAM(x) __MINGW_PRAGMA_PARAM_IMPL(x)
 #endif
 
 #define __MINGW_BROKEN_INTERFACE(x) \
@@ -303,6 +318,14 @@
 #endif
 #endif /* __nothrow */
 
+#ifdef __cplusplus
+#define _CRT_BEGIN_C_HEADER extern "C" {
+#define _CRT_END_C_HEADER   }
+#else
+#define _CRT_BEGIN_C_HEADER
+#define _CRT_END_C_HEADER
+#endif
+
 #include <vadefs.h>	/* other headers depend on this include */
 
 #ifndef _CRT_STRINGIZE
@@ -474,7 +497,6 @@
 #undef  _CRT_glob
 #define _CRT_glob _dowildcard
 
-
 #if defined(_MSC_VER) && !defined(_MSC_EXTENSIONS)
 #define NONAMELESSUNION		1
 #endif
@@ -584,10 +606,7 @@
 
 #endif
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
+_CRT_BEGIN_C_HEADER
 
 #ifdef __MINGW_INTRIN_INLINE
 
@@ -660,9 +679,7 @@
 /* mingw-w64 specific functions: */
 const char *__mingw_get_crt_info (void);
 
-#ifdef __cplusplus
-}
-#endif
+_CRT_END_C_HEADER
 
 #define __STDC_SECURE_LIB__ 200411L
 #define __GOT_SECURE_LIB__ __STDC_SECURE_LIB__