1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-02 13:13:36 +02:00

Fix for 195127 and 195227, binding resolution for plain C.

This commit is contained in:
Markus Schorn 2007-07-03 09:19:16 +00:00
parent b9916f7846
commit 925667561d
3 changed files with 103 additions and 62 deletions

View file

@ -136,7 +136,7 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// // don't include header // // don't include header
// char globalVar; // char globalVar;
public void _testAstIndexConflictVariable_Bug195127() throws Exception { public void testAstIndexConflictVariable_Bug195127() throws Exception {
fakeFailForMultiProject(); fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("globalVar;", 9); IBinding b0 = getBindingFromASTName("globalVar;", 9);
assertTrue(b0 instanceof IVariable); assertTrue(b0 instanceof IVariable);
@ -150,7 +150,7 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// // don't include header // // don't include header
// char globalFunc(); // char globalFunc();
public void _testAstIndexConflictFunction_Bug195127() throws Exception { public void testAstIndexConflictFunction_Bug195127() throws Exception {
fakeFailForMultiProject(); fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("globalFunc(", 10); IBinding b0 = getBindingFromASTName("globalFunc(", 10);
assertTrue(b0 instanceof IFunction); assertTrue(b0 instanceof IFunction);
@ -169,7 +169,7 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// char member; // char member;
// int additionalMember; // int additionalMember;
// }; // };
public void _testAstIndexConflictStruct_Bug195127() throws Exception { public void testAstIndexConflictStruct_Bug195127() throws Exception {
fakeFailForMultiProject(); fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("astruct", 7); IBinding b0 = getBindingFromASTName("astruct", 7);
assertTrue(b0 instanceof ICompositeType); assertTrue(b0 instanceof ICompositeType);
@ -196,9 +196,9 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// enum anenum { // enum anenum {
// eItem0, eItem1 // eItem0, eItem1
// }; // };
public void _testAstIndexConflictEnumerator_Bug195127() throws Exception { public void testAstIndexConflictEnumerator_Bug195127() throws Exception {
fakeFailForMultiProject(); fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("anenum", 7); IBinding b0 = getBindingFromASTName("anenum", 6);
assertTrue(b0 instanceof IEnumeration); assertTrue(b0 instanceof IEnumeration);
IEnumeration enumeration= (IEnumeration) b0; IEnumeration enumeration= (IEnumeration) b0;
IEnumerator[] enumerators= enumeration.getEnumerators(); IEnumerator[] enumerators= enumeration.getEnumerators();
@ -228,10 +228,44 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// void func(struct st_20070703* x) { // void func(struct st_20070703* x) {
// x->member= 0; // x->member= 0;
// } // }
public void _testAstIndexConflictStruct_Bug195227() throws Exception { public void testAstIndexStructFwdDecl_Bug195227() throws Exception {
fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("member=", 6); IBinding b0 = getBindingFromASTName("member=", 6);
assertTrue(b0 instanceof IField); assertTrue(b0 instanceof IField);
} }
// struct astruct {
// int member;
// };
// enum anenum {
// eItem0
// };
// #include "header.h"
// struct astruct;
// enum anenum;
// void func(struct astruct a, enum anenum b) {
// }
public void testAstIndexFwdDecl_Bug195227() throws Exception {
IBinding b0 = getBindingFromASTName("astruct;", 7);
IBinding b1 = getBindingFromASTName("anenum;", 6);
assertTrue(b0 instanceof ICompositeType);
ICompositeType t= (ICompositeType) b0;
IField[] f= t.getFields();
assertEquals(1, f.length);
assertTrue(b1 instanceof IEnumeration);
IEnumeration e= (IEnumeration) b1;
IEnumerator[] ei= e.getEnumerators();
assertEquals(1, ei.length);
b0 = getBindingFromASTName("astruct a", 7);
b1 = getBindingFromASTName("anenum b", 6);
assertTrue(b0 instanceof ICompositeType);
t= (ICompositeType) b0;
f= t.getFields();
assertEquals(1, f.length);
assertTrue(b1 instanceof IEnumeration);
e= (IEnumeration) b1;
ei= e.getEnumerators();
assertEquals(1, ei.length);
}
} }

View file

@ -31,6 +31,7 @@ import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -40,7 +41,6 @@ import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor; import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICScope; import org.eclipse.cdt.core.dom.ast.c.ICScope;
import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IndexFilter; import org.eclipse.cdt.core.index.IndexFilter;
@ -56,7 +56,7 @@ import org.eclipse.core.runtime.NullProgressMonitor;
*/ */
public class CScope implements ICScope, IASTInternalScope { public class CScope implements ICScope, IASTInternalScope {
/** /**
* ISO C:99 6.2.3 there are seperate namespaces for various categories of * ISO C:99 6.2.3 there are separate namespaces for various categories of
* identifiers: - label names ( labels have ICFunctionScope ) - tags of * identifiers: - label names ( labels have ICFunctionScope ) - tags of
* structures or unions : NAMESPACE_TYPE_TAG - members of structures or * structures or unions : NAMESPACE_TYPE_TAG - members of structures or
* unions ( members have ICCompositeTypeScope ) - all other identifiers : * unions ( members have ICCompositeTypeScope ) - all other identifiers :
@ -170,50 +170,47 @@ public class CScope implements ICScope, IASTInternalScope {
return NAMESPACE_TYPE_OTHER; return NAMESPACE_TYPE_OTHER;
} }
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.c.ICScope#getBinding(org.eclipse.cdt.core.dom.ast.IASTName, boolean)
*/
public IBinding getBinding( IASTName name, boolean resolve ) { public IBinding getBinding( IASTName name, boolean resolve ) {
char [] c = name.toCharArray(); char [] c = name.toCharArray();
if( c.length == 0 ){ if( c.length == 0 ){
return null; return null;
} }
int type = getNamespaceType( name ); int type = getNamespaceType( name );
Object o = bindings[type].get( name.toCharArray() ); Object o = bindings[type].get( name.toCharArray() );
if( o == null ) { if( o == null || name == o) {
IBinding result= null; IBinding result= null;
if(physicalNode instanceof IASTTranslationUnit) { if(physicalNode instanceof IASTTranslationUnit) {
IIndex index= ((IASTTranslationUnit)physicalNode).getIndex(); IIndex index= ((IASTTranslationUnit)physicalNode).getIndex();
if(index!=null) { if(index!=null) {
try { try {
IBinding[] bindings= index.findBindings(name.toCharArray(), getIndexFilter(type), new NullProgressMonitor()); IBinding[] bindings= index.findBindings(name.toCharArray(), getIndexFilter(type), new NullProgressMonitor());
result= processIndexResults(name, bindings); result= processIndexResults(name, bindings);
} catch(CoreException ce) { } catch(CoreException ce) {
CCorePlugin.log(ce); CCorePlugin.log(ce);
} }
} }
} }
return result; return result;
} }
if( o instanceof IBinding ) if( o instanceof IBinding )
return (IBinding) o; return (IBinding) o;
IASTName foundName= (IASTName) o; IASTName foundName= (IASTName) o;
if( (resolve || foundName.getBinding() != null) && ( foundName != name ) ) { if( (resolve || foundName.getBinding() != null) && ( foundName != name ) ) {
if(!isTypeDefinition(name) || CVisitor.declaredBefore(foundName, name)) { if(!isTypeDefinition(name) || CVisitor.declaredBefore(foundName, name)) {
return foundName.resolveBinding(); return foundName.resolveBinding();
} }
} }
return null; return null;
} }
private boolean isTypeDefinition(IASTName name) { private boolean isTypeDefinition(IASTName name) {
return name.getPropertyInParent()==ICASTTypedefNameSpecifier.NAME; return name.getPropertyInParent()==IASTNamedTypeSpecifier.NAME;
} }
/* (non-Javadoc) /* (non-Javadoc)

View file

@ -500,9 +500,9 @@ public class CVisitor {
} catch ( DOMException e ) { } catch ( DOMException e ) {
binding = null; binding = null;
} }
if( binding != null ){ if (binding != null && !(binding instanceof IIndexBinding)) {
if(binding instanceof IEnumeration && (binding instanceof IIndexBinding || binding instanceof CEnumeration) ) { if (binding instanceof IEnumeration) {
if( binding instanceof CEnumeration ) { if (binding instanceof CEnumeration) {
((CEnumeration)binding).addDefinition( name ); ((CEnumeration)binding).addDefinition( name );
} }
} else { } else {
@ -887,7 +887,7 @@ public class CVisitor {
} catch (DOMException e) { } catch (DOMException e) {
} }
} else if( funcDeclarator != null ){ } else if( funcDeclarator != null ){
if( binding != null ) { if( binding != null && !(binding instanceof IIndexBinding)) {
if( binding instanceof IFunction ){ if( binding instanceof IFunction ){
IFunction function = (IFunction) binding; IFunction function = (IFunction) binding;
if( function instanceof CFunction ) if( function instanceof CFunction )
@ -905,7 +905,7 @@ public class CVisitor {
binding = new CTypedef( name ); binding = new CTypedef( name );
} else { } else {
IType t1 = null, t2 = null; IType t1 = null, t2 = null;
if( binding != null ) { if( binding != null && !(binding instanceof IIndexBinding)) {
if( binding instanceof IParameter ){ if( binding instanceof IParameter ){
return new ProblemBinding( name, IProblemBinding.SEMANTIC_INVALID_REDECLARATION, name.toCharArray() ); return new ProblemBinding( name, IProblemBinding.SEMANTIC_INVALID_REDECLARATION, name.toCharArray() );
} else if( binding instanceof IVariable ){ } else if( binding instanceof IVariable ){
@ -948,7 +948,7 @@ public class CVisitor {
scope = (ICScope) scope.getParent(); scope = (ICScope) scope.getParent();
binding = scope.getBinding( name, false ); binding = scope.getBinding( name, false );
if( binding != null ){ if( binding != null && !(binding instanceof IIndexBinding)){
if (binding instanceof CStructure) if (binding instanceof CStructure)
((CStructure)binding).addDefinition( compositeTypeSpec ); ((CStructure)binding).addDefinition( compositeTypeSpec );
return binding; return binding;
@ -1197,7 +1197,7 @@ public class CVisitor {
protected static Object findBinding( IASTNode blockItem, IASTName name, int bits ) throws DOMException{ protected static Object findBinding( IASTNode blockItem, IASTName name, int bits ) throws DOMException{
boolean prefix = ( bits & PREFIX_LOOKUP ) != 0; boolean prefix = ( bits & PREFIX_LOOKUP ) != 0;
Object binding = prefix ? new ObjectSet( 2 ) : null; Object binding = prefix ? new ObjectSet( 2 ) : null;
IIndexBinding foundIndexBinding= null;
CharArrayObjectMap prefixMap = prefix ? new CharArrayObjectMap(2) : null; CharArrayObjectMap prefixMap = prefix ? new CharArrayObjectMap(2) : null;
while( blockItem != null ){ while( blockItem != null ){
@ -1252,11 +1252,18 @@ public class CVisitor {
} }
if( binding != null ) if( binding != null )
return binding; return binding;
} else if (!prefix && scope != null && scope.getParent() == null
&& scope.getBinding( name, false ) != null) {
binding = scope.getBinding( name, false );
return binding;
} else { } else {
if (!prefix && scope != null && scope.getParent() == null) {
binding= scope.getBinding(name, false);
if (binding != null) {
if (binding instanceof IIndexBinding) {
foundIndexBinding= (IIndexBinding) binding;
}
else {
return binding;
}
}
}
Object result = null; Object result = null;
boolean reachedBlockItem = false; boolean reachedBlockItem = false;
@ -1336,6 +1343,9 @@ public class CVisitor {
if( blockItem instanceof IASTTranslationUnit ) if( blockItem instanceof IASTTranslationUnit )
break; break;
} }
if (foundIndexBinding != null) {
return foundIndexBinding;
}
if( prefixMap != null ){ if( prefixMap != null ){
IBinding [] result = null; IBinding [] result = null;
Object [] vals = prefixMap.valueArray(); Object [] vals = prefixMap.valueArray();