xref: /aosp_15_r20/external/clang/test/Sema/constant-conversion.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s
2*67e74705SXin Li 
3*67e74705SXin Li // This file tests -Wconstant-conversion, a subcategory of -Wconversion
4*67e74705SXin Li // which is on by default.
5*67e74705SXin Li 
6*67e74705SXin Li // rdar://problem/6792488
test_6792488(void)7*67e74705SXin Li void test_6792488(void) {
8*67e74705SXin Li   int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}}
9*67e74705SXin Li }
10*67e74705SXin Li 
test_7809123(void)11*67e74705SXin Li void test_7809123(void) {
12*67e74705SXin Li   struct { int i5 : 5; } a;
13*67e74705SXin Li 
14*67e74705SXin Li   a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}}
15*67e74705SXin Li }
16*67e74705SXin Li 
test()17*67e74705SXin Li void test() {
18*67e74705SXin Li   struct { int bit : 1; } a;
19*67e74705SXin Li   a.bit = 1; // shouldn't warn
20*67e74705SXin Li }
21*67e74705SXin Li 
22*67e74705SXin Li enum Test2 { K_zero, K_one };
test2(enum Test2 * t)23*67e74705SXin Li enum Test2 test2(enum Test2 *t) {
24*67e74705SXin Li   *t = 20;
25*67e74705SXin Li   return 10; // shouldn't warn
26*67e74705SXin Li }
27*67e74705SXin Li 
test3()28*67e74705SXin Li void test3() {
29*67e74705SXin Li   struct A {
30*67e74705SXin Li     unsigned int foo : 2;
31*67e74705SXin Li     int bar : 2;
32*67e74705SXin Li   };
33*67e74705SXin Li 
34*67e74705SXin Li   struct A a = { 0, 10 };            // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
35*67e74705SXin Li   struct A b[] = { 0, 10, 0, 0 };    // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
36*67e74705SXin Li   struct A c[] = {{10, 0}};          // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
37*67e74705SXin Li   struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
38*67e74705SXin Li   struct A e = { .foo = 10 };        // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
39*67e74705SXin Li }
40*67e74705SXin Li 
test4()41*67e74705SXin Li void test4() {
42*67e74705SXin Li   struct A {
43*67e74705SXin Li     char c : 2;
44*67e74705SXin Li   } a;
45*67e74705SXin Li 
46*67e74705SXin Li   a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}}
47*67e74705SXin Li }
48*67e74705SXin Li 
test5()49*67e74705SXin Li void test5() {
50*67e74705SXin Li   struct A {
51*67e74705SXin Li     _Bool b : 1;
52*67e74705SXin Li   } a;
53*67e74705SXin Li 
54*67e74705SXin Li   // Don't warn about this implicit conversion to bool, or at least
55*67e74705SXin Li   // don't warn about it just because it's a bitfield.
56*67e74705SXin Li   a.b = 100;
57*67e74705SXin Li }
58*67e74705SXin Li 
test6()59*67e74705SXin Li void test6() {
60*67e74705SXin Li   // Test that unreachable code doesn't trigger the truncation warning.
61*67e74705SXin Li   unsigned char x = 0 ? 65535 : 1; // no-warning
62*67e74705SXin Li   unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}}
63*67e74705SXin Li }
64*67e74705SXin Li 
test7()65*67e74705SXin Li void test7() {
66*67e74705SXin Li 	struct {
67*67e74705SXin Li 		unsigned int twoBits1:2;
68*67e74705SXin Li 		unsigned int twoBits2:2;
69*67e74705SXin Li 		unsigned int reserved:28;
70*67e74705SXin Li 	} f;
71*67e74705SXin Li 
72*67e74705SXin Li 	f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}}
73*67e74705SXin Li 	f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}}
74*67e74705SXin Li 	f.twoBits1 &= ~1; // no-warning
75*67e74705SXin Li 	f.twoBits2 &= ~2; // no-warning
76*67e74705SXin Li }
77*67e74705SXin Li 
test8()78*67e74705SXin Li void test8() {
79*67e74705SXin Li   enum E { A, B, C };
80*67e74705SXin Li   struct { enum E x : 1; } f;
81*67e74705SXin Li   f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}}
82*67e74705SXin Li }
83*67e74705SXin Li 
test9()84*67e74705SXin Li void test9() {
85*67e74705SXin Li   const char max_char = 0x7F;
86*67e74705SXin Li   const short max_short = 0x7FFF;
87*67e74705SXin Li   const int max_int = 0x7FFFFFFF;
88*67e74705SXin Li 
89*67e74705SXin Li   const short max_char_plus_one = (short)max_char + 1;
90*67e74705SXin Li   const int max_short_plus_one = (int)max_short + 1;
91*67e74705SXin Li   const long max_int_plus_one = (long)max_int + 1;
92*67e74705SXin Li 
93*67e74705SXin Li   char new_char = max_char_plus_one;  // expected-warning {{implicit conversion from 'const short' to 'char' changes value from 128 to -128}}
94*67e74705SXin Li   short new_short = max_short_plus_one;  // expected-warning {{implicit conversion from 'const int' to 'short' changes value from 32768 to -32768}}
95*67e74705SXin Li   int new_int = max_int_plus_one;  // expected-warning {{implicit conversion from 'const long' to 'int' changes value from 2147483648 to -2147483648}}
96*67e74705SXin Li 
97*67e74705SXin Li   char hex_char = 0x80;
98*67e74705SXin Li   short hex_short = 0x8000;
99*67e74705SXin Li   int hex_int = 0x80000000;
100*67e74705SXin Li 
101*67e74705SXin Li   char oct_char = 0200;
102*67e74705SXin Li   short oct_short = 0100000;
103*67e74705SXin Li   int oct_int = 020000000000;
104*67e74705SXin Li 
105*67e74705SXin Li   char bin_char = 0b10000000;
106*67e74705SXin Li   short bin_short = 0b1000000000000000;
107*67e74705SXin Li   int bin_int = 0b10000000000000000000000000000000;
108*67e74705SXin Li 
109*67e74705SXin Li #define CHAR_MACRO_HEX 0xff
110*67e74705SXin Li   char macro_char_hex = CHAR_MACRO_HEX;
111*67e74705SXin Li #define CHAR_MACRO_DEC 255
112*67e74705SXin Li   char macro_char_dec = CHAR_MACRO_DEC;  // expected-warning {{implicit conversion from 'int' to 'char' changes value from 255 to -1}}
113*67e74705SXin Li 
114*67e74705SXin Li   char array_init[] = { 255, 127, 128, 129, 0 };
115*67e74705SXin Li }
116