1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

tests for bug 86267 & 86269

parameters try to return name of primary declaration
This commit is contained in:
Andrew Niefer 2005-02-23 16:13:43 +00:00
parent 173f521b41
commit 48933be7c6
2 changed files with 84 additions and 4 deletions

View file

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

View file

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