mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-25 18:05:33 +02:00
fix bug 72712: [Parser] Selection Search fails upon new expression
This commit is contained in:
parent
42ebad496a
commit
28c842f6a3
3 changed files with 99 additions and 37 deletions
|
@ -452,5 +452,31 @@ public class SelectionParseTest extends SelectionParseBaseTest {
|
|||
assertFalse( ((IASTMethod)node).previouslyDeclared() );
|
||||
assertEquals( ((IASTMethod) node).getNameOffset(), code.indexOf( " initialize();" ) + 1 ); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
public void testBug72712() throws Exception{
|
||||
Writer writer = new StringWriter();
|
||||
writer.write( "class B{ public: B(); }; void f(){ B* b; b = new B(); }" ); //$NON-NLS-1$
|
||||
|
||||
String code = writer.toString();
|
||||
int startIndex = code.indexOf( "new B" ) + 4; //$NON-NLS-1$
|
||||
|
||||
IASTNode node = parse( code, startIndex, startIndex + 1 );
|
||||
assertTrue( node instanceof IASTMethod );
|
||||
assertTrue( ((IASTMethod) node).isConstructor() );
|
||||
}
|
||||
|
||||
public void testBug72712_2() throws Exception{
|
||||
Writer writer = new StringWriter();
|
||||
writer.write( "class A {}; \n"); //$NON-NLS-1$
|
||||
writer.write( "class B{ public: B( A* ); }; \n"); //$NON-NLS-1$
|
||||
writer.write( "void f(){ B* b; b = new B( (A*)0 ); } \n"); //$NON-NLS-1$
|
||||
|
||||
String code = writer.toString();
|
||||
int startIndex = code.indexOf( "(A*)" ) + 1; //$NON-NLS-1$
|
||||
|
||||
IASTNode node = parse( code, startIndex, startIndex + 1 );
|
||||
assertTrue( node instanceof IASTClassSpecifier );
|
||||
assertEquals( ((IASTClassSpecifier)node).getName(), "A" ); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,4 +84,31 @@ public class ASTNewExpression extends ASTExpression {
|
|||
public String toString(){
|
||||
return ASTUtil.getExpressionString( this );
|
||||
}
|
||||
|
||||
public ASTExpression findOwnerExpressionForIDExpression(ITokenDuple duple) {
|
||||
ASTTypeId typeId = (ASTTypeId) getTypeId();
|
||||
ITokenDuple typeDuple = typeId.getTokenDuple();
|
||||
|
||||
if( typeDuple.equals( duple ) )
|
||||
return this;
|
||||
// check subduple
|
||||
if( typeDuple.contains( duple ) )
|
||||
return this;
|
||||
|
||||
//else, check the parameters
|
||||
ASTExpression ownerExpression = null;
|
||||
ASTNewDescriptor newDescriptor = (ASTNewDescriptor)getNewExpressionDescriptor();
|
||||
List newInitializerExpressions = newDescriptor.getNewInitializerExpressionsList();
|
||||
int size = newInitializerExpressions.size();
|
||||
for( int i = 0; i < size; i++ )
|
||||
{
|
||||
ASTExpression expressionList = (ASTExpression) newInitializerExpressions.get(i);
|
||||
ownerExpression = expressionList.findOwnerExpressionForIDExpression( duple );
|
||||
if( ownerExpression != null ){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ownerExpression;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3596,42 +3596,7 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
|
|||
else if( expression.getExpressionKind() == IASTExpression.Kind.NEW_NEWTYPEID ||
|
||||
expression.getExpressionKind() == IASTExpression.Kind.NEW_TYPEID )
|
||||
{
|
||||
IContainerSymbol classSymbol = null;
|
||||
try {
|
||||
classSymbol = (IContainerSymbol) lookupQualifiedName(scopeToSymbol( scope ), duple, null, false );
|
||||
} catch (ASTSemanticException e) {
|
||||
}
|
||||
if( classSymbol != null && classSymbol.getTypeInfo().checkBit( ITypeInfo.isTypedef ) ){
|
||||
ITypeInfo info = classSymbol.getTypeInfo().getFinalType( pst.getTypeInfoProvider() );
|
||||
classSymbol = (IContainerSymbol) info.getTypeSymbol();
|
||||
pst.getTypeInfoProvider().returnTypeInfo( info );
|
||||
}
|
||||
if( classSymbol == null || ! (classSymbol instanceof IDerivableContainerSymbol ) ){
|
||||
return null;
|
||||
}
|
||||
|
||||
List parameters = new ArrayList();
|
||||
ASTNewDescriptor newDescriptor = (ASTNewDescriptor) expression.getNewExpressionDescriptor();
|
||||
List newInitializerExpressions = newDescriptor.getNewInitializerExpressionsList();
|
||||
int size = newInitializerExpressions.size();
|
||||
for( int i = 0; i < size; i++ )
|
||||
{
|
||||
ASTExpression expressionList = (ASTExpression) newInitializerExpressions.get(i);
|
||||
while( expressionList != null ){
|
||||
parameters.add( expressionList.getResultType().getResult() );
|
||||
if( expressionList.getExpressionKind() == IASTExpression.Kind.EXPRESSIONLIST )
|
||||
expressionList = (ASTExpression) expressionList.getRHSExpression();
|
||||
else
|
||||
expressionList = null;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
s = ((IDerivableContainerSymbol)classSymbol).lookupConstructor( parameters );
|
||||
} catch (ParserSymbolTableException e1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
s = lookupSymbolInNewExpression( scope, duple, expression );
|
||||
}
|
||||
else if( expression.getExpressionKind() == Kind.POSTFIX_FUNCTIONCALL &&
|
||||
CharArrayUtils.equals( expression.getLHSExpression().getIdExpressionCharArray(), dupleAsCharArray ))
|
||||
|
@ -3658,11 +3623,16 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
|
|||
return null;
|
||||
}
|
||||
}
|
||||
else if( ownerExpression.getExpressionKind() == IASTExpression.Kind.NEW_NEWTYPEID ||
|
||||
ownerExpression.getExpressionKind() == IASTExpression.Kind.NEW_TYPEID )
|
||||
{
|
||||
s = lookupSymbolInNewExpression( scope, duple, ownerExpression );
|
||||
}
|
||||
else
|
||||
{
|
||||
try {
|
||||
s = lookupQualifiedName( scopeToSymbol( scope ), duple, null, false );
|
||||
} catch (ASTSemanticException e1) {
|
||||
} catch (ASTSemanticException e1) { //we'll return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3672,6 +3642,45 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
|
|||
return s.getASTExtension().getPrimaryDeclaration();
|
||||
}
|
||||
|
||||
public ISymbol lookupSymbolInNewExpression( IASTScope scope, ITokenDuple duple, ASTExpression expression ){
|
||||
IContainerSymbol classSymbol = null;
|
||||
try {
|
||||
classSymbol = (IContainerSymbol) lookupQualifiedName(scopeToSymbol( scope ), duple, null, false );
|
||||
} catch (ASTSemanticException e) {
|
||||
return null;
|
||||
}
|
||||
if( classSymbol != null && classSymbol.getTypeInfo().checkBit( ITypeInfo.isTypedef ) ){
|
||||
ITypeInfo info = classSymbol.getTypeInfo().getFinalType( pst.getTypeInfoProvider() );
|
||||
classSymbol = (IContainerSymbol) info.getTypeSymbol();
|
||||
pst.getTypeInfoProvider().returnTypeInfo( info );
|
||||
}
|
||||
if( classSymbol == null || ! (classSymbol instanceof IDerivableContainerSymbol ) ){
|
||||
return null;
|
||||
}
|
||||
|
||||
List parameters = new ArrayList();
|
||||
ASTNewDescriptor newDescriptor = (ASTNewDescriptor) expression.getNewExpressionDescriptor();
|
||||
List newInitializerExpressions = newDescriptor.getNewInitializerExpressionsList();
|
||||
int size = newInitializerExpressions.size();
|
||||
for( int i = 0; i < size; i++ )
|
||||
{
|
||||
ASTExpression expressionList = (ASTExpression) newInitializerExpressions.get(i);
|
||||
while( expressionList != null ){
|
||||
parameters.add( expressionList.getResultType().getResult() );
|
||||
if( expressionList.getExpressionKind() == IASTExpression.Kind.EXPRESSIONLIST )
|
||||
expressionList = (ASTExpression) expressionList.getRHSExpression();
|
||||
else
|
||||
expressionList = null;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return ((IDerivableContainerSymbol)classSymbol).lookupConstructor( parameters );
|
||||
} catch (ParserSymbolTableException e1) {
|
||||
//fall out and return null
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.core.parser.ast.IASTFactory#getNodeForThisExpression(org.eclipse.cdt.core.parser.ast.IASTExpression)
|
||||
*/
|
||||
|
|
Loading…
Add table
Reference in a new issue