[Reland][Runtimes] Merge 'compile_commands.json' files from runtimes build (#116303)
[llvm-project.git] / clang / test / Sema / constant-conversion.c
blobffc25b9cc497872c1c75b137405c1a7f63eec41c
1 // RUN: %clang_cc1 -fsyntax-only -ffreestanding -verify=expected,one-bit -triple x86_64-apple-darwin %s
2 // RUN: %clang_cc1 -fsyntax-only -ffreestanding -Wno-single-bit-bitfield-constant-conversion -verify -triple x86_64-apple-darwin %s
4 #include <stdbool.h>
6 // This file tests -Wconstant-conversion, a subcategory of -Wconversion
7 // which is on by default.
9 void test_6792488(void) {
10 int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}}
13 void test_7809123(void) {
14 struct { int i5 : 5; } a;
16 a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 36 to 4}}
19 void test(void) {
20 struct S {
21 int b : 1; // The only valid values are 0 and -1.
22 } s;
24 s.b = -3; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -3 to -1}}
25 s.b = -2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -2 to 0}}
26 s.b = -1; // no-warning
27 s.b = 0; // no-warning
28 s.b = 1; // one-bit-warning {{implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1}}
29 s.b = true; // no-warning (we suppress it manually to reduce false positives)
30 s.b = false; // no-warning
31 s.b = 2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 2 to 0}}
34 enum Test2 { K_zero, K_one };
35 enum Test2 test2(enum Test2 *t) {
36 *t = 20;
37 return 10; // shouldn't warn
40 void test3(void) {
41 struct A {
42 unsigned int foo : 2;
43 int bar : 2;
46 struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to -2}}
47 struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to -2}}
48 struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}}
49 struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}}
50 struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}}
53 void test4(void) {
54 struct A {
55 char c : 2;
56 } a;
58 a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 257 to 1}}
61 void test5(void) {
62 struct A {
63 _Bool b : 1;
64 } a;
66 // Don't warn about this implicit conversion to bool, or at least
67 // don't warn about it just because it's a bit-field.
68 a.b = 100;
71 void test6(void) {
72 // Test that unreachable code doesn't trigger the truncation warning.
73 unsigned char x = 0 ? 65535 : 1; // no-warning
74 unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}}
77 void test7(void) {
78 struct {
79 unsigned int twoBits1:2;
80 unsigned int twoBits2:2;
81 unsigned int reserved:28;
82 } f;
84 f.twoBits1 = ~0; // no-warning
85 f.twoBits1 = ~1; // no-warning
86 f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -3 to 1}}
87 f.twoBits1 &= ~1; // no-warning
88 f.twoBits2 &= ~2; // no-warning
91 void test8(void) {
92 enum E { A, B, C };
93 struct { enum E x : 1; } f;
94 f.x = C; // expected-warning {{implicit truncation from 'int' to bit-field changes value from 2 to 0}}
97 void test9(void) {
98 const char max_char = 0x7F;
99 const short max_short = 0x7FFF;
100 const int max_int = 0x7FFFFFFF;
102 const short max_char_plus_one = (short)max_char + 1;
103 const int max_short_plus_one = (int)max_short + 1;
104 const long max_int_plus_one = (long)max_int + 1;
106 char new_char = max_char_plus_one; // expected-warning {{implicit conversion from 'const short' to 'char' changes value from 128 to -128}}
107 short new_short = max_short_plus_one; // expected-warning {{implicit conversion from 'const int' to 'short' changes value from 32768 to -32768}}
108 int new_int = max_int_plus_one; // expected-warning {{implicit conversion from 'const long' to 'int' changes value from 2147483648 to -2147483648}}
110 char hex_char = 0x80;
111 short hex_short = 0x8000;
112 int hex_int = 0x80000000;
114 char oct_char = 0200;
115 short oct_short = 0100000;
116 int oct_int = 020000000000;
118 char bin_char = 0b10000000;
119 short bin_short = 0b1000000000000000;
120 int bin_int = 0b10000000000000000000000000000000;
122 #define CHAR_MACRO_HEX 0xff
123 char macro_char_hex = CHAR_MACRO_HEX;
124 #define CHAR_MACRO_DEC 255
125 char macro_char_dec = CHAR_MACRO_DEC; // expected-warning {{implicit conversion from 'int' to 'char' changes value from 255 to -1}}
127 char array_init[] = { 255, 127, 128, 129, 0 };
130 #define A 1
132 void test10(void) {
133 struct S {
134 unsigned a : 4;
135 } s;
136 s.a = -1;
137 s.a = 15;
138 s.a = -8;
139 s.a = ~0;
140 s.a = ~0U;
141 s.a = ~(1<<A);
143 s.a = -9; // expected-warning{{implicit truncation from 'int' to bit-field changes value from -9 to 7}}
144 s.a = 16; // expected-warning{{implicit truncation from 'int' to bit-field changes value from 16 to 0}}