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

fix bug 98760/103339 - CCE or Adding declarators to external functions

This commit is contained in:
Andrew Niefer 2005-07-12 15:54:05 +00:00
parent 1c13ce92c0
commit a923eec166
6 changed files with 84 additions and 88 deletions

View file

@ -3203,4 +3203,26 @@ public class AST2Tests extends AST2BaseTest {
assertNoProblemBindings( col ); 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() );
}
} }

View file

@ -15,11 +15,9 @@
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTName; 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.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType; 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.IScope;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding; 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 * @author aniefer
*/ */
public class CExternalFunction implements IFunction, ICExternalBinding { public class CExternalFunction extends CFunction implements IFunction, ICExternalBinding {
private IASTName name = null; private IASTName name = null;
private IASTTranslationUnit tu = null; private IASTTranslationUnit tu = null;
private IFunctionType fType = null;
public CExternalFunction( IASTTranslationUnit tu, IASTName name ) { public CExternalFunction( IASTTranslationUnit tu, IASTName name ) {
super( null );
this.name = name; this.name = name;
this.tu = tu; 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) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#getType() * @see org.eclipse.cdt.core.dom.ast.IFunction#getType()
*/ */
public IFunctionType getType() { public IFunctionType getType() {
if( fType == null ){ IFunctionType t = super.getType();
fType = new CPPFunctionType( CPPSemantics.VOID_TYPE, IType.EMPTY_TYPE_ARRAY ); if( t == null ) {
type = new CPPFunctionType( CPPSemantics.VOID_TYPE, IType.EMPTY_TYPE_ARRAY );
} }
return fType; return type;
}
protected IASTTranslationUnit getTranslationUnit() {
return tu;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -88,45 +73,10 @@ public class CExternalFunction implements IFunction, ICExternalBinding {
return tu.getScope(); return tu.getScope();
} }
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#isStatic()
*/
public boolean isStatic() {
return false;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#isExtern() * @see org.eclipse.cdt.core.dom.ast.IFunction#isExtern()
*/ */
public boolean isExtern() { public boolean isExtern() {
return true; 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;
}
} }

View file

@ -45,18 +45,24 @@ public class CFunction implements IFunction, ICInternalFunction {
private static final int RESOLUTION_IN_PROGRESS = 1 << 1; private static final int RESOLUTION_IN_PROGRESS = 1 << 1;
private int bits = 0; private int bits = 0;
IFunctionType type = null; protected IFunctionType type = null;
public CFunction( IASTFunctionDeclarator declarator ){ public CFunction( IASTFunctionDeclarator declarator ){
if( declarator != null ) {
if( declarator.getParent() instanceof IASTFunctionDefinition || declarator instanceof ICASTKnRFunctionDeclarator ) if( declarator.getParent() instanceof IASTFunctionDefinition || declarator instanceof ICASTKnRFunctionDeclarator )
definition = declarator; definition = declarator;
else { else {
declarators = new IASTStandardFunctionDeclarator [] { (IASTStandardFunctionDeclarator) declarator }; declarators = new IASTStandardFunctionDeclarator [] { (IASTStandardFunctionDeclarator) declarator };
} }
} }
}
public IASTNode getPhysicalNode(){ 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 ){ public void addDeclarator( IASTFunctionDeclarator fnDeclarator ){
updateParameterBindings( 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(){ private void resolveAllDeclarations(){
if( (bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0 ){ if( (bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0 ){
bits |= RESOLUTION_IN_PROGRESS; bits |= RESOLUTION_IN_PROGRESS;
IASTTranslationUnit tu = null; IASTTranslationUnit tu = getTranslationUnit();
if( definition != null )
tu = definition.getTranslationUnit();
else if( declarators != null )
tu = declarators[0].getTranslationUnit();
if( tu != null ){ if( tu != null ){
CPPVisitor.getDeclarations( tu, this ); CPPVisitor.getDeclarations( tu, this );
} }
@ -102,9 +111,14 @@ public class CFunction implements IFunction, ICInternalFunction {
* @see org.eclipse.cdt.core.dom.ast.IFunction#getParameters() * @see org.eclipse.cdt.core.dom.ast.IFunction#getParameters()
*/ */
public IParameter[] getParameters() { public IParameter[] getParameters() {
IParameter [] result = null; IParameter [] result = IParameter.EMPTY_PARAMETER_ARRAY;
IASTFunctionDeclarator dtor = (IASTFunctionDeclarator) getPhysicalNode();
if( dtor == null && (bits & FULLY_RESOLVED) == 0){
resolveAllDeclarations();
dtor = (IASTFunctionDeclarator) getPhysicalNode();
}
IASTFunctionDeclarator dtor = ( definition != null ) ? definition : declarators[0];
if (dtor instanceof IASTStandardFunctionDeclarator) { if (dtor instanceof IASTStandardFunctionDeclarator) {
IASTParameterDeclaration[] params = ((IASTStandardFunctionDeclarator)dtor).getParameters(); IASTParameterDeclaration[] params = ((IASTStandardFunctionDeclarator)dtor).getParameters();
int size = params.length; int size = params.length;
@ -150,8 +164,10 @@ public class CFunction implements IFunction, ICInternalFunction {
* @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope()
*/ */
public IScope getScope() { public IScope getScope() {
IASTFunctionDeclarator dtor = ( definition != null ) ? definition : declarators[0]; IASTFunctionDeclarator dtor = (IASTFunctionDeclarator) getPhysicalNode();
if( dtor != null )
return CVisitor.getContainingScope( dtor.getParent() ); return CVisitor.getContainingScope( dtor.getParent() );
return null;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -170,15 +186,20 @@ public class CFunction implements IFunction, ICInternalFunction {
*/ */
public IFunctionType getType() { public IFunctionType getType() {
if( type == null ) { if( type == null ) {
IASTDeclarator functionName = ( definition != null ) ? definition : declarators[0]; 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();
while (functionName.getNestedDeclarator() != null) IType tempType = CVisitor.createType( functionDtor );
functionName = functionName.getNestedDeclarator();
IType tempType = CVisitor.createType( functionName );
if (tempType instanceof IFunctionType) if (tempType instanceof IFunctionType)
type = (IFunctionType)tempType; type = (IFunctionType)tempType;
} }
}
return type; return type;
} }

View file

@ -27,7 +27,6 @@ public class CImplicitFunction extends CExternalFunction implements IFunction, I
private IParameter[] parms=null; private IParameter[] parms=null;
private IScope scope=null; private IScope scope=null;
private IFunctionType type=null;
private boolean takesVarArgs=false; private boolean takesVarArgs=false;
private char[] name=null; private char[] name=null;

View file

@ -779,8 +779,8 @@ public class CVisitor {
if ( CharArrayUtils.equals(declarator.getName().toCharArray(), name.toCharArray()) ){ if ( CharArrayUtils.equals(declarator.getName().toCharArray(), name.toCharArray()) ){
binding = resolveBinding( parent, CURRENT_SCOPE ); binding = resolveBinding( parent, CURRENT_SCOPE );
if( binding != null ) { if( binding != null ) {
if( binding instanceof IFunction ) if( binding instanceof ICInternalFunction )
((CFunction)binding).addDeclarator( (ICASTKnRFunctionDeclarator) declarator ); ((ICInternalFunction)binding).addDeclarator( (ICASTKnRFunctionDeclarator) declarator );
else else
binding = new ProblemBinding( name, IProblemBinding.SEMANTIC_INVALID_OVERLOAD, name.toCharArray() ); binding = new ProblemBinding( name, IProblemBinding.SEMANTIC_INVALID_OVERLOAD, name.toCharArray() );
} else { } else {

View file

@ -14,10 +14,14 @@
*/ */
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
/** /**
* @author aniefer * @author aniefer
* *
*/ */
public interface ICInternalFunction extends ICInternalBinding { public interface ICInternalFunction extends ICInternalBinding {
public void setFullyResolved( boolean resolved ); public void setFullyResolved( boolean resolved );
public void addDeclarator( IASTFunctionDeclarator fnDeclarator );
} }