1 // RUN: %clang_cc1 -triple aarch64-unknown-unknown -std=c2x -fsyntax-only -verify -Wno-unused %s
3 // Test that the preprocessor behavior makes sense.
5 #error "wb suffix must be recognized by preprocessor"
8 #error "uwb suffix must be recognized by preprocessor"
11 #error "wb suffix must be interpreted as signed"
14 #error "uwb suffix must be interpreted as unsigned"
17 #if 18446744073709551615uwb != 18446744073709551615ULL
18 #error "expected the max value for uintmax_t to compare equal"
21 // Test that the preprocessor gives appropriate diagnostics when the
22 // literal value is larger than what can be stored in a [u]intmax_t.
23 #if 18446744073709551616wb != 0ULL // expected-error {{integer literal is too large to be represented in any integer type}}
24 #error "never expected to get here due to error"
26 #if 18446744073709551616uwb != 0ULL // expected-error {{integer literal is too large to be represented in any integer type}}
27 #error "never expected to get here due to error"
30 // Despite using a bit-precise integer, this is expected to overflow
31 // because all preprocessor arithmetic is done in [u]intmax_t, so this
32 // should result in the value 0.
33 #if 18446744073709551615uwb + 1 != 0ULL
34 #error "expected modulo arithmetic with uintmax_t width"
37 // Because this bit-precise integer is signed, it will also overflow,
38 // but Clang handles that by converting to uintmax_t instead of
40 #if 18446744073709551615wb + 1 != 0LL // expected-warning {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
41 #error "expected modulo arithmetic with uintmax_t width"
44 // Test that just because the preprocessor can't figure out the bit
45 // width doesn't mean we can't form the constant, it just means we
46 // can't use the value in a preprocessor conditional.
47 unsigned _BitInt(65) Val
= 18446744073709551616uwb
;
49 void ValidSuffix(void) {
54 _Static_assert((int)1wb
== 1, "not 1?");
55 _Static_assert((int)-1wb
== -1, "not -1?");
61 _Static_assert((unsigned int)1uwb
== 1u, "not 1?");
65 _Static_assert((int)1'2wb
== 12, "not 12?");
66 _Static_assert((unsigned int)1'2uwb
== 12u, "not 12?");
68 // Hexadecimal literals.
73 _Static_assert((int)0x0'1'2'3wb
== 0x0123, "not 0x0123");
74 _Static_assert((unsigned int)0xA'B'c'duwb
== 0xABCDu
, "not 0xABCD");
81 _Static_assert((int)0b1wb
== 1, "not 1?");
82 _Static_assert((unsigned int)0b1uwb
== 1u, "not 1?");
94 _Static_assert((int)0wb
== 0, "not 0?");
95 _Static_assert((unsigned int)0wbu
== 0u, "not 0?");
97 // Imaginary or Complex. These are allowed because _Complex can work with any
98 // integer type, and that includes _BitInt.
103 void InvalidSuffix(void) {
104 // Can't mix the case of wb or WB, and can't rearrange the letters.
105 0wB
; // expected-error {{invalid suffix 'wB' on integer constant}}
106 0Wb
; // expected-error {{invalid suffix 'Wb' on integer constant}}
107 0bw
; // expected-error {{invalid digit 'b' in octal constant}}
108 0BW
; // expected-error {{invalid digit 'B' in octal constant}}
110 // Trailing digit separators should still diagnose.
111 1'2'wb
; // expected-error {{digit separator cannot appear at end of digit sequence}}
112 1'2'uwb
; // expected-error {{digit separator cannot appear at end of digit sequence}}
115 1lwb
; // expected-error {{invalid suffix}}
116 1wbl
; // expected-error {{invalid suffix}}
117 1luwb
; // expected-error {{invalid suffix}}
118 1ulwb
; // expected-error {{invalid suffix}}
121 1llwb
; // expected-error {{invalid suffix}}
122 1uwbll
; // expected-error {{invalid suffix}}
125 0.1wb
; // expected-error {{invalid suffix}}
126 0.1fwb
; // expected-error {{invalid suffix}}
128 // Repetitive suffix.
129 1wbwb
; // expected-error {{invalid suffix}}
130 1uwbuwb
; // expected-error {{invalid suffix}}
131 1wbuwb
; // expected-error {{invalid suffix}}
132 1uwbwb
; // expected-error {{invalid suffix}}
135 void ValidSuffixInvalidValue(void) {
136 // This is a valid suffix, but the value is larger than one that fits within
137 // the width of BITINT_MAXWIDTH. When this value changes in the future, the
138 // test cases should pick a new value that can't be represented by a _BitInt,
139 // but also add a test case that a 129-bit literal still behaves as-expected.
140 _Static_assert(__BITINT_MAXWIDTH__
<= 128,
141 "Need to pick a bigger constant for the test case below.");
142 0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'1wb
; // expected-error {{integer literal is too large to be represented in any signed integer type}}
143 0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'1uwb
; // expected-error {{integer literal is too large to be represented in any integer type}}
146 void TestTypes(void) {
147 // 2 value bits, one sign bit
148 _Static_assert(__builtin_types_compatible_p(__typeof__(3wb
), _BitInt(3)));
149 // 2 value bits, one sign bit
150 _Static_assert(__builtin_types_compatible_p(__typeof__(-3wb
), _BitInt(3)));
151 // 2 value bits, no sign bit
152 _Static_assert(__builtin_types_compatible_p(__typeof__(3uwb
), unsigned _BitInt(2)));
153 // 4 value bits, one sign bit
154 _Static_assert(__builtin_types_compatible_p(__typeof__(0xFwb
), _BitInt(5)));
155 // 4 value bits, one sign bit
156 _Static_assert(__builtin_types_compatible_p(__typeof__(-0xFwb
), _BitInt(5)));
157 // 4 value bits, no sign bit
158 _Static_assert(__builtin_types_compatible_p(__typeof__(0xFuwb
), unsigned _BitInt(4)));