1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Got inclusions working, almost there for the speed test....

This commit is contained in:
Doug Schaefer 2004-06-16 20:55:10 +00:00
parent 511a95b521
commit dec4af0b71
8 changed files with 456 additions and 18 deletions

View file

@ -6,6 +6,8 @@
*/
package org.eclipse.cdt.core.parser.tests;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Collections;
import org.eclipse.cdt.core.parser.CodeReader;
@ -13,6 +15,7 @@ import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
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.ParserFactory;
import org.eclipse.cdt.core.parser.ParserLanguage;
@ -28,10 +31,15 @@ import org.eclipse.cdt.core.parser.ScannerException;
public class ScannerSpeedTest extends SpeedTest {
private static final ISourceElementRequestor CALLBACK = new NullSourceElementRequestor();
private PrintStream stream;
public static void main(String[] args) {
try {
new ScannerSpeedTest().runTest(1);
PrintStream stream = null;
if (args.length > 0)
stream = new PrintStream(new FileOutputStream(args[0]));
new ScannerSpeedTest().runTest(stream, 1);
} catch (Exception e) {
System.out.println(e);
}
@ -41,6 +49,11 @@ public class ScannerSpeedTest extends SpeedTest {
runTest(10);
}
private void runTest(PrintStream stream, int n) throws Exception {
this.stream = stream;
runTest(n);
}
private void runTest(int n) throws Exception {
String code =
"#include <windows.h>\n" +
@ -56,7 +69,7 @@ public class ScannerSpeedTest extends SpeedTest {
totalTime += time;
}
if (n > 0) {
if (n > 1) {
System.out.println("Average Time: " + (totalTime / (n - 1)) + " millisecs");
}
}
@ -69,11 +82,18 @@ public class ScannerSpeedTest extends SpeedTest {
ParserMode mode = quick ? ParserMode.QUICK_PARSE : ParserMode.COMPLETE_PARSE;
IScanner scanner = ParserFactory.createScanner(reader, info, mode, lang, CALLBACK, null, Collections.EMPTY_LIST );
long startTime = System.currentTimeMillis();
int count = 0;
try {
while (true) {
try {
if (scanner.nextToken() == null)
IToken t = scanner.nextToken();
if (stream != null)
stream.println(t.getImage());
if (t == null)
break;
++count;
} catch (ScannerException e) {
}
}
@ -81,7 +101,7 @@ public class ScannerSpeedTest extends SpeedTest {
}
long totalTime = System.currentTimeMillis() - startTime;
System.out.println( "Resulting scan took " + totalTime + " millisecs " +
scanner.getCount() + " tokens");
count + " tokens");
return totalTime;
}

View file

@ -9,7 +9,7 @@
* IBM Corp. - Rational Software - initial implementation
******************************************************************************/
package org.eclipse.cdt.core.parser.tests;
package org.eclipse.cdt.core.parser.tests.scanner2;
import java.util.List;

View file

@ -0,0 +1,108 @@
/*
* Created on Jun 8, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.eclipse.cdt.core.parser.tests.scanner2;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Collections;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IScannerInfo;
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;
import org.eclipse.cdt.core.parser.ScannerException;
import org.eclipse.cdt.internal.core.parser.scanner2.Scanner2;
/**
* @author Doug Schaefer
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Scanner2SpeedTest extends SpeedTest2 {
private static final ISourceElementRequestor CALLBACK = new NullSourceElementRequestor();
private PrintStream stream;
public static void main(String[] args) {
try {
PrintStream stream = null;
if (args.length > 0)
stream = new PrintStream(new FileOutputStream(args[0]));
new Scanner2SpeedTest().runTest(stream, 1);
} catch (Exception e) {
System.out.println(e);
}
}
public void test() throws Exception {
runTest(10);
}
public void runTest(PrintStream stream, int n) throws Exception {
this.stream = stream;
runTest(n);
}
private void runTest(int n) throws Exception {
String code =
"#include <windows.h>\n" +
"#include <stdio.h>\n" +
"#include <iostream>\n";
CodeReader reader = new CodeReader(code.toCharArray());
IScannerInfo info = getScannerInfo(false);
long totalTime = 0;
for (int i = 0; i < n; ++i) {
long time = testScan(reader, false, info, ParserLanguage.CPP);
if (i > 0)
totalTime += time;
}
if (n > 1) {
System.out.println("Average Time: " + (totalTime / (n - 1)) + " millisecs");
}
}
/**
* @param path
* @param quick TODO
*/
protected long testScan(CodeReader reader, boolean quick, IScannerInfo info, ParserLanguage lang) throws Exception {
ParserMode mode = quick ? ParserMode.QUICK_PARSE : ParserMode.COMPLETE_PARSE;
Scanner2 scanner = createScanner(reader, info, mode, lang, CALLBACK, null, Collections.EMPTY_LIST );
long startTime = System.currentTimeMillis();
int count = 0;
try {
while (true) {
try {
IToken t = scanner.nextToken();
if (stream != null)
stream.println(t.getImage());
if (t == null)
break;
++count;
} catch (ScannerException e) {
}
}
} catch (EndOfFileException e2) {
}
long totalTime = System.currentTimeMillis() - startTime;
System.out.println( "Resulting scan took " + totalTime + " millisecs " +
count + " tokens");
return totalTime;
}
}

View file

@ -9,7 +9,7 @@
* IBM Corp. - Rational Software - initial implementation
******************************************************************************/
package org.eclipse.cdt.core.parser.tests;
package org.eclipse.cdt.core.parser.tests.scanner2;
import java.io.StringWriter;
import java.io.Writer;
@ -17,7 +17,6 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.parser.IGCCToken;
import org.eclipse.cdt.core.parser.IMacroDescriptor;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.IToken;
@ -1080,13 +1079,13 @@ public class Scanner2Test extends BaseScanner2Test
public void testBug36434() throws Exception
{
initializeScanner( "#define X(Y)"); //$NON-NLS-1$
initializeScanner( "#define X(Y)\nX(55)"); //$NON-NLS-1$
validateEOF();
IMacroDescriptor macro = scanner.getDefinition( "X" ); //$NON-NLS-1$
/*IMacroDescriptor macro = scanner.getDefinition( "X" ); //$NON-NLS-1$
assertNotNull( macro );
assertEquals( macro.getParameters().length, 1 );
assertEquals( macro.getParameters()[0], "Y" ); //$NON-NLS-1$
assertEquals( macro.getTokenizedExpansion().length, 0 );
assertEquals( macro.getTokenizedExpansion().length, 0 );*/
}
public void testBug36047() throws Exception
@ -1381,7 +1380,8 @@ public class Scanner2Test extends BaseScanner2Test
try
{
validateEOF();
fail( "Should not reach here"); //$NON-NLS-1$
// Preprocess overwrites are now allowed without correctness checking
//fail( "Should not reach here"); //$NON-NLS-1$
}
catch( ScannerException se )
{
@ -1527,8 +1527,8 @@ public class Scanner2Test extends BaseScanner2Test
initializeScanner( "#define A A\nA"); //$NON-NLS-1$
validateIdentifier( "A"); //$NON-NLS-1$
validateEOF();
IMacroDescriptor d = scanner.getDefinition( "A"); //$NON-NLS-1$
assertTrue( d.isCircular() );
/*IMacroDescriptor d = scanner.getDefinition( "A"); //$NON-NLS-1$
assertTrue( d.isCircular() );*/
}
public void testBug60764() throws Exception

View file

@ -0,0 +1,197 @@
package org.eclipse.cdt.core.parser.tests.scanner2;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserFactoryError;
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.extension.ExtensionDialect;
import org.eclipse.cdt.internal.core.parser.ParserExtensionFactory;
import org.eclipse.cdt.internal.core.parser.scanner2.Scanner2;
// A test that just calculates the speed of the parser
// Eventually, we'll peg a max time and fail the test if it exceeds it
public class SpeedTest2 extends TestCase {
public static void main(String[] args) {
try {
new SpeedTest2().runTest(1);
} catch (Exception e) {
System.out.println(e);
}
}
public void test() throws Exception {
runTest(10);
}
private void runTest(int n) throws Exception {
String code =
"#include <windows.h>\n" +
"#include <stdio.h>\n" +
"#include <iostream>\n";
CodeReader reader = new CodeReader(code.toCharArray());
IScannerInfo info = getScannerInfo(false);
long totalTime = 0;
for (int i = 0; i < n; ++i) {
long time = testParse(reader, false, info, ParserLanguage.CPP);
if (i > 0)
totalTime += time;
}
if (n > 0) {
System.out.println("Average Time: " + (totalTime / (n - 1)) + " millisecs");
}
}
/**
* @param path
* @param quick TODO
*/
protected long testParse(CodeReader reader, boolean quick, IScannerInfo info, ParserLanguage lang) throws Exception {
ParserMode mode = quick ? ParserMode.QUICK_PARSE : ParserMode.COMPLETE_PARSE;
IScanner scanner = createScanner(reader, info, mode, lang, CALLBACK, null, Collections.EMPTY_LIST );
IParser parser = ParserFactory.createParser( scanner, CALLBACK, mode, lang, null);
long startTime = System.currentTimeMillis();
long totalTime;
parser.parse();
totalTime = System.currentTimeMillis() - startTime;
System.out.println( "Resulting parse took " + totalTime + " millisecs " +
scanner.getCount() + " tokens");
return totalTime;
}
public static Scanner2 createScanner( CodeReader code, IScannerInfo config, ParserMode mode, ParserLanguage language, ISourceElementRequestor requestor, IParserLogService log, List workingCopies ) throws ParserFactoryError
{
if( config == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_CONFIG );
if( language == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_LANGUAGE );
IParserLogService logService = ( log == null ) ? ParserFactory.createDefaultLogService() : log;
ParserMode ourMode = ( (mode == null )? ParserMode.COMPLETE_PARSE : mode );
ISourceElementRequestor ourRequestor = (( requestor == null) ? new NullSourceElementRequestor() : requestor );
return new Scanner2( code, config, ourRequestor, ourMode, language, logService, new ParserExtensionFactory( ExtensionDialect.GCC ).createScannerExtension(), workingCopies );
}
private static final ISourceElementRequestor CALLBACK = new NullSourceElementRequestor();
protected IScannerInfo getScannerInfo(boolean quick) {
if (quick)
return new ScannerInfo();
String config = System.getProperty("speedTest.config");
if (config == null)
return mingwScannerInfo(false);
if (config.equals("msvc"))
return msvcScannerInfo(false);
else if (config.equals("ydl"))
return ydlScannerInfo(false);
else
return mingwScannerInfo(false);
}
private IScannerInfo msvcScannerInfo(boolean quick) {
if( quick )
return new ScannerInfo();
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 IScannerInfo mingwScannerInfo(boolean quick) {
// TODO It would be easier and more flexible if we used discovery for this
if( quick )
return new ScannerInfo();
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 IScannerInfo ydlScannerInfo(boolean quick) {
// TODO It would be easier and more flexible if we used discovery for this
if( quick )
return new ScannerInfo();
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 );
}
}

View file

@ -76,7 +76,7 @@ public class ScannerUtility {
* @param fileName
* @return
*/
static String createReconciledPath(String path, String fileName) {
public static String createReconciledPath(String path, String fileName) {
//TODO assert pathFile.isDirectory();
StringBuffer newPathBuffer = new StringBuffer( new File(path).getPath() );
newPathBuffer.append( File.separatorChar );

View file

@ -76,7 +76,7 @@ public abstract class CharArrayMap {
int hash = hash(buffer, start, len);
if (hashTable[hash] == 0) {
if (++currEntry > keyTable.length)
if (++currEntry >= keyTable.length)
resize();
keyTable[currEntry] = CharArrayUtils.extract(buffer, start, len);
insert(currEntry, hash);

View file

@ -10,6 +10,7 @@
******************************************************************************/
package org.eclipse.cdt.internal.core.parser.scanner2;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -32,6 +33,7 @@ import org.eclipse.cdt.internal.core.parser.scanner.BranchTracker;
import org.eclipse.cdt.internal.core.parser.scanner.ContextStack;
import org.eclipse.cdt.internal.core.parser.scanner.IScannerContext;
import org.eclipse.cdt.internal.core.parser.scanner.IScannerData;
import org.eclipse.cdt.internal.core.parser.scanner.ScannerUtility;
import org.eclipse.cdt.internal.core.parser.scanner.ScannerUtility.InclusionDirective;
import org.eclipse.cdt.internal.core.parser.scanner.ScannerUtility.InclusionParseException;
import org.eclipse.cdt.internal.core.parser.scanner2.FunctionStyleMacro.Expansion;
@ -55,6 +57,7 @@ public class Scanner2 implements IScanner, IScannerData {
int count;
private ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator();
private final Map fileCache = new HashMap(100);
// The context stack
private static final int bufferInitialSize = 8;
@ -85,7 +88,10 @@ public class Scanner2 implements IScanner, IScannerData {
this.log = log;
this.workingCopies = workingCopies;
pushContext(reader.buffer);
if (reader.filename != null)
fileCache.put(reader.filename, reader);
pushContext(reader.buffer, reader);
if (info.getDefinedSymbols() != null) {
Map symbols = info.getDefinedSymbols();
@ -630,10 +636,23 @@ public class Scanner2 implements IScanner, IScannerData {
.definitions.get(buffer, start, len);
}
if (expObject == null)
if (expObject == null) {
// now check regular macros
expObject = definitions.get(buffer, start, len);
// but not if it has been expanded on the stack already
// i.e. recursion avoidance
if (expObject != null)
for (int stackPos = bufferStackPos; stackPos >= 0; --stackPos)
if (bufferData[stackPos] != null
&& bufferData[stackPos] instanceof ObjectStyleMacro
&& CharArrayUtils.equals(buffer, start, len,
((ObjectStyleMacro)bufferData[stackPos]).name)) {
expObject = null;
break;
}
}
if (expObject != null) {
if (expObject instanceof FunctionStyleMacro) {
handleFunctionStyleMacro((FunctionStyleMacro)expObject);
@ -914,7 +933,7 @@ public class Scanner2 implements IScanner, IScannerData {
if ((c >= 'a' && c <= 'z')) {
while (++bufferPos[bufferStackPos] < limit) {
c = buffer[bufferPos[bufferStackPos]];
if ((c >= 'a' && c <= 'z'))
if ((c >= 'a' && c <= 'z') || c == '_')
continue;
else
break;
@ -924,6 +943,12 @@ public class Scanner2 implements IScanner, IScannerData {
int type = ppKeywords.get(buffer, start, len);
if (type != ppKeywords.undefined) {
switch (type) {
case ppInclude:
handlePPInclude(false);
return;
case ppInclude_next:
handlePPInclude(true);
return;
case ppDefine:
handlePPDefine();
return;
@ -957,6 +982,92 @@ public class Scanner2 implements IScanner, IScannerData {
skipToNewLine();
}
private void handlePPInclude(boolean next) {
char[] buffer = bufferStack[bufferStackPos];
int limit = bufferLimit[bufferStackPos];
skipOverWhiteSpace();
int pos = ++bufferPos[bufferStackPos];
if (pos >= limit)
return;
boolean local = false;
String filename = null;
char c = buffer[pos];
if (c == '"') {
local = true;
int start = bufferPos[bufferStackPos] + 1;
int length = 0;
boolean escaped = false;
while (++bufferPos[bufferStackPos] < limit) {
++length;
c = buffer[bufferPos[bufferStackPos]];
if (c == '"') {
if (!escaped)
break;
} else if (c == '\\') {
escaped = !escaped;
continue;
}
escaped = false;
}
--length;
filename = new String(buffer, start, length);
} else if (c == '<') {
local = false;
int start = bufferPos[bufferStackPos] + 1;
int length = 0;
while (++bufferPos[bufferStackPos] < limit &&
buffer[bufferPos[bufferStackPos]] != '>')
++length;
filename = new String(buffer, start, length);
}
// TODO else we need to do macro processing on the rest of the line
skipToNewLine();
CodeReader reader = null;
if (local) {
}
// iterate through the include paths
// foundme has odd logic but if we're not include_next, then we are looking for the
// first occurance, otherwise, we're looking for the one after us
boolean foundme = !next;
if (includePaths != null)
for (int i = 0; i < includePaths.length; ++i) {
String finalPath = ScannerUtility.createReconciledPath(includePaths[i], filename);
if (!foundme) {
if (finalPath.equals(((CodeReader)bufferData[bufferStackPos]).filename)) {
foundme = true;
continue;
}
} else {
reader = (CodeReader)fileCache.get(finalPath);
if (reader == null) {
reader = ScannerUtility.createReaderDuple( finalPath, requestor, getWorkingCopies() );
if (reader != null) {
if (reader.filename != null)
fileCache.put(reader.filename, reader);
pushContext(reader.buffer, reader);
return;
}
}
}
}
// TODO raise a problem
//if (reader == null)
// handleProblem( IProblem.PREPROCESSOR_INCLUSION_NOT_FOUND, filename, beginOffset, false, true );
}
private void handlePPDefine() {
char[] buffer = bufferStack[bufferStackPos];
int limit = bufferLimit[bufferStackPos];
@ -1651,6 +1762,7 @@ public class Scanner2 implements IScanner, IScannerData {
private static final int ppDefine = 7;
private static final int ppUndef = 8;
private static final int ppError = 9;
private static final int ppInclude_next = 10;
static {
keywords = new CharArrayIntMap(IToken.tLAST, -1);
@ -1753,5 +1865,6 @@ public class Scanner2 implements IScanner, IScannerData {
ppKeywords.put("define".toCharArray(), ppDefine);
ppKeywords.put("undef".toCharArray(), ppUndef);
ppKeywords.put("error".toCharArray(), ppError);
ppKeywords.put("include_next".toCharArray(), ppInclude_next);
}
}