[clang] [Clang] Add wraps attribute (for granular integer overflow handling) (PR #86618)

Justin Stitt via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 27 15:17:16 PDT 2024


JustinStitt wrote:

> Adding attributes to types as type sugar (not part of the canonical type) is generally problematic: non-canonical types are not reliably preserved throughout the compiler, particularly in cases involving templates.

I see, here's an example that currently breaks my attribute (without any special handling):

```c++
typedef int __attribute__((wraps)) wrapping_int;

template <typename T>
void foo(T a) {
  ++a; // this will throw a sanitizer error because the generic method 
       // created for this templated type is generic
}

int main(void) {
  wrapping_int A = INT_MAX;
  foo(A);
}
```


> 
> Can we use a type qualifier here?

FWICT, qualifiers are extremely sparse with only a handful existing at all. 
```
Type.h +142:
/// The collection of all-type qualifiers we support.
/// Clang supports five independent qualifiers:
/// * C99: const, volatile, and restrict
/// * MS: __unaligned
/// * Embedded C (TR18037): address spaces
/// * Objective C: the GC attributes (none, weak, or strong)
```

Most if not all type qualifiers are strongly associated with how the storage memory of a type is handled. For the behavior needed out of `wraps`, I was thinking this kind of thing is typically done via an attribute. Something like [noderef](https://clang.llvm.org/docs/AttributeReference.html#noderef) is similar. You apply it to types which then adjusts Clang's handling of variables of that type.

If it comes down to it, we can make this attribute only available for C under ``__attribute__(())`` or I can try to add support for templates to be made aware of specific type attributes.

https://github.com/llvm/llvm-project/pull/86618


More information about the cfe-commits mailing list