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

IASTNodeLocation support for #inclusions.

This commit is contained in:
John Camelon 2005-01-21 21:12:17 +00:00
parent 0f10569314
commit e22c304553
10 changed files with 836 additions and 504 deletions

View file

@ -16,13 +16,7 @@ import junit.framework.TestSuite;
import org.eclipse.cdt.core.model.tests.CModelElementsTests; import org.eclipse.cdt.core.model.tests.CModelElementsTests;
import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests; 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.DOMParserTestSuite;
import org.eclipse.cdt.core.parser.tests.ast2.AST2KnRTests;
import org.eclipse.cdt.core.parser.tests.ast2.AST2Tests;
import org.eclipse.cdt.core.parser.tests.ast2.CompleteParser2Tests;
import org.eclipse.cdt.core.parser.tests.ast2.DOMLocationTests;
import org.eclipse.cdt.core.parser.tests.ast2.GCCTests;
import org.eclipse.cdt.core.parser.tests.ast2.QuickParser2Tests;
import org.eclipse.cdt.core.parser.tests.scanner2.ObjectMapTest; import org.eclipse.cdt.core.parser.tests.scanner2.ObjectMapTest;
import org.eclipse.cdt.core.parser.tests.scanner2.Scanner2Test; import org.eclipse.cdt.core.parser.tests.scanner2.Scanner2Test;
@ -58,13 +52,7 @@ public class ParserTestSuite extends TestCase {
suite.addTest( IScannerInfoPluginTest.suite() ); suite.addTest( IScannerInfoPluginTest.suite() );
suite.addTest( ScannerParserLoopTest.suite() ); suite.addTest( ScannerParserLoopTest.suite() );
suite.addTest( GCCParserExtensionTestSuite.suite() ); suite.addTest( GCCParserExtensionTestSuite.suite() );
suite.addTestSuite( AST2Tests.class ); suite.addTest( DOMParserTestSuite.suite() );
suite.addTestSuite( GCCTests.class );
suite.addTestSuite( AST2CPPTests.class );
suite.addTestSuite( QuickParser2Tests.class );
suite.addTestSuite( CompleteParser2Tests.class );
suite.addTestSuite( DOMLocationTests.class );
suite.addTestSuite( AST2KnRTests.class );
return suite; return suite;
} }
} }

View file

