1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-10 09:45:39 +02:00

Fixed Bug 85049 -[Parser2] B * bp; declaration parsed as binary expression.

This commit is contained in:
John Camelon 2005-04-06 02:02:04 +00:00
parent 93e17563e0
commit 8b59056395
5 changed files with 4372 additions and 4241 deletions

View file

@ -1648,16 +1648,16 @@ public class AST2CPPTests extends AST2BaseTest {
// assertTrue(binding instanceof ICPPClassType);
// }
// public void testBug85049() throws Exception {
// StringBuffer buffer = new StringBuffer( "struct B { };\n" ); //$NON-NLS-1$
// buffer.append( "void g() {\n" ); //$NON-NLS-1$
// buffer.append( "B * bp; //1\n" ); //$NON-NLS-1$
// buffer.append( "}\n" ); //$NON-NLS-1$
// IASTTranslationUnit t = parse( buffer.toString(), ParserLanguage.CPP );
// IASTFunctionDefinition g = (IASTFunctionDefinition) t.getDeclarations()[1];
// IASTCompoundStatement body = (IASTCompoundStatement) g.getBody();
// assertTrue( body.getStatements()[0] instanceof IASTDeclarationStatement );
// }
public void testBug85049() throws Exception {
StringBuffer buffer = new StringBuffer( "struct B { };\n" ); //$NON-NLS-1$
buffer.append( "void g() {\n" ); //$NON-NLS-1$
buffer.append( "B * bp; //1\n" ); //$NON-NLS-1$
buffer.append( "}\n" ); //$NON-NLS-1$
IASTTranslationUnit t = parse( buffer.toString(), ParserLanguage.CPP );
IASTFunctionDefinition g = (IASTFunctionDefinition) t.getDeclarations()[1];
IASTCompoundStatement body = (IASTCompoundStatement) g.getBody();
assertTrue( body.getStatements()[0] instanceof IASTDeclarationStatement );
}
public void testPMConversions() throws Exception {

View file

@ -2920,16 +2920,20 @@ public class AST2Tests extends AST2BaseTest {
assertInstances(col, B, 4);
}
// public void testBug85049() throws Exception {
// StringBuffer buffer = new StringBuffer("typedef int B;\n"); //$NON-NLS-1$
// buffer.append("void g() {\n"); //$NON-NLS-1$
// buffer.append("B * bp; //1\n"); //$NON-NLS-1$
// buffer.append("}\n"); //$NON-NLS-1$
// IASTTranslationUnit t = parse(buffer.toString(), ParserLanguage.C );
// IASTFunctionDefinition g = (IASTFunctionDefinition) t.getDeclarations()[1];
// IASTCompoundStatement body = (IASTCompoundStatement) g.getBody();
// assertTrue(body.getStatements()[0] instanceof IASTDeclarationStatement);
// }
public void testBug85049() throws Exception {
StringBuffer buffer = new StringBuffer("typedef int B;\n"); //$NON-NLS-1$
buffer.append("void g() {\n"); //$NON-NLS-1$
buffer.append("B * bp; //1\n"); //$NON-NLS-1$
buffer.append("}\n"); //$NON-NLS-1$
IASTTranslationUnit t = parse(buffer.toString(), ParserLanguage.C );
IASTFunctionDefinition g = (IASTFunctionDefinition) t.getDeclarations()[1];
IASTCompoundStatement body = (IASTCompoundStatement) g.getBody();
final IASTStatement statement = body.getStatements()[0];
assertTrue(statement instanceof IASTDeclarationStatement);
IASTSimpleDeclaration bp = (IASTSimpleDeclaration) ((IASTDeclarationStatement)statement).getDeclaration();
assertTrue( bp.getDeclarators()[0].getName().resolveBinding() instanceof IVariable );
}
public void testBug84466() throws Exception {
StringBuffer buffer = new StringBuffer();

View file

@ -1524,17 +1524,17 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
}
}
// // A*B
// if (expressionStatement.getExpression() instanceof IASTBinaryExpression) {
// IASTBinaryExpression exp = (IASTBinaryExpression) expressionStatement
// .getExpression();
// if (exp.getOperator() == IASTBinaryExpression.op_multiply) {
// IASTExpression lhs = exp.getOperand1();
// if (lhs instanceof IASTIdExpression)
// if (queryIsTypeName(((IASTIdExpression) lhs).getName()))
// return ds;
// }
// }
// A*B
if (expressionStatement.getExpression() instanceof IASTBinaryExpression) {
IASTBinaryExpression exp = (IASTBinaryExpression) expressionStatement
.getExpression();
if (exp.getOperator() == IASTBinaryExpression.op_multiply) {
IASTExpression lhs = exp.getOperand1();
if (lhs instanceof IASTIdExpression)
if (queryIsTypeName(((IASTIdExpression) lhs).getName()))
return ds;
}
}
// x = y; // default to int
// valid @ Translation Unit scope
@ -1565,6 +1565,8 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
return expressionStatement;
}
protected abstract boolean queryIsTypeName(IASTName name);
/**
* @param ds
* @param expressionStatement

View file

@ -73,6 +73,7 @@ import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
@ -2554,4 +2555,49 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
return pd;
}
static class HeuristicTypeDetector extends CASTVisitor
{
private final char[] lookingForName;
boolean result = false;
{
shouldVisitDeclarations = true;
}
public HeuristicTypeDetector( char [] name )
{
this.lookingForName = name;
}
public int visit(IASTDeclaration declaration) {
if( declaration instanceof IASTSimpleDeclaration )
{
IASTSimpleDeclaration sd = (IASTSimpleDeclaration) declaration;
if( sd.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_typedef )
{
IASTDeclarator [] declarators = sd.getDeclarators();
for( int i = 0; i < declarators.length; ++i )
if( CharArrayUtils.equals( declarators[i].getName().toCharArray(), lookingForName ) )
{
result = true;
return PROCESS_ABORT;
}
}
}
return PROCESS_CONTINUE;
}
public boolean getAnswer()
{
return result;
}
}
protected boolean queryIsTypeName(IASTName name) {
HeuristicTypeDetector nc = new HeuristicTypeDetector( name.toCharArray() );
translationUnit.accept(nc);
return nc.result;
}
}