mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-01 06:05:24 +02:00
Added typedefs to PDOM.
This commit is contained in:
parent
894657561b
commit
58aa2a2651
12 changed files with 310 additions and 11 deletions
|
@ -0,0 +1,88 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2006 QNX 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:
|
||||
* QNX - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.pdom.tests;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
||||
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.IField;
|
||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.internal.core.pdom.PDOM;
|
||||
|
||||
/**
|
||||
* @author Doug Schaefer
|
||||
*
|
||||
*/
|
||||
public class TypesTests extends PDOMTestBase {
|
||||
|
||||
protected ICProject project;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
project = createProject("types");
|
||||
}
|
||||
|
||||
public void testC() throws Exception {
|
||||
PDOM pdom = (PDOM)CCorePlugin.getPDOMManager().getPDOM(project);
|
||||
|
||||
// Get the binding for A::f
|
||||
IBinding [] CAs = pdom.findBindings(Pattern.compile("CA"));
|
||||
assertEquals(1, CAs.length);
|
||||
ICompositeType CA = (ICompositeType)CAs[0];
|
||||
IField [] CAfields = CA.getFields();
|
||||
assertEquals(1, CAfields.length);
|
||||
IField x = CAfields[0];
|
||||
assertEquals("x", x.getName());
|
||||
|
||||
// Make sure that there is a reference in g();
|
||||
IASTName[] xRefs = pdom.getReferences(x);
|
||||
assertEquals(1, xRefs.length);
|
||||
IASTFileLocation loc = xRefs[0].getFileLocation();
|
||||
assertEquals(offset(85, 75), loc.getNodeOffset());
|
||||
}
|
||||
|
||||
public void testCPP() throws Exception {
|
||||
PDOM pdom = (PDOM)CCorePlugin.getPDOMManager().getPDOM(project);
|
||||
|
||||
// Get the binding for A::f
|
||||
IBinding [] As = pdom.findBindings(Pattern.compile("A"));
|
||||
assertEquals(1, As.length);
|
||||
ICPPClassType A = (ICPPClassType)As[0];
|
||||
ICPPMethod[] Amethods = A.getMethods();
|
||||
assertEquals(1, Amethods.length);
|
||||
ICPPMethod f = Amethods[0];
|
||||
assertEquals("f", f.getName());
|
||||
|
||||
// Make sure that there is a reference in g();
|
||||
IASTName[] fRefs = pdom.getReferences(f);
|
||||
assertEquals(1, fRefs.length);
|
||||
IASTFileLocation loc = fRefs[0].getFileLocation();
|
||||
assertEquals(offset(84, 74), loc.getNodeOffset());
|
||||
}
|
||||
|
||||
public void test145351() throws Exception {
|
||||
PDOM pdom = (PDOM)CCorePlugin.getPDOMManager().getPDOM(project);
|
||||
IBinding [] bindings = pdom.findBindings(Pattern.compile("spinlock_t"));
|
||||
assertEquals(1, bindings.length);
|
||||
ITypedef spinlock_t = (ITypedef)bindings[0];
|
||||
IASTName [] refs = pdom.getReferences(spinlock_t);
|
||||
assertEquals(1, refs.length);
|
||||
IASTFileLocation loc = refs[0].getFileLocation();
|
||||
assertEquals(offset(44, 40), loc.getNodeOffset());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
typedef struct {
|
||||
int x;
|
||||
} spinlock_t;
|
||||
|
||||
spinlock_t global_bh_lock;
|
|
@ -0,0 +1,10 @@
|
|||
struct CA {
|
||||
int x;
|
||||
};
|
||||
|
||||
typedef struct CA * CX;
|
||||
|
||||
int g() {
|
||||
CX x;
|
||||
return x->x;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
class A {
|
||||
public:
|
||||
void f() {
|
||||
}
|
||||
};
|
||||
|
||||
typedef A * X;
|
||||
|
||||
void g() {
|
||||
X x;
|
||||
x->f();
|
||||
}
|
|
@ -30,6 +30,7 @@ import org.eclipse.cdt.core.dom.ast.IParameter;
|
|||
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||
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.dom.ast.c.ICASTElaboratedTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
|
||||
|
@ -68,6 +69,7 @@ public class PDOMCLinkage extends PDOMLinkage {
|
|||
public static final int CFIELD = PDOMLinkage.LAST_NODE_TYPE + 4;
|
||||
public static final int CENUMERATION = PDOMLinkage.LAST_NODE_TYPE + 5;
|
||||
public static final int CENUMERATOR = PDOMLinkage.LAST_NODE_TYPE + 6;
|
||||
public static final int CTYPEDEF = PDOMLinkage.LAST_NODE_TYPE + 7;
|
||||
|
||||
public ILanguage getLanguage() {
|
||||
return new GCCLanguage();
|
||||
|
@ -138,7 +140,8 @@ public class PDOMCLinkage extends PDOMLinkage {
|
|||
if (pdomEnumeration instanceof PDOMCEnumeration)
|
||||
pdomBinding = new PDOMCEnumerator(pdom, parent, name,
|
||||
(PDOMCEnumeration)pdomEnumeration);
|
||||
}
|
||||
} else if (binding instanceof ITypedef)
|
||||
pdomBinding = new PDOMCTypedef(pdom, parent, name, (ITypedef)binding);
|
||||
}
|
||||
|
||||
if (pdomBinding != null)
|
||||
|
@ -184,6 +187,8 @@ public class PDOMCLinkage extends PDOMLinkage {
|
|||
return CENUMERATION;
|
||||
else if (binding instanceof IEnumerator)
|
||||
return CENUMERATOR;
|
||||
else if (binding instanceof ITypedef)
|
||||
return CTYPEDEF;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
@ -223,6 +228,8 @@ public class PDOMCLinkage extends PDOMLinkage {
|
|||
return new PDOMCEnumeration(pdom, record);
|
||||
case CENUMERATOR:
|
||||
return new PDOMCEnumerator(pdom, record);
|
||||
case CTYPEDEF:
|
||||
return new PDOMCTypedef(pdom, record);
|
||||
}
|
||||
|
||||
return super.getNode(record);
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
package org.eclipse.cdt.internal.core.pdom.dom.c;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
|
@ -52,7 +54,19 @@ public class PDOMCStructure extends PDOMMemberOwner implements ICompositeType {
|
|||
}
|
||||
|
||||
public IField[] getFields() throws DOMException {
|
||||
throw new PDOMNotImplementedError();
|
||||
try {
|
||||
ArrayList fields = new ArrayList();
|
||||
|
||||
for (PDOMMember member = getFirstMember(); member != null; member = member.getNextMember()) {
|
||||
if (member instanceof IField)
|
||||
fields.add(member);
|
||||
}
|
||||
|
||||
return (IField[])fields.toArray(new IField[fields.size()]);
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
return new IField[0];
|
||||
}
|
||||
}
|
||||
|
||||
public IField findField(String name) throws DOMException {
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2006 QNX 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:
|
||||
* QNX - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.pdom.dom.c;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||
import org.eclipse.cdt.internal.core.pdom.PDOM;
|
||||
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;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
||||
/**
|
||||
* @author Doug Schaefer
|
||||
*/
|
||||
public class PDOMCTypedef extends PDOMBinding implements ITypedef {
|
||||
|
||||
private static final int TYPE = PDOMBinding.RECORD_SIZE + 0;
|
||||
|
||||
protected static final int RECORD_SIZE = PDOMBinding.RECORD_SIZE + 4;
|
||||
|
||||
public PDOMCTypedef(PDOM pdom, PDOMNode parent, IASTName name, ITypedef typedef)
|
||||
throws CoreException {
|
||||
super(pdom, parent, name);
|
||||
|
||||
IType type = typedef.getType();
|
||||
PDOMNode typeNode = parent.getLinkage().addType(this, type);
|
||||
if (typeNode != null)
|
||||
pdom.getDB().putInt(record + TYPE, typeNode.getRecord());
|
||||
}
|
||||
|
||||
public PDOMCTypedef(PDOM pdom, int record) {
|
||||
super(pdom, record);
|
||||
}
|
||||
|
||||
protected int getRecordSize() {
|
||||
return RECORD_SIZE;
|
||||
}
|
||||
|
||||
public int getNodeType() {
|
||||
return PDOMCLinkage.CTYPEDEF;
|
||||
}
|
||||
|
||||
public IType getType() throws DOMException {
|
||||
try {
|
||||
int typeRec = pdom.getDB().getInt(record + TYPE);
|
||||
return (IType)getLinkage().getNode(typeRec);
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSameType(IType type) {
|
||||
throw new PDOMNotImplementedError();
|
||||
}
|
||||
|
||||
public Object clone() {
|
||||
throw new PDOMNotImplementedError();
|
||||
}
|
||||
|
||||
}
|
|
@ -19,7 +19,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
|
|||
import org.eclipse.cdt.internal.core.pdom.PDOM;
|
||||
import org.eclipse.cdt.internal.core.pdom.db.Database;
|
||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
|
||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
||||
/**
|
||||
|
@ -80,7 +79,9 @@ public class PDOMCPPBasicType extends PDOMNode implements ICPPBasicType {
|
|||
}
|
||||
|
||||
public IASTExpression getValue() throws DOMException {
|
||||
throw new PDOMNotImplementedError();
|
||||
// Returning null for now, not sure what needs to be here if anything
|
||||
// Values only seem to be used at type resolution time.
|
||||
return null;
|
||||
}
|
||||
|
||||
private char getFlags() throws CoreException {
|
||||
|
|
|
@ -173,7 +173,19 @@ public class PDOMCPPClassType extends PDOMMemberOwner implements ICPPClassType,
|
|||
}
|
||||
|
||||
public ICPPMethod[] getMethods() throws DOMException {
|
||||
throw new PDOMNotImplementedError();
|
||||
try {
|
||||
ArrayList methods = new ArrayList();
|
||||
|
||||
for (PDOMMember member = getFirstMember(); member != null; member = member.getNextMember()) {
|
||||
if (member instanceof ICPPMethod)
|
||||
methods.add(member);
|
||||
}
|
||||
|
||||
return (ICPPMethod[])methods.toArray(new ICPPMethod[methods.size()]);
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
return new ICPPMethod[0];
|
||||
}
|
||||
}
|
||||
|
||||
public ICPPClassType[] getNestedClasses() throws DOMException {
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.eclipse.cdt.core.dom.ast.IParameter;
|
|||
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceAlias;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||
|
@ -88,6 +89,7 @@ public class PDOMCPPLinkage extends PDOMLinkage {
|
|||
public static final int CPPPARAMETER = PDOMLinkage.LAST_NODE_TYPE + 9;
|
||||
public static final int CPPENUMERATION = PDOMLinkage.LAST_NODE_TYPE + 10;
|
||||
public static final int CPPENUMERATOR = PDOMLinkage.LAST_NODE_TYPE + 11;
|
||||
public static final int CPPTYPEDEF = PDOMLinkage.LAST_NODE_TYPE + 12;
|
||||
|
||||
public ILanguage getLanguage() {
|
||||
return new GPPLanguage();
|
||||
|
@ -159,6 +161,8 @@ public class PDOMCPPLinkage extends PDOMLinkage {
|
|||
if (pdomEnumeration instanceof PDOMCPPEnumeration)
|
||||
pdomBinding = new PDOMCPPEnumerator(pdom, parent, name,
|
||||
(PDOMCPPEnumeration)pdomEnumeration);
|
||||
} else if (binding instanceof ITypedef) {
|
||||
pdomBinding = new PDOMCPPTypedef(pdom, parent, name, (ITypedef)binding);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -233,6 +237,8 @@ public class PDOMCPPLinkage extends PDOMLinkage {
|
|||
return CPPENUMERATION;
|
||||
else if (binding instanceof IEnumerator)
|
||||
return CPPENUMERATOR;
|
||||
else if (binding instanceof ITypedef)
|
||||
return CPPTYPEDEF;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
@ -382,6 +388,8 @@ public class PDOMCPPLinkage extends PDOMLinkage {
|
|||
return new PDOMCPPEnumeration(pdom, record);
|
||||
case CPPENUMERATOR:
|
||||
return new PDOMCPPEnumerator(pdom, record);
|
||||
case CPPTYPEDEF:
|
||||
return new PDOMCPPTypedef(pdom, record);
|
||||
default:
|
||||
return super.getNode(record);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2006 QNX 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:
|
||||
* QNX - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.pdom.dom.cpp;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||
import org.eclipse.cdt.internal.core.pdom.PDOM;
|
||||
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;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
||||
/**
|
||||
* @author Doug Schaefer
|
||||
*/
|
||||
public class PDOMCPPTypedef extends PDOMBinding implements ITypedef {
|
||||
|
||||
private static final int TYPE = PDOMBinding.RECORD_SIZE + 0;
|
||||
|
||||
protected static final int RECORD_SIZE = PDOMBinding.RECORD_SIZE + 4;
|
||||
|
||||
public PDOMCPPTypedef(PDOM pdom, PDOMNode parent, IASTName name, ITypedef typedef)
|
||||
throws CoreException {
|
||||
super(pdom, parent, name);
|
||||
IType type = typedef.getType();
|
||||
PDOMNode typeNode = parent.getLinkage().addType(this, type);
|
||||
if (typeNode != null)
|
||||
pdom.getDB().putInt(record + TYPE, typeNode.getRecord());
|
||||
}
|
||||
|
||||
public PDOMCPPTypedef(PDOM pdom, int record) {
|
||||
super(pdom, record);
|
||||
}
|
||||
|
||||
protected int getRecordSize() {
|
||||
return RECORD_SIZE;
|
||||
}
|
||||
|
||||
public int getNodeType() {
|
||||
return PDOMCPPLinkage.CPPTYPEDEF;
|
||||
}
|
||||
|
||||
public IType getType() throws DOMException {
|
||||
try {
|
||||
PDOMNode node = getLinkage().getNode(pdom.getDB().getInt(record + TYPE));
|
||||
return node instanceof IType ? (IType)node : null;
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSameType(IType type) {
|
||||
throw new PDOMNotImplementedError();
|
||||
}
|
||||
|
||||
public Object clone() {
|
||||
throw new PDOMNotImplementedError();
|
||||
}
|
||||
|
||||
}
|
|
@ -16,7 +16,6 @@ import org.eclipse.cdt.core.dom.ast.DOMException;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
|
||||
|
@ -44,11 +43,9 @@ public class PDOMCPPVariable extends PDOMBinding implements ICPPVariable {
|
|||
if (nameParent instanceof IASTDeclarator) {
|
||||
IASTDeclarator declarator = (IASTDeclarator)nameParent;
|
||||
IType type = CPPVisitor.createType(declarator);
|
||||
if (type != null && type instanceof IBinding) {
|
||||
PDOMBinding pdomType = parent.getLinkage().adaptBinding((IBinding)type);
|
||||
if (pdomType != null)
|
||||
pdom.getDB().putInt(record + TYPE_OFFSET, pdomType.getRecord());
|
||||
}
|
||||
PDOMNode typeNode = parent.getLinkage().addType(this, type);
|
||||
if (typeNode != null)
|
||||
pdom.getDB().putInt(record + TYPE_OFFSET, typeNode.getRecord());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue