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

Patch for Victor Mozgin.

K&R Support.  God help us.
This commit is contained in:
John Camelon 2003-06-18 19:36:20 +00:00
parent f7b57711f9
commit 5ef908593a
14 changed files with 343 additions and 25 deletions

View file

@ -1,3 +1,14 @@
2003-06-16 Victor Mozgin
Implemented support for old K&R-style C function declarations.
Added oldKRParametersBegin() and oldKRParametersEnd() to DOMBuilder.
Added OldKRParameterDeclarationClause.java to dom/org/eclipse/cdt/internal/core/dom.
Added handling of OldKRParameterDeclarationClause to ParameterDeclarationClause.
2003-06-14 Victor Mozgin
Added support for pointers to members to DOMBuilder.
Added new kind of pointer operator : t_pointer_to_member (PointerOperator).
Added nameSpecifier field and set/get operations to PointerOperator.
2003-06-13 Andrew Niefer
Added search\org.eclipse.cdt.core.search
search\org.eclipse.cdt.internal.core.search
@ -6,11 +17,6 @@
with skeleton classes based on the JDT search as a beginning for
implementing C/CPP search.
2003-06-14 Victor Mozgin
Added support for pointers to members to DOMBuilder.
Added new kind of pointer operator : t_pointer_to_member (PointerOperator).
Added nameSpecifier field and set/get operations to PointerOperator.
2003-06-06 Sean Evoy
Added new interface, IResourceBuildInfo, so clients could

View file

@ -55,6 +55,23 @@ public class DOMBuilder implements IParserCallback, ISourceElementRequestor
public void argumentsEnd(Object parameterDeclarationClause) {
}
/**
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#oldKRParametersBegin()
*/
public Object oldKRParametersBegin( Object parameterDeclarationClause ) {
ParameterDeclarationClause clause = ((ParameterDeclarationClause)parameterDeclarationClause);
OldKRParameterDeclarationClause KRclause = new OldKRParameterDeclarationClause( clause );
domScopes.push(KRclause);
return KRclause;
}
/**
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#oldKRParametersEnd()
*/
public void oldKRParametersEnd(Object oldKRParameterDeclarationClause) {
domScopes.pop();
}
/**
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#classBegin(java.lang.String, org.eclipse.cdt.internal.core.newparser.Token)
*/
@ -318,6 +335,7 @@ public class DOMBuilder implements IParserCallback, ISourceElementRequestor
ParameterDeclaration d = (ParameterDeclaration)declaration;
d.getOwnerScope().addDeclaration(d);
}
/**
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#declaratorAbort(java.lang.Object, java.lang.Object)
*/

View file

@ -0,0 +1,40 @@
package org.eclipse.cdt.internal.core.dom;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* @author vmozgin
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class OldKRParameterDeclarationClause implements IScope {
/**
* @see org.eclipse.cdt.core.dom.IScope#addDeclaration(org.eclipse.cdt.internal.core.dom.Declaration)
*/
public void addDeclaration( Declaration declaration) {
declarations.add( declaration );
}
/**
* @see org.eclipse.cdt.core.dom.IScope#getDeclarations()
*/
public List getDeclarations() {
return Collections.unmodifiableList( declarations );
}
private List declarations = new LinkedList();
private ParameterDeclarationClause owner;
OldKRParameterDeclarationClause( ParameterDeclarationClause owner )
{
this.owner = owner;
this.owner.addOldKRParms( this );
}
}

View file

@ -37,4 +37,19 @@ public class ParameterDeclarationClause implements IScope {
this.owner = owner;
this.owner.addParms( this );
}
OldKRParameterDeclarationClause oldKRparms = null;
public void addOldKRParms( OldKRParameterDeclarationClause oldKRparms )
{
this.oldKRparms = oldKRparms;
}
/**
* Returns the parms.
* @return OldKRParameterDeclarationClause
*/
public OldKRParameterDeclarationClause getOldKRParms() {
return oldKRparms;
}
}

View file

