Skip to content

Commit

Permalink
Add simple evaluation for c++20 three-way comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
i-garrison authored and jonahgraham committed Jan 28, 2023
1 parent a0f9103 commit a90cbe1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4193,6 +4193,32 @@ public void testThreeWayComparisonOperatorName() throws Exception {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,13 @@ final protected IASTTranslationUnit parseAndCheckImplicitNameBindings() throws E
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ private static IntegralValue applyBinaryOperator(final int op, final long v1, fi
break;
case IASTBinaryExpression.op_threewaycomparison:
// TODO: implement for <=>
value = v1 < v2 ? -1l : (v1 > v2 ? 1l : 0);
break;
case IASTBinaryExpression.op_binaryAnd:
value = v1 & v2;
Expand Down

0 comments on commit a90cbe1

Please sign in to comment.