1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Base case DOMScanner/LocationMap support.

This commit is contained in:
John Camelon 2005-01-18 19:46:43 +00:00
parent 2713b4695c
commit 0a8634d93f
9 changed files with 165 additions and 45 deletions

View file

@ -18,6 +18,7 @@ import org.eclipse.cdt.core.model.tests.CModelElementsTests;
import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests;
import org.eclipse.cdt.core.parser.tests.ast2.AST2CPPTests;
import org.eclipse.cdt.core.parser.tests.ast2.AST2Tests;
import org.eclipse.cdt.core.parser.tests.ast2.DOMScannerTests;
import org.eclipse.cdt.core.parser.tests.ast2.GCCTests;
import org.eclipse.cdt.core.parser.tests.parser2.CompleteParser2Tests;
import org.eclipse.cdt.core.parser.tests.parser2.QuickParser2Tests;
@ -61,6 +62,7 @@ public class ParserTestSuite extends TestCase {
suite.addTestSuite( AST2CPPTests.class );
suite.addTestSuite( QuickParser2Tests.class );
suite.addTestSuite( CompleteParser2Tests.class );
suite.addTestSuite( DOMScannerTests.class );
return suite;
}
}

View file

@ -10,6 +10,7 @@
**********************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -22,16 +23,25 @@ import org.eclipse.cdt.internal.core.parser.ParserException;
public class DOMScannerTests extends AST2BaseTest {
public void testSimpleLocation() throws ParserException {
IASTTranslationUnit tu = parse("int x;", ParserLanguage.C); //$NON-NLS-1$
IASTNodeLocation[] nodeLocations = tu.getDeclarations()[0]
.getNodeLocations();
assertNotNull(nodeLocations);
assertEquals(nodeLocations.length, 1);
assertTrue(nodeLocations[0] instanceof IASTFileLocation);
IASTFileLocation fileLocation = ((IASTFileLocation) nodeLocations[0]);
assertEquals(fileLocation.getFileName(), "<text>"); //$NON-NLS-1$
assertEquals(fileLocation.getNodeOffset(), 0);
assertEquals(fileLocation.getNodeLength(), 6);
for( ParserLanguage p = ParserLanguage.C; p != null; p = ( p == ParserLanguage.C ) ? ParserLanguage.CPP : null )
{
IASTTranslationUnit tu = parse("int x;", p); //$NON-NLS-1$
IASTDeclaration declaration = tu.getDeclarations()[0];
IASTNodeLocation[] nodeLocations = declaration
.getNodeLocations();
assertNotNull(nodeLocations);
assertEquals(nodeLocations.length, 1);
assertTrue(nodeLocations[0] instanceof IASTFileLocation);
IASTFileLocation fileLocation = ((IASTFileLocation) nodeLocations[0]);
assertEquals(fileLocation.getFileName(), "<text>"); //$NON-NLS-1$
assertEquals(fileLocation.getNodeOffset(), 0);
assertEquals(fileLocation.getNodeLength(), 6);
IASTNodeLocation [] tuLocations = tu.getNodeLocations();
assertEquals( tuLocations.length, nodeLocations.length );
assertEquals(fileLocation.getFileName(), ((IASTFileLocation)tuLocations[0]).getFileName()); //$NON-NLS-1$
assertEquals(fileLocation.getNodeOffset(), tuLocations[0].getNodeOffset());
assertEquals(fileLocation.getNodeLength(), tuLocations[0].getNodeLength());
}
}
}

View file

@ -23,6 +23,4 @@ public interface IASTFileLocation extends IASTNodeLocation {
* @return the name of the file
*/
public String getFileName();
public void setFileName( String fileName );
}

View file

@ -26,7 +26,6 @@ public interface IASTNodeLocation {
* @return
*/
public int getNodeOffset();
public void setNodeOffset( int offset );
/**
* This is the length of the node contained in this location.
@ -34,6 +33,5 @@ public interface IASTNodeLocation {
* @return
*/
public int getNodeLength();
public void setNodeLength( int length );
}

View file