@ -3,6 +3,14 @@
Added proper support for function pointers as parameters.
This fixes PR 38921 and 39002.
2003-06-16 Victor Mozgin
Implemented support for old K&R-style C function declarations.
Added oldKRParametersBegin() and oldKRParametersEnd() to IParserCallback.
Added getParameterTypes() with support of K&R to CModelBuilder.
Fixed ExpressionEvaluator and NullSourceElementRequestor for additions to IParserCallback.
Added handling of K&R syntax to the parser.
This fixes PR 7541, 35320 and 38434.
2003-06-14 Victor Mozgin
Fixed handling of parameter lists for typedefs for functions.
More errorhandling in CModelBuilder.

View file

@ -55,8 +55,11 @@ public interface IParserCallback {
public Object argumentsBegin( Object declarator );
public void argumentsEnd(Object parameterDeclarationClause);
public Object functionBodyBegin(Object declaration);
public Object oldKRParametersBegin( Object parameterDeclarationClause );
public void oldKRParametersEnd(Object oldKRParameterDeclarationClause);
public Object functionBodyBegin(Object declaration);
public void functionBodyEnd(Object functionBody);
public Object classSpecifierBegin(Object container, IToken classKey);

View file

@ -40,6 +40,7 @@ import org.eclipse.cdt.internal.core.dom.ITemplateParameterListOwner;
import org.eclipse.cdt.internal.core.dom.Inclusion;
import org.eclipse.cdt.internal.core.dom.Macro;
import org.eclipse.cdt.internal.core.dom.NamespaceDefinition;
import org.eclipse.cdt.internal.core.dom.OldKRParameterDeclarationClause;
import org.eclipse.cdt.internal.core.dom.ParameterDeclaration;
import org.eclipse.cdt.internal.core.dom.ParameterDeclarationClause;
import org.eclipse.cdt.internal.core.dom.PointerOperator;
@ -52,6 +53,7 @@ import org.eclipse.cdt.internal.core.parser.Name;
import org.eclipse.cdt.internal.core.parser.Parser;
import org.eclipse.core.resources.IProject;
public class CModelBuilder {
protected org.eclipse.cdt.internal.core.model.TranslationUnit translationUnit;
@ -872,8 +874,13 @@ public class CModelBuilder {
return arrayString.toString();
}
private String[] getParameterTypes(Declarator declarator)
{
return getParameterTypes(declarator, null);
}
private String[] getParameterTypes(Declarator declarator)
private String[] getParameterTypes(Declarator declarator, HashMap mapOfKRParams)
{
if (declarator == null) return null;
@ -886,7 +893,26 @@ public class CModelBuilder {
for (int j = 0; j < parameterList.size(); ++j) {
ParameterDeclaration param = (ParameterDeclaration) parameterList.get(j);
parameterTypes[j] = getType(param, (Declarator) param.getDeclarators().get(0));
Declarator decl = (Declarator) param.getDeclarators().get(0);
parameterTypes[j] = getType(param, decl);
if ( (mapOfKRParams != null)
&& (mapOfKRParams.size() > 0)
&& (decl.getName() == null))
{
// We have some K&R-style parameter declarations,
// and the current parameter has been declared with a single identifier,
// (like ...(argname)...)
// It has been parsed as a typename, so 'argname' is a name of the type,
// and parameter name is empty. But for this particular case,
// 'argname' is a name, and its type we have to lookup in the map
// of old K&R-style parameter declarations.
// If we can't find it, we keep parameter name in the signature
String oldKRParamType = (String)mapOfKRParams.get(parameterTypes[j]);
if (oldKRParamType != null) {
parameterTypes[j] = oldKRParamType;
}
}
}
}
@ -904,8 +930,42 @@ public class CModelBuilder {
}
currentDeclarator = currentDeclarator.getDeclarator();
}
HashMap mapOfKRParams = null;
if ( declarator != null
&& declarator.getParms() != null
&& declarator.getParms().getOldKRParms() != null) {
mapOfKRParams = new HashMap();
OldKRParameterDeclarationClause oldKRpdc = declarator.getParms().getOldKRParms();
List oldKRParameterList = oldKRpdc.getDeclarations();
for (int j = 0; j < oldKRParameterList.size(); ++j) {
if(oldKRParameterList.get(j) instanceof SimpleDeclaration) { // Must be
SimpleDeclaration declKR = (SimpleDeclaration)oldKRParameterList.get(j);
List declarators = declKR.getDeclarators();
Iterator d = declarators.iterator();
while (d.hasNext()) {
Declarator decl = (Declarator) d.next();
Name oldKRparamName = getDOMName(decl);
String oldKRparamType = getType(declKR, decl);
if ( (oldKRparamType != null)
&& (oldKRparamName != null)
&& (oldKRparamName.toString().length() > 0)
) {
mapOfKRParams.put(oldKRparamName.toString(), oldKRparamType);
}
}
}
}
}
return getParameterTypes(innermostPDCDeclarator);
return getParameterTypes(innermostPDCDeclarator, mapOfKRParams);
}
private String getParametersString(String[] parameterTypes)

View file

@ -201,6 +201,17 @@ public class ExpressionEvaluator implements IParserCallback {
*/
public void argumentsEnd(Object parameterDeclarationClause) {
}
/**
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#oldKRParametersBegin()
*/
public Object oldKRParametersBegin( Object parameterDeclarationClause ) {
return null;
}
/**
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#oldKRParametersEnd()
*/
public void oldKRParametersEnd(Object oldKRParameterDeclarationClause) {
}
/**
* @see org.eclipse.cdt.internal.core.parser.IParserCallback#functionBodyBegin()
*/

View file

@ -397,6 +397,19 @@ public class NullSourceElementRequestor implements ISourceElementRequestor, IPar
*/
public void argumentsEnd(Object parameterDeclarationClause) {
}
/**
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#oldKRParametersBegin()
*/
public Object oldKRParametersBegin( Object parameterDeclarationClause ) {
return null;
}
/**
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#oldKRParametersEnd()
*/
public void oldKRParametersEnd(Object oldKRParameterDeclarationClause) {
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IParserCallback#functionBodyBegin(java.lang.Object)

View file

@ -648,13 +648,13 @@ c, quickParse);
IToken mark = mark();
try
{
simpleDeclaration( container, true, scope ); // try it first with the original strategy
simpleDeclaration( container, true, false, scope ); // try it first with the original strategy
}
catch( Backtrack bt)
{
// did not work
backup( mark );
simpleDeclaration( container, false, scope ); // try it again with the second strategy
simpleDeclaration( container, false, false, scope ); // try it again with the second strategy
}
}
}
@ -749,10 +749,11 @@ c, quickParse);
* - work in functionTryBlock
*
* @param container IParserCallback object which serves as the owner scope for this declaration.
* @param tryConstructor true == take strategy1 (constructor ) : false == take strategy 2 ( pointer to function)
* @param tryConstructor true == take strategy1 (constructor ) : false == take strategy 2 ( pointer to function)
* @param forKR Is this for K&R-style parameter declaration (true) or simple declaration (false)
* @throws Backtrack request a backtrack
*/
protected void simpleDeclaration( Object container, boolean tryConstructor, IASTScope scope ) throws Backtrack {
protected void simpleDeclaration( Object container, boolean tryConstructor, boolean forKR, IASTScope scope ) throws Backtrack {
Object simpleDecl = null;
try{ simpleDecl = callback.simpleDeclarationBegin( container, LA(1));} catch( Exception e ) {}
declSpecifierSeq(simpleDecl, false, tryConstructor, scope);
@ -780,9 +781,11 @@ c, quickParse);
consume(IToken.tSEMI);
break;
case IToken.tCOLON:
if (forKR) throw backtrack;
ctorInitializer(declarator);
// Falling through on purpose
case IToken.tLBRACE:
if (forKR) throw backtrack;
Object function = null;
try{ function = callback.functionBodyBegin(simpleDecl ); } catch( Exception e ) {}
if (quickParse) {
@ -812,6 +815,7 @@ c, quickParse);
try{ callback.simpleDeclarationEnd(simpleDecl, (Token)lastToken);} catch( Exception e ) {}
}
/**
* This method parses a constructor chain
* ctorinitializer: : meminitializerlist
@ -1486,6 +1490,7 @@ c, quickParse);
* (exceptionSpecification)*
* | directDeclarator "[" (constantExpression)? "]"
* | "(" declarator")"
* | directDeclarator "(" parameterDeclarationClause ")" (oldKRParameterDeclaration)*
*
* declaratorId
* : name
@ -1692,11 +1697,20 @@ c, quickParse);
// this is most likely the definition of the constructor
return declarator;
}
// const-volatile marker on the method
IToken beforeCVModifier = mark();
IToken cvModifier = null;
IToken afterCVModifier = beforeCVModifier;
// const-volatile
// 2 options: either this is a marker for the method,
// or it might be the beginning of old K&R style parameter declaration, see
// void getenv(name) const char * name; {}
// This will be determined further below
if( LT(1) == IToken.t_const || LT(1) == IToken.t_volatile )
{
try{ callback.declaratorCVModifier( declarator, consume() );} catch( Exception e ) {}
cvModifier = consume();
afterCVModifier = mark();
}
//check for throws clause here
@ -1737,7 +1751,35 @@ c, quickParse);
consume( IToken.tINTEGER);
try{ callback.declaratorPureVirtual( declarator ); } catch( Exception e ) { }
}
if (afterCVModifier != currToken || LT(1) == IToken.tSEMI) {
// There were C++-specific clauses after const/volatile modifier
// Then it is a marker for the method
try{ callback.declaratorCVModifier( declarator, cvModifier );} catch( Exception e ) {}
// In this case (method) we can't expect K&R parameter declarations,
// but we'll check anyway, for errorhandling
} else {
// let's try this modifier as part of K&R parameter declaration
backup(beforeCVModifier);
}
if (LT(1) != IToken.tSEMI) {
// try K&R-style parameter declarations
Object oldKRParameterDeclarationClause = null;
try { oldKRParameterDeclarationClause = callback.oldKRParametersBegin(clause);} catch( Exception e ) {}
try {
do {
simpleDeclaration(oldKRParameterDeclarationClause, false, true, null);
} while (LT(1) != IToken.tLBRACE);
} catch (Exception e) {
// Something is wrong,
// this is not a proper K&R declaration clause
backup(afterCVModifier);
}
try{ callback.oldKRParametersEnd(oldKRParameterDeclarationClause);} catch( Exception e ) {}
}
}
break;
case IToken.tLBRACKET:

View file

@ -1,6 +1,10 @@
2003-06-17 Brent Nicolle
Added Interface tests of IStructure.java.
2003-06-17 Victor Mozgin
Added DeclaratorsTests.java (invocation in AllCoreTests).
Added DeclaratorsTests.cpp to org.eclipse.cdt.core.model.tests.resources.
2003-06-16 Vladimir Hirsl
Added /build, /parser, /failures and /suite directories to the library.
Copied resources from /model/org.eclipse.cdt.core.model.tests.resources
@ -8,9 +12,9 @@
Added class AISResultPrinter to format test results.
Class AutomatedIntegrationSuite now implements IPlatformRunnable.
2003-06-17 Victor Mozgin
Added DeclaratorsTests.java (invocation in AllCoreTests).
Added DeclaratorsTests.cpp to org.eclipse.cdt.core.model.tests.resources.
2003-06-16 Victor Mozgin
Added testOldKRFunctionDeclarations() to DOMTests.
Added testKRFunctionDeclarations() to TranslationUnitTests.
2003-06-14 Victor Mozgin
Moved testBugSingleton192() from LokiFailures to DOMTests.

View file

@ -16,6 +16,7 @@ import junit.framework.TestSuite;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IFunction;
import org.eclipse.cdt.core.model.IInclude;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.testplugin.CProjectHelper;
@ -51,8 +52,8 @@ public class TranslationUnitTests extends TestCase {
*/
String[] expectedStringList= {"stdio.h", "unistd.h", "func2p",
"globalvar", "myenum", "mystruct", "mystruct_t", "myunion", "mytype",
"func1", "func2", "main", "func3"};
int[] expectedLines={ 12,14,17,20,23,28,32,35,42,47,53,58,65};
"func1", "func2", "main", "func3", "KRFunction"};
int[] expectedLines={ 12,14,17,20,23,28,32,35,42,47,53,58,65,70};
/* This is a list of that the types of the above list of elements is
* expected to be.
*/
@ -60,7 +61,8 @@ public class TranslationUnitTests extends TestCase {
ICElement.C_FUNCTION_DECLARATION, ICElement.C_VARIABLE,
ICElement.C_ENUMERATION, ICElement.C_STRUCT, ICElement.C_TYPEDEF,
ICElement.C_UNION, ICElement.C_TYPEDEF, ICElement.C_FUNCTION,
ICElement.C_FUNCTION, ICElement.C_FUNCTION,ICElement.C_FUNCTION};
ICElement.C_FUNCTION, ICElement.C_FUNCTION,ICElement.C_FUNCTION,
ICElement.C_FUNCTION};
/**
@ -163,6 +165,7 @@ public class TranslationUnitTests extends TestCase {
suite.addTest(new TranslationUnitTests("testGetElement"));
suite.addTest(new TranslationUnitTests("testBug23478A"));
suite.addTest(new TranslationUnitTests("testBug23478B"));
suite.addTest(new TranslationUnitTests("testKRFunctionDeclarations"));
// TODO: suite.addTest(new TranslationUnitTests("testGetElementAtLine"));
return suite;
}
@ -358,5 +361,17 @@ public class TranslationUnitTests extends TestCase {
assertTrue(myExp.getMissingString(), myExp.gotAll());
assertTrue(myExp.getExtraString(), !myExp.gotExtra());
}
*/
*/
/***
* Simple sanity test for old K&R-style C function declaration
*/
public void testKRFunctionDeclarations() throws CModelException {
ITranslationUnit myTranslationUnit = CProjectHelper.findTranslationUnit(testProject,"exetest.c");
assertTrue(myTranslationUnit.getElement("KRFunction") instanceof IFunction);
IFunction myKRFunction = (IFunction)myTranslationUnit.getElement("KRFunction");
assertEquals(myKRFunction.getSignature(), "KRFunction(const char*, int(*)(float), parm3)");
assertEquals(myKRFunction.getReturnType(), "bool");
}
}

