1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-01 05:15:43 +02:00

Patch for Devin Steffler.

- group the IASTPreprocessorIncludeStatements and everything included by them
- IASTPreprocessorIncludeStatement filter
This commit is contained in:
John Camelon 2005-02-09 21:01:43 +00:00
parent 2b9e8ed503
commit d9c4c2f676
6 changed files with 192 additions and 15 deletions

View file

@ -111,6 +111,14 @@
class="org.eclipse.cdt.ui.tests.DOMAST.PreprocessorFilter" class="org.eclipse.cdt.ui.tests.DOMAST.PreprocessorFilter"
id="org.eclipse.cdt.ui.tests.DOMAST.DOMAST.PreprocessorFilter"> id="org.eclipse.cdt.ui.tests.DOMAST.DOMAST.PreprocessorFilter">
</filter> </filter>
<filter
targetId="org.eclipse.cdt.ui.tests.DOMAST.DOMASTFilterGroup"
name="Include Statements Filter"
enabled="false"
description="Filter Include Statements"
class="org.eclipse.cdt.ui.tests.DOMAST.IncludeStatementFilter"
id="org.eclipse.cdt.ui.tests.DOMAST.DOMAST.IncludeStatementFilter">
</filter>
</extension> </extension>

View file

@ -38,6 +38,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction;
import org.eclipse.cdt.internal.core.parser.scanner2.LocationMap.ASTInclusionStatement;
/** /**
* @author dsteffle * @author dsteffle
@ -67,7 +68,7 @@ public class CPPPopulateASTViewAction extends CPPBaseVisitorAction implements IP
private void addRoot(IASTNode node) { private void addRoot(IASTNode node) {
if (node == null) return; if (node == null) return;
TreeParent parent = root.findParentOfNode(node); TreeParent parent = root.findTreeParentForNode(node);
if (parent == null) if (parent == null)
parent = root; parent = root;
@ -76,12 +77,12 @@ public class CPPPopulateASTViewAction extends CPPBaseVisitorAction implements IP
parent.addChild(tree); parent.addChild(tree);
// set filter flags // set filter flags
if (node instanceof IASTProblemHolder) if (node instanceof IASTProblemHolder || node instanceof IASTProblem)
tree.setFiltersFlag(TreeObject.FLAG_PROBLEM);
if (node instanceof IASTProblem)
tree.setFiltersFlag(TreeObject.FLAG_PROBLEM); tree.setFiltersFlag(TreeObject.FLAG_PROBLEM);
if (node instanceof IASTPreprocessorStatement) if (node instanceof IASTPreprocessorStatement)
tree.setFiltersFlag(TreeObject.FLAG_PREPROCESSOR); tree.setFiltersFlag(TreeObject.FLAG_PREPROCESSOR);
if (node instanceof IASTPreprocessorIncludeStatement)
tree.setFiltersFlag(TreeObject.FLAG_INCLUDE_STATEMENTS);
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -246,9 +247,40 @@ public class CPPPopulateASTViewAction extends CPPBaseVisitorAction implements IP
mergePreprocessorProblems(tu.getPreprocesorProblems()); mergePreprocessorProblems(tu.getPreprocesorProblems());
// merge include directives // merge include directives
mergeIncludeDirectives(tu.getIncludeDirectives()); IASTPreprocessorIncludeStatement[] includes = tu.getIncludeDirectives();
mergeIncludeDirectives(includes);
// group #includes
groupIncludes(includes);
} }
return root; return root;
} }
private void groupIncludes(IASTPreprocessorIncludeStatement[] includes) {
// get the tree model elements corresponding to the includes
TreeParent[] treeIncludes = new TreeParent[includes.length];
for (int i=0; i<treeIncludes.length; i++) {
treeIncludes[i] = root.findTreeObject(includes[i]);
}
// loop through the includes and make sure that all of the nodes
// that are children of the TU are in the proper include (based on offset)
TreeObject child = null;
for (int i=treeIncludes.length-1; i>=0; i--) {
if (treeIncludes[i] == null) continue;
for(int j=root.getChildren().length-1; j>=0; j--) {
child = root.getChildren()[j];
if (treeIncludes[i] != child &&
includes[i] instanceof ASTInclusionStatement &&
((ASTNode)child.getNode()).getOffset() >= ((ASTInclusionStatement)includes[i]).startOffset &&
((ASTNode)child.getNode()).getOffset() <= ((ASTInclusionStatement)includes[i]).endOffset) {
root.removeChild(child);
treeIncludes[i].addChild(child);
}
}
}
}
} }

View file

@ -34,6 +34,7 @@ import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator; import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction; import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction;
import org.eclipse.cdt.internal.core.parser.scanner2.LocationMap.ASTInclusionStatement;
/** /**
* @author dsteffle * @author dsteffle
@ -68,7 +69,7 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
nodeLocations[0].getNodeLength() > 0)) nodeLocations[0].getNodeLength() > 0))
return; return;
TreeParent parent = root.findParentOfNode(node); TreeParent parent = root.findTreeParentForNode(node);
if (parent == null) if (parent == null)
parent = root; parent = root;
@ -77,12 +78,12 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
parent.addChild(tree); parent.addChild(tree);
// set filter flags // set filter flags
if (node instanceof IASTProblemHolder) if (node instanceof IASTProblemHolder || node instanceof IASTProblem)
tree.setFiltersFlag(TreeObject.FLAG_PROBLEM);
if (node instanceof IASTProblem)
tree.setFiltersFlag(TreeObject.FLAG_PROBLEM); tree.setFiltersFlag(TreeObject.FLAG_PROBLEM);
if (node instanceof IASTPreprocessorStatement) if (node instanceof IASTPreprocessorStatement)
tree.setFiltersFlag(TreeObject.FLAG_PREPROCESSOR); tree.setFiltersFlag(TreeObject.FLAG_PREPROCESSOR);
if (node instanceof IASTPreprocessorIncludeStatement)
tree.setFiltersFlag(TreeObject.FLAG_INCLUDE_STATEMENTS);
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -225,10 +226,41 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
mergePreprocessorProblems(tu.getPreprocesorProblems()); mergePreprocessorProblems(tu.getPreprocesorProblems());
// merge include directives // merge include directives
mergeIncludeDirectives(tu.getIncludeDirectives()); IASTPreprocessorIncludeStatement[] includes = tu.getIncludeDirectives();
mergeIncludeDirectives(includes);
// group #includes
groupIncludes(includes);
} }
return root; return root;
} }
private void groupIncludes(IASTPreprocessorIncludeStatement[] includes) {
// get the tree model elements corresponding to the includes
TreeParent[] treeIncludes = new TreeParent[includes.length];
for (int i=0; i<treeIncludes.length; i++) {
treeIncludes[i] = root.findTreeObject(includes[i]);
}
// loop through the includes and make sure that all of the nodes
// that are children of the TU are in the proper include (based on offset)
TreeObject child = null;
for (int i=treeIncludes.length-1; i>=0; i--) {
if (treeIncludes[i] == null) continue;
for(int j=root.getChildren().length-1; j>=0; j--) {
child = root.getChildren()[j];
if (treeIncludes[i] != child &&
includes[i] instanceof ASTInclusionStatement &&
((ASTNode)child.getNode()).getOffset() >= ((ASTInclusionStatement)includes[i]).startOffset &&
((ASTNode)child.getNode()).getOffset() <= ((ASTInclusionStatement)includes[i]).endOffset) {
root.removeChild(child);
treeIncludes[i].addChild(child);
}
}
}
}
} }

View file

@ -0,0 +1,41 @@
/**********************************************************************
* Copyright (c) 2005 IBM Canada and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.ui.tests.DOMAST;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
/**
* @author dsteffle
*/
public class IncludeStatementFilter extends ViewerFilter {
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
*/
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (element instanceof TreeObject) {
int flag = ((TreeObject)element).getFiltersFlag() & TreeObject.FLAG_INCLUDE_STATEMENTS;
if (flag > 0) {
IASTNode node = ((TreeObject)element).getNode();
if (node instanceof IASTPreprocessorIncludeStatement)
return false;
return true;
}
}
return true;
}
}

