1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Fix for 193962, isSameType() for anonymous types.

This commit is contained in:
Markus Schorn 2007-07-03 12:01:55 +00:00
parent 925667561d
commit 6ca0ffde23
8 changed files with 320 additions and 61 deletions

View file

@ -719,5 +719,4 @@ public class IndexBugsTests extends BaseTestCase {
CProjectHelper.delete(p2);
}
}
}

View file

@ -25,6 +25,7 @@ import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.index.IIndexBinding;
/**
* For testing PDOM binding resolution
@ -268,4 +269,62 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
ei= e.getEnumerators();
assertEquals(1, ei.length);
}
// // no header needed
// typedef struct {
// int member;
// } t_struct;
// typedef union {
// int member;
// } t_union;
// typedef enum {
// ei
// } t_enum;
public void testIsSameAnonymousType_Bug193962() throws DOMException {
// struct
IBinding tdAST = getBindingFromASTName("t_struct;", 8);
assertFalse(tdAST instanceof IIndexBinding);
IBinding tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
IType tAST= ((ITypedef) tdAST).getType();
IType tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof ICompositeType);
assertTrue(tIndex instanceof ICompositeType);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
// union
tdAST = getBindingFromASTName("t_union;", 7);
assertFalse(tdAST instanceof IIndexBinding);
tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
tAST= ((ITypedef) tdAST).getType();
tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof ICompositeType);
assertTrue(tIndex instanceof ICompositeType);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
// enum
tdAST = getBindingFromASTName("t_enum;", 6);
assertFalse(tdAST instanceof IIndexBinding);
tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
tAST= ((ITypedef) tdAST).getType();
tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof IEnumeration);
assertTrue(tIndex instanceof IEnumeration);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
}
}

View file

@ -16,6 +16,7 @@ import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IParameter;
@ -32,6 +33,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.parser.util.ObjectMap;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalTemplateInstantiator;
@ -385,4 +387,159 @@ public class IndexCPPBindingResolutionBugs extends IndexBindingResolutionTestBas
type= ((ITypedef) type).getType();
assertTrue(type instanceof IEnumeration);
}
// // no header needed
// typedef class {
// int member;
// } t_class;
// typedef struct {
// int member;
// } t_struct;
// typedef union {
// int member;
// } t_union;
// typedef enum {
// ei
// } t_enum;
public void testIsSameAnonymousType_Bug193962() throws DOMException {
// class
IBinding tdAST = getBindingFromASTName("t_class;", 7);
assertFalse(tdAST instanceof IIndexBinding);
IBinding tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
IType tAST= ((ITypedef) tdAST).getType();
IType tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof ICompositeType);
assertTrue(tIndex instanceof ICompositeType);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
// struct
tdAST = getBindingFromASTName("t_struct;", 8);
assertFalse(tdAST instanceof IIndexBinding);
tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
tAST= ((ITypedef) tdAST).getType();
tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof ICompositeType);
assertTrue(tIndex instanceof ICompositeType);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
// union
tdAST = getBindingFromASTName("t_union;", 7);
assertFalse(tdAST instanceof IIndexBinding);
tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
tAST= ((ITypedef) tdAST).getType();
tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof ICompositeType);
assertTrue(tIndex instanceof ICompositeType);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
// enum
tdAST = getBindingFromASTName("t_enum;", 6);
assertFalse(tdAST instanceof IIndexBinding);
tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
tAST= ((ITypedef) tdAST).getType();
tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof IEnumeration);
assertTrue(tIndex instanceof IEnumeration);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
}
// // no header needed
// namespace ns {
// typedef class {
// int member;
// } t_class;
// typedef struct {
// int member;
// } t_struct;
// typedef union {
// int member;
// } t_union;
// typedef enum {
// ei
// } t_enum;
// };
public void testIsSameNestedAnonymousType_Bug193962() throws DOMException {
// class
IBinding tdAST = getBindingFromASTName("t_class;", 7);
assertFalse(tdAST instanceof IIndexBinding);
IBinding tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
IType tAST= ((ITypedef) tdAST).getType();
IType tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof ICompositeType);
assertTrue(tIndex instanceof ICompositeType);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
// struct
tdAST = getBindingFromASTName("t_struct;", 8);
assertFalse(tdAST instanceof IIndexBinding);
tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
tAST= ((ITypedef) tdAST).getType();
tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof ICompositeType);
assertTrue(tIndex instanceof ICompositeType);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
// union
tdAST = getBindingFromASTName("t_union;", 7);
assertFalse(tdAST instanceof IIndexBinding);
tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
tAST= ((ITypedef) tdAST).getType();
tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof ICompositeType);
assertTrue(tIndex instanceof ICompositeType);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
// enum
tdAST = getBindingFromASTName("t_enum;", 6);
assertFalse(tdAST instanceof IIndexBinding);
tdIndex= strategy.getIndex().adaptBinding(tdAST);
assertTrue(tdIndex instanceof IIndexBinding);
assertTrue(tdAST instanceof ITypedef);
assertTrue(tdIndex instanceof ITypedef);
tAST= ((ITypedef) tdAST).getType();
tIndex= ((ITypedef) tdIndex).getType();
assertTrue(tAST instanceof IEnumeration);
assertTrue(tIndex instanceof IEnumeration);
assertTrue(tAST.isSameType(tIndex));
assertTrue(tIndex.isSameType(tAST));
}
}

View file

@ -30,6 +30,7 @@ import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
@ -246,11 +247,11 @@ public class PDOMASTAdapter {
}
}
private static class AnonymousClassType implements ICPPClassType {
private ICPPClassType fDelegate;
private static class AnonymousCPPBinding implements ICPPBinding {
protected ICPPBinding fDelegate;
private char[] fName;
public AnonymousClassType(char[] name, ICPPClassType delegate) {
public AnonymousCPPBinding(char[] name, ICPPBinding delegate) {
fName= name;
fDelegate= delegate;
}
@ -267,70 +268,32 @@ public class PDOMASTAdapter {
return fName;
}
public IField findField(String name) throws DOMException {
return fDelegate.findField(name);
public String[] getQualifiedName() throws DOMException {
String[] qn= fDelegate.getQualifiedName();
if (qn.length < 1) {
qn= new String[]{null};
}
qn[qn.length-1]= new String(fName);
return qn;
}
public char[][] getQualifiedNameCharArray() throws DOMException {
char[][] qn= fDelegate.getQualifiedNameCharArray();
if (qn.length < 1) {
qn= new char[][]{null};
}
qn[qn.length-1]= fName;
return qn;
}
public Object getAdapter(Class adapter) {
return fDelegate.getAdapter(adapter);
}
public ICPPMethod[] getAllDeclaredMethods() throws DOMException {
return fDelegate.getAllDeclaredMethods();
}
public ICPPBase[] getBases() throws DOMException {
return fDelegate.getBases();
}
public IScope getCompositeScope() throws DOMException {
return fDelegate.getCompositeScope();
}
public ICPPConstructor[] getConstructors() throws DOMException {
return fDelegate.getConstructors();
}
public ICPPField[] getDeclaredFields() throws DOMException {
return fDelegate.getDeclaredFields();
}
public ICPPMethod[] getDeclaredMethods() throws DOMException {
return fDelegate.getDeclaredMethods();
}
public IField[] getFields() throws DOMException {
return fDelegate.getFields();
}
public IBinding[] getFriends() throws DOMException {
return fDelegate.getFriends();
}
public int getKey() throws DOMException {
return fDelegate.getKey();
}
public ILinkage getLinkage() throws CoreException {
return fDelegate.getLinkage();
}
public ICPPMethod[] getMethods() throws DOMException {
return fDelegate.getMethods();
}
public ICPPClassType[] getNestedClasses() throws DOMException {
return fDelegate.getNestedClasses();
}
public String[] getQualifiedName() throws DOMException {
return fDelegate.getQualifiedName();
}
public char[][] getQualifiedNameCharArray() throws DOMException {
return fDelegate.getQualifiedNameCharArray();
}
public IScope getScope() throws DOMException {
return fDelegate.getScope();
}
@ -338,9 +301,77 @@ public class PDOMASTAdapter {
public boolean isGloballyQualified() throws DOMException {
return fDelegate.isGloballyQualified();
}
}
private static class AnonymousCPPEnumeration extends AnonymousCPPBinding implements IEnumeration, ICPPBinding {
public AnonymousCPPEnumeration(char[] name, IEnumeration delegate) {
super(name, (ICPPBinding) delegate);
}
public IEnumerator[] getEnumerators() throws DOMException {
return ((IEnumeration) fDelegate).getEnumerators();
}
public boolean isSameType(IType type) {
return fDelegate.isSameType(type);
return ((IEnumeration) fDelegate).isSameType(type);
}
}
private static class AnonymousClassType extends AnonymousCPPBinding implements ICPPClassType {
public AnonymousClassType(char[] name, ICPPClassType delegate) {
super(name, delegate);
}
public IField findField(String name) throws DOMException {
return ((ICPPClassType) fDelegate).findField(name);
}
public ICPPMethod[] getAllDeclaredMethods() throws DOMException {
return ((ICPPClassType) fDelegate).getAllDeclaredMethods();
}
public ICPPBase[] getBases() throws DOMException {
return ((ICPPClassType) fDelegate).getBases();
}
public IScope getCompositeScope() throws DOMException {
return ((ICPPClassType) fDelegate).getCompositeScope();
}
public ICPPConstructor[] getConstructors() throws DOMException {
return ((ICPPClassType) fDelegate).getConstructors();
}
public ICPPField[] getDeclaredFields() throws DOMException {
return ((ICPPClassType) fDelegate).getDeclaredFields();
}
public ICPPMethod[] getDeclaredMethods() throws DOMException {
return ((ICPPClassType) fDelegate).getDeclaredMethods();
}
public IField[] getFields() throws DOMException {
return ((ICPPClassType) fDelegate).getFields();
}
public IBinding[] getFriends() throws DOMException {
return ((ICPPClassType) fDelegate).getFriends();
}
public int getKey() throws DOMException {
return ((ICPPClassType) fDelegate).getKey();
}
public ICPPMethod[] getMethods() throws DOMException {
return ((ICPPClassType) fDelegate).getMethods();
}
public ICPPClassType[] getNestedClasses() throws DOMException {
return ((ICPPClassType) fDelegate).getNestedClasses();
}
public boolean isSameType(IType type) {
return ((ICPPClassType) fDelegate).isSameType(type);
}
}
@ -358,6 +389,9 @@ public class PDOMASTAdapter {
if (binding instanceof IEnumeration) {
name= ASTTypeUtil.createNameForAnonymous(binding);
if (name != null) {
if (binding instanceof ICPPBinding) {
return new AnonymousCPPEnumeration(name, (IEnumeration) binding);
}
return new AnonymousEnumeration(name, (IEnumeration) binding);
}
}

View file

@ -19,8 +19,10 @@ import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.internal.core.index.IIndexCBindingConstants;
import org.eclipse.cdt.internal.core.index.IIndexType;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMASTAdapter;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
@ -49,7 +51,7 @@ class PDOMCEnumeration extends PDOMBinding implements IEnumeration, IIndexType {
}
public int getNodeType() {
return PDOMCLinkage.CENUMERATION;
return IIndexCBindingConstants.CENUMERATION;
}
public IEnumerator[] getEnumerators() throws DOMException {
@ -103,6 +105,7 @@ class PDOMCEnumeration extends PDOMBinding implements IEnumeration, IIndexType {
if (type instanceof IEnumeration) {
IEnumeration etype= (IEnumeration) type;
etype= (IEnumeration) PDOMASTAdapter.getAdapterIfAnonymous(etype);
try {
return getDBName().equals(etype.getNameCharArray());
} catch (CoreException e) {

View file

@ -36,6 +36,7 @@ import org.eclipse.cdt.internal.core.index.IIndexType;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.db.PDOMNodeLinkedList;
import org.eclipse.cdt.internal.core.pdom.dom.IPDOMMemberOwner;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMASTAdapter;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
@ -181,6 +182,7 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
if (type instanceof ICompositeType) {
ICompositeType etype= (ICompositeType) type;
etype= (ICompositeType) PDOMASTAdapter.getAdapterIfAnonymous(etype);
try {
return getDBName().equals(etype.getNameCharArray());
} catch (CoreException e) {

View file

@ -51,6 +51,7 @@ import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.db.PDOMNodeLinkedList;
import org.eclipse.cdt.internal.core.pdom.dom.BindingCollector;
import org.eclipse.cdt.internal.core.pdom.dom.IPDOMMemberOwner;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMASTAdapter;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
@ -128,6 +129,7 @@ class PDOMCPPClassType extends PDOMCPPBinding implements ICPPClassType,
if (type instanceof ICPPClassType && !(type instanceof ProblemBinding)) {
ICPPClassType ctype= (ICPPClassType) type;
ctype= (ICPPClassType) PDOMASTAdapter.getAdapterIfAnonymous(ctype);
try {
if (ctype.getKey() == getKey()) {
char[][] qname= ctype.getQualifiedNameCharArray();

View file

@ -24,8 +24,10 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPDelegate;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDelegateCreator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPEnumeration.CPPEnumerationDelegate;
import org.eclipse.cdt.internal.core.index.IIndexCPPBindingConstants;
import org.eclipse.cdt.internal.core.index.IIndexType;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMASTAdapter;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
@ -55,7 +57,7 @@ class PDOMCPPEnumeration extends PDOMCPPBinding
}
public int getNodeType() {
return PDOMCPPLinkage.CPPENUMERATION;
return IIndexCPPBindingConstants.CPPENUMERATION;
}
public IEnumerator[] getEnumerators() throws DOMException {
@ -111,6 +113,7 @@ class PDOMCPPEnumeration extends PDOMCPPBinding
if (type instanceof IEnumeration) {
if (type instanceof ICPPBinding) {
ICPPBinding etype= (ICPPBinding) type;
etype= (ICPPBinding) PDOMASTAdapter.getAdapterIfAnonymous(etype);
char[][] qname = etype.getQualifiedNameCharArray();
return hasQualifiedName(qname, qname.length-1);
}