From 1786169b2b99eb8793af364ba2ca27e97aefde3b Mon Sep 17 00:00:00 2001 From: John Camelon Date: Mon, 30 May 2005 19:32:59 +0000 Subject: [PATCH] Added IASTNode#getFileLocation(). Updated DOM Indexer to use it. --- .../domsourceindexer/IndexEncoderUtil.java | 12 +--- .../eclipse/cdt/core/dom/ast/IASTNode.java | 9 +++ .../cdt/internal/core/dom/parser/ASTNode.java | 66 +++++++++++-------- 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexEncoderUtil.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexEncoderUtil.java index 710570730a9..0f098abfa5f 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexEncoderUtil.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexEncoderUtil.java @@ -13,7 +13,6 @@ package org.eclipse.cdt.internal.core.index.domsourceindexer; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTNode; -import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.internal.core.index.cindexstorage.IndexedFileEntry; import org.eclipse.cdt.internal.core.index.sourceindexer.SourceIndexer; import org.eclipse.core.resources.IFile; @@ -73,16 +72,7 @@ public class IndexEncoderUtil { * @return */ public static IASTFileLocation getFileLocation(IASTNode node) { - IASTFileLocation fileLoc = null; - - IASTNodeLocation[] locs = node.getNodeLocations(); - if (locs.length == 1 && locs[0] instanceof IASTFileLocation) { - fileLoc = (IASTFileLocation) locs[0]; - } - else if (locs.length > 0) { - fileLoc = node.getTranslationUnit().flattenLocationsToFile(locs); - } - return fileLoc; + return node.getFileLocation(); } public static boolean nodeInExternalHeader(IASTNode node) { 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 5c90e69b373..a431c5b0738 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 @@ -44,6 +44,15 @@ public interface IASTNode { */ public IASTNodeLocation[] getNodeLocations(); + + /** + * Get the location of the node as a file. + * + * @return IASTFileLocation + */ + public IASTFileLocation getFileLocation(); + + /** * Lightweight check for understanding what file we are in. * 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 4d5ec5428dd..f9c918f0188 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 @@ -9,6 +9,7 @@ * IBM Rational Software - Initial API and implementation */ package org.eclipse.cdt.internal.core.dom.parser; +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; @@ -18,7 +19,9 @@ import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; public abstract class ASTNode implements IASTNode { private int length; + private int offset; + private static final IASTNodeLocation[] EMPTY_LOCATION_ARRAY = new IASTNodeLocation[0]; public int getOffset() { @@ -29,48 +32,57 @@ public abstract class ASTNode implements IASTNode { return length; } - public void setOffset( int offset ) - { + public void setOffset(int offset) { this.offset = offset; this.locations = null; } - - public void setLength( int length ) - { + + public void setLength(int length) { this.length = length; this.locations = null; } - + public void setOffsetAndLength(int offset, int length) { this.offset = offset; this.length = length; this.locations = null; } - - public void setOffsetAndLength( ASTNode node ) - { - setOffsetAndLength( node.getOffset(), node.getLength() ); + + public void setOffsetAndLength(ASTNode node) { + setOffsetAndLength(node.getOffset(), node.getLength()); } - - private IASTNodeLocation [] locations = null; - /* (non-Javadoc) + + private IASTNodeLocation[] locations = null; + + /* + * (non-Javadoc) + * * @see org.eclipse.cdt.core.dom.ast.IASTNode#getNodeLocations() */ public IASTNodeLocation[] getNodeLocations() { - if( locations != null ) return locations; - if( length == 0 ) return EMPTY_LOCATION_ARRAY; - locations = getTranslationUnit().getLocationInfo( offset, length ); + if (locations != null) + return locations; + if (length == 0) + return EMPTY_LOCATION_ARRAY; + locations = getTranslationUnit().getLocationInfo(offset, length); return locations; } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IASTNode#getRawSignature() - */ - public String getRawSignature() { - return getTranslationUnit().getUnpreprocessedSignature( getNodeLocations() ); - } - - public String getContainingFilename() { - return getTranslationUnit().getContainingFilename( offset ); - } + + /* + * (non-Javadoc) + * + * @see org.eclipse.cdt.core.dom.ast.IASTNode#getRawSignature() + */ + public String getRawSignature() { + return getTranslationUnit().getUnpreprocessedSignature( + getNodeLocations()); + } + + public String getContainingFilename() { + return getTranslationUnit().getContainingFilename(offset); + } + + public IASTFileLocation getFileLocation() { + return getTranslationUnit().flattenLocationsToFile( getNodeLocations() ); + } }