https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91554
Bug ID: 91554
Summary: if (!__builtin_constant_p (x)) warning_function()
works in inline when x is int, not when x is void *
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: zackw at panix dot com
Target Milestone: ---
This snippet
```
extern void thefun_called_with_nonnull_arg (void)
__attribute__((__warning__(
"'thefun' called with second argument not NULL")));
extern int real_thefun (void *, void *);
static inline int
thefun (void *a, void *b)
{
if (!__builtin_constant_p(b) || b != 0)
thefun_called_with_nonnull_arg();
return real_thefun(a, b);
}
int warning_expected (void *a, void *b)
{
return thefun(a, b);
}
int warning_not_expected (void *a)
{
return thefun(a, 0);
}
```
generates warnings from _both_ `warning_expected` and `warning_not_expected`,
on all versions of GCC I can conveniently test (see
https://godbolt.org/z/V-FHtZ ). If I change the type of `b` to be `int`
throughout, or if I convert the static inline to a macro
```
#define thefun(a, b) \
(((!__builtin_constant_p(b) || (b) != 0) \
? thefun_called_with_nonnull_arg() \
: (void) 0), \
real_thefun(a, b))
```
then I get a warning only for `warning_expected`, which is the behavior I want.
It seems to me that whether or not you can use `__builtin_constant_p` to guard
a call to a function declared with attribute warning, should not depend on the
type of __builtin_constant_p's argument.