@ -40,6 +40,7 @@ public abstract class ASTNode implements IASTNode {
public void setOffsetAndLength(int offset, int length) {
this.offset = offset;
this.length = length;
}
public void setOffsetAndLength( ASTNode node )

View file

@ -433,10 +433,11 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
boolean hasFunctionBody = false;
boolean hasFunctionTryBlock = false;
boolean consumedSemi = false;
int semiOffset = 0;
switch (LT(1)) {
case IToken.tSEMI:
consume(IToken.tSEMI);
semiOffset = consume(IToken.tSEMI).getEndOffset();
consumedSemi = true;
break;
case IToken.tLBRACE:
@ -485,8 +486,11 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
IASTSimpleDeclaration simpleDeclaration = createSimpleDeclaration();
int length = figureEndOffset(declSpec, declarators) - firstOffset;
if( consumedSemi )
length = semiOffset - firstOffset;
((ASTNode) simpleDeclaration).setOffsetAndLength(firstOffset,
figureEndOffset(declSpec, declarators) - firstOffset);
length);
simpleDeclaration.setDeclSpecifier(declSpec);
declSpec.setParent(simpleDeclaration);
declSpec.setPropertyInParent(IASTSimpleDeclaration.DECL_SPECIFIER);
@ -1688,6 +1692,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
IASTName declaratorName = null;
IToken la = LA(1);
int startingOffset = la.getOffset();
int finalOffset = startingOffset;
la = null;
List pointerOps = new ArrayList(DEFAULT_POINTEROPS_LIST_SIZE);
List parameters = Collections.EMPTY_LIST;
@ -1698,14 +1703,19 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
overallLoop: do {
consumePointerOperators(pointerOps);
if( ! pointerOps.isEmpty() )
{
finalOffset = calculateEndOffset( (IASTPointerOperator) pointerOps.get( pointerOps.size() - 1 ) );
}
if (LT(1) == IToken.tLPAREN) {
consume();
innerDecl = declarator();
consume(IToken.tRPAREN);
finalOffset = consume(IToken.tRPAREN).getEndOffset();
declaratorName = createName();
} else if (LT(1) == IToken.tIDENTIFIER) {
declaratorName = createName(identifier());
finalOffset = calculateEndOffset(declaratorName );
} else
declaratorName = createName();
@ -1716,19 +1726,23 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
// d.setIsFunction(true);
// TODO need to create a temporary scope object here
IToken last = consume(IToken.tLPAREN);
finalOffset = last.getEndOffset();
isFunction = true;
boolean seenParameter = false;
parameterDeclarationLoop: for (;;) {
switch (LT(1)) {
case IToken.tRPAREN:
last = consume();
finalOffset = last.getEndOffset();
break parameterDeclarationLoop;
case IToken.tELLIPSIS:
last = consume();
encounteredVarArgs = true;
finalOffset = last.getEndOffset();
break;
case IToken.tCOMMA:
last = consume();
finalOffset = last.getEndOffset();
seenParameter = false;
break;
default:
@ -1737,6 +1751,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
.getEndOffset()
- startingOffset);
IASTParameterDeclaration pd = parameterDeclaration();
finalOffset = calculateEndOffset(pd);
if (parameters == Collections.EMPTY_LIST)
parameters = new ArrayList(
DEFAULT_PARAMETERS_LIST_SIZE);
@ -1750,10 +1765,13 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
if (arrayMods == Collections.EMPTY_LIST)
arrayMods = new ArrayList(DEFAULT_POINTEROPS_LIST_SIZE);
consumeArrayModifiers(arrayMods);
if( !arrayMods.isEmpty() )
finalOffset = calculateEndOffset((IASTArrayModifier) arrayMods.get( arrayMods.size() - 1 ));
continue;
case IToken.tCOLON:
consume(IToken.tCOLON);
bitField = constantExpression();
finalOffset = calculateEndOffset(bitField);
default:
break;
}
@ -1808,6 +1826,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
declaratorName.setPropertyInParent(IASTDeclarator.DECLARATOR_NAME);
}
((ASTNode)d).setOffsetAndLength( startingOffset, finalOffset - startingOffset );
return d;
}
@ -1835,8 +1854,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
*/
protected IASTName createName(IToken t) {
IASTName n = new CASTName(t.getCharImage());
((ASTNode) n).setOffsetAndLength(t.getOffset(), t.getEndOffset()
- t.getOffset());
((ASTNode) n).setOffsetAndLength(t.getOffset(), t.getEndOffset() - t.getOffset());
return n;
}

