1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

Patch for Devin Steffler.

Further updates to DOM AST View.
This commit is contained in:
John Camelon 2005-01-31 20:17:59 +00:00
parent 4017f92c70
commit b33b116998
5 changed files with 256 additions and 14 deletions

View file

@ -21,6 +21,7 @@ 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.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
@ -199,6 +200,13 @@ public class CPPPopulateASTViewAction extends CPPBaseVisitorAction implements IP
}
}
private void mergeIncludeDirectives(IASTPreprocessorIncludeStatement[] includes) {
for(int i=0; i<includes.length; i++) {
if (includes[i] instanceof ASTNode)
mergeNode((ASTNode)includes[i]);
}
}
public TreeParent getTree() {
if (root.getNode() instanceof IASTTranslationUnit) {
IASTTranslationUnit tu = (IASTTranslationUnit)root.getNode();
@ -209,6 +217,8 @@ public class CPPPopulateASTViewAction extends CPPBaseVisitorAction implements IP
// merge preprocessor problems to the tree
mergePreprocessorProblems(tu.getPreprocesorProblems());
// merge include directives
mergeIncludeDirectives(tu.getIncludeDirectives());
}
return root;

View file

@ -21,12 +21,15 @@ 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.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer;
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.ASTObjectMacro;
@ -121,6 +124,12 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
*/
public int processInitializer(IASTInitializer initializer) {
addRoot(initializer);
if (initializer instanceof ICASTDesignatedInitializer) {
ICASTDesignator[] designators = ((ICASTDesignatedInitializer)initializer).getDesignators();
for (int i=0; i<designators.length; i++) {
addRoot(designators[i]);
}
}
return PROCESS_CONTINUE;
}
@ -178,6 +187,13 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
mergeNode((ASTNode)problems[i]);
}
}
private void mergeIncludeDirectives(IASTPreprocessorIncludeStatement[] includes) {
for(int i=0; i<includes.length; i++) {
if (includes[i] instanceof ASTNode)
mergeNode((ASTNode)includes[i]);
}
}
public TreeParent getTree() {
if (root.getNode() instanceof IASTTranslationUnit) {
@ -189,6 +205,8 @@ public class CPopulateASTViewAction extends CBaseVisitorAction implements IPopul
// merge preprocessor problems to the tree
mergePreprocessorProblems(tu.getPreprocesorProblems());
// merge include directives
mergeIncludeDirectives(tu.getIncludeDirectives());
}
return root;

View file

@ -24,6 +24,7 @@ import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTProblemDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -244,7 +245,10 @@ private static final String REFRESH_DOM_AST = "Refresh DOM AST"; //$NON-NLS-1
} else if (node instanceof IASTProblem) {
imageKey = DOMASTPluginImages.IMG_IASTProblem;
} else if (node instanceof IASTSimpleDeclaration) {
imageKey = DOMASTPluginImages.IMG_IASTSimpleDeclaration;
if (node instanceof IASTProblemDeclaration)
imageKey = DOMASTPluginImages.IMG_IASTProblem;
else
imageKey = DOMASTPluginImages.IMG_IASTSimpleDeclaration;
} else if (node instanceof IASTStatement) {
imageKey = DOMASTPluginImages.IMG_IASTStatement;
} else if (node instanceof IASTTranslationUnit) {
@ -311,7 +315,7 @@ private static final String REFRESH_DOM_AST = "Refresh DOM AST"; //$NON-NLS-1
viewer.setContentProvider(new ViewContentProvider(((CEditor) part)
.getInputFile()));
else
viewer.setContentProvider(new ViewContentProvider(file));
viewer.setContentProvider(new ViewContentProvider(null)); // don't attempt to create a view based on old file info
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setInput(getViewSite());
@ -496,42 +500,54 @@ private static final String REFRESH_DOM_AST = "Refresh DOM AST"; //$NON-NLS-1
}
private class DisplayDeclarationsAction extends DisplaySearchResultAction {
public void run() {
private static final String STRING_QUOTE = "\""; //$NON-NLS-1$
public void run() {
ISelection selection = viewer.getSelection();
if (selection instanceof IStructuredSelection &&
((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() instanceof IASTName) {
IASTName name = (IASTName)((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode();
StringBuffer pattern = new StringBuffer(STRING_QUOTE);
if (name.toString() != null)
pattern.append(name.toString());
pattern.append(STRING_QUOTE);
if (lang == ParserLanguage.CPP) {
// TODO Devin when implemented in CPPVisitor
} else {
IASTName[] names = CVisitor.getDeclarations( ((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode().getTranslationUnit(), ((IASTName)((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()).resolveBinding() );
displayNames(names, OPEN_DECLARATIONS);
IASTName[] names = CVisitor.getDeclarations( ((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode().getTranslationUnit(), name.resolveBinding() );
displayNames(names, OPEN_DECLARATIONS, pattern.toString());
}
}
}
}
private class DisplayReferencesAction extends DisplaySearchResultAction {
private static final String STRING_QUOTE = "\""; //$NON-NLS-1$
public void run() {
ISelection selection = viewer.getSelection();
if (selection instanceof IStructuredSelection &&
((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() instanceof IASTName) {
IASTName name = (IASTName)((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode();
StringBuffer pattern = new StringBuffer(STRING_QUOTE);
if (name.toString() != null)
pattern.append(name.toString());
pattern.append(STRING_QUOTE);
if (lang == ParserLanguage.CPP) {
// TODO Devin when implemented in CPPVisitor
} else {
IASTName[] names = CVisitor.getReferences( ((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode().getTranslationUnit(), ((IASTName)((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()).resolveBinding() );
displayNames(names, OPEN_REFERENCES);
IASTName[] names = CVisitor.getReferences( ((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode().getTranslationUnit(), name.resolveBinding() );
displayNames(names, OPEN_REFERENCES, pattern.toString());
}
}
}
}
private class DisplaySearchResultAction extends Action {
protected void displayNames(IASTName[] names, String queryLabel) {
DOMQuery job = new DOMQuery(names, queryLabel);
protected void displayNames(IASTName[] names, String queryLabel, String pattern) {
DOMQuery job = new DOMQuery(names, queryLabel, pattern);
NewSearchUI.activateSearchResultView();
NewSearchUI.runQuery(job);
}

View file

@ -47,8 +47,8 @@ public class DOMQuery extends CSearchQuery implements ISearchQuery {
/**
*
*/
public DOMQuery(IASTName[] names, String queryLabel) {
super(CTestPlugin.getWorkspace(), null, null, BLANK_STRING, null); //$NON-NLS-1$
public DOMQuery(IASTName[] names, String queryLabel, String pattern) {
super(CTestPlugin.getWorkspace(), pattern, false, null, null, null, queryLabel, null);
this.names = names;
this.queryLabel = queryLabel;
}

View file

@ -10,22 +10,69 @@
**********************************************************************/
package org.eclipse.cdt.ui.tests.DOMAST;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorObjectStyleMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTFieldDesignator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTBinaryExpression;
import org.eclipse.cdt.core.parser.ast.IASTDesignator;
import org.eclipse.core.runtime.IAdaptable;
/**
* @author dsteffle
*/
public class TreeObject implements IAdaptable {
private static final String FILE_SEPARATOR = "\\";
private static final String DASH = "-";
private static final String OP_NOTEQUALS = "!="; //$NON-NLS-1$
private static final String OP_EQUALS = "=="; //$NON-NLS-1$
private static final String OP_BINARYXORASSIGN = "^="; //$NON-NLS-1$
private static final String OP_BINARYANDASSIGN = "&="; //$NON-NLS-1$
private static final String OP_BINARYORASSIGN = "|="; //$NON-NLS-1$
private static final String OP_SHIFTRIGHTASSIGN = ">>="; //$NON-NLS-1$
private static final String OP_SHIFTLEFTASSIGN = "<<="; //$NON-NLS-1$
private static final String OP_MINUSASSIGN = "-="; //$NON-NLS-1$
private static final String OP_PLUSASSIGN = "+="; //$NON-NLS-1$
private static final String OP_MODULOASSIGN = "%="; //$NON-NLS-1$
private static final String OP_DIVIDEASSIGN = "/="; //$NON-NLS-1$
private static final String OP_MULTIPLYASSIGN = "*="; //$NON-NLS-1$
private static final String OP_ASSIGN = "="; //$NON-NLS-1$
private static final String OP_LOGICALOR = "||"; //$NON-NLS-1$
private static final String OP_LOGICALAND = "&&"; //$NON-NLS-1$
private static final String OP_BINARYOR = "|"; //$NON-NLS-1$
private static final String OP_BINARYXOR = "^"; //$NON-NLS-1$
private static final String OP_BINARYAND = "&"; //$NON-NLS-1$
private static final String OP_GREATEREQUAL = ">="; //$NON-NLS-1$
private static final String OP_LESSEQUAL = "<="; //$NON-NLS-1$
private static final String OP_GREATERTHAN = ">"; //$NON-NLS-1$
private static final String OP_LESSTHAN = "<"; //$NON-NLS-1$
private static final String OP_SHIFTRIGHT = ">>"; //$NON-NLS-1$
private static final String OP_SHIFTLEFT = "<<"; //$NON-NLS-1$
private static final String OP_MINUS = DASH; //$NON-NLS-1$
private static final String OP_PLUS = "+"; //$NON-NLS-1$
private static final String OP_MODULO = "%"; //$NON-NLS-1$
private static final String OP_DIVIDE = "/"; //$NON-NLS-1$
private static final String OP_MULTIPLY = "*"; //$NON-NLS-1$
private static final String OP_MIN = "<?"; //$NON-NLS-1$
private static final String OP_MAX = ">?"; //$NON-NLS-1$
private static final String OP_PMDOT = "."; //$NON-NLS-1$
private static final String OP_PMARROW = "->"; //$NON-NLS-1$
private static final String FILE_SEPARATOR = "\\"; //$NON-NLS-1$
public static final String BLANK_STRING = ""; //$NON-NLS-1$
private static final String IGCCAST_PREFIX = "IGCCAST"; //$NON-NLS-1$
private static final String IGNUAST_PREFIX = "IGNUAST"; //$NON-NLS-1$
@ -35,7 +82,7 @@ public class TreeObject implements IAdaptable {
private static final String IAST_PREFIX = "IAST"; //$NON-NLS-1$
private static final String START_OF_LIST = ": "; //$NON-NLS-1$
private static final String LIST_SEPARATOR = ", "; //$NON-NLS-1$
private static final String FILENAME_SEPARATOR = "."; //$NON-NLS-1$
private static final String FILENAME_SEPARATOR = OP_PMDOT; //$NON-NLS-1$
private IASTNode node = null;
private TreeParent parent;
@ -119,12 +166,163 @@ public class TreeObject implements IAdaptable {
buffer.append( START_OF_LIST );
buffer.append( ((IASTDeclSpecifier)node).getUnpreprocessedSignature() );
return buffer.toString();
} else if ( node instanceof IASTPreprocessorIncludeStatement ) {
String path = ((IASTPreprocessorIncludeStatement)node).getPath();
int lastSlash = path.lastIndexOf(FILE_SEPARATOR) + 1;
buffer.append( START_OF_LIST );
buffer.append( path.substring(lastSlash) );
} else if ( node instanceof IASTPreprocessorObjectStyleMacroDefinition ) {
String name = ((IASTPreprocessorObjectStyleMacroDefinition)node).getName().toString();
if (name != null) {
buffer.append( START_OF_LIST );
buffer.append( name );
}
} else if ( node instanceof IASTLiteralExpression ) {
buffer.append(START_OF_LIST);
buffer.append(node.toString());
} else if ( node instanceof IASTBinaryExpression ) {
buffer.append(START_OF_LIST);
buffer.append( getBinaryOperatorString( (IASTBinaryExpression)node) );
} else if ( node instanceof ICASTDesignator ) {
if (node instanceof ICASTArrayDesignator) {
buffer.append(START_OF_LIST);
buffer.append(((ICASTArrayDesignator)node).getSubscriptExpression()); // TODO Devin need to test this!
} else if (node instanceof ICASTFieldDesignator) {
buffer.append(START_OF_LIST);
buffer.append(((ICASTFieldDesignator)node).getName());
} else if (node instanceof IGCCASTArrayRangeDesignator) {
buffer.append(START_OF_LIST);
buffer.append(((IGCCASTArrayRangeDesignator)node).getRangeCeiling());
buffer.append(DASH);
buffer.append(((IGCCASTArrayRangeDesignator)node).getRangeFloor());
}
} else if ( node instanceof IASTArrayModifier ) {
buffer.append(START_OF_LIST);
buffer.append(((IASTArrayModifier)node).getConstantExpression());
}
return buffer.toString();
}
private String getBinaryOperatorString(IASTBinaryExpression be) {
int op = be.getOperator();
String opString = BLANK_STRING;
if (be instanceof ICPPASTBinaryExpression) {
switch(op) {
case ICPPASTBinaryExpression.op_pmarrow:
opString = OP_PMARROW;
break;
case ICPPASTBinaryExpression.op_pmdot:
opString = OP_PMDOT;
break;
}
} else if (be instanceof IGPPASTBinaryExpression) {
switch(op) {
case IGPPASTBinaryExpression.op_max:
opString = OP_MAX;
break;
case IGPPASTBinaryExpression.op_min:
opString = OP_MIN;
break;
}
}
if (!opString.equals(BLANK_STRING)) return opString;
switch(op) {
case IASTBinaryExpression.op_multiply:
opString = OP_MULTIPLY;
break;
case IASTBinaryExpression.op_divide:
opString = OP_DIVIDE;
break;
case IASTBinaryExpression.op_modulo:
opString = OP_MODULO;
break;
case IASTBinaryExpression.op_plus:
opString = OP_PLUS;
break;
case IASTBinaryExpression.op_minus:
opString = OP_MINUS;
break;
case IASTBinaryExpression.op_shiftLeft:
opString = OP_SHIFTLEFT;
break;
case IASTBinaryExpression.op_shiftRight:
opString = OP_SHIFTRIGHT;
break;
case IASTBinaryExpression.op_lessThan:
opString = OP_LESSTHAN;
break;
case IASTBinaryExpression.op_greaterThan:
opString = OP_GREATERTHAN;
break;
case IASTBinaryExpression.op_lessEqual:
opString = OP_LESSEQUAL;
break;
case IASTBinaryExpression.op_greaterEqual:
opString = OP_GREATEREQUAL;
break;
case IASTBinaryExpression.op_binaryAnd:
opString = OP_BINARYAND;
break;
case IASTBinaryExpression.op_binaryXor:
opString = OP_BINARYXOR;
break;
case IASTBinaryExpression.op_binaryOr:
opString = OP_BINARYOR;
break;
case IASTBinaryExpression.op_logicalAnd:
opString = OP_LOGICALAND;
break;
case IASTBinaryExpression.op_logicalOr:
opString = OP_LOGICALOR;
break;
case IASTBinaryExpression.op_assign:
opString = OP_ASSIGN;
break;
case IASTBinaryExpression.op_multiplyAssign:
opString = OP_MULTIPLYASSIGN;
break;
case IASTBinaryExpression.op_divideAssign:
opString = OP_DIVIDEASSIGN;
break;
case IASTBinaryExpression.op_moduloAssign:
opString = OP_MODULOASSIGN;
break;
case IASTBinaryExpression.op_plusAssign:
opString = OP_PLUSASSIGN;
break;
case IASTBinaryExpression.op_minusAssign:
opString = OP_MINUSASSIGN;
break;
case IASTBinaryExpression.op_shiftLeftAssign:
opString = OP_SHIFTLEFTASSIGN;
break;
case IASTBinaryExpression.op_shiftRightAssign:
opString = OP_SHIFTRIGHTASSIGN;
break;
case IASTBinaryExpression.op_binaryAndAssign:
opString = OP_BINARYANDASSIGN;
break;
case IASTBinaryExpression.op_binaryXorAssign:
opString = OP_BINARYXORASSIGN;
break;
case IASTBinaryExpression.op_binaryOrAssign:
opString = OP_BINARYORASSIGN;
break;
case IASTBinaryExpression.op_equals:
opString = OP_EQUALS;
break;
case IASTBinaryExpression.op_notequals:
opString = OP_NOTEQUALS;
break;
}
return opString;
}
private String getDeclaratorName(IASTDeclarator decltor) {
String name = BLANK_STRING;
while (decltor != null && decltor.getName() != null && decltor.getName().toString() == null) {