1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 161609 - more testcases for PDOM/index

This commit is contained in:
Vivian Kong 2006-11-06 15:43:39 +00:00
parent df871fe06b
commit b38a103a8e
17 changed files with 1139 additions and 0 deletions

View file

@ -0,0 +1,268 @@
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation.
* 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;
import java.util.regex.Pattern;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.core.runtime.NullProgressMonitor;
/**
* Tests for verifying whether the PDOM correctly stores information about
* C structs and unions.
*/
public class CCompositeTypeTests extends PDOMTestBase {
private ICProject project;
private PDOM pdom;
public static Test suite() {
return new TestSuite(CCompositeTypeTests.class);
}
protected void setUp() throws Exception {
CCompositeTypeTests foo = null;
project = createProject("compositeTypeTests");
pdom = (PDOM) CCorePlugin.getPDOMManager().getPDOM(project);
pdom.acquireReadLock();
}
protected void tearDown() throws Exception {
pdom.releaseReadLock();
}
//TODO PDOM does not distinguish between a struct or union in C
public void _testSimpleCStructureDistinction() throws Exception {
assertType(pdom, "SimpleCStructure", ICompositeType.class);
IIndexBinding[] bindings = pdom.findBindings(Pattern.compile("SimpleCStructure"), false, new IndexFilter(), new NullProgressMonitor());
assertEquals(1, bindings.length);
assertEquals(ICompositeType.k_struct, ((ICompositeType)bindings[0]).getKey());
}
// test struct definitions and struct member declarations in C
public void testSimpleCStructureDeclarations() throws Exception {
assertDeclarationCount(pdom, "SimpleCStructure", 1);
assertDeclarationCount(pdom, "SimpleCStructure::scsa", 1);
}
// test struct definitions and struct member definitions in C
public void testSimpleCStructureDefinitions() throws Exception {
assertDefinitionCount(pdom, "SimpleCStructure", 1);
assertDefinitionCount(pdom, "SimpleCStructure::scsa", 1);
}
// test struct definitions and struct member references in C
public void testSimpleCStructureReferences() throws Exception {
assertReferenceCount(pdom, "SimpleCStructure", 2);
assertReferenceCount(pdom, "SimpleCStructure::scsa", 2);
}
// test nesting of structs in C, they should not nest
public void testDeepCStructure() throws Exception {
assertType(pdom, "CStructure1", ICompositeType.class);
assertType(pdom, "CStructure2", ICompositeType.class);
assertType(pdom, "CStructure3", ICompositeType.class);
}
// test "nested" struct declarations in C, they should not nest
public void testDeepCStructureDeclarations() throws Exception {
assertDeclarationCount(pdom, "CStructure1", 1);
assertDeclarationCount(pdom, "CStructure1::CStructure2", 0);
assertDeclarationCount(pdom, "CStructure2", 1);
assertDeclarationCount(pdom, "CStructure1::CStructure2::CStructure3", 0);
assertDeclarationCount(pdom, "CStructure3", 1);
}
// test "nested" struct member declarations in C, they should not nest
public void testDeepCStructureMemberDeclarations() throws Exception {
assertDeclarationCount(pdom, "CStructure1::cs1a", 1);
assertDeclarationCount(pdom, "CStructure1::cs1b", 1);
assertDeclarationCount(pdom, "CStructure1::cs1c", 1);
assertDeclarationCount(pdom, "CStructure1::CStructure2::cs2b", 0);
assertDeclarationCount(pdom, "CStructure2::cs2b", 1);
assertDeclarationCount(pdom, "CStructure1::CStructure2::CStructure3::cs3a", 0);
assertDeclarationCount(pdom, "CStructure3::cs3a", 1);
}
// test "nested" struct definitions in C, they should not nest
public void testDeepCStructureDefinitions() throws Exception {
assertDefinitionCount(pdom, "CStructure1", 1);
assertDefinitionCount(pdom, "CStructure1::CStructure2", 0);
assertDefinitionCount(pdom, "CStructure2", 1);
assertDefinitionCount(pdom, "CStructure1::CStructure2::CStructure3", 0);
assertDefinitionCount(pdom, "CStructure3", 1);
}
// test "nested" struct member definitions in C, they should not nest
public void testDeepCStructureMemberDefinitions() throws Exception {
assertDefinitionCount(pdom, "CStructure1::cs1a", 1);
assertDefinitionCount(pdom, "CStructure1::cs1b", 1);
assertDefinitionCount(pdom, "CStructure1::cs1c", 1);
assertDefinitionCount(pdom, "CStructure1::CStructure2::cs2b", 0);
assertDefinitionCount(pdom, "CStructure2::cs2b", 1);
assertDefinitionCount(pdom, "CStructure1::CStructure2::CStructure3::cs3a", 0);
assertDefinitionCount(pdom, "CStructure3::cs3a", 1);
}
// test "nested" struct references in C, they should not nest
public void testDeepCStructureReferences() throws Exception {
assertReferenceCount(pdom, "CStructure1", 2);
assertReferenceCount(pdom, "CStructure1::CStructure2", 0);
assertReferenceCount(pdom, "CStructure2", 2);
assertReferenceCount(pdom, "CStructure1::CStructure2::CStructure3", 0);
assertReferenceCount(pdom, "CStructure3", 2);
}
// test "nested" struct member references in C, they should not nest
public void testDeepCStructureMemberReferences() throws Exception {
assertReferenceCount(pdom, "CStructure1::cs1a", 2);
assertReferenceCount(pdom, "CStructure1::cs1b", 3);
assertReferenceCount(pdom, "CStructure1::cs1c", 14);
assertReferenceCount(pdom, "CStructure1::CStructure2::cs2b", 0);
assertReferenceCount(pdom, "CStructure2::cs2b", 12);
assertReferenceCount(pdom, "CStructure1::CStructure2::CStructure3::cs3a", 0);
assertReferenceCount(pdom, "CStructure3::cs3a", 8);
}
// TODO PDOM does not distinguish between a struct or union in C
public void _testCUnionDistinction() throws Exception {
IIndexBinding[] bindings = pdom.findBindings(Pattern.compile("CUnion1"), false, new IndexFilter(), new NullProgressMonitor());
assertEquals(1, bindings.length);
assertEquals(ICompositeType.k_union, ((ICompositeType)bindings[0]).getKey());
}
//test union and "nested" union declarations in C, but there is no nesting in C
public void testCUnionDeclarations() throws Exception {
assertDeclarationCount(pdom, "CUnion1", 1);
assertDeclarationCount(pdom, "CUnion1::CUnion2", 0);
assertDeclarationCount(pdom, "CUnion2", 1);
}
//test union and "nested" union definitons in C, but there is no nesting in C
public void testCUnionDefinitions() throws Exception {
assertDefinitionCount(pdom, "CUnion1", 1);
assertDefinitionCount(pdom, "CUnion1::CUnion2", 0);
assertDefinitionCount(pdom, "CUnion2", 1);
}
//test union and "nested" union references in C, but there is no nesting in C
public void testCUnionReferences() throws Exception {
assertReferenceCount(pdom, "CUnion1", 2);
assertReferenceCount(pdom, "CUnion1::CUnion2", 0);
assertReferenceCount(pdom, "CUnion2", 2);
}
//test union member declarations in C
public void testCUnionMemberDeclarations() throws Exception {
assertDeclarationCount(pdom, "CUnion1::cu1a", 1);
assertDeclarationCount(pdom, "CUnion1::cu1d", 1);
}
//test union member defintions in C
public void testCUnionMemberDefinitions() throws Exception {
assertDefinitionCount(pdom, "CUnion1::cu1a", 1);
assertDefinitionCount(pdom, "CUnion1::cu1d", 1);
}
//test union member references in C
public void testCUnionMemberReferences() throws Exception {
assertReferenceCount(pdom, "CUnion1::cu1a", 2);
assertReferenceCount(pdom, "CUnion1::cu1d", 1);
}
// test "nested" unions and structs declarations in C, they should not nest
public void testCMixedDeclarations() throws Exception {
assertDeclarationCount(pdom, "CMixedS1::CMixedU1", 0);
assertDeclarationCount(pdom, "CMixedS1::CMixedU1::CMixedS2", 0);
assertDeclarationCount(pdom, "CMixedS1::CMixedU1::CMixedU2", 0);
assertDeclarationCount(pdom, "CMixedS1::CMixedS3", 0);
assertDeclarationCount(pdom, "CMixedU1", 1);
assertDeclarationCount(pdom, "CMixedS2", 1);
assertDeclarationCount(pdom, "CMixedU2", 1);
assertDeclarationCount(pdom, "CMixedS3", 1);
}
// test "nested" unions and structs definitions in C, they should not nest
public void testCMixedDefinitions() throws Exception {
assertDefinitionCount(pdom, "CMixedS1::CMixedU1", 0);
assertDefinitionCount(pdom, "CMixedS1::CMixedU1::CMixedS2", 0);
assertDefinitionCount(pdom, "CMixedS1::CMixedU1::CMixedU2", 0);
assertDefinitionCount(pdom, "CMixedS1::CMixedS3", 0);
assertDefinitionCount(pdom, "CMixedU1", 1);
assertDefinitionCount(pdom, "CMixedS2", 1);
assertDefinitionCount(pdom, "CMixedU2", 1);
assertDefinitionCount(pdom, "CMixedS3", 1);
}
// test "nested" unions and structs references in C, they should not nest
public void testCMixedReferences() throws Exception {
assertReferenceCount(pdom, "CMixedS1::CMixedU1", 0);
assertReferenceCount(pdom, "CMixedS1::CMixedU1::CMixedS2", 0);
assertReferenceCount(pdom, "CMixedS1::CMixedU1::CMixedU2", 0);
assertReferenceCount(pdom, "CMixedS1::CMixedS3", 0);
assertReferenceCount(pdom, "CMixedU1", 2);
assertReferenceCount(pdom, "CMixedS2", 2);
assertReferenceCount(pdom, "CMixedU2", 2);
assertReferenceCount(pdom, "CMixedS3", 2);
}
// test "nested" union members and struct members declarations in C, they should not nest
public void testCMixedMemberDeclarations() throws Exception {
assertDeclarationCount(pdom, "CMixedS1::CMixedU1::cmu1a", 0);
assertDeclarationCount(pdom, "CMixedS1::CMixedU1::CMixedS2::cms2a", 0);
assertDeclarationCount(pdom, "CMixedS1::CMixedU1::CMixedU2::cmu2a", 0);
assertDeclarationCount(pdom, "CMixedS1::CMixedS3::cms3a", 0);
assertDeclarationCount(pdom, "CMixedU1::cmu1a", 1);
assertDeclarationCount(pdom, "CMixedS2::cms2a", 1);
assertDeclarationCount(pdom, "CMixedU2::cmu2a", 1);
assertDeclarationCount(pdom, "CMixedS3::cms3a", 1);
}
// test "nested" union members and struct members definitions in C, they should not nest
public void testCMixedMemberDefinitions() throws Exception {
assertDefinitionCount(pdom, "CMixedS1::CMixedU1::cmu1a", 0);
assertDefinitionCount(pdom, "CMixedS1::CMixedU1::CMixedS2::cms2a", 0);
assertDefinitionCount(pdom, "CMixedS1::CMixedU1::CMixedU2::cmu2a", 0);
assertDefinitionCount(pdom, "CMixedS1::CMixedS3::cms3a", 0);
assertDefinitionCount(pdom, "CMixedU1::cmu1a", 1);
assertDefinitionCount(pdom, "CMixedS2::cms2a", 1);
assertDefinitionCount(pdom, "CMixedU2::cmu2a", 1);
assertDefinitionCount(pdom, "CMixedS3::cms3a", 1);
}
// test "nested" union members and struct members references in C, they should not nest
public void testCMixedMemberReferences() throws Exception {
assertReferenceCount(pdom, "CMixedS1::CMixedU1::cmu1a", 0);
assertReferenceCount(pdom, "CMixedS1::CMixedU1::CMixedS2::cms2a", 0);
assertReferenceCount(pdom, "CMixedS1::CMixedU1::CMixedU2::cmu2a", 0);
assertReferenceCount(pdom, "CMixedS1::CMixedS3::cms3a", 0);
assertReferenceCount(pdom, "CMixedU1::cmu1a", 2);
assertReferenceCount(pdom, "CMixedS2::cms2a", 2);
assertReferenceCount(pdom, "CMixedU2::cmu2a", 2);
assertReferenceCount(pdom, "CMixedS3::cms3a", 2);
}
}

