diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Parser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Parser.java index 270bf790ca1..e6756160f98 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Parser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/Parser.java @@ -106,13 +106,17 @@ c, quick); // Mark as failure and try to reach a recovery point failParse(); - if (lastBacktrack != null && lastBacktrack == LA(1)) { - // we haven't progressed from the last backtrack - // try and find tne next definition - consumeToNextSemicolon(); - } else { - // start again from here - lastBacktrack = LA(1); + try { + if (lastBacktrack != null && lastBacktrack == LA(1)) { + // we haven't progressed from the last backtrack + // try and find tne next definition + consumeToNextSemicolon(); + } else { + // start again from here + lastBacktrack = LA(1); + } + } catch (EndOfFile e){ + break; } } catch( Exception e ) @@ -2280,4 +2284,11 @@ c, quick); { return scanner.getLineNumberForOffset(offset); } + + public int getLastLineNumber(){ + if( lastToken != null ){ + return scanner.getLineNumberForOffset( lastToken.offset ); + } + return -1; + } } diff --git a/core/org.eclipse.cdt.ui.tests/ChangeLog b/core/org.eclipse.cdt.ui.tests/ChangeLog index 8a15c1fba1f..0879acf0619 100644 --- a/core/org.eclipse.cdt.ui.tests/ChangeLog +++ b/core/org.eclipse.cdt.ui.tests/ChangeLog @@ -1,3 +1,8 @@ +2003-04-17 Andrew Niefer + Added AutomatedTest + Added resources.cFiles + Added resources.cppFiles + 2003-04-16 John Camelon Added DOMTests::testBug36532(). Added DOMTests::testBug36432(). diff --git a/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/.cvsignore b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/.cvsignore new file mode 100644 index 00000000000..0cd655ecc77 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/.cvsignore @@ -0,0 +1 @@ +AutomatedTest.properties diff --git a/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cFiles/Simple.c b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cFiles/Simple.c new file mode 100644 index 00000000000..16bc625fe55 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cFiles/Simple.c @@ -0,0 +1,43 @@ +#include + +const SimpleStruct simpleStruct = +{ + 1 + , "mySimple" + , 0.1232 +}; + +#define SIZEOF( A, B ) sizeof( A.B ) + +const OtherStruct array[] = +{ + { +#if FOO + "foo" +#else + "bar" +#endif + , SIZEOF( simpleStruct, num ) + , &t_int + , 0 + } + , { + "name" + , SIZEOF( simpleStruct, floatnum ) + , &t_float + , 1 + } +}; + + +void SimpleStruct_construct( struct SimpleStruct * const this ) +{ + this->num = 1; + this->name = "boo"; + this->floatNum = 1.5; +} + +int ConnectParams_doSomething( const struct SimpleStruct * const this ) +{ + return 1; +} diff --git a/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cFiles/Simple.h b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cFiles/Simple.h new file mode 100644 index 00000000000..b5012c10f5b --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cFiles/Simple.h @@ -0,0 +1,17 @@ +#ifndef SIMPLE_H +#define SIMPLE_H + +struct SimpleStruct +{ + int num; + char name[ ]; + float floatNum; +}; + + +void SimpleStruct_construct( struct SimpleStruct * const this ); + +int SimpleStruct_doSomething( const struct SimpleStruct * const this ); + +#endif /* SIMPLE_H */ + diff --git a/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cppFiles/Simple.cpp b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cppFiles/Simple.cpp new file mode 100644 index 00000000000..471f78e6c91 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cppFiles/Simple.cpp @@ -0,0 +1,37 @@ +#include + +#include + +#define NULL (void *)0 + +SimpleClass::SimpleClass( void ) +{ + init( NULL ); +} + +SimpleClass::~SimpleClass( void ) +{ +} + +SimpleClass::SimpleClass( const SimpleClass & arg ) +{ + //TODO: copy constructor +} + +SimpleClass & SimpleClass::operator=( const SimpleClass & arg ) +{ + if( this != &arg ) + { + } + return *this; +} + + +void SimpleClass::init( void * foo) +{ +} + +InnerStruct & SimpleClass::getInner( void ) +{ + return inner; +} diff --git a/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cppFiles/Simple.h b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cppFiles/Simple.h new file mode 100644 index 00000000000..b385fcd46ff --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/resources/cppFiles/Simple.h @@ -0,0 +1,32 @@ +#ifndef SIMPLE_H +#define SIMPLE_H + +class OtherClass; + +class SimpleClass +{ +public: + SimpleClass( void ); + SimpleClass( const SimpleClass & arg ); + + virtual ~SimpleClass( void ); + + SimpleClass & operator=( const SimpleClass & arg ); + +private: + struct InnerStruct + { + inline InnerStruct( int a ){ _a = a; } + inline ~InnerStruct( void ){} + unsigned int _a; + }; + + InnerStruct inner; + + void init( void * ); + +public: + InnerStruct & getInner( void ); +}; + +#endif /* SIMPLE_H */ diff --git a/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/tests/AutomatedTest.java b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/tests/AutomatedTest.java new file mode 100644 index 00000000000..bca8b08f026 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/parser/org/eclipse/cdt/core/parser/tests/AutomatedTest.java @@ -0,0 +1,267 @@ +/******************************************************************************* + * Copyright (c) 2001 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v0.5 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v05.html + * + * Contributors: + * IBM Corp. - Rational Software - initial implementation + ******************************************************************************/ + +package org.eclipse.cdt.core.parser.tests; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FilenameFilter; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.NoSuchElementException; +import java.util.Properties; +import java.util.Set; +import java.util.StringTokenizer; + +import org.eclipse.core.runtime.Path; + +import org.eclipse.cdt.internal.core.parser.IParserCallback; +import org.eclipse.cdt.internal.core.parser.NullParserCallback; +import org.eclipse.cdt.internal.core.parser.Parser; + +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + + + +/** + * @author aniefer + * + * To change the template for this generated type comment go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ +public class AutomatedTest extends TestCase { + public AutomatedTest(String name){ + super(name); + } + + public void doFile() throws Throwable { + assertNotNull( fileList ); + + File file = null; + Parser parser = null; + + try{ + file = (File)fileList.removeFirst(); + FileInputStream stream = new FileInputStream( file ); + + String filePath = file.getCanonicalPath(); + String nature = (String)natures.get( filePath ); + + boolean cppNature = nature.equalsIgnoreCase("cpp"); + + parser = new Parser( stream, nullCallback, true); + parser.setCppNature( cppNature ); + + assertTrue( parser.parse() ); + } + catch( Throwable e ) + { + String output = null; + if( e instanceof AssertionFailedError ){ + output = file.getCanonicalPath() + ": Parse failed on line "; + output += parser.getLastLineNumber() + "\n"; + } else { + StackTraceElement frames[] = e.getStackTrace(); + output = file.getCanonicalPath() + ": " + e.getClass().toString(); + output += " on line " + parser.getLastLineNumber() + "\n"; + output += "\t" + "at " + frames[0].getClassName() + "." + frames[0].getMethodName() + "\n"; + output += "\t" + "at " + frames[1].getClassName() + "." + frames[1].getMethodName() + "\n"; + } + if( report != null ){ + report.write( output.getBytes() ); + } + + fail( output ); + } + } + + public void reportFailed(){ + fail( "Unable to open " + outputFile + "for output of results." ); + } + + public void propertiesFailed(){ + fail( "Unable to load properties file." ); + } + + protected void runTest() throws Throwable { + String name = getName(); + + if( name.equals("propertiesFailed") ) + propertiesFailed(); + else if ( name.equals("reportFailed") ) + reportFailed(); + else + doFile(); + } + + public static Test suite() + { + TestSuite suite = new TestSuite(); + + try{ + loadProperties(); + } catch( Exception e ){ + suite.addTest( new AutomatedTest( "propertiesFailed") ); + } + + if( outputFile != null && !outputFile.equals("") ){ + try{ + + File output = new File( outputFile ); + + if( output.exists() ){ + output.delete(); + } + + output.createNewFile(); + + report = new FileOutputStream( output ); + + } catch( Exception e ) { + suite.addTest( new AutomatedTest( "reportFailed" ) ); + } + } + + Set keys = testSources.keySet(); + Iterator iter = keys.iterator(); + int size = keys.size(); + String item = null; + for( int i = size; i > 0; i-- ) + { + item = (String) iter.next(); + File file = new File( item ); + if( file.exists() ){ + defaultNature = (String) testSources.get( item ); + fillSuite( suite, file ); + } + } + + return suite; + } + private static void fillSuite( TestSuite suite, File path ){ + File files[] = null; + if( path.isFile() ){ + files = new File[ 1 ]; + files[0] = path; + } + else + files = path.listFiles(); + + File file = null; + String filePath = null; + int i = 0; + try{ + file = files[ i++ ]; + while( file != null ) + { + if( file.isDirectory() ) + fillSuite( suite, file ); + else if( file.isFile() && nameFilter.accept( file.getParentFile(), file.getName() ) ){ + try{ + filePath = file.getCanonicalPath(); + } catch ( Exception e ){ + continue; + } + + if( filePath.endsWith(".cpp") || filePath.endsWith(".hpp") || + filePath.endsWith(".hxx") || filePath.endsWith(".hh") ) + { + natures.put( filePath, "cpp" ); + } else if( filePath.endsWith(".c") ){ + natures.put( filePath, "c" ); + } else { + natures.put( filePath, defaultNature ); + } + + fileList.add( file ); + suite.addTest( new AutomatedTest( file.getName() ) ); + } + file = files[ i++ ]; + } + } catch( ArrayIndexOutOfBoundsException e ){ + //done + } + } + + protected void tearDown () throws Exception { + if( fileList != null && fileList.size() == 0 && report != null ){ + report.flush(); + report.close(); + } + } + + static private void loadProperties() throws Exception{ + String resourcePath = org.eclipse.core.runtime.Platform.getPlugin("org.eclipse.cdt.ui.tests").find(new Path("/")).getFile(); + resourcePath += "/parser/org/eclipse/cdt/core/parser/resources"; + + try{ + FileInputStream propertiesIn = new FileInputStream( resourcePath + "/AutomatedTest.properties"); + properties.load( propertiesIn ); + + outputFile = properties.getProperty( "outputFile", "" ); + String sourceInfo = properties.getProperty( "source", "" ); + if( sourceInfo.equals("") ) + throw new FileNotFoundException(); + else{ + StringTokenizer tokenizer = new StringTokenizer( sourceInfo, "," ); + String str = null, val = null; + try{ + while( tokenizer.hasMoreTokens() ){ + str = tokenizer.nextToken().trim(); + val = tokenizer.nextToken().trim(); + + testSources.put( str, val ); + } + } catch ( NoSuchElementException e ){ + //only way to get here is to have a missing val, assume cpp for that str + testSources.put( str, "cpp" ); + } + + } + } catch ( FileNotFoundException e ){ + testSources.put( resourcePath + "/cppFiles", "cpp" ); + testSources.put( resourcePath + "/cFiles", "c" ); + } + } + + private static LinkedList fileList = new LinkedList(); + private static FilenameFilter nameFilter = new Filter(); + private static FileOutputStream report = null; + private static IParserCallback nullCallback = new NullParserCallback(); + private static Properties properties = new Properties(); + private static String defaultNature; + private static String outputFile = null; + private static HashMap testSources = new HashMap(); + private static HashMap natures = new HashMap(); + + static private class Filter implements FilenameFilter + { + public boolean accept(File dir, String name) { + if( name.endsWith(".cpp") || + name.endsWith(".c") || + name.endsWith(".cc") || + name.endsWith(".h") || + name.endsWith(".hh") || + name.endsWith(".hxx")) + { + return true; + } + else + return false; + } + } +}