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

Justin Stitt via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 11 15:01:46 PDT 2024


JustinStitt wrote:

> > Forbidding usage in C++ probably avoids the worst of the canonical-type issues, but there's still some potential for weird results. Particularly with type merging; for example, if you write `a ? (wrap_int)x : 1`, is the result a wrapping type?
> 
> I had a similar question in the general case: is the annotation contagious?
> 
> ```
> int foo(wrap_int x, int a, int b, int c) {
>   if (x + a + b < c)
>     return 0;
>   return 1;
> }
> ```
> 
> The `x + a` is checked, but is that result checked when added to `b`?

Yes, it's "contagious":

```
|-FunctionDecl 0x55f64065a5d8 <line:6:1, line:10:1> line:6:5 foo 'int (wrap_int, int, int, int)'
| |-ParmVarDecl 0x55f64065a300 <col:9, col:18> col:18 used x 'wrap_int':'int'
| |-ParmVarDecl 0x55f64065a380 <col:21, col:25> col:25 used a 'int'
| |-ParmVarDecl 0x55f64065a400 <col:28, col:32> col:32 used b 'int'
| |-ParmVarDecl 0x55f64065a480 <col:35, col:39> col:39 used c 'int'
| `-CompoundStmt 0x55f64065a8a8 <col:42, line:10:1>
|   |-IfStmt 0x55f64065a858 <line:7:3, line:8:12>
|   | |-BinaryOperator 0x55f64065a808 <line:7:7, col:19> 'int __attribute__((wraps))':'int' '<'
|   | | |-BinaryOperator 0x55f64065a7b0 <col:7, col:15> 'int __attribute__((wraps))':'int' '+'
|   | | | |-BinaryOperator 0x55f64065a758 <col:7, col:11> 'int __attribute__((wraps))':'int' '+'
|   | | | | |-ImplicitCastExpr 0x55f64065a728 <col:7> 'wrap_int':'int' <LValueToRValue>
|   | | | | | `-DeclRefExpr 0x55f64065a6e8 <col:7> 'wrap_int':'int' lvalue ParmVar 0x55f64065a300 'x' 'wrap_int':'int'
|   | | | | `-ImplicitCastExpr 0x55f64065a740 <col:11> 'int' <LValueToRValue>
|   | | | |   `-DeclRefExpr 0x55f64065a708 <col:11> 'int' lvalue ParmVar 0x55f64065a380 'a' 'int'
|   | | | `-ImplicitCastExpr 0x55f64065a798 <col:15> 'int' <LValueToRValue>
|   | | |   `-DeclRefExpr 0x55f64065a778 <col:15> 'int' lvalue ParmVar 0x55f64065a400 'b' 'int'
|   | | `-ImplicitCastExpr 0x55f64065a7f0 <col:19> 'int' <LValueToRValue>
|   | |   `-DeclRefExpr 0x55f64065a7d0 <col:19> 'int' lvalue ParmVar 0x55f64065a480 'c' 'int'
|   | `-ReturnStmt 0x55f64065a848 <line:8:5, col:12>
|   |   `-IntegerLiteral 0x55f64065a828 <col:12> 'int' 0
|   `-ReturnStmt 0x55f64065a898 <line:9:3, col:10>
|     `-IntegerLiteral 0x55f64065a878 <col:10> 'int' 1
```

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


More information about the cfe-commits mailing list