diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/InclusionTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/InclusionTests.java new file mode 100644 index 00000000000..b2ea118ab87 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/InclusionTests.java @@ -0,0 +1,188 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + * Markus Schorn (Wind River Systems) + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.scanner; + +import java.util.Collections; + +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.parser.CodeReader; +import org.eclipse.cdt.core.parser.ExtendedScannerInfo; +import org.eclipse.cdt.core.parser.IScannerInfo; +import org.eclipse.cdt.core.parser.IToken; +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.testplugin.CProjectHelper; +import org.eclipse.cdt.core.testplugin.util.TestSourceReader; +import org.eclipse.cdt.internal.core.pdom.indexer.nulli.PDOMNullIndexer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.runtime.NullProgressMonitor; + +/** + * Scanner2Tests ported to use the CPreprocessor + */ +public class InclusionTests extends PreprocessorTestsBase { + public static TestSuite suite() { + return suite(InclusionTests.class); + } + + private ICProject fProject; + + public InclusionTests() { + super(); + } + + public InclusionTests(String name) { + super(name); + } + + protected void tearDown() throws Exception { + if (fProject != null) { + CProjectHelper.delete(fProject); + fProject= null; + } + super.tearDown(); + } + + public final static int SIZEOF_TRUTHTABLE = 10; + + private IFile importFile(String fileName, String contents ) throws Exception{ + if (fProject == null) { + fProject= CProjectHelper.createCProject(getClass().getName(), null, PDOMNullIndexer.ID); + } + return TestSourceReader.createFile(fProject.getProject(), fileName, contents); + } + + private IFolder importFolder(String name) throws Exception{ + if (fProject == null) { + fProject= CProjectHelper.createCProject(getClass().getName(), null, PDOMNullIndexer.ID); + } + IFolder folder= fProject.getProject().getFolder(name); + if (!folder.exists()) { + folder.create(true, true, new NullProgressMonitor()); + } + return folder; + } + + public void testIncludeNext() throws Exception { + String baseFile = "int zero; \n#include \"foo.h\""; //$NON-NLS-1$ + String i1Next = "int one; \n#include_next "; //$NON-NLS-1$ + String i2Next = "int two; \n#include_next \"foo.h\""; //$NON-NLS-1$ + String i3Next = "int three; \n"; //$NON-NLS-1$ + + + IFile base = importFile( "base.cpp", baseFile ); //$NON-NLS-1$ + importFile( "foo.h", i1Next ); //$NON-NLS-1$ + IFolder twof = importFolder("two"); //$NON-NLS-1$ + IFolder threef = importFolder("three"); //$NON-NLS-1$ + importFile( "two/foo.h", i2Next ); //$NON-NLS-1$ + importFile( "three/foo.h", i3Next ); //$NON-NLS-1$ + + String [] path = new String[2]; + path[0] = twof.getLocation().toOSString(); + path[1] = threef.getLocation().toOSString(); + + IScannerInfo scannerInfo = new ExtendedScannerInfo( Collections.EMPTY_MAP, path, new String[]{}, null ); + CodeReader reader= new CodeReader(base.getLocation().toString()); + initializeScanner(reader, ParserLanguage.C, ParserMode.COMPLETE_PARSE, scannerInfo); + + validateToken(IToken.t_int); + validateIdentifier("zero"); + validateToken(IToken.tSEMI); + + validateToken(IToken.t_int); + validateIdentifier("one"); + validateToken(IToken.tSEMI); + + validateToken(IToken.t_int); + validateIdentifier("two"); + validateToken(IToken.tSEMI); + + validateToken(IToken.t_int); + validateIdentifier("three"); + validateToken(IToken.tSEMI); + + validateEOF(); + } + + public void testIncludePathOrdering() throws Exception + { + // create directory structure: + // project/base.cpp + // project/foo.h + // project/two/foo.h + // project/three/foo.h + + // this test sets the include path to be two;three and include foo.h (we should see the contents of two/foo.h + // then we change to three;two and we should see the contents of three/foo.h. + + String baseFile = "#include "; //$NON-NLS-1$ + String i1Next = "int one;\n"; //$NON-NLS-1$ + String i2Next = "int two;\n"; //$NON-NLS-1$ + String i3Next = "int three;\n"; //$NON-NLS-1$ + + IFile base = importFile( "base.cpp", baseFile ); //$NON-NLS-1$ + importFile( "foo.h", i1Next ); //$NON-NLS-1$ + IFolder twof = importFolder("two"); //$NON-NLS-1$ + IFolder threef = importFolder("three"); //$NON-NLS-1$ + importFile( "two/foo.h", i2Next ); //$NON-NLS-1$ + importFile( "three/foo.h", i3Next ); //$NON-NLS-1$ + + String [] path = new String[2]; + path[0] = twof.getLocation().toOSString(); + path[1] = threef.getLocation().toOSString(); + + IScannerInfo scannerInfo = new ExtendedScannerInfo( Collections.EMPTY_MAP, path, new String[]{}, null ); + CodeReader reader= new CodeReader(base.getLocation().toString()); + initializeScanner(reader, ParserLanguage.C, ParserMode.COMPLETE_PARSE, scannerInfo); + + validateToken(IToken.t_int); + validateIdentifier("two"); + validateToken(IToken.tSEMI); + validateEOF(); + + path[0] = threef.getLocation().toOSString(); + path[1] = twof.getLocation().toOSString(); + + scannerInfo = new ExtendedScannerInfo( Collections.EMPTY_MAP, path, new String[]{}, null ); + initializeScanner(reader, ParserLanguage.C, ParserMode.COMPLETE_PARSE, scannerInfo); + + validateToken(IToken.t_int); + validateIdentifier("three"); + validateToken(IToken.tSEMI); + validateEOF(); + } + + public void testBug91086() throws Exception { + IFile inclusion = importFile( "file.h", "#define FOUND 666\n" ); //$NON-NLS-1$ //$NON-NLS-2$ + StringBuffer buffer = new StringBuffer( "#include \"" ); //$NON-NLS-1$ + buffer.append( inclusion.getLocation().toOSString() ); + buffer.append( "\"\n"); //$NON-NLS-1$ + buffer.append( "int var = FOUND;\n"); //$NON-NLS-1$ + IFile base = importFile( "base.cpp", buffer.toString() ); //$NON-NLS-1$ + + CodeReader reader= new CodeReader(base.getLocation().toString()); + ParserLanguage lang[]= {ParserLanguage.C, ParserLanguage.CPP}; + for (int i = 0; i < lang.length; i++) { + initializeScanner(reader, lang[i], ParserMode.COMPLETE_PARSE, new ScannerInfo()); + validateToken(IToken.t_int); + validateIdentifier("var"); + validateToken(IToken.tASSIGN); + validateInteger("666"); + validateToken(IToken.tSEMI); + validateEOF(); + } + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PortedScannerTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PortedScannerTests.java index 4dcafbb8611..20718a92b5b 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PortedScannerTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PortedScannerTests.java @@ -22,10 +22,9 @@ import junit.framework.TestSuite; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTProblem; import org.eclipse.cdt.core.dom.ast.IMacroBinding; +import org.eclipse.cdt.core.parser.IGCCToken; import org.eclipse.cdt.core.parser.IProblem; -import org.eclipse.cdt.core.parser.ISourceElementRequestor; import org.eclipse.cdt.core.parser.IToken; -import org.eclipse.cdt.core.parser.NullSourceElementRequestor; import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.core.parser.ParserMode; @@ -1539,12 +1538,6 @@ public class PortedScannerTests extends PreprocessorTestsBase { StringBuffer buffer = new StringBuffer( "#if CONST \n #endif \n #elif CONST \n int"); final List problems = new ArrayList(); - ISourceElementRequestor requestor = new NullSourceElementRequestor() { - public boolean acceptProblem(IProblem problem) { - problems.add(problem); - return super.acceptProblem(problem); - } - }; initializeScanner(buffer.toString()); validateToken(IToken.t_int); validateProblemCount(1); @@ -2393,4 +2386,31 @@ public class PortedScannerTests extends PreprocessorTestsBase { initializeScanner(buffer.toString()); validateToken(IToken.tPLUS); } + + public void testBug39698() throws Exception { + initializeScanner( "?"); //$NON-NLS-1$ + validateToken( IGCCToken.tMIN ); + validateToken( IGCCToken.tMAX ); + validateEOF(); + } + + public void test__attribute__() throws Exception { + initializeScanner( + "#define __cdecl __attribute__((cdecl))\n" + //$NON-NLS-1$ + "__cdecl;"); //$NON-NLS-1$ + validateToken(IGCCToken.t__attribute__); + validateToken(IToken.tLPAREN); + validateToken(IToken.tLPAREN); + validateToken(IToken.tIDENTIFIER); + validateToken(IToken.tRPAREN); + validateToken(IToken.tRPAREN); + validateToken(IToken.tSEMI); + validateEOF(); + } + + public void testImaginary() throws Exception { + initializeScanner( "3i", ParserLanguage.C ); //$NON-NLS-1$ + validateInteger( "3i" ); //$NON-NLS-1$ + validateEOF(); + } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorTestsBase.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorTestsBase.java index d553ecb4f6e..31ca62949d6 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorTestsBase.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorTestsBase.java @@ -56,18 +56,16 @@ public abstract class PreprocessorTestsBase extends BaseTestCase { } protected void initializeScanner(String input, ParserMode mode) throws IOException { - initializeScanner(input, ParserLanguage.CPP, mode); + initializeScanner(new CodeReader(input.toCharArray()), ParserLanguage.CPP, mode, new ScannerInfo()); } protected void initializeScanner(String input, ParserLanguage lang) throws IOException { - initializeScanner(input, lang, ParserMode.COMPLETE_PARSE); + initializeScanner(new CodeReader(input.toCharArray()), lang, ParserMode.COMPLETE_PARSE, new ScannerInfo()); } - protected void initializeScanner(String input, ParserLanguage lang, ParserMode mode) throws IOException { + protected void initializeScanner(CodeReader input, ParserLanguage lang, ParserMode mode, IScannerInfo scannerInfo) throws IOException { ICodeReaderFactory readerFactory= FileCodeReaderFactory.getInstance(); - CodeReader reader= new CodeReader(input.toCharArray()); IScannerExtensionConfiguration scannerConfig; - IScannerInfo scannerInfo= new ScannerInfo(); if (lang == ParserLanguage.C) { scannerConfig= new GCCScannerExtensionConfiguration(); @@ -76,7 +74,7 @@ public abstract class PreprocessorTestsBase extends BaseTestCase { scannerConfig= new GPPScannerExtensionConfiguration(); } - fScanner= new CPreprocessor(reader, scannerInfo, lang, NULL_LOG, scannerConfig, readerFactory); + fScanner= new CPreprocessor(input, scannerInfo, lang, NULL_LOG, scannerConfig, readerFactory); fLocationResolver= (ILocationResolver) fScanner.getAdapter(ILocationResolver.class); } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/ScannerTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/ScannerTestSuite.java index 99a33ff4c73..26df045ebb0 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/ScannerTestSuite.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/ScannerTestSuite.java @@ -21,6 +21,7 @@ public class ScannerTestSuite extends TestSuite { suite.addTest(LocationMapTests.suite()); suite.addTest(PortedScannerTests.suite()); suite.addTest(PreprocessorTests.suite()); + suite.addTest(InclusionTests.suite()); suite.addTest(PreprocessorBugsTests.suite()); return suite; }