[cfe-dev] noderef attribute

Leonard Chan via cfe-dev cfe-dev at lists.llvm.org
Thu Jul 19 12:33:00 PDT 2018


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


More information about the cfe-dev mailing list