| @@ -1,52 +1,52 @@ | | | @@ -1,52 +1,52 @@ |
1 | /* $NetBSD: msg_324.c,v 1.3 2021/01/05 23:20:53 rillig Exp $ */ | | 1 | /* $NetBSD: msg_324.c,v 1.4 2021/01/06 09:23:04 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 | /* | | 6 | /* |
7 | * This warning applies to binary operators if the result of the operator | | 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 | | 8 | * is converted to a type that is bigger than the operands' result type |
9 | * after the usual arithmetic promotions. | | 9 | * after the usual arithmetic promotions. |
10 | * | | 10 | * |
11 | * In such a case, the operator's result would be truncated to the operator's | | 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 | | 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 | | 13 | * truncated value would then be converted. At that point, a few bits may |
14 | * have been lost. | | 14 | * have been lost. |
15 | */ | | 15 | */ |
16 | | | 16 | |
17 | /* lint1-flags: -g -S -w -P */ | | 17 | /* lint1-flags: -g -S -w -P */ |
18 | | | 18 | |
19 | void | | 19 | void |
20 | example(char c, int i, unsigned u) | | 20 | example(char c, int i, unsigned u) |
21 | { | | 21 | { |
22 | long l; | | 22 | long long ll; |
23 | unsigned long ul; | | 23 | unsigned long long ull; |
24 | | | 24 | |
25 | l = c + i; | | 25 | ll = c + i; |
26 | l = i - c; | | 26 | ll = i - c; |
27 | ul = c * u; | | 27 | ull = c * u; |
28 | ul = u + c; | | 28 | ull = u + c; |
29 | ul = i - u; | | 29 | ull = i - u; |
30 | ul = u * i; | | 30 | ull = u * i; |
31 | l = i << c; | | 31 | ll = i << c; |
32 | | | 32 | |
33 | /* | | 33 | /* |
34 | * The operators SHR, DIV and MOD cannot produce an overflow, | | 34 | * The operators SHR, DIV and MOD cannot produce an overflow, |
35 | * therefore no warning is necessary for them. | | 35 | * therefore no warning is necessary for them. |
36 | */ | | 36 | */ |
37 | l = i >> c; | | 37 | ll = i >> c; |
38 | ul = u / c; | | 38 | ull = u / c; |
39 | ul = u % c; | | 39 | ull = u % c; |
40 | | | 40 | |
41 | /* | | 41 | /* |
42 | * Assigning the result of an increment or decrement operator to a | | 42 | * Assigning the result of an increment or decrement operator to a |
43 | * differently-sized type is no unusual that there is no need to warn | | 43 | * differently-sized type is no unusual that there is no need to warn |
44 | * about it. It's also more unlikely that there is an actual loss | | 44 | * about it. It's also more unlikely that there is an actual loss |
45 | * since this only happens for a single value of the old type, unlike | | 45 | * since this only happens for a single value of the old type, unlike |
46 | * "ul = u * u", which has many more possibilities for overflowing. | | 46 | * "ull = u * u", which has many more possibilities for overflowing. |
47 | */ | | 47 | */ |
48 | ul = u++; | | 48 | ull = u++; |
49 | ul = ++u; | | 49 | ull = ++u; |
50 | ul = u--; | | 50 | ull = u--; |
51 | ul = --u; | | 51 | ull = --u; |
52 | } | | 52 | } |