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>
</filter>
</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">
<filter id="420679712">
<message_arguments>

View file

@ -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();
}

View file

@ -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();
}

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.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;
}

View file

@ -131,4 +131,9 @@ public class ASTLiteralNode implements IASTNode {
public boolean isActive() {
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
* 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<IASTComment> getLeadingComments(IASTNode node) {
List<IASTComment> 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;
}

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
* 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<String, List<IIndexName>> macroExpansion = new TreeMap<String, List<IIndexName>>();
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<IIndexName> 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<String, List<IIndexName>>();
IASTPreprocessorMacroDefinition[] md = tu.getMacroDefinitions();
IASTPreprocessorMacroDefinition[] md = ast.getMacroDefinitions();
TreeSet<String>paths = new TreeSet<String>();
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<IIndexName> fileList = macroExpansion.get(filename2);
if (paths.contains(filename2)) {
String filename = iIndexName.getFileLocation().getFileName();
List<IIndexName> fileList = macroExpansion.get(filename);
if (paths.contains(filename)) {
if (fileList == null) {
fileList = new ArrayList<IIndexName>();
macroExpansion.put(filename2, fileList);
macroExpansion.put(filename, fileList);
}
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
* 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<IASTComment> getTrailingComments(IASTNode node) {
List<IASTComment> 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<IASTComment> getFreestandingComments(IASTNode node) {
List<IASTComment> 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;
}

View file

@ -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 {

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.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()) {

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.
* 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:
* 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<IASTComment> comments = oldRewriter.getComments(copyLoc.getOriginalNode(), pos);
IASTNode originalNode = node.getOriginalNode();
if (originalNode != node) {
List<IASTComment> comments = oldRewriter.getComments(originalNode, pos);
for (IASTComment comment : comments) {
newRewriter.addComment(node, comment, pos);
}