| @@ -1,7 +1,40 @@ | | | @@ -1,7 +1,40 @@ |
1 | /* $NetBSD: msg_324.c,v 1.1 2021/01/02 10:22:44 rillig Exp $ */ | | 1 | /* $NetBSD: msg_324.c,v 1.2 2021/01/05 22:38:51 rillig Exp $ */ |
2 | # 3 "msg_324.c" | | 2 | # 3 "msg_324.c" |
3 | | | 3 | |
4 | // Test for message: suggest cast from '%s' to '%s' on op %s to avoid overflow [324] | | 4 | // Test for message: suggest cast from '%s' to '%s' on op %s to avoid overflow [324] |
5 | | | 5 | |
6 | TODO: "Add example code that triggers the above message." | | 6 | /* |
7 | TODO: "Add example code that almost triggers the above message." | | 7 | * This warning applies to binary operators if the result of the operator |
| | | 8 | * is converted to a type that is bigger than the operands' result type |
| | | 9 | * after the usual arithmetic promotions. |
| | | 10 | * |
| | | 11 | * In such a case, the operator's result would be truncated to the operator's |
| | | 12 | * result type (invoking undefined behavior for signed integers), and that |
| | | 13 | * truncated value would then be converted. At that point, a few bits may |
| | | 14 | * have been lost. |
| | | 15 | */ |
| | | 16 | |
| | | 17 | /* lint1-flags: -g -S -w -P */ |
| | | 18 | |
| | | 19 | void |
| | | 20 | example(char c, int i, unsigned u) |
| | | 21 | { |
| | | 22 | long l; |
| | | 23 | unsigned long ul; |
| | | 24 | |
| | | 25 | l = c + i; |
| | | 26 | l = i - c; |
| | | 27 | ul = c * u; |
| | | 28 | ul = u + c; |
| | | 29 | ul = i - u; |
| | | 30 | ul = u * i; |
| | | 31 | l = i << c; |
| | | 32 | |
| | | 33 | /* |
| | | 34 | * The operators SHR, DIV and MOD cannot produce an overflow, |
| | | 35 | * therefore no warning is necessary for them. |
| | | 36 | */ |
| | | 37 | l = i >> c; |
| | | 38 | ul = u / c; |
| | | 39 | ul = u % c; |
| | | 40 | } |