1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Fixes parsing of 'void (proto) ();'

This commit is contained in:
Markus Schorn 2007-04-04 09:36:32 +00:00
parent 0f1f113134
commit 9bcb71d896
6 changed files with 133 additions and 92 deletions

View file

@ -16,10 +16,8 @@ import junit.framework.TestSuite;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IFunction;
import org.eclipse.cdt.core.model.IFunctionDeclaration;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.ITypeDef;
import org.eclipse.cdt.core.model.IVariable;
import org.eclipse.cdt.core.model.tests.IntegratedCModelTest;
import org.eclipse.cdt.core.tests.FailingTest;
@ -57,58 +55,14 @@ public class FailedDeclaratorsTest extends IntegratedCModelTest
*/
public static Test suite() {
TestSuite suite= new TestSuite("FailedDeclaratorsTest");
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0002"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0003"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0006"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0007"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0011"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0013"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0014"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0016"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0017"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0023"), 40768));
suite.addTest(new FailingTest(new FailedDeclaratorsTest("testDeclarators_0024"), 40768));
return suite;
}
public void testDeclarators_0002() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0002");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_FUNCTION_DECLARATION);
IFunctionDeclaration decl = (IFunctionDeclaration)element;
assertEquals(decl.getSignature(), "decl_0002(char)");
assertEquals(decl.getReturnType(), "void");
}
public void testDeclarators_0003() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0003");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_FUNCTION_DECLARATION);
IFunctionDeclaration decl = (IFunctionDeclaration)element;
assertEquals(decl.getSignature(), "decl_0003(char)");
assertEquals(decl.getReturnType(), "void");
}
public void testDeclarators_0006() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0006");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_VARIABLE);
IVariable decl = (IVariable)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0007() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0007");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_VARIABLE);
IVariable decl = (IVariable)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0011() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0011");
@ -144,25 +98,7 @@ public class FailedDeclaratorsTest extends IntegratedCModelTest
ITypeDef decl = (ITypeDef)element;
assertEquals(decl.getTypeName(), "void*()(char)");
}
public void testDeclarators_0016() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0016");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_TYPEDEF);
ITypeDef decl = (ITypeDef)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0017() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0017");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_TYPEDEF);
ITypeDef decl = (ITypeDef)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0023() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0023");
@ -172,14 +108,4 @@ public class FailedDeclaratorsTest extends IntegratedCModelTest
assertEquals(decl.getSignature(), "decl_0023(int)");
assertEquals(decl.getReturnType(), "void(*(*))(char)");
}
public void testDeclarators_0024() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0024");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_VARIABLE);
IVariable decl = (IVariable)element;
assertEquals(decl.getTypeName(), "void(*(*(*)(int))(float))(char)");
}
}

View file

@ -71,6 +71,26 @@ public class DeclaratorsTests extends IntegratedCModelTest {
assertEquals(decl.getReturnType(), "void");
}
public void testDeclarators_0002() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0002");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_FUNCTION_DECLARATION);
IFunctionDeclaration decl = (IFunctionDeclaration)element;
assertEquals(decl.getSignature(), "decl_0002(char)");
assertEquals(decl.getReturnType(), "void");
}
public void testDeclarators_0003() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0003");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_FUNCTION_DECLARATION);
IFunctionDeclaration decl = (IFunctionDeclaration)element;
assertEquals(decl.getSignature(), "decl_0003(char)");
assertEquals(decl.getReturnType(), "void");
}
public void testDeclarators_0004() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0004");
@ -90,6 +110,25 @@ public class DeclaratorsTests extends IntegratedCModelTest {
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0006() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0006");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_VARIABLE);
IVariable decl = (IVariable)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0007() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0007");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_VARIABLE);
IVariable decl = (IVariable)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0015() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0015");
@ -98,6 +137,33 @@ public class DeclaratorsTests extends IntegratedCModelTest {
ITypeDef decl = (ITypeDef)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0016() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0016");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_TYPEDEF);
ITypeDef decl = (ITypeDef)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0017() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0017");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_TYPEDEF);
ITypeDef decl = (ITypeDef)element;
assertEquals(decl.getTypeName(), "void(*)(char)");
}
public void testDeclarators_0024() throws CModelException {
ITranslationUnit tu = getTU();
ICElement element = tu.getElement("decl_0024");
assertNotNull(element);
assertEquals(element.getElementType(), ICElement.C_VARIABLE);
IVariable decl = (IVariable)element;
assertEquals(decl.getTypeName(), "void(*(*(*)(int))(float))(char)");
}
public void testDeclarators_0031() throws CModelException {
ITranslationUnit tu = getTU();
@ -106,6 +172,5 @@ public class DeclaratorsTests extends IntegratedCModelTest {
assertEquals(element.getElementType(), ICElement.C_VARIABLE);
IVariable decl = (IVariable)element;
assertEquals(decl.getTypeName(), "int(*)(char(*)(bool))");
}
}
}

View file

@ -236,10 +236,20 @@ public class ASTStringUtil {
}
final IASTDeclarator nestedDeclarator= declarator.getNestedDeclarator();
if (nestedDeclarator != null) {
buffer.append(Keywords.cpLPAREN);
appendDeclaratorString(buffer, nestedDeclarator, addParams);
trimRight(buffer);
buffer.append(Keywords.cpRPAREN);
StringBuffer tmp= new StringBuffer();
appendDeclaratorString(tmp, nestedDeclarator, addParams);
trimRight(tmp);
final int tmpLength= tmp.length();
if (tmpLength > 0) {
if (tmp.charAt(0) != Keywords.cpLPAREN[0] || tmp.charAt(tmpLength-1) != Keywords.cpRPAREN[0]) {
buffer.append(Keywords.cpLPAREN);
buffer.append(tmp);
buffer.append(Keywords.cpRPAREN);
}
else {
buffer.append(tmp);
}
}
}
if (!addParams) {
return buffer;

View file

@ -574,14 +574,24 @@ public class CModelBuilder2 implements IContributedModelBuilder {
return createTypeDef(parent, declSpecifier, declarator);
}
if (declarator != null) {
IASTDeclarator nestedDeclarator= declarator.getNestedDeclarator();
if (nestedDeclarator == null && declarator instanceof IASTFunctionDeclarator) {
if (declarator instanceof IASTFunctionDeclarator && !hasNestedPointerOperators(declarator)) {
return createFunctionDeclaration(parent, declSpecifier, (IASTFunctionDeclarator)declarator, isTemplate);
}
}
return createVariable(parent, declSpecifier, declarator, isTemplate);
}
private boolean hasNestedPointerOperators(IASTDeclarator declarator) {
declarator= declarator.getNestedDeclarator();
while (declarator != null) {
if (declarator.getPointerOperators().length > 0) {
return true;
}
declarator= declarator.getNestedDeclarator();
}
return false;
}
private void createNamespace(Parent parent, ICPPASTNamespaceDefinition declaration) throws CModelException, DOMException{
// create element
final String type= Keywords.NAMESPACE;

View file

@ -831,6 +831,21 @@ public class CVisitor {
while( declarator.getNestedDeclarator() != null )
declarator = declarator.getNestedDeclarator();
IASTFunctionDeclarator funcDeclarator= null;
IASTNode node= declarator;
do {
if (node instanceof IASTFunctionDeclarator) {
funcDeclarator= (IASTFunctionDeclarator) node;
break;
}
if (((IASTDeclarator) node).getPointerOperators().length > 0 ||
node.getPropertyInParent() != IASTDeclarator.NESTED_DECLARATOR) {
break;
}
node= node.getParent();
}
while (node instanceof IASTDeclarator);
ICScope scope = (ICScope) getContainingScope( parent );
ASTNodeProperty prop = parent.getPropertyInParent();
@ -865,19 +880,19 @@ public class CVisitor {
}
} catch (DOMException e) {
}
} else if( declarator instanceof IASTFunctionDeclarator ){
} else if( funcDeclarator != null ){
if( binding != null ) {
if( binding instanceof IFunction ){
IFunction function = (IFunction) binding;
if( function instanceof CFunction )
((CFunction)function).addDeclarator( (IASTFunctionDeclarator) declarator );
((CFunction)function).addDeclarator( funcDeclarator );
return function;
}
binding = new ProblemBinding( name, IProblemBinding.SEMANTIC_INVALID_OVERLOAD, name.toCharArray() );
} else if( parent instanceof IASTSimpleDeclaration && ((IASTSimpleDeclaration) parent).getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_typedef)
binding = new CTypedef( name );
else
binding = new CFunction( (IASTFunctionDeclarator) declarator );
binding = new CFunction( funcDeclarator );
} else if( parent instanceof IASTSimpleDeclaration ){
IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) parent;
if( simpleDecl.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_typedef ){

View file

@ -469,6 +469,21 @@ public class CPPVisitor {
while( declarator.getNestedDeclarator() != null )
declarator = declarator.getNestedDeclarator();
IASTFunctionDeclarator funcDeclarator= null;
IASTNode node= declarator;
do {
if (node instanceof IASTFunctionDeclarator) {
funcDeclarator= (IASTFunctionDeclarator) node;
break;
}
if (((IASTDeclarator) node).getPointerOperators().length > 0 ||
node.getPropertyInParent() != IASTDeclarator.NESTED_DECLARATOR) {
break;
}
node= node.getParent();
}
while (node instanceof IASTDeclarator);
IASTName name = declarator.getName();
if( name instanceof ICPPASTQualifiedName ){
IASTName [] ns = ((ICPPASTQualifiedName)name).getNames();
@ -549,10 +564,10 @@ public class CPPVisitor {
return new ProblemBinding( name, IProblemBinding.SEMANTIC_INVALID_REDECLARATION, name.toCharArray() );
}
binding = new CPPTypedef( name );
} else if( declarator instanceof ICPPASTFunctionDeclarator ){
} else if( funcDeclarator != null ){
if( binding instanceof ICPPInternalBinding && binding instanceof IFunction ){
IFunction function = (IFunction) binding;
if( CPPSemantics.isSameFunction( function, declarator ) ){
if( CPPSemantics.isSameFunction( function, funcDeclarator ) ){
ICPPInternalBinding internal = (ICPPInternalBinding) function;
if( parent instanceof IASTSimpleDeclaration )
internal.addDeclaration( name );
@ -587,15 +602,15 @@ public class CPPVisitor {
}
if( scope instanceof ICPPClassScope ){
if( isConstructor( scope, declarator) )
if( isConstructor( scope, funcDeclarator) )
binding = template ? (ICPPConstructor) new CPPConstructorTemplate( name )
: new CPPConstructor( (ICPPASTFunctionDeclarator) declarator );
: new CPPConstructor( (ICPPASTFunctionDeclarator) funcDeclarator );
else
binding = template ? (ICPPMethod) new CPPMethodTemplate( name )
: new CPPMethod( (ICPPASTFunctionDeclarator) declarator );
: new CPPMethod( (ICPPASTFunctionDeclarator) funcDeclarator );
} else {
binding = template ? (ICPPFunction) new CPPFunctionTemplate( name )
: new CPPFunction( (ICPPASTFunctionDeclarator) declarator );
: new CPPFunction( (ICPPASTFunctionDeclarator) funcDeclarator );
}
} else if( parent instanceof IASTSimpleDeclaration ){
IType t1 = null, t2 = null;