From 2fd430eb63dbbb970dd781aea41ac9c38fa726c5 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Sat, 11 Feb 2012 12:27:26 -0800 Subject: [PATCH] Added IASTNode.getOriginalNode method. --- .../.settings/.api_filters | 13 ++++ .../cdt/core/dom/ast/IASTCopyLocation.java | 6 +- .../eclipse/cdt/core/dom/ast/IASTNode.java | 7 +++ .../cdt/internal/core/dom/parser/ASTNode.java | 10 +-- .../core/dom/rewrite/ASTLiteralNode.java | 5 ++ .../rewrite/astwriter/ASTWriterVisitor.java | 12 ++-- .../astwriter/MacroExpansionHandler.java | 61 +++++++------------ .../dom/rewrite/astwriter/NodeWriter.java | 21 +++---- .../core/pdom/dom/PDOMASTAdapter.java | 5 ++ .../extractfunction/FunctionExtractor.java | 4 +- ...eFromInHeaderToImplementationStrategy.java | 12 ++-- 11 files changed, 79 insertions(+), 77 deletions(-) diff --git a/core/org.eclipse.cdt.core/.settings/.api_filters b/core/org.eclipse.cdt.core/.settings/.api_filters index bf5b479e6bf..543278bb554 100644 --- a/core/org.eclipse.cdt.core/.settings/.api_filters +++ b/core/org.eclipse.cdt.core/.settings/.api_filters @@ -14,6 +14,19 @@ + + + + + + + + + + + + + diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTCopyLocation.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTCopyLocation.java index 4671066b93f..b313cba0959 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTCopyLocation.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTCopyLocation.java @@ -13,9 +13,13 @@ package org.eclipse.cdt.core.dom.ast; /** * @author Emanuel Graf IFS + * @noextend This interface is not intended to be extended by clients. + * @noimplement This interface is not intended to be implemented by clients. * @since 5.3 */ public interface IASTCopyLocation extends IASTNodeLocation { - + /** + * @noreference This method is not intended to be referenced by clients. + */ public IASTNode getOriginalNode(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTNode.java index 7db3fed28ea..7ec5a145ce7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTNode.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTNode.java @@ -277,4 +277,11 @@ public interface IASTNode { * if this node or one of its descendants does not support copying */ public IASTNode copy(CopyStyle style); + + /** + * If the node is a copy of some other node, returns the original node. + * Otherwise returns the node itself. + * @since 5.4 + */ + public IASTNode getOriginalNode(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNode.java index 5aa930983af..a5ed9162714 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNode.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNode.java @@ -15,7 +15,6 @@ package org.eclipse.cdt.internal.core.dom.parser; import org.eclipse.cdt.core.dom.ast.ASTNodeProperty; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ExpansionOverlapsBoundaryException; -import org.eclipse.cdt.core.dom.ast.IASTCopyLocation; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTImageLocation; import org.eclipse.cdt.core.dom.ast.IASTNode; @@ -375,17 +374,14 @@ public abstract class ASTNode implements IASTNode { locations = new IASTNodeLocation[] { new ASTCopyLocation(originalNode) }; } - /** - * If the node is a copy of some other node, returns the original node. - * Otherwise returns the node itself. - */ + @Override public IASTNode getOriginalNode() { IASTNode node = this; while (true) { IASTNodeLocation[] locations = node.getNodeLocations(); - if (locations.length == 0 || !(locations[0] instanceof IASTCopyLocation)) + if (locations.length == 0 || !(locations[0] instanceof ASTCopyLocation)) break; - node = ((IASTCopyLocation) locations[0]).getOriginalNode(); + node = ((ASTCopyLocation) locations[0]).getOriginalNode(); } return node; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/ASTLiteralNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/ASTLiteralNode.java index f4e3bcbb436..4bfd9b5d47f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/ASTLiteralNode.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/ASTLiteralNode.java @@ -131,4 +131,9 @@ public class ASTLiteralNode implements IASTNode { public boolean isActive() { return true; } + + @Override + public IASTNode getOriginalNode() { + return this; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java index ef08a0ba868..4f63e427e7c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -19,7 +19,6 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; import org.eclipse.cdt.core.dom.ast.IASTComment; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; -import org.eclipse.cdt.core.dom.ast.IASTCopyLocation; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; @@ -27,7 +26,6 @@ import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTInitializer; 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.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTStatement; @@ -130,11 +128,9 @@ public class ASTWriterVisitor extends ASTVisitor { private List getLeadingComments(IASTNode node) { List leadingComments = commentMap.getLeadingCommentsForNode(node); - IASTNodeLocation[] locs = node.getNodeLocations(); - if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) { - IASTCopyLocation copyLoc = (IASTCopyLocation) locs[0]; - leadingComments.addAll(commentMap.getLeadingCommentsForNode(copyLoc.getOriginalNode())); - } + IASTNode originalNode = node.getOriginalNode(); + if (originalNode != node) + leadingComments.addAll(commentMap.getLeadingCommentsForNode(originalNode)); return leadingComments; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/MacroExpansionHandler.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/MacroExpansionHandler.java index 1a6910dfea6..49315c0d272 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/MacroExpansionHandler.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/MacroExpansionHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Institute for Software - initial API and implementation + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.rewrite.astwriter; @@ -18,7 +19,6 @@ import java.util.Map; import java.util.TreeMap; import java.util.TreeSet; -import org.eclipse.cdt.core.dom.ast.IASTCopyLocation; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation; import org.eclipse.cdt.core.dom.ast.IASTNode; @@ -41,9 +41,9 @@ import org.eclipse.core.runtime.CoreException; * @author Emanuel Graf IFS */ public class MacroExpansionHandler { - private int lastMacroExpOffset; private final Scribe scribe; - private IASTTranslationUnit tu; + private int lastMacroExpOffset; + private IASTTranslationUnit ast; private Map> macroExpansion = new TreeMap>(); public MacroExpansionHandler(Scribe scribe) { @@ -81,11 +81,11 @@ public class MacroExpansionHandler { protected boolean checkisMacroExpansionNode(IASTNode node, boolean write) { IASTTranslationUnit unit = node.getTranslationUnit(); - if (tu == null || !tu.equals(unit)) { + if (ast == null || !ast.equals(unit)) { initEmptyMacros(unit); } IASTNodeLocation[] locs = getNodeLocations(node); - if (locs != null && locs.length ==1) { + if (locs != null && locs.length == 1) { if (locs[0] instanceof IASTMacroExpansionLocation) { IASTMacroExpansionLocation macroNode = (IASTMacroExpansionLocation) locs[0]; @@ -94,7 +94,7 @@ public class MacroExpansionHandler { } if (write) { lastMacroExpOffset = macroNode.asFileLocation().getNodeOffset(); - node = getOriginalNode(node); + node = node.getOriginalNode(); scribe.print(node.getRawSignature()); } return true; @@ -105,24 +105,13 @@ public class MacroExpansionHandler { return false; } - private IASTNode getOriginalNode(IASTNode node) { - IASTNodeLocation[] locs = node.getNodeLocations(); - if (locs != null && locs.length == 1 && locs[0] instanceof IASTCopyLocation) { - node = ((IASTCopyLocation) locs[0]).getOriginalNode(); - } - return node; - } - private IASTNodeLocation[] getNodeLocations(IASTNode node) { - IASTNodeLocation[] locs = node.getNodeLocations(); - if (locs != null && locs.length == 1 && locs[0] instanceof IASTCopyLocation) { - locs = ((IASTCopyLocation) locs[0]).getOriginalNode().getNodeLocations(); - } - return locs; + return node.getOriginalNode().getNodeLocations(); } private void handleEmptyMacroExpansion(IASTNode node) { - if (node.getTranslationUnit() == null)return; + if (node.getTranslationUnit() == null) + return; String file = node.getContainingFilename(); List exps = macroExpansion.get(file); if (exps != null && !exps.isEmpty()) { @@ -144,46 +133,40 @@ public class MacroExpansionHandler { } private IASTFileLocation getFileLocation(IASTNode node) { - IASTFileLocation fileLocation = node.getFileLocation(); - if (fileLocation == null) { - IASTNodeLocation[] locs = node.getNodeLocations(); - if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) { - fileLocation = ((IASTCopyLocation) locs[0]).getOriginalNode().getFileLocation(); - } - } - return fileLocation; + return node.getOriginalNode().getFileLocation(); } private void initEmptyMacros(IASTTranslationUnit unit) { if (unit != null) { - tu = unit; - IIndex index = tu.getIndex(); + ast = unit; + IIndex index = ast.getIndex(); if (index != null) { macroExpansion = new TreeMap>(); - IASTPreprocessorMacroDefinition[] md = tu.getMacroDefinitions(); + IASTPreprocessorMacroDefinition[] md = ast.getMacroDefinitions(); TreeSetpaths = new TreeSet(); - for (IASTPreprocessorIncludeStatement is :tu.getIncludeDirectives()) { + for (IASTPreprocessorIncludeStatement is :ast.getIncludeDirectives()) { if (!is.isSystemInclude()) { paths.add(is.getContainingFilename()); } } - paths.add(tu.getContainingFilename()); + paths.add(ast.getContainingFilename()); for (IASTPreprocessorMacroDefinition iastPreprocessorMacroDefinition : md) { if (iastPreprocessorMacroDefinition.getExpansion().length() == 0) { try { - IIndexMacro[] macroBinding = index.findMacros(iastPreprocessorMacroDefinition.getName().toCharArray(), + IIndexMacro[] macroBinding = index.findMacros( + iastPreprocessorMacroDefinition.getName().toCharArray(), IndexFilter.ALL, null); if (macroBinding.length > 0) { IIndexName[] refs = index.findReferences(macroBinding[0]); for (IIndexName iIndexName : refs) { - String filename2 = iIndexName.getFileLocation().getFileName(); - List fileList = macroExpansion.get(filename2); - if (paths.contains(filename2)) { + String filename = iIndexName.getFileLocation().getFileName(); + List fileList = macroExpansion.get(filename); + if (paths.contains(filename)) { if (fileList == null) { fileList = new ArrayList(); - macroExpansion.put(filename2, fileList); + macroExpansion.put(filename, fileList); } fileList.add(iIndexName); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java index aa160a77bdf..1a5e4b1e44f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,15 +8,14 @@ * * Contributors: * Institute for Software - initial API and implementation + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.rewrite.astwriter; import java.util.List; import org.eclipse.cdt.core.dom.ast.IASTComment; -import org.eclipse.cdt.core.dom.ast.IASTCopyLocation; import org.eclipse.cdt.core.dom.ast.IASTNode; -import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; /** @@ -108,11 +107,9 @@ public class NodeWriter { private List getTrailingComments(IASTNode node) { List trailingComments = commentMap.getTrailingCommentsForNode(node); - IASTNodeLocation[] locs = node.getNodeLocations(); - if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) { - IASTCopyLocation loc = (IASTCopyLocation) locs[0]; - trailingComments.addAll(commentMap.getTrailingCommentsForNode(loc.getOriginalNode())); - } + IASTNode originalNode = node.getOriginalNode(); + if (originalNode != node) + trailingComments.addAll(commentMap.getTrailingCommentsForNode(originalNode)); return trailingComments; } @@ -122,11 +119,9 @@ public class NodeWriter { private List getFreestandingComments(IASTNode node) { List freestandingComments = commentMap.getFreestandingCommentsForNode(node); - IASTNodeLocation[] locs = node.getNodeLocations(); - if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) { - IASTCopyLocation loc = (IASTCopyLocation) locs[0]; - freestandingComments.addAll(commentMap.getFreestandingCommentsForNode(loc.getOriginalNode())); - } + IASTNode originalNode = node.getOriginalNode(); + if (originalNode != node) + freestandingComments.addAll(commentMap.getFreestandingCommentsForNode(originalNode)); return freestandingComments; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTAdapter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTAdapter.java index 45fb3973180..133265932d7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTAdapter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMASTAdapter.java @@ -280,6 +280,11 @@ public class PDOMASTAdapter { public boolean isQualified() { return fDelegate.isQualified(); } + + @Override + public IASTNode getOriginalNode() { + return this; + } } private static class AnonymousEnumeration implements IEnumeration { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/FunctionExtractor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/FunctionExtractor.java index 37ca4c431c8..41ce0f3b2d7 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/FunctionExtractor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/FunctionExtractor.java @@ -39,8 +39,6 @@ import org.eclipse.cdt.core.dom.ast.INodeFactory; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; -import org.eclipse.cdt.internal.core.dom.parser.ASTNode; - import org.eclipse.cdt.internal.ui.refactoring.NameInformation; import org.eclipse.cdt.internal.ui.refactoring.NameInformation.Indirection; @@ -134,7 +132,7 @@ public abstract class FunctionExtractor { @Override public int visit(IASTName name) { - NameInformation param = changedParameters.get(((ASTNode) name).getOriginalNode()); + NameInformation param = changedParameters.get(name.getOriginalNode()); if (param != null) { IASTName newName = null; if (param.isRenamed()) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleFromInHeaderToImplementationStrategy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleFromInHeaderToImplementationStrategy.java index ff13a676cd2..2b165c72248 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleFromInHeaderToImplementationStrategy.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/togglefunction/ToggleFromInHeaderToImplementationStrategy.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2011, 2012 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -7,7 +7,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Martin Schwab & Thomas Kallenberg - initial API and implementation + * Martin Schwab & Thomas Kallenberg - initial API and implementation + * Sergey Prigogin (Google) ******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.togglefunction; @@ -18,7 +19,6 @@ import org.eclipse.text.edits.TextEditGroup; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; import org.eclipse.cdt.core.dom.ast.IASTComment; -import org.eclipse.cdt.core.dom.ast.IASTCopyLocation; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; @@ -229,9 +229,9 @@ public class ToggleFromInHeaderToImplementationStrategy implements IToggleRefact private void copyComments(IASTNode node, ASTRewrite newRewriter, ASTRewrite oldRewriter, CommentPosition pos) { - if (node.getNodeLocations().length > 0 && node.getNodeLocations()[0] instanceof IASTCopyLocation) { - IASTCopyLocation copyLoc = (IASTCopyLocation) node.getNodeLocations()[0]; - List comments = oldRewriter.getComments(copyLoc.getOriginalNode(), pos); + IASTNode originalNode = node.getOriginalNode(); + if (originalNode != node) { + List comments = oldRewriter.getComments(originalNode, pos); for (IASTComment comment : comments) { newRewriter.addComment(node, comment, pos); }