From aaa0f85bb3c166800484570a59b89fab99896b03 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Sun, 12 Aug 2012 17:25:57 -0700 Subject: [PATCH] Added failing IndexCPPTemplateResolutionTest._testSFINAE_a test. --- .../tests/IndexBindingResolutionTestBase.java | 157 +++++++++++------- .../tests/IndexCPPBindingResolutionBugs.java | 6 +- .../tests/IndexCPPTemplateResolutionTest.java | 81 +++++++++ 3 files changed, 180 insertions(+), 64 deletions(-) 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 64cb7063c1f..1ceb2d2ba5d 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, 2010 Symbian Software Systems and others. + * Copyright (c) 2006, 2012 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 @@ -20,6 +20,7 @@ import java.util.List; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.IPDOMManager; import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; +import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNodeSelector; @@ -59,30 +60,30 @@ import org.osgi.framework.Bundle; * is backed by the PDOM), it must be possible to resolve which binding a name * in the AST is referring to. If the binding is not defined in the AST fragment * then it is assumed to have come from a file which is already indexed. - * + * * This class is for testing the process by which bindings are looked up in * the PDOM purely from AST information (i.e. without a real binding from the DOM) */ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { private static final boolean DEBUG= false; protected ITestStrategy strategy; - + public void setStrategy(ITestStrategy strategy) { this.strategy = strategy; } - + @Override protected void setUp() throws Exception { super.setUp(); strategy.setUp(); } - + @Override protected void tearDown() throws Exception { strategy.tearDown(); super.tearDown(); } - + protected IASTName findName(String section, int len) { if (len == 0) len= section.length(); @@ -97,10 +98,10 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { return name; } } - + return null; } - + /** * Attempts to get an IBinding from the initial specified number of characters * from the specified code fragment. Fails the test if @@ -122,25 +123,25 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { IASTName name= findName(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) */ protected T getBindingFromASTName(String section, int len) { if (len <= 0) len += section.length(); - + IASTName name= findName(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())); @@ -157,17 +158,17 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { IASTName name= findName(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); assertTrue("Binding is not a ProblemBinding for name \"" + name.getRawSignature() + "\"", IProblemBinding.class.isAssignableFrom(name.resolveBinding().getClass())); return name.resolveBinding(); } - + protected static void assertQNEquals(String expectedQN, IBinding b) { assertInstance(b, IBinding.class); if (b instanceof ICPPBinding) { - assertEquals(expectedQN, ASTTypeUtil.getQualifiedName((ICPPBinding)b)); + assertEquals(expectedQN, ASTTypeUtil.getQualifiedName((ICPPBinding) b)); } else { assertEquals(expectedQN, b.getName()); } @@ -175,7 +176,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { protected IType getVariableType(IBinding binding) throws DOMException { assertTrue(binding instanceof IVariable); - return ((IVariable)binding).getType(); + return ((IVariable) binding).getType(); } protected IType getPtrType(IBinding binding) throws DOMException { @@ -195,8 +196,8 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { // assert function is IFunctionType IFunctionType ft = (IFunctionType) function; assertTrue(ICPPClassType.class.isInstance((ft.getParameterTypes()[index]))); - assertEquals(compositeTypeKey, ((ICPPClassType)ft.getParameterTypes()[index]).getKey()); - assertEquals(qn, ASTTypeUtil.getQualifiedName((ICPPClassType)ft.getParameterTypes()[index])); + assertEquals(compositeTypeKey, ((ICPPClassType) ft.getParameterTypes()[index]).getKey()); + assertEquals(qn, ASTTypeUtil.getQualifiedName((ICPPClassType) ft.getParameterTypes()[index])); } protected static T assertInstance(Object o, Class clazz, Class ... cs) { @@ -207,7 +208,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { } return clazz.cast(o); } - + protected String readTaggedComment(final String tag) throws IOException { return TestSourceReader.readTaggedComment(CTestPlugin.getDefault().getBundle(), "parser", getClass(), tag); } @@ -216,6 +217,63 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { return strategy.getIndex(); } + protected static void assertVariable(IBinding b, String qn, Class expType, String expTypeQN) { + assertInstance(b, IVariable.class); + IVariable variable = (IVariable) b; + assertQNEquals(qn, variable); + assertInstance(variable.getType(), expType); + if (expTypeQN != null) { + IType type= variable.getType(); + assertInstance(type, IBinding.class); + assertQNEquals(expTypeQN, (IBinding) type); + } + } + + protected static void assertTypeContainer(IType conType, String expQN, Class containerType, Class expContainedType, String expContainedTypeQN) { + assertInstance(conType, ITypeContainer.class); + assertInstance(conType, containerType); + IType containedType= ((ITypeContainer) conType).getType(); + assertInstance(containedType, expContainedType); + if (expContainedTypeQN != null) { + assertInstance(containedType, IBinding.class); + assertQNEquals(expContainedTypeQN, (IBinding) containedType); + } + } + + final protected void checkBindings() throws Exception { + for (int i = 0; i < strategy.getAstCount(); i++) { + IASTTranslationUnit ast = strategy.getAst(i); + CNameCollector col = new CNameCollector(); + ast.accept(col); + for (IASTName n : col.nameList) { + assertFalse("ProblemBinding for " + n.getRawSignature(), n.resolveBinding() instanceof IProblemBinding); + } + } + } + + static protected class CNameCollector extends ASTVisitor { + { + shouldVisitNames = true; + } + public List nameList = new ArrayList(); + + @Override + public int visit(IASTName name) { + nameList.add(name); + return PROCESS_CONTINUE; + } + + public IASTName getName(int idx) { + if (idx < 0 || idx >= nameList.size()) + return null; + return nameList.get(idx); + } + + public int size() { + return nameList.size(); + } + } + interface ITestStrategy { IIndex getIndex(); void setUp() throws Exception; @@ -227,30 +285,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { public ICProject getCProject(); public boolean isCompositeIndex(); } - - protected static void assertVariable(IBinding b, String qn, Class expType, String expTypeQN) { - assertInstance(b, IVariable.class); - IVariable variable = (IVariable) b; - assertQNEquals(qn, variable); - assertInstance(variable.getType(), expType); - if (expTypeQN != null) { - IType type= variable.getType(); - assertInstance(type, IBinding.class); - assertQNEquals(expTypeQN, (IBinding) type); - } - } - - protected static void assertTypeContainer(IType conType, String expQN, Class containerType, Class expContainedType, String expContainedTypeQN) { - assertInstance(conType, ITypeContainer.class); - assertInstance(conType, containerType); - IType containedType= ((ITypeContainer)conType).getType(); - assertInstance(containedType, expContainedType); - if (expContainedTypeQN != null) { - assertInstance(containedType, IBinding.class); - assertQNEquals(expContainedTypeQN, (IBinding) containedType); - } - } - + class SinglePDOMTestFirstASTStrategy implements ITestStrategy { private IIndex index; private ICProject cproject; @@ -266,7 +301,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { public ICProject getCProject() { return cproject; } - + @Override public StringBuilder[] getTestData() { return testData; @@ -293,7 +328,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { @Override public void setUp() throws Exception { - cproject = cpp ? CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) + cproject = cpp ? CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) : CProjectHelper.createCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); Bundle b = CTestPlugin.getDefault().getBundle(); testData = TestSourceReader.getContentsForTest(b, "parser", IndexBindingResolutionTestBase.this.getClass(), getName(), 2); @@ -330,7 +365,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { public IIndex getIndex() { return index; } - + @Override public boolean isCompositeIndex() { return false; @@ -379,7 +414,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { @Override public void setUp() throws Exception { - cproject = cpp ? CProjectHelper.createCCProject(getName()+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) + cproject = cpp ? CProjectHelper.createCCProject(getName()+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) : CProjectHelper.createCProject(getName()+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); Bundle b = CTestPlugin.getDefault().getBundle(); testData = TestSourceReader.getContentsForTest(b, "parser", IndexBindingResolutionTestBase.this.getClass(), getName(), 2); @@ -390,7 +425,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { IFile cppfile= TestSourceReader.createFile(cproject.getProject(), new Path("references.c" + (cpp ? "pp" : "")), testData[1].toString()); assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000, new NullProgressMonitor())); - + if (DEBUG) { System.out.println("Project PDOM: " + getName()); ((PDOM)CCoreInternals.getPDOMManager().getPDOM(cproject)).accept(new PDOMPrettyPrinter()); @@ -416,7 +451,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { public IIndex getIndex() { return index; } - + @Override public boolean isCompositeIndex() { return false; @@ -472,7 +507,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { @Override public void setUp() throws Exception { - cproject = cpp ? CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) + cproject = cpp ? CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) : CProjectHelper.createCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); Bundle b = CTestPlugin.getDefault().getBundle(); testData = TestSourceReader.getContentsForTest(b, "parser", IndexBindingResolutionTestBase.this.getClass(), getName(), 0); @@ -499,7 +534,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { } CCorePlugin.getIndexManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER); assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000, new NullProgressMonitor())); - + if (DEBUG) { System.out.println("Project PDOM: " + getName()); ((PDOM) CCoreInternals.getPDOMManager().getPDOM(cproject)).accept(new PDOMPrettyPrinter()); @@ -527,7 +562,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { public IIndex getIndex() { return index; } - + @Override public boolean isCompositeIndex() { return false; @@ -549,7 +584,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { public ICProject getCProject() { return cproject; } - + @Override public void tearDown() throws Exception { if (index != null) { @@ -582,7 +617,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { IndexerPreferences.set(cproject.getProject(), IndexerPreferences.KEY_INDEXER_ID, IPDOMManager.ID_FAST_INDEXER); CCorePlugin.getIndexManager().reindex(cproject); assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000, new NullProgressMonitor())); - + if (DEBUG) { System.out.println("Online: "+getName()); ((PDOM)CCoreInternals.getPDOMManager().getPDOM(cproject)).accept(new PDOMPrettyPrinter()); @@ -598,17 +633,17 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { : CProjectHelper.createCProject("ReferencedContent"+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); String content = testData[0].toString(); IFile file = TestSourceReader.createFile(referenced.getProject(), new Path("header.h"), content); - + IndexerPreferences.set(referenced.getProject(), IndexerPreferences.KEY_INDEXER_ID, IPDOMManager.ID_FAST_INDEXER); CCorePlugin.getIndexManager().reindex(referenced); - + assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000, new NullProgressMonitor())); - + if (DEBUG) { System.out.println("Referenced: "+getName()); ((PDOM)CCoreInternals.getPDOMManager().getPDOM(referenced)).accept(new PDOMPrettyPrinter()); } - + return referenced; } @@ -640,7 +675,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { public StringBuilder[] getTestData() { return testData; } - + @Override public boolean isCompositeIndex() { return true; @@ -650,7 +685,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { /** * When a test is failing only for the strategy where the test data is split over * multiple index fragments, we artificially fail the single fragment strategy also. - * This is not ideal, but as both strategies behavior are typically the same, is + * This is not ideal, but as both strategies behavior are typically the same, is * quite rare. */ protected void fakeFailForSingle() { @@ -662,7 +697,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase { /** * When a test is failing only for the strategy where the test data is not split over * multiple index fragments, we artificially fail the single fragment strategy also. - * This is not ideal, but as both strategies behavior are typically the same, is + * This is not ideal, but as both strategies behavior are typically the same, is * quite rare. */ protected void fakeFailForMultiProject() { diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java index 05fc01240ba..de0b1a2065f 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java @@ -6,8 +6,9 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Markus Schorn - initial API and implementation - * Andrew Ferguson (Symbian) + * Markus Schorn - initial API and implementation + * Andrew Ferguson (Symbian) + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.index.tests; @@ -58,7 +59,6 @@ import org.eclipse.cdt.core.parser.util.ObjectMap; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPTemplateTypeArgument; import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInstanceCache; -import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil; import org.eclipse.core.runtime.CoreException; /** 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 64423a8ab91..5aafb47c898 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 @@ -8,6 +8,7 @@ * Contributors: * Andrew Ferguson (Symbian) - Initial implementation * Markus Schorn (Wind River Systems) + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.index.tests; @@ -1954,4 +1955,84 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa ITypedef tdef= getBindingFromASTName("type t;", 4, ITypedef.class); assertEquals("int", ASTTypeUtil.getType(tdef, true)); } + + // template + // struct integral_constant { + // static constexpr T value = v; + // typedef T value_type; + // typedef integral_constant type; + // }; + // + // typedef integral_constant true_type; + // + // typedef integral_constant false_type; + // + // template + // class helper { + // typedef char one; + // typedef struct { char arr[2]; } two; + // template struct Wrap_type {}; + // template static one test(Wrap_type*); + // template static two test(...); + // public: static const bool value = sizeof(test(0)) == 1; + // }; + // + // template + // struct has_category : integral_constant::value> {}; + // + // template::value> + // struct traits {}; + // + // template + // struct traits { + // typedef typename Iterator::value_type value_type; + // }; + // + // struct tag {}; + + // struct C { + // typedef int value_type; + // typedef tag category; + // }; + // + // template::value_type> + // class A { + // }; + // + // typedef A type; + public void _testSFINAE_a() throws Exception { + checkBindings(); + } + + // template struct enable_if { typedef T type; }; + // template struct enable_if {}; + // + // template struct is_int { static const bool value = false; }; + // template <> struct is_int { static const bool value = true; }; + // + // template struct is_double { static const bool value = false; }; + // template <> struct is_double { static const bool value = true; }; + // + // template + // struct A { + // static int get() { return 0; } + // }; + // + // template + // struct A::value>::type> { + // static int get() { return 1; } + // }; + // + // template + // struct A::value>::type> { + // static int get() { return 2; } + // }; + + // void test() { + // A::get(); + // A::get(); + // } + public void testSFINAE_b() throws Exception { + checkBindings(); + } }