mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Ported additional tests from Scanner2 to CPreprocessor
This commit is contained in:
parent
478f884711
commit
0f377144c4
4 changed files with 221 additions and 14 deletions
|
@ -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 <foo.h>"; //$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 <foo.h>"; //$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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,10 +22,9 @@ import junit.framework.TestSuite;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTProblem;
|
import org.eclipse.cdt.core.dom.ast.IASTProblem;
|
||||||
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
|
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.IProblem;
|
||||||
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
|
|
||||||
import org.eclipse.cdt.core.parser.IToken;
|
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.ParserLanguage;
|
||||||
import org.eclipse.cdt.core.parser.ParserMode;
|
import org.eclipse.cdt.core.parser.ParserMode;
|
||||||
|
|
||||||
|
@ -1539,12 +1538,6 @@ public class PortedScannerTests extends PreprocessorTestsBase {
|
||||||
StringBuffer buffer = new StringBuffer(
|
StringBuffer buffer = new StringBuffer(
|
||||||
"#if CONST \n #endif \n #elif CONST \n int");
|
"#if CONST \n #endif \n #elif CONST \n int");
|
||||||
final List problems = new ArrayList();
|
final List problems = new ArrayList();
|
||||||
ISourceElementRequestor requestor = new NullSourceElementRequestor() {
|
|
||||||
public boolean acceptProblem(IProblem problem) {
|
|
||||||
problems.add(problem);
|
|
||||||
return super.acceptProblem(problem);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
initializeScanner(buffer.toString());
|
initializeScanner(buffer.toString());
|
||||||
validateToken(IToken.t_int);
|
validateToken(IToken.t_int);
|
||||||
validateProblemCount(1);
|
validateProblemCount(1);
|
||||||
|
@ -2393,4 +2386,31 @@ public class PortedScannerTests extends PreprocessorTestsBase {
|
||||||
initializeScanner(buffer.toString());
|
initializeScanner(buffer.toString());
|
||||||
validateToken(IToken.tPLUS);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,18 +56,16 @@ public abstract class PreprocessorTestsBase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void initializeScanner(String input, ParserMode mode) throws IOException {
|
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 {
|
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();
|
ICodeReaderFactory readerFactory= FileCodeReaderFactory.getInstance();
|
||||||
CodeReader reader= new CodeReader(input.toCharArray());
|
|
||||||
IScannerExtensionConfiguration scannerConfig;
|
IScannerExtensionConfiguration scannerConfig;
|
||||||
IScannerInfo scannerInfo= new ScannerInfo();
|
|
||||||
|
|
||||||
if (lang == ParserLanguage.C) {
|
if (lang == ParserLanguage.C) {
|
||||||
scannerConfig= new GCCScannerExtensionConfiguration();
|
scannerConfig= new GCCScannerExtensionConfiguration();
|
||||||
|
@ -76,7 +74,7 @@ public abstract class PreprocessorTestsBase extends BaseTestCase {
|
||||||
scannerConfig= new GPPScannerExtensionConfiguration();
|
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);
|
fLocationResolver= (ILocationResolver) fScanner.getAdapter(ILocationResolver.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ public class ScannerTestSuite extends TestSuite {
|
||||||
suite.addTest(LocationMapTests.suite());
|
suite.addTest(LocationMapTests.suite());
|
||||||
suite.addTest(PortedScannerTests.suite());
|
suite.addTest(PortedScannerTests.suite());
|
||||||
suite.addTest(PreprocessorTests.suite());
|
suite.addTest(PreprocessorTests.suite());
|
||||||
|
suite.addTest(InclusionTests.suite());
|
||||||
suite.addTest(PreprocessorBugsTests.suite());
|
suite.addTest(PreprocessorBugsTests.suite());
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue