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;
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];
10 uint64_t f0(uint64_t);
11 uint64_t f1(uint64_t, uint32_t);
12 uint64_t f2(uint64_t, ...);
14 static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
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));
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;
35 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
36 overflow += overflow2 = overflow3 = static_cast<uint64_t>(4608 * 1024 * 1024);
38 uint64_t not_overflow = 4608 * 1024 * 1024ULL;
39 uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);
41 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
42 overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0;
44 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
45 overflow = 0 ? 0 : 4608 * 1024 * 1024;
47 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
48 if (4608 * 1024 * 1024)
51 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
52 if ((uint64_t)(4608 * 1024 * 1024))
55 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
56 if (static_cast<uint64_t>(4608 * 1024 * 1024))
59 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
60 if ((uint64_t)(4608 * 1024 * 1024))
63 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
64 if ((uint64_t)(4608 * 1024 * 1024 * i))
67 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
68 if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
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)))
76 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
77 case 4608 * 1024 * 1024:
79 // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
80 case (uint64_t)(4609 * 1024 * 1024):
82 // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
83 case 1 + static_cast<uint64_t>(4609 * 1024 * 1024):
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)):
89 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
90 case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
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))):
98 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
99 while (4608 * 1024 * 1024);
101 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
102 while ((uint64_t)(4608 * 1024 * 1024));
104 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
105 while (static_cast<uint64_t>(4608 * 1024 * 1024));
107 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
108 while ((uint64_t)(4608 * 1024 * 1024));
110 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
111 while ((uint64_t)(4608 * 1024 * 1024 * i));
113 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
114 while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
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)));
119 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
120 do { } while (4608 * 1024 * 1024);
122 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
123 do { } while ((uint64_t)(4608 * 1024 * 1024));
125 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
126 do { } while (static_cast<uint64_t>(4608 * 1024 * 1024));
128 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
129 do { } while ((uint64_t)(4608 * 1024 * 1024));
131 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
132 do { } while ((uint64_t)(4608 * 1024 * 1024 * i));
134 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
135 do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
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)));
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));
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))));
154 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
155 _Complex long long x = 4608 * 1024 * 1024;
157 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
158 (__real__ x) = 4608 * 1024 * 1024;
160 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
161 (__imag__ x) = 4608 * 1024 * 1024;
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}}
167 a[4608 * 1024 * 1024] = 1;
169 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
170 return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
173 void check_integer_overflows_in_function_calls() {
174 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
175 (void)f0(4608 * 1024 * 1024);
177 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
178 uint64_t x = f0(4608 * 1024 * 1024);
180 // expected-warning@+2 {{overflow in expression; result is 536870912 with type 'int'}}
181 uint64_t (*f0_ptr)(uint64_t) = &f0;
182 (void)(*f0_ptr)(4608 * 1024 * 1024);
184 // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
185 (void)f2(0, f0(4608 * 1024 * 1024));