mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Patch for Devin Steffler.
This patch adds getProblems() to the CVisitor and CPPVisitor classes. This is required to get IASTProblems since the new parser doesn't use a callback anymore. Also, updated tests accordingly.
This commit is contained in:
parent
0d121179f8
commit
0f2803f134
5 changed files with 232 additions and 6 deletions
|
@ -28,6 +28,7 @@ import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTProblem;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
||||
|
@ -103,8 +104,16 @@ public class AST2BaseTest extends TestCase {
|
|||
if( parser2.encounteredError() )
|
||||
throw new ParserException( "FAILURE"); //$NON-NLS-1$
|
||||
|
||||
//TODO add in assertion here to visit all problems
|
||||
|
||||
if( lang == ParserLanguage.C )
|
||||
{
|
||||
IASTProblem [] problems = CVisitor.getProblems(tu);
|
||||
assertEquals( problems.length, 0 );
|
||||
}
|
||||
else if ( lang == ParserLanguage.CPP )
|
||||
{
|
||||
IASTProblem [] problems = CPPVisitor.getProblems(tu);
|
||||
assertEquals( problems.length, 0 );
|
||||
}
|
||||
|
||||
return tu;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTProblem;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
|
@ -59,6 +60,7 @@ import org.eclipse.cdt.core.parser.ScannerInfo;
|
|||
import org.eclipse.cdt.internal.core.dom.SavedCodeReaderFactory;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ISourceCodeParser;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.ANSICParserExtensionConfiguration;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.GCCParserExtensionConfiguration;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.ICParserExtensionConfiguration;
|
||||
|
@ -165,7 +167,16 @@ public class CompleteParser2Tests extends TestCase {
|
|||
throw new ParserException("FAILURE"); //$NON-NLS-1$
|
||||
if (expectedToPass)
|
||||
{
|
||||
//TODO visit translation unit and ensure that there aren't any problems
|
||||
if( lang == ParserLanguage.C )
|
||||
{
|
||||
IASTProblem [] problems = CVisitor.getProblems(tu);
|
||||
assertEquals( problems.length, 0 );
|
||||
}
|
||||
else if ( lang == ParserLanguage.CPP )
|
||||
{
|
||||
IASTProblem [] problems = CPPVisitor.getProblems(tu);
|
||||
assertEquals( problems.length, 0 );
|
||||
}
|
||||
}
|
||||
return tu;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ import java.io.Writer;
|
|||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTProblem;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.parser.CodeReader;
|
||||
import org.eclipse.cdt.core.parser.IScanner;
|
||||
import org.eclipse.cdt.core.parser.IScannerInfo;
|
||||
|
@ -25,10 +27,12 @@ import org.eclipse.cdt.core.parser.ScannerInfo;
|
|||
import org.eclipse.cdt.internal.core.dom.SavedCodeReaderFactory;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ISourceCodeParser;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.ANSICParserExtensionConfiguration;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.GCCParserExtensionConfiguration;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.ICParserExtensionConfiguration;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPParserExtensionConfiguration;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
|
||||
|
@ -1383,12 +1387,21 @@ public class QuickParser2Tests extends TestCase {
|
|||
parser2 = new GNUCSourceParser(scanner, ParserMode.QUICK_PARSE,
|
||||
NULL_LOG, config);
|
||||
}
|
||||
parser2.parse();
|
||||
IASTTranslationUnit tu = parser2.parse();
|
||||
if (parser2.encounteredError() && expectedToPass)
|
||||
throw new ParserException("FAILURE"); //$NON-NLS-1$
|
||||
if (expectedToPass)
|
||||
{
|
||||
//TODO need visitor to ensure that there aren't any problems
|
||||
if( lang == ParserLanguage.C )
|
||||
{
|
||||
IASTProblem [] problems = CVisitor.getProblems(tu);
|
||||
assertEquals( problems.length, 0 );
|
||||
}
|
||||
else if ( lang == ParserLanguage.CPP )
|
||||
{
|
||||
IASTProblem [] problems = CPPVisitor.getProblems(tu);
|
||||
assertEquals( problems.length, 0 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,8 @@ import org.eclipse.cdt.core.dom.ast.IASTName;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTProblem;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTProblemHolder;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||
|
@ -125,6 +127,92 @@ public class CVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
public static class CollectProblemsAction extends CBaseVisitorAction {
|
||||
{
|
||||
processDeclarations = true;
|
||||
processExpressions = true;
|
||||
processStatements = true;
|
||||
processTypeIds = true;
|
||||
}
|
||||
|
||||
private static final int DEFAULT_CHILDREN_LIST_SIZE = 8;
|
||||
private IASTProblem[] problems = null;
|
||||
int numFound = 0;
|
||||
|
||||
public CollectProblemsAction() {
|
||||
problems = new IASTProblem[DEFAULT_CHILDREN_LIST_SIZE];
|
||||
}
|
||||
|
||||
private void addProblem(IASTProblem problem) {
|
||||
if( problems.length == numFound ) // if the found array is full, then double the array
|
||||
{
|
||||
IASTProblem [] old = problems;
|
||||
problems = new IASTProblem[ old.length * 2 ];
|
||||
for( int j = 0; j < old.length; ++j )
|
||||
problems[j] = old[j];
|
||||
}
|
||||
problems[numFound++] = problem;
|
||||
}
|
||||
|
||||
private IASTProblem[] removeNullFromProblems() {
|
||||
if (problems[problems.length-1] != null) { // if the last element in the list is not null then return the list
|
||||
return problems;
|
||||
} else if (problems[0] == null) { // if the first element in the list is null, then return empty list
|
||||
return new IASTProblem[0];
|
||||
}
|
||||
|
||||
IASTProblem[] results = new IASTProblem[numFound];
|
||||
for (int i=0; i<results.length; i++)
|
||||
results[i] = problems[i];
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public IASTProblem[] getProblems() {
|
||||
return removeNullFromProblems();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration)
|
||||
*/
|
||||
public boolean processDeclaration(IASTDeclaration declaration) {
|
||||
if ( declaration instanceof IASTProblemHolder )
|
||||
addProblem(((IASTProblemHolder)declaration).getProblem());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processExpression(org.eclipse.cdt.core.dom.ast.IASTExpression)
|
||||
*/
|
||||
public boolean processExpression(IASTExpression expression) {
|
||||
if ( expression instanceof IASTProblemHolder )
|
||||
addProblem(((IASTProblemHolder)expression).getProblem());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processStatement(org.eclipse.cdt.core.dom.ast.IASTStatement)
|
||||
*/
|
||||
public boolean processStatement(IASTStatement statement) {
|
||||
if ( statement instanceof IASTProblemHolder )
|
||||
addProblem(((IASTProblemHolder)statement).getProblem());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processTypeId(org.eclipse.cdt.core.dom.ast.IASTTypeId)
|
||||
*/
|
||||
public boolean processTypeId(IASTTypeId typeId) {
|
||||
if ( typeId instanceof IASTProblemHolder )
|
||||
addProblem(((IASTProblemHolder)typeId).getProblem());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//lookup bits
|
||||
private static final int COMPLETE = 0;
|
||||
private static final int CURRENT_SCOPE = 1;
|
||||
|
@ -1229,4 +1317,11 @@ public class CVisitor {
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IASTProblem[] getProblems(IASTTranslationUnit tu) {
|
||||
CollectProblemsAction action = new CollectProblemsAction();
|
||||
visitTranslationUnit(tu, action);
|
||||
|
||||
return action.getProblems();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,6 +56,8 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTPointer;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTProblem;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTProblemHolder;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
||||
|
@ -110,6 +112,8 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope;
|
|||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CollectProblemsAction;
|
||||
|
||||
/**
|
||||
* @author aniefer
|
||||
|
@ -504,6 +508,92 @@ public class CPPVisitor {
|
|||
public boolean processBaseSpecifier(ICPPASTBaseSpecifier specifier) { return true; }
|
||||
}
|
||||
|
||||
public static class CollectProblemsAction extends CPPBaseVisitorAction {
|
||||
{
|
||||
processDeclarations = true;
|
||||
processExpressions = true;
|
||||
processStatements = true;
|
||||
processTypeIds = true;
|
||||
}
|
||||
|
||||
private static final int DEFAULT_CHILDREN_LIST_SIZE = 8;
|
||||
private IASTProblem[] problems = null;
|
||||
int numFound = 0;
|
||||
|
||||
public CollectProblemsAction() {
|
||||
problems = new IASTProblem[DEFAULT_CHILDREN_LIST_SIZE];
|
||||
}
|
||||
|
||||
private void addProblem(IASTProblem problem) {
|
||||
if( problems.length == numFound ) // if the found array is full, then double the array
|
||||
{
|
||||
IASTProblem [] old = problems;
|
||||
problems = new IASTProblem[ old.length * 2 ];
|
||||
for( int j = 0; j < old.length; ++j )
|
||||
problems[j] = old[j];
|
||||
}
|
||||
problems[numFound++] = problem;
|
||||
}
|
||||
|
||||
private IASTProblem[] removeNullFromProblems() {
|
||||
if (problems[problems.length-1] != null) { // if the last element in the list is not null then return the list
|
||||
return problems;
|
||||
} else if (problems[0] == null) { // if the first element in the list is null, then return empty list
|
||||
return new IASTProblem[0];
|
||||
}
|
||||
|
||||
IASTProblem[] results = new IASTProblem[numFound];
|
||||
for (int i=0; i<results.length; i++)
|
||||
results[i] = problems[i];
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public IASTProblem[] getProblems() {
|
||||
return removeNullFromProblems();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration)
|
||||
*/
|
||||
public boolean processDeclaration(IASTDeclaration declaration) {
|
||||
if ( declaration instanceof IASTProblemHolder )
|
||||
addProblem(((IASTProblemHolder)declaration).getProblem());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processExpression(org.eclipse.cdt.core.dom.ast.IASTExpression)
|
||||
*/
|
||||
public boolean processExpression(IASTExpression expression) {
|
||||
if ( expression instanceof IASTProblemHolder )
|
||||
addProblem(((IASTProblemHolder)expression).getProblem());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processStatement(org.eclipse.cdt.core.dom.ast.IASTStatement)
|
||||
*/
|
||||
public boolean processStatement(IASTStatement statement) {
|
||||
if ( statement instanceof IASTProblemHolder )
|
||||
addProblem(((IASTProblemHolder)statement).getProblem());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processTypeId(org.eclipse.cdt.core.dom.ast.IASTTypeId)
|
||||
*/
|
||||
public boolean processTypeId(IASTTypeId typeId) {
|
||||
if ( typeId instanceof IASTProblemHolder )
|
||||
addProblem(((IASTProblemHolder)typeId).getProblem());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void visitTranslationUnit( IASTTranslationUnit tu, CPPBaseVisitorAction action ){
|
||||
IASTDeclaration [] decls = tu.getDeclarations();
|
||||
for( int i = 0; i < decls.length; i++ ){
|
||||
|
@ -1056,4 +1146,12 @@ public class CPPVisitor {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IASTProblem[] getProblems(IASTTranslationUnit tu) {
|
||||
CollectProblemsAction action = new CollectProblemsAction();
|
||||
visitTranslationUnit(tu, action);
|
||||
|
||||
return action.getProblems();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue