noderef attribute

Hi all,

I'm working on a patch at https://reviews.llvm.org/D49511 that
implements the `noderef` attribute in clang. Essentially the attribute
works by raising a warning when dereferencing a pointer marked with
`__attribute__((noderef))`. This attribute is currently used by sparse
and would like to be ported to clang and wanted to see if anyone would
be up for reviewing it/had any comments on it.

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

Feel free to provide feedback/reviews.

Thanks,
Leo

Hi all,

I'm working on a patch at https://reviews.llvm.org/D49511 that
implements the `noderef` attribute in clang. Essentially the attribute
works by raising a warning when dereferencing a pointer marked with
`__attribute__((noderef))`. This attribute is currently used by sparse

Sparse is this: https://sparse.wiki.kernel.org/index.php/Main_Page

Any other attributes from Sparse you're interesting in adding, or just
this one?

If we have a function parameter that takes a noderef pointer, can we add
the readnone attribute on the argument so that the optimizer knows that
memory is not accessed through that pointer?

-Hal

Any other attributes from Sparse you're interesting in adding, or just
this one?

Not anything other immediate ones. We did have an idea of expanding
`address_space` into something that accepts names instead of just
numbers for representing address spaces in a more "verbose" way, but
that's down the road.

If we have a function parameter that takes a noderef pointer, can we add
the readnone attribute on the argument so that the optimizer knows that
memory is not accessed through that pointer?

Will do. Initially I wanted to just replicate this part of sparse but
this makes sense to add.