mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Fixed 81887 [Parser2] if () {...} else if () ... else {...} parsed incorrectly
Started DOMScannerTests.
This commit is contained in:
parent
35a922fdfd
commit
d019346a04
9 changed files with 97 additions and 151 deletions
|
@ -78,7 +78,8 @@ public class AST2BaseTest extends TestCase {
|
|||
configuration = new GCCScannerConfiguration();
|
||||
else
|
||||
configuration = new GPPScannerConfiguration();
|
||||
IScanner scanner = new DOMScanner( codeReader, scannerInfo, ParserMode.COMPLETE_PARSE, lang, NULL_LOG, configuration, SavedCodeReaderFactory.getInstance() );
|
||||
IScanner scanner = new DOMScanner( codeReader, scannerInfo, ParserMode.COMPLETE_PARSE, lang, NULL_LOG, configuration, SavedCodeReaderFactory.getInstance() );
|
||||
|
||||
ISourceCodeParser parser2 = null;
|
||||
if( lang == ParserLanguage.CPP )
|
||||
{
|
||||
|
@ -96,12 +97,15 @@ public class AST2BaseTest extends TestCase {
|
|||
parser2 = new GNUCSourceParser( scanner, ParserMode.COMPLETE_PARSE,
|
||||
NULL_LOG, config );
|
||||
}
|
||||
|
||||
IASTTranslationUnit tu = parser2.parse();
|
||||
|
||||
if( parser2.encounteredError() )
|
||||
throw new ParserException( "FAILURE"); //$NON-NLS-1$
|
||||
|
||||
//TODO add in assertion here to visit all problems
|
||||
|
||||
|
||||
return tu;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
/**********************************************************************
|
||||
* 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 org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.parser.ParserLanguage;
|
||||
import org.eclipse.cdt.internal.core.parser.ParserException;
|
||||
|
||||
|
||||
/**
|
||||
* @author jcamelon
|
||||
*/
|
||||
public class DOMScannerTests extends AST2BaseTest {
|
||||
|
||||
|
||||
public void testSimpleLocation() throws ParserException {
|
||||
IASTTranslationUnit tu = parse( "int x;", ParserLanguage.C ); //$NON-NLS-1$
|
||||
assertEquals( tu.getDeclarations()[0].getNodeLocations().length, 1 );
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
/**********************************************************************
|
||||
* 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 java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.cdt.core.parser.IScannerInfo;
|
||||
import org.eclipse.cdt.core.parser.ScannerInfo;
|
||||
|
||||
/**
|
||||
* @author Doug Schaefer
|
||||
*/
|
||||
public class ScannerConfigFactory {
|
||||
|
||||
public static IScannerInfo getScannerInfo() {
|
||||
String config = System.getProperty("speedTest.config");
|
||||
|
||||
if (config == null)
|
||||
return mingwScannerInfo();
|
||||
|
||||
if (config.equals("msvc"))
|
||||
return msvcScannerInfo();
|
||||
else if (config.equals("ydl"))
|
||||
return ydlScannerInfo();
|
||||
else
|
||||
return mingwScannerInfo();
|
||||
}
|
||||
|
||||
private static IScannerInfo msvcScannerInfo() {
|
||||
Map definitions = new Hashtable();
|
||||
//definitions.put( "__GNUC__", "3" ); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
||||
String [] includePaths = new String[] {
|
||||
"C:\\Program Files\\Microsoft SDK\\Include",
|
||||
"C:\\Program Files\\Microsoft Visual C++ Toolkit 2003\\include"
|
||||
};
|
||||
return new ScannerInfo( definitions, includePaths );
|
||||
}
|
||||
|
||||
private static IScannerInfo mingwScannerInfo() {
|
||||
// TODO It would be easier and more flexible if we used discovery for this
|
||||
Map definitions = new Hashtable();
|
||||
definitions.put("__GNUC__", "3");
|
||||
definitions.put("__GNUC_MINOR__", "2");
|
||||
definitions.put("__GNUC_PATCHLEVEL__", "3");
|
||||
definitions.put("__GXX_ABI_VERSION", "102");
|
||||
definitions.put("_WIN32", "");
|
||||
definitions.put("__WIN32", "");
|
||||
definitions.put("__WIN32__", "");
|
||||
definitions.put("WIN32", "");
|
||||
definitions.put("__MINGW32__", "");
|
||||
definitions.put("__MSVCRT__", "");
|
||||
definitions.put("WINNT", "");
|
||||
definitions.put("_X86_", "1");
|
||||
definitions.put("__WINNT", "");
|
||||
definitions.put("_NO_INLINE__", "");
|
||||
definitions.put("__STDC_HOSTED__", "1");
|
||||
definitions.put("i386", "");
|
||||
definitions.put("__i386", "");
|
||||
definitions.put("__i386__", "");
|
||||
definitions.put("__tune_i586__", "");
|
||||
definitions.put("__tune_pentium__", "");
|
||||
definitions.put("__stdcall", "__attribute__((__stdcall__))");
|
||||
definitions.put("__cdecl", "__attribute__((__cdecl__))");
|
||||
definitions.put("__fastcall", "__attribute__((__fastcall__))");
|
||||
definitions.put("_stdcall", "__attribute__((__stdcall__))");
|
||||
definitions.put("_cdecl", "__attribute__((__cdecl__))");
|
||||
definitions.put("_fastcall", "__attribute__((__fastcall__))");
|
||||
definitions.put("__declspec(x)", "__attribute__((x))");
|
||||
definitions.put("__DEPRECATED", "");
|
||||
definitions.put("__EXCEPTIONS", "");
|
||||
|
||||
String [] includePaths = new String[] {
|
||||
"c:/mingw/include/c++/3.2.3",
|
||||
"c:/mingw/include/c++/3.2.3/mingw32",
|
||||
"c:/mingw/include/c++/3.2.3/backward",
|
||||
"c:/mingw/include",
|
||||
"c:/mingw/lib/gcc-lib/mingw32/3.2.3/include"
|
||||
};
|
||||
|
||||
return new ScannerInfo( definitions, includePaths );
|
||||
}
|
||||
|
||||
private static IScannerInfo ydlScannerInfo() {
|
||||
// TODO It would be easier and more flexible if we used discovery for this
|
||||
Map definitions = new Hashtable();
|
||||
definitions.put("__GNUC__", "3");
|
||||
definitions.put("__GNUC_MINOR__", "3");
|
||||
definitions.put("__GNUC_PATCHLEVEL__", "3");
|
||||
definitions.put("_GNU_SOURCE", "");
|
||||
definitions.put("__unix__", "");
|
||||
definitions.put("__gnu_linux__", "");
|
||||
definitions.put("__linux__", "");
|
||||
definitions.put("unix", "");
|
||||
definitions.put("__unix", "");
|
||||
definitions.put("linux", "");
|
||||
definitions.put("__linux", "");
|
||||
definitions.put("__GNUG__", "3");
|
||||
|
||||
String [] includePaths = new String[] {
|
||||
"/usr/include/g++",
|
||||
"/usr/include/g++/powerpc-yellowdog-linux",
|
||||
"/usr/include/g++/backward",
|
||||
"/usr/local/include",
|
||||
"/usr/lib/gcc-lib/powerpc-yellowdog-linux/3.3.3/include",
|
||||
"/usr/include"
|
||||
};
|
||||
|
||||
return new ScannerInfo( definitions, includePaths );
|
||||
}
|
||||
|
||||
}
|
|
@ -18,10 +18,16 @@ import java.util.List;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IArrayType;
|
||||
import org.eclipse.cdt.core.dom.ast.IBasicType;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
|
@ -922,26 +928,26 @@ public class CompleteParser2Tests extends TestCase {
|
|||
|
||||
public void testSimpleIfStatement() throws Exception
|
||||
{
|
||||
/*IASTTranslationUnit tu =*/ parse( "const bool T = true; int foo() { if( T ) { return 5; } else if( ! T ) return 20; else { return 10; } }"); //$NON-NLS-1$
|
||||
IASTTranslationUnit tu =parse( "const bool T = true; int foo() { if( T ) { return 5; } else if( ! T ) return 20; else { return 10; } }"); //$NON-NLS-1$
|
||||
|
||||
// IASTFunctionDefinition foo = (IASTFunctionDefinition) tu.getDeclarations()[1];
|
||||
// IASTCompoundStatement compound = (IASTCompoundStatement) foo.getBody();
|
||||
// IASTIfStatement ifstmt = (IASTIfStatement) compound.getStatements()[0];
|
||||
// assertTrue( ifstmt.getCondition() instanceof IASTIdExpression );
|
||||
// assertTrue( ifstmt.getThenClause() instanceof IASTCompoundStatement );
|
||||
// assertTrue( ifstmt.getElseClause() instanceof IASTIfStatement );
|
||||
// ifstmt = (IASTIfStatement) ifstmt.getElseClause();
|
||||
// assertTrue( ifstmt.getCondition() instanceof IASTUnaryExpression );
|
||||
// assertTrue( ifstmt.getThenClause() instanceof IASTReturnStatement );
|
||||
// assertTrue( ifstmt.getElseClause() instanceof IASTCompoundStatement );
|
||||
//
|
||||
// CPPNameCollector col = new CPPNameCollector();
|
||||
// CPPVisitor.visitTranslationUnit( tu, col );
|
||||
//
|
||||
// assertEquals( col.size(), 4 );
|
||||
//
|
||||
// IVariable T = (IVariable) col.getName(0).resolveBinding();
|
||||
// assertInstances( col, T, 3 );
|
||||
IASTFunctionDefinition foo = (IASTFunctionDefinition) tu.getDeclarations()[1];
|
||||
IASTCompoundStatement compound = (IASTCompoundStatement) foo.getBody();
|
||||
IASTIfStatement ifstmt = (IASTIfStatement) compound.getStatements()[0];
|
||||
assertTrue( ifstmt.getCondition() instanceof IASTIdExpression );
|
||||
assertTrue( ifstmt.getThenClause() instanceof IASTCompoundStatement );
|
||||
assertTrue( ifstmt.getElseClause() instanceof IASTIfStatement );
|
||||
ifstmt = (IASTIfStatement) ifstmt.getElseClause();
|
||||
assertTrue( ifstmt.getCondition() instanceof IASTUnaryExpression );
|
||||
assertTrue( ifstmt.getThenClause() instanceof IASTReturnStatement );
|
||||
assertTrue( ifstmt.getElseClause() instanceof IASTCompoundStatement );
|
||||
|
||||
CPPNameCollector col = new CPPNameCollector();
|
||||
CPPVisitor.visitTranslationUnit( tu, col );
|
||||
|
||||
assertEquals( col.size(), 4 );
|
||||
|
||||
IVariable T = (IVariable) col.getName(0).resolveBinding();
|
||||
assertInstances( col, T, 3 );
|
||||
}
|
||||
|
||||
public void testSimpleWhileStatement() throws Exception
|
||||
|
|
|
@ -1726,15 +1726,22 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
|
|||
if_statement.setElseClause( new_if_statement );
|
||||
new_if_statement.setParent( if_statement );
|
||||
new_if_statement.setPropertyInParent( IASTIfStatement.ELSE );
|
||||
if_statement = new_if_statement;
|
||||
}
|
||||
if_statement = new_if_statement;
|
||||
continue if_loop;
|
||||
}
|
||||
IASTStatement elseStatement = statement();
|
||||
new_if_statement.setElseClause( elseStatement );
|
||||
elseStatement.setParent( new_if_statement );
|
||||
elseStatement.setPropertyInParent( IASTIfStatement.ELSE );
|
||||
if_statement = new_if_statement;
|
||||
if( if_statement != null )
|
||||
{
|
||||
if_statement.setElseClause( new_if_statement );
|
||||
new_if_statement.setParent( if_statement );
|
||||
new_if_statement.setPropertyInParent( IASTIfStatement.ELSE );
|
||||
}
|
||||
else
|
||||
if_statement = new_if_statement;
|
||||
}
|
||||
else
|
||||
if_statement = new_if_statement;
|
||||
|
|
|
@ -536,6 +536,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
|
|||
return;
|
||||
}
|
||||
|
||||
translationUnit.setLocationResolver(scanner.getLocationResolver());
|
||||
|
||||
int lastBacktrack = -1;
|
||||
while (true) {
|
||||
try {
|
||||
|
|
|
@ -149,7 +149,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
|
||||
private static final int DEFAULT_CATCH_HANDLER_LIST_SIZE = 4;
|
||||
private ScopeStack templateIdScopes = new ScopeStack();
|
||||
protected IASTTranslationUnit translationUnit;
|
||||
protected CPPASTTranslationUnit translationUnit;
|
||||
private static class ScopeStack {
|
||||
private int[] stack;
|
||||
|
||||
|
@ -4080,8 +4080,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
logException("translationUnit::createCompilationUnit()", e2); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
|
||||
// compilationUnit.enterScope( requestor );
|
||||
translationUnit.setLocationResolver(scanner.getLocationResolver());
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
|
@ -4145,7 +4144,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
/**
|
||||
* @return
|
||||
*/
|
||||
protected IASTTranslationUnit createTranslationUnit() {
|
||||
protected CPPASTTranslationUnit createTranslationUnit() {
|
||||
return new CPPASTTranslationUnit();
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,6 @@ import org.eclipse.cdt.internal.core.parser.token.SimpleToken;
|
|||
public class DOMScanner extends BaseScanner {
|
||||
|
||||
private final ICodeReaderFactory codeReaderFactory;
|
||||
// private int overallOffset = 0;
|
||||
|
||||
private static class DOMInclusion
|
||||
{
|
||||
|
@ -109,7 +108,30 @@ public class DOMScanner extends BaseScanner {
|
|||
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)
|
||||
*/
|
||||
protected void pushContext(char[] buffer, Object data) {
|
||||
//TODO calibrate offsets
|
||||
super.pushContext(buffer, data);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#popContext()
|
||||
*/
|
||||
protected void popContext() {
|
||||
//TODO calibrate offsets
|
||||
super.popContext();
|
||||
}
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
|
|
|
@ -31,7 +31,6 @@ import org.eclipse.cdt.core.parser.ParserLanguage;
|
|||
import org.eclipse.cdt.core.parser.ParserMode;
|
||||
import org.eclipse.cdt.core.parser.ParserUtil;
|
||||
import org.eclipse.cdt.core.parser.ScannerInfo;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.IRequiresLocationInformation;
|
||||
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.GCCParserExtensionConfiguration;
|
||||
|
@ -156,8 +155,6 @@ public class InternalASTServiceProvider implements IASTServiceProvider {
|
|||
}
|
||||
}
|
||||
IASTTranslationUnit tu = parser.parse();
|
||||
if( tu instanceof IRequiresLocationInformation )
|
||||
((IRequiresLocationInformation)tu).setLocationResolver( scanner.getLocationResolver() );
|
||||
return tu;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue