1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56:01 +02:00

Complete support for members of anonymous unions, bug 206450.

This commit is contained in:
Markus Schorn 2008-07-28 14:42:42 +00:00
parent b03ff9f8d1
commit 1c2c0848b7
24 changed files with 230 additions and 22 deletions

View file

@ -5244,4 +5244,22 @@ public class AST2Tests extends AST2BaseTest {
parseAndCheckBindings(getAboveComment(), lang, true); parseAndCheckBindings(getAboveComment(), lang, true);
} }
} }
// struct Outer {
// struct {int a1;};
// struct {int a2;} a3;
// };
// void test() {
// struct Outer o;
// o.a1=0; o.a2=0; o.a3=0;
// }
public void testAnonymousUnionMember() throws Exception {
final boolean[] isCpps= {false, true};
for (boolean isCpp : isCpps) {
BindingAssertionHelper bh= new BindingAssertionHelper(getAboveComment(), isCpp);
bh.assertNonProblem("a1=", 2);
bh.assertProblem("a2=", 2);
bh.assertNonProblem("a3=", 2);
}
}
} }

View file

@ -728,12 +728,16 @@ public class IndexCPPBindingResolutionBugs extends IndexBindingResolutionTestBas
// struct { // struct {
// int var1; // int var1;
// }; // };
// struct {
// int var2;
// } hide;
// }; // };
// #include "header.h" // #include "header.h"
// void test() { // void test() {
// union outer x; // union outer x;
// x.var1=1; // x.var1=1;
// x.var2= 2; // must be a problem
// } // }
public void testAnonymousStruct_Bug216791() throws DOMException { public void testAnonymousStruct_Bug216791() throws DOMException {
// struct // struct
@ -743,6 +747,8 @@ public class IndexCPPBindingResolutionBugs extends IndexBindingResolutionTestBas
IScope outer= f.getCompositeTypeOwner().getScope(); IScope outer= f.getCompositeTypeOwner().getScope();
assertTrue(outer instanceof ICPPClassScope); assertTrue(outer instanceof ICPPClassScope);
assertEquals("outer", outer.getScopeName().toString()); assertEquals("outer", outer.getScopeName().toString());
getProblemFromASTName("var2=", 4);
} }
// namespace ns { // namespace ns {

View file

@ -12,6 +12,7 @@
package org.eclipse.cdt.core.dom.ast; package org.eclipse.cdt.core.dom.ast;
import org.eclipse.cdt.core.dom.ILinkage; import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition; 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.ICPPTemplateParameter;
@ -53,8 +54,10 @@ public interface IBinding extends IAdaptable {
* declared else where). * declared else where).
* <p> Possible owners are: * <p> Possible owners are:
* <br> {@link IFunction}: for parameters, local types, variables, enumerators, labels and using declarations; * <br> {@link IFunction}: for parameters, local types, variables, enumerators, labels and using declarations;
* <br> {@link ICompositeType}: for class-, struct- and union-members, even if the composite type is anonymous; * <br> {@link ICPPClassType}: for class-, struct- and union-members, even if the composite type is anonymous;
* also for enumerators and using declarations; * also for enumerators and using declarations;
* <br> {@link ICompositeType}: for struct- and union-members, even if the composite type is anonymous;
* also for anonymous structs or unions found within another struct;
* <br> {@link ICPPNamespace}: for global types, functions, variables, enumerators, namespaces and using declarations; * <br> {@link ICPPNamespace}: for global types, functions, variables, enumerators, namespaces and using declarations;
* <br> <code>null</code>: for types, functions, variables, enumerators, namespaces and using declarations; * <br> <code>null</code>: for types, functions, variables, enumerators, namespaces and using declarations;
* @since 5.1 * @since 5.1

View file

@ -12,7 +12,9 @@ package org.eclipse.cdt.core.dom.ast;
/** /**
* @author Doug Schaefer * Interface for all composite types: classes, structs or unions.
*
* @noimplement This interface is not intended to be implemented by clients.
*/ */
public interface ICompositeType extends IBinding, IType { public interface ICompositeType extends IBinding, IType {
@ -23,6 +25,18 @@ public interface ICompositeType extends IBinding, IType {
*/ */
public int getKey() throws DOMException; public int getKey() throws DOMException;
/**
* Returns whether the type is anonymous or not. A type for which objects or
* pointers are declared is not considered an anonymous type.
* <pre> struct Outer {
* struct {int a;}; // anonymous
* struct {int b;} c; // not anonymous
* }
* </pre>
* @since 5.1
*/
boolean isAnonymous() throws DOMException;
/** /**
* Returns the fields for this type. * Returns the fields for this type.
* *

View file

@ -59,6 +59,9 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte
public int getKey() throws DOMException { public int getKey() throws DOMException {
throw new DOMException( this ); throw new DOMException( this );
} }
public boolean isAnonymous() throws DOMException {
throw new DOMException( this );
}
} }
private IASTName [] declarations = null; private IASTName [] declarations = null;
@ -281,6 +284,29 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte
node= declarations[0]; node= declarations[0];
} }
} }
return CVisitor.findEnclosingFunction(node); // local or global IBinding result= CVisitor.findEnclosingFunction(node); // local or global
if (result != null)
return result;
if (definition != null && isAnonymous()) {
return CVisitor.findDeclarationOwner(definition, false);
}
return null;
}
public boolean isAnonymous() throws DOMException {
if (getNameCharArray().length > 0 || definition == null)
return false;
IASTCompositeTypeSpecifier spec= ((IASTCompositeTypeSpecifier)definition.getParent());
if (spec != null) {
IASTNode node= spec.getParent();
if (node instanceof IASTSimpleDeclaration) {
if (((IASTSimpleDeclaration) node).getDeclarators().length == 0) {
return true;
}
}
}
return false;
} }
} }

View file

@ -309,4 +309,23 @@ public class CPPClassSpecialization extends CPPSpecialization implements ICPPCla
public ICPPClassType[] getNestedClasses() throws DOMException { public ICPPClassType[] getNestedClasses() throws DOMException {
return ICPPClassType.EMPTY_CLASS_ARRAY; return ICPPClassType.EMPTY_CLASS_ARRAY;
} }
public boolean isAnonymous() throws DOMException {
if (getNameCharArray().length > 0)
return false;
ICPPASTCompositeTypeSpecifier spec= getCompositeTypeSpecifier();
if (spec == null) {
return getSpecializedBinding().isAnonymous();
}
IASTNode node= spec.getParent();
if (node instanceof IASTSimpleDeclaration) {
if (((IASTSimpleDeclaration) node).getDeclarators().length == 0) {
return true;
}
}
return false;
}
} }

View file

@ -252,4 +252,8 @@ public class CPPClassTemplate extends CPPTemplateDefinition implements
public String toString() { public String toString() {
return ASTTypeUtil.getType(this); return ASTTypeUtil.getType(this);
} }
public boolean isAnonymous() {
return false;
}
} }

View file

@ -138,6 +138,9 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp
public ICPPClassType[] getNestedClasses() throws DOMException { public ICPPClassType[] getNestedClasses() throws DOMException {
throw new DOMException( this ); throw new DOMException( this );
} }
public boolean isAnonymous() throws DOMException {
throw new DOMException( this );
}
} }
private class FindDefinitionAction extends CPPASTVisitor { private class FindDefinitionAction extends CPPASTVisitor {
@ -462,4 +465,20 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp
public IBinding getOwner() throws DOMException { public IBinding getOwner() throws DOMException {
return CPPVisitor.findDeclarationOwner(definition != null ? definition : declarations[0], true); return CPPVisitor.findDeclarationOwner(definition != null ? definition : declarations[0], true);
} }
public boolean isAnonymous() throws DOMException {
if (getNameCharArray().length > 0)
return false;
ICPPASTCompositeTypeSpecifier spec= getCompositeTypeSpecifier();
if (spec != null) {
IASTNode node= spec.getParent();
if (node instanceof IASTSimpleDeclaration) {
if (((IASTSimpleDeclaration) node).getDeclarators().length == 0) {
return true;
}
}
}
return false;
}
} }

View file

@ -223,4 +223,8 @@ public class CPPTemplateTemplateParameter extends CPPTemplateParameter implement
public IASTName getUnknownName() { public IASTName getUnknownName() {
return new CPPASTName(getNameCharArray()); return new CPPASTName(getNameCharArray());
} }
public boolean isAnonymous() {
return false;
}
} }

View file

@ -110,4 +110,8 @@ public class CPPUnknownClass extends CPPUnknownBinding implements ICPPUnknownCla
public ICPPClassType[] getNestedClasses() { public ICPPClassType[] getNestedClasses() {
return ICPPClassType.EMPTY_CLASS_ARRAY; return ICPPClassType.EMPTY_CLASS_ARRAY;
} }
public boolean isAnonymous() {
return false;
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007 Symbian Software Systems and others. * Copyright (c) 2007, 2008 Symbian Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -51,4 +51,8 @@ class CompositeCStructure extends CompositeCBinding implements ICompositeType, I
@Override @Override
public Object clone() {fail(); return null;} public Object clone() {fail(); return null;}
public boolean isAnonymous() throws DOMException {
return ((ICompositeType)rbinding).isAnonymous();
}
} }

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Andrew Ferguson (Symbian) - Initial implementation * Andrew Ferguson (Symbian) - Initial implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.index.composite.cpp; package org.eclipse.cdt.internal.core.index.composite.cpp;
@ -174,4 +175,8 @@ class CompositeCPPClassType extends CompositeCPPBinding implements ICPPClassType
public boolean isSameType(IType type) { public boolean isSameType(IType type) {
return ((ICPPClassType)rbinding).isSameType(type); return ((ICPPClassType)rbinding).isSameType(type);
} }
public boolean isAnonymous() throws DOMException {
return ((ICPPClassType)rbinding).isAnonymous();
}
} }

View file

@ -135,4 +135,8 @@ class CompositeCPPUnknownClassType extends CompositeCPPBinding implements ICPPUn
public IASTName getUnknownName() { public IASTName getUnknownName() {
return ((ICPPUnknownClassType) rbinding).getUnknownName(); return ((ICPPUnknownClassType) rbinding).getUnknownName();
} }
public boolean isAnonymous() {
return false;
}
} }

View file

@ -177,6 +177,7 @@ public class PDOM extends PlatformObject implements IPDOM {
* #61.0# - properly insert macro undef statements into macro-containers (bug 234591) - <<CDT 5.0>> * #61.0# - properly insert macro undef statements into macro-containers (bug 234591) - <<CDT 5.0>>
* *
* 70.0 - cleaned up templates, fixes bug 236197 * 70.0 - cleaned up templates, fixes bug 236197
* 71.0 - proper support for anonymous unions, bug 206450
*/ */
public static final int LINKAGES = Database.DATA_AREA; public static final int LINKAGES = Database.DATA_AREA;

View file

@ -279,6 +279,10 @@ public class PDOMASTAdapter {
public IBinding getOwner() throws DOMException { public IBinding getOwner() throws DOMException {
return fDelegate.getOwner(); return fDelegate.getOwner();
} }
public boolean isAnonymous() throws DOMException {
return fDelegate.isAnonymous();
}
} }
private static class AnonymousCPPBinding implements ICPPBinding { private static class AnonymousCPPBinding implements ICPPBinding {
@ -413,6 +417,10 @@ public class PDOMASTAdapter {
public boolean isSameType(IType type) { public boolean isSameType(IType type) {
return ((ICPPClassType) fDelegate).isSameType(type); return ((ICPPClassType) fDelegate).isSameType(type);
} }
public boolean isAnonymous() throws DOMException {
return ((ICPPClassType) fDelegate).isAnonymous();
}
} }

View file

@ -53,12 +53,14 @@ import org.eclipse.core.runtime.Status;
public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCompositeTypeScope, IPDOMMemberOwner, IIndexType, IIndexScope { public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCompositeTypeScope, IPDOMMemberOwner, IIndexType, IIndexScope {
private static final int MEMBERLIST = PDOMBinding.RECORD_SIZE; private static final int MEMBERLIST = PDOMBinding.RECORD_SIZE;
private static final int KEY = MEMBERLIST + 4; // byte private static final int KEY = MEMBERLIST + 4; // byte
private static final int ANONYMOUS= MEMBERLIST + 5;
@SuppressWarnings("hiding") @SuppressWarnings("hiding")
protected static final int RECORD_SIZE = PDOMBinding.RECORD_SIZE + 8; protected static final int RECORD_SIZE = MEMBERLIST + 6;
public PDOMCStructure(PDOM pdom, PDOMNode parent, ICompositeType compType) throws CoreException { public PDOMCStructure(PDOM pdom, PDOMNode parent, ICompositeType compType) throws CoreException {
super(pdom, parent, compType.getNameCharArray()); super(pdom, parent, compType.getNameCharArray());
setKind(compType); setKind(compType);
setAnonymous(compType);
// linked list is initialized by malloc zeroing allocated storage // linked list is initialized by malloc zeroing allocated storage
} }
@ -71,6 +73,7 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
if (newBinding instanceof ICompositeType) { if (newBinding instanceof ICompositeType) {
ICompositeType ct= (ICompositeType) newBinding; ICompositeType ct= (ICompositeType) newBinding;
setKind(ct); setKind(ct);
setAnonymous(ct);
super.update(linkage, newBinding); super.update(linkage, newBinding);
} }
} }
@ -83,6 +86,15 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
} }
} }
private void setAnonymous(ICompositeType ct) throws CoreException {
try {
pdom.getDB().putByte(record + ANONYMOUS, (byte) (ct.isAnonymous() ? 1 : 0));
} catch (DOMException e) {
throw new CoreException(Util.createStatus(e));
}
}
@Override @Override
public void accept(IPDOMVisitor visitor) throws CoreException { public void accept(IPDOMVisitor visitor) throws CoreException {
super.accept(visitor); super.accept(visitor);
@ -108,6 +120,15 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
} }
} }
public boolean isAnonymous() throws DOMException {
try {
return pdom.getDB().getByte(record + ANONYMOUS) != 0;
} catch (CoreException e) {
CCorePlugin.log(e);
return false;
}
}
private static class GetFields implements IPDOMVisitor { private static class GetFields implements IPDOMVisitor {
private List<IPDOMNode> fields = new ArrayList<IPDOMNode>(); private List<IPDOMNode> fields = new ArrayList<IPDOMNode>();
public boolean visit(IPDOMNode node) throws CoreException { public boolean visit(IPDOMNode node) throws CoreException {
@ -116,6 +137,13 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
if (IndexFilter.ALL_DECLARED_OR_IMPLICIT.acceptBinding(field)) { if (IndexFilter.ALL_DECLARED_OR_IMPLICIT.acceptBinding(field)) {
fields.add(node); fields.add(node);
} }
} else if (node instanceof ICompositeType) {
try {
if (((ICompositeType) node).isAnonymous()) {
return true; // visit children
}
} catch (DOMException e) {
}
} }
return false; return false;
} }
@ -151,12 +179,13 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
throw new CoreException(Status.OK_STATUS); throw new CoreException(Status.OK_STATUS);
} }
} }
} } else if (node instanceof ICompositeType) {
else if (node instanceof ICompositeType) { try {
char[] nchars= ((ICompositeType) node).getNameCharArray(); if (((ICompositeType) node).isAnonymous()) {
if (nchars.length > 0 && nchars[0] == '{') {
return true; // visit children return true; // visit children
} }
} catch (DOMException e) {
}
} }
return false; return false;
} }

