From a90cbe17361e76723fba4f67ec41dbdc33b69e93 Mon Sep 17 00:00:00 2001 From: "Igor V. Kovalenko" Date: Thu, 26 Jan 2023 10:30:18 +0300 Subject: [PATCH] Add simple evaluation for c++20 three-way comparison --- .../core/parser/tests/ast2/AST2CPPTests.java | 26 +++++++++++++++++++ .../core/parser/tests/ast2/AST2TestBase.java | 7 ++++- .../core/dom/parser/ValueFactory.java | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index 4a8138fe0c8..e60b1e376a9 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -4193,6 +4193,32 @@ public class AST2CPPTests extends AST2CPPTestBase { assertEquals(col.getName(1).toString(), "operator <=>"); } + // constexpr bool less(int x, int y) { return x <=> y < 0; } + // constexpr bool equals(int x, int y) { return x <=> y == 0; } + // constexpr bool greater(int x, int y) { return x <=> y > 0; } + // + // static constexpr auto less01 = less(0, 1); + // static constexpr auto less00 = less(0, 0); + // static constexpr auto less10 = less(1, 0); + // static constexpr auto equals01 = equals(0, 1); + // static constexpr auto equals11 = equals(1, 1); + // static constexpr auto equals10 = equals(1, 0); + // static constexpr auto greater01 = greater(0, 1); + // static constexpr auto greater00 = greater(0, 0); + // static constexpr auto greater10 = greater(1, 0); + public void testThreeWayComparisonSimpleCase() throws Exception { + BindingAssertionHelper helper = getAssertionHelper(CPP, ScannerKind.STDCPP20); + helper.assertVariableValue("less01", 1); + helper.assertVariableValue("less00", 0); + helper.assertVariableValue("less10", 0); + helper.assertVariableValue("equals01", 0); + helper.assertVariableValue("equals11", 1); + helper.assertVariableValue("equals10", 0); + helper.assertVariableValue("greater01", 0); + helper.assertVariableValue("greater00", 0); + helper.assertVariableValue("greater10", 1); + } + // typedef int I; // typedef int I; // typedef I I; diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TestBase.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TestBase.java index 13743cbd05b..610c48038c4 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TestBase.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TestBase.java @@ -576,8 +576,13 @@ public abstract class AST2TestBase extends SemanticTestBase { } protected BindingAssertionHelper getAssertionHelper(ParserLanguage lang) throws ParserException, IOException { + return getAssertionHelper(lang, ScannerKind.GNU); + } + + protected BindingAssertionHelper getAssertionHelper(ParserLanguage lang, ScannerKind scannerKind) + throws ParserException, IOException { String code = getAboveComment(); - return new AST2AssertionHelper(code, lang); + return new AST2AssertionHelper(code, lang, scannerKind); } final protected void assertNoProblemBindings(NameCollector col) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ValueFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ValueFactory.java index fbb7eb713fb..2b08f3223a0 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ValueFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ValueFactory.java @@ -226,6 +226,7 @@ public class ValueFactory { break; case IASTBinaryExpression.op_threewaycomparison: // TODO: implement for <=> + value = v1 < v2 ? -1l : (v1 > v2 ? 1l : 0); break; case IASTBinaryExpression.op_binaryAnd: value = v1 & v2;