179 lines · cpp
1// RUN: %clang_cc1 -triple aarch64-unknown-unknown -fsyntax-only -verify -Wno-unused %s2 3// Test that the preprocessor behavior makes sense.4#if 1__wb != 15#error "wb suffix must be recognized by preprocessor"6#endif7#if 1__uwb != 18#error "uwb suffix must be recognized by preprocessor"9#endif10#if !(-1__wb < 0)11#error "wb suffix must be interpreted as signed"12#endif13#if !(-1__uwb > 0)14#error "uwb suffix must be interpreted as unsigned"15#endif16 17#if 18446744073709551615__uwb != 18446744073709551615ULL18#error "expected the max value for uintmax_t to compare equal"19#endif20 21// Test that the preprocessor gives appropriate diagnostics when the22// 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"25#endif26#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"28#endif29 30// Despite using a bit-precise integer, this is expected to overflow31// because all preprocessor arithmetic is done in [u]intmax_t, so this32// should result in the value 0.33#if 18446744073709551615__uwb + 1 != 0ULL34#error "expected modulo arithmetic with uintmax_t width"35#endif36 37// Because this bit-precise integer is signed, it will also overflow,38// but Clang handles that by converting to uintmax_t instead of39// intmax_t.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"42#endif43 44// Test that just because the preprocessor can't figure out the bit45// width doesn't mean we can't form the constant, it just means we46// can't use the value in a preprocessor conditional.47unsigned _BitInt(65) Val = 18446744073709551616__uwb;48// UDL test to make sure underscore parsing is correct49unsigned operator ""_(const char *);50 51void ValidSuffix(void) {52 // Decimal literals.53 1__wb;54 1__WB;55 -1__wb;56 _Static_assert((int)1__wb == 1, "not 1?");57 _Static_assert((int)-1__wb == -1, "not -1?");58 59 1__uwb;60 1__uWB;61 1__Uwb;62 1__UWB;63 1u__wb;64 1__WBu;65 1U__WB;66 _Static_assert((unsigned int)1__uwb == 1u, "not 1?");67 68 1'2__wb;69 1'2__uwb;70 _Static_assert((int)1'2__wb == 12, "not 12?");71 _Static_assert((unsigned int)1'2__uwb == 12u, "not 12?");72 73 // Hexadecimal literals.74 0x1__wb;75 0x1__uwb;76 0x0'1'2'3__wb;77 0xA'B'c'd__uwb;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");80 81 // Binary literals.82 0b1__wb;83 0b1__uwb;84 0b1'0'1'0'0'1__wb;85 0b0'1'0'1'1'0__uwb;86 _Static_assert((int)0b1__wb == 1, "not 1?");87 _Static_assert((unsigned int)0b1__uwb == 1u, "not 1?");88 89 // Octal literals.90 01__wb;91 01__uwb;92 0'6'0__wb;93 0'0'1__uwb;94 0__wbu;95 0__WBu;96 0U__wb;97 0U__WB;98 0__wb;99 _Static_assert((int)0__wb == 0, "not 0?");100 _Static_assert((unsigned int)0__wbu == 0u, "not 0?");101 102 // Imaginary or Complex. These are allowed because _Complex can work with any103 // integer type, and that includes _BitInt.104 1__wbi;105 1i__wb;106 1__wbj;107 108 //UDL test as single underscore109 unsigned i = 1.0_;110}111 112void 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}}118 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}}122 123 // Long.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}}129 130 // Long long.131 1ll__wb; // expected-error {{invalid suffix}}132 1__uwbll; // expected-error {{invalid suffix}}133 134 // Floating point.135 0.1__wb; // expected-error {{invalid suffix}}136 0.1f__wb; // expected-error {{invalid suffix}}137 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}}143 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}}152}153 154void ValidSuffixInvalidValue(void) {155 // This is a valid suffix, but the value is larger than one that fits within156 // the width of BITINT_MAXWIDTH. When this value changes in the future, the157 // 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}}163}164 165void TestTypes(void) {166 // 2 value bits, one sign bit167 _Static_assert(__is_same(decltype(3__wb), _BitInt(3)));168 // 2 value bits, one sign bit169 _Static_assert(__is_same(decltype(-3__wb), _BitInt(3)));170 // 2 value bits, no sign bit171 _Static_assert(__is_same(decltype(3__uwb), unsigned _BitInt(2)));172 // 4 value bits, one sign bit173 _Static_assert(__is_same(decltype(0xF__wb), _BitInt(5)));174 // 4 value bits, one sign bit175 _Static_assert(__is_same(decltype(-0xF__wb), _BitInt(5)));176 // 4 value bits, no sign bit177 _Static_assert(__is_same(decltype(0xF__uwb), unsigned _BitInt(4)));178}179