Richard Trieu | 729c8e9 | 2016-04-11 20:37:04 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -verify -fsyntax-only -triple x86_64-pc-linux-gnu |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 2 | typedef unsigned long long uint64_t; |
Richard Trieu | 729c8e9 | 2016-04-11 20:37:04 +0000 | [diff] [blame] | 3 | typedef unsigned int uint32_t; |
| 4 | |
| 5 | // Check integer sizes. |
| 6 | int array64[sizeof(uint64_t) == 8 ? 1 : -1]; |
| 7 | int array32[sizeof(uint32_t) == 4 ? 1 : -1]; |
| 8 | int arrayint[sizeof(int) < sizeof(uint64_t) ? 1 : -1]; |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 9 | |
| 10 | uint64_t f0(uint64_t); |
| 11 | uint64_t f1(uint64_t, uint32_t); |
| 12 | uint64_t f2(uint64_t, ...); |
| 13 | |
| 14 | static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}} |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 15 | |
| 16 | uint64_t check_integer_overflows(int i) { |
| 17 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 18 | uint64_t overflow = 4608 * 1024 * 1024, |
| 19 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 20 | overflow2 = (uint64_t)(4608 * 1024 * 1024), |
| 21 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 22 | overflow3 = (uint64_t)(4608 * 1024 * 1024 * i), |
| 23 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 24 | overflow4 = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL), |
| 25 | // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} |
| 26 | multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)); |
| 27 | |
| 28 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 29 | overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024); |
| 30 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 31 | overflow += overflow2 = overflow3 = 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 ((uint64_t)(4608 * 1024 * 1024)) |
| 52 | return 2; |
| 53 | |
| 54 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 55 | if ((uint64_t)(4608 * 1024 * 1024 * i)) |
| 56 | return 3; |
| 57 | |
| 58 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 59 | if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)) |
| 60 | return 4; |
| 61 | |
| 62 | // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} |
| 63 | if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))) |
| 64 | return 5; |
| 65 | |
| 66 | switch (i) { |
| 67 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 68 | case 4608 * 1024 * 1024: |
| 69 | return 6; |
| 70 | // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}} |
| 71 | case (uint64_t)(4609 * 1024 * 1024): |
| 72 | return 7; |
| 73 | // expected-error@+1 {{expression is not an integer constant expression}} |
| 74 | case ((uint64_t)(4608 * 1024 * 1024 * i)): |
| 75 | return 8; |
| 76 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 77 | case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)): |
| 78 | return 9; |
| 79 | // expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}} |
| 80 | // expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}} |
| 81 | case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))): |
| 82 | return 10; |
| 83 | } |
| 84 | |
| 85 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 86 | while (4608 * 1024 * 1024); |
| 87 | |
| 88 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 89 | while ((uint64_t)(4608 * 1024 * 1024)); |
| 90 | |
| 91 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 92 | while ((uint64_t)(4608 * 1024 * 1024)); |
| 93 | |
| 94 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 95 | while ((uint64_t)(4608 * 1024 * 1024 * i)); |
| 96 | |
| 97 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 98 | while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)); |
| 99 | |
| 100 | // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} |
| 101 | while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))); |
| 102 | |
| 103 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 104 | do { } while (4608 * 1024 * 1024); |
| 105 | |
| 106 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 107 | do { } while ((uint64_t)(4608 * 1024 * 1024)); |
| 108 | |
| 109 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 110 | do { } while ((uint64_t)(4608 * 1024 * 1024)); |
| 111 | |
| 112 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 113 | do { } while ((uint64_t)(4608 * 1024 * 1024 * i)); |
| 114 | |
| 115 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 116 | do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)); |
| 117 | |
| 118 | // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} |
| 119 | do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))); |
| 120 | |
| 121 | // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}} |
| 122 | // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}} |
| 123 | // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}} |
| 124 | for (uint64_t i = 4608 * 1024 * 1024; |
| 125 | (uint64_t)(4608 * 1024 * 1024); |
| 126 | i += (uint64_t)(4608 * 1024 * 1024 * i)); |
| 127 | |
| 128 | // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}} |
| 129 | // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}} |
| 130 | // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}} |
| 131 | for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL); |
| 132 | ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))); |
| 133 | i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)))); |
| 134 | |
| 135 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 136 | _Complex long long x = 4608 * 1024 * 1024; |
| 137 | |
| 138 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 139 | (__real__ x) = 4608 * 1024 * 1024; |
| 140 | |
| 141 | // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} |
| 142 | (__imag__ x) = 4608 * 1024 * 1024; |
| 143 | |
| 144 | // expected-warning@+4 {{overflow in expression; result is 536870912 with type 'int'}} |
| 145 | // expected-warning@+3 {{array index 536870912 is past the end of the array (which contains 10 elements)}} |
| 146 | // expected-note@+1 {{array 'a' declared here}} |
| 147 | uint64_t a[10]; |
| 148 | a[4608 * 1024 * 1024] = 1i; |
| 149 | |
| 150 | // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} |
| 151 | return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))); |
| 152 | } |
Akira Hatanaka | f5c1361 | 2016-01-11 17:22:01 +0000 | [diff] [blame] | 153 | |
| 154 | struct s { |
| 155 | unsigned x; |
| 156 | unsigned y; |
| 157 | } s = { |
| 158 | .y = 5, |
| 159 | .x = 4 * 1024 * 1024 * 1024 // expected-warning {{overflow in expression; result is 0 with type 'int'}} |
| 160 | }; |
Akira Hatanaka | dfe2156f | 2016-02-10 06:06:06 +0000 | [diff] [blame] | 161 | |
| 162 | struct s2 { |
| 163 | unsigned a0; |
| 164 | |
| 165 | struct s3 { |
| 166 | unsigned a2; |
| 167 | |
| 168 | struct s4 { |
| 169 | unsigned a4; |
| 170 | } a3; |
| 171 | } a1; |
| 172 | } s2 = { |
| 173 | .a0 = 4 * 1024 * 1024 * 1024, // expected-warning {{overflow in expression; result is 0 with type 'int'}} |
| 174 | { |
| 175 | .a2 = 4 * 1024 * 1024 * 1024, // expected-warning {{overflow in expression; result is 0 with type 'int'}} |
| 176 | { |
| 177 | .a4 = 4 * 1024 * 1024 * 1024 // expected-warning {{overflow in expression; result is 0 with type 'int'}} |
| 178 | } |
| 179 | } |
| 180 | }; |