View file

@ -8,6 +8,7 @@
* Contributors:
* QNX - Initial API and implementation
* Markus Schorn (Wind River Systems)
* IBM Corporation
*******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;
@ -18,9 +19,11 @@ import junit.framework.Test;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IName;
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.IField;
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.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope;
@ -28,6 +31,7 @@ import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
import org.eclipse.core.runtime.NullProgressMonitor;
/**
@ -102,4 +106,52 @@ public class ClassTests extends PDOMTestBase {
System.out.println(refs[i].getFileLocation().getNodeOffset());
assertEquals(5, refs.length);
}
/* Test friend relationships between classes */
public void _testFriend() throws Exception {
//TODO this test is failing
IBinding[] bindings = pdom.findBindings(Pattern.compile("ClassA"), false, new IndexFilter(), new NullProgressMonitor());
assertEquals(1, bindings.length);
ICPPClassType classA = (ICPPClassType) bindings[0];
IBinding[] friends = classA.getFriends();
assertEquals(1, friends.length);
bindings = pdom.findBindings(Pattern.compile("ClassC"), false, new IndexFilter(), new NullProgressMonitor());
assertEquals(bindings[0], friends[0]); //ClassC is a friend class of ClassA
bindings = pdom.findBindings(Pattern.compile("ClassC"), false, new IndexFilter(), new NullProgressMonitor());
assertEquals(1, bindings.length);
ICPPClassType classC = (ICPPClassType) bindings[0];
friends = classC.getFriends();
assertEquals(1, friends.length);
Pattern[] patterns = {Pattern.compile("ClassB"),Pattern.compile("functionB")};
bindings = pdom.findBindings(patterns, false, new IndexFilter(), new NullProgressMonitor());
assertEquals(bindings[0], friends[0]); //functionB is a friend of ClassC
}
public void testConstructor() throws Exception {
//TODO PDOM doesn't have information on constructor
IBinding[] bindings = pdom.findBindings(Pattern.compile("Class1"), false, new IndexFilter(), new NullProgressMonitor());
assertEquals(2, bindings.length);
assertTrue(bindings[0] instanceof ICPPClassType);
assertTrue(bindings[1] instanceof ICPPMethod);
IName[] decls = pdom.findNames(bindings[1], IIndex.FIND_DECLARATIONS_DEFINITIONS);
assertEquals(2, decls.length);
IASTFileLocation loc = decls[0].getFileLocation();
assertEquals(offset("constructor.cpp","Class1(int num);"), loc.getNodeOffset()); //character offset
loc = decls[1].getFileLocation();
assertEquals(offset("constructor.cpp","Class1::Class1") + 8, loc.getNodeOffset()); //character offset
/* Member initialization */
bindings = pdom.findBindings(Pattern.compile("number"), false, new IndexFilter(), new NullProgressMonitor());
assertEquals(1, bindings.length);
IName[] refs = pdom.findNames(bindings[0], IIndex.FIND_REFERENCES);
assertEquals(1, refs.length);
loc = refs[0].getFileLocation();
assertEquals(offset("constructor.cpp","number(num)"), loc.getNodeOffset()); //character offset
assertEquals(bindings[0], ((PDOMName)refs[0]).resolveBinding());
}
}

