Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] C(PP)Visitor.getProblems(IASTTranslationUnit)


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.

Devin Steffler
IBM's Eclipse CDT
Ottawa (Palladium), Ontario, Canada


Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java,v
retrieving revision 1.6
diff -u -r1.6 CVisitor.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java	21 Dec 2004 16:27:04 -0000	1.6
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java	10 Jan 2005 20:34:46 -0000
@@ -48,6 +48,8 @@
 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 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;
@@ -1228,5 +1316,12 @@
 		}
 		
 		return null;
+	}
+	
+	public static IASTProblem[] getProblems(IASTTranslationUnit tu) {
+		CollectProblemsAction action = new CollectProblemsAction();
+		visitTranslationUnit(tu, action);
+		
+		return action.getProblems();
 	}
 }
Index: parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java,v
retrieving revision 1.10
diff -u -r1.10 CPPVisitor.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java	24 Dec 2004 00:13:43 -0000	1.10
+++ parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java	10 Jan 2005 20:34:46 -0000
@@ -56,6 +56,8 @@
 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.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 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 @@
 	    }
 	    return null;
 	}
+	
+	public static IASTProblem[] getProblems(IASTTranslationUnit tu) {
+		CollectProblemsAction action = new CollectProblemsAction();
+		visitTranslationUnit(tu, action);
+		
+		return action.getProblems();
+	}
+
 }

Back to the top