mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-23 17:05:26 +02:00
Patch for Devin Steffler.
- group the IASTPreprocessorIncludeStatements and everything included by them - IASTPreprocessorIncludeStatement filter
This commit is contained in:
parent
2b9e8ed503
commit
d9c4c2f676
6 changed files with 192 additions and 15 deletions
|
@ -111,6 +111,14 @@
|
|||
class="org.eclipse.cdt.ui.tests.DOMAST.PreprocessorFilter"
|
||||
id="org.eclipse.cdt.ui.tests.DOMAST.DOMAST.PreprocessorFilter">
|
||||
</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>
|
||||
|
||||
|
||||
|
|
|
@ -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.internal.core.dom.parser.ASTNode;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction;
|
||||
import org.eclipse.cdt.internal.core.parser.scanner2.LocationMap.ASTInclusionStatement;
|
||||
|
||||
/**
|
||||
* @author dsteffle
|
||||
|
@ -67,7 +68,7 @@ public class CPPPopulateASTViewAction extends CPPBaseVisitorAction implements IP
|
|||
private void addRoot(IASTNode node) {
|
||||
if (node == null) return;
|
||||
|
||||
TreeParent parent = root.findParentOfNode(node);
|
||||
TreeParent parent = root.findTreeParentForNode(node);
|
||||
|
||||
if (parent == null)
|
||||
parent = root;
|
||||
|
@ -76,12 +77,12 @@ public class CPPPopulateASTViewAction extends CPPBaseVisitorAction implements IP
|
|||
parent.addChild(tree);
|
||||
|
||||
// set filter flags
|
||||
if (node instanceof IASTProblemHolder)
|
||||
tree.setFiltersFlag(TreeObject.FLAG_PROBLEM);
|
||||
if (node instanceof IASTProblem)
|
||||
if (node instanceof IASTProblemHolder || node instanceof IASTProblem)
|
||||
tree.setFiltersFlag(TreeObject.FLAG_PROBLEM);
|
||||
if (node instanceof IASTPreprocessorStatement)
|
||||
tree.setFiltersFlag(TreeObject.FLAG_PREPROCESSOR);
|
||||
if (node instanceof IASTPreprocessorIncludeStatement)
|
||||
tree.setFiltersFlag(TreeObject.FLAG_INCLUDE_STATEMENTS);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
@ -246,9 +247,40 @@ public class CPPPopulateASTViewAction extends CPPBaseVisitorAction implements IP
|
|||
mergePreprocessorProblems(tu.getPreprocesorProblems());
|
||||
|
||||
// merge include directives
|
||||
mergeIncludeDirectives(tu.getIncludeDirectives());
|
||||
IASTPreprocessorIncludeStatement[] includes = tu.getIncludeDirectives();
|
||||
mergeIncludeDirectives(includes);
|
||||
|
||||
// group #includes
|
||||
groupIncludes(includes);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.internal.core.dom.parser.ASTNode;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction;
|
||||
import org.eclipse.cdt.internal.core.parser.scanner2.LocationMap.ASTInclusionStatement;
|
||||
|
||||
/**
|
||||
* @author dsteffle
|
||||
|
@ -68,7 +69,7 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
|
|||
nodeLocations[0].getNodeLength() > 0))
|
||||
return;
|
||||
|
||||
TreeParent parent = root.findParentOfNode(node);
|
||||
TreeParent parent = root.findTreeParentForNode(node);
|
||||
|
||||
if (parent == null)
|
||||
parent = root;
|
||||
|
@ -77,12 +78,12 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
|
|||
parent.addChild(tree);
|
||||
|
||||
// set filter flags
|
||||
if (node instanceof IASTProblemHolder)
|
||||
tree.setFiltersFlag(TreeObject.FLAG_PROBLEM);
|
||||
if (node instanceof IASTProblem)
|
||||
if (node instanceof IASTProblemHolder || node instanceof IASTProblem)
|
||||
tree.setFiltersFlag(TreeObject.FLAG_PROBLEM);
|
||||
if (node instanceof IASTPreprocessorStatement)
|
||||
tree.setFiltersFlag(TreeObject.FLAG_PREPROCESSOR);
|
||||
if (node instanceof IASTPreprocessorIncludeStatement)
|
||||
tree.setFiltersFlag(TreeObject.FLAG_INCLUDE_STATEMENTS);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
@ -225,10 +226,41 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
|
|||
mergePreprocessorProblems(tu.getPreprocesorProblems());
|
||||
|
||||
// merge include directives
|
||||
mergeIncludeDirectives(tu.getIncludeDirectives());
|
||||
IASTPreprocessorIncludeStatement[] includes = tu.getIncludeDirectives();
|
||||
mergeIncludeDirectives(includes);
|
||||
|
||||
// group #includes
|
||||
groupIncludes(includes);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -122,6 +122,7 @@ public class TreeObject implements IAdaptable {
|
|||
private int filterFlag = 0;
|
||||
public static final int FLAG_PROBLEM = 0x1;
|
||||
public static final int FLAG_PREPROCESSOR = 0x2;
|
||||
public static final int FLAG_INCLUDE_STATEMENTS = 0x2;
|
||||
|
||||
public TreeObject(IASTNode node) {
|
||||
this.node = node;
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.util.ArrayList;
|
|||
import java.util.Iterator;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
|
@ -60,14 +61,21 @@ public class TreeParent extends TreeObject {
|
|||
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++) {
|
||||
|
||||
if (trees[i] != null && trees[i] instanceof TreeParent) {
|
||||
if ( ((TreeParent)trees[i]).getNode() == node.getParent() ) {
|
||||
return (TreeParent)trees[i];
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +84,13 @@ public class TreeParent extends TreeObject {
|
|||
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;
|
||||
|
||||
Iterator itr = children.iterator();
|
||||
|
@ -86,7 +100,7 @@ public class TreeParent extends TreeObject {
|
|||
if ( ((TreeParent)o).getNode() == node.getParent() ) {
|
||||
return (TreeParent)o;
|
||||
} else if ( ((TreeParent)o).hasChildren() ){
|
||||
TreeParent tree = findParentOfNode( ((TreeParent)o).getChildren(), node );
|
||||
TreeParent tree = findTreeParentForNode( ((TreeParent)o).getChildren(), node );
|
||||
if (tree != null) return tree;
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +110,7 @@ public class TreeParent extends TreeObject {
|
|||
IASTNode parent = node.getParent();
|
||||
TreeParent tree = null;
|
||||
while (parent != null && tree == null) {
|
||||
tree = findParentOfNode(parent);
|
||||
tree = findTreeParentForNode(parent);
|
||||
if (tree != null) return tree;
|
||||
|
||||
parent = parent.getParent();
|
||||
|
@ -104,6 +118,55 @@ public class TreeParent extends TreeObject {
|
|||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue