mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
fixed problems with DOM parser running in LR parser suite
This commit is contained in:
parent
72312ab02a
commit
3f5cccb36d
12 changed files with 105 additions and 34 deletions
|
@ -16,6 +16,8 @@ import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import junit.framework.AssertionFailedError;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
|
import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
|
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
|
||||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||||
|
@ -411,8 +413,10 @@ public class AST2BaseTest extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected StringBuffer[] getContents(int sections) throws IOException {
|
protected StringBuffer[] getContents(int sections) throws IOException {
|
||||||
return TestSourceReader.getContentsForTest(
|
CTestPlugin plugin = CTestPlugin.getDefault();
|
||||||
CTestPlugin.getDefault().getBundle(), "parser", getClass(), getName(), sections);
|
if(plugin == null)
|
||||||
|
throw new AssertionFailedError("This test must be run as a JUnit plugin test");
|
||||||
|
return TestSourceReader.getContentsForTest(plugin.getBundle(), "parser", getClass(), getName(), sections);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static <T> T assertInstance(Object o, Class<T> clazz, Class... cs) {
|
protected static <T> T assertInstance(Object o, Class<T> clazz, Class... cs) {
|
||||||
|
|
|
@ -34,9 +34,9 @@ public class ASTNodeSelectorTest extends AST2BaseTest {
|
||||||
return suite(ASTNodeSelectorTest.class);
|
return suite(ASTNodeSelectorTest.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String fCode;
|
protected String fCode;
|
||||||
private IASTTranslationUnit fTu;
|
protected IASTTranslationUnit fTu;
|
||||||
private IASTNodeSelector fSelector;
|
protected IASTNodeSelector fSelector;
|
||||||
|
|
||||||
public ASTNodeSelectorTest() {
|
public ASTNodeSelectorTest() {
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ public class ASTNodeSelectorTest extends AST2BaseTest {
|
||||||
createTranslationUnit();
|
createTranslationUnit();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createTranslationUnit() throws IOException {
|
protected void createTranslationUnit() throws IOException {
|
||||||
fCode= getContents(1)[0].toString();
|
fCode= getContents(1)[0].toString();
|
||||||
CodeReader codeReader = new CodeReader(fCode.toCharArray());
|
CodeReader codeReader = new CodeReader(fCode.toCharArray());
|
||||||
ScannerInfo scannerInfo = new ScannerInfo();
|
ScannerInfo scannerInfo = new ScannerInfo();
|
||||||
|
|
|
@ -188,7 +188,7 @@ public class DOMPreprocessorInformationTest extends AST2BaseTest {
|
||||||
// #endif
|
// #endif
|
||||||
public void testElifWithoutCondition_bug185324() throws Exception {
|
public void testElifWithoutCondition_bug185324() throws Exception {
|
||||||
StringBuffer code= getContents(1)[0];
|
StringBuffer code= getContents(1)[0];
|
||||||
IASTTranslationUnit tu = parse(code.toString(), ParserLanguage.CPP, false, false, true);
|
IASTTranslationUnit tu = parse(code.toString(), ParserLanguage.CPP, false, false);
|
||||||
IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements();
|
IASTPreprocessorStatement[] st = tu.getAllPreprocessorStatements();
|
||||||
assertEquals(3, st.length);
|
assertEquals(3, st.length);
|
||||||
assertTrue(st[0] instanceof IASTPreprocessorIfdefStatement);
|
assertTrue(st[0] instanceof IASTPreprocessorIfdefStatement);
|
||||||
|
|
|
@ -44,6 +44,13 @@ public class LRCPPSpecFailingTest extends AST2CPPSpecFailingTest {
|
||||||
return ParseHelper.parse(code, language, true, checkBindings, expectedProblemBindings );
|
return ParseHelper.parse(code, language, true, checkBindings, expectedProblemBindings );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected IASTTranslationUnit parse(String code, ParserLanguage lang, String[] problems) throws ParserException {
|
||||||
|
ILanguage language = lang.isCPP() ? getCPPLanguage() : getCLanguage();
|
||||||
|
return ParseHelper.parse(code, language, problems);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected BaseExtensibleLanguage getCLanguage() {
|
protected BaseExtensibleLanguage getCLanguage() {
|
||||||
return C99Language.getDefault();
|
return C99Language.getDefault();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,12 @@ public class LRCPPSpecTest extends AST2CPPSpecTest {
|
||||||
return ParseHelper.parse(code, language, true, checkBindings, expectedProblemBindings );
|
return ParseHelper.parse(code, language, true, checkBindings, expectedProblemBindings );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected IASTTranslationUnit parse(String code, ParserLanguage lang, String[] problems) throws ParserException {
|
||||||
|
ILanguage language = lang.isCPP() ? getCPPLanguage() : getCLanguage();
|
||||||
|
return ParseHelper.parse(code, language, problems);
|
||||||
|
}
|
||||||
|
|
||||||
protected BaseExtensibleLanguage getCLanguage() {
|
protected BaseExtensibleLanguage getCLanguage() {
|
||||||
return C99Language.getDefault();
|
return C99Language.getDefault();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,12 @@ public class LRCSpecFailingTest extends AST2CSpecFailingTest {
|
||||||
return ParseHelper.parse(code, language, true, checkBindings, expectedProblemBindings );
|
return ParseHelper.parse(code, language, true, checkBindings, expectedProblemBindings );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected IASTTranslationUnit parse(String code, ParserLanguage lang, String[] problems) throws ParserException {
|
||||||
|
ILanguage language = lang.isCPP() ? getCPPLanguage() : getCLanguage();
|
||||||
|
return ParseHelper.parse(code, language, problems);
|
||||||
|
}
|
||||||
|
|
||||||
protected BaseExtensibleLanguage getCLanguage() {
|
protected BaseExtensibleLanguage getCLanguage() {
|
||||||
return C99Language.getDefault();
|
return C99Language.getDefault();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,12 @@ public class LRCSpecTests extends AST2CSpecTest {
|
||||||
return ParseHelper.parse(code, language, true, checkBindings, expectedProblemBindings );
|
return ParseHelper.parse(code, language, true, checkBindings, expectedProblemBindings );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected IASTTranslationUnit parse(String code, ParserLanguage lang, String[] problems) throws ParserException {
|
||||||
|
ILanguage language = lang.isCPP() ? getCPPLanguage() : getCLanguage();
|
||||||
|
return ParseHelper.parse(code, language, problems);
|
||||||
|
}
|
||||||
|
|
||||||
protected ILanguage getCLanguage() {
|
protected ILanguage getCLanguage() {
|
||||||
return C99Language.getDefault();
|
return C99Language.getDefault();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.core.lrparser.tests;
|
package org.eclipse.cdt.core.lrparser.tests;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
@ -37,6 +39,14 @@ public class LRNodeSelectorTest extends ASTNodeSelectorTest {
|
||||||
return ParseHelper.parse(code, language, expectNoProblems);
|
return ParseHelper.parse(code, language, expectNoProblems);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void createTranslationUnit() throws IOException {
|
||||||
|
fCode= getContents(1)[0].toString();
|
||||||
|
fTu = ParseHelper.parse(fCode, getCPPLanguage(), false);
|
||||||
|
fSelector= fTu.getNodeSelector(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected ILanguage getC99Language() {
|
protected ILanguage getC99Language() {
|
||||||
return C99Language.getDefault();
|
return C99Language.getDefault();
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,7 +74,7 @@ public class LRSelectionParseTest extends AST2SelectionParseTest {
|
||||||
String fileName = file.getLocation().toOSString();
|
String fileName = file.getLocation().toOSString();
|
||||||
ICodeReaderFactory fileCreator = SavedCodeReaderFactory.getInstance();
|
ICodeReaderFactory fileCreator = SavedCodeReaderFactory.getInstance();
|
||||||
CodeReader reader = fileCreator.createCodeReaderForTranslationUnit(fileName);
|
CodeReader reader = fileCreator.createCodeReaderForTranslationUnit(fileName);
|
||||||
return ParseHelper.parse(reader, language, scanInfo, fileCreator, expectNoProblems, true, 0);
|
return ParseHelper.parse(reader, language, scanInfo, fileCreator, expectNoProblems, true, 0, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -35,6 +35,10 @@ public class LRTaskParserTest extends TaskParserTest {
|
||||||
return ParseHelper.parse(code, language, expectNoProblems);
|
return ParseHelper.parse(code, language, expectNoProblems);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected IASTTranslationUnit parse( String code, ParserLanguage lang, boolean useGNUExtensions, boolean expectNoProblems, @SuppressWarnings("unused") boolean parseComments) throws ParserException {
|
||||||
|
return parse(code,lang, useGNUExtensions, expectNoProblems );
|
||||||
|
}
|
||||||
|
|
||||||
protected ILanguage getC99Language() {
|
protected ILanguage getC99Language() {
|
||||||
return C99Language.getDefault();
|
return C99Language.getDefault();
|
||||||
|
|
|
@ -47,6 +47,10 @@ public class LRTests extends AST2Tests {
|
||||||
return ParseHelper.parse(code, language, expectNoProblems);
|
return ParseHelper.parse(code, language, expectNoProblems);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected IASTTranslationUnit parse( String code, ParserLanguage lang, boolean useGNUExtensions, boolean expectNoProblems, @SuppressWarnings("unused") boolean parseComments) throws ParserException {
|
||||||
|
return parse(code,lang, useGNUExtensions, expectNoProblems );
|
||||||
|
}
|
||||||
|
|
||||||
protected ILanguage getC99Language() {
|
protected ILanguage getC99Language() {
|
||||||
return C99Language.getDefault();
|
return C99Language.getDefault();
|
||||||
|
|
|
@ -10,6 +10,9 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.core.lrparser.tests;
|
package org.eclipse.cdt.core.lrparser.tests;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import junit.framework.AssertionFailedError;
|
import junit.framework.AssertionFailedError;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
|
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
|
||||||
|
@ -32,54 +35,69 @@ import org.eclipse.core.runtime.CoreException;
|
||||||
*
|
*
|
||||||
* @author Mike Kucera
|
* @author Mike Kucera
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("restriction")
|
@SuppressWarnings({"restriction", "nls"})
|
||||||
public class ParseHelper {
|
public class ParseHelper {
|
||||||
|
|
||||||
static int testsRun = 0;
|
static int testsRun = 0;
|
||||||
|
|
||||||
private static class NameResolver extends ASTVisitor {
|
|
||||||
|
|
||||||
|
static protected class NameResolver extends ASTVisitor {
|
||||||
{
|
{
|
||||||
shouldVisitNames = true;
|
shouldVisitNames = true;
|
||||||
}
|
}
|
||||||
public int numProblemBindings = 0;
|
|
||||||
|
public List<IASTName> nameList = new ArrayList<IASTName>();
|
||||||
|
public List<String> problemBindings = new ArrayList<String>();
|
||||||
public int numNullBindings = 0;
|
public int numNullBindings = 0;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int visit( IASTName name ){
|
public int visit(IASTName name) {
|
||||||
//System.out.println("Visit Name: '" + name.toString() + "'");
|
nameList.add(name);
|
||||||
IBinding binding = name.resolveBinding();
|
IBinding binding = name.resolveBinding();
|
||||||
if (binding instanceof IProblemBinding) {
|
if (binding instanceof IProblemBinding)
|
||||||
numProblemBindings++;
|
problemBindings.add(name.toString());
|
||||||
//System.out.println("Problem Binding: " + name);
|
if (binding == null)
|
||||||
}
|
|
||||||
if (binding == null) {
|
|
||||||
numNullBindings++;
|
numNullBindings++;
|
||||||
//System.out.println("Null Binding: " + name);
|
|
||||||
}
|
|
||||||
return PROCESS_CONTINUE;
|
return PROCESS_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IASTName getName(int idx) {
|
||||||
|
if(idx < 0 || idx >= nameList.size())
|
||||||
|
return null;
|
||||||
|
return nameList.get(idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return nameList.size();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static IASTTranslationUnit parse(char[] code, ILanguage lang, boolean expectNoProblems, boolean checkBindings, int expectedProblemBindings) {
|
public static IASTTranslationUnit parse(char[] code, ILanguage lang, boolean expectNoProblems, boolean checkBindings, int expectedProblemBindings) {
|
||||||
CodeReader codeReader = new CodeReader(code);
|
CodeReader codeReader = new CodeReader(code);
|
||||||
return parse(codeReader, lang, new ScannerInfo(), null, expectNoProblems, checkBindings, expectedProblemBindings);
|
return parse(codeReader, lang, new ScannerInfo(), null, expectNoProblems, checkBindings, expectedProblemBindings, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IASTTranslationUnit parse(String code, ILanguage lang, boolean expectNoProblems, boolean checkBindings, int expectedProblemBindings) {
|
public static IASTTranslationUnit parse(String code, ILanguage lang, boolean expectNoProblems, boolean checkBindings, int expectedProblemBindings) {
|
||||||
return parse(code.toCharArray(), lang, expectNoProblems, checkBindings, expectedProblemBindings);
|
return parse(code.toCharArray(), lang, expectNoProblems, checkBindings, expectedProblemBindings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static IASTTranslationUnit parse(String code, ILanguage lang, boolean expectNoProblems) {
|
public static IASTTranslationUnit parse(String code, ILanguage lang, boolean expectNoProblems) {
|
||||||
return parse(code, lang, expectNoProblems, false, 0);
|
return parse(code, lang, expectNoProblems, false, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IASTTranslationUnit parse(String code, ILanguage lang, String[] problems) {
|
||||||
|
CodeReader codeReader = new CodeReader(code.toCharArray());
|
||||||
|
return parse(codeReader, lang, new ScannerInfo(), null, true, true, problems.length, problems);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static IASTTranslationUnit parse(CodeReader codeReader, ILanguage language, IScannerInfo scanInfo,
|
public static IASTTranslationUnit parse(CodeReader codeReader, ILanguage language, IScannerInfo scanInfo,
|
||||||
ICodeReaderFactory fileCreator, boolean expectNoProblems, boolean checkBindings, int expectedProblemBindings) {
|
ICodeReaderFactory fileCreator, boolean expectNoProblems,
|
||||||
|
boolean checkBindings, int expectedProblemBindings, String[] problems) {
|
||||||
testsRun++;
|
testsRun++;
|
||||||
|
|
||||||
IASTTranslationUnit tu;
|
IASTTranslationUnit tu;
|
||||||
|
@ -90,26 +108,32 @@ public class ParseHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
// should parse correctly first before we look at the bindings
|
// should parse correctly first before we look at the bindings
|
||||||
if(expectNoProblems )
|
if(expectNoProblems) {
|
||||||
{
|
|
||||||
if (CVisitor.getProblems(tu).length != 0) {
|
|
||||||
throw new AssertionFailedError(" CVisitor has AST Problems " ); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: actually collect preprocessor problems
|
// this should work for C++ also, CVisitor.getProblems() and CPPVisitor.getProblems() are exactly the same code!
|
||||||
|
if (CVisitor.getProblems(tu).length != 0) {
|
||||||
|
throw new AssertionFailedError(" CVisitor has AST Problems " );
|
||||||
|
}
|
||||||
if (tu.getPreprocessorProblems().length != 0) {
|
if (tu.getPreprocessorProblems().length != 0) {
|
||||||
throw new AssertionFailedError(" C TranslationUnit has Preprocessor Problems " ); //$NON-NLS-1$
|
throw new AssertionFailedError(" C TranslationUnit has Preprocessor Problems " );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolve all bindings
|
// resolve all bindings
|
||||||
if (checkBindings) {
|
if (checkBindings) {
|
||||||
|
|
||||||
NameResolver res = new NameResolver();
|
NameResolver res = new NameResolver();
|
||||||
tu.accept( res );
|
tu.accept( res );
|
||||||
if (res.numProblemBindings != expectedProblemBindings )
|
if(res.problemBindings.size() != expectedProblemBindings)
|
||||||
throw new AssertionFailedError("Expected " + expectedProblemBindings + " problem(s), encountered " + res.numProblemBindings ); //$NON-NLS-1$ //$NON-NLS-2$
|
throw new AssertionFailedError("Expected " + expectedProblemBindings + " problem(s), encountered " + res.problemBindings.size());
|
||||||
|
|
||||||
|
if(problems != null) {
|
||||||
|
for(int i = 0; i < problems.length; i++) {
|
||||||
|
String expected = problems[i];
|
||||||
|
String actual = res.problemBindings.get(i);
|
||||||
|
if(!expected.equals(actual))
|
||||||
|
throw new AssertionFailedError(String.format("Problem binding not equal, expected: %s, got: %s", expected, actual));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return tu;
|
return tu;
|
||||||
|
|
Loading…
Add table
Reference in a new issue