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