1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Minor cleanups.

This commit is contained in:
John Camelon 2004-07-16 18:47:37 +00:00
parent bd43f8a328
commit cd1605463c
4 changed files with 63 additions and 98 deletions

View file

@ -16,6 +16,8 @@ import junit.framework.TestSuite;
import org.eclipse.cdt.core.model.tests.CModelElementsTests; import org.eclipse.cdt.core.model.tests.CModelElementsTests;
import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests; import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.tests.scanner2.Scanner2Test;
/** /**
* @author jcamelon * @author jcamelon
@ -25,10 +27,15 @@ import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests;
*/ */
public class ParserTestSuite extends TestCase { public class ParserTestSuite extends TestCase {
public static Test suite() { public static Test suite() {
TestSuite suite= new TestSuite(ParserTestSuite.class.getName()); TestSuite suite= new TestSuite(ParserTestSuite.class.getName());
suite.addTestSuite(BranchTrackerTest.class); if( ParserFactory.USE_NEW_SCANNER )
suite.addTestSuite(ScannerTestCase.class); suite.addTestSuite(Scanner2Test.class );
suite.addTestSuite(ExprEvalTest.class); else
{
suite.addTestSuite(BranchTrackerTest.class);
suite.addTestSuite(ScannerTestCase.class);
suite.addTestSuite(ExprEvalTest.class);
}
suite.addTestSuite(QuickParseASTTests.class); suite.addTestSuite(QuickParseASTTests.class);
suite.addTestSuite(ParserSymbolTableTest.class); suite.addTestSuite(ParserSymbolTableTest.class);
suite.addTestSuite(ParserSymbolTableTemplateTests.class ); suite.addTestSuite(ParserSymbolTableTemplateTests.class );

View file

@ -488,21 +488,21 @@ public class Scanner2Test extends BaseScanner2Test
validateDefinition("TWO", "ONE + ONE"); //$NON-NLS-1$ //$NON-NLS-2$ validateDefinition("TWO", "ONE + ONE"); //$NON-NLS-1$ //$NON-NLS-2$
initializeScanner( initializeScanner(
"#ifndef ONE\n" + "#ifndef ONE\r\n" + //$NON-NLS-1$
"# define ONE 1\n" + "# define ONE 1\n" + //$NON-NLS-1$
"# ifndef TWO\n" + "# ifndef TWO\n" + //$NON-NLS-1$
"# define TWO ONE + ONE \n" + "# define TWO ONE + ONE \n" + //$NON-NLS-1$
"# else\n" + "# else\n" + //$NON-NLS-1$
"# undef TWO\n" + "# undef TWO\n" + //$NON-NLS-1$
"# define TWO 2 \n" + "# define TWO 2 \n" + //$NON-NLS-1$
"# endif\n" + "# endif\n" + //$NON-NLS-1$
"#else\n" + "#else\n" + //$NON-NLS-1$
"# ifndef TWO\n" + "# ifndef TWO\n" + //$NON-NLS-1$
"# define TWO ONE + ONE \n" + "# define TWO ONE + ONE \n" + //$NON-NLS-1$
"# else\n" + "# else\n" + //$NON-NLS-1$
"# undef TWO\n" + "# undef TWO\n" + //$NON-NLS-1$
"# define TWO 2 \n" + "# define TWO 2 \n" + //$NON-NLS-1$
"# endif\n" + "# endif\n" + //$NON-NLS-1$
"#endif\n"); //$NON-NLS-1$" + "#endif\n"); //$NON-NLS-1$" +
scanner.addDefinition("ONE", "one"); //$NON-NLS-1$ //$NON-NLS-2$ scanner.addDefinition("ONE", "one"); //$NON-NLS-1$ //$NON-NLS-2$
@ -581,15 +581,15 @@ public class Scanner2Test extends BaseScanner2Test
try try
{ {
initializeScanner( initializeScanner(
"#ifndef FIVE \n" + "#ifndef FIVE \n" + //$NON-NLS-1$
"#define FIVE 5\n" + "#define FIVE 5\n" + //$NON-NLS-1$
"#endif \n" + "#endif \n" + //$NON-NLS-1$
"#ifndef TEN\n" + "#ifndef TEN\n" + //$NON-NLS-1$
"#define TEN 2 * FIVE\n" + "#define TEN 2 * FIVE\n" + //$NON-NLS-1$
"#endif\n" + "#endif\n" + //$NON-NLS-1$
"#if TEN != 10\n" + "#if TEN != 10\n" + //$NON-NLS-1$
"#define MISTAKE 1\n" + "#define MISTAKE 1\n" + //$NON-NLS-1$
"#error Five does not equal 10\n" + "#error Five does not equal 10\n" + //$NON-NLS-1$
"#endif\n"); //$NON-NLS-1$ "#endif\n"); //$NON-NLS-1$
scanner.addDefinition("FIVE", "55"); //$NON-NLS-1$ //$NON-NLS-2$ scanner.addDefinition("FIVE", "55"); //$NON-NLS-1$ //$NON-NLS-2$
validateEOF(); validateEOF();
@ -1547,7 +1547,7 @@ public class Scanner2Test extends BaseScanner2Test
{ {
Callback callback = new Callback(ParserMode.QUICK_PARSE); Callback callback = new Callback(ParserMode.QUICK_PARSE);
initializeScanner( "0x", ParserMode.QUICK_PARSE, callback ); //$NON-NLS-1$ initializeScanner( "0x", ParserMode.QUICK_PARSE, callback ); //$NON-NLS-1$
validateInteger("0x"); // to me this is a valid number validateInteger("0x"); // to me this is a valid number //$NON-NLS-1$
validateEOF(); validateEOF();
//assertFalse( callback.problems.isEmpty() ); //assertFalse( callback.problems.isEmpty() );
} }
@ -1644,43 +1644,43 @@ public class Scanner2Test extends BaseScanner2Test
public void test__attribute__() throws Exception { public void test__attribute__() throws Exception {
initializeScanner( initializeScanner(
"#define __cdecl __attribute__((cdecl))\n" + "#define __cdecl __attribute__((cdecl))\n" + //$NON-NLS-1$
"__cdecl;"); "__cdecl;"); //$NON-NLS-1$
validateToken(IToken.tSEMI); validateToken(IToken.tSEMI);
validateEOF(); validateEOF();
} }
public void testUndef() throws Exception { public void testUndef() throws Exception {
initializeScanner( initializeScanner(
"#define A 5\n" + "#define A 5\n" + //$NON-NLS-1$
"#define B 10\n" + "#define B 10\n" + //$NON-NLS-1$
"#undef A\n" + "#undef A\n" + //$NON-NLS-1$
"A B"); "A B"); //$NON-NLS-1$
validateIdentifier("A"); validateIdentifier("A"); //$NON-NLS-1$
validateInteger("10"); validateInteger("10"); //$NON-NLS-1$
validateEOF(); validateEOF();
} }
public void testWackyFunctionMacros() throws Exception { public void testWackyFunctionMacros() throws Exception {
initializeScanner( initializeScanner(
"#define A(X) hi##X\n" + "#define A(X) hi##X\n" + //$NON-NLS-1$
"#define B(Y) A(Y)\n" + "#define B(Y) A(Y)\n" + //$NON-NLS-1$
"B(there)"); "B(there)"); //$NON-NLS-1$
validateIdentifier("hithere"); validateIdentifier("hithere"); //$NON-NLS-1$
validateEOF(); validateEOF();
} }
public void testSlashes() throws Exception { public void testSlashes() throws Exception {
initializeScanner("__q / __n"); initializeScanner("__q / __n"); //$NON-NLS-1$
validateIdentifier("__q"); validateIdentifier("__q"); //$NON-NLS-1$
validateToken(IToken.tDIV); validateToken(IToken.tDIV);
validateIdentifier("__n"); validateIdentifier("__n"); //$NON-NLS-1$
validateEOF(); validateEOF();
} }
public void testStringify() throws Exception { public void testStringify() throws Exception {
initializeScanner("#define xS(s) #s\n#define S(s) xS(s)#define X hi\nS(X)"); initializeScanner("#define xS(s) #s\n#define S(s) xS(s)\n#define X hi\nS(X)"); //$NON-NLS-1$
validateString("hi"); validateString("hi"); //$NON-NLS-1$
validateEOF(); validateEOF();
} }
} }

View file

@ -30,6 +30,7 @@ import org.eclipse.cdt.internal.core.parser.ast.complete.CompleteParseASTFactory
import org.eclipse.cdt.internal.core.parser.ast.expression.ExpressionParseASTFactory; import org.eclipse.cdt.internal.core.parser.ast.expression.ExpressionParseASTFactory;
import org.eclipse.cdt.internal.core.parser.ast.quick.QuickParseASTFactory; import org.eclipse.cdt.internal.core.parser.ast.quick.QuickParseASTFactory;
import org.eclipse.cdt.internal.core.parser.scanner.Scanner; import org.eclipse.cdt.internal.core.parser.scanner.Scanner;
import org.eclipse.cdt.internal.core.parser.scanner2.Scanner2;
import org.eclipse.cdt.internal.core.parser.token.KeywordSets; import org.eclipse.cdt.internal.core.parser.token.KeywordSets;
@ -39,6 +40,8 @@ import org.eclipse.cdt.internal.core.parser.token.KeywordSets;
*/ */
public class ParserFactory { public class ParserFactory {
public static final boolean USE_NEW_SCANNER = false;
private static IParserExtensionFactory extensionFactory = new ParserExtensionFactory( ExtensionDialect.GCC ); private static IParserExtensionFactory extensionFactory = new ParserExtensionFactory( ExtensionDialect.GCC );
public static IASTFactory createASTFactory( IFilenameProvider provider, ParserMode mode, ParserLanguage language ) public static IASTFactory createASTFactory( IFilenameProvider provider, ParserMode mode, ParserLanguage language )
@ -100,8 +103,12 @@ public class ParserFactory {
if( language == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_LANGUAGE ); if( language == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_LANGUAGE );
IParserLogService logService = ( log == null ) ? createDefaultLogService() : log; IParserLogService logService = ( log == null ) ? createDefaultLogService() : log;
ParserMode ourMode = ( (mode == null )? ParserMode.COMPLETE_PARSE : mode ); ParserMode ourMode = ( (mode == null )? ParserMode.COMPLETE_PARSE : mode );
ISourceElementRequestor ourRequestor = (( requestor == null) ? new NullSourceElementRequestor() : requestor ); ISourceElementRequestor ourRequestor = (( requestor == null) ? new NullSourceElementRequestor() : requestor );
IScanner s = new Scanner( code, config, ourRequestor, ourMode, language, logService, extensionFactory.createScannerExtension(), workingCopies ); IScanner s = null;
if( USE_NEW_SCANNER )
s = new Scanner2( code, config, ourRequestor, ourMode, language, logService, extensionFactory.createScannerExtension(), workingCopies );
else
s = new Scanner( code, config, ourRequestor, ourMode, language, logService, extensionFactory.createScannerExtension(), workingCopies );
return s; return s;
} }

View file

@ -2593,59 +2593,10 @@ public final class Scanner implements IScanner, IScannerData {
} }
} }
protected boolean evaluateExpressionOld(String expression, int beginningOffset )
throws ScannerException {
IExpressionParser parser = null;
strbuff.startString();
strbuff.append(expression);
strbuff.append(';');
IScanner trial = new Scanner(
new CodeReader(strbuff.toString().toCharArray()),
definitions,
includePathNames,
NULL_REQUESTOR,
ParserMode.QUICK_PARSE,
language,
NULL_LOG_SERVICE,
scannerExtension );
parser = InternalParserUtil.createExpressionParser(trial, language, NULL_LOG_SERVICE);
try {
IASTExpression exp = parser.expression(null, null, null);
return (exp.evaluateExpression() != 0);
} catch( BacktrackException backtrack )
{
if( parserMode == ParserMode.QUICK_PARSE )
return false;
handleProblem( IProblem.PREPROCESSOR_CONDITIONAL_EVAL_ERROR, expression, beginningOffset, false, true );
}
catch (ASTExpressionEvaluationException e) {
if( parserMode == ParserMode.QUICK_PARSE )
return false;
handleProblem( IProblem.PREPROCESSOR_CONDITIONAL_EVAL_ERROR, expression, beginningOffset, false, true );
} catch (EndOfFileException e) {
if( parserMode == ParserMode.QUICK_PARSE )
return false;
handleProblem( IProblem.PREPROCESSOR_CONDITIONAL_EVAL_ERROR, expression, beginningOffset, false, true );
}
return true;
}
protected boolean evaluateExpression(String expression, int beginningOffset ) protected boolean evaluateExpression(String expression, int beginningOffset )
throws ScannerException { throws ScannerException {
return evaluateExpressionNew(expression, beginningOffset);
// boolean old_e = evaluateExpressionOld(expression, beginningOffset);
boolean new_e = evaluateExpressionNew(expression, beginningOffset);
// if (old_e != new_e) {
// System.out.println("Ouch " + expression + " New: " + new_e + " Old: " + old_e);
// }
// if (true)
return new_e;
// else
// return old_e;
} }