| John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s |
| 2 | |
| 3 | // This file tests -Wconstant-conversion, a subcategory of -Wconversion |
| 4 | // which is on by default. |
| 5 | |
| 6 | // rdar://problem/6792488 |
| 7 | void test_6792488(void) { |
| 8 | int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}} |
| 9 | } |
| John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 10 | |
| 11 | void test_7809123(void) { |
| 12 | struct { int i5 : 5; } a; |
| 13 | |
| Chandler Carruth | cbe1eba | 2016-12-20 02:43:58 +0000 | [diff] [blame] | 14 | a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 36 to 4}} |
| John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 15 | } |
| John McCall | fd81c52 | 2010-11-10 00:26:50 +0000 | [diff] [blame] | 16 | |
| 17 | void test() { |
| 18 | struct { int bit : 1; } a; |
| 19 | a.bit = 1; // shouldn't warn |
| 20 | } |
| John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 21 | |
| 22 | enum Test2 { K_zero, K_one }; |
| 23 | enum Test2 test2(enum Test2 *t) { |
| 24 | *t = 20; |
| 25 | return 10; // shouldn't warn |
| 26 | } |
| John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 27 | |
| 28 | void test3() { |
| 29 | struct A { |
| 30 | unsigned int foo : 2; |
| 31 | int bar : 2; |
| 32 | }; |
| 33 | |
| Chandler Carruth | cbe1eba | 2016-12-20 02:43:58 +0000 | [diff] [blame] | 34 | struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to -2}} |
| 35 | struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to -2}} |
| 36 | struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}} |
| 37 | struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}} |
| 38 | struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}} |
| John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 39 | } |
| John McCall | deebbcf | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 40 | |
| 41 | void test4() { |
| 42 | struct A { |
| 43 | char c : 2; |
| 44 | } a; |
| 45 | |
| Chandler Carruth | cbe1eba | 2016-12-20 02:43:58 +0000 | [diff] [blame] | 46 | a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 257 to 1}} |
| John McCall | deebbcf | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void test5() { |
| 50 | struct A { |
| 51 | _Bool b : 1; |
| 52 | } a; |
| 53 | |
| 54 | // Don't warn about this implicit conversion to bool, or at least |
| Chandler Carruth | cbe1eba | 2016-12-20 02:43:58 +0000 | [diff] [blame] | 55 | // don't warn about it just because it's a bit-field. |
| John McCall | deebbcf | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 56 | a.b = 100; |
| 57 | } |
| Ted Kremenek | 33ba995 | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 58 | |
| 59 | void test6() { |
| 60 | // Test that unreachable code doesn't trigger the truncation warning. |
| 61 | unsigned char x = 0 ? 65535 : 1; // no-warning |
| 62 | unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}} |
| 63 | } |
| 64 | |
| Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 65 | void test7() { |
| 66 | struct { |
| 67 | unsigned int twoBits1:2; |
| 68 | unsigned int twoBits2:2; |
| Timur Iskhodzhanov | 554bdc6 | 2013-03-29 00:22:03 +0000 | [diff] [blame] | 69 | unsigned int reserved:28; |
| Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 70 | } f; |
| 71 | |
| Daniel Marjamaki | ee5b5f5 | 2016-09-22 14:13:46 +0000 | [diff] [blame] | 72 | f.twoBits1 = ~0; // no-warning |
| 73 | f.twoBits1 = ~1; // no-warning |
| Chandler Carruth | cbe1eba | 2016-12-20 02:43:58 +0000 | [diff] [blame] | 74 | f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -3 to 1}} |
| Eli Friedman | 66b6395 | 2012-01-26 23:34:06 +0000 | [diff] [blame] | 75 | f.twoBits1 &= ~1; // no-warning |
| 76 | f.twoBits2 &= ~2; // no-warning |
| Eli Friedman | c267a32 | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 77 | } |
| Eli Friedman | e1ffd49 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 78 | |
| 79 | void test8() { |
| 80 | enum E { A, B, C }; |
| 81 | struct { enum E x : 1; } f; |
| Chandler Carruth | cbe1eba | 2016-12-20 02:43:58 +0000 | [diff] [blame] | 82 | f.x = C; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 2 to 0}} |
| Eli Friedman | e1ffd49 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 83 | } |
| Richard Trieu | dcb5557 | 2016-01-29 23:51:16 +0000 | [diff] [blame] | 84 | |
| 85 | void test9() { |
| 86 | const char max_char = 0x7F; |
| 87 | const short max_short = 0x7FFF; |
| 88 | const int max_int = 0x7FFFFFFF; |
| 89 | |
| 90 | const short max_char_plus_one = (short)max_char + 1; |
| 91 | const int max_short_plus_one = (int)max_short + 1; |
| 92 | const long max_int_plus_one = (long)max_int + 1; |
| 93 | |
| 94 | char new_char = max_char_plus_one; // expected-warning {{implicit conversion from 'const short' to 'char' changes value from 128 to -128}} |
| 95 | short new_short = max_short_plus_one; // expected-warning {{implicit conversion from 'const int' to 'short' changes value from 32768 to -32768}} |
| 96 | int new_int = max_int_plus_one; // expected-warning {{implicit conversion from 'const long' to 'int' changes value from 2147483648 to -2147483648}} |
| 97 | |
| 98 | char hex_char = 0x80; |
| 99 | short hex_short = 0x8000; |
| 100 | int hex_int = 0x80000000; |
| 101 | |
| 102 | char oct_char = 0200; |
| 103 | short oct_short = 0100000; |
| 104 | int oct_int = 020000000000; |
| 105 | |
| 106 | char bin_char = 0b10000000; |
| 107 | short bin_short = 0b1000000000000000; |
| 108 | int bin_int = 0b10000000000000000000000000000000; |
| 109 | |
| 110 | #define CHAR_MACRO_HEX 0xff |
| 111 | char macro_char_hex = CHAR_MACRO_HEX; |
| 112 | #define CHAR_MACRO_DEC 255 |
| 113 | char macro_char_dec = CHAR_MACRO_DEC; // expected-warning {{implicit conversion from 'int' to 'char' changes value from 255 to -1}} |
| Richard Trieu | fc404c7 | 2016-02-05 23:02:38 +0000 | [diff] [blame] | 114 | |
| 115 | char array_init[] = { 255, 127, 128, 129, 0 }; |
| Richard Trieu | dcb5557 | 2016-01-29 23:51:16 +0000 | [diff] [blame] | 116 | } |
| Richard Trieu | 7561ed0 | 2016-08-05 02:39:30 +0000 | [diff] [blame] | 117 | |
| Daniel Marjamaki | ee5b5f5 | 2016-09-22 14:13:46 +0000 | [diff] [blame] | 118 | #define A 1 |
| 119 | |
| Richard Trieu | 7561ed0 | 2016-08-05 02:39:30 +0000 | [diff] [blame] | 120 | void test10() { |
| 121 | struct S { |
| 122 | unsigned a : 4; |
| 123 | } s; |
| 124 | s.a = -1; |
| 125 | s.a = 15; |
| 126 | s.a = -8; |
| Daniel Marjamaki | ee5b5f5 | 2016-09-22 14:13:46 +0000 | [diff] [blame] | 127 | s.a = ~0; |
| 128 | s.a = ~0U; |
| 129 | s.a = ~(1<<A); |
| Richard Trieu | 7561ed0 | 2016-08-05 02:39:30 +0000 | [diff] [blame] | 130 | |
| Chandler Carruth | cbe1eba | 2016-12-20 02:43:58 +0000 | [diff] [blame] | 131 | s.a = -9; // expected-warning{{implicit truncation from 'int' to bit-field changes value from -9 to 7}} |
| 132 | s.a = 16; // expected-warning{{implicit truncation from 'int' to bit-field changes value from 16 to 0}} |
| Richard Trieu | 7561ed0 | 2016-08-05 02:39:30 +0000 | [diff] [blame] | 133 | } |