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