From 8d489aa58ebc79bd6b5039e83e4cd953bfdbdb2c Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Fri, 6 Mar 2009 14:37:05 +0000 Subject: [PATCH] Visit method for pointer operators, bug 260461. --- .../cdt/core/parser/tests/ast2/AST2Tests.java | 23 ++++++++++--- .../cdt/core/dom/ast/ASTGenericVisitor.java | 10 ++++++ .../eclipse/cdt/core/dom/ast/ASTVisitor.java | 19 +++++++++++ .../core/dom/parser/c/CASTDeclarator.java | 13 ++++---- .../core/dom/parser/c/CASTPointer.java | 21 +++++++----- .../dom/parser/cpp/CPPASTArrayModifier.java | 33 ++++++++----------- .../core/dom/parser/cpp/CPPASTDeclarator.java | 12 +++---- .../core/dom/parser/cpp/CPPASTPointer.java | 20 ++++++++--- .../dom/parser/cpp/CPPASTPointerToMember.java | 25 +++++++++----- .../parser/cpp/CPPASTReferenceOperator.java | 23 +++++++++---- 10 files changed, 135 insertions(+), 64 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index e9603b47d7c..6e0c20eeb8f 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -52,6 +52,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNullStatement; import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTProblem; import org.eclipse.cdt.core.dom.ast.IASTProblemDeclaration; @@ -5868,11 +5869,12 @@ public class AST2Tests extends AST2BaseTest { assertInstance(children[1], IASTDeclarator.class); children= children[1].getChildren(); - assertEquals(2, children.length); - assertInstance(children[0], IASTName.class); - assertInstance(children[1], IASTInitializer.class); + assertEquals(3, children.length); + assertInstance(children[0], IASTPointerOperator.class); + assertInstance(children[1], IASTName.class); + assertInstance(children[2], IASTInitializer.class); - children= children[1].getChildren()[0].getChildren(); // skip binary expression + children= children[2].getChildren()[0].getChildren(); // skip binary expression assertEquals(2, children.length); assertInstance(children[0], IASTLiteralExpression.class); assertInstance(children[1], IASTLiteralExpression.class); @@ -6007,4 +6009,17 @@ public class AST2Tests extends AST2BaseTest { IVariable v= ba.assertNonProblem("y);", 1); } } + + // int* v; + public void testPointerOperatorsAsChildren_260461() throws Exception { + final String code= getAboveComment(); + for (ParserLanguage lang : ParserLanguage.values()) { + IASTTranslationUnit tu= parseAndCheckBindings(code, lang, true); + IASTSimpleDeclaration decl= getDeclaration(tu, 0); + IASTDeclarator dtor= decl.getDeclarators()[0]; + IASTNode[] nodes = dtor.getChildren(); + assertEquals(2, nodes.length); + assertInstance(nodes[0], IASTPointerOperator.class); + } + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTGenericVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTGenericVisitor.java index 43d16930568..e829b22773c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTGenericVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTGenericVisitor.java @@ -52,6 +52,11 @@ public abstract class ASTGenericVisitor extends ASTVisitor implements ICPPASTVis public int visit(IASTArrayModifier arrayModifier) { return genericVisit(arrayModifier); } + + @Override + public int visit(IASTPointerOperator ptrOperator) { + return genericVisit(ptrOperator); + } @Override public int visit(IASTDeclaration declaration) { @@ -137,6 +142,11 @@ public abstract class ASTGenericVisitor extends ASTVisitor implements ICPPASTVis public int leave(IASTArrayModifier arrayModifier) { return genericLeave(arrayModifier); } + + @Override + public int leave(IASTPointerOperator ptrOperator) { + return genericLeave(ptrOperator); + } @Override public int leave(IASTDeclaration declaration) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTVisitor.java index 5d42d03657f..469aa630bf5 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTVisitor.java @@ -70,6 +70,11 @@ public abstract class ASTVisitor { * @since 5.1 */ public boolean shouldVisitArrayModifiers = false; + /** + * Set this flag to visit pointer operators of declarators. + * @since 5.1 + */ + public boolean shouldVisitPointerOperators = false; /** * Set this flag to visit expressions. */ @@ -160,6 +165,7 @@ public abstract class ASTVisitor { shouldVisitNames= visitNodes; shouldVisitNamespaces= visitNodes; shouldVisitParameterDeclarations= visitNodes; + shouldVisitPointerOperators= visitNodes; shouldVisitProblems= visitNodes; shouldVisitStatements= visitNodes; shouldVisitTemplateParameters= visitNodes; @@ -203,6 +209,13 @@ public abstract class ASTVisitor { return PROCESS_CONTINUE; } + /** + * @since 5.1 + */ + public int visit(IASTPointerOperator ptrOperator) { + return PROCESS_CONTINUE; + } + public int visit(IASTExpression expression) { return PROCESS_CONTINUE; } @@ -258,6 +271,12 @@ public abstract class ASTVisitor { public int leave(IASTArrayModifier arrayModifier) { return PROCESS_CONTINUE; } + /** + * @since 5.1 + */ + public int leave(IASTPointerOperator ptrOperator) { + return PROCESS_CONTINUE; + } public int leave(IASTExpression expression) { return PROCESS_CONTINUE; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTDeclarator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTDeclarator.java index dc02f58c747..8e85922cdb8 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTDeclarator.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTDeclarator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2008 IBM Corporation and others. + * Copyright (c) 2005, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -131,6 +131,11 @@ public class CASTDeclarator extends ASTNode implements IASTDeclarator, IASTAmbig } } + for (int i = 0; i <= pointerOpsPos; i++) { + if (!pointerOps[i].accept(action)) + return false; + } + if (getPropertyInParent() != IASTTypeId.ABSTRACT_DECLARATOR && nestedDeclarator == null) { if (getParent() instanceof IASTDeclarator) { IASTDeclarator outermostDeclarator = (IASTDeclarator) getParent(); @@ -148,12 +153,6 @@ public class CASTDeclarator extends ASTNode implements IASTDeclarator, IASTAmbig return false; } - IASTPointerOperator[] ptrOps = getPointerOperators(); - for (int i = 0; i < ptrOps.length; i++) { - if (!ptrOps[i].accept(action)) - return false; - } - if (!postAccept(action)) return false; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTPointer.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTPointer.java index 3e7d8aef2c3..dc8a6ae3a4e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTPointer.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTPointer.java @@ -1,12 +1,13 @@ /******************************************************************************* - * Copyright (c) 2005, 2008 IBM Corporation and others. + * Copyright (c) 2005, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * IBM Rational Software - Initial API and implementation + * John Camelon (IBM Rational Software) - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.c; @@ -14,9 +15,6 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.c.ICASTPointer; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; -/** - * @author jcamelon - */ public class CASTPointer extends ASTNode implements ICASTPointer { private boolean isRestrict; @@ -60,8 +58,15 @@ public class CASTPointer extends ASTNode implements ICASTPointer { } @Override - public boolean accept(ASTVisitor visitor) { - return true; + public boolean accept(ASTVisitor action) { + if (action.shouldVisitPointerOperators) { + switch (action.visit(this)) { + case ASTVisitor.PROCESS_ABORT : return false; + case ASTVisitor.PROCESS_SKIP : return true; + } + if (action.leave(this) == ASTVisitor.PROCESS_ABORT) + return false; + } + return true; } - } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTArrayModifier.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTArrayModifier.java index 07d81b2974e..ccd7d51ed4b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTArrayModifier.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTArrayModifier.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2008 IBM Corporation and others. + * Copyright (c) 2004, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -53,25 +53,20 @@ public class CPPASTArrayModifier extends ASTNode implements IASTArrayModifier, I @Override public boolean accept(ASTVisitor action) { - if (exp != null) { - if( action.shouldVisitArrayModifiers ){ - switch( action.visit( this ) ){ - case ASTVisitor.PROCESS_ABORT : return false; - case ASTVisitor.PROCESS_SKIP : return true; - default : break; - } + if (action.shouldVisitArrayModifiers) { + switch (action.visit(this)) { + case ASTVisitor.PROCESS_ABORT : return false; + case ASTVisitor.PROCESS_SKIP : return true; + default : break; } - if (!exp.accept(action)) - return false; - if( action.shouldVisitArrayModifiers ){ - switch( action.leave( this ) ){ - case ASTVisitor.PROCESS_ABORT : return false; - case ASTVisitor.PROCESS_SKIP : return true; - default : break; - } - } - } - return true; + } + if (exp != null && !exp.accept(action)) + return false; + + if (action.shouldVisitArrayModifiers && action.leave(this) == ASTVisitor.PROCESS_ABORT) + return false; + + return true; } public void replace(IASTNode child, IASTNode other) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTDeclarator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTDeclarator.java index 245bff42807..a651bfacf1a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTDeclarator.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTDeclarator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2008 IBM Corporation and others. + * Copyright (c) 2004, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -26,7 +26,7 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTTypeId; import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; -import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; +import org.eclipse.cdt.internal.core.dom.parser.ASTQueries; /** * C++ specific declarator. @@ -130,13 +130,13 @@ public class CPPASTDeclarator extends ASTNode implements IASTDeclarator { } } - IASTPointerOperator[] ptrOps = getPointerOperators(); - for (int i = 0; i < ptrOps.length; i++) { - if (!ptrOps[i].accept(action)) return false; + for (int i = 0; i <= pointerOpsPos; i++) { + if (!pointerOps[i].accept(action)) + return false; } if (nested == null && name != null) { - IASTDeclarator outermost= CPPVisitor.findOutermostDeclarator(this); + IASTDeclarator outermost= ASTQueries.findOutermostDeclarator(this); if (outermost.getPropertyInParent() != IASTTypeId.ABSTRACT_DECLARATOR) { if (!name.accept(action)) return false; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTPointer.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTPointer.java index d39e8e99d49..9940be4e3fd 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTPointer.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTPointer.java @@ -1,12 +1,13 @@ /******************************************************************************* - * Copyright (c) 2004, 2008 IBM Corporation and others. + * Copyright (c) 2004, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * IBM - Initial API and implementation + * John Camelon (IBM) - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.cpp; @@ -15,14 +16,15 @@ import org.eclipse.cdt.core.dom.ast.IASTPointer; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; /** - * @author jcamelon + * A pointer operator of a declarator */ public class CPPASTPointer extends ASTNode implements IASTPointer { private boolean isConst; - private boolean isVolatile; + public CPPASTPointer() { + } public CPPASTPointer copy() { CPPASTPointer copy = new CPPASTPointer(); @@ -52,6 +54,14 @@ public class CPPASTPointer extends ASTNode implements IASTPointer { @Override public boolean accept(ASTVisitor action) { - return true; + if (action.shouldVisitPointerOperators) { + switch (action.visit(this)) { + case ASTVisitor.PROCESS_ABORT : return false; + case ASTVisitor.PROCESS_SKIP : return true; + } + if (action.leave(this) == ASTVisitor.PROCESS_ABORT) + return false; + } + return true; } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTPointerToMember.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTPointerToMember.java index d090808d87d..d4449526822 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTPointerToMember.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTPointerToMember.java @@ -1,12 +1,13 @@ /******************************************************************************* - * Copyright (c) 2004, 2008 IBM Corporation and others. + * Copyright (c) 2004, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * IBM - Initial API and implementation + * John Camelon (IBM) - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.cpp; @@ -14,9 +15,6 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember; -/** - * @author jcamelon - */ public class CPPASTPointerToMember extends CPPASTPointer implements ICPPASTPointerToMember { private IASTName n; @@ -52,11 +50,20 @@ public class CPPASTPointerToMember extends CPPASTPointer implements ICPPASTPoint } @Override - public boolean accept( ASTVisitor action ){ - if( n != null ) if( !n.accept( action ) ) return false; - return true; + public boolean accept(ASTVisitor action) { + if (action.shouldVisitPointerOperators) { + switch (action.visit(this)) { + case ASTVisitor.PROCESS_ABORT : return false; + case ASTVisitor.PROCESS_SKIP : return true; + } + } + if (n != null && !n.accept(action)) + return false; + + if (action.shouldVisitPointerOperators && action.leave(this) == ASTVisitor.PROCESS_ABORT) + return false; + return true; } - public int getRoleForName(IASTName name ) { if( name == this.n ) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTReferenceOperator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTReferenceOperator.java index ba04e479e72..99676400df9 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTReferenceOperator.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTReferenceOperator.java @@ -1,12 +1,13 @@ /******************************************************************************* - * Copyright (c) 2004, 2008 IBM Corporation and others. + * Copyright (c) 2004, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * IBM - Initial API and implementation + * John Camelon (IBM) - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.cpp; @@ -15,10 +16,13 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; /** - * @author jcamelon + * Reference operator for declarators. */ public class CPPASTReferenceOperator extends ASTNode implements ICPPASTReferenceOperator { + public CPPASTReferenceOperator() { + } + public CPPASTReferenceOperator copy() { CPPASTReferenceOperator copy = new CPPASTReferenceOperator(); copy.setOffsetAndLength(this); @@ -26,8 +30,15 @@ public class CPPASTReferenceOperator extends ASTNode implements ICPPASTReference } @Override - public boolean accept( ASTVisitor action ){ - return true; + public boolean accept(ASTVisitor action) { + if (action.shouldVisitPointerOperators) { + switch (action.visit(this)) { + case ASTVisitor.PROCESS_ABORT : return false; + case ASTVisitor.PROCESS_SKIP : return true; + } + if (action.leave(this) == ASTVisitor.PROCESS_ABORT) + return false; + } + return true; } - }