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.StructuralCModelElementsTests;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.tests.scanner2.Scanner2Test;
/**
* @author jcamelon
@ -25,10 +27,15 @@ import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests;
*/
public class ParserTestSuite extends TestCase {
public static Test suite() {
TestSuite suite= new TestSuite(ParserTestSuite.class.getName());
suite.addTestSuite(BranchTrackerTest.class);
suite.addTestSuite(ScannerTestCase.class);
suite.addTestSuite(ExprEvalTest.class);
TestSuite suite= new TestSuite(ParserTestSuite.class.getName());
if( ParserFactory.USE_NEW_SCANNER )
suite.addTestSuite(Scanner2Test.class );
else
{
suite.addTestSuite(BranchTrackerTest.class);
suite.addTestSuite(ScannerTestCase.class);
suite.addTestSuite(ExprEvalTest.class);
}
suite.addTestSuite(QuickParseASTTests.class);
suite.addTestSuite(ParserSymbolTableTest.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$
initializeScanner(
"#ifndef ONE\n" +
"# define ONE 1\n" +
"# ifndef TWO\n" +
"# define TWO ONE + ONE \n" +
"# else\n" +
"# undef TWO\n" +
"# define TWO 2 \n" +
"# endif\n" +
"#else\n" +
"# ifndef TWO\n" +
"# define TWO ONE + ONE \n" +
"# else\n" +
"# undef TWO\n" +
"# define TWO 2 \n" +
"# endif\n" +
"#ifndef ONE\r\n" + //$NON-NLS-1$
"# define ONE 1\n" + //$NON-NLS-1$
"# ifndef TWO\n" + //$NON-NLS-1$
"# define TWO ONE + ONE \n" + //$NON-NLS-1$
"# else\n" + //$NON-NLS-1$
"# undef TWO\n" + //$NON-NLS-1$
"# define TWO 2 \n" + //$NON-NLS-1$
"# endif\n" + //$NON-NLS-1$
"#else\n" + //$NON-NLS-1$
"# ifndef TWO\n" + //$NON-NLS-1$
"# define TWO ONE + ONE \n" + //$NON-NLS-1$
"# else\n" + //$NON-NLS-1$
"# undef TWO\n" + //$NON-NLS-1$
"# define TWO 2 \n" + //$NON-NLS-1$
"# endif\n" + //$NON-NLS-1$
"#endif\n"); //$NON-NLS-1$" +
scanner.addDefinition("ONE", "one"); //$NON-NLS-1$ //$NON-NLS-2$
@ -581,15 +581,15 @@ public class Scanner2Test extends BaseScanner2Test
try
{
initializeScanner(
"#ifndef FIVE \n" +
"#define FIVE 5\n" +
"#endif \n" +
"#ifndef TEN\n" +
"#define TEN 2 * FIVE\n" +
"#endif\n" +
"#if TEN != 10\n" +
"#define MISTAKE 1\n" +
"#error Five does not equal 10\n" +
"#ifndef FIVE \n" + //$NON-NLS-1$
"#define FIVE 5\n" + //$NON-NLS-1$
"#endif \n" + //$NON-NLS-1$
"#ifndef TEN\n" + //$NON-NLS-1$
"#define TEN 2 * FIVE\n" + //$NON-NLS-1$
"#endif\n" + //$NON-NLS-1$
"#if TEN != 10\n" + //$NON-NLS-1$
"#define MISTAKE 1\n" + //$NON-NLS-1$
"#error Five does not equal 10\n" + //$NON-NLS-1$
"#endif\n"); //$NON-NLS-1$
scanner.addDefinition("FIVE", "55"); //$NON-NLS-1$ //$NON-NLS-2$
validateEOF();
@ -1547,7 +1547,7 @@ public class Scanner2Test extends BaseScanner2Test
{
Callback callback = new Callback(ParserMode.QUICK_PARSE);
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();
//assertFalse( callback.problems.isEmpty() );
}
@ -1644,43 +1644,43 @@ public class Scanner2Test extends BaseScanner2Test
public void test__attribute__() throws Exception {
initializeScanner(
"#define __cdecl __attribute__((cdecl))\n" +
"__cdecl;");
"#define __cdecl __attribute__((cdecl))\n" + //$NON-NLS-1$
"__cdecl;"); //$NON-NLS-1$
validateToken(IToken.tSEMI);
validateEOF();
}
public void testUndef() throws Exception {
initializeScanner(
"#define A 5\n" +
"#define B 10\n" +
"#undef A\n" +
"A B");
validateIdentifier("A");
validateInteger("10");
"#define A 5\n" + //$NON-NLS-1$
"#define B 10\n" + //$NON-NLS-1$
"#undef A\n" + //$NON-NLS-1$
"A B"); //$NON-NLS-1$
validateIdentifier("A"); //$NON-NLS-1$
validateInteger("10"); //$NON-NLS-1$
validateEOF();
}
public void testWackyFunctionMacros() throws Exception {
initializeScanner(
"#define A(X) hi##X\n" +
"#define B(Y) A(Y)\n" +
"B(there)");
validateIdentifier("hithere");
"#define A(X) hi##X\n" + //$NON-NLS-1$
"#define B(Y) A(Y)\n" + //$NON-NLS-1$
"B(there)"); //$NON-NLS-1$
validateIdentifier("hithere"); //$NON-NLS-1$
validateEOF();
}
public void testSlashes() throws Exception {
initializeScanner("__q / __n");
validateIdentifier("__q");
initializeScanner("__q / __n"); //$NON-NLS-1$
validateIdentifier("__q"); //$NON-NLS-1$
validateToken(IToken.tDIV);
validateIdentifier("__n");
validateIdentifier("__n"); //$NON-NLS-1$
validateEOF();
}
public void testStringify() throws Exception {
initializeScanner("#define xS(s) #s\n#define S(s) xS(s)#define X hi\nS(X)");
validateString("hi");
initializeScanner("#define xS(s) #s\n#define S(s) xS(s)\n#define X hi\nS(X)"); //$NON-NLS-1$
validateString("hi"); //$NON-NLS-1$
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.quick.QuickParseASTFactory;
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;
@ -39,6 +40,8 @@ import org.eclipse.cdt.internal.core.parser.token.KeywordSets;
*/
public class ParserFactory {
public static final boolean USE_NEW_SCANNER = false;
private static IParserExtensionFactory extensionFactory = new ParserExtensionFactory( ExtensionDialect.GCC );
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 );
IParserLogService logService = ( log == null ) ? createDefaultLogService() : log;
ParserMode ourMode = ( (mode == null )? ParserMode.COMPLETE_PARSE : mode );
ISourceElementRequestor ourRequestor = (( requestor == null) ? new NullSourceElementRequestor() : requestor );
IScanner s = new Scanner( code, config, ourRequestor, ourMode, language, logService, extensionFactory.createScannerExtension(), workingCopies );
ISourceElementRequestor ourRequestor = (( requestor == null) ? new NullSourceElementRequestor() : requestor );
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;
}

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 )
throws ScannerException {
// 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;
return evaluateExpressionNew(expression, beginningOffset);
}