blob: 91e428bc5b1caf4ca53a0a7cb7d56d7ea389d5f7 [file] [log] [blame]
John McCall18a2c2c2010-11-09 22:22:12 +00001// 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
7void test_6792488(void) {
8 int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}}
9}
John McCalld2a53122010-11-09 23:24:47 +000010
11void test_7809123(void) {
12 struct { int i5 : 5; } a;
13
John McCall0e6cfaf2010-11-09 23:36:43 +000014 a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}}
John McCalld2a53122010-11-09 23:24:47 +000015}
John McCallfd81c522010-11-10 00:26:50 +000016
17void test() {
18 struct { int bit : 1; } a;
19 a.bit = 1; // shouldn't warn
20}
John McCall817d4af2010-11-10 23:38:19 +000021
22enum Test2 { K_zero, K_one };
23enum Test2 test2(enum Test2 *t) {
24 *t = 20;
25 return 10; // shouldn't warn
26}
John McCall1f425642010-11-11 03:21:53 +000027
28void test3() {
29 struct A {
30 unsigned int foo : 2;
31 int bar : 2;
32 };
33
Eli Friedmanc267a322012-01-26 23:11:39 +000034 struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
35 struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
John McCall1f425642010-11-11 03:21:53 +000036 struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
37 struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
38 struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
39}
John McCalldeebbcf2010-11-11 05:33:51 +000040
41void test4() {
42 struct A {
43 char c : 2;
44 } a;
45
46 a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}}
47}
48
49void test5() {
50 struct A {
51 _Bool b : 1;
52 } a;
53
54 // Don't warn about this implicit conversion to bool, or at least
55 // don't warn about it just because it's a bitfield.
56 a.b = 100;
57}
Ted Kremenek33ba9952011-10-22 02:37:33 +000058
59void 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 Friedmanc267a322012-01-26 23:11:39 +000065void test7() {
66 struct {
67 unsigned int twoBits1:2;
68 unsigned int twoBits2:2;
Sam Panzer6fffec62013-03-28 19:07:11 +000069 unsigned int twoBits3:2;
70 unsigned int reserved:26;
Eli Friedmanc267a322012-01-26 23:11:39 +000071 } f;
72
73 f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}}
74 f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}}
Eli Friedman66b63952012-01-26 23:34:06 +000075 f.twoBits1 &= ~1; // no-warning
76 f.twoBits2 &= ~2; // no-warning
Sam Panzer6fffec62013-03-28 19:07:11 +000077 f.twoBits3 |= 4; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 4 to 0}}
78 f.twoBits3 += 4; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 4 to 0}}
79 f.twoBits3 *= 4; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 4 to 0}}
80 f.twoBits3 |= 1; // no-warning
Eli Friedmanc267a322012-01-26 23:11:39 +000081}
Eli Friedmane1ffd492012-02-02 00:40:20 +000082
83void test8() {
84 enum E { A, B, C };
85 struct { enum E x : 1; } f;
86 f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}}
87}
Sam Panzer6fffec62013-03-28 19:07:11 +000088
89int func(int);
90
91void test9() {
92 unsigned char x = 0;
93 unsigned char y = 0;
94 x = y | 0x1ff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
95 x = y | 0xff; // no-warning
96 x = y & 0xdff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 3583 to 255 if converted before operation}}
97 x = y & 0xff; // no-warning
98 x = y & ~1; // no-warning
99 x = 0x1ff | y; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
100 x = 0xff | y; // no-warning
101 x = (y | 0x1ff); // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
102 x = (y | 0xff); // no-warning
103 x = 0xff + y; // no-warning
104 x += 0x1ff; // expected-warning {{implicit conversion from 'int' to 'unsigned char' changes value from 511 to 255}}
105 x = 0xff - y; // no-warning
106 x -= 0x1ff; // expected-warning {{implicit conversion from 'int' to 'unsigned char' changes value from 511 to 255}}
107 x = y * 0x1ff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
108 x = y * 0xff; // no-warning
109 x *= 0x1ff; // expected-warning {{implicit conversion from 'int' to 'unsigned char' changes value from 511 to 255}}
110 x = y ^ 0xff; // no-warning
111 x ^= 0x1ff; // expected-warning {{implicit conversion from 'int' to 'unsigned char' changes value from 511 to 255}}
112 x = (func(1), 0x1ff); // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
113 x = (func(1), 0xff); // no-warning
114 x = 0xff << y; // no-warning
115 x = 0x1ff << y; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
116
117
118 // These next two tests make sure that both LHS and RHS are checked for
119 // narrowing operations.
120 x = 0x1ff | 0xff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
121 x = 0xff | 0x1ff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
122}