From a923eec16629f99265f9e892b172f36697d91df7 Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Tue, 12 Jul 2005 15:54:05 +0000 Subject: [PATCH] fix bug 98760/103339 - CCE or Adding declarators to external functions --- .../cdt/core/parser/tests/ast2/AST2Tests.java | 22 ++++++ .../core/dom/parser/c/CExternalFunction.java | 70 +++--------------- .../internal/core/dom/parser/c/CFunction.java | 71 ++++++++++++------- .../core/dom/parser/c/CImplicitFunction.java | 1 - .../internal/core/dom/parser/c/CVisitor.java | 4 +- .../core/dom/parser/c/ICInternalFunction.java | 4 ++ 6 files changed, 84 insertions(+), 88 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 65af97de208..88971d03f9a 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -3203,4 +3203,26 @@ public class AST2Tests extends AST2BaseTest { assertNoProblemBindings( col ); } + public void testBug98760() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("struct nfa; \n"); //$NON-NLS-1$ + buffer.append("void f() { \n"); //$NON-NLS-1$ + buffer.append(" struct nfa * n; \n"); //$NON-NLS-1$ + buffer.append(" freenfa( n ); \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + buffer.append("static void freenfa( nfa ) \n"); //$NON-NLS-1$ + buffer.append("struct nfa * nfa; \n"); //$NON-NLS-1$ + buffer.append("{ \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C, true); + CNameCollector col = new CNameCollector(); + tu.accept(col); + + IFunction free = (IFunction) col.getName(4).resolveBinding(); + IParameter [] ps = free.getParameters(); + assertEquals( ps.length, 1 ); + + assertSame( free, col.getName(6).resolveBinding() ); + } } \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalFunction.java index 6585725c3e5..d9c11884b7f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalFunction.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalFunction.java @@ -15,11 +15,9 @@ package org.eclipse.cdt.internal.core.dom.parser.c; import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunctionType; -import org.eclipse.cdt.core.dom.ast.IParameter; import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding; @@ -29,42 +27,29 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics; /** * @author aniefer */ -public class CExternalFunction implements IFunction, ICExternalBinding { +public class CExternalFunction extends CFunction implements IFunction, ICExternalBinding { private IASTName name = null; private IASTTranslationUnit tu = null; - private IFunctionType fType = null; public CExternalFunction( IASTTranslationUnit tu, IASTName name ) { + super( null ); this.name = name; this.tu = tu; } - - public IASTNode getPhysicalNode(){ - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IFunction#getParameters() - */ - public IParameter[] getParameters() { - return IParameter.EMPTY_PARAMETER_ARRAY; - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IFunction#getFunctionScope() - */ - public IScope getFunctionScope() { - return null; - } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IFunction#getType() */ public IFunctionType getType() { - if( fType == null ){ - fType = new CPPFunctionType( CPPSemantics.VOID_TYPE, IType.EMPTY_TYPE_ARRAY ); + IFunctionType t = super.getType(); + if( t == null ) { + type = new CPPFunctionType( CPPSemantics.VOID_TYPE, IType.EMPTY_TYPE_ARRAY ); } - return fType; + return type; + } + + protected IASTTranslationUnit getTranslationUnit() { + return tu; } /* (non-Javadoc) @@ -88,45 +73,10 @@ public class CExternalFunction implements IFunction, ICExternalBinding { return tu.getScope(); } - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IFunction#isStatic() - */ - public boolean isStatic() { - return false; - } - /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IFunction#isExtern() */ public boolean isExtern() { return true; } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IFunction#isAuto() - */ - public boolean isAuto() { - return false; - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IFunction#isRegister() - */ - public boolean isRegister() { - return false; - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IFunction#isInline() - */ - public boolean isInline() { - return false; - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IFunction#takesVarArgs() - */ - public boolean takesVarArgs() { - return false; - } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java index 6fa543f19c0..0aef1324db5 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java @@ -45,18 +45,24 @@ public class CFunction implements IFunction, ICInternalFunction { private static final int RESOLUTION_IN_PROGRESS = 1 << 1; private int bits = 0; - IFunctionType type = null; + protected IFunctionType type = null; public CFunction( IASTFunctionDeclarator declarator ){ - if( declarator.getParent() instanceof IASTFunctionDefinition || declarator instanceof ICASTKnRFunctionDeclarator ) - definition = declarator; - else { - declarators = new IASTStandardFunctionDeclarator [] { (IASTStandardFunctionDeclarator) declarator }; - } + if( declarator != null ) { + if( declarator.getParent() instanceof IASTFunctionDefinition || declarator instanceof ICASTKnRFunctionDeclarator ) + definition = declarator; + else { + declarators = new IASTStandardFunctionDeclarator [] { (IASTStandardFunctionDeclarator) declarator }; + } + } } public IASTNode getPhysicalNode(){ - return ( definition != null ) ? definition : declarators[0]; + if( definition != null ) + return definition; + else if( declarators != null && declarators.length > 0 ) + return declarators[0]; + return null; } public void addDeclarator( IASTFunctionDeclarator fnDeclarator ){ updateParameterBindings( fnDeclarator ); @@ -80,15 +86,18 @@ public class CFunction implements IFunction, ICInternalFunction { } } + protected IASTTranslationUnit getTranslationUnit() { + if( definition != null ) + return definition.getTranslationUnit(); + else if( declarators != null ) + return declarators[0].getTranslationUnit(); + return null; + } + private void resolveAllDeclarations(){ if( (bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0 ){ bits |= RESOLUTION_IN_PROGRESS; - IASTTranslationUnit tu = null; - if( definition != null ) - tu = definition.getTranslationUnit(); - else if( declarators != null ) - tu = declarators[0].getTranslationUnit(); - + IASTTranslationUnit tu = getTranslationUnit(); if( tu != null ){ CPPVisitor.getDeclarations( tu, this ); } @@ -102,9 +111,14 @@ public class CFunction implements IFunction, ICInternalFunction { * @see org.eclipse.cdt.core.dom.ast.IFunction#getParameters() */ public IParameter[] getParameters() { - IParameter [] result = null; + IParameter [] result = IParameter.EMPTY_PARAMETER_ARRAY; - IASTFunctionDeclarator dtor = ( definition != null ) ? definition : declarators[0]; + IASTFunctionDeclarator dtor = (IASTFunctionDeclarator) getPhysicalNode(); + if( dtor == null && (bits & FULLY_RESOLVED) == 0){ + resolveAllDeclarations(); + dtor = (IASTFunctionDeclarator) getPhysicalNode(); + } + if (dtor instanceof IASTStandardFunctionDeclarator) { IASTParameterDeclaration[] params = ((IASTStandardFunctionDeclarator)dtor).getParameters(); int size = params.length; @@ -150,8 +164,10 @@ public class CFunction implements IFunction, ICInternalFunction { * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() */ public IScope getScope() { - IASTFunctionDeclarator dtor = ( definition != null ) ? definition : declarators[0]; - return CVisitor.getContainingScope( dtor.getParent() ); + IASTFunctionDeclarator dtor = (IASTFunctionDeclarator) getPhysicalNode(); + if( dtor != null ) + return CVisitor.getContainingScope( dtor.getParent() ); + return null; } /* (non-Javadoc) @@ -170,14 +186,19 @@ public class CFunction implements IFunction, ICInternalFunction { */ public IFunctionType getType() { if( type == null ) { - IASTDeclarator functionName = ( definition != null ) ? definition : declarators[0]; - - while (functionName.getNestedDeclarator() != null) - functionName = functionName.getNestedDeclarator(); - - IType tempType = CVisitor.createType( functionName ); - if (tempType instanceof IFunctionType) - type = (IFunctionType)tempType; + IASTDeclarator functionDtor = (IASTDeclarator) getPhysicalNode(); + if( functionDtor == null && (bits & FULLY_RESOLVED) == 0){ + resolveAllDeclarations(); + functionDtor = (IASTDeclarator) getPhysicalNode(); + } + if( functionDtor != null ) { + while (functionDtor.getNestedDeclarator() != null) + functionDtor = functionDtor.getNestedDeclarator(); + + IType tempType = CVisitor.createType( functionDtor ); + if (tempType instanceof IFunctionType) + type = (IFunctionType)tempType; + } } return type; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CImplicitFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CImplicitFunction.java index 5cbaacac93a..a219515e6a7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CImplicitFunction.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CImplicitFunction.java @@ -27,7 +27,6 @@ public class CImplicitFunction extends CExternalFunction implements IFunction, I private IParameter[] parms=null; private IScope scope=null; - private IFunctionType type=null; private boolean takesVarArgs=false; private char[] name=null; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java index 93d73162529..b09eb21a212 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java @@ -779,8 +779,8 @@ public class CVisitor { if ( CharArrayUtils.equals(declarator.getName().toCharArray(), name.toCharArray()) ){ binding = resolveBinding( parent, CURRENT_SCOPE ); if( binding != null ) { - if( binding instanceof IFunction ) - ((CFunction)binding).addDeclarator( (ICASTKnRFunctionDeclarator) declarator ); + if( binding instanceof ICInternalFunction ) + ((ICInternalFunction)binding).addDeclarator( (ICASTKnRFunctionDeclarator) declarator ); else binding = new ProblemBinding( name, IProblemBinding.SEMANTIC_INVALID_OVERLOAD, name.toCharArray() ); } else { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/ICInternalFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/ICInternalFunction.java index 2dbe9ff40ac..2350f36406d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/ICInternalFunction.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/ICInternalFunction.java @@ -14,10 +14,14 @@ */ package org.eclipse.cdt.internal.core.dom.parser.c; +import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; + /** * @author aniefer * */ public interface ICInternalFunction extends ICInternalBinding { public void setFullyResolved( boolean resolved ); + + public void addDeclarator( IASTFunctionDeclarator fnDeclarator ); }