View file

@ -0,0 +1,222 @@
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation.
* 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;
import java.util.regex.Pattern;
import junit.framework.Test;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceAlias;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
/**
* Tests for verifying whether the PDOM correctly stores information about
* C++ namespaces.
*
* @author Vivian Kong
*/
public class NamespaceTests extends PDOMTestBase {
protected ICProject project;
protected PDOM pdom;
protected IProgressMonitor NULL_MONITOR = new NullProgressMonitor();
protected IndexFilter INDEX_FILTER = new IndexFilter();
public static Test suite() {
return suite(NamespaceTests.class);
}
protected void setUp() throws Exception {
if (pdom == null) {
ICProject project = createProject("namespaceTests", true);
pdom = (PDOM)CCorePlugin.getPDOMManager().getPDOM(project);
}
pdom.acquireReadLock();
}
protected void tearDown() throws Exception {
pdom.releaseReadLock();
}
public void _testAlias() throws Exception {
/* Find all the namespace */
IBinding[] namespaces = pdom.findBindings(Pattern.compile("namespace1"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace);
ICPPNamespace namespace1 = (ICPPNamespace) namespaces[0];
IBinding[] members = namespace1.getMemberBindings();
assertEquals(1, members.length);
assertTrue(members[0] instanceof ICPPNamespace);
assertEquals("namespace2", ((ICPPNamespace) members[0]).getName()); //nested namespace
ICPPNamespace namespace2 = (ICPPNamespace) members[0];
namespaces = pdom.findBindings(Pattern.compile("namespaceNew"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace);
assertTrue(namespaces[0] instanceof ICPPNamespaceAlias);
ICPPNamespaceAlias namespaceAlias = (ICPPNamespaceAlias) namespaces[0];
//TODO PDOM has no alias information
// namespace2 and namespaceAlias should be referencing the same namespace
assertEquals(namespace2, namespaceAlias.getBinding());
}
public void testNested() throws Exception {
/* Find deeply nested namespace */
Pattern[] patterns = {Pattern.compile("namespace1"), Pattern.compile("namespace2"), Pattern.compile("namespace3")};
IBinding[] namespaces = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace);
}
public void testMemberDefinition() throws Exception {
/* Find the definition of a member declared in a namespace */
Pattern[] patterns = {Pattern.compile("namespace1"), Pattern.compile("namespace2"), Pattern.compile("foo")};
IBinding[] members = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, members.length);
assertTrue(members[0] instanceof ICPPFunction);
IName[] decls = pdom.findNames(members[0], IIndex.FIND_DECLARATIONS);
assertEquals(1, decls.length);
IASTFileLocation loc = decls[0].getFileLocation();
assertEquals(offset("namespace.cpp", "void foo()") + 5, loc.getNodeOffset()); //character offset
IName[] defs = pdom.findNames(members[0], IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length);
loc = defs[0].getFileLocation();
assertEquals(offset("namespace.cpp", "::foo()") + 2, loc.getNodeOffset()); //character offset
}
public void testExtend() throws Exception {
/* Extending a namespace */
IBinding[] namespaces = pdom.findBindings(Pattern.compile("ns1"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace);
ICPPNamespace namespace1 = (ICPPNamespace) namespaces[0];
Pattern[] patterns = {Pattern.compile("ns1"), Pattern.compile("c")};
IBinding[] members = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, members.length); //c was added by extending the namespace
}
public void testOverload() throws Exception {
//Function overloading in namespace
Pattern[] patterns = {Pattern.compile("ns3"), Pattern.compile("blah")};
IBinding[] functions = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, functions.length);
assertTrue(functions[0] instanceof ICPPFunction);
ICPPFunction function = (ICPPFunction) functions[0];
IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("overload.cpp","void blah(char)") + 5, loc.getNodeOffset()); //character offset
IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS_DEFINITIONS);
assertEquals(1, decls.length);
loc = decls[0].getFileLocation();
assertEquals(offset("overload.cpp","void blah(char)") + 5, loc.getNodeOffset()); //character offset
IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES);
assertEquals(1, refs.length);
loc = refs[0].getFileLocation();
assertEquals(offset("overload.cpp","blah('a')"), loc.getNodeOffset()); //character offset
}
public void _testUnnamed() throws Exception {
//TODO test case is failing - see Bugzilla 162226
/* Unnamed Namespace */
IBinding[] functions = pdom.findBindings(Pattern.compile("function1"), true, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, functions.length);
assertTrue(functions[0] instanceof ICPPFunction);
ICPPFunction function = (ICPPFunction) functions[0];
IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("unnamed.cpp","void function1()") + 5, loc.getNodeOffset()); //character offset
IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS_DEFINITIONS);
assertEquals(1, decls.length);
loc = decls[0].getFileLocation();
assertEquals(offset("unnamed.cpp","void function1()") + 5, loc.getNodeOffset()); //character offset
IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES);
assertEquals(1, refs.length);
loc = refs[0].getFileLocation();
assertEquals(offset("unnamed.cpp","function1();"), loc.getNodeOffset()); //character offset
}
public void testFriend() throws Exception {
/* Friend in namespace - function2 is not in Class1*/
// Bugzilla 162011
IBinding[] functions = pdom.findBindings(Pattern.compile("function2"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, functions.length);
assertTrue(functions[0] instanceof ICPPFunction);
ICPPFunction function = (ICPPFunction) functions[0];
IName[] defs = pdom.findNames(function, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("friend.cpp","void function2(Class1){};") + 5, loc.getNodeOffset()); //character offset
IName[] decls = pdom.findNames(function, IIndex.FIND_DECLARATIONS);
assertEquals(1, decls.length);
loc = decls[0].getFileLocation();
assertEquals(offset("friend.cpp","friend void function2(Class1);") + 12, loc.getNodeOffset()); //character offset
IName[] refs = pdom.findNames(function, IIndex.FIND_REFERENCES);
assertEquals(1, refs.length);
loc = refs[0].getFileLocation();
assertEquals(offset("friend.cpp","ns4::function2(element)") + 5, loc.getNodeOffset()); //character offset
}
public void testUsingDirective() throws Exception {
//TODO need to test for PDOM? or is it more for compiler?
Pattern[] patterns = {Pattern.compile("ns4"), Pattern.compile("element")};
IBinding[] variables = pdom.findBindings(patterns, false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, variables.length);
assertTrue(variables[0] instanceof ICPPVariable);
ICPPVariable variable1 = (ICPPVariable) variables[0];
IName[] defs = pdom.findNames(variable1, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("friend.cpp","Class1 element;") + 7, loc.getNodeOffset()); //character offset
IName[] decls = pdom.findNames(variable1, IIndex.FIND_DECLARATIONS);
assertEquals(0, decls.length);
//TODO: should this be 2?
IName[] refs = pdom.findNames(variable1, IIndex.FIND_REFERENCES);
assertEquals(0, refs.length);
}
}

