Skip to content

Commit

Permalink
C++23: literal suffix for (signed) size_t
Browse files Browse the repository at this point in the history
  • Loading branch information
guwirth committed Sep 3, 2024
1 parent 34c41b1 commit 960d74a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
11 changes: 0 additions & 11 deletions cxx-squid/dox/diff-cpp20-cpp23_grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,6 @@ identifier-continue:
nondigit
an element of the translation character set with the Unicode property XID_Continue

integer-suffix:
unsigned-suffix long-suffixopt
unsigned-suffix long-long-suffixopt
unsigned-suffix size-suffixopt
long-suffix unsigned-suffixopt
long-long-suffix unsigned-suffixopt
size-suffix unsigned-suffixopt

size-suffix: one of
z Z

c-char:
basic-c-char
escape-sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class CxxLexerPool {
private static final String BIN_PREFIX = "0[bB]";
private static final String EXPONENT = "[Ee][+-]?+[0-9_]([']?+[0-9_]++)*+";
private static final String BINARY_EXPONENT = "[pP][+-]?+\\d([']?+\\d++)*+"; // since C++17
//private static final String INTEGER_SUFFIX = "(((U|u)(i64|LL|ll|L|l)?)|((i64|LL|ll|L|l)(u|U)?))";
//private static final String INTEGER_SUFFIX = "(((U|u)(i64|LL|ll|L|l)?)|((i64|LL|ll|L|l)(u|U)?)|((z|Z)(u|U)))";
//private static final String FLOAT_SUFFIX = "(f|l|F|L)";
// ud-suffix: identifier (including INTEGER_SUFFIX, FLOAT_SUFFIX)
private static final String UD_SUFFIX = "[_a-zA-Z]\\w*+";
Expand Down
36 changes: 32 additions & 4 deletions cxx-squid/src/test/java/org/sonar/cxx/lexer/CxxLexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@ void decimal_integer_literals() {
LiteralValuesBuilder.builder("7LLU").tokenValue("7LLU").tokenType(CxxTokenType.NUMBER).build(),
// With Microsoft specific 64-bit integer-suffix: i64
LiteralValuesBuilder.builder("7i64").tokenValue("7i64").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("7ui64").tokenValue("7ui64").tokenType(CxxTokenType.NUMBER).build()
LiteralValuesBuilder.builder("7ui64").tokenValue("7ui64").tokenType(CxxTokenType.NUMBER).build(),
// C++23
LiteralValuesBuilder.builder("7z").tokenValue("7z").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("7uz").tokenValue("7uz").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("7zu").tokenValue("7zu").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("7Z").tokenValue("7Z").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("7UZ").tokenValue("7UZ").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("7ZU").tokenValue("7ZU").tokenType(CxxTokenType.NUMBER).build()
));

values.forEach(value
Expand Down Expand Up @@ -179,7 +186,14 @@ void octal_integer_literals() {
LiteralValuesBuilder.builder("07LLU").tokenValue("07LLU").tokenType(CxxTokenType.NUMBER).build(),
// With Microsoft specific 64-bit integer-suffix: i64
LiteralValuesBuilder.builder("07i64").tokenValue("07i64").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("07ui64").tokenValue("07ui64").tokenType(CxxTokenType.NUMBER).build()
LiteralValuesBuilder.builder("07ui64").tokenValue("07ui64").tokenType(CxxTokenType.NUMBER).build(),
// C++23
LiteralValuesBuilder.builder("07z").tokenValue("07z").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("07uz").tokenValue("07uz").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("07zu").tokenValue("07zu").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("07Z").tokenValue("07Z").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("07UZ").tokenValue("07UZ").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("07ZU").tokenValue("07ZU").tokenType(CxxTokenType.NUMBER).build()
));

values.forEach(value
Expand Down Expand Up @@ -226,7 +240,14 @@ void hex_integer_literals() {
LiteralValuesBuilder.builder("0x7LLU").tokenValue("0x7LLU").tokenType(CxxTokenType.NUMBER).build(),
// With Microsoft specific 64-bit integer-suffix: i64
LiteralValuesBuilder.builder("0x7i64").tokenValue("0x7i64").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0x7ui64").tokenValue("0x7ui64").tokenType(CxxTokenType.NUMBER).build()
LiteralValuesBuilder.builder("0x7ui64").tokenValue("0x7ui64").tokenType(CxxTokenType.NUMBER).build(),
// C++23
LiteralValuesBuilder.builder("0x7z").tokenValue("0x7z").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0x7uz").tokenValue("0x7uz").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0x7zu").tokenValue("0x7zu").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0x7Z").tokenValue("0x7Z").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0x7UZ").tokenValue("0x7UZ").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0x7ZU").tokenValue("0x7ZU").tokenType(CxxTokenType.NUMBER).build()
));

values.forEach(value
Expand All @@ -243,7 +264,14 @@ void bin_integer_literals() {
// bin integer
LiteralValuesBuilder.builder("0b0").tokenValue("0b0").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0B1").tokenValue("0B1").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0b10101001").tokenValue("0b10101001").tokenType(CxxTokenType.NUMBER).build()
LiteralValuesBuilder.builder("0b10101001").tokenValue("0b10101001").tokenType(CxxTokenType.NUMBER).build(),
// C++23
LiteralValuesBuilder.builder("0b1z").tokenValue("0b1z").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0b1uz").tokenValue("0b1uz").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0b1zu").tokenValue("0b1zu").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0b1Z").tokenValue("0b1Z").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0b1UZ").tokenValue("0b1UZ").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0b1ZU").tokenValue("0b1ZU").tokenType(CxxTokenType.NUMBER).build()
));

values.forEach(value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,22 @@ void numbers() {
assertThat(evaluate("0xffffffffffffffffui64")).isTrue();
assertThat(evaluate("0xffffffffffffffffi64")).isTrue();
assertThat(evaluate("0x7FFFFFL")).isTrue();
assertThat(evaluate("1z")).isTrue();
assertThat(evaluate("1ZU")).isTrue();
assertThat(evaluate("0x1z")).isTrue();
assertThat(evaluate("0x1ZU")).isTrue();
assertThat(evaluate("0b1z")).isTrue();
assertThat(evaluate("0b1ZU")).isTrue();

assertThat(evaluate("0")).isFalse();
assertThat(evaluate("0x0")).isFalse();
assertThat(evaluate("0b0")).isFalse();
assertThat(evaluate("0z")).isFalse();
assertThat(evaluate("0ZU")).isFalse();
assertThat(evaluate("0x0z")).isFalse();
assertThat(evaluate("0x0ZU")).isFalse();
assertThat(evaluate("0b0z")).isFalse();
assertThat(evaluate("0b0ZU")).isFalse();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
auto a = 0z;
auto b = 0zu;
auto c = 0uz;

auto d = 0Z;
auto e = 0ZU;
auto f = 0UZ;

void foo() {
for (auto i = 0zu; i < v.size(); ++i) {
std::cout << i << ": " << v[i] << '\n';
}
}

0 comments on commit 960d74a

Please sign in to comment.