This patch adds the noderef attribute in clang and checks for dereferences of types that have this attribute. This attribute is currently used by sparse and would like to be ported to clang.
The following are examples of when a warning would be raised on dereferencing a noderef type.
int __attribute__((noderef)) *p; int x = *p; // warning: dereference of noderef expression [-Wignored-attributes] int __attribute__((noderef)) **p2; x = **p2; // warning: dereference of noderef expression [-Wignored-attributes] int * __attribute__((noderef)) *p3; p = *p3; // warning: dereference of noderef expression [-Wignored-attributes] struct S { int a; }; struct S __attribute__((noderef)) *s; x = s->a; // warning: dereference of noderef expression [-Wignored-attributes] x = (*s).a; // warning: dereference of noderef expression [-Wignored-attributes]
Not all accesses may raise a warning if the value directed by the pointer may not be accessed. The following are examples where no warning may be raised:
int *q; int __attribute__((noderef)) *p; q = &*p; q = *&p; struct S { int a; }; struct S __attribute__((noderef)) *s; p = &s->a; p = &(*s).a;
More examples of existing usage of noderef in sparse can be found in https://git.kernel.org/pub/scm/devel/sparse/sparse.git/tree/validation/noderef.c
CheckSingleAssignmentConstraints also warns in this case; is the explicit warning emission above necessary?