View file

@ -66,3 +66,9 @@ void func3()
{
printf("This is not really here\n");
}
bool KRFunction( parm1, parm2, parm3 )
const char* parm1;
int (*parm2)(float);
{
}

View file

@ -29,6 +29,7 @@ import org.eclipse.cdt.internal.core.dom.Inclusion;
import org.eclipse.cdt.internal.core.dom.LinkageSpecification;
import org.eclipse.cdt.internal.core.dom.Macro;
import org.eclipse.cdt.internal.core.dom.NamespaceDefinition;
import org.eclipse.cdt.internal.core.dom.OldKRParameterDeclarationClause;
import org.eclipse.cdt.internal.core.dom.ParameterDeclaration;
import org.eclipse.cdt.internal.core.dom.ParameterDeclarationClause;
import org.eclipse.cdt.internal.core.dom.PointerOperator;
@ -2159,4 +2160,80 @@ public class DOMTests extends BaseDOMTest {
parse("template <class B,C> A::nested::operator int() {} ");
}
public void testOldKRFunctionDeclarations() throws Exception
{
// Parse and get the translaton unit
Writer code = new StringWriter();
code.write("bool myFunction( parm1, parm2, parm3 )\n");
code.write("const char* parm1;\n");
code.write("int (*parm2)(float);\n");
code.write("{}");
TranslationUnit translationUnit = parse(code.toString());
// Get the declaration
List declarations = translationUnit.getDeclarations();
assertEquals(1, declarations.size());
SimpleDeclaration simpleDeclaration = (SimpleDeclaration)declarations.get(0);
assertEquals( simpleDeclaration.getDeclSpecifier().getType(), DeclSpecifier.t_bool );
List declarators = simpleDeclaration.getDeclarators();
assertEquals( 1, declarators.size() );
Declarator functionDeclarator = (Declarator)declarators.get( 0 );
assertEquals( functionDeclarator.getName().toString(), "myFunction" );
ParameterDeclarationClause pdc = functionDeclarator.getParms();
assertNotNull( pdc );
List parameterDecls = pdc.getDeclarations();
assertEquals( 3, parameterDecls.size() );
ParameterDeclaration parm1 = (ParameterDeclaration)parameterDecls.get( 0 );
assertNotNull( parm1.getDeclSpecifier().getName() );
assertEquals( "parm1", parm1.getDeclSpecifier().getName().toString() );
List parm1Decls = parm1.getDeclarators();
assertEquals( 1, parm1Decls.size() );
ParameterDeclaration parm2 = (ParameterDeclaration)parameterDecls.get( 1 );
assertNotNull( parm2.getDeclSpecifier().getName() );
assertEquals( "parm2", parm2.getDeclSpecifier().getName().toString() );
List parm2Decls = parm2.getDeclarators();
assertEquals( 1, parm2Decls.size() );
ParameterDeclaration parm3 = (ParameterDeclaration)parameterDecls.get( 2 );
assertNotNull( parm3.getDeclSpecifier().getName() );
assertEquals( "parm3", parm3.getDeclSpecifier().getName().toString() );
List parm3Decls = parm3.getDeclarators();
assertEquals( 1, parm3Decls.size() );
OldKRParameterDeclarationClause clause = pdc.getOldKRParms();
assertNotNull( clause );
assertEquals( clause.getDeclarations().size(), 2 );
SimpleDeclaration decl1 = (SimpleDeclaration)clause.getDeclarations().get(0);
assertEquals( decl1.getDeclarators().size(), 1 );
assertTrue(decl1.getDeclSpecifier().isConst());
assertFalse(decl1.getDeclSpecifier().isVolatile());
assertEquals( decl1.getDeclSpecifier().getType(), DeclSpecifier.t_char);
Declarator declarator1 = (Declarator)decl1.getDeclarators().get( 0 );
assertEquals( declarator1.getName().toString(), "parm1" );
Expression initValue1 = declarator1.getExpression();
List ptrOps1 = declarator1.getPointerOperators();
assertNotNull( ptrOps1 );
assertEquals( 1, ptrOps1.size() );
PointerOperator po1 = (PointerOperator)ptrOps1.get(0);
assertNotNull( po1 );
assertFalse( po1.isConst() );
assertFalse( po1.isVolatile() );
assertEquals( po1.getType(), PointerOperator.t_pointer );
SimpleDeclaration declaration = (SimpleDeclaration)clause.getDeclarations().get(1);
assertEquals( declaration.getDeclSpecifier().getType(), DeclSpecifier.t_int );
assertEquals( declaration.getDeclarators().size(), 1);
assertNull( ((Declarator)declaration.getDeclarators().get(0)).getName() );
assertNotNull( ((Declarator)declaration.getDeclarators().get(0)).getDeclarator() );
assertEquals( ((Declarator)declaration.getDeclarators().get(0)).getDeclarator().getName().toString(), "parm2" );
ParameterDeclarationClause clause2 = ((Declarator)declaration.getDeclarators().get(0)).getParms();
assertEquals( clause2.getDeclarations().size(), 1 );
assertEquals( ((ParameterDeclaration)clause2.getDeclarations().get(0)).getDeclarators().size(), 1 );
assertNull( ((Declarator)((ParameterDeclaration)clause2.getDeclarations().get(0)).getDeclarators().get(0)).getName() );
assertEquals( ((ParameterDeclaration)clause2.getDeclarations().get(0)).getDeclSpecifier().getType(), DeclSpecifier.t_float );
}
}