1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 14:12:10 +02:00

Fixed Bug 86274 - [Bindings] references inside typeid() don't have proper bindings

Fixed NPE in LocationMap.
This commit is contained in:
John Camelon 2005-04-06 21:02:57 +00:00
parent 5cbaa2e78d
commit cdbad1649e
4 changed files with 69 additions and 13 deletions

View file

@ -13,6 +13,7 @@
*/
package org.eclipse.cdt.core.parser.tests.ast2;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
@ -31,8 +32,10 @@ import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBasicType;
@ -3278,6 +3281,8 @@ public class AST2CPPTests extends AST2BaseTest {
assertEquals( expression.getSimpleType(), ICPPASTSimpleTypeConstructorExpression.t_int );
}
public void testBug90498_1() throws Exception {
IASTTranslationUnit tu = parse( "typedef INT ( FOO ) (INT);", ParserLanguage.CPP ); //$NON-NLS-1$
@ -3309,14 +3314,50 @@ public class AST2CPPTests extends AST2BaseTest {
assertNotNull( dtor.getInitializer() );
}
public void testBug866274() throws Exception {
StringBuffer buffer = new StringBuffer( "class D { /* ... */ };\n" ); //$NON-NLS-1$
buffer.append( "D d1;\n"); //$NON-NLS-1$
buffer.append( "const D d2;\n"); //$NON-NLS-1$
buffer.append( "void foo() {\n"); //$NON-NLS-1$
buffer.append( " typeid(d1) == typeid(d2);\n"); //$NON-NLS-1$
buffer.append( " typeid(D) == typeid(d2);\n"); //$NON-NLS-1$
buffer.append( "}\n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
IASTFunctionDefinition foo = (IASTFunctionDefinition) tu.getDeclarations()[3];
IASTCompoundStatement cs = (IASTCompoundStatement) foo.getBody();
IASTStatement [] subs = cs.getStatements();
for( int i = 0; i < subs.length; ++i )
{
IASTBinaryExpression be = (IASTBinaryExpression) ((IASTExpressionStatement)subs[i]).getExpression();
if( i == 1 )
{
IASTTypeIdExpression expression = (IASTTypeIdExpression) be.getOperand1();
IASTTypeId typeId = expression.getTypeId();
assertTrue( ((IASTNamedTypeSpecifier)typeId.getDeclSpecifier()).getName().resolveBinding() instanceof IType );
}
else
{
IASTUnaryExpression expression = (IASTUnaryExpression) be.getOperand1();
IASTIdExpression idExpression = (IASTIdExpression) expression.getOperand();
assertTrue( idExpression.getName().resolveBinding() instanceof IVariable );
}
IASTUnaryExpression expression = (IASTUnaryExpression) be.getOperand2();
IASTIdExpression idExpression = (IASTIdExpression) expression.getOperand();
assertTrue( idExpression.getName().resolveBinding() instanceof IVariable );
}
}
public void testTypedefFunction() throws Exception {
IASTTranslationUnit tu = parse( "typedef int foo (int);", ParserLanguage.CPP ); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
tu.accept( col );
tu.accept( col );
IBinding binding = col.getName(0).resolveBinding();
assertTrue( binding instanceof ITypedef );
assertTrue( ((ITypedef)binding).getType() instanceof IFunctionType );
IBinding binding = col.getName(0).resolveBinding();
assertTrue( binding instanceof ITypedef );
assertTrue( ((ITypedef)binding).getType() instanceof IFunctionType );
}
}

View file

@ -44,7 +44,6 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
@ -393,7 +392,7 @@ public class DOMLocationTests extends AST2BaseTest {
IASTBinaryExpression bexp = (IASTBinaryExpression) ((IASTExpressionStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) tu
.getDeclarations()[3]).getBody()).getStatements()[0])
.getExpression();
IASTTypeIdExpression exp = (IASTTypeIdExpression) ((IASTBinaryExpression) ((IASTExpressionStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) tu
IASTUnaryExpression exp = (IASTUnaryExpression) ((IASTBinaryExpression) ((IASTExpressionStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) tu
.getDeclarations()[3]).getBody()).getStatements()[0])
.getExpression()).getOperand1();
@ -402,7 +401,7 @@ public class DOMLocationTests extends AST2BaseTest {
code.indexOf("typeid(d1) == typeid(d2)"), "typeid(d1) == typeid(d2)".length()); //$NON-NLS-1$ //$NON-NLS-2$
assertSoleLocation(exp,
code.indexOf("typeid(d1)"), "typeid(d1)".length()); //$NON-NLS-1$ //$NON-NLS-2$
exp = (IASTTypeIdExpression) ((IASTBinaryExpression) ((IASTExpressionStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) tu
exp = (IASTUnaryExpression) ((IASTBinaryExpression) ((IASTExpressionStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) tu
.getDeclarations()[3]).getBody()).getStatements()[0])
.getExpression()).getOperand2();
assertSoleLocation(exp,

View file

@ -1444,10 +1444,22 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
boolean isTypeId = true;
IASTExpression lhs = null;
IASTTypeId typeId = null;
IToken m = mark();
try {
boolean amb = false;
if ( LT(1) == IToken.tIDENTIFIER )
amb = true;
typeId = typeId(false, false);
if( amb && typeId.getDeclSpecifier() instanceof IASTNamedTypeSpecifier )
{
if( ! queryIsTypeName( ((IASTNamedTypeSpecifier) typeId.getDeclSpecifier()).getName() ) )
{
backup( m );
throwBacktrack( ((CPPASTNode)typeId).getOffset(), ((CPPASTNode)typeId).getLength() );
}
}
} catch (BacktrackException b) {
typeId = null;
isTypeId = false;
lhs = expression();
}

View file

@ -1207,7 +1207,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
protected static class _FunctionMacroDefinition extends _MacroDefinition
implements _IPreprocessorDirective {
public final char[][] parms;
private final char[][] parms;
/**
* @param parent
@ -1221,6 +1221,10 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
super(parent, startOffset, endOffset, name, nameOffset, expansion);
this.parms = parameters;
}
public char[][] getParms() {
return parms;
}
}
protected static class _Problem extends _Context {
@ -1351,7 +1355,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
r = new ASTObjectMacro();
else if (d instanceof _FunctionMacroDefinition) {
IASTPreprocessorFunctionStyleMacroDefinition f = new ASTFunctionMacro();
char[][] parms = ((_FunctionMacroDefinition) d).parms;
char[][] parms = ((_FunctionMacroDefinition) d).getParms();
for (int j = 0; j < parms.length; ++j) {
IASTFunctionStyleMacroParameter parm = new ASTFunctionMacroParameter();
parm.setParameter(new String(parms[j]));
@ -2454,7 +2458,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
}
public IMacroDefinition registerBuiltinFunctionStyleMacro(FunctionStyleMacro macro) {
IMacroDefinition result = new _FunctionMacroDefinition( tu, -1, -1, macro.name, -1, macro.expansion, macro.arglist );
IMacroDefinition result = new _FunctionMacroDefinition( tu, -1, -1, macro.name, -1, macro.expansion, removeNullArguments( macro.arglist ) );
tu.addBuiltinMacro( result );
return result;
}