View file

@ -167,9 +167,12 @@ class PDOMCPPClassScope implements ICPPClassScope, IIndexScope {
} }
list.add(binding); list.add(binding);
if (binding instanceof ICompositeType && nchars.length > 0 && nchars[0] == '{') { try {
if (binding instanceof ICompositeType && ((ICompositeType) binding).isAnonymous()) {
return true; // visit children return true; // visit children
} }
} catch (DOMException e) {
}
} }
return false; return false;
} }

View file

@ -343,4 +343,8 @@ class PDOMCPPClassSpecialization extends PDOMCPPSpecialization implements
result+=" <"+map.keyAt(i)+"=>"+getArgumentMap().getAt(i)+">"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ result+=" <"+map.keyAt(i)+"=>"+getArgumentMap().getAt(i)+">"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
return result; return result;
} }
public boolean isAnonymous() {
return false;
}
} }

View file

@ -57,11 +57,13 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
private static final int FIRSTBASE = PDOMCPPBinding.RECORD_SIZE + 0; private static final int FIRSTBASE = PDOMCPPBinding.RECORD_SIZE + 0;
private static final int KEY = PDOMCPPBinding.RECORD_SIZE + 4; // byte private static final int MEMBERLIST = PDOMCPPBinding.RECORD_SIZE + 4;
private static final int KEY = PDOMCPPBinding.RECORD_SIZE + 8; // byte
private static final int ANONYMOUS= PDOMCPPBinding.RECORD_SIZE + 9; // byte
private static final int MEMBERLIST = PDOMCPPBinding.RECORD_SIZE + 8;
@SuppressWarnings("hiding") @SuppressWarnings("hiding")
protected static final int RECORD_SIZE = PDOMCPPBinding.RECORD_SIZE + 12; protected static final int RECORD_SIZE = PDOMCPPBinding.RECORD_SIZE + 10;
private ICPPClassScope fScope; private ICPPClassScope fScope;
@ -70,6 +72,7 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
super(pdom, parent, classType.getNameCharArray()); super(pdom, parent, classType.getNameCharArray());
setKind(classType); setKind(classType);
setAnonymous(classType);
// linked list is initialized by storage being zero'd by malloc // linked list is initialized by storage being zero'd by malloc
} }
@ -82,6 +85,7 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
if (newBinding instanceof ICPPClassType) { if (newBinding instanceof ICPPClassType) {
ICPPClassType ct= (ICPPClassType) newBinding; ICPPClassType ct= (ICPPClassType) newBinding;
setKind(ct); setKind(ct);
setAnonymous(ct);
super.update(linkage, newBinding); super.update(linkage, newBinding);
} }
} }
@ -94,6 +98,14 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
} }
} }
private void setAnonymous(ICPPClassType ct) throws CoreException {
try {
pdom.getDB().putByte(record + ANONYMOUS, (byte) (ct.isAnonymous() ? 1 : 0));
} catch (DOMException e) {
throw new CoreException(Util.createStatus(e));
}
}
@Override @Override
public void addChild(PDOMNode member) throws CoreException { public void addChild(PDOMNode member) throws CoreException {
pdom.removeCachedResult(record+PDOMCPPLinkage.CACHE_MEMBERS); pdom.removeCachedResult(record+PDOMCPPLinkage.CACHE_MEMBERS);
@ -329,6 +341,15 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
} }
} }
public boolean isAnonymous() throws DOMException {
try {
return pdom.getDB().getByte(record + ANONYMOUS) != 0;
} catch (CoreException e) {
CCorePlugin.log(e);
return false;
}
}
@Override @Override
public boolean isGloballyQualified() throws DOMException { public boolean isGloballyQualified() throws DOMException {
try { try {

View file

@ -233,4 +233,8 @@ class PDOMCPPDeferredClassInstance extends PDOMCPPSpecialization implements ICPP
return IType.EMPTY_TYPE_ARRAY; return IType.EMPTY_TYPE_ARRAY;
} }
} }
public boolean isAnonymous() {
return false;
}
} }

