From f66dfa5d9af9c89d4022ccbe241d7913acc9ea12 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Fri, 14 Mar 2008 10:25:46 +0000 Subject: [PATCH] Fix for 222398: [Model Builder] problem with inlined qualified member declaration and fix handling of multiple namespace definitions --- .../cdt/core/model/tests/AllCoreTests.java | 1 + .../model/tests/CModelBuilderBugsTest.java | 70 +++++++++++++++++++ .../cdt/core/model/tests/ITemplateTests.java | 9 ++- .../resources/cmodel/CModelBuilderTest.cpp | 12 ++++ .../internal/core/model/CModelBuilder2.java | 37 +++++++--- .../cdt/internal/core/model/Namespace.java | 37 +++++++--- 6 files changed, 140 insertions(+), 26 deletions(-) create mode 100644 core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelBuilderBugsTest.java create mode 100644 core/org.eclipse.cdt.core.tests/resources/cmodel/CModelBuilderTest.cpp diff --git a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/AllCoreTests.java b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/AllCoreTests.java index ea99f47ce41..a882db00d71 100644 --- a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/AllCoreTests.java +++ b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/AllCoreTests.java @@ -58,6 +58,7 @@ public class AllCoreTests { suite.addTest(PathSettingsContainerTests.suite()); suite.addTest(ASTCacheTests.suite()); suite.addTest(AsmModelBuilderTest.suite()); + suite.addTest(CModelBuilderBugsTest.suite()); return suite; } diff --git a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelBuilderBugsTest.java b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelBuilderBugsTest.java new file mode 100644 index 00000000000..2180e1f9447 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelBuilderBugsTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2008 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Anton Leherbauer (Wind River Systems) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.model.tests; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.IPDOMManager; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.INamespace; +import org.eclipse.cdt.core.model.IStructure; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.testplugin.CProjectHelper; +import org.eclipse.cdt.core.testplugin.CTestPlugin; +import org.eclipse.cdt.core.testplugin.util.BaseTestCase; + +/** + * Tests for C model builder bugs. + */ +public class CModelBuilderBugsTest extends BaseTestCase { + + public static Test suite() { + return suite(CModelBuilderBugsTest.class, "_"); + } + + private ICProject fCProject; + private ITranslationUnit fTU; + + public CModelBuilderBugsTest(String name) { + super(name); + } + + protected void setUp() throws Exception { + super.setUp(); + fCProject= CProjectHelper.createCProject(getName(), null, IPDOMManager.ID_FAST_INDEXER); + assertNotNull(fCProject); + CProjectHelper.importSourcesFromPlugin(fCProject, CTestPlugin.getDefault().getBundle(), "/resources/cmodel"); + fTU= (ITranslationUnit) CProjectHelper.findElement(fCProject, "CModelBuilderTest.cpp"); + assertNotNull(fTU); + } + + protected void tearDown() throws Exception { + CProjectHelper.delete(fCProject); + super.tearDown(); + } + + public void testModelBuilderBug222398() throws Exception { + IStructure clazz= (IStructure) fTU.getElement("Test"); + assertNotNull(clazz); + ICElement[] methods= clazz.getChildren(); + assertEquals(2, methods.length); + assertEquals("inlined", methods[0].getElementName()); + assertEquals("decl", methods[1].getElementName()); + + INamespace ns= (INamespace) fTU.getElement("nsTest"); + ICElement[] functions= ns.getChildren(); + assertEquals(2, functions.length); + assertEquals("inlined", functions[0].getElementName()); + assertEquals("decl", functions[1].getElementName()); + } + +} diff --git a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/ITemplateTests.java b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/ITemplateTests.java index be8a75385b0..1685b8b4f20 100644 --- a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/ITemplateTests.java +++ b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/ITemplateTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2006 IBM Corporation and others. + * Copyright (c) 2005, 2008 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 + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ /* * Created on Jun 17, 2003 @@ -153,7 +154,7 @@ public class ITemplateTests extends IntegratedCModelTest { // actually, none of the two are function templates, but method templates String[] myExpectedValues = { // "nonVector::first", - "Foo::fum", +// "Foo::fum", }; assertEquals(myExpectedValues.length, arrayElements.size()); // This test is no correct there is no guaranty on the order @@ -169,11 +170,9 @@ public class ITemplateTests extends IntegratedCModelTest { { // Check the template method List arrayElements = tu.getChildrenOfType(ICElement.C_TEMPLATE_METHOD); - // actually, both of the two are method templates, but Foo is not resolved - // to a cpp class String[] myExpectedValues = { "nonVector::first", -// "Foo::fum", + "Foo::fum", }; assertEquals(myExpectedValues.length, arrayElements.size()); // This test is no correct there is no guaranty on the order diff --git a/core/org.eclipse.cdt.core.tests/resources/cmodel/CModelBuilderTest.cpp b/core/org.eclipse.cdt.core.tests/resources/cmodel/CModelBuilderTest.cpp new file mode 100644 index 00000000000..35a7f1f8086 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/resources/cmodel/CModelBuilderTest.cpp @@ -0,0 +1,12 @@ +class Test { + void Test::inlined() {}; // wrong label in outline: Test::inlined(): void + void Test::decl(); // label in outline (ok): decl(): void +}; +namespace nsTest { + void nsTest::inlined() {}; // wrong label in outline: nsTest::inlined(): void + void nsTest::decl(); // label in outline (ok): decl(): void +} +namespace nsTest { + void nsTest::inlined2() {}; // wrong label in outline: nsTest::inlined(): void + void nsTest::decl2(); // label in outline (ok): decl(): void +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder2.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder2.java index effc6869ffe..037eba13b40 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder2.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder2.java @@ -91,9 +91,11 @@ import org.eclipse.core.runtime.OperationCanceledException; public class CModelBuilder2 implements IContributedModelBuilder { private final TranslationUnit fTranslationUnit; + private final IProgressMonitor fProgressMonitor; + private ASTAccessVisibility fCurrentVisibility; private Stack fVisibilityStack; - private IProgressMonitor fProgressMonitor; + private Set fAllNamespaces; /** * Create a model builder for the given translation unit. @@ -182,6 +184,7 @@ public class CModelBuilder2 implements IContributedModelBuilder { */ private void buildModel(IASTTranslationUnit ast) throws CModelException, DOMException { fVisibilityStack= new Stack(); + fAllNamespaces= new HashSet(); // includes final IASTPreprocessorIncludeStatement[] includeDirectives= ast.getIncludeDirectives(); @@ -489,15 +492,20 @@ public class CModelBuilder2 implements IContributedModelBuilder { // create element final String type= Keywords.NAMESPACE; final IASTName name= declaration.getName(); - String nsName= ASTStringUtil.getQualifiedName(name); - final Namespace element= new Namespace (parent, nsName); + final String nsName= ASTStringUtil.getQualifiedName(name); + final Namespace element= new Namespace(parent, nsName); + // if there is a duplicate namespace, also set the index + if (!fAllNamespaces.add(element)) { + element.setIndex(fAllNamespaces.size()); + fAllNamespaces.add(element); + } // add to parent parent.addChild(element); // set positions if (name != null && nsName.length() > 0) { setIdentifierPosition(element, name); } else { - final IASTFileLocation nsLocation= getMinFileLocation(declaration.getNodeLocations()); + final IASTFileLocation nsLocation= declaration.getFileLocation(); if (nsLocation != null) { element.setIdPos(nsLocation.getNodeOffset(), type.length()); } @@ -506,6 +514,8 @@ public class CModelBuilder2 implements IContributedModelBuilder { element.setTypeName(type); + final Set savedNamespaces= fAllNamespaces; + fAllNamespaces= new HashSet(); IASTDeclaration[] nsDeclarations= declaration.getDeclarations(); for (int i= 0; i < nsDeclarations.length; i++) { IASTDeclaration nsDeclaration= nsDeclarations[i]; @@ -513,6 +523,7 @@ public class CModelBuilder2 implements IContributedModelBuilder { createDeclaration(element, nsDeclaration); } } + fAllNamespaces= savedNamespaces; } private StructureDeclaration createElaboratedTypeDeclaration(Parent parent, IASTElaboratedTypeSpecifier elaboratedTypeSpecifier, boolean isTemplate) throws CModelException{ @@ -853,16 +864,22 @@ public class CModelBuilder2 implements IContributedModelBuilder { } if (!isMethod) { scope= CPPVisitor.getContainingScope(simpleName); - isMethod= scope instanceof ICPPClassScope; + isMethod= scope instanceof ICPPClassScope || simpleName.resolveBinding() instanceof ICPPMethod; } } if (isMethod) { // method final MethodDeclaration methodElement; - if (isTemplate) { - methodElement= new MethodTemplate(parent, ASTStringUtil.getQualifiedName(name)); + final String methodName; + if (parent instanceof IStructure) { + methodName= ASTStringUtil.getSimpleName(name); } else { - methodElement= new Method(parent, ASTStringUtil.getQualifiedName(name)); + methodName= ASTStringUtil.getQualifiedName(name); + } + if (isTemplate) { + methodElement= new MethodTemplate(parent, methodName); + } else { + methodElement= new Method(parent, methodName); } element= methodElement; // establish identity attributes before getElementInfo() @@ -909,10 +926,10 @@ public class CModelBuilder2 implements IContributedModelBuilder { } else { if (isTemplate) { // template function - element= new FunctionTemplate(parent, ASTStringUtil.getQualifiedName(name)); + element= new FunctionTemplate(parent, ASTStringUtil.getSimpleName(name)); } else { // function - element= new Function(parent, ASTStringUtil.getQualifiedName(name)); + element= new Function(parent, ASTStringUtil.getSimpleName(name)); } element.setParameterTypes(parameterTypes); element.setReturnType(returnType); diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Namespace.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Namespace.java index ca5045760e7..11f2586374e 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Namespace.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Namespace.java @@ -1,12 +1,13 @@ /******************************************************************************* - * Copyright (c) 2002, 2005 IBM Corporation and others. + * Copyright (c) 2002, 2008 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 * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Rational Software - Initial API and implementation + * Rational Software - Initial API and implementation + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.model; @@ -17,6 +18,7 @@ import org.eclipse.cdt.core.model.INamespace; public class Namespace extends SourceManipulation implements INamespace{ String typeName = ""; //$NON-NLS-1$ + int fIndex; public Namespace(ICElement parent, String name) { super(parent, name, ICElement.C_NAMESPACE); } @@ -37,14 +39,27 @@ public class Namespace extends SourceManipulation implements INamespace{ this.typeName = typeName; } -// /* (non-Javadoc) -// * @see java.lang.Object#equals(java.lang.Object) -// */ -// public boolean equals(Object other) { -// return (super.equals(other) -// && (this.getStartPos() == ((Namespace)other).getStartPos()) -// && (this.getLength() == ((Namespace)other).getLength()) -// ); -// } + /** + * Set the index of this namespace, in case the same namespace is referenced + * multiple times. + * + * @param index + */ + public void setIndex(int index) { + fIndex= index; + } + + /* + * @see org.eclipse.cdt.internal.core.model.CElement#equals(java.lang.Object) + */ + public boolean equals(Object other) { + if (other instanceof INamespace && equals(this, (INamespace) other)) { + if (other instanceof Namespace) { + return fIndex == ((Namespace)other).fIndex; + } + return true; + } + return false; + } }