218 lines · c
1// RUN: %clang_cc1 %s -verify -fsyntax-only -triple x86_64-pc-linux-gnu2// RUN: %clang_cc1 %s -verify -fsyntax-only -triple x86_64-pc-linux-gnu -fexperimental-new-constant-interpreter3typedef unsigned long long uint64_t;4typedef unsigned int uint32_t;5 6// Check integer sizes.7int array64[sizeof(uint64_t) == 8 ? 1 : -1];8int array32[sizeof(uint32_t) == 4 ? 1 : -1];9int arrayint[sizeof(int) < sizeof(uint64_t) ? 1 : -1];10 11uint64_t f0(uint64_t);12uint64_t f1(uint64_t, uint32_t);13uint64_t f2(uint64_t, ...);14 15static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536'870'912 with type 'int'}}16 17uint64_t check_integer_overflows(int i) {18// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}19 uint64_t overflow = 4608 * 1024 * 1024,20// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}21 overflow2 = (uint64_t)(4608 * 1024 * 1024),22// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}23 overflow3 = (uint64_t)(4608 * 1024 * 1024 * i),24// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}25 overflow4 = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL),26// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}27 multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024));28 29// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}30 overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024);31// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}32 overflow += overflow2 = overflow3 = 4608 * 1024 * 1024;33 34 uint64_t not_overflow = 4608 * 1024 * 1024ULL;35 uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);36 37// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}38 overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0;39 40// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}41 overflow = 0 ? 0 : 4608 * 1024 * 1024;42 43// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}44 if (4608 * 1024 * 1024)45 return 0;46 47// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}48 if ((uint64_t)(4608 * 1024 * 1024))49 return 1;50 51// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}52 if ((uint64_t)(4608 * 1024 * 1024))53 return 2;54 55// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}56 if ((uint64_t)(4608 * 1024 * 1024 * i))57 return 3;58 59// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}60 if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))61 return 4;62 63// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}64 if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)))65 return 5;66 67 switch (i) {68// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}69 case 4608 * 1024 * 1024:70 return 6;71// expected-warning@+1 {{overflow in expression; result is 537'919'488 with type 'int'}}72 case (uint64_t)(4609 * 1024 * 1024):73 return 7;74// expected-error@+1 {{expression is not an integer constant expression}}75 case ((uint64_t)(4608 * 1024 * 1024 * i)):76 return 8;77// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}78 case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):79 return 9;80// expected-warning@+2 2{{overflow in expression; result is 536'870'912 with type 'int'}}81// expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}82 case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):83 return 10;84 }85 86// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}87 while (4608 * 1024 * 1024);88 89// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}90 while ((uint64_t)(4608 * 1024 * 1024));91 92// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}93 while ((uint64_t)(4608 * 1024 * 1024));94 95// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}96 while ((uint64_t)(4608 * 1024 * 1024 * i));97 98// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}99 while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));100 101// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}102 while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));103 104// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}105 do { } while (4608 * 1024 * 1024);106 107// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}108 do { } while ((uint64_t)(4608 * 1024 * 1024));109 110// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}111 do { } while ((uint64_t)(4608 * 1024 * 1024));112 113// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}114 do { } while ((uint64_t)(4608 * 1024 * 1024 * i));115 116// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}117 do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));118 119// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}120 do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));121 122// expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}123// expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}124// expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}125 for (uint64_t i = 4608 * 1024 * 1024;126 (uint64_t)(4608 * 1024 * 1024);127 i += (uint64_t)(4608 * 1024 * 1024 * i));128 129// expected-warning@+3 {{overflow in expression; result is 536'870'912 with type 'int'}}130// expected-warning@+3 2{{overflow in expression; result is 536'870'912 with type 'int'}}131// expected-warning@+3 2{{overflow in expression; result is 536'870'912 with type 'int'}}132 for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);133 ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));134 i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));135 136// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}137 _Complex long long x = 4608 * 1024 * 1024;138 139// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}140 (__real__ x) = 4608 * 1024 * 1024;141 142// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}143 (__imag__ x) = 4608 * 1024 * 1024;144 145// expected-warning@+4 {{overflow in expression; result is 536'870'912 with type 'int'}}146// expected-warning@+3 {{array index 536'870'912 is past the end of the array (that has type 'uint64_t[10]' (aka 'unsigned long long[10]'))}}147// expected-note@+1 {{array 'a' declared here}}148 uint64_t a[10];149 a[4608 * 1024 * 1024] = 1i;150 151// expected-warning@+2 {{overflow in expression; result is 536'870'912 with type 'int'}}152 uint64_t *b;153 uint64_t b2 = b[4608 * 1024 * 1024] + 1;154 155// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}156 (void)((i ? (4608 * 1024 * 1024) : (4608 * 1024 * 1024)) + 1);157 158// expected-warning@+1 2{{overflow in expression; result is 536'870'912 with type 'int'}}159 return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));160}161 162void check_integer_overflows_in_function_calls(void) {163// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}164 (void)f0(4608 * 1024 * 1024);165 166// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}167 uint64_t x = f0(4608 * 1024 * 1024);168 169// expected-warning@+2 {{overflow in expression; result is 536'870'912 with type 'int'}}170 uint64_t (*f0_ptr)(uint64_t) = &f0;171 (void)(*f0_ptr)(4608 * 1024 * 1024);172 173// expected-warning@+1 {{overflow in expression; result is 536'870'912 with type 'int'}}174 (void)f2(0, f0(4608 * 1024 * 1024));175}176void check_integer_overflows_in_array_size(void) {177 int arr[4608 * 1024 * 1024]; // expected-warning {{overflow in expression; result is 536'870'912 with type 'int'}}178 // expected-warning@-1 {{variable length array folded to constant array as an extension}}179 // expected-note@-2 {{value 4831838208 is outside the range of representable values of type 'int'}}180}181 182struct s {183 unsigned x;184 unsigned y;185} s = {186 .y = 5,187 .x = 4 * 1024 * 1024 * 1024 // expected-warning {{overflow in expression; result is 0 with type 'int'}}188};189 190struct s2 {191 unsigned a0;192 193 struct s3 {194 unsigned a2;195 196 struct s4 {197 unsigned a4;198 } a3;199 } a1;200} s2 = {201 .a0 = 4 * 1024 * 1024 * 1024, // expected-warning {{overflow in expression; result is 0 with type 'int'}}202 {203 .a2 = 4 * 1024 * 1024 * 1024, // expected-warning {{overflow in expression; result is 0 with type 'int'}}204 {205 .a4 = 4 * 1024 * 1024 * 1024 // expected-warning {{overflow in expression; result is 0 with type 'int'}}206 }207 }208};209 210void PR49619(void) {211 int n;212 n = ({213 while (1)214 ;215 0;216 });217}218