mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-19 23:15:24 +02:00
Anonymous unions and structs, bug 216791.
This commit is contained in:
parent
a39c03d796
commit
9cd75c484e
7 changed files with 150 additions and 24 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others.
|
* Copyright (c) 2006, 2008 Wind River Systems, Inc. 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
|
||||||
|
@ -22,9 +22,11 @@ import org.eclipse.cdt.core.dom.ast.IEnumerator;
|
||||||
import org.eclipse.cdt.core.dom.ast.IField;
|
import org.eclipse.cdt.core.dom.ast.IField;
|
||||||
import org.eclipse.cdt.core.dom.ast.IFunction;
|
import org.eclipse.cdt.core.dom.ast.IFunction;
|
||||||
import org.eclipse.cdt.core.dom.ast.IParameter;
|
import org.eclipse.cdt.core.dom.ast.IParameter;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||||
import org.eclipse.cdt.core.dom.ast.IType;
|
import org.eclipse.cdt.core.dom.ast.IType;
|
||||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||||
import org.eclipse.cdt.core.dom.ast.IVariable;
|
import org.eclipse.cdt.core.dom.ast.IVariable;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
|
||||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -327,4 +329,46 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
|
||||||
assertTrue(tAST.isSameType(tIndex));
|
assertTrue(tAST.isSameType(tIndex));
|
||||||
assertTrue(tIndex.isSameType(tAST));
|
assertTrue(tIndex.isSameType(tAST));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// struct outer {
|
||||||
|
// union {
|
||||||
|
// int var1;
|
||||||
|
// };
|
||||||
|
// };
|
||||||
|
|
||||||
|
// #include "header.h"
|
||||||
|
// void test() {
|
||||||
|
// struct outer x;
|
||||||
|
// x.var1=1;
|
||||||
|
// }
|
||||||
|
public void testAnonymousUnion_Bug216791() throws DOMException {
|
||||||
|
// struct
|
||||||
|
IBinding b = getBindingFromASTName("var1=", 4);
|
||||||
|
assertTrue(b instanceof IField);
|
||||||
|
IField f= (IField) b;
|
||||||
|
IScope outer= f.getCompositeTypeOwner().getScope();
|
||||||
|
assertTrue(outer instanceof ICCompositeTypeScope);
|
||||||
|
assertEquals("outer", outer.getScopeName().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// union outer {
|
||||||
|
// struct {
|
||||||
|
// int var1;
|
||||||
|
// };
|
||||||
|
// };
|
||||||
|
|
||||||
|
// #include "header.h"
|
||||||
|
// void test() {
|
||||||
|
// union outer x;
|
||||||
|
// x.var1=1;
|
||||||
|
// }
|
||||||
|
public void testAnonymousStruct_Bug216791() throws DOMException {
|
||||||
|
// struct
|
||||||
|
IBinding b = getBindingFromASTName("var1=", 4);
|
||||||
|
assertTrue(b instanceof IField);
|
||||||
|
IField f= (IField) b;
|
||||||
|
IScope outer= f.getCompositeTypeOwner().getScope();
|
||||||
|
assertTrue(outer instanceof ICCompositeTypeScope);
|
||||||
|
assertEquals("outer", outer.getScopeName().toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,14 @@ import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||||
import org.eclipse.cdt.core.dom.ast.ICompositeType;
|
import org.eclipse.cdt.core.dom.ast.ICompositeType;
|
||||||
import org.eclipse.cdt.core.dom.ast.IEnumeration;
|
import org.eclipse.cdt.core.dom.ast.IEnumeration;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IField;
|
||||||
import org.eclipse.cdt.core.dom.ast.IFunction;
|
import org.eclipse.cdt.core.dom.ast.IFunction;
|
||||||
import org.eclipse.cdt.core.dom.ast.IParameter;
|
import org.eclipse.cdt.core.dom.ast.IParameter;
|
||||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||||
import org.eclipse.cdt.core.dom.ast.IType;
|
import org.eclipse.cdt.core.dom.ast.IType;
|
||||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPDelegate;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPDelegate;
|
||||||
|
@ -708,4 +710,46 @@ public class IndexCPPBindingResolutionBugs extends IndexBindingResolutionTestBas
|
||||||
assertTrue(cl instanceof ICPPClassType);
|
assertTrue(cl instanceof ICPPClassType);
|
||||||
assertEquals("BAR", cl.getScope().getScopeName().toString());
|
assertEquals("BAR", cl.getScope().getScopeName().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// struct outer {
|
||||||
|
// union {
|
||||||
|
// int var1;
|
||||||
|
// };
|
||||||
|
// };
|
||||||
|
|
||||||
|
// #include "header.h"
|
||||||
|
// void test() {
|
||||||
|
// struct outer x;
|
||||||
|
// x.var1=1;
|
||||||
|
// }
|
||||||
|
public void testAnonymousUnion_Bug216791() throws DOMException {
|
||||||
|
// struct
|
||||||
|
IBinding b = getBindingFromASTName("var1=", 4);
|
||||||
|
assertTrue(b instanceof IField);
|
||||||
|
IField f= (IField) b;
|
||||||
|
IScope outer= f.getCompositeTypeOwner().getScope();
|
||||||
|
assertTrue(outer instanceof ICPPClassScope);
|
||||||
|
assertEquals("outer", outer.getScopeName().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// union outer {
|
||||||
|
// struct {
|
||||||
|
// int var1;
|
||||||
|
// };
|
||||||
|
// };
|
||||||
|
|
||||||
|
// #include "header.h"
|
||||||
|
// void test() {
|
||||||
|
// union outer x;
|
||||||
|
// x.var1=1;
|
||||||
|
// }
|
||||||
|
public void testAnonymousStruct_Bug216791() throws DOMException {
|
||||||
|
// struct
|
||||||
|
IBinding b = getBindingFromASTName("var1=", 4);
|
||||||
|
assertTrue(b instanceof IField);
|
||||||
|
IField f= (IField) b;
|
||||||
|
IScope outer= f.getCompositeTypeOwner().getScope();
|
||||||
|
assertTrue(outer instanceof ICPPClassScope);
|
||||||
|
assertEquals("outer", outer.getScopeName().toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
@ -189,6 +189,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
||||||
stateCount[0]++;
|
stateCount[0]++;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
CCorePlugin.getIndexManager().joinIndexer(8000, new NullProgressMonitor());
|
||||||
CCorePlugin.getIndexManager().addIndexerStateListener(listener);
|
CCorePlugin.getIndexManager().addIndexerStateListener(listener);
|
||||||
|
|
||||||
URL url= FileLocator.find(CTestPlugin.getDefault().getBundle(), new Path(locProject1), null);
|
URL url= FileLocator.find(CTestPlugin.getDefault().getBundle(), new Path(locProject1), null);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2007 Wind River Systems, Inc. and others.
|
* Copyright (c) 2007, 2008 Wind River Systems, Inc. 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
|
||||||
|
@ -15,6 +15,7 @@ import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.IPDOMNode;
|
import org.eclipse.cdt.core.dom.IPDOMNode;
|
||||||
import org.eclipse.cdt.core.dom.IPDOMVisitor;
|
import org.eclipse.cdt.core.dom.IPDOMVisitor;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.ICompositeType;
|
||||||
import org.eclipse.cdt.internal.core.pdom.db.IBTreeVisitor;
|
import org.eclipse.cdt.internal.core.pdom.db.IBTreeVisitor;
|
||||||
import org.eclipse.cdt.internal.core.pdom.db.IString;
|
import org.eclipse.cdt.internal.core.pdom.db.IString;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
@ -32,8 +33,9 @@ public class NamedNodeCollector implements IBTreeVisitor, IPDOMVisitor {
|
||||||
private final boolean caseSensitive;
|
private final boolean caseSensitive;
|
||||||
private IProgressMonitor monitor= null;
|
private IProgressMonitor monitor= null;
|
||||||
private int monitorCheckCounter= 0;
|
private int monitorCheckCounter= 0;
|
||||||
|
private boolean visitAnonymousClassTypes= false;
|
||||||
|
|
||||||
private List nodes = new ArrayList();
|
private List<PDOMNamedNode> nodes = new ArrayList<PDOMNamedNode>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collects all nodes with given name.
|
* Collects all nodes with given name.
|
||||||
|
@ -61,6 +63,10 @@ public class NamedNodeCollector implements IBTreeVisitor, IPDOMVisitor {
|
||||||
monitor= pm;
|
monitor= pm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setVisitAnonymousClassTypes(boolean val) {
|
||||||
|
visitAnonymousClassTypes= val;
|
||||||
|
}
|
||||||
|
|
||||||
final public int compare(int record) throws CoreException {
|
final public int compare(int record) throws CoreException {
|
||||||
if (monitor != null)
|
if (monitor != null)
|
||||||
checkCancelled();
|
checkCancelled();
|
||||||
|
@ -110,12 +116,12 @@ public class NamedNodeCollector implements IBTreeVisitor, IPDOMVisitor {
|
||||||
return true; // look for more
|
return true; // look for more
|
||||||
}
|
}
|
||||||
|
|
||||||
final protected List getNodeList() {
|
final protected List<PDOMNamedNode> getNodeList() {
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
final public PDOMNamedNode[] getNodes() {
|
final public PDOMNamedNode[] getNodes() {
|
||||||
return (PDOMNamedNode[])nodes.toArray(new PDOMNamedNode[nodes.size()]);
|
return nodes.toArray(new PDOMNamedNode[nodes.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
final public boolean visit(IPDOMNode node) throws CoreException {
|
final public boolean visit(IPDOMNode node) throws CoreException {
|
||||||
|
@ -127,6 +133,14 @@ public class NamedNodeCollector implements IBTreeVisitor, IPDOMVisitor {
|
||||||
if (compare(pb.getDBName()) == 0) {
|
if (compare(pb.getDBName()) == 0) {
|
||||||
addNode(pb);
|
addNode(pb);
|
||||||
}
|
}
|
||||||
|
else if (visitAnonymousClassTypes) {
|
||||||
|
if (pb instanceof ICompositeType) {
|
||||||
|
char[] nchars= pb.getNameCharArray();
|
||||||
|
if (nchars.length > 0 && nchars[0] == '{') {
|
||||||
|
return true; // visit children
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false; // don't visit children
|
return false; // don't visit children
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,9 +34,11 @@ import org.eclipse.cdt.core.dom.ast.IType;
|
||||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||||
import org.eclipse.cdt.core.dom.ast.IVariable;
|
import org.eclipse.cdt.core.dom.ast.IVariable;
|
||||||
import org.eclipse.cdt.core.dom.ast.c.ICBasicType;
|
import org.eclipse.cdt.core.dom.ast.c.ICBasicType;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
|
||||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||||
import org.eclipse.cdt.internal.core.Util;
|
import org.eclipse.cdt.internal.core.Util;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
|
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
|
||||||
|
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
|
||||||
import org.eclipse.cdt.internal.core.index.IIndexCBindingConstants;
|
import org.eclipse.cdt.internal.core.index.IIndexCBindingConstants;
|
||||||
import org.eclipse.cdt.internal.core.index.IIndexScope;
|
import org.eclipse.cdt.internal.core.index.IIndexScope;
|
||||||
import org.eclipse.cdt.internal.core.index.composite.CompositeScope;
|
import org.eclipse.cdt.internal.core.index.composite.CompositeScope;
|
||||||
|
@ -83,16 +85,15 @@ class PDOMCLinkage extends PDOMLinkage implements IIndexCBindingConstants {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
// assign names to anonymous types.
|
||||||
|
binding= PDOMASTAdapter.getAdapterForAnonymousASTBinding(binding);
|
||||||
|
if (binding == null || binding instanceof IParameter)
|
||||||
|
return null; // skip parameters
|
||||||
|
|
||||||
PDOMNode parent = getAdaptedParent(binding);
|
PDOMNode parent = getAdaptedParent(binding);
|
||||||
if (parent == null)
|
if (parent == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
// assign names to anonymous types.
|
|
||||||
binding= PDOMASTAdapter.getAdapterForAnonymousASTBinding(binding);
|
|
||||||
|
|
||||||
if (binding == null || binding instanceof IParameter)
|
|
||||||
return null; // skip parameters
|
|
||||||
|
|
||||||
if (binding instanceof IField) { // must be before IVariable
|
if (binding instanceof IField) { // must be before IVariable
|
||||||
if (parent instanceof IPDOMMemberOwner)
|
if (parent instanceof IPDOMMemberOwner)
|
||||||
pdomBinding = new PDOMCField(pdom, (IPDOMMemberOwner)parent, (IField) binding);
|
pdomBinding = new PDOMCField(pdom, (IPDOMMemberOwner)parent, (IField) binding);
|
||||||
|
@ -224,18 +225,33 @@ class PDOMCLinkage extends PDOMLinkage implements IIndexCBindingConstants {
|
||||||
IBinding scopeBinding = null;
|
IBinding scopeBinding = null;
|
||||||
if (scopeNode instanceof IASTCompoundStatement) {
|
if (scopeNode instanceof IASTCompoundStatement) {
|
||||||
return null;
|
return null;
|
||||||
} else if (scopeNode instanceof IASTTranslationUnit) {
|
}
|
||||||
return this;
|
if (scopeNode instanceof IASTTranslationUnit) {
|
||||||
} else {
|
boolean isGlobal= true;
|
||||||
IName scopeName = scope.getScopeName();
|
if (binding instanceof ICompositeType) {
|
||||||
if (scopeName instanceof IASTName) {
|
ICompositeType ct= (ICompositeType) binding;
|
||||||
scopeBinding = ((IASTName) scopeName).resolveBinding();
|
final IScope ctscope = ct.getCompositeScope();
|
||||||
|
if (ctscope != null) {
|
||||||
|
final IName myOrigScopeName = ctscope.getScopeName();
|
||||||
|
if (myOrigScopeName instanceof IASTName && myOrigScopeName.toCharArray().length == 0) {
|
||||||
|
scope= CVisitor.getContainingScope((IASTName) myOrigScopeName);
|
||||||
|
if (scope instanceof ICCompositeTypeScope) {
|
||||||
|
isGlobal= false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isGlobal) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IName scopeName = scope.getScopeName();
|
||||||
|
if (scopeName instanceof IASTName) {
|
||||||
|
scopeBinding = ((IASTName) scopeName).resolveBinding();
|
||||||
|
if (scopeBinding != null && scopeBinding != binding) {
|
||||||
|
return adaptBinding(scopeBinding);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (scopeBinding != null && scopeBinding != binding) {
|
|
||||||
PDOMBinding scopePDOMBinding= adaptBinding(scopeBinding);
|
|
||||||
if (scopePDOMBinding != null)
|
|
||||||
return scopePDOMBinding;
|
|
||||||
}
|
}
|
||||||
} catch (DOMException e) {
|
} catch (DOMException e) {
|
||||||
throw new CoreException(Util.createStatus(e));
|
throw new CoreException(Util.createStatus(e));
|
||||||
|
|
|
@ -152,6 +152,12 @@ public class PDOMCStructure extends PDOMBinding implements ICompositeType, ICCom
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (node instanceof ICompositeType) {
|
||||||
|
char[] nchars= ((ICompositeType) node).getNameCharArray();
|
||||||
|
if (nchars.length > 0 && nchars[0] == '{') {
|
||||||
|
return true; // visit children
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public void leave(IPDOMNode node) throws CoreException {
|
public void leave(IPDOMNode node) throws CoreException {
|
||||||
|
|
|
@ -367,7 +367,7 @@ class PDOMCPPClassType extends PDOMCPPBinding implements ICPPClassType,
|
||||||
// the class itself
|
// the class itself
|
||||||
visitor.visit(this);
|
visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
visitor.setVisitAnonymousClassTypes(true);
|
||||||
accept(visitor);
|
accept(visitor);
|
||||||
result= visitor.getBindings();
|
result= visitor.getBindings();
|
||||||
} catch (CoreException e) {
|
} catch (CoreException e) {
|
||||||
|
@ -386,6 +386,7 @@ class PDOMCPPClassType extends PDOMCPPBinding implements ICPPClassType,
|
||||||
if (getDBName().compare(name, true) == 0) {
|
if (getDBName().compare(name, true) == 0) {
|
||||||
visitor.visit(this);
|
visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
visitor.setVisitAnonymousClassTypes(true);
|
||||||
accept(visitor);
|
accept(visitor);
|
||||||
result = visitor.getBindings();
|
result = visitor.getBindings();
|
||||||
pdom.putCachedResult(key, result);
|
pdom.putCachedResult(key, result);
|
||||||
|
|
Loading…
Add table
Reference in a new issue