Skip to content

Commit

Permalink
Add rewriter 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 5eb8963 commit a8b3401
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
package org.eclipse.cdt.core.parser.tests.rewrite.astwriter;

import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.tests.ast2.AST2TestBase.ScannerKind;
import org.eclipse.cdt.core.parser.tests.rewrite.TestSourceFile;

/**
* @author Guido Zgraggen IFS
*/
public class ASTWriterTestSourceFile extends TestSourceFile {
private ParserLanguage parserLanguage = ParserLanguage.CPP;
private boolean useGNUExtensions = false;
private ScannerKind scannerKind = ScannerKind.STD;

public ASTWriterTestSourceFile(String name) {
super(name);
Expand All @@ -36,11 +37,11 @@ public ParserLanguage getParserLanguage() {
return parserLanguage;
}

public boolean isUseGNUExtensions() {
return useGNUExtensions;
public ScannerKind getScannerKind() {
return scannerKind;
}

public void setUseGNUExtensions(boolean useGNUExtensions) {
this.useGNUExtensions = useGNUExtensions;
public void setScannerKind(ScannerKind scannerKind) {
this.scannerKind = scannerKind;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.tests.ast2.AST2TestBase;
import org.eclipse.cdt.core.parser.tests.ast2.AST2TestBase.ScannerKind;
import org.eclipse.cdt.core.parser.tests.rewrite.RewriteBaseTest;
import org.eclipse.cdt.core.parser.tests.rewrite.TestHelper;
import org.eclipse.cdt.core.parser.tests.rewrite.TestSourceFile;
Expand Down Expand Up @@ -94,16 +95,16 @@ public String generateSource(TestSourceFile testFile) throws Exception {
protected ISourceCodeParser getParser(TestSourceFile testFile) throws Exception {
FileContent codeReader = FileContent.create(file);

ScannerInfo scannerInfo = new ScannerInfo();
ParserLanguage language = getLanguage(testFile);
boolean useGNUExtensions = getGNUExtension(testFile);
ScannerKind scannerKind = getScannerKind(testFile);
ScannerInfo scannerInfo = AST2TestBase.createScannerInfo(scannerKind);

IScanner scanner = AST2TestBase.createScanner(codeReader, language, ParserMode.COMPLETE_PARSE, scannerInfo);

ISourceCodeParser parser = null;
if (language == ParserLanguage.CPP) {
ICPPParserExtensionConfiguration config = null;
if (useGNUExtensions) {
if (scannerKind.isUseGNUExtensions()) {
config = new GPPParserExtensionConfiguration();
} else {
config = new ANSICPPParserExtensionConfiguration();
Expand All @@ -112,7 +113,7 @@ protected ISourceCodeParser getParser(TestSourceFile testFile) throws Exception
} else {
ICParserExtensionConfiguration config = null;

if (useGNUExtensions) {
if (scannerKind.isUseGNUExtensions()) {
config = new GCCParserExtensionConfiguration();
} else {
config = new ANSICParserExtensionConfiguration();
Expand All @@ -123,10 +124,10 @@ protected ISourceCodeParser getParser(TestSourceFile testFile) throws Exception
return parser;
}

private boolean getGNUExtension(TestSourceFile file) {
private ScannerKind getScannerKind(TestSourceFile file) {
if (file instanceof ASTWriterTestSourceFile)
return ((ASTWriterTestSourceFile) file).isUseGNUExtensions();
return false;
return ((ASTWriterTestSourceFile) file).getScannerKind();
return ScannerKind.STD;
}

private ParserLanguage getLanguage(TestSourceFile file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.regex.Pattern;

import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.tests.ast2.AST2TestBase.ScannerKind;
import org.eclipse.cdt.core.parser.tests.rewrite.RewriteBaseTest;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.core.runtime.FileLocator;
Expand All @@ -34,7 +35,7 @@

public class SourceRewriteTest extends TestSuite {
private static final String testRegexp = "//!(.*)\\s*(\\w*)*$"; //$NON-NLS-1$
private static final String codeTypeRegexp = "//%(C|CPP)( GNU)?$"; //$NON-NLS-1$
private static final String codeTypeRegexp = "//%(C|CPP|CPP20)( GNU)?$"; //$NON-NLS-1$
private static final String resultRegexp = "//=.*$"; //$NON-NLS-1$

enum MatcherState {
Expand Down Expand Up @@ -149,7 +150,7 @@ private static ArrayList<RewriteBaseTest> createTests(BufferedReader inputReader
matcherState = MatcherState.inSource;
if (file != null) {
file.setParserLanguage(getParserLanguage(line));
file.setUseGNUExtensions(useGNUExtensions(line));
file.setScannerKind(getScannerKind(line));
}
continue;
}
Expand All @@ -172,17 +173,20 @@ private static ArrayList<RewriteBaseTest> createTests(BufferedReader inputReader
return testCases;
}

protected static boolean useGNUExtensions(String line) {
protected static ScannerKind getScannerKind(String line) {
Matcher matcherBeginOfTest = createMatcherFromString(codeTypeRegexp, line);
if (matcherBeginOfTest.find()) {
String codeType = matcherBeginOfTest.group(2);
if (codeType == null) {
return false;
String codeType = matcherBeginOfTest.group(1);
String gnuExtensionsType = matcherBeginOfTest.group(2);
if (gnuExtensionsType == null) {
if (codeType.equalsIgnoreCase("CPP20")) { //$NON-NLS-1$
return ScannerKind.STDCPP20;
}
} else {
return true;
return ScannerKind.GNU;
}
}
return false;
return ScannerKind.STD;
}

protected static ParserLanguage getParserLanguage(String line) {
Expand All @@ -191,6 +195,8 @@ protected static ParserLanguage getParserLanguage(String line) {
String codeType = matcherBeginOfTest.group(1);
if (codeType.equalsIgnoreCase("CPP")) { //$NON-NLS-1$
return ParserLanguage.CPP;
} else if (codeType.equalsIgnoreCase("CPP20")) { //$NON-NLS-1$
return ParserLanguage.CPP;
} else {
return ParserLanguage.C;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ bool b = 1 != x;
int i = a.x;
int i = a->x;

//!CPP20BinaryExpression
//%CPP20
auto r = x <=> y;

//!BinaryExpression with MacroExpansions
//%CPP
#define ZWO 2
Expand Down

0 comments on commit a8b3401

Please sign in to comment.