@ -12,9 +12,17 @@ package org.eclipse.cdt.core.parser.tests.ast2;
import java.io.InputStream; import java.io.InputStream;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.CDOM; import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.ICodeReaderFactory; import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
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.core.dom.ast.IASTProblem; import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService; import org.eclipse.cdt.core.parser.IParserLogService;
@ -67,9 +75,11 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
language, NULL_LOG, getScannerConfig(language), factory); language, NULL_LOG, getScannerConfig(language), factory);
ISourceCodeParser parser = null; ISourceCodeParser parser = null;
if (language == ParserLanguage.CPP) { if (language == ParserLanguage.CPP) {
parser = new GNUCPPSourceParser( scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, new GPPParserExtensionConfiguration() ); parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GPPParserExtensionConfiguration());
} else { } else {
parser = new GNUCSourceParser( scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, new GCCParserExtensionConfiguration() ); parser = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GCCParserExtensionConfiguration());
} }
stream.close(); stream.close();
IASTTranslationUnit parseResult = parser.parse(); IASTTranslationUnit parseResult = parser.parse();
@ -92,9 +102,79 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
* @param language * @param language
* @return * @return
*/ */
private IScannerExtensionConfiguration getScannerConfig(ParserLanguage language) { private IScannerExtensionConfiguration getScannerConfig(
ParserLanguage language) {
if (language == ParserLanguage.CPP) if (language == ParserLanguage.CPP)
return new GPPScannerExtensionConfiguration(); return new GPPScannerExtensionConfiguration();
return new GCCScannerExtensionConfiguration(); return new GCCScannerExtensionConfiguration();
} }
/**
* @param pathEndsWith
* TODO
* @param offset
* @param length
* @param declarator
*/
private void assertSoleFileLocation(IASTNode n, String pathEndsWith,
int offset, int length) {
IASTNodeLocation[] locations = n.getNodeLocations();
assertEquals(locations.length, 1);
IASTFileLocation nodeLocation = (IASTFileLocation) locations[0];
assertTrue(nodeLocation.getFileName().endsWith(pathEndsWith));
assertEquals(nodeLocation.getNodeOffset(), offset);
assertEquals(nodeLocation.getNodeLength(), length);
}
public void testSimpleInclusion() throws Exception {
String foo = "int FOO;"; //$NON-NLS-1$
String code = "int bar;\n#include \"foo.h\"\n"; //$NON-NLS-1$
importFile("foo.h", foo); //$NON-NLS-1$
IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 2);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
assertSoleFileLocation(bar,
"code.cpp", code.indexOf("int"), code.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(FOO,
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
public void testSimpleInclusion2() throws Exception {
String foo = "int FOO;"; //$NON-NLS-1$
String code = "int bar;\n#include \"foo.h\"\nfloat byob;\n"; //$NON-NLS-1$
importFile("foo.h", foo); //$NON-NLS-1$
IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 3);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
IASTSimpleDeclaration byob = (IASTSimpleDeclaration) declarations[2];
assertSoleFileLocation(bar,
"code.cpp", code.indexOf("int"), code.indexOf("r;") + 2 - code.indexOf( "int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(FOO,
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1 - foo.indexOf( "int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation( byob, "code.cpp", code.indexOf( "float"), code.indexOf( "b;") + 2 - code.indexOf( "float") ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
public static Test suite() {
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class);
suite.addTest(new DOMLocationInclusionTests("cleanupProject")); //$NON-NLS-1$
return suite;
}
} }

View file

@ -0,0 +1,39 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.tests.ParserTestSuite;
/**
* @author jcamelon
*/
public class DOMParserTestSuite extends TestCase {
public static Test suite() {
TestSuite suite= new TestSuite(ParserTestSuite.class.getName());
suite.addTestSuite( AST2Tests.class );
suite.addTestSuite( GCCTests.class );
suite.addTestSuite( AST2CPPTests.class );
suite.addTestSuite( QuickParser2Tests.class );
suite.addTestSuite( CompleteParser2Tests.class );
// suite.addTestSuite( DOMScannerTests.class );
suite.addTestSuite( DOMLocationTests.class );
suite.addTest( DOMLocationInclusionTests.suite() );
suite.addTestSuite( AST2KnRTests.class );
return suite;
}
}

View file

@ -0,0 +1,34 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import junit.framework.TestCase;
/**
* @author jcamelon
*/
public class DOMScannerTests extends TestCase {
/**
*
*/
public DOMScannerTests() {
super();
}
/**
* @param name
*/
public DOMScannerTests(String name) {
super(name);
}
}

View file

@ -1278,26 +1278,11 @@ abstract class BaseScanner implements IScanner {
} }
pushContext(buffer); pushContext(buffer);
bufferData[bufferStackPos] = data; bufferData[bufferStackPos] = data;
if (data instanceof InclusionData)
pushInclusion(data);
} }
/**
* @param data
*/
protected void pushInclusion(Object data) {
if (log.isTracing()) {
StringBuffer b = new StringBuffer("Entering inclusion "); //$NON-NLS-1$
b.append(((InclusionData) data).reader.filename);
log.traceLog(b.toString());
}
}
protected Object popContext() { protected Object popContext() {
bufferStack[bufferStackPos] = null; bufferStack[bufferStackPos] = null;
if (bufferData[bufferStackPos] instanceof InclusionData)
popInclusion(((InclusionData) bufferData[bufferStackPos]).inclusion);
Object result = bufferData[bufferStackPos]; Object result = bufferData[bufferStackPos];
bufferData[bufferStackPos] = null; bufferData[bufferStackPos] = null;
@ -1308,20 +1293,6 @@ abstract class BaseScanner implements IScanner {
return result; return result;
} }
/**
* @param data
* TODO
*
*/
protected void popInclusion(java.lang.Object data) {
if (log.isTracing()) {
StringBuffer buffer = new StringBuffer("Exiting inclusion "); //$NON-NLS-1$
buffer
.append(((InclusionData) bufferData[bufferStackPos]).reader.filename);
log.traceLog(buffer.toString());
}
}
/** /**
* *
*/ */
@ -1991,7 +1962,7 @@ abstract class BaseScanner implements IScanner {
pushContext(expText); pushContext(expText);
} }
if (expanding) if (expanding)
return new MacroExpansionToken(); return EXPANSION_TOKEN;
} }
char[] result = escapedNewline ? removedEscapedNewline(buffer, start, len) char[] result = escapedNewline ? removedEscapedNewline(buffer, start, len)
@ -2683,12 +2654,11 @@ abstract class BaseScanner implements IScanner {
fileCache.put(reader.filename, reader); fileCache.put(reader.filename, reader);
} }
if (reader != null) { if (reader != null) {
Object inclusion = createInclusionConstruct(fileNameArray, pushContext(reader.buffer, new InclusionData(reader,
createInclusionConstruct(fileNameArray,
reader.filename, local, startOffset, reader.filename, local, startOffset,
startingLineNumber, nameOffset, nameEndOffset, startingLineNumber, nameOffset, nameEndOffset,
nameLine, endOffset, endLine, false); nameLine, endOffset, endLine, false)));
pushContext(reader.buffer, new InclusionData(reader,
inclusion));
return; return;
} }
} }
@ -4377,6 +4347,7 @@ abstract class BaseScanner implements IScanner {
protected static final char[] TAB = { '\t' }; protected static final char[] TAB = { '\t' };
protected static final char[] SPACE = { ' ' }; protected static final char[] SPACE = { ' ' };
private static final MacroExpansionToken EXPANSION_TOKEN = new MacroExpansionToken();
static { static {
CharArrayIntMap words = new CharArrayIntMap(IToken.tLAST, -1); CharArrayIntMap words = new CharArrayIntMap(IToken.tLAST, -1);

View file

@ -10,7 +10,6 @@
**********************************************************************/ **********************************************************************/
package org.eclipse.cdt.internal.core.parser.scanner2; package org.eclipse.cdt.internal.core.parser.scanner2;
import org.eclipse.cdt.core.dom.ICodeReaderFactory; import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.ast.IASTProblem; import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
@ -32,213 +31,249 @@ import org.eclipse.cdt.internal.core.parser.token.SimpleToken;
*/ */
public class DOMScanner extends BaseScanner { public class DOMScanner extends BaseScanner {
private final ICodeReaderFactory codeReaderFactory; private final ICodeReaderFactory codeReaderFactory;
private int globalCounter = 0; private int globalCounter = 0;
private int contextDelta = 0; private int contextDelta = 0;
private static class DOMInclusion
{
public final char[] pt;
public final int o;
/** private static class DOMInclusion {
* public final char[] pt;
*/ public final int o;
public DOMInclusion( char [] path, int offset ) {
this.pt = path;
this.o = offset;
}
}
/** /**
* @param reader *
* @param info */
* @param parserMode public DOMInclusion(char[] path, int offset) {
* @param language this.pt = path;
* @param log this.o = offset;
* @param readerFactory TODO }
* @param requestor }
*/
public DOMScanner(CodeReader reader, IScannerInfo info, ParserMode parserMode, ParserLanguage language, IParserLogService log, IScannerExtensionConfiguration configuration, ICodeReaderFactory readerFactory) {
super(reader, info, parserMode, language, log, configuration);
this.expressionEvaluator = new ExpressionEvaluator(null, null);
this.codeReaderFactory = readerFactory;
postConstructorSetup(reader, info);
}
/* (non-Javadoc) /**
* @see org.eclipse.cdt.core.parser.IScanner#getLocationResolver() * @param reader
*/ * @param info
public ILocationResolver getLocationResolver() { * @param parserMode
if( locationMap instanceof ILocationResolver ) * @param language
return (ILocationResolver) locationMap; * @param log
return null; * @param readerFactory
} * TODO
* @param requestor
*/
public DOMScanner(CodeReader reader, IScannerInfo info,
ParserMode parserMode, ParserLanguage language, IParserLogService log,
IScannerExtensionConfiguration configuration,
ICodeReaderFactory readerFactory) {
super(reader, info, parserMode, language, log, configuration);
this.expressionEvaluator = new ExpressionEvaluator(null, null);
this.codeReaderFactory = readerFactory;
postConstructorSetup(reader, info);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.parser.IScanner#getLocationResolver()
*/
public ILocationResolver getLocationResolver() {
if (locationMap instanceof ILocationResolver)
return (ILocationResolver) locationMap;
return null;
}
final IScannerPreprocessorLog locationMap = new LocationMap();
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.parser.IScanner#setASTFactory(org.eclipse.cdt.core.parser.ast.IASTFactory)
*/
public void setASTFactory(IASTFactory f) {
// do nothing
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createInclusionConstruct(char[],
* char[], boolean, int, int, int, int, int, int, int, boolean)
*/
protected Object createInclusionConstruct(char[] fileName,
char[] filenamePath, boolean local, int startOffset,
int startingLineNumber, int nameOffset, int nameEndOffset,
int nameLine, int endOffset, int endLine, boolean isForced) {
return new DOMInclusion(filenamePath, resolveOffset(startOffset));
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#processMacro(char[],
* int, int, int, int, int, int, int)
*/
protected void processMacro(char[] name, int startingOffset,
int startingLineNumber, int idstart, int idend, int nameLine,
int textEnd, int endingLine, IMacro macro) {
if (macro instanceof ObjectStyleMacro)
locationMap.defineObjectStyleMacro((ObjectStyleMacro) macro,
startingLineNumber, idstart, idend, textEnd);
else if (macro instanceof FunctionStyleMacro)
locationMap.defineFunctionStyleMacro((FunctionStyleMacro) macro,
startingLineNumber, idstart, idend, textEnd);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createReaderDuple(java.lang.String)
*/
protected CodeReader createReaderDuple(String finalPath) {
return codeReaderFactory.createCodeReaderForInclusion(finalPath);
}
final IScannerPreprocessorLog locationMap = new LocationMap(); /*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#pushContext(char[],
* java.lang.Object)
*/
protected void pushContext(char[] buffer, Object data) {
if (data instanceof InclusionData) {
if (log.isTracing()) {
StringBuffer b = new StringBuffer("Entering inclusion "); //$NON-NLS-1$
b.append(((InclusionData) data).reader.filename);
log.traceLog(b.toString());
}
DOMInclusion inc = ((DOMInclusion) ((InclusionData) data).inclusion);
globalCounter += getCurrentOffset();
locationMap.startInclusion(inc.pt, inc.o, globalCounter);
contextDelta = 0;
}
super.pushContext(buffer, data);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#popContext()
*/
protected Object popContext() {
//TODO calibrate offsets
Object result = super.popContext();
if (result instanceof CodeReader) {
globalCounter += (((CodeReader) result).buffer.length - contextDelta);
locationMap.endTranslationUnit(globalCounter);
}
if (result instanceof InclusionData) {
if (log.isTracing()) {
StringBuffer buffer = new StringBuffer("Exiting inclusion "); //$NON-NLS-1$
buffer
.append(((InclusionData) bufferData[bufferStackPos]).reader.filename);
log.traceLog(buffer.toString());
}
/* (non-Javadoc) globalCounter += (((InclusionData) result).reader.buffer.length - contextDelta);
* @see org.eclipse.cdt.core.parser.IScanner#setASTFactory(org.eclipse.cdt.core.parser.ast.IASTFactory) contextDelta = getCurrentOffset();
*/ locationMap.endInclusion(globalCounter);
public void setASTFactory(IASTFactory f) { }
// do nothing return result;
} }
/* (non-Javadoc) /**
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createInclusionConstruct(char[], char[], boolean, int, int, int, int, int, int, int, boolean) * @return
*/ */
protected Object createInclusionConstruct(char[] fileName, char[] filenamePath, boolean local, int startOffset, int startingLineNumber, int nameOffset, int nameEndOffset, int nameLine, int endOffset, int endLine, boolean isForced) { protected IToken newToken(int signal) {
return new DOMInclusion( filenamePath, startOffset ); if (bufferData[bufferStackPos] instanceof MacroData) {
} int mostRelevant;
for (mostRelevant = bufferStackPos; mostRelevant >= 0; --mostRelevant)
if (bufferData[mostRelevant] instanceof InclusionData
|| bufferData[mostRelevant] instanceof CodeReader)
break;
MacroData data = (MacroData) bufferData[mostRelevant + 1];
return new SimpleExpansionToken(signal,
resolveOffset(data.startOffset), data.endOffset
- data.startOffset + 1, getCurrentFilename(),
getLineNumber(bufferPos[mostRelevant] + 1));
}
return new SimpleToken(signal,
resolveOffset(bufferPos[bufferStackPos] + 1), getCurrentFilename(),
getLineNumber(bufferPos[bufferStackPos] + 1));
}
/* (non-Javadoc) protected IToken newToken(int signal, char[] buffer) {
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#processMacro(char[], int, int, int, int, int, int, int) if (bufferData[bufferStackPos] instanceof MacroData) {
*/ int mostRelevant;
protected void processMacro(char[] name, int startingOffset, int startingLineNumber, int idstart, int idend, int nameLine, int textEnd, int endingLine, IMacro macro) { for (mostRelevant = bufferStackPos; mostRelevant >= 0; --mostRelevant)
if( macro instanceof ObjectStyleMacro ) if (bufferData[mostRelevant] instanceof InclusionData
locationMap.defineObjectStyleMacro( (ObjectStyleMacro) macro, startingLineNumber, idstart, idend, textEnd ); || bufferData[mostRelevant] instanceof CodeReader)
else if( macro instanceof FunctionStyleMacro ) break;
locationMap.defineFunctionStyleMacro( (FunctionStyleMacro) macro, startingLineNumber, idstart, idend, textEnd ); MacroData data = (MacroData) bufferData[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,
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
/* (non-Javadoc) return i;
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createReaderDuple(java.lang.String) }
*/
protected CodeReader createReaderDuple(String finalPath) {
return codeReaderFactory.createCodeReaderForInclusion(finalPath);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#pushContext(char[])
*/
protected void pushContext(char[] buffer) {
//TODO calibrate offsets
super.pushContext(buffer);
}
/* (non-Javadoc) /*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#pushContext(char[], java.lang.Object) * (non-Javadoc)
*/ *
protected void pushContext(char[] buffer, Object data) { * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#quickParsePushPopInclusion(java.lang.Object)
//TODO calibrate offsets */
super.pushContext(buffer, data); protected void quickParsePushPopInclusion(Object inclusion) {
} //do nothing
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#popContext()
*/
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;
}
/**
* @return
*/
protected IToken newToken( int signal ) {
if( bufferData[bufferStackPos] instanceof MacroData )
{
int mostRelevant;
for( mostRelevant = bufferStackPos; mostRelevant >= 0; --mostRelevant )
if( bufferData[mostRelevant] instanceof InclusionData || bufferData[mostRelevant] instanceof CodeReader )
break;
MacroData data = (MacroData)bufferData[mostRelevant + 1];
return new SimpleExpansionToken( signal, resolveOffset( data.startOffset ), data.endOffset - data.startOffset + 1, getCurrentFilename(), getLineNumber( bufferPos[mostRelevant] + 1));
}
return new SimpleToken(signal, resolveOffset( bufferPos[bufferStackPos] + 1 ) , getCurrentFilename(), getLineNumber( bufferPos[bufferStackPos] + 1) );
}
protected IToken newToken( int signal, char [] buffer ) /*
{ * (non-Javadoc)
if( bufferData[bufferStackPos] instanceof MacroData ) *
{ * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#handleProblem(int,
int mostRelevant; * int, char[])
for( mostRelevant = bufferStackPos; mostRelevant >= 0; --mostRelevant ) */
if( bufferData[mostRelevant] instanceof InclusionData || bufferData[mostRelevant] instanceof CodeReader ) protected void handleProblem(int id, int offset, char[] arg) {
break; IASTProblem problem = new ScannerASTProblem(id, arg, true, false);
MacroData data = (MacroData)bufferData[mostRelevant + 1]; int o = resolveOffset(offset);
return new ImagedExpansionToken( signal, buffer, resolveOffset( data.startOffset ), data.endOffset - data.startOffset + 1, getCurrentFilename(), getLineNumber( bufferPos[mostRelevant] + 1)); ((ScannerASTProblem) problem).setOffsetAndLength(o,
} resolveOffset(getCurrentOffset() + 1) - o);
IToken i = new ImagedToken(signal, buffer, resolveOffset( bufferPos[bufferStackPos] + 1 ), EMPTY_CHAR_ARRAY, getLineNumber( bufferPos[bufferStackPos] + 1)); locationMap.encounterProblem(problem);
if( buffer != null && buffer.length == 0 && signal != IToken.tSTRING && signal != IToken.tLSTRING ) }
bufferPos[bufferStackPos] += 1; //TODO - remove this hack at some point
return i;
}
/* (non-Javadoc) /**
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#quickParsePushPopInclusion(java.lang.Object) * @param offset
*/ * @return
protected void quickParsePushPopInclusion(Object inclusion) { */
//do nothing private int resolveOffset(int offset) {
} return globalCounter - contextDelta + offset;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#pushInclusion(java.lang.Object)
*/
protected void pushInclusion(Object data) {
super.pushInclusion(data);
if( data instanceof DOMInclusion )
{
DOMInclusion d = (DOMInclusion)data;
locationMap.startInclusion( d.pt, d.o );
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#popInclusion()
*/
protected void popInclusion(Object data) {
super.popInclusion(data);
if( data instanceof DOMInclusion )
{
DOMInclusion d = (DOMInclusion)data;
locationMap.endInclusion( d.pt, d.o );
}
}
/* (non-Javadoc) /*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#handleProblem(int, int, char[]) * (non-Javadoc)
*/ *
protected void handleProblem(int id, int offset, char[] arg) { * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#postConstructorSetup(org.eclipse.cdt.core.parser.CodeReader,
IASTProblem problem = new ScannerASTProblem(id, arg, true, false ); * org.eclipse.cdt.core.parser.IScannerInfo)
int o = resolveOffset( offset );
((ScannerASTProblem)problem).setOffsetAndLength( o, resolveOffset( getCurrentOffset() + 1 ) - o );
locationMap.encounterProblem(problem);
}
/**
* @param offset
* @return
*/
private int resolveOffset(int offset) {
return globalCounter - contextDelta + offset;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#postConstructorSetup(org.eclipse.cdt.core.parser.CodeReader, org.eclipse.cdt.core.parser.IScannerInfo)
*/ */
protected void postConstructorSetup(CodeReader reader, IScannerInfo info) { protected void postConstructorSetup(CodeReader reader, IScannerInfo info) {
super.postConstructorSetup(reader, info); super.postConstructorSetup(reader, info);
locationMap.startTranslationUnit( getMainFilename() ); locationMap.startTranslationUnit(getMainFilename());
} }
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#throwEOF() * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#throwEOF()
*/ */
protected void throwEOF() throws EndOfFileException { protected void throwEOF() throws EndOfFileException {
locationMap.endTranslationUnit( globalCounter ); locationMap.endTranslationUnit(globalCounter);
super.throwEOF(); super.throwEOF();
} }
} }

View file

@ -31,7 +31,6 @@ public interface ILocationResolver {
public IASTProblem[] getScannerProblems(); public IASTProblem[] getScannerProblems();
public String getTranslationUnitPath(); public String getTranslationUnitPath();
public String [] getInclusionsPaths();
public void cleanup(); public void cleanup();

View file

@ -20,40 +20,29 @@ public interface IScannerPreprocessorLog {
public void startTranslationUnit( char [] filename ); public void startTranslationUnit( char [] filename );
public void endTranslationUnit(int offset); public void endTranslationUnit(int offset);
public void startInclusion(char[] includePath, int offset); public void startInclusion(char[] includePath, int offset, int endOffset);
public void endInclusion(int offset);
public void endInclusion(char[] includePath, int offset);
public void enterObjectStyleMacroExpansion(char[] name, char[] expansion, public void enterObjectStyleMacroExpansion(char[] name, char[] expansion,
int offset); int offset);
public void exitObjectStyleMacroExpansion(char[] name, int offset); public void exitObjectStyleMacroExpansion(char[] name, int offset);
public void enterFunctionStyleExpansion(char[] name, char[][] parameters, public void enterFunctionStyleExpansion(char[] name, char[][] parameters,
char[] expansion, int offset); char[] expansion, int offset);
public void exitFunctionStyleExpansion(char[] name, int offset); public void exitFunctionStyleExpansion(char[] name, int offset);
public void defineObjectStyleMacro(ObjectStyleMacro m, int startOffset, public void defineObjectStyleMacro(ObjectStyleMacro m, int startOffset,
int nameOffset, int nameEndOffset, int endOffset); int nameOffset, int nameEndOffset, int endOffset);
public void defineFunctionStyleMacro(FunctionStyleMacro m, int startOffset, public void defineFunctionStyleMacro(FunctionStyleMacro m, int startOffset,
int nameOffset, int nameEndOffset, int endOffset); int nameOffset, int nameEndOffset, int endOffset);
public void encounterPoundIf(int startOffset, int endOffset); public void encounterPoundIf(int startOffset, int endOffset);
public void encounterPoundPragma(int startOffset, int endOffset); public void encounterPoundPragma(int startOffset, int endOffset);
public void encounterPoundError(int startOffset, int endOffset); public void encounterPoundError(int startOffset, int endOffset);
public void encounterPoundIfdef(int startOffset, int endOffset); public void encounterPoundIfdef(int startOffset, int endOffset);
public void encounterPoundUndef(int startOffset, int endOffset); public void encounterPoundUndef(int startOffset, int endOffset);
public void encounterPoundElse(int startOffset, int endOffset); public void encounterPoundElse(int startOffset, int endOffset);
public void encounterPoundElif(int startOffset, int endOffset); public void encounterPoundElif(int startOffset, int endOffset);
public void encounterPoundEndIf(int startOffset, int endOffset); public void encounterPoundEndIf(int startOffset, int endOffset);
public void encounterProblem( IASTProblem problem ); public void encounterProblem( IASTProblem problem );

View file

@ -27,8 +27,7 @@ import org.eclipse.cdt.core.dom.ast.IASTProblem;
*/ */
public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
public static class Location implements IASTNodeLocation public static class Location implements IASTNodeLocation {
{
private final int nodeOffset; private final int nodeOffset;
private final int nodeLength; private final int nodeLength;
@ -41,28 +40,31 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
nodeLength = length; nodeLength = length;
} }
/*
/* (non-Javadoc) * (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.IASTNodeLocation#getNodeOffset() * @see org.eclipse.cdt.core.dom.ast.IASTNodeLocation#getNodeOffset()
*/ */
public int getNodeOffset() { public int getNodeOffset() {
return nodeOffset; return nodeOffset;
} }
/*
/* (non-Javadoc) * (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.IASTNodeLocation#getNodeLength() * @see org.eclipse.cdt.core.dom.ast.IASTNodeLocation#getNodeLength()
*/ */
public int getNodeLength() { public int getNodeLength() {
return nodeLength; return nodeLength;
} }
} }
/** /**
* @author jcamelon * @author jcamelon
*/ */
public static class FileLocation extends Location implements IASTFileLocation { public static class FileLocation extends Location implements
IASTFileLocation {
private String fileName; private String fileName;
@ -70,14 +72,16 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @param length * @param length
* @param offset * @param offset
* @param tu_filename * @param tu_filename
* *
*/ */
public FileLocation(char[] tu_filename, int offset, int length) { public FileLocation(char[] tu_filename, int offset, int length) {
super( offset, length ); super(offset, length);
fileName = new String( tu_filename ); fileName = new String(tu_filename);
} }
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.IASTFileLocation#getFileName() * @see org.eclipse.cdt.core.dom.ast.IASTFileLocation#getFileName()
*/ */
public String getFileName() { public String getFileName() {
@ -85,17 +89,102 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
} }
} }
private static final char [] EMPTY_CHAR_ARRAY = "".toCharArray(); //$NON-NLS-1$
private List problems = Collections.EMPTY_LIST; private static final IASTProblem[] EMPTY_PROBLEMS_ARRAY = new IASTProblem[0];
private List inclusions = Collections.EMPTY_LIST; private static final IASTNodeLocation[] EMPTY_LOCATION_ARRAY = new IASTNodeLocation[0];
private List macroExpansions = Collections.EMPTY_LIST;
private static final IASTProblem[] EMPTY_PROBLEMS_ARRAY = new IASTProblem[0]; public static class Context {
private static final IASTNodeLocation [] EMPTY_LOCATION_ARRAY = new IASTNodeLocation[0]; /**
* @param startOffset
* @param endOffset
private char[] tu_filename = EMPTY_CHAR_ARRAY ; */
private static final String[] EMPTY_STRING_ARRAY = new String[0]; public Context(CompositeContext parent, int startOffset, int endOffset) {
private int finalOffset = 0; this.context_directive_start = startOffset;
this.context_directive_end = endOffset;
this.parent = parent;
}
public final int context_directive_start;
public final int context_directive_end;
public int context_ends = -1;
private final CompositeContext parent;
public CompositeContext getParent() {
return parent;
}
}
public static class CompositeContext extends Context {
public CompositeContext(CompositeContext parent, int startOffset,
int endOffset) {
super(parent, startOffset, endOffset);
}
private static final int DEFAULT_SUBCONTEXT_ARRAY_SIZE = 8;
protected List subContexts = Collections.EMPTY_LIST;
public List getSubContexts() {
return subContexts;
}
public void addSubContext(Context c) {
if (subContexts == Collections.EMPTY_LIST)
subContexts = new ArrayList(DEFAULT_SUBCONTEXT_ARRAY_SIZE);
subContexts.add(c);
}
/**
* @return
*/
public boolean hasSubContexts() {
return subContexts != Collections.EMPTY_LIST;
}
}
public static class Inclusion extends CompositeContext {
public final char[] path;
public Inclusion(CompositeContext parent, char[] path, int startOffset,
int endOffset) {
super(parent, startOffset, endOffset);
this.path = path;
}
}
public static class TranslationUnit extends CompositeContext {
public final char[] path;
/**
* @param startOffset
* @param endOffset
*/
public TranslationUnit(char[] path) {
super(null, 0, 0);
this.path = path;
}
}
public static class Problem extends Context {
public final IASTProblem problem;
/**
* @param parent
* @param startOffset
* @param endOffset
*/
public Problem(CompositeContext parent, int startOffset, int endOffset,
IASTProblem problem) {
super(parent, startOffset, endOffset);
this.problem = problem;
}
}
protected TranslationUnit tu;
protected CompositeContext currentContext;
/** /**
* *
@ -142,15 +231,96 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* int) * int)
*/ */
public IASTNodeLocation[] getLocations(int offset, int length) { public IASTNodeLocation[] getLocations(int offset, int length) {
if( tu_filename == EMPTY_CHAR_ARRAY ) return EMPTY_LOCATION_ARRAY; if (tu == null)
if( macroExpansions.isEmpty() && inclusions.isEmpty() ) return EMPTY_LOCATION_ARRAY;
{ Context c = findContextForOffset(offset);
if( offset + length > finalOffset ) return EMPTY_LOCATION_ARRAY; if (c.context_ends >= offset + length)
IASTNodeLocation [] result = new IASTNodeLocation[1]; return createSoleLocation(c, offset, length);
result[0] = new FileLocation( tu_filename, offset, length );
return EMPTY_LOCATION_ARRAY;
}
/**
* @param c
* @param offset
* @param length
* @return
*/
protected IASTNodeLocation[] createSoleLocation(Context c, int offset,
int length) {
IASTNodeLocation[] result = new IASTNodeLocation[1];
if (c instanceof TranslationUnit) {
result[0] = new FileLocation(((TranslationUnit) c).path, reconcileOffset( c, offset ),
length);
return result; return result;
} }
return EMPTY_LOCATION_ARRAY; if( c instanceof Inclusion )
{
result[0] = new FileLocation(((Inclusion) c).path, reconcileOffset( c, offset ),
length);
return result;
}
return EMPTY_LOCATION_ARRAY;
}
/**
* @param c
* @param offset
* @return
*/
protected static int reconcileOffset(Context c, int offset) {
int subtractOff = 0;
if( c instanceof CompositeContext )
{
List subs = ((CompositeContext)c).getSubContexts();
for( int i = 0; i < subs.size(); ++i )
{
Context subC = (Context) subs.get(i);
if( subC.context_ends < offset )
subtractOff += subC.context_ends - subC.context_directive_end;
else
break;
}
}
return offset - c.context_directive_end - subtractOff;
}
/**
* @param offset
* @return
*/
protected Context findContextForOffset(int offset) {
return findContextForOffset(tu, offset);
}
protected static Context findContextForOffset(CompositeContext context,
int offset) {
if (!context.hasSubContexts() )
{
if( context.context_ends >= offset)
return context;
return null;
}
List subContexts = context.getSubContexts();
//check first
Context bestContext = (Context) subContexts.get(0);
if (bestContext.context_directive_end > offset)
return context;
for (int i = 1; i < subContexts.size(); ++i) {
Context nextContext = (Context) subContexts.get(i);
if (nextContext.context_directive_end < offset)
bestContext = nextContext;
else
break;
}
if ( bestContext.context_ends < offset )
return context;
if (bestContext instanceof CompositeContext)
return findContextForOffset((CompositeContext) bestContext, offset);
return bestContext;
} }
/* /*
@ -169,7 +339,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#startTranslationUnit() * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#startTranslationUnit()
*/ */
public void startTranslationUnit(char[] filename) { public void startTranslationUnit(char[] filename) {
this.tu_filename = filename; tu = new TranslationUnit(filename);
currentContext = tu;
} }
/* /*
@ -178,7 +349,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#endTranslationUnit(int) * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#endTranslationUnit(int)
*/ */
public void endTranslationUnit(int offset) { public void endTranslationUnit(int offset) {
this.finalOffset = offset; tu.context_ends = offset;
} }
/* /*
@ -187,8 +358,11 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#startInclusion(char[], * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#startInclusion(char[],
* int) * int)
*/ */
public void startInclusion(char[] includePath, int offset) { public void startInclusion(char[] includePath, int offset, int endOffset) {
inclusions.add( new String( includePath )); Inclusion i = new Inclusion(currentContext, includePath, offset,
endOffset);
currentContext.addSubContext(i);
currentContext = i;
} }
/* /*
@ -197,9 +371,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#endInclusion(char[], * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#endInclusion(char[],
* int) * int)
*/ */
public void endInclusion(char[] includePath, int offset) { public void endInclusion(int offset) {
// TODO Auto-generated method stub ((Inclusion) currentContext).context_ends = offset;
currentContext = currentContext.getParent();
} }
/* /*
@ -366,20 +540,9 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.ILocationResolver#getTranslationUnitPath() * @see org.eclipse.cdt.internal.core.parser.scanner2.ILocationResolver#getTranslationUnitPath()
*/ */
public String getTranslationUnitPath() { public String getTranslationUnitPath() {
if( tu_filename == EMPTY_CHAR_ARRAY ) return ""; //$NON-NLS-1$ return new String(tu.path);
return new String( tu_filename );
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.ILocationResolver#getInclusionsPaths()
*/
public String[] getInclusionsPaths() {
if (inclusions == Collections.EMPTY_LIST)
return EMPTY_STRING_ARRAY;
return (String[]) inclusions.toArray(new String[inclusions.size()]); }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
@ -395,9 +558,15 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.ILocationResolver#getScannerProblems() * @see org.eclipse.cdt.internal.core.parser.scanner2.ILocationResolver#getScannerProblems()
*/ */
public IASTProblem[] getScannerProblems() { public IASTProblem[] getScannerProblems() {
if (problems == Collections.EMPTY_LIST) List contexts = new ArrayList(8);
LocationMap.collectContexts(V_PROBLEMS, tu, contexts);
if (contexts.isEmpty())
return EMPTY_PROBLEMS_ARRAY; return EMPTY_PROBLEMS_ARRAY;
return (IASTProblem[]) problems.toArray(new IASTProblem[problems.size()]); IASTProblem[] result = new IASTProblem[contexts.size()];
for (int i = 0; i < contexts.size(); ++i)
result[i] = ((Problem) contexts.get(i)).problem;
return result;
} }
/* /*
@ -406,9 +575,35 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#encounterIProblem(org.eclipse.cdt.core.parser.IProblem) * @see org.eclipse.cdt.internal.core.parser.scanner2.IScannerPreprocessorLog#encounterIProblem(org.eclipse.cdt.core.parser.IProblem)
*/ */
public void encounterProblem(IASTProblem problem) { public void encounterProblem(IASTProblem problem) {
if (problems == Collections.EMPTY_LIST) ScannerASTProblem p = (ScannerASTProblem) problem;
problems = new ArrayList(4); Problem pr = new Problem(currentContext, p.getOffset(), p.getOffset()
problems.add(problem); + p.getLength(), problem);
pr.context_ends = p.getOffset() + p.getLength();
}
protected static final int V_ALL = 1;
protected static final int V_INCLUSIONS = 2;
protected static final int V_PROBLEMS = 3;
protected static void collectContexts(int key, Context source, List result) {
switch (key) {
case V_ALL:
result.add(source);
break;
case V_INCLUSIONS:
if (source instanceof Inclusion)
result.add(source);
break;
case V_PROBLEMS:
if (source instanceof Problem)
result.add(source);
break;
}
if (source instanceof CompositeContext) {
List l = ((CompositeContext) source).getSubContexts();
for (int i = 0; i < l.size(); ++i)
collectContexts(key, (Context) l.get(i), result);
}
} }
} }

View file

@ -34,222 +34,224 @@ import org.eclipse.cdt.internal.core.parser.token.SimpleToken;
*/ */
public class Scanner2 extends BaseScanner { public class Scanner2 extends BaseScanner {
/** /**
* @param reader * @param reader
* @param info * @param info
* @param requestor * @param requestor
* @param parserMode * @param parserMode
* @param language * @param language
* @param log * @param log
* @param workingCopies * @param workingCopies
* @param configuration * @param configuration
*/ */
public Scanner2(CodeReader reader, IScannerInfo info, public Scanner2(CodeReader reader, IScannerInfo info,
ISourceElementRequestor requestor, ParserMode parserMode, ISourceElementRequestor requestor, ParserMode parserMode,
ParserLanguage language, IParserLogService log, List workingCopies, ParserLanguage language, IParserLogService log, List workingCopies,
IScannerExtensionConfiguration configuration) { IScannerExtensionConfiguration configuration) {
super(reader, info, parserMode, language, log, configuration); super(reader, info, parserMode, language, log, configuration);
this.requestor = requestor; this.requestor = requestor;
this.callbackManager = new ScannerCallbackManager(requestor); this.callbackManager = new ScannerCallbackManager(requestor);
this.expressionEvaluator = new ExpressionEvaluator(callbackManager, spf); this.expressionEvaluator = new ExpressionEvaluator(callbackManager, spf);
this.workingCopies = workingCopies; this.workingCopies = workingCopies;
postConstructorSetup(reader, info); postConstructorSetup(reader, info);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner.IScannerData#getASTFactory() * @see org.eclipse.cdt.internal.core.parser.scanner.IScannerData#getASTFactory()
*/ */
protected IASTFactory getASTFactory() { protected IASTFactory getASTFactory() {
if (astFactory == null) if (astFactory == null)
astFactory = ParserFactory.createASTFactory(parserMode, language); astFactory = ParserFactory.createASTFactory(parserMode, language);
return astFactory; return astFactory;
} }
protected IASTFactory astFactory; protected IASTFactory astFactory;
// callbacks // callbacks
protected ScannerCallbackManager callbackManager; protected ScannerCallbackManager callbackManager;
protected ISourceElementRequestor requestor; protected ISourceElementRequestor requestor;
protected List workingCopies; protected List workingCopies;
public final void setASTFactory(IASTFactory f) { public final void setASTFactory(IASTFactory f) {
astFactory = f; astFactory = f;
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createInclusionConstruct(char[], * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createInclusionConstruct(char[],
* char[], boolean, int, int, int, int, int, int, int, boolean) * char[], boolean, int, int, int, int, int, int, int, boolean)
*/ */
protected Object createInclusionConstruct(char[] fileName, protected Object createInclusionConstruct(char[] fileName,
char[] filenamePath, boolean local, int startOffset, char[] filenamePath, boolean local, int startOffset,
int startingLineNumber, int nameOffset, int nameEndOffset, int startingLineNumber, int nameOffset, int nameEndOffset,
int nameLine, int endOffset, int endLine, boolean isForced) { int nameLine, int endOffset, int endLine, boolean isForced) {
return getASTFactory().createInclusion(fileName, filenamePath, local, return getASTFactory().createInclusion(fileName, filenamePath, local,
startOffset, startingLineNumber, nameOffset, nameEndOffset, startOffset, startingLineNumber, nameOffset, nameEndOffset,
nameLine, endOffset, endLine, getCurrentFilename(), isForced); nameLine, endOffset, endLine, getCurrentFilename(), isForced);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#processMacro(char[], * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#processMacro(char[],
* int, int, int, int, int, int, int) * int, int, int, int, int, int, int)
*/ */
protected void processMacro(char[] name, int startingOffset, protected void processMacro(char[] name, int startingOffset,
int startingLineNumber, int idstart, int idend, int nameLine, int startingLineNumber, int idstart, int idend, int nameLine,
int textEnd, int endingLine, int textEnd, int endingLine, org.eclipse.cdt.core.parser.IMacro macro) {
org.eclipse.cdt.core.parser.IMacro macro) { callbackManager.pushCallback(getASTFactory().createMacro(name,
callbackManager.pushCallback(getASTFactory().createMacro(name, startingOffset, startingLineNumber, idstart, idend, nameLine,
startingOffset, startingLineNumber, idstart, idend, nameLine, textEnd, endingLine, getCurrentFilename(), !isInitialized));
textEnd, endingLine, getCurrentFilename(), !isInitialized)); }
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#pushInclusion(java.lang.Object) * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#pushContext(char[],
*/ * java.lang.Object)
protected void pushInclusion(Object data) { */
callbackManager.pushCallback(data); protected void pushContext(char[] buffer, Object data) {
super.pushInclusion(data); super.pushContext(buffer, data);
} if (data instanceof InclusionData) {
callbackManager.pushCallback(data);
if (log.isTracing()) {
StringBuffer b = new StringBuffer("Entering inclusion "); //$NON-NLS-1$
b.append(((InclusionData) data).reader.filename);
log.traceLog(b.toString());
}
}
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#popInclusion() * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#beforeSecondFetchToken()
*/ */
protected void popInclusion(java.lang.Object data) { protected void beforeSecondFetchToken() {
super.popInclusion(data); if (callbackManager.hasCallbacks())
callbackManager callbackManager.popCallbacks();
.pushCallback(((InclusionData) bufferData[bufferStackPos]).inclusion); }
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#beforeSecondFetchToken() * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#quickParsePushPopInclusion(java.lang.Object)
*/ */
protected void beforeSecondFetchToken() { protected void quickParsePushPopInclusion(Object inclusion) {
if (callbackManager.hasCallbacks()) callbackManager.pushCallback(new InclusionData(null, inclusion));
callbackManager.popCallbacks(); callbackManager.pushCallback(inclusion);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#pushProblem(org.eclipse.cdt.core.parser.IProblem) * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createReaderDuple(java.lang.String)
*/ */
protected void pushProblem(IProblem p) { protected CodeReader createReaderDuple(String finalPath) {
callbackManager.pushCallback(p); return ScannerUtility.createReaderDuple(finalPath, requestor,
} getWorkingCopies());
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#quickParsePushPopInclusion(java.lang.Object) * @see org.eclipse.cdt.core.parser.IScanner#getLocationResolver()
*/ */
protected void quickParsePushPopInclusion(Object inclusion) { public ILocationResolver getLocationResolver() {
callbackManager.pushCallback(new InclusionData(null, inclusion)); return null;
callbackManager.pushCallback(inclusion); }
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#createReaderDuple(java.lang.String) * @see org.eclipse.cdt.internal.core.parser.scanner.IScannerData#getWorkingCopies()
*/ */
protected CodeReader createReaderDuple(String finalPath) { protected Iterator getWorkingCopies() {
return ScannerUtility.createReaderDuple(finalPath, requestor, if (workingCopies == null)
getWorkingCopies()); return EmptyIterator.EMPTY_ITERATOR;
} return workingCopies.iterator();
}
/* /**
* (non-Javadoc) * @return
* */
* @see org.eclipse.cdt.core.parser.IScanner#getLocationResolver() protected IToken newToken(int signal) {
*/ if (bufferData[bufferStackPos] instanceof MacroData) {
public ILocationResolver getLocationResolver() { int mostRelevant;
return null; for (mostRelevant = bufferStackPos; mostRelevant >= 0; --mostRelevant)
} 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 SimpleToken(signal, bufferPos[bufferStackPos] + 1,
getCurrentFilename(), getLineNumber(bufferPos[bufferStackPos] + 1));
}
/* protected IToken newToken(int signal, char[] buffer) {
* (non-Javadoc) if (bufferData[bufferStackPos] instanceof MacroData) {
* int mostRelevant;
* @see org.eclipse.cdt.internal.core.parser.scanner.IScannerData#getWorkingCopies() for (mostRelevant = bufferStackPos; mostRelevant >= 0; --mostRelevant)
*/ if (bufferData[mostRelevant] instanceof InclusionData
protected Iterator getWorkingCopies() { || bufferData[mostRelevant] instanceof CodeReader)
if (workingCopies == null) break;
return EmptyIterator.EMPTY_ITERATOR; MacroData data = (MacroData) bufferData[mostRelevant + 1];
return workingCopies.iterator(); return new ImagedExpansionToken(signal, buffer, 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));
if (buffer != null && buffer.length == 0 && signal != IToken.tSTRING
&& signal != IToken.tLSTRING)
bufferPos[bufferStackPos] += 1; // TODO - remove this hack at some
// point
/** return i;
* @return }
*/
protected IToken newToken(int signal) {
if (bufferData[bufferStackPos] instanceof MacroData) {
int mostRelevant;
for (mostRelevant = bufferStackPos; mostRelevant >= 0; --mostRelevant)
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 SimpleToken(signal, bufferPos[bufferStackPos] + 1,
getCurrentFilename(),
getLineNumber(bufferPos[bufferStackPos] + 1));
}
protected IToken newToken(int signal, char[] buffer) { protected static final ScannerProblemFactory spf = new ScannerProblemFactory();
if (bufferData[bufferStackPos] instanceof MacroData) {
int mostRelevant;
for (mostRelevant = bufferStackPos; mostRelevant >= 0; --mostRelevant)
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));
}
IToken i = new ImagedToken(signal, buffer,
bufferPos[bufferStackPos] + 1, getCurrentFilename(),
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
return i; /*
} * (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#handleProblem(int,
* int, char[])
*/
protected void handleProblem(int id, int offset, char[] arg) {
if (parserMode == ParserMode.COMPLETION_PARSE)
return;
IProblem p = spf.createProblem(id, offset, bufferPos[bufferStackPos],
getLineNumber(bufferPos[bufferStackPos]), getCurrentFilename(),
arg != null ? arg : EMPTY_CHAR_ARRAY, false, true);
callbackManager.pushCallback(p);
}
protected static final ScannerProblemFactory spf = new ScannerProblemFactory(); /*
/* * (non-Javadoc)
* (non-Javadoc) *
* * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#popContext()
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#handleProblem(int, */
* int, char[]) protected Object popContext() {
*/ if (bufferData[bufferStackPos] instanceof InclusionData) {
protected void handleProblem(int id, int offset, char[] arg) { if (log.isTracing()) {
if (parserMode == ParserMode.COMPLETION_PARSE) StringBuffer buffer = new StringBuffer("Exiting inclusion "); //$NON-NLS-1$
return; buffer
IProblem p = spf.createProblem(id, offset, .append(((InclusionData) bufferData[bufferStackPos]).reader.filename);
bufferPos[bufferStackPos], log.traceLog(buffer.toString());
getLineNumber(bufferPos[bufferStackPos]), getCurrentFilename(), }
arg != null ? arg : EMPTY_CHAR_ARRAY, false, true); callbackManager
pushProblem(p); .pushCallback(((InclusionData) bufferData[bufferStackPos]).inclusion);
} }
return super.popContext();
}
} }