View file

@ -270,4 +270,8 @@ class PDOMCPPUnknownClassType extends PDOMCPPBinding implements ICPPClassScope,
public IASTName getUnknownName() { public IASTName getUnknownName() {
return new CPPASTName(getNameCharArray()); return new CPPASTName(getNameCharArray());
} }
public boolean isAnonymous() {
return false;
}
} }

View file

@ -1164,7 +1164,7 @@ public class CompletionTests extends AbstractContentAssistTest {
} }
// void test() {struct s206450 x; x./*cursor*/ // void test() {struct s206450 x; x./*cursor*/
public void _testNestedAnonymousStructs_Bug206450() throws Exception { public void testNestedAnonymousStructs_Bug206450() throws Exception {
final String[] expected= {"a1", "a2", "u1", "u2", "a4", "b", "s206450"}; final String[] expected= {"a1", "a2", "u1", "u2", "a4", "b", "s206450"};
assertCompletionResults(expected); assertCompletionResults(expected);
} }

View file

@ -661,7 +661,7 @@ public class CompletionTests_PlainC extends AbstractContentAssistTest {
//void f() {e/*cursor*/ //void f() {e/*cursor*/
public void testEnums_FunctionScope() throws Exception { public void testEnums_FunctionScope() throws Exception {
final String[] expected= { final String[] expected= {
"e11", "e12" "e11", "e12", "e21", "e22"
}; };
assertCompletionResults(expected); assertCompletionResults(expected);
} }
@ -950,7 +950,7 @@ public class CompletionTests_PlainC extends AbstractContentAssistTest {
} }
// void test() {struct s206450 x; x./*cursor*/ // void test() {struct s206450 x; x./*cursor*/
public void _testNestedAnonymousStructs_Bug206450() throws Exception { public void testNestedAnonymousStructs_Bug206450() throws Exception {
final String[] expected= {"a1", "a2", "u1", "u2", "a4", "b"}; final String[] expected= {"a1", "a2", "u1", "u2", "a4", "b"};
assertCompletionResults(expected); assertCompletionResults(expected);
} }

View file

@ -154,4 +154,8 @@ public class C99Structure extends PlatformObject implements IC99Binding, ICompos
} }
return null; return null;
} }
public boolean isAnonymous() {
return name == null || name.length() == 0;
}
} }