On Tue, 7 Jun 2022 at 20:40, Miika via Gcc <gcc@gcc.gnu.org> wrote: > > On Monday, June 6th, 2022 at 9:42 PM, Ben Boeckel <ben.boec...@kitware.com> > wrote: > > > Based on Jakub's and Yair's comments I created a new attribute "inrange". > > > Inrage takes three arguments, pos min and max. > > > Pos being the argument position in the function, and min and max defines > > > the > > > range of valid integer. Both min and max are inclusive and work with > > > enums. > > > Warnings are enabled with the new flag: "-Winrange". > > > > > > Is this something that could be applied to variables or types (I've not > > much experience with GCC attribute internal mechanisms, so maybe not)? > > I took a closer look at it and looks like it can be applied. > > So trying to compile this: > ``` > typedef int __attribute__((inrange(0, 100))) percentage_t; > int main() { > int percentage __attribute__((inrange(0, 100))) = -1; > percentage_t per __attribute__((inrange(0, 100))) = -1; > } > ``` > > Would print out something like this: > > foo.c: In function 'main': > foo.c:3:59: warning: inrange variable 'percentage' requires integer in rage > of 0..100 [-Winrange]
N.B. "rage" should be "range". >From the diagnostic it's not clear to me whether this is an inclusive range. Is 0 allowed? Is 100 allowed? Using [0,100] interval notation would imply both endpoints are valid, which I think matches the semantics of your attribute. Is interval notation sufficiently widely understood to use here?