View file

@ -0,0 +1,292 @@
/*******************************************************************************
* Copyright (c) 2006 IBM Corporation.
* 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;
import java.util.regex.Pattern;
import junit.framework.Test;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.ICProject;
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.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
/**
* Test the correctness of C/C++ searches
*
* @author Vivian Kong
*
*/
public class PDOMSearchTest extends PDOMTestBase {
protected ICProject project;
protected PDOM pdom;
protected IProgressMonitor NULL_MONITOR = new NullProgressMonitor();
protected IndexFilter INDEX_FILTER = new IndexFilter();
public static Test suite() {
return suite(PDOMSearchTest.class);
}
protected void setUp() throws Exception {
if (pdom == null) {
ICProject project = createProject("searchTests", true);
pdom = (PDOM)CCorePlugin.getPDOMManager().getPDOM(project);
}
pdom.acquireReadLock();
}
protected void tearDown() throws Exception {
pdom.releaseReadLock();
}
/**
* Test the members inside namespaces
*/
public void testNamespaces() throws Exception {
/* Members in the namespace */
IBinding[] namespaces = pdom.findBindings(Pattern.compile("namespace1"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace);
ICPPNamespace namespace1 = (ICPPNamespace) namespaces[0];
/* Consistent search results */
// Searching for "namespace1::namespace2"
Pattern[] patterns = { Pattern.compile("namespace1"), Pattern.compile("namespace2") };
namespaces = pdom.findBindings(patterns, true, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace);
ICPPNamespace namespace2 = (ICPPNamespace) namespaces[0];
// Searching for "namespace2"
namespaces = pdom.findBindings(Pattern.compile("namespace2"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, namespaces.length);
assertTrue(namespaces[0] instanceof ICPPNamespace);
assertEquals(namespace2, namespaces[0]);
/* Namespace references */
IName[] refs = pdom.findNames(namespace1,IIndex.FIND_REFERENCES);
assertEquals(3, refs.length);
IASTFileLocation loc = refs[0].getFileLocation();
assertEquals(offset("main.cpp","namespace1::Class1"), loc.getNodeOffset()); //character offset
loc = refs[1].getFileLocation();
assertEquals(offset("Class1.cpp","namespace1::Class1::~Class1()"), loc.getNodeOffset()); //character offset
loc = refs[2].getFileLocation();
assertEquals(offset("Class1.cpp","namespace1::Class1::Class1()"), loc.getNodeOffset()); //character offset
/* Namespace declaration */
IName[] decls = pdom.findNames(namespace1, IIndex.FIND_DECLARATIONS);
assertEquals(0, decls.length);
/* Namespace definition */
IName[] defs = pdom.findNames(namespace1, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length);
loc = defs[0].getFileLocation();
assertEquals(offset("Class1.h","namespace namespace1") + 10, loc.getNodeOffset()); //character offset
}
public void testClasses() throws Exception {
// Bugzilla 160913
// classes and nested classes
/* Search for "Class1" */
IBinding[] class1s = pdom.findBindings(Pattern.compile("Class1"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(4, class1s.length);
assertTrue(class1s[0] instanceof ICPPClassType);
assertTrue(class1s[1] instanceof ICPPClassType);
assertTrue(class1s[2] instanceof ICPPMethod);
assertTrue(class1s[3] instanceof ICPPClassType);
/** result #1 * */
ICPPClassType class1 = (ICPPClassType) class1s[0];
assertEquals("Class1", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(class1)));
IBinding[] methods = class1.getMethods();
assertEquals(0, methods.length);
/** result #2 * */
ICPPClassType class2 = (ICPPClassType) class1s[1];
assertEquals("namespace1::Class1", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(class2)));
/* Members in this class */
// methods
methods = class2.getMethods();
assertEquals(2, methods.length);
assertEquals("Class1", methods[0].getName());
assertEquals("~Class1", methods[1].getName());
// nested class
IBinding[] nested = class2.getNestedClasses();
assertEquals(1, nested.length);
assertEquals("Class2", nested[0].getName());
// fields
IBinding[] fields = class2.getFields();
assertEquals(2, fields.length);
assertEquals("class1x", fields[0].getName());
assertEquals("class1y", fields[1].getName());
/** result #3 * */
ICPPMethod method3 = (ICPPMethod) class1s[2];
assertEquals("namespace1::Class1::Class1", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(method3)));
assertEquals(method3, methods[0]);
/** result #4 * */
ICPPClassType class4 = (ICPPClassType) class1s[3];
assertEquals("namespace1::namespace2::Class1", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(class4)));
methods = class4.getMethods();
assertEquals(0, methods.length);
/* Search for "Class2" */
IBinding[] class2s = pdom.findBindings(Pattern.compile("Class2"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(4, class2s.length);
assertTrue(class2s[0] instanceof ICPPClassType);
assertTrue(class2s[1] instanceof ICPPMethod);
assertTrue(class2s[2] instanceof ICPPClassType);
assertTrue(class2s[3] instanceof ICPPClassType);
/** result #1 * */
ICPPClassType cls1 = (ICPPClassType) class2s[0];
assertEquals("Class2", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(cls1)));
methods = cls1.getMethods();
assertEquals(3, methods.length);
assertEquals("Class2", methods[0].getName());
assertEquals("~Class2", methods[1].getName());
assertEquals("foo", methods[2].getName());
/** result #2 * */
ICPPMethod meth2 = (ICPPMethod) class2s[1];
assertEquals("Class2::Class2", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(meth2)));
assertEquals(meth2, methods[0]);
/** result #3 * */
ICPPClassType cls3 = (ICPPClassType) class2s[2];
assertEquals("namespace1::Class1::Class2", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(cls3)));
/** result #3 * */
ICPPClassType cls4 = (ICPPClassType) class2s[3];
assertEquals("namespace1::Class2", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(cls4)));
/* Nested class references - namespace1::Class1::Class2 */
IName[] refs = pdom.findNames(cls3, IIndex.FIND_REFERENCES);
assertEquals(0, refs.length);
/* Nested class declaration */
IName[] decls = pdom.findNames(cls3, IIndex.FIND_DECLARATIONS);
assertEquals(0, decls.length);
/* Nested class definition */
IName[] defs = pdom.findNames(cls3, IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length);
IASTFileLocation loc = defs[0].getFileLocation();
assertEquals(offset("Class1.h","class Class2 { //namespace1::Class1::Class2") + 6, loc.getNodeOffset()); //character offset
}
public void testFunction() throws Exception {
IBinding[] functions = pdom.findBindings(Pattern.compile("foo2"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, functions.length);
assertTrue(functions[0] instanceof ICPPFunction);
assertEquals("foo2", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(functions[0])));
functions = pdom.findBindings(Pattern.compile("main"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, functions.length);
assertTrue(functions[0] instanceof ICPPFunction);
assertEquals("main", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(functions[0])));
}
public void testMethods() throws Exception {
IBinding[] methods = pdom.findBindings(Pattern.compile("~Class2"), false, INDEX_FILTER,NULL_MONITOR);
assertEquals(1, methods.length);
assertTrue(methods[0] instanceof ICPPMethod);
assertEquals("Class2::~Class2", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(methods[0])));
}
public void testFields() throws Exception {
IBinding[] fields = pdom.findBindings(Pattern.compile("class1x"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, fields.length);
assertTrue(fields[0] instanceof ICPPField);
assertEquals("namespace1::Class1::class1x", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(fields[0])));
fields = pdom.findBindings(Pattern.compile("class1y"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, fields.length);
assertTrue(fields[0] instanceof ICPPField);
assertEquals("namespace1::Class1::class1y", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(fields[0])));
}
public void testVariables() throws Exception {
IBinding[] variables = pdom.findBindings(Pattern.compile("var"), false, INDEX_FILTER, NULL_MONITOR);
assertEquals(1, variables.length);
assertTrue(variables[0] instanceof ICPPVariable);
assertEquals("var", getBindingQualifiedName(pdom.getLinkages()[0].adaptBinding(variables[0])));
/* Variable references */
IName[] refs = pdom.findNames(variables[0], IIndex.FIND_REFERENCES);
assertEquals(1, refs.length);
IASTFileLocation loc = refs[0].getFileLocation();
assertEquals(offset("main.cpp","var = 0;"), loc.getNodeOffset()); //character offset
/* Variable declaration */
IName[] decls = pdom.findNames(variables[0], IIndex.FIND_DECLARATIONS_DEFINITIONS);
assertEquals(1, decls.length);
loc = decls[0].getFileLocation();
assertEquals(offset("main.cpp","int var;") + 4, loc.getNodeOffset()); //character offset
/* Variable definition */
IName[] defs = pdom.findNames(variables[0], IIndex.FIND_DEFINITIONS);
assertEquals(1, defs.length);
loc = defs[0].getFileLocation();
assertEquals(offset("main.cpp","int var;") + 4, loc.getNodeOffset()); //character offset
}
/**
* Get the fully qualified name for a given PDOMBinding
*
* @param pdomBinding
* @return binding's fully qualified name
* @throws CoreException
*/
private String getBindingQualifiedName(PDOMBinding pdomBinding) throws CoreException {
StringBuffer buf = new StringBuffer(pdomBinding.getName());
PDOMNode parent = pdomBinding.getParentNode();
while (parent != null) {
if (parent instanceof PDOMBinding) {
buf.insert(0, ((PDOMBinding) parent).getName() + "::");
}
parent = parent.getParentNode();
}
return buf.toString();
}
}

