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