| 1 |
// RUN: %clang_cc1 -triple aarch64-unknown-unknown -std=c2x -fsyntax-only -verify -Wno-unused %s |
| 2 |
|
| 3 |
// Test that the preprocessor behavior makes sense. |
| 4 |
#if 1wb != 1 |
| 5 |
#error "wb suffix must be recognized by preprocessor" |
| 6 |
#endif |
| 7 |
#if 1uwb != 1 |
| 8 |
#error "uwb suffix must be recognized by preprocessor" |
| 9 |
#endif |
| 10 |
#if !(-1wb < 0) |
| 11 |
#error "wb suffix must be interpreted as signed" |
| 12 |
#endif |
| 13 |
#if !(-1uwb > 0) |
| 14 |
#error "uwb suffix must be interpreted as unsigned" |
| 15 |
#endif |
| 16 |
|
| 17 |
#if 18446744073709551615uwb != 18446744073709551615ULL |
| 18 |
#error "expected the max value for uintmax_t to compare equal" |
| 19 |
#endif |
| 20 |
|
| 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" |
| 25 |
#endif |
| 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" |
| 28 |
#endif |
| 29 |
|
| 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" |
| 35 |
#endif |
| 36 |
|
| 37 |
// Because this bit-precise integer is signed, it will also overflow, |
| 38 |
// but Clang handles that by converting to uintmax_t instead of |
| 39 |
// intmax_t. |
| 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" |
| 42 |
#endif |
| 43 |
|
| 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; |
| 48 |
|
| 49 |
void ValidSuffix(void) { |
| 50 |
// Decimal literals. |
| 51 |
1wb; |
| 52 |
1WB; |
| 53 |
-1wb; |
| 54 |
_Static_assert((int)1wb == 1, "not 1?"); |
| 55 |
_Static_assert((int)-1wb == -1, "not -1?"); |
| 56 |
|
| 57 |
1uwb; |
| 58 |
1uWB; |
| 59 |
1Uwb; |
| 60 |
1UWB; |
| 61 |
_Static_assert((unsigned int)1uwb == 1u, "not 1?"); |
| 62 |
|
| 63 |
1'2wb; |
| 64 |
1'2uwb; |
| 65 |
_Static_assert((int)1'2wb == 12, "not 12?"); |
| 66 |
_Static_assert((unsigned int)1'2uwb == 12u, "not 12?"); |
| 67 |
|
| 68 |
// Hexadecimal literals. |
| 69 |
0x1wb; |
| 70 |
0x1uwb; |
| 71 |
0x0'1'2'3wb; |
| 72 |
0xA'B'c'duwb; |
| 73 |
_Static_assert((int)0x0'1'2'3wb == 0x0123, "not 0x0123"); |
| 74 |
_Static_assert((unsigned int)0xA'B'c'duwb == 0xABCDu, "not 0xABCD"); |
| 75 |
|
| 76 |
// Binary literals. |
| 77 |
0b1wb; |
| 78 |
0b1uwb; |
| 79 |
0b1'0'1'0'0'1wb; |
| 80 |
0b0'1'0'1'1'0uwb; |
| 81 |
_Static_assert((int)0b1wb == 1, "not 1?"); |
| 82 |
_Static_assert((unsigned int)0b1uwb == 1u, "not 1?"); |
| 83 |
|
| 84 |
// Octal literals. |
| 85 |
01wb; |
| 86 |
01uwb; |
| 87 |
0'6'0wb; |
| 88 |
0'0'1uwb; |
| 89 |
0wbu; |
| 90 |
0WBu; |
| 91 |
0wbU; |
| 92 |
0WBU; |
| 93 |
0wb; |
| 94 |
_Static_assert((int)0wb == 0, "not 0?"); |
| 95 |
_Static_assert((unsigned int)0wbu == 0u, "not 0?"); |
| 96 |
|
| 97 |
// Imaginary or Complex. These are allowed because _Complex can work with any |
| 98 |
// integer type, and that includes _BitInt. |
| 99 |
1iwb; |
| 100 |
1wbj; |
| 101 |
} |
| 102 |
|
| 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}} |
| 109 |
|
| 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}} |
| 113 |
|
| 114 |
// Long. |
| 115 |
1lwb; // expected-error {{invalid suffix}} |
| 116 |
1wbl; // expected-error {{invalid suffix}} |
| 117 |
1luwb; // expected-error {{invalid suffix}} |
| 118 |
1ulwb; // expected-error {{invalid suffix}} |
| 119 |
|
| 120 |
// Long long. |
| 121 |
1llwb; // expected-error {{invalid suffix}} |
| 122 |
1uwbll; // expected-error {{invalid suffix}} |
| 123 |
|
| 124 |
// Floating point. |
| 125 |
0.1wb; // expected-error {{invalid suffix}} |
| 126 |
0.1fwb; // expected-error {{invalid suffix}} |
| 127 |
|
| 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}} |
| 133 |
} |
| 134 |
|
| 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}} |
| 144 |
} |
| 145 |
|
| 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))); |
| 159 |
} |