John McCall | d5c376e | 2009-11-07 03:30:38 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only -verify -Wconversion -triple x86_64-apple-darwin %s |
| 2 | |
| 3 | #define BIG 0x7f7f7f7f7f7f7f7fL |
| 4 | |
| 5 | void test0(char c, short s, int i, long l, long long ll) { |
| 6 | c = c; |
| 7 | c = s; // expected-warning {{implicit cast loses integer precision}} |
| 8 | c = i; // expected-warning {{implicit cast loses integer precision}} |
| 9 | c = l; // expected-warning {{implicit cast loses integer precision}} |
| 10 | s = c; |
| 11 | s = s; |
| 12 | s = i; // expected-warning {{implicit cast loses integer precision}} |
| 13 | s = l; // expected-warning {{implicit cast loses integer precision}} |
| 14 | i = c; |
| 15 | i = s; |
| 16 | i = i; |
| 17 | i = l; // expected-warning {{implicit cast loses integer precision}} |
| 18 | l = c; |
| 19 | l = s; |
| 20 | l = i; |
| 21 | l = l; |
| 22 | |
| 23 | c = (char) 0; |
| 24 | c = (short) 0; |
| 25 | c = (int) 0; |
| 26 | c = (long) 0; |
| 27 | s = (char) 0; |
| 28 | s = (short) 0; |
| 29 | s = (int) 0; |
| 30 | s = (long) 0; |
| 31 | i = (char) 0; |
| 32 | i = (short) 0; |
| 33 | i = (int) 0; |
| 34 | i = (long) 0; |
| 35 | l = (char) 0; |
| 36 | l = (short) 0; |
| 37 | l = (int) 0; |
| 38 | l = (long) 0; |
| 39 | |
| 40 | c = (char) BIG; |
| 41 | c = (short) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 42 | c = (int) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 43 | c = (long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 44 | s = (char) BIG; |
| 45 | s = (short) BIG; |
| 46 | s = (int) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 47 | s = (long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 48 | i = (char) BIG; |
| 49 | i = (short) BIG; |
| 50 | i = (int) BIG; |
| 51 | i = (long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 52 | l = (char) BIG; |
| 53 | l = (short) BIG; |
| 54 | l = (int) BIG; |
| 55 | l = (long) BIG; |
| 56 | } |
| 57 | |
| 58 | char test1(long long ll) { |
| 59 | return (long long) ll; // expected-warning {{implicit cast loses integer precision}} |
| 60 | return (long) ll; // expected-warning {{implicit cast loses integer precision}} |
| 61 | return (int) ll; // expected-warning {{implicit cast loses integer precision}} |
| 62 | return (short) ll; // expected-warning {{implicit cast loses integer precision}} |
| 63 | return (char) ll; |
| 64 | return (long long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 65 | return (long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 66 | return (int) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 67 | return (short) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 68 | return (char) BIG; |
| 69 | } |
| 70 | |
| 71 | short test2(long long ll) { |
| 72 | return (long long) ll; // expected-warning {{implicit cast loses integer precision}} |
| 73 | return (long) ll; // expected-warning {{implicit cast loses integer precision}} |
| 74 | return (int) ll; // expected-warning {{implicit cast loses integer precision}} |
| 75 | return (short) ll; |
| 76 | return (char) ll; |
| 77 | return (long long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 78 | return (long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 79 | return (int) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 80 | return (short) BIG; |
| 81 | return (char) BIG; |
| 82 | } |
| 83 | |
| 84 | int test3(long long ll) { |
| 85 | return (long long) ll; // expected-warning {{implicit cast loses integer precision}} |
| 86 | return (long) ll; // expected-warning {{implicit cast loses integer precision}} |
| 87 | return (int) ll; |
| 88 | return (short) ll; |
| 89 | return (char) ll; |
| 90 | return (long long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 91 | return (long) BIG; // expected-warning {{implicit cast loses integer precision}} |
| 92 | return (int) BIG; |
| 93 | return (short) BIG; |
| 94 | return (char) BIG; |
| 95 | } |
| 96 | |
| 97 | long test4(long long ll) { |
| 98 | return (long long) ll; |
| 99 | return (long) ll; |
| 100 | return (int) ll; |
| 101 | return (short) ll; |
| 102 | return (char) ll; |
| 103 | return (long long) BIG; |
| 104 | return (long) BIG; |
| 105 | return (int) BIG; |
| 106 | return (short) BIG; |
| 107 | return (char) BIG; |
| 108 | } |
| 109 | |
| 110 | long long test5(long long ll) { |
| 111 | return (long long) ll; |
| 112 | return (long) ll; |
| 113 | return (int) ll; |
| 114 | return (short) ll; |
| 115 | return (char) ll; |
| 116 | return (long long) BIG; |
| 117 | return (long) BIG; |
| 118 | return (int) BIG; |
| 119 | return (short) BIG; |
| 120 | return (char) BIG; |
| 121 | } |
| 122 | |
| 123 | void takes_char(char); |
| 124 | void takes_short(short); |
| 125 | void takes_int(int); |
| 126 | void takes_long(long); |
| 127 | void takes_longlong(long long); |
| 128 | void takes_float(float); |
| 129 | void takes_double(double); |
| 130 | void takes_longdouble(long double); |
| 131 | |
| 132 | void test6(char v) { |
| 133 | takes_char(v); |
| 134 | takes_short(v); |
| 135 | takes_int(v); |
| 136 | takes_long(v); |
| 137 | takes_longlong(v); |
| 138 | takes_float(v); |
| 139 | takes_double(v); |
| 140 | takes_longdouble(v); |
| 141 | } |
| 142 | |
| 143 | void test7(short v) { |
| 144 | takes_char(v); // expected-warning {{implicit cast loses integer precision}} |
| 145 | takes_short(v); |
| 146 | takes_int(v); |
| 147 | takes_long(v); |
| 148 | takes_longlong(v); |
| 149 | takes_float(v); |
| 150 | takes_double(v); |
| 151 | takes_longdouble(v); |
| 152 | } |
| 153 | |
| 154 | void test8(int v) { |
| 155 | takes_char(v); // expected-warning {{implicit cast loses integer precision}} |
| 156 | takes_short(v); // expected-warning {{implicit cast loses integer precision}} |
| 157 | takes_int(v); |
| 158 | takes_long(v); |
| 159 | takes_longlong(v); |
| 160 | takes_float(v); |
| 161 | takes_double(v); |
| 162 | takes_longdouble(v); |
| 163 | } |
| 164 | |
| 165 | void test9(long v) { |
| 166 | takes_char(v); // expected-warning {{implicit cast loses integer precision}} |
| 167 | takes_short(v); // expected-warning {{implicit cast loses integer precision}} |
| 168 | takes_int(v); // expected-warning {{implicit cast loses integer precision}} |
| 169 | takes_long(v); |
| 170 | takes_longlong(v); |
| 171 | takes_float(v); |
| 172 | takes_double(v); |
| 173 | takes_longdouble(v); |
| 174 | } |
| 175 | |
| 176 | void test10(long long v) { |
| 177 | takes_char(v); // expected-warning {{implicit cast loses integer precision}} |
| 178 | takes_short(v); // expected-warning {{implicit cast loses integer precision}} |
| 179 | takes_int(v); // expected-warning {{implicit cast loses integer precision}} |
| 180 | takes_long(v); |
| 181 | takes_longlong(v); |
| 182 | takes_float(v); |
| 183 | takes_double(v); |
| 184 | takes_longdouble(v); |
| 185 | } |
| 186 | |
| 187 | void test11(float v) { |
| 188 | takes_char(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 189 | takes_short(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 190 | takes_int(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 191 | takes_long(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 192 | takes_longlong(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 193 | takes_float(v); |
| 194 | takes_double(v); |
| 195 | takes_longdouble(v); |
| 196 | } |
| 197 | |
| 198 | void test12(double v) { |
| 199 | takes_char(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 200 | takes_short(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 201 | takes_int(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 202 | takes_long(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 203 | takes_longlong(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 204 | takes_float(v); // expected-warning {{implicit cast loses floating-point precision}} |
| 205 | takes_double(v); |
| 206 | takes_longdouble(v); |
| 207 | } |
| 208 | |
| 209 | void test13(long double v) { |
| 210 | takes_char(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 211 | takes_short(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 212 | takes_int(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 213 | takes_long(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 214 | takes_longlong(v); // expected-warning {{implicit cast turns floating-point number into integer}} |
| 215 | takes_float(v); // expected-warning {{implicit cast loses floating-point precision}} |
| 216 | takes_double(v); // expected-warning {{implicit cast loses floating-point precision}} |
| 217 | takes_longdouble(v); |
| 218 | } |
| 219 | |
| 220 | void test14(long l) { |
| 221 | // Fine because of the boolean whitelist. |
| 222 | char c; |
| 223 | c = (l == 4); |
| 224 | c = ((l <= 4) && (l >= 0)); |
| 225 | c = ((l <= 4) && (l >= 0)) || (l > 20); |
| 226 | } |
John McCall | e8babd1 | 2009-11-07 08:15:46 +0000 | [diff] [blame] | 227 | |
| 228 | void test15(char c) { |
| 229 | c = c + 1 + c * 2; |
| 230 | c = (short) c + 1 + c * 2; // expected-warning {{implicit cast loses integer precision}} |
| 231 | } |
John McCall | 8406aed | 2009-11-11 22:52:37 +0000 | [diff] [blame] | 232 | |
| 233 | // PR 5422 |
| 234 | extern void *test16_external; |
| 235 | void test16(void) { |
| 236 | int a = (unsigned long) &test16_external; // expected-warning {{implicit cast loses integer precision}} |
| 237 | } |