Skip to content

Commit acf6bba

Browse files
authored
compiler: Introduce GNU_FEATURE(), GNU_EXTENSION(), NONCONST_CAST()
- Introduce GNU_FEATURE(x) as a portable __has_feature() wrapper - Introduce GNU_EXTENSION(x) as a portable __has_extension() wrapper - Introduce NONCONST_CAST(type, x) macro for portable -Wconst-qual suppression in rare cases where it's actually needed, like the unfortunate C11 atomic_load()
1 parent e457616 commit acf6bba

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/compiler.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.
7171
#define GNU_BUILTIN(builtin) 0
7272
#endif
7373

74+
// Check GNU feature presence
75+
#undef GNU_FEATURE
76+
#if defined(GNU_EXTS) && defined(__has_feature)
77+
#define GNU_FEATURE(feature) __has_feature(feature)
78+
#else
79+
#define GNU_FEATURE(feature) 0
80+
#endif
81+
82+
// Check GNU extension presence
83+
#undef GNU_EXTENSION
84+
#if defined(GNU_EXTS) && defined(__has_extension)
85+
#define GNU_EXTENSION(extension) __has_extension(extension)
86+
#else
87+
#define GNU_EXTENSION(extension) GNU_FEATURE(extension)
88+
#endif
89+
7490
// Check header presence
7591
#undef CHECK_INCLUDE
7692
#if defined(__has_include)
@@ -287,6 +303,10 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.
287303
#define GNU_CONSTRUCTOR GNU_DUMMY_ATTRIBUTE
288304
#endif
289305

306+
/*
307+
* Warning suppression helpers
308+
*/
309+
290310
// Mark unused arguments or variables to suppress warnings
291311
#undef UNUSED
292312
#define UNUSED(x) ((void)x)
@@ -299,6 +319,14 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.
299319
#define ZERO_INIT {0}
300320
#endif
301321

322+
// Portably cast to a non-constant pointer without triggering -Wconst-qual. Use with care!
323+
#undef NONCONST_CAST
324+
#if defined(GNU_EXTS)
325+
#define NONCONST_CAST(type, x) (((union { const void* _constptr; type _ptr; }){ ._constptr = (const void*)(x), })._ptr)
326+
#else
327+
#define NONCONST_CAST(type, x) ((type)(void*)(x))
328+
#endif
329+
302330
/*
303331
* Host platform feature detection
304332
*/

0 commit comments

Comments
 (0)