1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 02:36:01 +02:00

Fix for 222398: [Model Builder] problem with inlined qualified member declaration

and fix handling of multiple namespace definitions
This commit is contained in:
Anton Leherbauer 2008-03-14 10:25:46 +00:00
parent 7bce911157
commit f66dfa5d9a
6 changed files with 140 additions and 26 deletions

View file

@ -58,6 +58,7 @@ public class AllCoreTests {
suite.addTest(PathSettingsContainerTests.suite()); suite.addTest(PathSettingsContainerTests.suite());
suite.addTest(ASTCacheTests.suite()); suite.addTest(ASTCacheTests.suite());
suite.addTest(AsmModelBuilderTest.suite()); suite.addTest(AsmModelBuilderTest.suite());
suite.addTest(CModelBuilderBugsTest.suite());
return suite; return suite;
} }

View file

@ -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());
}
}

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
/* /*
* Created on Jun 17, 2003 * 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 // actually, none of the two are function templates, but method templates
String[] myExpectedValues = { String[] myExpectedValues = {
// "nonVector<T>::first", // "nonVector<T>::first",
"Foo::fum", // "Foo::fum",
}; };
assertEquals(myExpectedValues.length, arrayElements.size()); assertEquals(myExpectedValues.length, arrayElements.size());
// This test is no correct there is no guaranty on the order // 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 // Check the template method
List arrayElements = tu.getChildrenOfType(ICElement.C_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 = { String[] myExpectedValues = {
"nonVector<T>::first", "nonVector<T>::first",
// "Foo::fum", "Foo::fum",
}; };
assertEquals(myExpectedValues.length, arrayElements.size()); assertEquals(myExpectedValues.length, arrayElements.size());
// This test is no correct there is no guaranty on the order // This test is no correct there is no guaranty on the order

View file

@ -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
}

View file

@ -91,9 +91,11 @@ import org.eclipse.core.runtime.OperationCanceledException;
public class CModelBuilder2 implements IContributedModelBuilder { public class CModelBuilder2 implements IContributedModelBuilder {
private final TranslationUnit fTranslationUnit; private final TranslationUnit fTranslationUnit;
private final IProgressMonitor fProgressMonitor;
private ASTAccessVisibility fCurrentVisibility; private ASTAccessVisibility fCurrentVisibility;
private Stack<ASTAccessVisibility> fVisibilityStack; private Stack<ASTAccessVisibility> fVisibilityStack;
private IProgressMonitor fProgressMonitor; private Set<Namespace> fAllNamespaces;
/** /**
* Create a model builder for the given translation unit. * 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 { private void buildModel(IASTTranslationUnit ast) throws CModelException, DOMException {
fVisibilityStack= new Stack<ASTAccessVisibility>(); fVisibilityStack= new Stack<ASTAccessVisibility>();
fAllNamespaces= new HashSet<Namespace>();
// includes // includes
final IASTPreprocessorIncludeStatement[] includeDirectives= ast.getIncludeDirectives(); final IASTPreprocessorIncludeStatement[] includeDirectives= ast.getIncludeDirectives();
@ -489,15 +492,20 @@ public class CModelBuilder2 implements IContributedModelBuilder {
// create element // create element
final String type= Keywords.NAMESPACE; final String type= Keywords.NAMESPACE;
final IASTName name= declaration.getName(); final IASTName name= declaration.getName();
String nsName= ASTStringUtil.getQualifiedName(name); final String nsName= ASTStringUtil.getQualifiedName(name);
final Namespace element= new Namespace (parent, nsName); 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 // add to parent
parent.addChild(element); parent.addChild(element);
// set positions // set positions
if (name != null && nsName.length() > 0) { if (name != null && nsName.length() > 0) {
setIdentifierPosition(element, name); setIdentifierPosition(element, name);
} else { } else {
final IASTFileLocation nsLocation= getMinFileLocation(declaration.getNodeLocations()); final IASTFileLocation nsLocation= declaration.getFileLocation();
if (nsLocation != null) { if (nsLocation != null) {
element.setIdPos(nsLocation.getNodeOffset(), type.length()); element.setIdPos(nsLocation.getNodeOffset(), type.length());
} }
@ -506,6 +514,8 @@ public class CModelBuilder2 implements IContributedModelBuilder {
element.setTypeName(type); element.setTypeName(type);
final Set<Namespace> savedNamespaces= fAllNamespaces;
fAllNamespaces= new HashSet<Namespace>();
IASTDeclaration[] nsDeclarations= declaration.getDeclarations(); IASTDeclaration[] nsDeclarations= declaration.getDeclarations();
for (int i= 0; i < nsDeclarations.length; i++) { for (int i= 0; i < nsDeclarations.length; i++) {
IASTDeclaration nsDeclaration= nsDeclarations[i]; IASTDeclaration nsDeclaration= nsDeclarations[i];
@ -513,6 +523,7 @@ public class CModelBuilder2 implements IContributedModelBuilder {
createDeclaration(element, nsDeclaration); createDeclaration(element, nsDeclaration);
} }
} }
fAllNamespaces= savedNamespaces;
} }
private StructureDeclaration createElaboratedTypeDeclaration(Parent parent, IASTElaboratedTypeSpecifier elaboratedTypeSpecifier, boolean isTemplate) throws CModelException{ private StructureDeclaration createElaboratedTypeDeclaration(Parent parent, IASTElaboratedTypeSpecifier elaboratedTypeSpecifier, boolean isTemplate) throws CModelException{
@ -853,16 +864,22 @@ public class CModelBuilder2 implements IContributedModelBuilder {
} }
if (!isMethod) { if (!isMethod) {
scope= CPPVisitor.getContainingScope(simpleName); scope= CPPVisitor.getContainingScope(simpleName);
isMethod= scope instanceof ICPPClassScope; isMethod= scope instanceof ICPPClassScope || simpleName.resolveBinding() instanceof ICPPMethod;
} }
} }
if (isMethod) { if (isMethod) {
// method // method
final MethodDeclaration methodElement; final MethodDeclaration methodElement;
if (isTemplate) { final String methodName;
methodElement= new MethodTemplate(parent, ASTStringUtil.getQualifiedName(name)); if (parent instanceof IStructure) {
methodName= ASTStringUtil.getSimpleName(name);
} else { } 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; element= methodElement;
// establish identity attributes before getElementInfo() // establish identity attributes before getElementInfo()
@ -909,10 +926,10 @@ public class CModelBuilder2 implements IContributedModelBuilder {
} else { } else {
if (isTemplate) { if (isTemplate) {
// template function // template function
element= new FunctionTemplate(parent, ASTStringUtil.getQualifiedName(name)); element= new FunctionTemplate(parent, ASTStringUtil.getSimpleName(name));
} else { } else {
// function // function
element= new Function(parent, ASTStringUtil.getQualifiedName(name)); element= new Function(parent, ASTStringUtil.getSimpleName(name));
} }
element.setParameterTypes(parameterTypes); element.setParameterTypes(parameterTypes);
element.setReturnType(returnType); element.setReturnType(returnType);

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * 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; 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{ public class Namespace extends SourceManipulation implements INamespace{
String typeName = ""; //$NON-NLS-1$ String typeName = ""; //$NON-NLS-1$
int fIndex;
public Namespace(ICElement parent, String name) { public Namespace(ICElement parent, String name) {
super(parent, name, ICElement.C_NAMESPACE); super(parent, name, ICElement.C_NAMESPACE);
} }
@ -37,14 +39,27 @@ public class Namespace extends SourceManipulation implements INamespace{
this.typeName = typeName; this.typeName = typeName;
} }
// /* (non-Javadoc) /**
// * @see java.lang.Object#equals(java.lang.Object) * Set the index of this namespace, in case the same namespace is referenced
// */ * multiple times.
// public boolean equals(Object other) { *
// return (super.equals(other) * @param index
// && (this.getStartPos() == ((Namespace)other).getStartPos()) */
// && (this.getLength() == ((Namespace)other).getLength()) 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;
}
} }