Skip to content

Commit

Permalink
Add test for c++20 three-way comparison expression
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 5622e59 commit 5eb8963
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public boolean isUseGNUExtensions() {
public boolean isUseGNUExtensions() {
return true;
}
},
STDCPP20 {
@Override
public boolean isUseGNUExtensions() {
return false;
}
};

public abstract boolean isUseGNUExtensions();
Expand All @@ -122,6 +128,7 @@ public boolean isUseGNUExtensions() {

private static final ScannerInfo GNU_SCANNER_INFO = new ScannerInfo(getGnuMap());
private static final ScannerInfo SCANNER_INFO = new ScannerInfo(getStdMap());
private static final ScannerInfo STDCPP20_SCANNER_INFO = new ScannerInfo(getStdCpp20Map());

private static Map<String, String> getGnuMap() {
Map<String, String> map = new HashMap<>();
Expand All @@ -147,6 +154,12 @@ private static Map<String, String> getStdMap() {
return map;
}

private static Map<String, String> getStdCpp20Map() {
Map<String, String> map = getStdMap();
map.put("__cpp_impl_three_way_comparison", "201907L");
return map;
}

public AST2TestBase() {
super();
}
Expand Down Expand Up @@ -227,6 +240,8 @@ public static ScannerInfo createScannerInfo(ScannerKind scannerKind) {
switch (scannerKind) {
case GNU:
return GNU_SCANNER_INFO;
case STDCPP20:
return STDCPP20_SCANNER_INFO;
case STD:
default:
return SCANNER_INFO;
Expand Down Expand Up @@ -294,16 +309,25 @@ protected void validateConditionalExpressionC(String code) throws ParserExceptio
assertEquals(x.getName().toString(), x2.getName().toString());
}

protected void validateSimpleBinaryExpressionC(String code, int operand) throws ParserException {
IASTBinaryExpression e = (IASTBinaryExpression) getExpressionFromStatementInCode(code, ParserLanguage.C);
protected void validateSimpleBinaryExpression(String code, int operator, ParserLanguage language,
ScannerKind scannerKind) throws ParserException {
IASTBinaryExpression e = (IASTBinaryExpression) getExpressionFromStatementInCode(code, language, scannerKind);
assertNotNull(e);
assertEquals(e.getOperator(), operand);
assertEquals(e.getOperator(), operator);
IASTIdExpression x = (IASTIdExpression) e.getOperand1();
assertEquals(x.getName().toString(), "x"); //$NON-NLS-1$
IASTIdExpression y = (IASTIdExpression) e.getOperand2();
assertEquals(y.getName().toString(), "y"); //$NON-NLS-1$
}

protected void validateSimpleBinaryExpressionC(String code, int operator) throws ParserException {
validateSimpleBinaryExpression(code, operator, ParserLanguage.C, ScannerKind.STD);
}

protected void validateSimpleBinaryExpressionCPP20(String code, int operator) throws ParserException {
validateSimpleBinaryExpression(code, operator, ParserLanguage.CPP, ScannerKind.STDCPP20);
}

protected IASTExpression getExpressionFromStatementInCode(String code, ParserLanguage language)
throws ParserException {
return getExpressionFromStatementInCode(code, language, ScannerKind.STD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ public void testCExpressions() throws ParserException {
validateConditionalExpressionC("x ? y : x"); //$NON-NLS-1$
}

public void testCPP20Expressions() throws ParserException {
validateSimpleBinaryExpressionCPP20("x<=>y", IASTBinaryExpression.op_threewaycomparison); //$NON-NLS-1$
}

public void testMultipleDeclarators() throws Exception {
IASTTranslationUnit tu = parse("int r, s;", C); //$NON-NLS-1$
assertTrue(tu.isFrozen());
Expand Down

0 comments on commit 5eb8963

Please sign in to comment.