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

Added IASTNode.getOriginalNode method.

This commit is contained in:
Sergey Prigogin 2012-02-11 12:27:26 -08:00
parent d7b07a626d
commit 2fd430eb63
11 changed files with 79 additions and 77 deletions

View file

@ -14,6 +14,19 @@
</message_arguments> </message_arguments>
</filter> </filter>
</resource> </resource>
<resource path="parser/org/eclipse/cdt/core/dom/ast/IASTCopyLocation.java" type="org.eclipse.cdt.core.dom.ast.IASTCopyLocation">
<filter id="403853384">
<message_arguments>
<message_argument value="org.eclipse.cdt.core.dom.ast.IASTCopyLocation"/>
</message_arguments>
</filter>
<filter id="406052990">
<message_arguments>
<message_argument value="org.eclipse.cdt.core.dom.ast.IASTCopyLocation"/>
<message_argument value="getOriginalNode()"/>
</message_arguments>
</filter>
</resource>
<resource path="parser/org/eclipse/cdt/core/parser/util/ArrayUtil.java" type="org.eclipse.cdt.core.parser.util.ArrayUtil"> <resource path="parser/org/eclipse/cdt/core/parser/util/ArrayUtil.java" type="org.eclipse.cdt.core.parser.util.ArrayUtil">
<filter id="420679712"> <filter id="420679712">
<message_arguments> <message_arguments>

View file

@ -13,9 +13,13 @@ package org.eclipse.cdt.core.dom.ast;
/** /**
* @author Emanuel Graf IFS * @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 * @since 5.3
*/ */
public interface IASTCopyLocation extends IASTNodeLocation { public interface IASTCopyLocation extends IASTNodeLocation {
/**
* @noreference This method is not intended to be referenced by clients.
*/
public IASTNode getOriginalNode(); public IASTNode getOriginalNode();
} }

View file

@ -277,4 +277,11 @@ public interface IASTNode {
* if this node or one of its descendants does not support copying * if this node or one of its descendants does not support copying
*/ */
public IASTNode copy(CopyStyle style); 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();
} }

View file

@ -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.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.ExpansionOverlapsBoundaryException; 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.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTImageLocation; import org.eclipse.cdt.core.dom.ast.IASTImageLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
@ -375,17 +374,14 @@ public abstract class ASTNode implements IASTNode {
locations = new IASTNodeLocation[] { new ASTCopyLocation(originalNode) }; locations = new IASTNodeLocation[] { new ASTCopyLocation(originalNode) };
} }
/** @Override
* If the node is a copy of some other node, returns the original node.
* Otherwise returns the node itself.
*/
public IASTNode getOriginalNode() { public IASTNode getOriginalNode() {
IASTNode node = this; IASTNode node = this;
while (true) { while (true) {
IASTNodeLocation[] locations = node.getNodeLocations(); IASTNodeLocation[] locations = node.getNodeLocations();
if (locations.length == 0 || !(locations[0] instanceof IASTCopyLocation)) if (locations.length == 0 || !(locations[0] instanceof ASTCopyLocation))
break; break;
node = ((IASTCopyLocation) locations[0]).getOriginalNode(); node = ((ASTCopyLocation) locations[0]).getOriginalNode();
} }
return node; return node;
} }

View file

@ -131,4 +131,9 @@ public class ASTLiteralNode implements IASTNode {
public boolean isActive() { public boolean isActive() {
return true; return true;
} }
@Override
public IASTNode getOriginalNode() {
return this;
}
} }

View file

@ -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 * Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * 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.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTComment; import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; 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.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; 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.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; 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.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTStatement;
@ -130,11 +128,9 @@ public class ASTWriterVisitor extends ASTVisitor {
private List<IASTComment> getLeadingComments(IASTNode node) { private List<IASTComment> getLeadingComments(IASTNode node) {
List<IASTComment> leadingComments = commentMap.getLeadingCommentsForNode(node); List<IASTComment> leadingComments = commentMap.getLeadingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations(); IASTNode originalNode = node.getOriginalNode();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) { if (originalNode != node)
IASTCopyLocation copyLoc = (IASTCopyLocation) locs[0]; leadingComments.addAll(commentMap.getLeadingCommentsForNode(originalNode));
leadingComments.addAll(commentMap.getLeadingCommentsForNode(copyLoc.getOriginalNode()));
}
return leadingComments; return leadingComments;
} }

View file

