From 21c0055276f3a66648a6771300e4dd9721885814 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Mon, 7 May 2007 11:27:55 +0000 Subject: [PATCH] Fix for 185408, API-compliance of IScope.find(String) --- .../core/parser/tests/ast2/AST2CPPTests.java | 49 +++++++++++++++ .../cdt/internal/pdom/tests/ClassTests.java | 59 +++++++++++++++++-- .../resources/pdomtests/classTests/class.cpp | 4 ++ .../core/dom/parser/cpp/CPPClassScope.java | 4 +- 4 files changed, 110 insertions(+), 6 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index ff9cf738bd4..9ae1e0512cc 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -3394,6 +3394,55 @@ public class AST2CPPTests extends AST2BaseTest { assertSame(bs[1], f1); assertSame(bs[2], f2); } + + // class A { + // public: + // A(); + // void f(); + // }; + // class B : public A { + // public: + // B(); + // void bf(); + // }; + public void testFind_bug185408() throws Exception { + StringBuffer buffer = getContents(1)[0]; + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + CPPNameCollector col = new CPPNameCollector(); + tu.accept(col); + + IFunction f1 = (IFunction) col.getName(6).resolveBinding(); + IScope classScope= f1.getScope(); + assertTrue(classScope instanceof ICPPClassScope); + IBinding[] bindings = classScope.find("bf"); + ICPPMethod method= extractSingleMethod(bindings); + assertEquals(method.getQualifiedName()[0], "B"); + + bindings= classScope.find("f"); + method= extractSingleMethod(bindings); + assertEquals(method.getQualifiedName()[0], "A"); + + bindings= classScope.find("B"); + ICPPClassType classType= extractSingleClass(bindings); + assertEquals(classType.getQualifiedName()[0], "B"); + + bindings= classScope.find("A"); + classType= extractSingleClass(bindings); + assertEquals(classType.getQualifiedName()[0], "A"); + } + + private ICPPMethod extractSingleMethod(IBinding[] bindings) { + assertEquals(1, bindings.length); + assertTrue(bindings[0] instanceof ICPPMethod); + return (ICPPMethod) bindings[0]; + } + + private ICPPClassType extractSingleClass(IBinding[] bindings) { + assertEquals(1, bindings.length); + assertTrue(bindings[0] instanceof ICPPClassType); + return (ICPPClassType) bindings[0]; + } public void testGets() throws Exception { StringBuffer buffer = new StringBuffer(); diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java index 97fc553e3a8..5082a612ea8 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java @@ -20,6 +20,8 @@ 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.IField; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope; 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; @@ -57,19 +59,32 @@ public class ClassTests extends PDOMTestBase { } public void test1() throws Exception { - IBinding[] Bs = pdom.findBindings(Pattern.compile("B"), false, IndexFilter.ALL, NPM); + IBinding[] Bs = pdom.findBindings(Pattern.compile("B"), true, IndexFilter.ALL, NPM); assertEquals(1, Bs.length); ICPPClassType B = (ICPPClassType)Bs[0]; ICPPMethod[] Bmethods = B.getAllDeclaredMethods(); - assertEquals(1, Bmethods.length); - ICPPMethod Bf = Bmethods[0]; - assertEquals("f", Bf.getName()); + assertEquals(4, Bmethods.length); + assertNotNull(findMethod(Bmethods, "B")); + assertNotNull(findMethod(Bmethods, "A")); + assertNotNull(findMethod(Bmethods, "bf")); + ICPPMethod Bf = findMethod(Bmethods, "f"); + assertNotNull(Bf); IName [] Bf_refs = pdom.findNames(Bf, IIndex.FIND_REFERENCES); assertEquals(1, Bf_refs.length); IASTFileLocation loc = Bf_refs[0].getFileLocation(); assertEquals(offset("class.cpp", "b.f()") + 2, loc.getNodeOffset()); } + private ICPPMethod findMethod(ICPPMethod[] bmethods, String name) { + for (int i = 0; i < bmethods.length; i++) { + ICPPMethod method = bmethods[i]; + if (method.getName().equals(name)) { + return method; + } + } + return null; + } + public void testNested() throws Exception { IBinding[] bindings = pdom.findBindings(Pattern.compile("NestedA"), false, IndexFilter.ALL, NPM); assertEquals(1, bindings.length); @@ -181,4 +196,40 @@ public class ClassTests extends PDOMTestBase { // expecting just D(D &) assertEquals(1, bindings.length); } + + public void testClassScope_bug185408() throws Exception { + char[][] name= {"B".toCharArray(), "bf".toCharArray()}; + IBinding[] bindings= pdom.findBindings(name, IndexFilter.ALL, NPM); + assertEquals(1, bindings.length); + IScope classScope= bindings[0].getScope(); + + assertTrue(classScope instanceof ICPPClassScope); + bindings= classScope.find("bf"); + ICPPMethod method= extractSingleMethod(bindings); + assertEquals(method.getQualifiedName()[0], "B"); + + bindings= classScope.find("f"); + method= extractSingleMethod(bindings); + assertEquals(method.getQualifiedName()[0], "A"); + + bindings= classScope.find("B"); + ICPPClassType classType= extractSingleClass(bindings); + assertEquals(classType.getQualifiedName()[0], "B"); + + bindings= classScope.find("A"); + classType= extractSingleClass(bindings); + assertEquals(classType.getQualifiedName()[0], "A"); + } + + private ICPPMethod extractSingleMethod(IBinding[] bindings) { + assertEquals(1, bindings.length); + assertTrue(bindings[0] instanceof ICPPMethod); + return (ICPPMethod) bindings[0]; + } + + private ICPPClassType extractSingleClass(IBinding[] bindings) { + assertEquals(1, bindings.length); + assertTrue(bindings[0] instanceof ICPPClassType); + return (ICPPClassType) bindings[0]; + } } diff --git a/core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp b/core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp index 3d2a9ac180f..401a5a257d1 100644 --- a/core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp +++ b/core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp @@ -1,10 +1,14 @@ class A { public: + A() {} void f() { } }; class B : public A { +public: + B() {} + void bf() {} }; int main() { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassScope.java index 38dae2ade0e..af9538f29d7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassScope.java @@ -310,7 +310,7 @@ public class CPPClassScope extends CPPScope implements ICPPClassScope { } if(CharArrayUtils.equals(compName.toCharArray(), n)) { - return getConstructors( bindings, true ); + return new IBinding[] {getClassType()}; } return super.find(name); @@ -401,7 +401,7 @@ class ImplicitsAnalysis { outer: for(int i=0; i= 1 ){ if(paramHasTypeReferenceToTheAssociatedClassType(ps[0])) { // and all remaining arguments have initializers