Skip to content

Commit

Permalink
C++23: floting point suffixes
Browse files Browse the repository at this point in the history
- [Requirements for optional extended floating-point types](https://en.cppreference.com/w/cpp/language/floating_literal) [P1467R9](https://wg21.link/P1467R9)
- linked SonarOpenCommunity#2536
  • Loading branch information
guwirth committed Sep 4, 2024
1 parent df8b1e0 commit f000b41
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
3 changes: 0 additions & 3 deletions cxx-squid/dox/diff-cpp20-cpp23_grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ conditional-escape-sequence-char:
any member of the basic character set that is not an octal-digit, a simple-escape-sequence-char, or the
characters N, o, u, U, or x

floating-point-suffix: one of
f l f16 f32 f64 f128 bf16 F L F16 F32 F64 F128 BF16

s-char:
basic-s-char

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class CxxLexerPool {
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)?)|((z|Z)(u|U)))";
//private static final String FLOAT_SUFFIX = "(f|l|F|L)";
//private static final String FLOAT_SUFFIX = "(f|l|F|L|f16|f32|f64|f128|bf16|F16|F32|F64|F128|BF16)";
// ud-suffix: identifier (including INTEGER_SUFFIX, FLOAT_SUFFIX)
private static final String UD_SUFFIX = "[_a-zA-Z]\\w*+";
private static final String DECDIGIT_SEQUENCE = "\\d([']?+\\d++)*+";
Expand Down
15 changes: 13 additions & 2 deletions cxx-squid/src/test/java/org/sonar/cxx/lexer/CxxLexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,24 @@ void floating_point_literals() {
LiteralValuesBuilder.builder("3.14e-10L").tokenValue("3.14e-10L").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("3.14E-10L").tokenValue("3.14E-10L").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0e0L").tokenValue("0e0L").tokenType(CxxTokenType.NUMBER).build(),
// c++17: hexadecimal floating literals
// C++17: hexadecimal floating literals
LiteralValuesBuilder.builder("0x1ffp10").tokenValue("0x1ffp10").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0X0p-1").tokenValue("0X0p-1").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0x1.p0").tokenValue("0x1.p0").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0xf.p-1").tokenValue("0xf.p-1").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0x0.123p-1").tokenValue("0x0.123p-1").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("0xa.bp10l").tokenValue("0xa.bp10l").tokenType(CxxTokenType.NUMBER).build()
LiteralValuesBuilder.builder("0xa.bp10l").tokenValue("0xa.bp10l").tokenType(CxxTokenType.NUMBER).build(),
// C++23
LiteralValuesBuilder.builder("1.f16").tokenValue("1.f16").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.f32").tokenValue("1.f32").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.f64").tokenValue("1.f64").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.f128").tokenValue("1.f128").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.bf16").tokenValue("1.bf16").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.F16").tokenValue("1.F16").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.F32").tokenValue("1.F32").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.F64").tokenValue("1.F64").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.F128").tokenValue("1.F128").tokenType(CxxTokenType.NUMBER).build(),
LiteralValuesBuilder.builder("1.BF16").tokenValue("1.BF16").tokenType(CxxTokenType.NUMBER).build()
));

values.forEach(value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ void numbers() {
assertThat(evaluate("0x1ZU")).isTrue();
assertThat(evaluate("0b1z")).isTrue();
assertThat(evaluate("0b1ZU")).isTrue();
assertThat(evaluate("1.")).isTrue();
assertThat(evaluate("1.f")).isTrue();
assertThat(evaluate("1.F")).isTrue();
assertThat(evaluate("1.l")).isTrue();
assertThat(evaluate("1.L")).isTrue();
assertThat(evaluate("1.f16")).isTrue();
assertThat(evaluate("1.f32")).isTrue();
assertThat(evaluate("1.f64")).isTrue();
assertThat(evaluate("1.f128")).isTrue();
assertThat(evaluate("1.bf16")).isTrue();
assertThat(evaluate("1.F16")).isTrue();
assertThat(evaluate("1.F32")).isTrue();
assertThat(evaluate("1.F64")).isTrue();
assertThat(evaluate("1.F128")).isTrue();
assertThat(evaluate("1.BF16")).isTrue();

assertThat(evaluate("0")).isFalse();
assertThat(evaluate("0x0")).isFalse();
Expand All @@ -91,6 +106,21 @@ void numbers() {
assertThat(evaluate("0x0ZU")).isFalse();
assertThat(evaluate("0b0z")).isFalse();
assertThat(evaluate("0b0ZU")).isFalse();
assertThat(evaluate("0.")).isFalse();
assertThat(evaluate("0.f")).isFalse();
assertThat(evaluate("0.F")).isFalse();
assertThat(evaluate("0.l")).isFalse();
assertThat(evaluate("0.L")).isFalse();
assertThat(evaluate("0.f16")).isFalse();
assertThat(evaluate("0.f32")).isFalse();
assertThat(evaluate("0.f64")).isFalse();
assertThat(evaluate("0.f128")).isFalse();
assertThat(evaluate("0.bf16")).isFalse();
assertThat(evaluate("0.F16")).isFalse();
assertThat(evaluate("0.F32")).isFalse();
assertThat(evaluate("0.F64")).isFalse();
assertThat(evaluate("0.F128")).isFalse();
assertThat(evaluate("0.BF16")).isFalse();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
std::float16_t f = 58.f16;
std::float16_t f = 0x1p5F16;
std::float32_t f = 4e2f32;
std::float32_t f = 1.18e-4932lF32;
std::float64_t f = 123.456e-67f64;
std::float64_t f = 0.1F64;
std::float128_t f = .1E4f128;
std::float128_t f = .1F128;
std::bfloat16_t f = 3.14'15'92bf16;
std::bfloat16_t f = -10.0BF16;

0 comments on commit f000b41

Please sign in to comment.