From a11aba0b33b41fcd00ce52271ecc5c84c07bb89e Mon Sep 17 00:00:00 2001 From: John Camelon Date: Tue, 5 Apr 2005 18:31:36 +0000 Subject: [PATCH] Fixed Bug 88996 - [DOM AST] IASTFileLocation needs to make reference to line numbers. --- .../cdt/core/dom/ast/IASTFileLocation.java | 14 ++++ .../core/parser/scanner2/LocationMap.java | 64 +++++++++++++++++-- .../cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java | 2 +- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTFileLocation.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTFileLocation.java index a21e2e22e4b..e56324ca845 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTFileLocation.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTFileLocation.java @@ -23,4 +23,18 @@ public interface IASTFileLocation extends IASTNodeLocation { * @return the name of the file */ public String getFileName(); + + /** + * Get the starting line number. + * + * @return in representing line number + */ + public int getStartingLineNumber(); + + /** + * Get the ending line number. + * + * @return int representing line number + */ + public int getEndingLineNumber(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java index 555ded7fa52..5f63c117c4f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java @@ -87,6 +87,11 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { public IASTFileLocation asFileLocation() { return rootNode.flattenLocationsToFile(getExpansionLocations()); } + + public String toString() { + + return "Macro Expansion " + definition.getName().toString() + " flattened to " + asFileLocation().toString(); //$NON-NLS-1$ //$NON-NLS-2$ + } } @@ -871,10 +876,11 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { } - public static class FileLocation extends Location implements + public class FileLocation extends Location implements IASTFileLocation { private String fileName; + private _CompositeFileContext _fileContext; /** * @param length @@ -900,6 +906,40 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { return this; } + public int getStartingLineNumber() { + _CompositeFileContext i = getFileContext(); + if( i != null ) + return i.getLineNumber( getNodeOffset() ); + return 0; + } + + private _CompositeFileContext getFileContext() { + if( _fileContext == null ) + { + if( fileName.equals( getTranslationUnitPath() )) + _fileContext = tu; + else + _fileContext = findInclusion( tu, fileName ); + } + return _fileContext; + } + + public int getEndingLineNumber() { + _CompositeFileContext i = getFileContext(); + if( i != null ) + return i.getLineNumber( getNodeOffset() + getNodeLength()); + return 0; + } + + public String toString() { + StringBuffer buffer = new StringBuffer( fileName ); + buffer.append(" line " ); //$NON-NLS-1$ + buffer.append( getStartingLineNumber() ); + buffer.append( " to " ); //$NON-NLS-1$ + buffer.append( getEndingLineNumber() ); + return buffer.toString(); + } + } protected static class _Context { @@ -1050,6 +1090,16 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { this.reader = reader; } + public int getLineNumber(int nodeOffset) { + int lineNumber = 1; + for( int i = 0; i < nodeOffset; ++i ) + { + if( reader.buffer[i] == '\n') + ++lineNumber; + } + return lineNumber; + } + } protected static class _Inclusion extends _CompositeFileContext implements @@ -2137,19 +2187,19 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { endOffset, taken)); } - private _Context findInclusion(_CompositeContext context, String path) { - _Context foundContext = null; + _Inclusion findInclusion(_CompositeContext context, String path) { + _Inclusion foundContext = null; _Context[] contexts = context.getSubContexts(); - + _Inclusion tempContext= null; for (int i = 0; foundContext == null && i < contexts.length; i++) { if (contexts[i] instanceof _Inclusion) { - context = (_Inclusion) contexts[i]; + tempContext = (_Inclusion) contexts[i]; // check if the file matches the #include if (CharArrayUtils.equals( - ((_Inclusion) context).reader.filename, path + tempContext.reader.filename, path .toCharArray())) { - foundContext = context; + foundContext = tempContext; break; } foundContext = findInclusion(context, path); diff --git a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java index 6b20f4aec50..61fefe14b2e 100644 --- a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java +++ b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMASTNodeLeaf.java @@ -481,7 +481,7 @@ public class DOMASTNodeLeaf implements IAdaptable { else buffer.append(trimObjectToString(obj.toString())); } else - buffer.append(trimObjectToString(obj.toString())); + buffer.append(obj.toString()); if( obj instanceof IBinding ){ buffer.append( OPEN_PAREN );