1 // RUN: %clang_cc1 %s -verify -fsyntax-only -std=gnu++98 -triple x86_64-pc-linux-gnu
2 typedef unsigned long long uint64_t;
3 typedef unsigned int uint32_t;
4
5 // Check integer sizes.
6 int array64[sizeof(uint64_t) == 8 ? 1 : -1];
7 int array32[sizeof(uint32_t) == 4 ? 1 : -1];
8 int arrayint[sizeof(int) < sizeof(uint64_t) ? 1 : -1];
9
10 uint64_t f0(uint64_t);
11 uint64_t f1(uint64_t, uint32_t);
12 uint64_t f2(uint64_t, ...);
13
14 static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
15
check_integer_overflows(int i)16 uint64_t check_integer_overflows(int i) { //expected-note {{declared here}}
17 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
18 uint64_t overflow = 4608 * 1024 * 1024,
19 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
20 overflow2 = (uint64_t)(4608 * 1024 * 1024),
21 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
22 overflow3 = (uint64_t)(4608 * 1024 * 1024 * i),
23 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
24 overflow4 = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL),
25 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
26 overflow5 = static_cast<uint64_t>(4608 * 1024 * 1024),
27 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
28 multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024));
29
30 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
31 overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024);
32 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
33 overflow += overflow2 = overflow3 = 4608 * 1024 * 1024;
34
35 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
36 overflow += overflow2 = overflow3 = static_cast<uint64_t>(4608 * 1024 * 1024);
37
38 uint64_t not_overflow = 4608 * 1024 * 1024ULL;
39 uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);
40
41 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
42 overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0;
43
44 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
45 overflow = 0 ? 0 : 4608 * 1024 * 1024;
46
47 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
48 if (4608 * 1024 * 1024)
49 return 0;
50
51 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
52 if ((uint64_t)(4608 * 1024 * 1024))
53 return 1;
54
55 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
56 if (static_cast<uint64_t>(4608 * 1024 * 1024))
57 return 1;
58
59 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
60 if ((uint64_t)(4608 * 1024 * 1024))
61 return 2;
62
63 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
64 if ((uint64_t)(4608 * 1024 * 1024 * i))
65 return 3;
66
67 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
68 if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
69 return 4;
70
71 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
72 if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)))
73 return 5;
74
75 switch (i) {
76 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
77 case 4608 * 1024 * 1024:
78 return 6;
79 // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
80 case (uint64_t)(4609 * 1024 * 1024):
81 return 7;
82 // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
83 case 1 + static_cast<uint64_t>(4609 * 1024 * 1024):
84 return 7;
85 // expected-error@+2 {{expression is not an integral constant expression}}
86 // expected-note@+1 {{read of non-const variable 'i' is not allowed in a constant expression}}
87 case ((uint64_t)(4608 * 1024 * 1024 * i)):
88 return 8;
89 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
90 case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
91 return 9;
92 // expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}}
93 // expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}
94 case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):
95 return 10;
96 }
97
98 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
99 while (4608 * 1024 * 1024);
100
101 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
102 while ((uint64_t)(4608 * 1024 * 1024));
103
104 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
105 while (static_cast<uint64_t>(4608 * 1024 * 1024));
106
107 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
108 while ((uint64_t)(4608 * 1024 * 1024));
109
110 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
111 while ((uint64_t)(4608 * 1024 * 1024 * i));
112
113 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
114 while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
115
116 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
117 while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
118
119 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
120 do { } while (4608 * 1024 * 1024);
121
122 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
123 do { } while ((uint64_t)(4608 * 1024 * 1024));
124
125 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
126 do { } while (static_cast<uint64_t>(4608 * 1024 * 1024));
127
128 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
129 do { } while ((uint64_t)(4608 * 1024 * 1024));
130
131 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
132 do { } while ((uint64_t)(4608 * 1024 * 1024 * i));
133
134 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
135 do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
136
137 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
138 do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
139
140 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
141 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
142 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
143 for (uint64_t i = 4608 * 1024 * 1024;
144 (uint64_t)(4608 * 1024 * 1024);
145 i += (uint64_t)(4608 * 1024 * 1024 * i));
146
147 // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
148 // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
149 // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
150 for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);
151 ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
152 i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));
153
154 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
155 _Complex long long x = 4608 * 1024 * 1024;
156
157 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
158 (__real__ x) = 4608 * 1024 * 1024;
159
160 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
161 (__imag__ x) = 4608 * 1024 * 1024;
162
163 // expected-warning@+4 {{overflow in expression; result is 536870912 with type 'int'}}
164 // expected-warning@+3 {{array index 536870912 is past the end of the array (which contains 10 elements)}}
165 // expected-note@+1 {{array 'a' declared here}}
166 uint64_t a[10];
167 a[4608 * 1024 * 1024] = 1i;
168
169 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
170 return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
171 }
172