View file

@ -122,6 +122,7 @@ public class TreeObject implements IAdaptable {
private int filterFlag = 0; private int filterFlag = 0;
public static final int FLAG_PROBLEM = 0x1; public static final int FLAG_PROBLEM = 0x1;
public static final int FLAG_PREPROCESSOR = 0x2; public static final int FLAG_PREPROCESSOR = 0x2;
public static final int FLAG_INCLUDE_STATEMENTS = 0x2;
public TreeObject(IASTNode node) { public TreeObject(IASTNode node) {
this.node = node; this.node = node;

View file

@ -14,6 +14,7 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/** /**
@ -60,14 +61,21 @@ public class TreeParent extends TreeObject {
return children.size()>0; return children.size()>0;
} }
private TreeParent findParentOfNode(TreeObject[] trees, IASTNode node) { /**
* Returns the TreeParent whose IASTNode is the parent of the IASTNode.
*
* @param trees
* @param node
* @return
*/
private TreeParent findTreeParentForNode(TreeObject[] trees, IASTNode node) {
for (int i=0; i<trees.length; i++) { for (int i=0; i<trees.length; i++) {
if (trees[i] != null && trees[i] instanceof TreeParent) { if (trees[i] != null && trees[i] instanceof TreeParent) {
if ( ((TreeParent)trees[i]).getNode() == node.getParent() ) { if ( ((TreeParent)trees[i]).getNode() == node.getParent() ) {
return (TreeParent)trees[i]; return (TreeParent)trees[i];
} else if ( ((TreeParent)trees[i]).hasChildren() ){ } else if ( ((TreeParent)trees[i]).hasChildren() ){
TreeParent tree = findParentOfNode( ((TreeParent)trees[i]).getChildren(), node ); TreeParent tree = findTreeParentForNode( ((TreeParent)trees[i]).getChildren(), node );
if (tree != null) return tree; if (tree != null) return tree;
} }
} }
@ -76,7 +84,13 @@ public class TreeParent extends TreeObject {
return null; // nothing found return null; // nothing found
} }
public TreeParent findParentOfNode(IASTNode node) { /**
* Returns the TreeParent whose IASTNode is the parent of the IASTNode.
*
* @param node
* @return
*/
public TreeParent findTreeParentForNode(IASTNode node) {
if (node == null || node.getParent() == null) return null; if (node == null || node.getParent() == null) return null;
Iterator itr = children.iterator(); Iterator itr = children.iterator();
@ -86,7 +100,7 @@ public class TreeParent extends TreeObject {
if ( ((TreeParent)o).getNode() == node.getParent() ) { if ( ((TreeParent)o).getNode() == node.getParent() ) {
return (TreeParent)o; return (TreeParent)o;
} else if ( ((TreeParent)o).hasChildren() ){ } else if ( ((TreeParent)o).hasChildren() ){
TreeParent tree = findParentOfNode( ((TreeParent)o).getChildren(), node ); TreeParent tree = findTreeParentForNode( ((TreeParent)o).getChildren(), node );
if (tree != null) return tree; if (tree != null) return tree;
} }
} }
@ -96,7 +110,7 @@ public class TreeParent extends TreeObject {
IASTNode parent = node.getParent(); IASTNode parent = node.getParent();
TreeParent tree = null; TreeParent tree = null;
while (parent != null && tree == null) { while (parent != null && tree == null) {
tree = findParentOfNode(parent); tree = findTreeParentForNode(parent);
if (tree != null) return tree; if (tree != null) return tree;
parent = parent.getParent(); parent = parent.getParent();
@ -104,6 +118,55 @@ public class TreeParent extends TreeObject {
return null; // nothing found return null; // nothing found
} }
/**
* Returns the TreeParent that corresponds to the IASTNode. This is the TreeParent
* that represents the IASTNode in the DOM AST View.
*
* @param trees
* @param node
* @return
*/
private TreeParent findTreeObject(TreeObject[] trees, IASTNode node) {
for (int i=0; i<trees.length; i++) {
if (trees[i] != null && trees[i] instanceof TreeParent) {
if ( ((TreeParent)trees[i]).getNode() == node ) {
return (TreeParent)trees[i];
} else if ( ((TreeParent)trees[i]).hasChildren() ){
TreeParent tree = findTreeObject( ((TreeParent)trees[i]).getChildren(), node );
if (tree != null) return tree;
}
}
}
return null; // nothing found
}
/**
* Returns the TreeParent that corresponds to the IASTNode. This is the TreeParent
* that represents the IASTNode in the DOM AST View.
*
* @param node
* @return
*/
public TreeParent findTreeObject(IASTNode node) {
if (node == null) return null;
Iterator itr = children.iterator();
while (itr.hasNext()) {
Object o = itr.next();
if (o != null && o instanceof TreeParent) {
if ( ((TreeParent)o).getNode() == node ) {
return (TreeParent)o;
} else if ( ((TreeParent)o).hasChildren() ){
TreeParent tree = findTreeObject( ((TreeParent)o).getChildren(), node );
if (tree != null) return tree;
}
}
}
return null; // nothing found
}
} }