gmacros: Don't define bogus __has_* macros

This pollutes the reserved compiler namespace and breaks applications
trying to do their own feature detection. For instance, this falsely
detects that alloca is not a builtin on gcc:

    #include <glib.h>
    #if defined(__has_builtin)
    # if !__has_builtin(alloca)
    #  error "wtf glib?"
    # endif
    #else
    /* version-checking to determine alloca existence */
    #endif

Instead, define our own g_macro__has_* versions that have the
behaviour that we need.

https://bugzilla.gnome.org/show_bug.cgi?id=794635
This commit is contained in:
Nirbheek Chauhan
2018-03-23 23:10:57 +05:30
parent ba4a9538e1
commit e2bd6a6a8f
3 changed files with 31 additions and 14 deletions

View File

@@ -250,21 +250,23 @@ template <int w>
#define verify(R) _GL_VERIFY (R, "verify (" #R ")")
#ifndef __has_builtin
# define __has_builtin(x) 0
#ifdef __has_builtin
# define _GL_MACRO__has_builtin __has_builtin
#else
# define _GL_MACRO__has_builtin(x) 0
#endif
/* Assume that R always holds. This lets the compiler optimize
accordingly. R should not have side-effects; it may or may not be
evaluated. Behavior is undefined if R is false. */
#if (__has_builtin (__builtin_unreachable) \
#if (_GL_MACRO__has_builtin (__builtin_unreachable) \
|| 4 < __GNUC__ + (5 <= __GNUC_MINOR__))
# define assume(R) ((R) ? (void) 0 : __builtin_unreachable ())
#elif 1200 <= _MSC_VER
# define assume(R) __assume (R)
#elif (defined lint \
&& (__has_builtin (__builtin_trap) \
&& (_GL_MACRO__has_builtin (__builtin_trap) \
|| 3 < __GNUC__ + (3 < __GNUC_MINOR__ + (4 <= __GNUC_PATCHLEVEL__))))
/* Doing it this way helps various packages when configured with
--enable-gcc-warnings, which compiles with -Dlint. It's nicer