View file

@ -7,6 +7,7 @@
*
* Contributors:
* QNX - Initial API and implementation
* IBM Corporation
*******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;
@ -22,6 +23,7 @@ public class PDOMTests extends TestSuite {
public static Test suite() {
TestSuite suite = new PDOMTests();
suite.addTest(PDOMSearchTest.suite());
suite.addTestSuite(EnumerationTests.class);
suite.addTestSuite(ClassTests.class);
suite.addTestSuite(TypesTests.class);
@ -35,9 +37,11 @@ public class PDOMTests extends TestSuite {
suite.addTest(CPPFunctionTests.suite());
suite.addTest(CPPVariableTests.suite());
suite.addTest(MethodTests.suite());
suite.addTest(NamespaceTests.suite());
suite.addTest(CFunctionTests.suite());
suite.addTest(CVariableTests.suite());
suite.addTest(CCompositeTypeTests.suite());
suite.addTest(DefDeclTests.suite());

View file

@ -0,0 +1,129 @@
struct blah {
int lala;
};
struct blahblah{
struct blah first;
};
struct SimpleCStructure {
char scsa;
};
struct CStructure1 {
int cs1a;
double cs1b;
struct CStructure2 {
float cs2a;
struct CStructure3 {
char cs3a;
} cs2b;
} cs1c;
};
union CUnion1 {
int cu1a;
double cu1b;
char cu1c;
union CUnion2 {
int cu2a;
} cu1d;
};
struct CMixedS1 {
union CMixedU1 {
struct CMixedS2 {
double cms2a;
} cmu1a;
union CMixedU2 {
int cmu2a;
} cmu1b;
float cmu1c;
} cms1a;
struct CMixedS3 {
char cms3a;
} cms1b;
};
int main() {
struct SimpleCStructure css;
struct SimpleCStructure *cpss;
css.scsa = 0;
cpss->scsa = 1;
struct CStructure1 cs1;
struct CStructure1 *cps1 = &cs1;
cs1.cs1a = 0;
cps1->cs1a = 1;
cs1.cs1b = 2;
cs1.cs1b = 3;
cps1->cs1b = 4;
struct CStructure2 cs2;
struct CStructure2 *cps2 = &cs2;
cs1.cs1c = cs2;
cs1.cs1c = cs2;
cs1.cs1c = cs2;
cps1->cs1c = cs2;
struct CStructure3 cs3;
struct CStructure3 *cps3 = &cs3;
cs1.cs1c.cs2b = 9;
cs1.cs1c.cs2b = 10;
cs1.cs1c.cs2b = 11;
cps1->cs1c.cs2b = 12;
cps2->cs2b = 13;
cs1.cs1c.cs2b.cs3a = 13;
cs1.cs1c.cs2b.cs3a = 14;
cs1.cs1c.cs2b.cs3a = 15;
cs1.cs1c.cs2b.cs3a = 16;
cs1.cs1c.cs2b.cs3a = 17;
cps1->cs1c.cs2b.cs3a = 18;
cps2->cs2b.cs3a = 19;
cps3->cs3a = 19;
union CUnion1 cu1;
union CUnion1 *cpu1 = &cu1;
cu1.cu1a = 0;
cpu1->cu1a = 1;
union CUnion2 cu2;
union CUnion2 *cpu2 = &cu1.cu1d;
cu2.cu2a = 2;
cpu2->cu2a = 3;
union CMixedU1 cmu1;
union CMixedU1 *cpmu1 = &cmu1;
cmu1.cmu1a;
cpmu1->cmu1a;
union CMixedU2 cmu2;
union CMixedU2 *cpmu2 = &cmu2;
cmu2.cmu2a;
cpmu2->cmu2a;
struct CMixedS2 cms2;
struct CMixedS2 *cpms2 = &cms2;
cms2.cms2a;
cpms2->cms2a;
struct CMixedS3 cms3;
struct CMixedS3 *cpms3 = &cms3;
cms3.cms3a;
cpms3->cms3a;
return 0;
}

View file

@ -0,0 +1,21 @@
void forwardCDeclaration();
void normalCDeclaration1() {
forwardCDeclaration();
}
void normalCDeclaration2() {
normalCDeclaration1();
}
void forwardCDeclaration() {
normalCDeclaration2();
}
void spin() {
normalCDeclaration1();
normalCDeclaration2();
normalCDeclaration2();
forwardCDeclaration();
}

View file

@ -0,0 +1,9 @@
namespace ns1 {
int a;
int b;
}
namespace ns1 {
int c;
int d;
}

View file

@ -0,0 +1,15 @@
namespace ns4 {
class Class1 {
friend void function2(Class1);
};
Class1 element;
void function2(Class1){};
}
using ns4::element;
void Z()
{
ns4::function2(element);
ns4::Class1::function2(element); //error!
}

View file

@ -0,0 +1,15 @@
namespace namespace1 {
namespace namespace2 {
void foo();
namespace namespace3 {
}
}
}
namespace namespaceNew = namespace1::namespace2;
void namespace1::namespace2::foo() {
/* definition */
}

View file

@ -0,0 +1,15 @@
namespace ns2 {
void blah(int){};
}
namespace ns3 {
void blah(char){};
}
using namespace ns2;
using namespace ns3;
void foo2()
{
blah('a'); //ns3::blah(char)
}

View file

@ -0,0 +1,8 @@
namespace {
void function1() {};
}
int main()
{
function1();
}

View file

@ -0,0 +1,11 @@
#include "Class1.h"
namespace1::Class1::Class1()
{
class1x = 1;
class1y = 2;
}
namespace1::Class1::~Class1()
{
}

View file

@ -0,0 +1,29 @@
#ifndef CLASS1_H_
#define CLASS1_H_
class Class1 {
};
namespace namespace1 {
class Class1 {
public:
Class1();
~Class1();
int class1x;
int class1y;
class Class2 { //namespace1::Class1::Class2
};
};
namespace namespace2 {
class Class1 {
};
};
class Class2 {
};
};
#endif /*CLASS1_H_*/

View file

@ -0,0 +1,15 @@
#include <stdio.h>
#include "Class2.h"
Class2::Class2()
{
}
Class2::~Class2()
{
}
void Class2::foo()
{
printf("foo - Class2\n");
}

View file

@ -0,0 +1,12 @@
#ifndef CLASS2_H_
#define CLASS2_H_
class Class2
{
public:
Class2();
virtual ~Class2();
void foo();
};
#endif /*CLASS2_H_*/

View file

@ -0,0 +1,22 @@
#include <stdio.h>
#include "Class1.h"
#include "Class2.h"
int var;
//function
void foo2()
{
printf("foo2\n");
}
int main()
{
namespace1::Class1 element;
printf("%d\n", element.class1x);
foo2();
Class2 class2;
class2.foo();
var = 0;
return 0;
}