__has_builtin() falsely indicates support for certain built-in functions

The following input:

echo '__has_builtin(__builtin_add_overflow)' | cproc -E -xc -

...seems to evaluate to 1, even though the function in question doesn't seem to be supported by the compiler. I'm guessing this is due to the use of an external pre-processor?

FWIW, I ran into this problem when using code like the following:

#include <stdbool.h>
#include <stdint.h>

#ifdef __has_builtin
    #define HAS_BUILTIN(x) __has_builtin(x)
    #define HAS_BUILTIN(x) 0

static inline bool size_add_overflows(size_t a, size_t b, size_t *result)
#if HAS_BUILTIN(__builtin_add_overflow)
    return __builtin_add_overflow(a, b, result);
    if (b > SIZE_MAX - a) {
        return true;
    *result = a + b;
    return false;

If the feature query would evaluate to 0, the fallback would work fine. However, I currently just get the following error instead:

error: undeclared identifier: __builtin_add_overflow
Assigned to
2 years ago
2 months ago
No labels applied.

~mcf 2 months ago

Yeah, this is due to the external preprocessor. I'm not sure the best way to solve it. Adding -U __has_builtin to the preprocessor flags results in

<command-line>: warning: undefining "__has_builtin"

with no apparent way of turning it off.

Similarly, -D __has_builtin(x)= results in

<command-line>: warning: "__has_builtin" redefined

Looking in the gcc source and manual, there is -Wno-builtin-macro-redefined. Unfortunately, it seems that __has_builtin is defined internally with always_warn_if_redefined, so the warning persists.

I was hoping to ignore this problem until the cproc preprocessor is finished. But maybe the warning is better than falsely claiming support for built-ins?

Register here or Log in to comment, or comment via email.