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