Say I have some function
<code>unsigned int my_random_value(unsigned int a);
</code>
<code>unsigned int my_random_value(unsigned int a);
</code>
unsigned int my_random_value(unsigned int a);
that returns a value in the range 0, 1, …, a – 1. I want to assert GCC that the return value will be in this range, but I don’t know how to do it nicely. By wrapping this function, GCC could be noticed of this via
<code>unsigned int my_random_value(unsigned int a);
static inline
unsigned int my_random_value_wrapper(unsigned int a)
{
unsigned int ret = my_random_value(a);
if (ret >= a)
__builtin_unreachable();
return ret;
}
</code>
<code>unsigned int my_random_value(unsigned int a);
static inline
unsigned int my_random_value_wrapper(unsigned int a)
{
unsigned int ret = my_random_value(a);
if (ret >= a)
__builtin_unreachable();
return ret;
}
</code>
unsigned int my_random_value(unsigned int a);
static inline
unsigned int my_random_value_wrapper(unsigned int a)
{
unsigned int ret = my_random_value(a);
if (ret >= a)
__builtin_unreachable();
return ret;
}
but that will be quite large in a header file, and will not let me use my_random_value
to directly access this function. Is there a better way to do it, perhaps through attributes?