From 48933be7c68dc53f7df20ee98bbbb534a15a6e6b Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Wed, 23 Feb 2005 16:13:43 +0000 Subject: [PATCH] tests for bug 86267 & 86269 parameters try to return name of primary declaration --- .../core/parser/tests/ast2/AST2CPPTests.java | 61 +++++++++++++++++++ .../core/dom/parser/cpp/CPPParameter.java | 27 ++++++-- 2 files changed, 84 insertions(+), 4 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 571ebb990c9..3c4000fb03f 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 @@ -1953,5 +1953,66 @@ public class AST2CPPTests extends AST2BaseTest { // IASTIfStatement if_stmt = (IASTIfStatement) body.getStatements()[0]; // assertNotNull( if_stmt.getCondition() ); // } + + public void testBug86267() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("struct B { void mutate(); }; \n"); //$NON-NLS-1$ + buffer.append("struct D1 : B {}; \n"); //$NON-NLS-1$ + buffer.append("struct D2 : B {}; \n"); //$NON-NLS-1$ + buffer.append("void B::mutate() { \n"); //$NON-NLS-1$ + buffer.append(" new (this) D2; \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + buffer.append("void g() { \n"); //$NON-NLS-1$ + buffer.append(" B* pb = new (p) D1; \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + CPPNameCollector col = new CPPNameCollector(); + tu.getVisitor().visitTranslationUnit( col); + + ICPPClassType D1 = (ICPPClassType) col.getName(2).resolveBinding(); + ICPPClassType D2 = (ICPPClassType) col.getName(4).resolveBinding(); + + ICPPConstructor [] ctors = D1.getConstructors(); + ICPPConstructor d1_ctor = ctors[0]; + + ctors = D2.getConstructors(); + ICPPConstructor d2_ctor = ctors[0]; + + assertInstances( col, d1_ctor, 1 ); + assertInstances( col, d2_ctor, 1 ); + } + + public void testBug86269() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("struct C { \n"); //$NON-NLS-1$ + buffer.append(" void f(); \n"); //$NON-NLS-1$ + buffer.append(" const C& operator =( const C& ); \n"); //$NON-NLS-1$ + buffer.append("}; \n"); //$NON-NLS-1$ + buffer.append("const C& C::operator = ( const C& other ) { \n"); //$NON-NLS-1$ + buffer.append(" if( this != &other ) { \n"); //$NON-NLS-1$ + buffer.append(" this->~C(); \n"); //$NON-NLS-1$ + buffer.append(" new (this) C(other); \n"); //$NON-NLS-1$ + buffer.append(" f(); \n"); //$NON-NLS-1$ + buffer.append(" } \n"); //$NON-NLS-1$ + buffer.append(" return *this; \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + CPPNameCollector col = new CPPNameCollector(); + tu.getVisitor().visitTranslationUnit( col); + + ICPPClassType C = (ICPPClassType) col.getName(0).resolveBinding(); + ICPPMethod f = (ICPPMethod) col.getName(1).resolveBinding(); + ICPPMethod op = (ICPPMethod) col.getName(3).resolveBinding(); + IParameter other = (IParameter) col.getName(5).resolveBinding(); + + assertInstances( col, C, 6 ); + assertInstances( col, f, 2 ); + assertInstances( col, op, 3 ); + assertInstances( col, other, 4 ); + + assertEquals( other.getName(), "other" ); //$NON-NLS-1$ + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameter.java index 6b3d8c747b6..861c4298092 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameter.java @@ -13,7 +13,9 @@ */ package org.eclipse.cdt.internal.core.dom.parser.cpp; +import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IParameter; @@ -66,12 +68,28 @@ public class CPPParameter implements IParameter, ICPPBinding { tmp[ declarations.length ] = name; declarations = tmp; } + + private IASTName getPrimaryDeclaration(){ + if( declarations != null ){ + for( int i = 0; i < declarations.length && declarations[i] != null; i++ ){ + IASTNode node = declarations[i].getParent(); + while( !(node instanceof IASTDeclaration) ) + node = node.getParent(); + + if( node instanceof IASTFunctionDefinition ) + return declarations[i]; + } + return declarations[0]; + } + return null; + } /* (non-Javadoc) * @see org.eclipse.cdt.core.dom.ast.IBinding#getName() */ public String getName() { - if( declarations != null ) - return declarations[0].toString(); + IASTName name = getPrimaryDeclaration(); + if( name != null ) + return name.toString(); return CPPSemantics.EMPTY_NAME; } @@ -79,8 +97,9 @@ public class CPPParameter implements IParameter, ICPPBinding { * @see org.eclipse.cdt.core.dom.ast.IBinding#getNameCharArray() */ public char[] getNameCharArray() { - if( declarations != null ) - return declarations[0].toCharArray(); + IASTName name = getPrimaryDeclaration(); + if( name != null ) + return name.toCharArray(); return CPPSemantics.EMPTY_NAME_ARRAY; }