1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc // This file tests -Wconstant-conversion, a subcategory of -Wconversion
4*f4a2713aSLionel Sambuc // which is on by default.
5*f4a2713aSLionel Sambuc
6*f4a2713aSLionel Sambuc // rdar://problem/6792488
test_6792488(void)7*f4a2713aSLionel Sambuc void test_6792488(void) {
8*f4a2713aSLionel Sambuc int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}}
9*f4a2713aSLionel Sambuc }
10*f4a2713aSLionel Sambuc
test_7809123(void)11*f4a2713aSLionel Sambuc void test_7809123(void) {
12*f4a2713aSLionel Sambuc struct { int i5 : 5; } a;
13*f4a2713aSLionel Sambuc
14*f4a2713aSLionel Sambuc a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}}
15*f4a2713aSLionel Sambuc }
16*f4a2713aSLionel Sambuc
test()17*f4a2713aSLionel Sambuc void test() {
18*f4a2713aSLionel Sambuc struct { int bit : 1; } a;
19*f4a2713aSLionel Sambuc a.bit = 1; // shouldn't warn
20*f4a2713aSLionel Sambuc }
21*f4a2713aSLionel Sambuc
22*f4a2713aSLionel Sambuc enum Test2 { K_zero, K_one };
test2(enum Test2 * t)23*f4a2713aSLionel Sambuc enum Test2 test2(enum Test2 *t) {
24*f4a2713aSLionel Sambuc *t = 20;
25*f4a2713aSLionel Sambuc return 10; // shouldn't warn
26*f4a2713aSLionel Sambuc }
27*f4a2713aSLionel Sambuc
test3()28*f4a2713aSLionel Sambuc void test3() {
29*f4a2713aSLionel Sambuc struct A {
30*f4a2713aSLionel Sambuc unsigned int foo : 2;
31*f4a2713aSLionel Sambuc int bar : 2;
32*f4a2713aSLionel Sambuc };
33*f4a2713aSLionel Sambuc
34*f4a2713aSLionel Sambuc struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
35*f4a2713aSLionel Sambuc struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
36*f4a2713aSLionel Sambuc struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
37*f4a2713aSLionel Sambuc struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
38*f4a2713aSLionel Sambuc struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
39*f4a2713aSLionel Sambuc }
40*f4a2713aSLionel Sambuc
test4()41*f4a2713aSLionel Sambuc void test4() {
42*f4a2713aSLionel Sambuc struct A {
43*f4a2713aSLionel Sambuc char c : 2;
44*f4a2713aSLionel Sambuc } a;
45*f4a2713aSLionel Sambuc
46*f4a2713aSLionel Sambuc a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}}
47*f4a2713aSLionel Sambuc }
48*f4a2713aSLionel Sambuc
test5()49*f4a2713aSLionel Sambuc void test5() {
50*f4a2713aSLionel Sambuc struct A {
51*f4a2713aSLionel Sambuc _Bool b : 1;
52*f4a2713aSLionel Sambuc } a;
53*f4a2713aSLionel Sambuc
54*f4a2713aSLionel Sambuc // Don't warn about this implicit conversion to bool, or at least
55*f4a2713aSLionel Sambuc // don't warn about it just because it's a bitfield.
56*f4a2713aSLionel Sambuc a.b = 100;
57*f4a2713aSLionel Sambuc }
58*f4a2713aSLionel Sambuc
test6()59*f4a2713aSLionel Sambuc void test6() {
60*f4a2713aSLionel Sambuc // Test that unreachable code doesn't trigger the truncation warning.
61*f4a2713aSLionel Sambuc unsigned char x = 0 ? 65535 : 1; // no-warning
62*f4a2713aSLionel Sambuc unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}}
63*f4a2713aSLionel Sambuc }
64*f4a2713aSLionel Sambuc
test7()65*f4a2713aSLionel Sambuc void test7() {
66*f4a2713aSLionel Sambuc struct {
67*f4a2713aSLionel Sambuc unsigned int twoBits1:2;
68*f4a2713aSLionel Sambuc unsigned int twoBits2:2;
69*f4a2713aSLionel Sambuc unsigned int reserved:28;
70*f4a2713aSLionel Sambuc } f;
71*f4a2713aSLionel Sambuc
72*f4a2713aSLionel Sambuc f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}}
73*f4a2713aSLionel Sambuc f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}}
74*f4a2713aSLionel Sambuc f.twoBits1 &= ~1; // no-warning
75*f4a2713aSLionel Sambuc f.twoBits2 &= ~2; // no-warning
76*f4a2713aSLionel Sambuc }
77*f4a2713aSLionel Sambuc
test8()78*f4a2713aSLionel Sambuc void test8() {
79*f4a2713aSLionel Sambuc enum E { A, B, C };
80*f4a2713aSLionel Sambuc struct { enum E x : 1; } f;
81*f4a2713aSLionel Sambuc f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}}
82*f4a2713aSLionel Sambuc }
83