1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-11 10:15:39 +02:00

Fixed Bug 84451 - [Offsets] simple multiple #include throws offsets off

Fixed Bug 84356 - [DOM] stdio.h - Location/offsets degrade as parse continues on
This commit is contained in:
John Camelon 2005-02-08 21:42:45 +00:00
parent cd55cc5761
commit 69d98316f4
4 changed files with 501 additions and 417 deletions

View file

@ -58,8 +58,10 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
private static final IParserLogService NULL_LOG = new NullLogService();
private static final ICodeReaderFactory factory = CDOM.getInstance()
.getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES);
private static final ICodeReaderFactory factory = CDOM
.getInstance()
.getCodeReaderFactory(
CDOM.PARSE_SAVED_RESOURCES);
/**
* @param name
@ -73,9 +75,8 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
throws Exception {
InputStream stream = code.getContents();
IScanner scanner = new DOMScanner(new CodeReader(code.getLocation()
.toOSString(), stream), SCANNER_INFO,
ParserMode.COMPLETE_PARSE, language, NULL_LOG,
getScannerConfig(language), factory);
.toOSString(), stream), SCANNER_INFO, ParserMode.COMPLETE_PARSE,
language, NULL_LOG, getScannerConfig(language), factory);
ISourceCodeParser parser = null;
if (language == ParserLanguage.CPP) {
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
@ -199,46 +200,108 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
: null) {
IASTTranslationUnit tu = parse(c_file, p); //$NON-NLS-1$
assertEquals(tu.getDeclarations().length, 0);
IASTPreprocessorMacroDefinition[] macroDefinitions = tu.getMacroDefinitions();
IASTPreprocessorMacroDefinition[] macroDefinitions = tu
.getMacroDefinitions();
assertNotNull(macroDefinitions);
assertEquals(macroDefinitions.length, 4);
assertSoleFileLocation( macroDefinitions[0], "blarg.c", c_file_code.indexOf( "#define"), c_file_code.indexOf( "4" ) + 1- c_file_code.indexOf( "#define" ) ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation( macroDefinitions[0].getName(), "blarg.c", c_file_code.indexOf( "X"), 1 ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation( macroDefinitions[1], "blarg.h", h_file_code.indexOf( "#define _BLARG_H_"), "#define _BLARG_H_\r".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation( macroDefinitions[1].getName(), "blarg.h", h_file_code.indexOf( "e _BLARG_H_") + 2, "_BLARG_H_".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation( macroDefinitions[2], "blarg.h", h_file_code.indexOf( "#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation( macroDefinitions[2].getName(), "blarg.h", h_file_code.indexOf( "PRINT"), "PRINT".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation( macroDefinitions[3], "blarg.c", c_file_code.indexOf( "#define POST_INCLUDE"), "#define POST_INCLUDE".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation( macroDefinitions[3].getName(), "blarg.c", c_file_code.indexOf( "POST_INCLUDE"), "POST_INCLUDE".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[0],
"blarg.c", c_file_code.indexOf("#define"), c_file_code.indexOf("4") + 1 - c_file_code.indexOf("#define")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[0].getName(),
"blarg.c", c_file_code.indexOf("X"), 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[1],
"blarg.h", h_file_code.indexOf("#define _BLARG_H_"), "#define _BLARG_H_\r".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[1].getName(),
"blarg.h", h_file_code.indexOf("e _BLARG_H_") + 2, "_BLARG_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[2],
"blarg.h", h_file_code.indexOf("#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[2].getName(),
"blarg.h", h_file_code.indexOf("PRINT"), "PRINT".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[3],
"blarg.c", c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[3].getName(),
"blarg.c", c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
// public void testMacrosInIncludeFile2() throws Exception {
// String c_file_code = "#define X 4\n\n#include \"blarg.h\"\n\n#define POST_INCLUDE\n#include \"second.h\"\n#define POST_SECOND\n"; //$NON-NLS-1$
// String h_file_code = "#ifndef _BLARG_H_\r\n#define _BLARG_H_\r\n// macro\r\n#define PRINT(s,m) printf(s,m)\r\n#endif //_BLARG_H_\r\n"; //$NON-NLS-1$
// String h_file2_code = "#ifndef _SECOND_H_ \n#define _SECOND_H_\n#endif\n"; //$NON-NLS-1$
// importFile( "blarg.h", h_file_code ); //$NON-NLS-1$
// importFile( "second.h", h_file2_code ); //$NON-NLS-1$
// IFile c_file = importFile( "blarg.c", c_file_code ); //$NON-NLS-1$
// for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
// : null) {
// IASTTranslationUnit tu = parse(c_file, p); //$NON-NLS-1$
// assertEquals( tu.getDeclarations().length, 0 );
// IASTPreprocessorMacroDefinition[] macroDefinitions = tu.getMacroDefinitions();
// assertNotNull( macroDefinitions );
// assertEquals( macroDefinitions.length, 6 );
// assertSoleFileLocation( macroDefinitions[0], "blarg.c", c_file_code.indexOf( "#define"), c_file_code.indexOf( "4" ) + 1- c_file_code.indexOf( "#define" ) ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
// assertSoleFileLocation( macroDefinitions[0].getName(), "blarg.c", c_file_code.indexOf( "X"), 1 ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
// assertSoleFileLocation( macroDefinitions[1], "blarg.h", h_file_code.indexOf( "#define _BLARG_H_"), "#define _BLARG_H_\r".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// assertSoleFileLocation( macroDefinitions[1].getName(), "blarg.h", h_file_code.indexOf( "e _BLARG_H_") + 2, "_BLARG_H_".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// assertSoleFileLocation( macroDefinitions[2], "blarg.h", h_file_code.indexOf( "#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
// assertSoleFileLocation( macroDefinitions[2].getName(), "blarg.h", h_file_code.indexOf( "PRINT"), "PRINT".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
// assertSoleFileLocation( macroDefinitions[3], "blarg.c", c_file_code.indexOf( "#define POST_INCLUDE"), "#define POST_INCLUDE".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// assertSoleFileLocation( macroDefinitions[3].getName(), "blarg.c", c_file_code.indexOf( "POST_INCLUDE"), "POST_INCLUDE".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// assertSoleFileLocation( macroDefinitions[4], "second.h", h_file2_code.indexOf( "#define _SECOND_H_"), "#define _SECOND_H_".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// assertSoleFileLocation( macroDefinitions[5], "blarg.c", c_file_code.indexOf( "#define POST_SECOND"), "#define POST_SECOND".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
// }
// }
public void testBug84451() throws Exception {
String header1_code = "int x;\n"; //$NON-NLS-1$
String header2_code = "int y;\n"; //$NON-NLS-1$
String cpp_code = "#include \"header1.h\"\n#include \"header2.h\"\nint z;\n"; //$NON-NLS-1$
importFile("header1.h", header1_code); //$NON-NLS-1$
importFile("header2.h", header2_code); //$NON-NLS-1$
IFile f = importFile("source.c", cpp_code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(f, p);
IASTDeclaration[] declarations = tu.getDeclarations();
IASTPreprocessorIncludeStatement[] includeDirectives = tu
.getIncludeDirectives();
assertSoleFileLocation(
includeDirectives[0],
"source.c", cpp_code.indexOf("#include \"header1.h\""), "#include \"header1.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(declarations[0],
"header1.h", 0, "int x;".length()); //$NON-NLS-1$ //$NON-NLS-2$
assertSoleFileLocation(declarations[1],
"header2.h", 0, "int y;".length()); //$NON-NLS-1$ //$NON-NLS-2$
assertSoleFileLocation(
includeDirectives[1],
"source.c", cpp_code.indexOf("#include \"header2.h\""), "#include \"header2.h\"".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(declarations[2],
"source.c", cpp_code.indexOf("int z;"), "int z;".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}
public void testMacrosInIncludeFile2() throws Exception {
String c_file_code = "#define X 4\n\n#include \"blarg.h\"\n\n#define POST_INCLUDE\n#include \"second.h\"\n#define POST_SECOND\n"; //$NON-NLS-1$
String h_file_code = "#ifndef _BLARG_H_\r\n#define _BLARG_H_\r\n//macro\r\n#define PRINT(s,m) printf(s,m)\r\n#endif //_BLARG_H_\r\n"; //$NON-NLS-1$
String h_file2_code = "#ifndef _SECOND_H_ \n#define _SECOND_H_\n#endif\n"; //$NON-NLS-1$
importFile("blarg.h", h_file_code); //$NON-NLS-1$
importFile("second.h", h_file2_code); //$NON-NLS-1$
IFile c_file = importFile("blarg.c", c_file_code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(c_file, p); //$NON-NLS-1$
assertEquals(tu.getDeclarations().length, 0);
IASTPreprocessorMacroDefinition[] macroDefinitions = tu
.getMacroDefinitions();
assertNotNull(macroDefinitions);
assertEquals(macroDefinitions.length, 6);
assertSoleFileLocation(
macroDefinitions[0],
"blarg.c", c_file_code.indexOf("#define"), c_file_code.indexOf("4") + 1 - c_file_code.indexOf("#define")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[0].getName(),
"blarg.c", c_file_code.indexOf("X"), 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[1],
"blarg.h", h_file_code.indexOf("#define _BLARG_H_"), "#define _BLARG_H_\r".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[1].getName(),
"blarg.h", h_file_code.indexOf("e _BLARG_H_") + 2, "_BLARG_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[2],
"blarg.h", h_file_code.indexOf("#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(macroDefinitions[2].getName(),
"blarg.h", h_file_code.indexOf("PRINT"), "PRINT".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
macroDefinitions[3],
"blarg.c", c_file_code.indexOf("#define POST_INCLUDE"), "#define POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[3].getName(),
"blarg.c", c_file_code.indexOf("POST_INCLUDE"), "POST_INCLUDE".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[4],
"second.h", h_file2_code.indexOf("#define _SECOND_H_"), "#define _SECOND_H_".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(
macroDefinitions[5],
"blarg.c", c_file_code.indexOf("#define POST_SECOND"), "#define POST_SECOND".length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
public static Test suite() {
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class);

View file

@ -1247,6 +1247,7 @@ abstract class BaseScanner implements IScanner {
System.arraycopy(oldLineOffsets, 0, lineOffsets, 0,
oldLineOffsets.length);
}
bufferStack[bufferStackPos] = buffer;
@ -1283,6 +1284,8 @@ abstract class BaseScanner implements IScanner {
}
protected Object popContext() {
//NOTE - do not set counters to 0 or -1 or something
//Subclasses may require those values in their popContext()
bufferStack[bufferStackPos] = null;
Object result = bufferData[bufferStackPos];
@ -1293,7 +1296,6 @@ abstract class BaseScanner implements IScanner {
pushForcedInclusion();
return result;
}
/**
*
*/
@ -2545,7 +2547,9 @@ abstract class BaseScanner implements IScanner {
* @param endPos
*/
protected abstract void processError(int startPos, int endPos);
protected abstract void processElsif(int startPos, int endPos, boolean taken);
protected abstract void processElse(int startPos, int endPos, boolean taken);
/**
@ -2555,7 +2559,8 @@ abstract class BaseScanner implements IScanner {
*/
protected abstract void processIf(int startPos, int endPos, boolean taken);
protected void handlePPInclude(int pos2, boolean include_next, int startingLineNumber) {
protected void handlePPInclude(int pos2, boolean include_next,
int startingLineNumber) {
char[] buffer = bufferStack[bufferStackPos];
int limit = bufferLimit[bufferStackPos];
@ -2688,12 +2693,12 @@ abstract class BaseScanner implements IScanner {
CodeReader reader = null;
File currentDirectory = null;
if (local || include_next) {
// if the include is eclosed in quotes OR we are in an include_next then we need to know what the current directory is!
// if the include is eclosed in quotes OR we are in an include_next
// then we need to know what the current directory is!
File file = new File(String.valueOf(getCurrentFilename()));
currentDirectory = file.getParentFile();
}
if (local && !include_next) {
// Check to see if we find a match in the current directory
if (currentDirectory != null) {
@ -2701,18 +2706,17 @@ abstract class BaseScanner implements IScanner {
reader = createReader(absolutePath, filename);
if (reader != null) {
pushContext(reader.buffer, new InclusionData(reader,
createInclusionConstruct(fileNameArray,
reader.filename, local, startOffset,
startingLineNumber, nameOffset,
nameEndOffset, nameLine, endOffset,
endLine, false)));
createInclusionConstruct(fileNameArray, reader.filename,
local, startOffset, startingLineNumber, nameOffset,
nameEndOffset, nameLine, endOffset, endLine, false)));
return;
}
}
}
// if we're not include_next, then we are looking for the
// first occurance of the file, otherwise, we ignore all the paths before the
// first occurance of the file, otherwise, we ignore all the paths before
// the
// current directory
if (includePaths != null) {
int startpos = 0;
@ -2722,28 +2726,27 @@ abstract class BaseScanner implements IScanner {
reader = createReader(includePaths[i], filename);
if (reader != null) {
pushContext(reader.buffer, new InclusionData(reader,
createInclusionConstruct(fileNameArray,
reader.filename, local, startOffset,
startingLineNumber, nameOffset,
nameEndOffset, nameLine, endOffset,
endLine, false)));
createInclusionConstruct(fileNameArray, reader.filename,
local, startOffset, startingLineNumber, nameOffset,
nameEndOffset, nameLine, endOffset, endLine, false)));
return;
}
}
}
handleProblem(IProblem.PREPROCESSOR_INCLUSION_NOT_FOUND, startOffset, fileNameArray);
handleProblem(IProblem.PREPROCESSOR_INCLUSION_NOT_FOUND, startOffset,
fileNameArray);
}
private CodeReader createReader(String path, String fileName)
{
protected CodeReader createReader(String path, String fileName) {
String finalPath = ScannerUtility.createReconciledPath(path, fileName);
char[] finalPathc = finalPath.toCharArray();
CodeReader reader = (CodeReader) fileCache.get(finalPathc);
if (reader != null)
return reader; // found the file in the cache
// create a new reader on this file (if the file does not exist we will get null)
// create a new reader on this file (if the file does not exist we will
// get null)
reader = createReaderDuple(finalPath);
if (reader == null)
return null; // the file was not found
@ -2755,8 +2758,7 @@ abstract class BaseScanner implements IScanner {
return reader;
}
private int findIncludePos( String[] paths, File currentDirectory)
{
private int findIncludePos(String[] paths, File currentDirectory) {
for (int i = 0; i < paths.length; ++i)
try {
String path = new File(paths[i]).getCanonicalPath();
@ -2768,6 +2770,7 @@ abstract class BaseScanner implements IScanner {
return -1;
}
/**
* @param finalPath
* @return
@ -3114,7 +3117,8 @@ abstract class BaseScanner implements IScanner {
*/
protected abstract void processUndef(int pos, int endPos);
protected void handlePPIfdef(int pos, boolean positive) throws EndOfFileException {
protected void handlePPIfdef(int pos, boolean positive)
throws EndOfFileException {
char[] buffer = bufferStack[bufferStackPos];
int limit = bufferLimit[bufferStackPos];
@ -3172,7 +3176,8 @@ abstract class BaseScanner implements IScanner {
handleInvalidCompletion();
}
protected abstract void processIfdef(int startPos, int endPos, boolean positive, boolean taken);
protected abstract void processIfdef(int startPos, int endPos,
boolean positive, boolean taken);
// checkelse - if potential for more, otherwise skip to endif
protected void skipOverConditionalCode(boolean checkelse) {
@ -3219,9 +3224,11 @@ abstract class BaseScanner implements IScanner {
branchState(BRANCH_IF);
skipToNewLine();
if (type == ppIfdef)
processIfdef( startPos, bufferPos[bufferStackPos], true, false );
processIfdef(startPos, bufferPos[bufferStackPos],
true, false);
else if (type == ppIfndef)
processIfdef( startPos, bufferPos[bufferStackPos], false, false );
processIfdef(startPos, bufferPos[bufferStackPos],
false, false);
else
processIf(startPos, bufferPos[bufferStackPos], false);
break;
@ -3229,10 +3236,12 @@ abstract class BaseScanner implements IScanner {
if (branchState(BRANCH_ELSE)) {
skipToNewLine();
if (checkelse && nesting == 0) {
processElse( startPos, bufferPos[ bufferStackPos], true );
processElse(startPos, bufferPos[bufferStackPos],
true);
return;
}
processElse( startPos, bufferPos[ bufferStackPos], false );
processElse(startPos, bufferPos[bufferStackPos],
false);
} else {
//problem, ignore this one.
handleProblem(
@ -3251,18 +3260,18 @@ abstract class BaseScanner implements IScanner {
if (expressionEvaluator.evaluate(buffer, start,
len, definitions,
getLineNumber(bufferPos[bufferStackPos]),
getCurrentFilename()) != 0)
{
getCurrentFilename()) != 0) {
// condition passed, we're good
processElsif( start, bufferPos[bufferStackPos], true);
processElsif(start, bufferPos[bufferStackPos],
true);
return;
}
processElsif( start, bufferPos[bufferStackPos], false );
}
else
{
processElsif(start, bufferPos[bufferStackPos],
false);
} else {
skipToNewLine();
processElsif( start, bufferPos[bufferStackPos], false );
processElsif(start, bufferPos[bufferStackPos],
false);
}
} else {
//problem, ignore this one.
@ -4395,8 +4404,7 @@ abstract class BaseScanner implements IScanner {
return language;
}
protected CodeReader getMainReader()
{
protected CodeReader getMainReader() {
if (bufferData != null && bufferData[0] != null
&& bufferData[0] instanceof CodeReader)
return ((CodeReader) bufferData[0]);

View file

@ -13,7 +13,6 @@ package org.eclipse.cdt.internal.core.parser.scanner2;
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IMacro;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScannerInfo;
@ -31,9 +30,8 @@ import org.eclipse.cdt.internal.core.parser.token.SimpleToken;
*/
public class DOMScanner extends BaseScanner {
private final ICodeReaderFactory codeReaderFactory;
private int globalCounter = 0;
private int contextDelta = 0;
protected final ICodeReaderFactory codeReaderFactory;
protected int [] bufferDelta = new int[ bufferInitialSize ];
private static class DOMInclusion {
public final char[] pt;
@ -138,16 +136,25 @@ public class DOMScanner extends BaseScanner {
* java.lang.Object)
*/
protected void pushContext(char[] buffer, Object data) {
if( bufferStackPos + 1 == bufferDelta.length )
{
int size = bufferDelta.length * 2;
int[] oldBufferDelta = bufferDelta;
bufferDelta = new int[size];
System.arraycopy(oldBufferDelta, 0, bufferDelta, 0, oldBufferDelta.length);
}
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(((InclusionData)data).reader, inc.o, globalCounter);
contextDelta = 0;
locationMap.startInclusion(((InclusionData)data).reader, inc.o, resolveOffset( getCurrentOffset() ));
bufferDelta[bufferStackPos + 1 ] = 0;
}
super.pushContext(buffer, data);
}
@ -161,24 +168,38 @@ public class DOMScanner extends BaseScanner {
//TODO calibrate offsets
Object result = super.popContext();
if (result instanceof CodeReader) {
globalCounter += (((CodeReader) result).buffer.length - contextDelta);
locationMap.endTranslationUnit(globalCounter);
locationMap.endTranslationUnit( bufferDelta[0] + ((CodeReader)result).buffer.length );
}
if (result instanceof InclusionData) {
CodeReader codeReader = ((InclusionData) result).reader;
if (log.isTracing()) {
StringBuffer buffer = new StringBuffer("Exiting inclusion "); //$NON-NLS-1$
buffer
.append(((InclusionData) bufferData[bufferStackPos]).reader.filename);
buffer.append(codeReader.filename);
log.traceLog(buffer.toString());
}
globalCounter += (((InclusionData) result).reader.buffer.length - contextDelta);
contextDelta = getCurrentOffset();
locationMap.endInclusion(globalCounter);
locationMap.endInclusion(getGlobalCounter( bufferStackPos + 1 ) + bufferPos[ bufferStackPos + 1 ]);
bufferDelta[ bufferStackPos ] += bufferDelta[ bufferStackPos + 1 ] + codeReader.buffer.length;
}
return result;
}
protected int getGlobalCounter( int value )
{
if( value < 0 ) return 0;
int result = bufferDelta[ value ];
for( int i = value - 1; i >= 0; --i )
result += bufferPos[ i ] + bufferDelta[ i ];
return result;
}
protected int getGlobalCounter()
{
return getGlobalCounter( bufferStackPos );
}
/**
* @return
*/
@ -252,7 +273,7 @@ public class DOMScanner extends BaseScanner {
* @return
*/
private int resolveOffset(int offset) {
return globalCounter - contextDelta + offset;
return getGlobalCounter() + offset;
}
/*
@ -266,16 +287,6 @@ public class DOMScanner extends BaseScanner {
locationMap.startTranslationUnit(getMainReader());
}
/*
* (non-Javadoc)
*
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#throwEOF()
*/
protected void throwEOF() throws EndOfFileException {
locationMap.endTranslationUnit(globalCounter);
super.throwEOF();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#processIfdef(int, int, boolean, boolean)
*/

View file

@ -839,6 +839,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
if (tu == null)
return EMPTY_LOCATION_ARRAY;
_Context c = findContextForOffset(offset);
if( c == null )
return EMPTY_LOCATION_ARRAY;
if (c.context_ends >= offset + length)
return createSoleLocation(c, offset, length);