mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-25 09:55:29 +02:00
Fixed Bug 85049 - [Parser2] B * bp; declaration parsed as binary expression.
This commit is contained in:
parent
b9cfea60b3
commit
98145350e0
7 changed files with 4679 additions and 4498 deletions
|
@ -13,9 +13,9 @@
|
|||
*/
|
||||
package org.eclipse.cdt.core.parser.tests.ast2;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
|
||||
|
@ -1596,17 +1596,26 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
public void testFindTypeBinding_2() throws Exception {
|
||||
IASTTranslationUnit tu = parse(
|
||||
"struct B; void f() { B * bp; }", ParserLanguage.CPP); //$NON-NLS-1$
|
||||
|
||||
IASTCompoundStatement compound = (IASTCompoundStatement) ((IASTFunctionDefinition) tu
|
||||
.getDeclarations()[1]).getBody();
|
||||
IASTBinaryExpression exp = (IASTBinaryExpression) ((IASTExpressionStatement) compound
|
||||
.getStatements()[0]).getExpression();
|
||||
|
||||
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) ((IASTDeclarationStatement) compound
|
||||
.getStatements()[0]).getDeclaration();
|
||||
IBinding binding = CPPSemantics.findTypeBinding(compound,
|
||||
((IASTIdExpression) exp.getOperand1()).getName());
|
||||
((ICPPASTNamedTypeSpecifier)decl.getDeclSpecifier()).getName());
|
||||
assertNotNull(binding);
|
||||
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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -35,12 +35,12 @@ public class CEnumeration implements IEnumeration {
|
|||
|
||||
private IASTName [] declarations = null;
|
||||
private IASTName definition = null;
|
||||
public CEnumeration( IASTName enum ){
|
||||
ASTNodeProperty prop = enum.getPropertyInParent();
|
||||
public CEnumeration( IASTName enumeration ){
|
||||
ASTNodeProperty prop = enumeration.getPropertyInParent();
|
||||
if( prop == IASTElaboratedTypeSpecifier.TYPE_NAME )
|
||||
declarations = new IASTName[] { enum };
|
||||
declarations = new IASTName[] { enumeration };
|
||||
else
|
||||
definition = enum;
|
||||
definition = enumeration;
|
||||
}
|
||||
|
||||
public void addDeclaration( IASTName decl ){
|
||||
|
|
|
@ -1976,4 +1976,25 @@ public class CVisitor {
|
|||
visitTranslationUnit( tu, action );
|
||||
return action.getReferences();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param startingPoint
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static IBinding findTypeBinding(IASTNode startingPoint, IASTName name) {
|
||||
if( startingPoint instanceof IASTTranslationUnit )
|
||||
{
|
||||
IASTDeclaration [] declarations = ((IASTTranslationUnit)startingPoint).getDeclarations();
|
||||
if( declarations.length > 0 )
|
||||
return findBinding( declarations[declarations.length - 1], name, COMPLETE | INCLUDE_BLOCK_ITEM );
|
||||
}
|
||||
if( startingPoint instanceof IASTCompoundStatement )
|
||||
{
|
||||
IASTStatement [] statements = ((IASTCompoundStatement)startingPoint).getStatements();
|
||||
if( statements.length > 0 )
|
||||
return findBinding( statements[ statements.length - 1 ], name, COMPLETE | INCLUDE_BLOCK_ITEM );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,6 @@ import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTGotoStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||
|
@ -65,6 +64,7 @@ import org.eclipse.cdt.core.dom.ast.IASTProblemStatement;
|
|||
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.IASTStandardFunctionDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
|
@ -72,6 +72,8 @@ 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.IASTWhileStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
|
||||
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayDesignator;
|
||||
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
|
||||
|
@ -548,6 +550,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
|
|||
}
|
||||
|
||||
translationUnit.setLocationResolver(scanner.getLocationResolver());
|
||||
mostRelevantScopeNode = translationUnit;
|
||||
|
||||
int lastBacktrack = -1;
|
||||
while (true) {
|
||||
|
@ -2532,5 +2535,16 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
|
|||
|
||||
return pd;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser#queryIsTypeName(org.eclipse.cdt.core.dom.ast.IASTName)
|
||||
*/
|
||||
protected boolean queryIsTypeName(IASTName name) {
|
||||
//TODO fix me
|
||||
IBinding b = CVisitor.findTypeBinding( mostRelevantScopeNode, name );
|
||||
if( b == null ) return false;
|
||||
if( b instanceof ITypedef ) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -74,6 +74,8 @@ 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.IASTWhileStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
|
||||
|
@ -1832,8 +1834,6 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
private static final int DEFAULT_POINTEROPS_LIST_SIZE = 4;
|
||||
private static final int DEFAULT_SIZE_EXCEPTIONS_LIST = 2;
|
||||
private static final int DEFAULT_CONSTRUCTOR_CHAIN_LIST_SIZE = 4;
|
||||
private IASTNode mostRelevantScopeNode;
|
||||
|
||||
/**
|
||||
* This is the standard cosntructor that we expect the Parser to be
|
||||
* instantiated with.
|
||||
|
@ -4806,8 +4806,32 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
.getArrayExpression() instanceof IASTFieldReference))
|
||||
return true;
|
||||
}
|
||||
//A & B = C;
|
||||
if (expressionStatement.getExpression() instanceof IASTBinaryExpression) {
|
||||
IASTBinaryExpression exp = (IASTBinaryExpression) expressionStatement
|
||||
.getExpression();
|
||||
if (exp.getOperator() == IASTBinaryExpression.op_assign) {
|
||||
IASTExpression lhs = exp.getOperand1();
|
||||
if (lhs instanceof IASTBinaryExpression
|
||||
&& ((IASTBinaryExpression) lhs).getOperator() == IASTBinaryExpression.op_binaryAnd) {
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return super
|
||||
.resolveOtherAmbiguitiesAsDeclaration(ds, expressionStatement);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser#queryIsTypeName(org.eclipse.cdt.core.dom.ast.IASTName)
|
||||
*/
|
||||
protected boolean queryIsTypeName(IASTName name) {
|
||||
IBinding b = CPPSemantics.findTypeBinding( mostRelevantScopeNode, name );
|
||||
if( b == null ) return false;
|
||||
if( b instanceof IType ) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue