annotate gcc/testsuite/gcc.dg/overflow-warn-9.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* PR c/80731 - poor -Woverflow warnings, missing detail
kono
parents:
diff changeset
2 { dg-do compile }
kono
parents:
diff changeset
3 { dg-options "-Wconversion -Woverflow -Wno-override-init -std=c99" }
kono
parents:
diff changeset
4 { dg-require-effective-target int32plus } */
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 #include <limits.h>
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 struct Types
kono
parents:
diff changeset
9 {
kono
parents:
diff changeset
10 signed char sc;
kono
parents:
diff changeset
11 unsigned char uc;
kono
parents:
diff changeset
12 signed short ss;
kono
parents:
diff changeset
13 unsigned short us;
kono
parents:
diff changeset
14 signed int si;
kono
parents:
diff changeset
15 unsigned int ui;
kono
parents:
diff changeset
16 signed long sl;
kono
parents:
diff changeset
17 unsigned long ul;
kono
parents:
diff changeset
18 signed long long sll;
kono
parents:
diff changeset
19 unsigned long long ull;
kono
parents:
diff changeset
20 };
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 const struct Types t1 = {
kono
parents:
diff changeset
23 /* According to 6.3.1.3 of C11:
kono
parents:
diff changeset
24 -2- Otherwise, if the new type is unsigned, the value is converted
kono
parents:
diff changeset
25 by repeatedly adding or subtracting one more than the maximum
kono
parents:
diff changeset
26 value that can be represented in the new type until the value
kono
parents:
diff changeset
27 is in the range of the new type.
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 These conversions are diagnosed by -Wsign-conversion and -Wconversion,
kono
parents:
diff changeset
30 respectively, by mentioning "unsigned conversion" if the conversion
kono
parents:
diff changeset
31 results in sign change, and just "conversion" otherwise, as follows: */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 .uc = SCHAR_MIN, /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value from .-128. to .128." } */
kono
parents:
diff changeset
34 .uc = -1, /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value from .-1. to .255." } */
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 .uc = UCHAR_MAX + 1, /* { dg-warning "conversion from 'int' to 'unsigned char' changes value from .256. to .0." } */
kono
parents:
diff changeset
37 .uc = UCHAR_MAX * 2, /* { dg-warning "conversion from 'int' to 'unsigned char' changes value from .510. to .254." } */
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 /* According to 6.3.1.3 of C11:
kono
parents:
diff changeset
40 -3- Otherwise, the new type is signed and the value cannot be
kono
parents:
diff changeset
41 represented in it; either the result is implementation-defined
kono
parents:
diff changeset
42 or an implementation-defined signal is raised.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 In GCC such conversions wrap and are diagnosed by mentioning "overflow"
kono
parents:
diff changeset
45 if the absolute value of the operand is in excess of the maximum of
kono
parents:
diff changeset
46 the destination of type, and "conversion" otherwise, as follows: */
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 .sc = SCHAR_MAX + 1, /* { dg-warning "conversion from .int. to .signed char. changes value from .128. to .-128." } */
kono
parents:
diff changeset
49 .sc = SCHAR_MAX + 2, /* { dg-warning "conversion from .int. to .signed char. changes value from .129. to .-127." } */
kono
parents:
diff changeset
50 .sc = SCHAR_MAX * 2, /* { dg-warning "conversion from .int. to .signed char. changes value from .254. to .-2." } */
kono
parents:
diff changeset
51 .sc = SCHAR_MAX * 2 + 3, /* { dg-warning "conversion from .int. to .signed char. changes value from .257. to .1." } */
kono
parents:
diff changeset
52 .sc = SCHAR_MAX * 3 + 3, /* { dg-warning "conversion from .int. to .signed char. changes value from .384. to .-128." } */
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 .ss = SHRT_MAX + 1, /* { dg-warning "conversion from 'int' to 'short int' changes value from .32768. to .-32768." } */
kono
parents:
diff changeset
56 .us = USHRT_MAX + 1, /* { dg-warning "unsigned conversion from .int. to .short unsigned int. changes value from .65536. to .0." } */
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 .si = INT_MAX + 1LU, /* { dg-warning "signed conversion from 'long unsigned int. to 'int' changes value from .2147483648. to .-2147483648." } */
kono
parents:
diff changeset
59 .ui = UINT_MAX + 1L, /* { dg-warning "signed conversion from .long int. to .unsigned int. changes value from .4294967296. to .0." "lp64" { target lp64 } } */
kono
parents:
diff changeset
60 .ui = UINT_MAX + 1LU, /* { dg-warning "conversion from .long unsigned int. to .unsigned int. changes value from .4294967296. to .0." "lp64" { target lp64 } } */
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 .sl = LONG_MAX + 1LU, /* { dg-warning "signed conversion from .long unsigned int. to .long int. changes value from .9223372036854775808. to .-9223372036854775808." "lp64" { target lp64 } } */
kono
parents:
diff changeset
63 /* { dg-warning "signed conversion from .long unsigned int. to .long int. changes value from .2147483648. to .-2147483648." "ilp32" { target ilp32 } .-1 } */
kono
parents:
diff changeset
64 .ul = ULONG_MAX + 1LU /* there should be some warning here */
kono
parents:
diff changeset
65 };