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

Patch for Devin Steffler.

Fixed Bug 86126   	[_Context] \r is included in context_directive_end
This commit is contained in:
John Camelon 2005-05-25 17:48:39 +00:00
parent fb70c25e85
commit 8206cea697
3 changed files with 150 additions and 5 deletions

View file

@ -10,20 +10,57 @@
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.tests.FileBasePluginTest;
import org.eclipse.cdt.internal.core.dom.parser.ISourceCodeParser;
import org.eclipse.cdt.internal.core.dom.parser.c.ANSICParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.cdt.internal.core.dom.parser.c.GCCParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.c.ICParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GPPParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.core.parser.scanner2.DOMScanner;
import org.eclipse.cdt.internal.core.parser.scanner2.FileCodeReaderFactory;
import org.eclipse.cdt.internal.core.parser.scanner2.GCCScannerExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser.scanner2.GPPScannerExtensionConfiguration;
import org.eclipse.cdt.internal.core.parser.scanner2.IScannerExtensionConfiguration;
import org.eclipse.core.resources.IFile;
/**
* @author dsteffle
*/
public class AST2SelectionParseBaseTest extends AST2BaseTest {
public class AST2SelectionParseBaseTest extends FileBasePluginTest {
private static final IParserLogService NULL_LOG = new NullLogService();
public AST2SelectionParseBaseTest(String name, Class className) {
super(name, className);
}
protected IASTNode parse(String code, ParserLanguage lang, int offset, int length) throws ParserException {
return parse(code, lang, false, false, offset, length);
}
protected IASTNode parse(IFile file, ParserLanguage lang, int offset, int length) throws ParserException {
IASTTranslationUnit tu = parse(file, lang, false, false);
return tu.selectNodeForLocation(tu.getFilePath(), offset, length);
}
protected IASTNode parse(String code, ParserLanguage lang, int offset, int length, boolean expectedToPass) throws ParserException {
return parse(code, lang, false, expectedToPass, offset, length);
}
@ -32,5 +69,95 @@ public class AST2SelectionParseBaseTest extends AST2BaseTest {
IASTTranslationUnit tu = parse(code, lang, useGNUExtensions, expectNoProblems);
return tu.selectNodeForLocation(tu.getFilePath(), offset, length);
}
/**
* @param string
* @param c
* @return
* @throws ParserException
*/
protected IASTTranslationUnit parse( String code, ParserLanguage lang, boolean useGNUExtensions, boolean expectNoProblems ) throws ParserException {
CodeReader codeReader = new CodeReader(code
.toCharArray());
ScannerInfo scannerInfo = new ScannerInfo();
IScannerExtensionConfiguration configuration = null;
if( lang == ParserLanguage.C )
configuration = new GCCScannerExtensionConfiguration();
else
configuration = new GPPScannerExtensionConfiguration();
IScanner scanner = new DOMScanner( codeReader, scannerInfo, ParserMode.COMPLETE_PARSE, lang, NULL_LOG, configuration, FileCodeReaderFactory.getInstance() );
ISourceCodeParser parser2 = null;
if( lang == ParserLanguage.CPP )
{
ICPPParserExtensionConfiguration config = null;
if (useGNUExtensions)
config = new GPPParserExtensionConfiguration();
else
config = new ANSICPPParserExtensionConfiguration();
parser2 = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG,
config );
}
else
{
ICParserExtensionConfiguration config = null;
if (useGNUExtensions)
config = new GCCParserExtensionConfiguration();
else
config = new ANSICParserExtensionConfiguration();
parser2 = new GNUCSourceParser( scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, config );
}
IASTTranslationUnit tu = parser2.parse();
if( parser2.encounteredError() && expectNoProblems )
throw new ParserException( "FAILURE"); //$NON-NLS-1$
if( lang == ParserLanguage.C && expectNoProblems )
{
assertEquals( CVisitor.getProblems(tu).length, 0 );
assertEquals( tu.getPreprocessorProblems().length, 0 );
}
else if ( lang == ParserLanguage.CPP && expectNoProblems )
{
assertEquals( CPPVisitor.getProblems(tu).length, 0 );
assertEquals( tu.getPreprocessorProblems().length, 0 );
}
if( expectNoProblems )
assertEquals( 0, tu.getPreprocessorProblems().length );
return tu;
}
protected IASTTranslationUnit parse( IFile file, ParserLanguage lang, boolean useGNUExtensions, boolean expectNoProblems ) throws ParserException {
IASTTranslationUnit tu=null;
try {
tu = CDOM.getInstance().getASTService().getTranslationUnit(file);
} catch (UnsupportedDialectException e) {
assertFalse(true); // shouldn't happen
}
if( lang == ParserLanguage.C && expectNoProblems )
{
assertEquals( CVisitor.getProblems(tu).length, 0 );
assertEquals( tu.getPreprocessorProblems().length, 0 );
}
else if ( lang == ParserLanguage.CPP && expectNoProblems )
{
assertEquals( CPPVisitor.getProblems(tu).length, 0 );
assertEquals( tu.getPreprocessorProblems().length, 0 );
}
if( expectNoProblems )
assertEquals( 0, tu.getPreprocessorProblems().length );
return tu;
}
}

View file

@ -38,11 +38,17 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.core.resources.IFile;
/**
* @author dsteffle
*/
public class AST2SelectionParseTest extends AST2SelectionParseBaseTest {
public AST2SelectionParseTest(String name) {
super(name, AST2SelectionParseTest.class);
}
public void testBaseCase_VariableReference() throws Exception
{
String code = "void f() { int x; x=3; }"; //$NON-NLS-1$
@ -1575,4 +1581,15 @@ public class AST2SelectionParseTest extends AST2SelectionParseBaseTest {
IASTNode node = parse( code, ParserLanguage.C, offset1, length );
assertNotNull(node);
}
public void testBug86126() throws Exception {
importFile("foo.h", "int x;\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
String code = "#include \"foo.h\"\r\n"; //$NON-NLS-1$
IFile file = importFile("blah.c", code);
int offset1 = code.indexOf( "#include \"foo.h\"" ); //$NON-NLS-1$
int length = "#include \"foo.h\"".length(); //$NON-NLS-1$
IASTNode node = parse( file, ParserLanguage.C, offset1, length );
assertNotNull(node);
}
}

View file

@ -42,13 +42,15 @@ public class DOMScanner extends BaseScanner {
public final char[] pt;
public final int o;
public final int eo;
/**
*
*/
public DOMInclusion(char[] path, int offset) {
public DOMInclusion(char[] path, int offset, int endOffset) {
this.pt = path;
this.o = offset;
this.eo = endOffset;
}
}
@ -135,7 +137,7 @@ public class DOMScanner extends BaseScanner {
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));
return new DOMInclusion(filenamePath, resolveOffset(startOffset), resolveOffset(endOffset));
}
/*
@ -196,8 +198,7 @@ public class DOMScanner extends BaseScanner {
if( ! isCircularInclusion( (InclusionData) data ))
{
DOMInclusion inc = ((DOMInclusion) ((InclusionData) data).inclusion);
locationMap.startInclusion(((InclusionData) data).reader, inc.o,
resolveOffset(getCurrentOffset()));
locationMap.startInclusion(((InclusionData) data).reader, inc.o, inc.eo);
bufferDelta[bufferStackPos + 1] = 0;
}
}