diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 2332410b4e1..8fee9d8a9bd 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -5244,4 +5244,22 @@ public class AST2Tests extends AST2BaseTest { 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); + } + } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java index 8b1590f3034..7ca51ad3aba 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java @@ -728,12 +728,16 @@ public class IndexCPPBindingResolutionBugs extends IndexBindingResolutionTestBas // struct { // int var1; // }; + // struct { + // int var2; + // } hide; // }; // #include "header.h" // void test() { // union outer x; // x.var1=1; + // x.var2= 2; // must be a problem // } public void testAnonymousStruct_Bug216791() throws DOMException { // struct @@ -743,6 +747,8 @@ public class IndexCPPBindingResolutionBugs extends IndexBindingResolutionTestBas IScope outer= f.getCompositeTypeOwner().getScope(); assertTrue(outer instanceof ICPPClassScope); assertEquals("outer", outer.getScopeName().toString()); + + getProblemFromASTName("var2=", 4); } // namespace ns { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IBinding.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IBinding.java index 58adac386c2..4da4022543a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IBinding.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IBinding.java @@ -12,6 +12,7 @@ package org.eclipse.cdt.core.dom.ast; 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.ICPPTemplateDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter; @@ -53,8 +54,10 @@ public interface IBinding extends IAdaptable { * declared else where). *
Possible owners are:
*
{@link IFunction}: for parameters, local types, variables, enumerators, labels and using declarations;
- *
{@link ICompositeType}: for class-, struct- and union-members, even if the composite type is anonymous;
+ *
{@link ICPPClassType}: for class-, struct- and union-members, even if the composite type is anonymous;
* also for enumerators and using declarations;
+ *
{@link ICompositeType}: for struct- and union-members, even if the composite type is anonymous;
+ * also for anonymous structs or unions found within another struct;
*
{@link ICPPNamespace}: for global types, functions, variables, enumerators, namespaces and using declarations;
*
null
: for types, functions, variables, enumerators, namespaces and using declarations;
* @since 5.1
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ICompositeType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ICompositeType.java
index 509f4ceb3b3..849107ee4dd 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ICompositeType.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ICompositeType.java
@@ -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 {
@@ -23,6 +25,18 @@ public interface ICompositeType extends IBinding, IType {
*/
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.
+ *
struct Outer { + * struct {int a;}; // anonymous + * struct {int b;} c; // not anonymous + * } + *+ * @since 5.1 + */ + boolean isAnonymous() throws DOMException; + /** * Returns the fields for this type. * diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CStructure.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CStructure.java index 6c0ef9e7c60..427f729b041 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CStructure.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CStructure.java @@ -59,6 +59,9 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte public int getKey() throws DOMException { throw new DOMException( this ); } + public boolean isAnonymous() throws DOMException { + throw new DOMException( this ); + } } private IASTName [] declarations = null; @@ -281,6 +284,29 @@ public class CStructure extends PlatformObject implements ICompositeType, ICInte 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; } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassSpecialization.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassSpecialization.java index 698852e6cbe..211652f8877 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassSpecialization.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassSpecialization.java @@ -309,4 +309,23 @@ public class CPPClassSpecialization extends CPPSpecialization implements ICPPCla public ICPPClassType[] getNestedClasses() throws DOMException { 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; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassTemplate.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassTemplate.java index 5c9432c0c1d..9446e62a105 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassTemplate.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassTemplate.java @@ -252,4 +252,8 @@ public class CPPClassTemplate extends CPPTemplateDefinition implements public String toString() { return ASTTypeUtil.getType(this); } + + public boolean isAnonymous() { + return false; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassType.java index 5da70a16c00..0e07fff09f0 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassType.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassType.java @@ -138,6 +138,9 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp public ICPPClassType[] getNestedClasses() throws DOMException { throw new DOMException( this ); } + public boolean isAnonymous() throws DOMException { + throw new DOMException( this ); + } } private class FindDefinitionAction extends CPPASTVisitor { @@ -462,4 +465,20 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp public IBinding getOwner() throws DOMException { 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; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateTemplateParameter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateTemplateParameter.java index a8567bf9c4e..843766eca14 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateTemplateParameter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPTemplateTemplateParameter.java @@ -223,4 +223,8 @@ public class CPPTemplateTemplateParameter extends CPPTemplateParameter implement public IASTName getUnknownName() { return new CPPASTName(getNameCharArray()); } + + public boolean isAnonymous() { + return false; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPUnknownClass.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPUnknownClass.java index 12cc831600c..922826e360d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPUnknownClass.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPUnknownClass.java @@ -110,4 +110,8 @@ public class CPPUnknownClass extends CPPUnknownBinding implements ICPPUnknownCla public ICPPClassType[] getNestedClasses() { return ICPPClassType.EMPTY_CLASS_ARRAY; } + + public boolean isAnonymous() { + return false; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/c/CompositeCStructure.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/c/CompositeCStructure.java index 6748c8fc2e9..f0c646b30c7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/c/CompositeCStructure.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/c/CompositeCStructure.java @@ -1,12 +1,12 @@ /******************************************************************************* - * 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 * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Andrew Ferguson (Symbian) - Initial implementation + * Andrew Ferguson (Symbian) - Initial implementation *******************************************************************************/ package org.eclipse.cdt.internal.core.index.composite.c; @@ -51,4 +51,8 @@ class CompositeCStructure extends CompositeCBinding implements ICompositeType, I @Override public Object clone() {fail(); return null;} + + public boolean isAnonymous() throws DOMException { + return ((ICompositeType)rbinding).isAnonymous(); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/cpp/CompositeCPPClassType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/cpp/CompositeCPPClassType.java index 55f68c99766..4a1707a81e1 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/cpp/CompositeCPPClassType.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/cpp/CompositeCPPClassType.java @@ -6,7 +6,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * 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; @@ -174,4 +175,8 @@ class CompositeCPPClassType extends CompositeCPPBinding implements ICPPClassType public boolean isSameType(IType type) { return ((ICPPClassType)rbinding).isSameType(type); } + + public boolean isAnonymous() throws DOMException { + return ((ICPPClassType)rbinding).isAnonymous(); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/cpp/CompositeCPPUnknownClassType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/cpp/CompositeCPPUnknownClassType.java index 196db2a224f..0b4a5c44e3e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/cpp/CompositeCPPUnknownClassType.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/composite/cpp/CompositeCPPUnknownClassType.java @@ -135,4 +135,8 @@ class CompositeCPPUnknownClassType extends CompositeCPPBinding implements ICPPUn public IASTName getUnknownName() { return ((ICPPUnknownClassType) rbinding).getUnknownName(); } + + public boolean isAnonymous() { + return false; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java index 2d8ae2e09ce..12a0c60c160 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java @@ -177,6 +177,7 @@ public class PDOM extends PlatformObject implements IPDOM { * #61.0# - properly insert macro undef statements into macro-containers (bug 234591) - <