diff options
author | Nathan Ridge | 2013-01-22 09:19:40 +0000 |
---|---|---|
committer | Sergey Prigogin | 2013-01-23 17:41:05 +0000 |
commit | 619b57268b99064e418774b018b7b03d249e3554 (patch) | |
tree | 462d0b02c2c6dca57bf1d3d3790b708fc93d5ac6 | |
parent | be4742a73931d845e46966e5222515bccc65f3d4 (diff) | |
download | org.eclipse.cdt-619b57268b99064e418774b018b7b03d249e3554.tar.gz org.eclipse.cdt-619b57268b99064e418774b018b7b03d249e3554.tar.xz org.eclipse.cdt-619b57268b99064e418774b018b7b03d249e3554.zip |
Bug 398706 - 'T' in 'new T' gets resolved to CPPConstructor instead of
CPPClassType
Change-Id: I7fecc996736cfacb40ad4ee7af8619bd3b6d2a2a
Reviewed-on: https://git.eclipse.org/r/9824
Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
12 files changed, 234 insertions, 118 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java index dff0c031a63..374a5014c03 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2012 IBM Corporation and others. + * Copyright (c) 2004, 2013 IBM Corporation 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 @@ -329,9 +329,15 @@ public class AST2BaseTest extends BaseTestCase { } static protected class CPPNameCollector extends ASTVisitor { - { - shouldVisitNames = true; + public CPPNameCollector() { + this(false); // don't visit implicit names by default } + + public CPPNameCollector(boolean shouldVisitImplicitNames) { + this.shouldVisitNames = true; + this.shouldVisitImplicitNames = shouldVisitImplicitNames; + } + public List<IASTName> nameList = new ArrayList<IASTName>(); @Override diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPImplicitNameTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPImplicitNameTests.java index 002916af926..2095a9e4b25 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPImplicitNameTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPImplicitNameTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2010 IBM Corporation and others. + * Copyright (c) 2009, 2013 IBM Corporation 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 @@ -9,6 +9,7 @@ * Mike Kucera (IBM) * Sergey Prigogin (Google) * Markus Schorn (Wind River Systems) + * Nathan Ridge *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -445,11 +446,11 @@ public class AST2CPPImplicitNameTests extends AST2BaseTest { IBinding f= bh.assertNonProblem("operator new(size_t b)", 12); IASTImplicitName[] names = bh.getImplicitNames("new A;", 3); - assertEquals(1, names.length); + assertEquals(2, names.length); assertSame(m, names[0].resolveBinding()); names = bh.getImplicitNames("new B;", 3); - assertEquals(1, names.length); + assertEquals(2, names.length); assertSame(f, names[0].resolveBinding()); } 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 dd9b9059b6b..c9148e1a408 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2012 IBM Corporation and others. + * Copyright (c) 2004, 2013 IBM Corporation 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 @@ -1200,10 +1200,10 @@ public class AST2CPPTests extends AST2BaseTest { // } public void testVirtualParentLookup() throws Exception { IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.CPP); - CPPNameCollector collector = new CPPNameCollector(); + CPPNameCollector collector = new CPPNameCollector(true); tu.accept(collector); - assertEquals(collector.size(), 15); + assertEquals(collector.size(), 16); ICPPClassType D = (ICPPClassType) collector.getName(0).resolveBinding(); ICPPField x = (ICPPField) collector.getName(1).resolveBinding(); @@ -1214,7 +1214,7 @@ public class AST2CPPTests extends AST2BaseTest { assertInstances(collector, D, 3); assertInstances(collector, C, 2); assertInstances(collector, B, 2); - assertInstances(collector, A, 2); + assertInstances(collector, A, 3); assertInstances(collector, ctor, 1); assertInstances(collector, x, 2); } @@ -1229,10 +1229,10 @@ public class AST2CPPTests extends AST2BaseTest { // } public void testAmbiguousVirtualParentLookup() throws Exception { IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.CPP); - CPPNameCollector collector = new CPPNameCollector(); + CPPNameCollector collector = new CPPNameCollector(true); tu.accept(collector); - assertEquals(collector.size(), 15); + assertEquals(collector.size(), 16); ICPPClassType D = (ICPPClassType) collector.getName(0).resolveBinding(); ICPPField x1 = (ICPPField) collector.getName(1).resolveBinding(); @@ -1240,13 +1240,13 @@ public class AST2CPPTests extends AST2BaseTest { ICPPClassType B = (ICPPClassType) collector.getName(4).resolveBinding(); ICPPClassType A = (ICPPClassType) collector.getName(6).resolveBinding(); ICPPConstructor ctor = A.getConstructors()[0]; - IProblemBinding x2 = (IProblemBinding) collector.getName(14).resolveBinding(); + IProblemBinding x2 = (IProblemBinding) collector.getName(15).resolveBinding(); assertEquals(x2.getID(), IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP); assertInstances(collector, D, 3); assertInstances(collector, C, 2); assertInstances(collector, B, 2); - assertInstances(collector, A, 2); + assertInstances(collector, A, 3); assertInstances(collector, ctor, 1); assertInstances(collector, x1, 1); } @@ -1842,7 +1842,7 @@ public class AST2CPPTests extends AST2BaseTest { ICPPMethod dtor = (ICPPMethod) col.getName(13).resolveBinding(); assertNotNull(dtor); assertEquals(dtor.getName(), "~C"); //$NON-NLS-1$ - assertInstances(col, C, 6); + assertInstances(col, C, 7); assertInstances(col, op, 3); assertInstances(col, other, 4); @@ -1992,7 +1992,7 @@ public class AST2CPPTests extends AST2BaseTest { assertInstances(col, pb, 2); assertInstances(col, mutate, 2); - assertInstances(col, B, 2); + assertInstances(col, B, 3); } // struct S { int i; }; @@ -2510,7 +2510,7 @@ public class AST2CPPTests extends AST2BaseTest { // } public void testBug86267() throws Exception { IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.CPP); - CPPNameCollector col = new CPPNameCollector(); + CPPNameCollector col = new CPPNameCollector(true); tu.accept(col); ICPPClassType D1 = (ICPPClassType) col.getName(2).resolveBinding(); @@ -2548,7 +2548,7 @@ public class AST2CPPTests extends AST2BaseTest { ICPPMethod op = (ICPPMethod) col.getName(3).resolveBinding(); IParameter other = (IParameter) col.getName(5).resolveBinding(); - assertInstances(col, C, 6); + assertInstances(col, C, 7); assertInstances(col, f, 2); assertInstances(col, op, 3); assertInstances(col, other, 4); @@ -4051,11 +4051,11 @@ public class AST2CPPTests extends AST2BaseTest { // X x = new X(y); public void testBug90654_1() throws Exception { IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.CPP); - CPPNameCollector col = new CPPNameCollector(); + CPPNameCollector col = new CPPNameCollector(true); tu.accept(col); ICPPConstructor ctor1 = (ICPPConstructor) col.getName(1).resolveBinding(); - ICPPConstructor ctor = (ICPPConstructor) col.getName(11).resolveBinding(); + ICPPConstructor ctor = (ICPPConstructor) col.getName(12).resolveBinding(); assertSame(ctor, ctor1); } @@ -8381,20 +8381,24 @@ public class AST2CPPTests extends AST2BaseTest { // fH({1}); // H(G(1)) // } public void testListInitialization_302412f() throws Exception { - ICPPConstructor ctor; IProblemBinding problem; String code= getAboveComment(); BindingAssertionHelper bh= new BindingAssertionHelper(code, true); bh.assertProblem("f({1,1})", 1); - ctor= bh.assertNonProblem("F({1,1})", 1); + bh.assertImplicitName("F({1,1})", 1, ICPPConstructor.class); bh.assertNonProblem("fF({1,1})", 2); bh.assertNonProblem("fG(1)", 2); bh.assertNonProblem("fG({1})", 2); - ctor= bh.assertNonProblem("H(1)", 1); - problem= bh.assertProblem("H({1})", 1); - assertEquals(IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, problem.getID()); + bh.assertImplicitName("H(1)", 1, ICPPConstructor.class); + bh.assertNoImplicitName("H({1})", 1); + // TODO(nathanridge): Perhaps we should store implicit names even if they + // resolve to ProblemBindings. Then we can do the stronger check in the + // 3 commented lines below. + //IASTImplicitName n= bh.assertImplicitName("H({1})", 1, IProblemBinding.class); + //problem= (IProblemBinding) n.resolveBinding(); + //assertEquals(IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, problem.getID()); bh.assertProblem("fH(1)", 2); bh.assertNonProblem("fH({1})", 2); } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2SelectionParseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2SelectionParseTest.java index 9ec90bf1263..d729230bdc5 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2SelectionParseTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2SelectionParseTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2010 IBM Corporation and others. + * Copyright (c) 2005, 2013 IBM Corporation 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Nathan Ridge *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -26,6 +27,7 @@ import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTypeId; +import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.IEnumeration; import org.eclipse.cdt.core.dom.ast.IField; @@ -352,9 +354,10 @@ public class AST2SelectionParseTest extends AST2SelectionParseBaseTest { assertTrue(node instanceof IASTTypeId); assertEquals(((IASTNamedTypeSpecifier)((IASTTypeId)node).getDeclSpecifier()).getName().toString(), "Gonzo"); //$NON-NLS-1$ name = ((IASTNamedTypeSpecifier)((IASTTypeId)node).getDeclSpecifier()).getName(); - assertNotNull(name.resolveBinding()); - assertTrue(name.resolveBinding() instanceof ICPPConstructor); - assertEquals(((ICPPConstructor)name.resolveBinding()).getName(), "Gonzo"); //$NON-NLS-1$ + name = TestUtil.findImplicitName(name); + IBinding binding = name.resolveBinding(); + assertTrue(binding instanceof ICPPConstructor); + assertEquals(((ICPPConstructor)binding).getName(), "Gonzo"); //$NON-NLS-1$ break; default: assertTrue(node instanceof IASTName); @@ -736,9 +739,10 @@ public class AST2SelectionParseTest extends AST2SelectionParseBaseTest { assertTrue(node instanceof IASTTypeId); assertEquals(((IASTNamedTypeSpecifier)((IASTTypeId)node).getDeclSpecifier()).getName().toString(), "B"); //$NON-NLS-1$ IASTName name = ((IASTNamedTypeSpecifier)((IASTTypeId)node).getDeclSpecifier()).getName(); - assertNotNull(name.resolveBinding()); - assertTrue(name.resolveBinding() instanceof ICPPConstructor); - assertEquals(((ICPPConstructor)name.resolveBinding()).getName(), "B"); //$NON-NLS-1$ + name = TestUtil.findImplicitName(name); + IBinding binding = name.resolveBinding(); + assertTrue(binding instanceof ICPPConstructor); + assertEquals(((ICPPConstructor)binding).getName(), "B"); //$NON-NLS-1$ } public void testBug72712_2() throws Exception{ diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java index 42468a11cb8..cb6cd028342 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2012 IBM Corporation and others. + * Copyright (c) 2005, 2013 IBM Corporation 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 @@ -2373,11 +2373,11 @@ public class AST2TemplateTests extends AST2BaseTest { public void testCPPConstructorTemplateSpecialization() throws Exception { IASTTranslationUnit tu = parse(getAboveComment(), CPP, true, true); - CPPNameCollector col = new CPPNameCollector(); + CPPNameCollector col = new CPPNameCollector(true); tu.accept(col); - ICPPASTTemplateId tid= (ICPPASTTemplateId) col.getName(20); - IASTName cn= col.getName(21); + IASTImplicitName tid= (IASTImplicitName) col.getName(20); + IASTName cn= col.getName(22); assertInstance(cn.resolveBinding(), ICPPClassTemplate.class); // *D*<int>(5, 6) assertInstance(cn.resolveBinding(), ICPPClassType.class); // *D*<int>(5, 6) assertInstance(tid.resolveBinding(), ICPPTemplateInstance.class); // *D<int>*(5, 6) @@ -3842,7 +3842,7 @@ public class AST2TemplateTests extends AST2BaseTest { // }; public void testNestedTemplates_259872_1() throws Exception { BindingAssertionHelper bh= new BindingAssertionHelper(getAboveComment(), CPP); - bh.assertNonProblem("A<B, int>", 9, ICPPConstructor.class); + bh.assertNonProblem("A<B, int>", 9, ICPPClassType.class); } // template <typename CL, typename T> @@ -3867,7 +3867,7 @@ public class AST2TemplateTests extends AST2BaseTest { // }; public void testNestedTemplates_259872_2() throws Exception { BindingAssertionHelper bh= new BindingAssertionHelper(getAboveComment(), CPP); - bh.assertNonProblem("A<B, int>", 9, ICPPConstructor.class); + bh.assertNonProblem("A<B, int>", 9, ICPPClassType.class); } // template <class T> diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java index d2c052b0c22..e8474a74621 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2013 IBM Corporation 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 @@ -8,6 +8,7 @@ * Contributors: * IBM - Initial API and implementation * Markus Schorn (Wind River Systems) + * Nathan Ridge *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -102,9 +103,13 @@ public class CompleteParser2Tests extends BaseTestCase { } static private class CPPNameCollector extends ASTVisitor { - { - shouldVisitNames = true; + public CPPNameCollector() { + this(false); // don't visit implicit names by default } + public CPPNameCollector(boolean shouldVisitImplicitNames) { + this.shouldVisitNames = true; + this.shouldVisitImplicitNames = shouldVisitImplicitNames; + } public List nameList = new ArrayList(); @Override public int visit(IASTName name){ @@ -141,7 +146,7 @@ public class CompleteParser2Tests extends BaseTestCase { if (nameCollector.getName(i).resolveBinding() == binding) count++; - assertEquals(count, num); + assertEquals(num, count); } protected void assertInstances(CNameCollector nameCollector, IBinding binding, int num) throws Exception { int count = 0; @@ -149,7 +154,7 @@ public class CompleteParser2Tests extends BaseTestCase { if (nameCollector.getName(i).resolveBinding() == binding) count++; - assertEquals(count, num); + assertEquals(num, count); } protected IASTTranslationUnit parse(String code, boolean expectedToPass, ParserLanguage lang) throws Exception { @@ -1112,14 +1117,14 @@ public class CompleteParser2Tests extends BaseTestCase { public void testBug43503A() throws Exception { IASTTranslationUnit tu = parse("class SD_01 { void f_SD_01() {}}; int main(){ SD_01 * a = new SD_01(); a->f_SD_01(); } "); - CPPNameCollector col = new CPPNameCollector(); + CPPNameCollector col = new CPPNameCollector(true); tu.accept(col); - assertEquals(col.size(), 8); + assertEquals(col.size(), 9); ICPPClassType SD_01 = (ICPPClassType) col.getName(0).resolveBinding(); ICPPMethod f_SD_01 = (ICPPMethod) col.getName(1).resolveBinding(); ICPPConstructor ctor = SD_01.getConstructors()[0]; - assertInstances(col, SD_01, 2); + assertInstances(col, SD_01, 3); assertInstances(col, ctor, 1); assertInstances(col, f_SD_01, 2); } @@ -1204,10 +1209,10 @@ public class CompleteParser2Tests extends BaseTestCase { buff.append("} \n"); IASTTranslationUnit tu = parse(buff.toString()); - CPPNameCollector col = new CPPNameCollector(); + CPPNameCollector col = new CPPNameCollector(true); tu.accept(col); - assertEquals(col.size(), 17); + assertEquals(col.size(), 18); ICompositeType SD_02 = (ICompositeType) col.getName(0).resolveBinding(); ICPPMethod f_SD_02 = (ICPPMethod) col.getName(1).resolveBinding(); ICPPClassType SD_01 = (ICPPClassType) col.getName(2).resolveBinding(); @@ -1217,7 +1222,7 @@ public class CompleteParser2Tests extends BaseTestCase { assertInstances(col, SD_02, 2); assertInstances(col, f_SD_02, 2); - assertInstances(col, SD_01, 3); + assertInstances(col, SD_01, 4); assertInstances(col, ctor, 1); assertInstances(col, next, 2); assertInstances(col, f_SD_01, 4); @@ -1268,10 +1273,10 @@ public class CompleteParser2Tests extends BaseTestCase { public void testBug44342() throws Exception { IASTTranslationUnit tu = parse("class A { void f(){} void f(int){} }; int main(){ A * a = new A(); a->f();} "); - CPPNameCollector col = new CPPNameCollector(); + CPPNameCollector col = new CPPNameCollector(true); tu.accept(col); - assertEquals(col.size(), 10); + assertEquals(col.size(), 11); ICPPClassType A = (ICPPClassType) col.getName(0).resolveBinding(); ICPPMethod f1 = (ICPPMethod) col.getName(1).resolveBinding(); ICPPMethod f2 = (ICPPMethod) col.getName(2).resolveBinding(); @@ -1279,7 +1284,7 @@ public class CompleteParser2Tests extends BaseTestCase { ICPPConstructor ctor = A.getConstructors()[0]; IVariable a = (IVariable) col.getName(6).resolveBinding(); - assertInstances(col, A, 2); + assertInstances(col, A, 3); assertInstances(col, f1, 2); assertInstances(col, f2, 1); assertInstances(col, ctor, 1); @@ -1420,23 +1425,23 @@ public class CompleteParser2Tests extends BaseTestCase { buffer.append("void main() { N::A * a = new N::A(); a->f(); } "); IASTTranslationUnit tu = parse(buffer.toString()); - CPPNameCollector col = new CPPNameCollector(); + CPPNameCollector col = new CPPNameCollector(true); tu.accept(col); - assertEquals(col.size(), 13); + assertEquals(col.size(), 14); ICPPNamespace N = (ICPPNamespace) col.getName(0).resolveBinding(); IFunction f = (IFunction) col.getName(1).resolveBinding(); ICPPClassType A = (ICPPClassType) col.getName(2).resolveBinding(); ICPPConstructor ctor = A.getConstructors()[0]; - IProblemBinding fp = (IProblemBinding) col.getName(12).resolveBinding(); + IProblemBinding fp = (IProblemBinding) col.getName(13).resolveBinding(); assertEquals(fp.getID(), IProblemBinding.SEMANTIC_NAME_NOT_FOUND); assertInstances(col, N, 3); assertInstances(col, f, 1); - assertInstances(col, A, 3); - assertInstances(col, ctor, 2); + assertInstances(col, A, 5); + assertInstances(col, ctor, 1); } public void testBug43110() throws Exception { diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseTest.java index 9d59cf1c8b2..ac21a5487f7 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2010 IBM Corporation and others. + * Copyright (c) 2002, 2013 IBM Corporation 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 @@ -8,6 +8,7 @@ * Contributors: * IBM Rational Software - Initial API and implementation * Markus Schorn (Wind River Systems) + * Nathan Ridge *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -37,6 +38,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase; +import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil; import org.eclipse.cdt.internal.core.model.CProject; import org.eclipse.core.resources.IFile; @@ -296,8 +298,12 @@ public class DOMSelectionParseTest extends DOMSelectionParseBaseTest { } IASTNode node = parse( code, startOffset, endOffset ); assertTrue( node instanceof IASTName ); - assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); IBinding binding = ((IASTName)node).resolveBinding(); + if (binding instanceof ICPPClassType) { + node = TestUtil.findImplicitName(node); + binding = ((IASTName)node).resolveBinding(); + } + assertTrue( binding instanceof ICPPMethod ); IName[] decls = null; switch( i ) { @@ -599,14 +605,15 @@ public class DOMSelectionParseTest extends DOMSelectionParseBaseTest { int startIndex = code.indexOf( "new B" ) + 4; //$NON-NLS-1$ IASTNode node = parse( code, startIndex, startIndex + 1 ); + node = TestUtil.findImplicitName(node); assertTrue( node instanceof IASTName ); - assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertTrue( ((IASTName) node).resolveBinding() instanceof ICPPConstructor ); assertEquals( ((IASTName)node).toString(), "B" ); //$NON-NLS-1$ IName[] decls = getDeclarationOffTU((IASTName)node); assertEquals(decls.length, 1); assertEquals( decls[0].toString(), "B" ); //$NON-NLS-1$ - assertEquals( ((ASTNode)decls[0]).getOffset(), 17); - assertEquals( ((ASTNode)decls[0]).getLength(), 1); + assertEquals( 17, ((ASTNode)decls[0]).getOffset() ); + assertEquals( 1, ((ASTNode)decls[0]).getLength() ); } public void testBug72712_2() throws Exception{ @@ -1111,8 +1118,9 @@ public class DOMSelectionParseTest extends DOMSelectionParseBaseTest { int index = code.indexOf("Point(10)"); //$NON-NLS-1$ IASTNode node = parse( code, index, index + 5, true ); + node = TestUtil.findImplicitName(node); assertTrue( node instanceof IASTName ); - assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertTrue( ((IASTName) node).resolveBinding() instanceof ICPPConstructor ); assertEquals( ((IASTName)node).toString(), "Point" ); //$NON-NLS-1$ IName[] decls = getDeclarationOffTU((IASTName)node); diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/TestUtil.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/TestUtil.java new file mode 100644 index 00000000000..89ddc566e2f --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/TestUtil.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2013 Nathan Ridge. + * 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: + * Nathan Ridge - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.ast2; + +import org.eclipse.cdt.core.dom.ast.IASTImplicitName; +import org.eclipse.cdt.core.dom.ast.IASTImplicitNameOwner; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; + +public class TestUtil { + /** + * Searches the AST upward from the given starting node to find the + * nearest IASTImplicitNameOwner and returns its first implicit name, + * or null if it has no implicit names. + */ + public static IASTName findImplicitName(IASTNode node) { + while (node != null) { + if (node instanceof IASTImplicitNameOwner) { + IASTImplicitName[] implicitNames = ((IASTImplicitNameOwner) node).getImplicitNames(); + if (implicitNames != null && implicitNames.length > 0) { + return implicitNames[0]; + } + } + node = node.getParent(); + } + return null; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java index 37b10fbad55..4208c2d3db1 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2012 Symbian Software Systems and others. + * Copyright (c) 2006, 2013 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 @@ -10,6 +10,7 @@ * IBM Corporation * Markus Schorn (Wind River Systems) * Sergey Prigogin (Google) + * Nathan Ridge *******************************************************************************/ package org.eclipse.cdt.internal.index.tests; @@ -83,7 +84,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { super.tearDown(); } - protected IASTName findName(String section, int len) { + protected IASTName findName(String section, int len, boolean preferImplicitName) { if (len == 0) len= section.length(); for (int i = 0; i < strategy.getAstCount(); i++) { @@ -91,15 +92,27 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { final IASTNodeSelector nodeSelector = ast.getNodeSelector(null); final int offset = strategy.getAstSource(i).indexOf(section); if (offset >= 0) { - IASTName name= nodeSelector.findName(offset, len); - if (name == null) - name= nodeSelector.findImplicitName(offset, len); - return name; + if (preferImplicitName) { + return nodeSelector.findImplicitName(offset, len); + } else { + IASTName name= nodeSelector.findName(offset, len); + if (name == null) + name= nodeSelector.findImplicitName(offset, len); + return name; + } } } return null; } + + protected IASTName findName(String section, int len) { + return findName(section, len, false); + } + + protected IASTName findImplicitName(String section, int len) { + return findName(section, len, true); + } /** * Attempts to get an IBinding from the initial specified number of characters @@ -129,9 +142,37 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { assertInstance(binding, clazz, cs); return clazz.cast(binding); } + + /** + * Attempts to get an IBinding attached to an implicit name from the initial specified + * number of characters from the specified code fragment. Fails the test if + * <ul> + * <li> There is not a unique implicit name with the specified criteria + * <li> The binding associated with the implicit name is null or a problem binding + * <li> The binding is not an instance of the specified class + * </ul> + * @param section the code fragment to search for in the AST. The first occurrence of an identical section is used. + * @param len the length of the specified section to use as a name + * @param clazz an expected class type or interface that the binding should extend/implement + * @return the associated implicit name's binding + */ + protected <T> T getBindingFromImplicitASTName(String section, int len, Class<T> clazz, Class ... cs) { + if (len < 1) { + len= section.length()+len; + } + IASTName name= findImplicitName(section, len); + assertNotNull("Name not found for \"" + section + "\"", name); + assertEquals(section.substring(0, len), name.getRawSignature()); + + IBinding binding = name.resolveBinding(); + assertNotNull("No binding for " + name.getRawSignature(), binding); + assertFalse("Binding is a ProblemBinding for name \"" + name.getRawSignature() + "\"", IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); + assertInstance(binding, clazz, cs); + return clazz.cast(binding); + } /* - * @see IndexBindingResolutionTestBase#getBindingFromASTName(Class, String, int) + * @see IndexBindingResolutionTestBase#getBindingFromASTName(String, int, Class<T>, Class ...) */ protected <T extends IBinding> T getBindingFromASTName(String section, int len) { if (len <= 0) @@ -146,6 +187,23 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { assertFalse("Binding is a ProblemBinding for name \"" + name.getRawSignature() + "\"", IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); return (T) binding; } + + /* + * @see IndexBindingResolutionTestBase#getBindingFromImplicitASTName(String, int, Class<T>, Class ...) + */ + protected <T extends IBinding> T getBindingFromImplicitASTName(String section, int len) { + if (len <= 0) + len += section.length(); + + IASTName name= findImplicitName(section, len); + assertNotNull("Name not found for \"" + section + "\"", name); + assertEquals(section.substring(0, len), name.getRawSignature()); + + IBinding binding = name.resolveBinding(); + assertNotNull("No binding for " + name.getRawSignature(), binding); + assertFalse("Binding is a ProblemBinding for name \"" + name.getRawSignature() + "\"", IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); + return (T) binding; + } /** * Attempts to verify that the resolved binding for a name is a problem binding. @@ -709,4 +767,4 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { fail("Artificially failing - see IndexBindingResolutionTestBase.fakeFailForReferenced()"); } } -}
\ No newline at end of file +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionTest.java index 2925bf8ffa3..082c99311a4 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Symbian Software Systems and others. + * Copyright (c) 2007, 2013 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 @@ -9,6 +9,7 @@ * Andrew Ferguson (Symbian) - Initial implementation * Markus Schorn (Wind River Systems) * Sergey Prigogin (Google) + * Nathan Ridge *******************************************************************************/ package org.eclipse.cdt.internal.index.tests; @@ -1490,9 +1491,7 @@ public abstract class IndexCPPBindingResolutionTest extends IndexBindingResoluti assertFalse(b0 instanceof IIndexBinding); ICPPConstructor b1 = getBindingFromASTName("B(int x)", 1, ICPPConstructor.class); assertFalse(b1 instanceof IIndexBinding); - ICPPConstructor b2 = getBindingFromASTName("B(0)", 1, ICPPConstructor.class); - assertFalse(b2 instanceof IIndexBinding); - assertEquals(b1, b2); + ICPPClassType b2 = getBindingFromASTName("B(0)", 1, ICPPClassType.class); ICPPMethod b3 = getBindingFromASTName("m(0)", 1, ICPPMethod.class); assertFalse(b3 instanceof IIndexBinding); } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPTemplateResolutionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPTemplateResolutionTest.java index 57ffaefda0b..2c1033ffc30 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPTemplateResolutionTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPTemplateResolutionTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Symbian Software Systems and others. + * Copyright (c) 2007, 2013 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 @@ -300,7 +300,7 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa // X<A> xa= new X<A>(); // } public void testUnindexedConstructorInstance() { - IBinding b0= getBindingFromASTName("X<A>()", 4); + IBinding b0= getBindingFromImplicitASTName("X<A>()", 4); assertInstance(b0, ICPPConstructor.class); } @@ -409,7 +409,7 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa // assertEquals(3, _ctcs.length); // two implicit plus the constructor template IBinding b2= getBindingFromASTName("D<int>(", 1); - IBinding b3= getBindingFromASTName("D<int>(", 6); + IBinding b3= getBindingFromImplicitASTName("D<int>(", 6); assertInstance(b2, ICPPClassTemplate.class); // *D*<int>(5, 6) assertInstance(b2, ICPPClassType.class); // *D*<int>(5, 6) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java index a447998e76e..70ca109fd0e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2012 IBM Corporation and others. + * Copyright (c) 2004, 2013 IBM Corporation 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 @@ -550,14 +550,6 @@ public class CPPSemantics { if (parent instanceof ICPPASTConstructorChainInitializer) { return true; } - if (parent instanceof ICPPASTNamedTypeSpecifier) { - parent= parent.getParent(); - if (parent instanceof IASTTypeId && parent.getParent() instanceof ICPPASTNewExpression) { - IASTDeclarator dtor = ((IASTTypeId) parent).getAbstractDeclarator(); - if (dtor != null && dtor.getPointerOperators().length == 0) - return true; - } - } return false; } @@ -3103,27 +3095,43 @@ public class CPPSemantics { return null; IType type = ((ICPPVariable) binding).getType(); - try { - type = SemanticUtil.getNestedType(type, TDEF | CVTYPE); - if (!(type instanceof ICPPClassType)) - return null; - if (type instanceof ICPPClassTemplate || type instanceof ICPPUnknownType || type instanceof ISemanticProblem) - return null; + type = SemanticUtil.getNestedType(type, TDEF | CVTYPE); + if (!(type instanceof ICPPClassType)) + return null; + if (type instanceof ICPPClassTemplate || type instanceof ICPPUnknownType || type instanceof ISemanticProblem) + return null; + + return findImplicitlyCalledConstructor((ICPPClassType) type, initializer, name); + } + + public static ICPPConstructor findImplicitlyCalledConstructor(ICPPASTNewExpression expr) { + IType type = getNestedType(expr.getExpressionType(), TDEF | REF | CVTYPE); + if (!(type instanceof IPointerType)) + return null; + type = ((IPointerType) type).getType(); + if (type instanceof ICPPClassType) { + return findImplicitlyCalledConstructor((ICPPClassType) type, + expr.getInitializer(), expr.getTypeId()); + } + return null; + } - final ICPPClassType classType = (ICPPClassType) type; + private static ICPPConstructor findImplicitlyCalledConstructor(ICPPClassType type, IASTInitializer initializer, + IASTNode typeId) { + try { if (initializer instanceof IASTEqualsInitializer) { // Copy initialization. IASTEqualsInitializer eqInit= (IASTEqualsInitializer) initializer; ICPPASTInitializerClause initClause = (ICPPASTInitializerClause) eqInit.getInitializerClause(); final ICPPEvaluation evaluation = initClause.getEvaluation(); - IType sourceType= evaluation.getTypeOrFunctionSet(name); - ValueCategory isLValue= evaluation.getValueCategory(name); + IType sourceType= evaluation.getTypeOrFunctionSet(typeId); + ValueCategory isLValue= evaluation.getValueCategory(typeId); if (sourceType != null) { Cost c; - if (calculateInheritanceDepth(sourceType, classType, name) >= 0) { - c = Conversions.copyInitializationOfClass(isLValue, sourceType, classType, false, name); + if (calculateInheritanceDepth(sourceType, type, typeId) >= 0) { + c = Conversions.copyInitializationOfClass(isLValue, sourceType, type, false, typeId); } else { - c = Conversions.checkImplicitConversionSequence(type, sourceType, isLValue, UDCMode.ALLOWED, Context.ORDINARY, name); + c = Conversions.checkImplicitConversionSequence(type, sourceType, isLValue, UDCMode.ALLOWED, Context.ORDINARY, typeId); } if (c.converts()) { ICPPFunction f = c.getUserDefinedConversion(); @@ -3136,7 +3144,7 @@ public class CPPSemantics { // List initialization. ICPPEvaluation eval= ((ICPPASTInitializerList) initializer).getEvaluation(); if (eval instanceof EvalInitList) { - Cost c= Conversions.listInitializationSequence((EvalInitList) eval, type, UDCMode.ALLOWED, true, name); + Cost c= Conversions.listInitializationSequence((EvalInitList) eval, type, UDCMode.ALLOWED, true, typeId); if (c.converts()) { ICPPFunction f = c.getUserDefinedConversion(); if (f instanceof ICPPConstructor) @@ -3145,35 +3153,22 @@ public class CPPSemantics { } } else if (initializer instanceof ICPPASTConstructorInitializer) { // Direct initialization. - return findImplicitlyCalledConstructor(classType, - (ICPPASTConstructorInitializer) initializer, name); + return findImplicitlyCalledConstructor(type, + (ICPPASTConstructorInitializer) initializer, typeId); } else if (initializer == null) { // Default initialization. - ICPPConstructor[] ctors = ClassTypeHelper.getConstructors(classType, name); + ICPPConstructor[] ctors = ClassTypeHelper.getConstructors(type, typeId); for (ICPPConstructor ctor : ctors) { if (ctor.getRequiredArgumentCount() == 0) return ctor; } return null; } - } catch (DOMException e) { + } catch (DOMException e) { } - return null; + return null; } - public static ICPPConstructor findImplicitlyCalledConstructor(ICPPASTNewExpression expr) { - IType type = getNestedType(expr.getExpressionType(), TDEF | REF | CVTYPE); - if (!(type instanceof IPointerType)) - return null; - type = ((IPointerType) type).getType(); - IASTInitializer initializer = expr.getInitializer(); - if (type instanceof ICPPClassType && initializer instanceof ICPPASTConstructorInitializer) { - return findImplicitlyCalledConstructor((ICPPClassType) type, - (ICPPASTConstructorInitializer) initializer, expr.getTypeId()); - } - return null; - } - private static ICPPConstructor findImplicitlyCalledConstructor(ICPPClassType classType, ICPPASTConstructorInitializer initializer, IASTNode typeId) { final IASTInitializerClause[] arguments = initializer.getArguments(); |