diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java index 3723978287e..53860d5e61a 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java @@ -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; } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMScannerTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMScannerTests.java new file mode 100644 index 00000000000..72f4ca2a053 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMScannerTests.java @@ -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 ); + } + + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/ScannerConfigFactory.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/ScannerConfigFactory.java deleted file mode 100644 index e5f5e07bda5..00000000000 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/ScannerConfigFactory.java +++ /dev/null @@ -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 ); - } - -} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/parser2/CompleteParser2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/parser2/CompleteParser2Tests.java index 4eefccea516..4f39bb519fe 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/parser2/CompleteParser2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/parser2/CompleteParser2Tests.java @@ -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 diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java index 712d454ec42..53acee0df6e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java @@ -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; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java index 583e5234e4e..43aa631c610 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java @@ -536,6 +536,8 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser { return; } + translationUnit.setLocationResolver(scanner.getLocationResolver()); + int lastBacktrack = -1; while (true) { try { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java index ec2e91552fe..d021a22c543 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java @@ -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(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java index bc7804c3f92..a932d1b579d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java @@ -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 */ diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/dom/InternalASTServiceProvider.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/dom/InternalASTServiceProvider.java index 4a725f7755a..2d8c9b8f7ea 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/dom/InternalASTServiceProvider.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/dom/InternalASTServiceProvider.java @@ -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; }