@ -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 * Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
@ -8,6 +8,7 @@
* *
* Contributors: * Contributors:
* Institute for Software - initial API and implementation * Institute for Software - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter; package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
@ -18,7 +19,6 @@ import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; 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.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation; import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
@ -41,9 +41,9 @@ import org.eclipse.core.runtime.CoreException;
* @author Emanuel Graf IFS * @author Emanuel Graf IFS
*/ */
public class MacroExpansionHandler { public class MacroExpansionHandler {
private int lastMacroExpOffset;
private final Scribe scribe; private final Scribe scribe;
private IASTTranslationUnit tu; private int lastMacroExpOffset;
private IASTTranslationUnit ast;
private Map<String, List<IIndexName>> macroExpansion = new TreeMap<String, List<IIndexName>>(); private Map<String, List<IIndexName>> macroExpansion = new TreeMap<String, List<IIndexName>>();
public MacroExpansionHandler(Scribe scribe) { public MacroExpansionHandler(Scribe scribe) {
@ -81,7 +81,7 @@ public class MacroExpansionHandler {
protected boolean checkisMacroExpansionNode(IASTNode node, boolean write) { protected boolean checkisMacroExpansionNode(IASTNode node, boolean write) {
IASTTranslationUnit unit = node.getTranslationUnit(); IASTTranslationUnit unit = node.getTranslationUnit();
if (tu == null || !tu.equals(unit)) { if (ast == null || !ast.equals(unit)) {
initEmptyMacros(unit); initEmptyMacros(unit);
} }
IASTNodeLocation[] locs = getNodeLocations(node); IASTNodeLocation[] locs = getNodeLocations(node);
@ -94,7 +94,7 @@ public class MacroExpansionHandler {
} }
if (write) { if (write) {
lastMacroExpOffset = macroNode.asFileLocation().getNodeOffset(); lastMacroExpOffset = macroNode.asFileLocation().getNodeOffset();
node = getOriginalNode(node); node = node.getOriginalNode();
scribe.print(node.getRawSignature()); scribe.print(node.getRawSignature());
} }
return true; return true;
@ -105,24 +105,13 @@ public class MacroExpansionHandler {
return false; 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) { private IASTNodeLocation[] getNodeLocations(IASTNode node) {
IASTNodeLocation[] locs = node.getNodeLocations(); return node.getOriginalNode().getNodeLocations();
if (locs != null && locs.length == 1 && locs[0] instanceof IASTCopyLocation) {
locs = ((IASTCopyLocation) locs[0]).getOriginalNode().getNodeLocations();
}
return locs;
} }
private void handleEmptyMacroExpansion(IASTNode node) { private void handleEmptyMacroExpansion(IASTNode node) {
if (node.getTranslationUnit() == null)return; if (node.getTranslationUnit() == null)
return;
String file = node.getContainingFilename(); String file = node.getContainingFilename();
List<IIndexName> exps = macroExpansion.get(file); List<IIndexName> exps = macroExpansion.get(file);
if (exps != null && !exps.isEmpty()) { if (exps != null && !exps.isEmpty()) {
@ -144,46 +133,40 @@ public class MacroExpansionHandler {
} }
private IASTFileLocation getFileLocation(IASTNode node) { private IASTFileLocation getFileLocation(IASTNode node) {
IASTFileLocation fileLocation = node.getFileLocation(); return node.getOriginalNode().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;
} }
private void initEmptyMacros(IASTTranslationUnit unit) { private void initEmptyMacros(IASTTranslationUnit unit) {
if (unit != null) { if (unit != null) {
tu = unit; ast = unit;
IIndex index = tu.getIndex(); IIndex index = ast.getIndex();
if (index != null) { if (index != null) {
macroExpansion = new TreeMap<String, List<IIndexName>>(); macroExpansion = new TreeMap<String, List<IIndexName>>();
IASTPreprocessorMacroDefinition[] md = tu.getMacroDefinitions(); IASTPreprocessorMacroDefinition[] md = ast.getMacroDefinitions();
TreeSet<String>paths = new TreeSet<String>(); TreeSet<String>paths = new TreeSet<String>();
for (IASTPreprocessorIncludeStatement is :tu.getIncludeDirectives()) { for (IASTPreprocessorIncludeStatement is :ast.getIncludeDirectives()) {
if (!is.isSystemInclude()) { if (!is.isSystemInclude()) {
paths.add(is.getContainingFilename()); paths.add(is.getContainingFilename());
} }
} }
paths.add(tu.getContainingFilename()); paths.add(ast.getContainingFilename());
for (IASTPreprocessorMacroDefinition iastPreprocessorMacroDefinition : md) { for (IASTPreprocessorMacroDefinition iastPreprocessorMacroDefinition : md) {
if (iastPreprocessorMacroDefinition.getExpansion().length() == 0) { if (iastPreprocessorMacroDefinition.getExpansion().length() == 0) {
try { try {
IIndexMacro[] macroBinding = index.findMacros(iastPreprocessorMacroDefinition.getName().toCharArray(), IIndexMacro[] macroBinding = index.findMacros(
iastPreprocessorMacroDefinition.getName().toCharArray(),
IndexFilter.ALL, null); IndexFilter.ALL, null);
if (macroBinding.length > 0) { if (macroBinding.length > 0) {
IIndexName[] refs = index.findReferences(macroBinding[0]); IIndexName[] refs = index.findReferences(macroBinding[0]);
for (IIndexName iIndexName : refs) { for (IIndexName iIndexName : refs) {
String filename2 = iIndexName.getFileLocation().getFileName(); String filename = iIndexName.getFileLocation().getFileName();
List<IIndexName> fileList = macroExpansion.get(filename2); List<IIndexName> fileList = macroExpansion.get(filename);
if (paths.contains(filename2)) { if (paths.contains(filename)) {
if (fileList == null) { if (fileList == null) {
fileList = new ArrayList<IIndexName>(); fileList = new ArrayList<IIndexName>();
macroExpansion.put(filename2, fileList); macroExpansion.put(filename, fileList);
} }
fileList.add(iIndexName); fileList.add(iIndexName);
} }

View file

@ -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 * Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
@ -8,15 +8,14 @@
* *
* Contributors: * Contributors:
* Institute for Software - initial API and implementation * Institute for Software - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter; package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTComment; 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.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
/** /**
@ -108,11 +107,9 @@ public class NodeWriter {
private List<IASTComment> getTrailingComments(IASTNode node) { private List<IASTComment> getTrailingComments(IASTNode node) {
List<IASTComment> trailingComments = commentMap.getTrailingCommentsForNode(node); List<IASTComment> trailingComments = commentMap.getTrailingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations(); IASTNode originalNode = node.getOriginalNode();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) { if (originalNode != node)
IASTCopyLocation loc = (IASTCopyLocation) locs[0]; trailingComments.addAll(commentMap.getTrailingCommentsForNode(originalNode));
trailingComments.addAll(commentMap.getTrailingCommentsForNode(loc.getOriginalNode()));
}
return trailingComments; return trailingComments;
} }
@ -122,11 +119,9 @@ public class NodeWriter {
private List<IASTComment> getFreestandingComments(IASTNode node) { private List<IASTComment> getFreestandingComments(IASTNode node) {
List<IASTComment> freestandingComments = commentMap.getFreestandingCommentsForNode(node); List<IASTComment> freestandingComments = commentMap.getFreestandingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations(); IASTNode originalNode = node.getOriginalNode();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) { if (originalNode != node)
IASTCopyLocation loc = (IASTCopyLocation) locs[0]; freestandingComments.addAll(commentMap.getFreestandingCommentsForNode(originalNode));
freestandingComments.addAll(commentMap.getFreestandingCommentsForNode(loc.getOriginalNode()));
}
return freestandingComments; return freestandingComments;
} }

View file

@ -280,6 +280,11 @@ public class PDOMASTAdapter {
public boolean isQualified() { public boolean isQualified() {
return fDelegate.isQualified(); return fDelegate.isQualified();
} }
@Override
public IASTNode getOriginalNode() {
return this;
}
} }
private static class AnonymousEnumeration implements IEnumeration { private static class AnonymousEnumeration implements IEnumeration {

View file

@ -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.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; 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;
import org.eclipse.cdt.internal.ui.refactoring.NameInformation.Indirection; import org.eclipse.cdt.internal.ui.refactoring.NameInformation.Indirection;
@ -134,7 +132,7 @@ public abstract class FunctionExtractor {
@Override @Override
public int visit(IASTName name) { public int visit(IASTName name) {
NameInformation param = changedParameters.get(((ASTNode) name).getOriginalNode()); NameInformation param = changedParameters.get(name.getOriginalNode());
if (param != null) { if (param != null) {
IASTName newName = null; IASTName newName = null;
if (param.isRenamed()) { if (param.isRenamed()) {

View file

@ -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. * Rapperswil, University of applied sciences and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
@ -8,6 +8,7 @@
* *
* Contributors: * 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; 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.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTComment; 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.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; 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, private void copyComments(IASTNode node, ASTRewrite newRewriter, ASTRewrite oldRewriter,
CommentPosition pos) { CommentPosition pos) {
if (node.getNodeLocations().length > 0 && node.getNodeLocations()[0] instanceof IASTCopyLocation) { IASTNode originalNode = node.getOriginalNode();
IASTCopyLocation copyLoc = (IASTCopyLocation) node.getNodeLocations()[0]; if (originalNode != node) {
List<IASTComment> comments = oldRewriter.getComments(copyLoc.getOriginalNode(), pos); List<IASTComment> comments = oldRewriter.getComments(originalNode, pos);
for (IASTComment comment : comments) { for (IASTComment comment : comments) {
newRewriter.addComment(node, comment, pos); newRewriter.addComment(node, comment, pos);
} }