View file

@ -2567,11 +2567,12 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
boolean hasFunctionBody = false;
boolean hasFunctionTryBlock = false;
boolean consumedSemi = false;
int semiOffset = 0;
List constructorChain = Collections.EMPTY_LIST;
switch (LT(1)) {
case IToken.tSEMI:
consume(IToken.tSEMI);
semiOffset = consume(IToken.tSEMI).getEndOffset();
consumedSemi = true;
break;
case IToken.t_try:
@ -2669,8 +2670,12 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
IASTSimpleDeclaration simpleDeclaration = createSimpleDeclaration();
int length = figureEndOffset(declSpec, declarators) - firstOffset;
if( consumedSemi )
length = semiOffset - firstOffset;
((ASTNode) simpleDeclaration).setOffsetAndLength(firstOffset,
figureEndOffset(declSpec, declarators) - firstOffset);
length);
simpleDeclaration.setDeclSpecifier(declSpec);
declSpec.setParent(simpleDeclaration);
declSpec.setPropertyInParent(IASTSimpleDeclaration.DECL_SPECIFIER);
@ -3447,17 +3452,19 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
IASTExpression bitField = null;
boolean isFunction = false;
boolean isPureVirtual = false, isConst = false, isVolatile = false;
int finalOffset = startingOffset;
overallLoop: do {
consumePointerOperators(pointerOps);
if( ! pointerOps.isEmpty() )
finalOffset = calculateEndOffset( (IASTNode) pointerOps.get( pointerOps.size() - 1 ) );
if (!forTypeID && LT(1) == IToken.tLPAREN) {
IToken mark = mark();
try {
consume();
innerDecl = declarator(strategy, forTypeID);
consume(IToken.tRPAREN);
finalOffset = consume(IToken.tRPAREN).getEndOffset();
} catch (BacktrackException bte) {
backup(mark);
}
@ -3466,6 +3473,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
try {
ITokenDuple d = consumeTemplatedOperatorName();
declaratorName = createName(d);
finalOffset = calculateEndOffset(declaratorName);
if (d.isConversion())
isFunction = true;
} catch (BacktrackException bt) {
@ -3506,19 +3514,23 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
isFunction = true;
// TODO need to create a temporary scope object here
IToken last = consume(IToken.tLPAREN);
finalOffset = last.getEndOffset();
boolean seenParameter = false;
parameterDeclarationLoop: for (;;) {
switch (LT(1)) {
case IToken.tRPAREN:
last = consume();
finalOffset = last.getEndOffset();
break parameterDeclarationLoop;
case IToken.tELLIPSIS:
last = consume();
encounteredVarArgs = true;
finalOffset = last.getEndOffset();
break;
case IToken.tCOMMA:
last = consume();
seenParameter = false;
finalOffset = last.getEndOffset();
break;
default:
int endOffset = (last != null) ? last
@ -3527,6 +3539,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
throwBacktrack(startingOffset, endOffset
- startingOffset);
IASTParameterDeclaration p = parameterDeclaration();
finalOffset = calculateEndOffset(p);
if (parameters == Collections.EMPTY_LIST)
parameters = new ArrayList(
DEFAULT_PARM_LIST_SIZE);
@ -3556,7 +3569,9 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
// This will be determined further below
while ((LT(1) == IToken.t_const || LT(1) == IToken.t_volatile)
&& numCVModifiers < 2) {
cvModifiers[numCVModifiers++] = consume();
IToken t = consume();
finalOffset = t.getEndOffset();
cvModifiers[numCVModifiers++] = t;
afterCVModifier = mark();
}
//check for throws clause here
@ -3570,7 +3585,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
while (!done) {
switch (LT(1)) {
case IToken.tRPAREN:
consume();
finalOffset = consume().getEndOffset();
done = true;
break;
case IToken.tCOMMA:
@ -3599,7 +3614,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
char[] image = LA(2).getCharImage();
if (image.length == 1 && image[0] == '0') {
consume(IToken.tASSIGN);
consume(IToken.tINTEGER);
finalOffset = consume(IToken.tINTEGER).getEndOffset();
isPureVirtual = true;
}
}
@ -3624,10 +3639,13 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
case IToken.tLBRACKET:
arrayMods = new ArrayList(DEFAULT_POINTEROPS_LIST_SIZE);
consumeArrayModifiers(arrayMods);
if( ! arrayMods.isEmpty() )
finalOffset = calculateEndOffset( (IASTNode) arrayMods.get( arrayMods.size() - 1 ) );
continue;
case IToken.tCOLON:
consume(IToken.tCOLON);
bitField = constantExpression();
finalOffset = calculateEndOffset( bitField );
break;
default:
break;
@ -3698,6 +3716,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
declaratorName.setPropertyInParent(IASTDeclarator.DECLARATOR_NAME);
}
((ASTNode)d).setOffsetAndLength( startingOffset, finalOffset - startingOffset );
return d;
}

View file

@ -33,7 +33,8 @@ import org.eclipse.cdt.internal.core.parser.token.SimpleToken;
public class DOMScanner extends BaseScanner {
private final ICodeReaderFactory codeReaderFactory;
private int contextStart = 0;
private int globalCounter = 0;
private int contextDelta = 0;
private static class DOMInclusion
{
@ -133,7 +134,11 @@ public class DOMScanner extends BaseScanner {
protected Object popContext() {
//TODO calibrate offsets
Object result = super.popContext();
if( result instanceof CodeReader )
{
globalCounter += (((CodeReader)result).buffer.length - contextDelta);
locationMap.endTranslationUnit( globalCounter );
}
return result;
}
/**
@ -147,9 +152,9 @@ public class DOMScanner extends BaseScanner {
if( bufferData[mostRelevant] instanceof InclusionData || bufferData[mostRelevant] instanceof CodeReader )
break;
MacroData data = (MacroData)bufferData[mostRelevant + 1];
return new SimpleExpansionToken( signal, data.startOffset, data.endOffset - data.startOffset + 1, getCurrentFilename(), getLineNumber( bufferPos[mostRelevant] + 1));
return new SimpleExpansionToken( signal, resolveOffset( data.startOffset ), data.endOffset - data.startOffset + 1, getCurrentFilename(), getLineNumber( bufferPos[mostRelevant] + 1));
}
return new SimpleToken(signal, bufferPos[bufferStackPos] + 1 , getCurrentFilename(), getLineNumber( bufferPos[bufferStackPos] + 1) );
return new SimpleToken(signal, resolveOffset( bufferPos[bufferStackPos] + 1 ) , getCurrentFilename(), getLineNumber( bufferPos[bufferStackPos] + 1) );
}
protected IToken newToken( int signal, char [] buffer )
@ -161,9 +166,9 @@ public class DOMScanner extends BaseScanner {
if( bufferData[mostRelevant] instanceof InclusionData || bufferData[mostRelevant] instanceof CodeReader )
break;
MacroData data = (MacroData)bufferData[mostRelevant + 1];
return new ImagedExpansionToken( signal, buffer, data.startOffset, data.endOffset - data.startOffset + 1, getCurrentFilename(), getLineNumber( bufferPos[mostRelevant] + 1));
return new ImagedExpansionToken( signal, buffer, resolveOffset( data.startOffset ), data.endOffset - data.startOffset + 1, getCurrentFilename(), getLineNumber( bufferPos[mostRelevant] + 1));
}
IToken i = new ImagedToken(signal, buffer, bufferPos[bufferStackPos] + 1 , getCurrentFilename(), getLineNumber( bufferPos[bufferStackPos] + 1));
IToken i = new ImagedToken(signal, buffer, resolveOffset( bufferPos[bufferStackPos] + 1 ), EMPTY_CHAR_ARRAY, getLineNumber( bufferPos[bufferStackPos] + 1));
if( buffer != null && buffer.length == 0 && signal != IToken.tSTRING && signal != IToken.tLSTRING )
bufferPos[bufferStackPos] += 1; //TODO - remove this hack at some point
@ -216,7 +221,7 @@ public class DOMScanner extends BaseScanner {
* @return
*/
private int resolveOffset(int offset) {
return contextStart + offset;
return globalCounter - contextDelta + offset;
}
@ -232,9 +237,8 @@ public class DOMScanner extends BaseScanner {
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#throwEOF()
*/
protected void throwEOF() throws EndOfFileException {
locationMap.endTranslationUnit( preprocessedOffset );
locationMap.endTranslationUnit( globalCounter );
super.throwEOF();
}
protected int preprocessedOffset = 0;
}

View file

@ -14,6 +14,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
@ -26,12 +27,75 @@ import org.eclipse.cdt.core.dom.ast.IASTProblem;
*/
public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
public static class Location implements IASTNodeLocation
{
private final int nodeOffset;
private final int nodeLength;
/**
* @param offset
* @param length
*/
public Location(int offset, int length) {
nodeOffset = offset;
nodeLength = length;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTNodeLocation#getNodeOffset()
*/
public int getNodeOffset() {
return nodeOffset;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTNodeLocation#getNodeLength()
*/
public int getNodeLength() {
return nodeLength;
}
}
/**
* @author jcamelon
*/
public static class FileLocation extends Location implements IASTFileLocation {
private String fileName;
/**
* @param length
* @param offset
* @param tu_filename
*
*/
public FileLocation(char[] tu_filename, int offset, int length) {
super( offset, length );
fileName = new String( tu_filename );
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTFileLocation#getFileName()
*/
public String getFileName() {
return fileName;
}
}
private static final char [] EMPTY_CHAR_ARRAY = "".toCharArray(); //$NON-NLS-1$
private List problems = Collections.EMPTY_LIST;
private List inclusions = Collections.EMPTY_LIST;
private List macroExpansions = Collections.EMPTY_LIST;
private static final IASTProblem[] EMPTY_PROBLEMS_ARRAY = new IASTProblem[0];
private static final IASTNodeLocation [] EMPTY_LOCATION_ARRAY = new IASTNodeLocation[0];
private char[] tu_filename = EMPTY_CHAR_ARRAY ;
// private int finalOffset = 0;
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private int finalOffset = 0;
/**
*
@ -79,7 +143,14 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
*/
public IASTNodeLocation[] getLocations(int offset, int length) {
if( tu_filename == EMPTY_CHAR_ARRAY ) return EMPTY_LOCATION_ARRAY;
return null;
if( macroExpansions.isEmpty() && inclusions.isEmpty() )
{
if( offset + length > finalOffset ) return EMPTY_LOCATION_ARRAY;
IASTNodeLocation [] result = new IASTNodeLocation[1];
result[0] = new FileLocation( tu_filename, offset, length );
return result;
}
return EMPTY_LOCATION_ARRAY;
}
/*
@ -107,7 +178,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#endTranslationUnit(int)
*/
public void endTranslationUnit(int offset) {
// this.finalOffset = offset;
this.finalOffset = offset;
}
/*
@ -117,8 +188,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* int)
*/
public void startInclusion(char[] includePath, int offset) {
// TODO Auto-generated method stub
inclusions.add( new String( includePath ));
}
/*
@ -296,8 +366,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.ILocationResolver#getTranslationUnitPath()
*/
public String getTranslationUnitPath() {
// TODO Auto-generated method stub
return null;
if( tu_filename == EMPTY_CHAR_ARRAY ) return ""; //$NON-NLS-1$
return new String( tu_filename );
}
/*
@ -306,9 +376,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.ILocationResolver#getInclusionsPaths()
*/
public String[] getInclusionsPaths() {
// TODO Auto-generated method stub
return null;
}
if (inclusions == Collections.EMPTY_LIST)
return EMPTY_STRING_ARRAY;
return (String[]) inclusions.toArray(new String[inclusions.size()]); }
/*
* (non-Javadoc)