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

Fixed 90851 - [DOM AST] NPE in LocationMap.defineObjectStyleMacro

This commit is contained in:
John Camelon 2005-04-10 21:54:27 +00:00
parent c35557f172
commit 26907961a3
3 changed files with 279 additions and 250 deletions

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.core.parser.tests.ast2; package org.eclipse.cdt.core.parser.tests.ast2;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
@ -21,16 +22,14 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorEndifStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfdefStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
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.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ExtendedScannerInfo;
import org.eclipse.cdt.core.parser.IExtendedScannerInfo;
import org.eclipse.cdt.core.parser.IParserLogService; import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner; import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo; import org.eclipse.cdt.core.parser.IScannerInfo;
@ -62,10 +61,10 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
private static final IParserLogService NULL_LOG = new NullLogService(); private static final IParserLogService NULL_LOG = new NullLogService();
private static final ICodeReaderFactory factory = CDOM private static final ICodeReaderFactory factory = CDOM.getInstance()
.getInstance() .getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES);
.getCodeReaderFactory(
CDOM.PARSE_SAVED_RESOURCES); private static final String[] EMPTY_STRING_ARRAY = new String[0];
/** /**
* @param name * @param name
@ -77,10 +76,15 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
protected IASTTranslationUnit parse(IFile code, ParserLanguage language) protected IASTTranslationUnit parse(IFile code, ParserLanguage language)
throws Exception { throws Exception {
return parse(code, language, SCANNER_INFO);
}
protected IASTTranslationUnit parse(IFile code, ParserLanguage language,
IScannerInfo s) throws Exception {
InputStream stream = code.getContents(); InputStream stream = code.getContents();
IScanner scanner = new DOMScanner(new CodeReader(code.getLocation() IScanner scanner = new DOMScanner(new CodeReader(code.getLocation()
.toOSString(), stream), SCANNER_INFO, ParserMode.COMPLETE_PARSE, .toOSString(), stream), s, ParserMode.COMPLETE_PARSE, language,
language, NULL_LOG, getScannerConfig(language), factory); 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, parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
@ -313,6 +317,19 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
} }
} }
public void testBug90851() throws Exception {
IFile imacro_file = importFile( "macro.h", "#define BEAST 666\n"); //$NON-NLS-1$ //$NON-NLS-2$
IFile include_file = importFile( "include.h", "int value = BEAST;\n"); //$NON-NLS-1$ //$NON-NLS-2$
String [] macros = { imacro_file.getLocation().toOSString() };
String [] includes = { include_file.getLocation().toOSString() };
IExtendedScannerInfo scannerInfo = new ExtendedScannerInfo( Collections.EMPTY_MAP, EMPTY_STRING_ARRAY, macros, includes );
IFile code = importFile( "main.c", "int main() { return value; } "); //$NON-NLS-1$ //$NON-NLS-2$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
parse(code, p, scannerInfo ); //$NON-NLS-1$
}
}
public static Test suite() { public static Test suite() {
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class); TestSuite suite = new TestSuite(DOMLocationInclusionTests.class);
suite.addTest(new DOMLocationInclusionTests("cleanupProject")); //$NON-NLS-1$ suite.addTest(new DOMLocationInclusionTests("cleanupProject")); //$NON-NLS-1$

View file

@ -1617,6 +1617,7 @@ abstract class BaseScanner implements IScanner {
* *
*/ */
protected void errorHandle() { protected void errorHandle() {
if( bufferStackPos > 0 )
++bufferPos[bufferStackPos]; ++bufferPos[bufferStackPos];
} }

View file

@ -49,6 +49,12 @@ import org.eclipse.cdt.internal.core.dom.parser.ASTPreprocessorSelectionResult;
*/ */
public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
public LocationMap() {
tu = new _TranslationUnit();
currentContext = tu;
}
public class MacroExpansionLocation implements IASTMacroExpansion { public class MacroExpansionLocation implements IASTMacroExpansion {
public MacroExpansionLocation( public MacroExpansionLocation(
@ -1087,7 +1093,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
} }
protected static class _CompositeFileContext extends _CompositeContext { protected static class _CompositeFileContext extends _CompositeContext {
public final CodeReader reader; public CodeReader reader;
public _CompositeFileContext(_CompositeContext parent, int startOffset, public _CompositeFileContext(_CompositeContext parent, int startOffset,
int endOffset, CodeReader reader) { int endOffset, CodeReader reader) {
@ -1095,6 +1101,12 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
this.reader = reader; this.reader = reader;
} }
public _CompositeFileContext(_CompositeContext parent, int startOffset,
int endOffset ) {
super(parent, startOffset, endOffset);
}
public int getLineNumber(int nodeOffset) { public int getLineNumber(int nodeOffset) {
if( nodeOffset >= reader.buffer.length ) if( nodeOffset >= reader.buffer.length )
return 1; return 1;
@ -1124,8 +1136,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
* @param startOffset * @param startOffset
* @param endOffset * @param endOffset
*/ */
public _TranslationUnit(CodeReader reader) { public _TranslationUnit() {
super(null, 0, 0, reader); super(null, 0, 0 );
} }
IMacroDefinition [] builtins = new IMacroDefinition[2]; IMacroDefinition [] builtins = new IMacroDefinition[2];
@ -1773,8 +1785,7 @@ 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(CodeReader tu_reader) { public void startTranslationUnit(CodeReader tu_reader) {
tu = new _TranslationUnit(tu_reader); tu.reader = tu_reader;
currentContext = tu;
} }
/* /*