On Sat, May 09, 2015 at 09:42:23AM -0700, Martin Uecker wrote: > here is a tentative patch to implement a new attribute nonzero, > which is similar to nonnull, but is not a function attribute > but a type attribute. > > One reason is that nonnull is awkward to use. For this reason, > clang allows the use of nonnull in function parameters, but this > is incompatible with old and current use of this attribute in gcc > (though in a rather obscure use case). > See: https://gcc.gnu.org/ml/gcc/2015-05/msg00077.html Sorry, I quite fail to see how such an attribute would be useful. It seems that the nonzero warning can only ever trigger when used on function parameters or on a return value, much as the nonnull / returns_nonnull attributes. The difference is that you can use the nonzero attribute on a particular function parameter, but the nonnull attribute has this ability as well:
__attribute__ ((nonnull (1))) void foo (int *, int *); void bar (void) { foo (0, 0); } Unlike nonnull, nonzero attribute can be attached to a typedef, but it doesn't seem to buy you anything you couldn't do with the nonnull / returns_nonnull attributes. The nonzero attribute can appertain even to integer types, not only pointer types. Can you give an example where this would come in handy? It doesn't seem too useful to me. +void foo1(int x[__attribute__((nonzero))]); This looks weird, the placement of the nonzero attribute here suggests that the array should have at least zero elements (the same that int x[static 0] does), but in fact it checks that the pointer passed to foo1 is non-NULL, i.e. something that could be easily achieved with the nonnull attribute. > The other reason is that a nonzero type attribute is conceptually > much simpler and at the same time more general than the existing > nonnull and nonnull_return function attributes (and could replace > both), e.g. we can define non-zero types and diagnose all stores > of known constant 0 to variables of this type, use this for > computing better value ranges, etc. Why would that be useful? What makes integer 0 special? Marek