From 97959cd7eb76b7ff52b3c40002d974e3d4c03f77 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Sat, 20 Mar 2021 10:15:02 +0800 Subject: [PATCH 1/4] msvc_recommended_pragmas.h: Refine for clang-cl Make msvc_recommended_pragmas.h work better with clang-cl so that we can use that to eliminate some warnings that are emitted as it also consumes Microsoft compiler and SDK headers. Also, for GLib builds, force-include msvc_recommended_pragmas.h for clang-cl builds as well, as it becomes usable and useful there. Fixes issue #2357. --- meson.build | 13 +++++++++---- msvc_recommended_pragmas.h | 4 +++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/meson.build b/meson.build index 2cf1c7b4f..65304b3c1 100644 --- a/meson.build +++ b/meson.build @@ -14,18 +14,23 @@ cxx = meson.get_compiler('cpp') cc_can_run = not meson.is_cross_build() or meson.has_exe_wrapper() -if cc.get_id() == 'msvc' +if cc.get_argument_syntax() == 'msvc' # Ignore several spurious warnings for things glib does very commonly + # (also for clang-cl) + add_project_arguments('/FImsvc_recommended_pragmas.h',language : 'c') +endif + +if cc.get_id() == 'msvc' # If a warning is completely useless and spammy, use '/wdXXXX' to suppress it # If a warning is harmless but hard to fix, use '/woXXXX' so it's shown once # NOTE: Only add warnings here if you are sure they're spurious add_project_arguments('/wd4035', '/wd4715', '/wd4116', - '/wd4046', '/wd4068', '/wo4090', '/FImsvc_recommended_pragmas.h',language : 'c') + '/wd4046', '/wd4068', '/wo4090',language : 'c') + # Set the input and exec encoding to utf-8, like is the default with GCC + add_project_arguments(cc.get_supported_arguments(['/utf-8']), language: 'c') # Disable SAFESEH with MSVC for plugins and libs that use external deps that # are built with MinGW noseh_link_args = ['/SAFESEH:NO'] - # Set the input and exec encoding to utf-8, like is the default with GCC - add_project_arguments(cc.get_supported_arguments(['/utf-8']), language: 'c') else noseh_link_args = [] # -mms-bitfields vs -fnative-struct ? diff --git a/msvc_recommended_pragmas.h b/msvc_recommended_pragmas.h index e70698712..051a02ae4 100644 --- a/msvc_recommended_pragmas.h +++ b/msvc_recommended_pragmas.h @@ -1,10 +1,11 @@ #ifndef _MSC_VER -#pragma error "This header is for Microsoft VC only." +#pragma error "This header is for Microsoft VC or clang-cl only." #endif /* _MSC_VER */ /* Make MSVC more pedantic, this is a recommended pragma list * from _Win32_Programming_ by Rector and Newcomer. */ +#ifndef __clang__ #pragma warning(error:4002) /* too many actual parameters for macro */ #pragma warning(error:4003) /* not enough actual parameters for macro */ #pragma warning(1:4010) /* single-line comment contains line-continuation character */ @@ -33,6 +34,7 @@ #pragma warning(disable:4305) /* No truncation from int to char warnings */ #pragma warning(error:4819) /* The file contains a character that cannot be represented in the current code page */ +#endif /* __clang__ */ /* work around Microsoft's premature attempt to deprecate the C-Library */ #define _CRT_SECURE_NO_WARNINGS From 7b76995298fb2ce2f751d630bb5c23a56a018725 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Sat, 20 Mar 2021 10:38:08 +0800 Subject: [PATCH 2/4] gmacros.h: Use _Static_assert for clang It appears that CLang supports _Static_assert() even when not in C11 mode, since at least CLang 3.1, so let's just use that for CLang builds. Fixes issue #2338. --- glib/gmacros.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glib/gmacros.h b/glib/gmacros.h index 2ece48ba0..666db5790 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -754,7 +754,7 @@ #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */ #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2 #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2) -#if !defined(__cplusplus) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +#if !defined(__cplusplus) && defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L || defined(__clang__)) #define G_STATIC_ASSERT(expr) _Static_assert (expr, "Expression evaluates to false") #elif (defined(__cplusplus) && __cplusplus >= 201103L) || \ (defined(__cplusplus) && defined (_MSC_VER) && (_MSC_VER >= 1600)) || \ From f801c84c52c9da24652e228d306d0ad82522ede9 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Tue, 30 Mar 2021 16:40:41 +0800 Subject: [PATCH 3/4] gmacros.h: Improve check for C _Static_assert Use the GCC/Clang macros __has_extension() and __has_feature() on c_static_assert, which is the documented way for GCC/CLang to check for _Static_assert if C11 mode is not enabled, as suggested by Aleksandr Mezin. As a result, add a private macro that is defined to be __has_extension if it exists, otherwise it is considered to be always false. --- glib/gmacros.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/glib/gmacros.h b/glib/gmacros.h index 666db5790..3466b268c 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -276,6 +276,12 @@ #define g_macro__has_builtin(x) 0 #endif +#ifdef __has_extension +#define g_macro__has_extension __has_extension +#else +#define g_macro__has_extension(x) 0 +#endif + /** * G_GNUC_ALLOC_SIZE: * @x: the index of the argument specifying the allocation size @@ -754,7 +760,8 @@ #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */ #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2 #define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (identifier1, identifier2) -#if !defined(__cplusplus) && defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L || defined(__clang__)) +#if !defined(__cplusplus) && defined(__STDC_VERSION__) && \ + (__STDC_VERSION__ >= 201112L || g_macro__has_feature(c_static_assert) || g_macro__has_extension(c_static_assert)) #define G_STATIC_ASSERT(expr) _Static_assert (expr, "Expression evaluates to false") #elif (defined(__cplusplus) && __cplusplus >= 201103L) || \ (defined(__cplusplus) && defined (_MSC_VER) && (_MSC_VER >= 1600)) || \ From 35154a3519b8626b52ffb2db80902b50f44732ba Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Wed, 31 Mar 2021 10:50:27 +0800 Subject: [PATCH 4/4] glib-sections.txt: Add g_macro__has_extension in private section This should satisfy the documentation build check. --- docs/reference/glib/glib-sections.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index b03c693d4..db0e554e7 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -753,6 +753,7 @@ glib_typeof_2_68 g_macro__has_attribute g_macro__has_builtin g_macro__has_feature +g_macro__has_extension