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

Justin Stitt via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 22 11:24:11 PDT 2024


JustinStitt wrote:

> Regarding there not being a `no-wraps` attribute. What happens with code like this? Is the attribute lost / casted away during the addition?
> 
> ```
> wrapping_int a = INT_MAX;
> a = (int) a + 1;
> ```

Good question, the attribute is cast away (intentionally so). Additionally, sanitizers will warn of the overflow since the addition no longer contains a wraps-annotated type/variable. The result of the addition is the same regardless: `-2147483648`


> Does it affect converting a number too large to fit in the target type?
> 
> ```
> wrapping_int a = INT_MAX;
> wrapping_short b = a;
> short c = a;
> ```

No, this attribute really doesn't modify any arithmetic. It just tells the sanitizer not to instrument.

```c
  wrapping_int a = INT_MAX;
  wrapping_short b = a;
  short c = a;
  printf("%d\n%hd\n%hd\n", a, b, c);

/*
 * 2147483647
 * -1
 * -1
*/
```


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


More information about the cfe-commits mailing list