1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +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
// char globalVar;
public void _testAstIndexConflictVariable_Bug195127() throws Exception {
public void testAstIndexConflictVariable_Bug195127() throws Exception {
fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("globalVar;", 9);
assertTrue(b0 instanceof IVariable);
@ -150,7 +150,7 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// // don't include header
// char globalFunc();
public void _testAstIndexConflictFunction_Bug195127() throws Exception {
public void testAstIndexConflictFunction_Bug195127() throws Exception {
fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("globalFunc(", 10);
assertTrue(b0 instanceof IFunction);
@ -169,7 +169,7 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// char member;
// int additionalMember;
// };
public void _testAstIndexConflictStruct_Bug195127() throws Exception {
public void testAstIndexConflictStruct_Bug195127() throws Exception {
fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("astruct", 7);
assertTrue(b0 instanceof ICompositeType);
@ -196,9 +196,9 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// enum anenum {
// eItem0, eItem1
// };
public void _testAstIndexConflictEnumerator_Bug195127() throws Exception {
public void testAstIndexConflictEnumerator_Bug195127() throws Exception {
fakeFailForMultiProject();
IBinding b0 = getBindingFromASTName("anenum", 7);
IBinding b0 = getBindingFromASTName("anenum", 6);
assertTrue(b0 instanceof IEnumeration);
IEnumeration enumeration= (IEnumeration) b0;
IEnumerator[] enumerators= enumeration.getEnumerators();
@ -228,10 +228,44 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
// void func(struct st_20070703* x) {
// x->member= 0;
// }
public void _testAstIndexConflictStruct_Bug195227() throws Exception {
fakeFailForMultiProject();
public void testAstIndexStructFwdDecl_Bug195227() throws Exception {
IBinding b0 = getBindingFromASTName("member=", 6);
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.IASTFunctionDefinition;
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.IASTStatement;
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.IScope;
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.index.IIndex;
import org.eclipse.cdt.core.index.IndexFilter;
@ -56,7 +56,7 @@ import org.eclipse.core.runtime.NullProgressMonitor;
*/
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
* structures or unions : NAMESPACE_TYPE_TAG - members of structures or
* unions ( members have ICCompositeTypeScope ) - all other identifiers :
@ -170,50 +170,47 @@ public class CScope implements ICScope, IASTInternalScope {
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 ) {
char [] c = name.toCharArray();
if( c.length == 0 ){
return null;
}
int type = getNamespaceType( name );
Object o = bindings[type].get( name.toCharArray() );
if( o == null ) {
IBinding result= null;
if(physicalNode instanceof IASTTranslationUnit) {
IIndex index= ((IASTTranslationUnit)physicalNode).getIndex();
if(index!=null) {
try {
IBinding[] bindings= index.findBindings(name.toCharArray(), getIndexFilter(type), new NullProgressMonitor());
result= processIndexResults(name, bindings);
} catch(CoreException ce) {
CCorePlugin.log(ce);
}
}
}
return result;
}
if( o instanceof IBinding )
return (IBinding) o;
IASTName foundName= (IASTName) o;
if( (resolve || foundName.getBinding() != null) && ( foundName != name ) ) {
if(!isTypeDefinition(name) || CVisitor.declaredBefore(foundName, name)) {
return foundName.resolveBinding();
}
}
return null;
}
char [] c = name.toCharArray();
if( c.length == 0 ){
return null;
}
int type = getNamespaceType( name );
Object o = bindings[type].get( name.toCharArray() );
if( o == null || name == o) {
IBinding result= null;
if(physicalNode instanceof IASTTranslationUnit) {
IIndex index= ((IASTTranslationUnit)physicalNode).getIndex();
if(index!=null) {
try {
IBinding[] bindings= index.findBindings(name.toCharArray(), getIndexFilter(type), new NullProgressMonitor());
result= processIndexResults(name, bindings);
} catch(CoreException ce) {
CCorePlugin.log(ce);
}
}
}
return result;
}
if( o instanceof IBinding )
return (IBinding) o;
IASTName foundName= (IASTName) o;
if( (resolve || foundName.getBinding() != null) && ( foundName != name ) ) {
if(!isTypeDefinition(name) || CVisitor.declaredBefore(foundName, name)) {
return foundName.resolveBinding();
}
}
return null;
}
private boolean isTypeDefinition(IASTName name) {
return name.getPropertyInParent()==ICASTTypedefNameSpecifier.NAME;
return name.getPropertyInParent()==IASTNamedTypeSpecifier.NAME;
}
/* (non-Javadoc)

View file

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