1 // RUN: %clang_cc1 -triple aarch64-unknown-unknown -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 18446744073709551615__uwb != 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 18446744073709551616__wb != 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 18446744073709551616__uwb != 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 18446744073709551615__uwb + 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 18446744073709551615__wb + 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
= 18446744073709551616__uwb
;
48 // UDL test to make sure underscore parsing is correct
49 unsigned operator ""_(const char *);
51 void ValidSuffix(void) {
56 _Static_assert((int)1__wb
== 1, "not 1?");
57 _Static_assert((int)-1__wb
== -1, "not -1?");
66 _Static_assert((unsigned int)1__uwb
== 1u, "not 1?");
70 _Static_assert((int)1'2__wb
== 12, "not 12?");
71 _Static_assert((unsigned int)1'2__uwb
== 12u, "not 12?");
73 // Hexadecimal literals.
78 _Static_assert((int)0x0'1'2'3__wb
== 0x0123, "not 0x0123");
79 _Static_assert((unsigned int)0xA'B'c'd__uwb
== 0xABCDu
, "not 0xABCD");
86 _Static_assert((int)0b1__wb
== 1, "not 1?");
87 _Static_assert((unsigned int)0b1__uwb
== 1u, "not 1?");
99 _Static_assert((int)0__wb
== 0, "not 0?");
100 _Static_assert((unsigned int)0__wbu
== 0u, "not 0?");
102 // Imaginary or Complex. These are allowed because _Complex can work with any
103 // integer type, and that includes _BitInt.
108 //UDL test as single underscore
112 void InvalidSuffix(void) {
113 // Can't mix the case of wb or WB, and can't rearrange the letters.
114 0__wB
; // expected-error {{invalid suffix '__wB' on integer constant}}
115 0__Wb
; // expected-error {{invalid suffix '__Wb' on integer constant}}
116 0__bw
; // expected-error {{invalid suffix '__bw' on integer constant}}
117 0__BW
; // expected-error {{invalid suffix '__BW' on integer constant}}
119 // Trailing digit separators should still diagnose.
120 1'2'__wb
; // expected-error {{digit separator cannot appear at end of digit sequence}}
121 1'2'__uwb
; // expected-error {{digit separator cannot appear at end of digit sequence}}
124 1l__wb
; // expected-error {{invalid suffix}}
125 1__wbl
; // expected-error {{invalid suffix}}
126 1l__uwb
; // expected-error {{invalid suffix}}
127 1__l
; // expected-error {{invalid suffix}}
128 1ul__wb
; // expected-error {{invalid suffix}}
131 1ll__wb
; // expected-error {{invalid suffix}}
132 1__uwbll
; // expected-error {{invalid suffix}}
135 0.1__wb
; // expected-error {{invalid suffix}}
136 0.1f__wb
; // expected-error {{invalid suffix}}
138 // Repetitive suffix.
139 1__wb__wb
; // expected-error {{invalid suffix}}
140 1__uwbuwb
; // expected-error {{invalid suffix}}
141 1__wbuwb
; // expected-error {{invalid suffix}}
142 1__uwbwb
; // expected-error {{invalid suffix}}
144 // Missing or extra characters in suffix.
145 1__
; // expected-error {{invalid suffix}}
146 1__u
; // expected-error {{invalid suffix}}
147 1___
; // expected-error {{invalid suffix}}
148 1___WB
; // expected-error {{invalid suffix}}
149 1__wb__
; // expected-error {{invalid suffix}}
150 1__w
; // expected-error {{invalid suffix}}
151 1__b
; // expected-error {{invalid suffix}}
154 void ValidSuffixInvalidValue(void) {
155 // This is a valid suffix, but the value is larger than one that fits within
156 // the width of BITINT_MAXWIDTH. When this value changes in the future, the
157 // test cases should pick a new value that can't be represented by a _BitInt,
158 // but also add a test case that a 129-bit literal still behaves as-expected.
159 _Static_assert(__BITINT_MAXWIDTH__
<= 128,
160 "Need to pick a bigger constant for the test case below.");
161 0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'1__wb
; // expected-error {{integer literal is too large to be represented in any signed integer type}}
162 0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'1__uwb
; // expected-error {{integer literal is too large to be represented in any integer type}}
165 void TestTypes(void) {
166 // 2 value bits, one sign bit
167 _Static_assert(__is_same(decltype(3__wb
), _BitInt(3)));
168 // 2 value bits, one sign bit
169 _Static_assert(__is_same(decltype(-3__wb
), _BitInt(3)));
170 // 2 value bits, no sign bit
171 _Static_assert(__is_same(decltype(3__uwb
), unsigned _BitInt(2)));
172 // 4 value bits, one sign bit
173 _Static_assert(__is_same(decltype(0xF__wb
), _BitInt(5)));
174 // 4 value bits, one sign bit
175 _Static_assert(__is_same(decltype(-0xF__wb
), _BitInt(5)));
176 // 4 value bits, no sign bit
177 _Static_assert(__is_same(decltype(0xF__uwb
), unsigned _BitInt(4)));