1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-28 19:35:36 +02:00

bugs 88338, 88501, 88459

This commit is contained in:
Andrew Niefer 2005-03-22 19:57:24 +00:00
parent 35c088c55c
commit a2ed1fa490
14 changed files with 169 additions and 22 deletions

View file

@ -3027,6 +3027,45 @@ public class AST2CPPTests extends AST2BaseTest {
assertEquals( C.getID(), IProblemBinding.SEMANTIC_BAD_SCOPE );
}
public void testBug88459() throws Exception {
IASTTranslationUnit tu = parse( "int f(); ", ParserLanguage.CPP ); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
tu.accept( col );
IFunction f = (IFunction) col.getName(0).resolveBinding();
assertFalse( f.isStatic() );
}
public void testBug88501_1() throws Exception {
IASTTranslationUnit tu = parse( "void f(); void f( int ); struct f;", ParserLanguage.CPP ); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
tu.accept(col);
assertTrue( col.getName(0).resolveBinding() instanceof IFunction );
assertTrue( col.getName(1).resolveBinding() instanceof IFunction );
assertTrue( col.getName(3).resolveBinding() instanceof ICPPClassType );
}
// public void testBug8342_1() throws Exception {
// IASTTranslationUnit tu = parse( "int a; int a;", ParserLanguage.CPP ); //$NON-NLS-1$
// CPPNameCollector col = new CPPNameCollector();
// tu.accept(col);
//
// assertTrue( col.getName(0).resolveBinding() instanceof IVariable );
// IProblemBinding p = (IProblemBinding) col.getName(1).resolveBinding();
// assertEquals( p.getID(), IProblemBinding.SEMANTIC_INVALID_REDEFINITION );
// }
public void testBug8342_2() throws Exception {
IASTTranslationUnit tu = parse( "extern int a; extern char a;", ParserLanguage.CPP ); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
tu.accept(col);
assertTrue( col.getName(0).resolveBinding() instanceof IVariable );
IProblemBinding p = (IProblemBinding) col.getName(1).resolveBinding();
assertEquals( p.getID(), IProblemBinding.SEMANTIC_INVALID_REDECLARATION );
}
public void testNamespaceAlias_2() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("namespace A { int i; } \n"); //$NON-NLS-1$

View file

@ -46,7 +46,7 @@ import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.internal.core.dom.parser.c.CParameter;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.cdt.internal.core.dom.parser.c.ICBinding;
import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalBinding;
/**
* @author dsteffle
@ -453,7 +453,7 @@ public class AST2KnRTests extends AST2BaseTest {
IASTName A_struct_name1 = ((IASTCompositeTypeSpecifier)A_struct.getDeclSpecifier()).getName();
assertEquals( A_struct_name1.toString(), "A_struct" ); //$NON-NLS-1$
ICompositeType A_struct_type1 = (ICompositeType)A_struct_name1.resolveBinding();
assertEquals( ((ICBinding)A_struct_type1).getPhysicalNode(), A_struct.getDeclSpecifier() );
assertEquals( ((ICInternalBinding)A_struct_type1).getPhysicalNode(), A_struct.getDeclSpecifier() );
IField[] fields = A_struct_type1.getFields();
IField a1 = fields[0];
IField c1 = fields[1];
@ -516,7 +516,7 @@ public class AST2KnRTests extends AST2BaseTest {
IASTExpressionStatement stmt1 = (IASTExpressionStatement)f_def_body.getStatements()[0];
IASTExpressionStatement stmt2 = (IASTExpressionStatement)f_def_body.getStatements()[1];
IASTName a2 = ((IASTFieldReference)((IASTBinaryExpression)stmt1.getExpression()).getOperand1()).getFieldName();
assertEquals( ((IASTName)((ICBinding)a1).getPhysicalNode()).resolveBinding(), a2.resolveBinding() );
assertEquals( ((IASTName)((ICInternalBinding)a1).getPhysicalNode()).resolveBinding(), a2.resolveBinding() );
IASTName x3 = ((IASTIdExpression)((IASTFieldReference)((IASTBinaryExpression)stmt1.getExpression()).getOperand1()).getFieldOwner()).getName();
assertEquals( x2.resolveBinding(), x3.resolveBinding() );
assertEquals( ((IASTBinaryExpression)stmt1.getExpression()).getOperand2().toString(), "0" ); //$NON-NLS-1$
@ -526,8 +526,8 @@ public class AST2KnRTests extends AST2BaseTest {
IASTName x4 = ((IASTIdExpression)((IASTFieldReference)((IASTArraySubscriptExpression)((IASTBinaryExpression)stmt2.getExpression()).getOperand1()).getArrayExpression()).getFieldOwner()).getName();
IASTName c3 = ((IASTFieldReference)((IASTArraySubscriptExpression)((IASTBinaryExpression)stmt2.getExpression()).getOperand2()).getArrayExpression()).getFieldName();
IASTName x5 = ((IASTIdExpression)((IASTFieldReference)((IASTArraySubscriptExpression)((IASTBinaryExpression)stmt2.getExpression()).getOperand1()).getArrayExpression()).getFieldOwner()).getName();
assertEquals( ((IASTName)((ICBinding)c1).getPhysicalNode()).resolveBinding(), c2.resolveBinding() );
assertEquals( ((IASTName)((ICBinding)c1).getPhysicalNode()).resolveBinding(), c3.resolveBinding() );
assertEquals( ((IASTName)((ICInternalBinding)c1).getPhysicalNode()).resolveBinding(), c2.resolveBinding() );
assertEquals( ((IASTName)((ICInternalBinding)c1).getPhysicalNode()).resolveBinding(), c3.resolveBinding() );
assertEquals( x3.resolveBinding(), x4.resolveBinding() );
assertEquals( x4.resolveBinding(), x5.resolveBinding() );

View file

@ -2960,4 +2960,46 @@ public class AST2Tests extends AST2BaseTest {
assertEquals( prob.getID(), IProblemBinding.SEMANTIC_INVALID_OVERLOAD );
assertNotNull( foo );
}
public void testBug88338_CPP() throws Exception {
IASTTranslationUnit tu = parse( "struct A; struct A* a;", ParserLanguage.CPP ); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
tu.accept( col );
assertTrue( col.getName(0).isDeclaration() );
assertFalse( col.getName(0).isReference() );
assertTrue( col.getName(1).isReference() );
assertFalse( col.getName(1).isDeclaration() );
tu = parse( "struct A* a;", ParserLanguage.CPP ); //$NON-NLS-1$
col = new CPPNameCollector();
tu.accept( col );
assertTrue( col.getName(0).isDeclaration() );
assertFalse( col.getName(0).isReference() );
}
public void testBug88338_C() throws Exception {
IASTTranslationUnit tu = parse( "struct A; struct A* a;", ParserLanguage.C ); //$NON-NLS-1$
CPPNameCollector col = new CPPNameCollector();
tu.accept( col );
assertTrue( col.getName(0).isDeclaration() );
assertFalse( col.getName(0).isReference() );
assertTrue( col.getName(1).isReference() );
assertFalse( col.getName(1).isDeclaration() );
tu = parse( "struct A* a; struct A;", ParserLanguage.C ); //$NON-NLS-1$
col = new CPPNameCollector();
tu.accept( col );
col.getName(2).resolveBinding();
assertTrue( col.getName(0).isDeclaration() );
assertFalse( col.getName(0).isReference() );
assertTrue( col.getName(2).isDeclaration() );
assertFalse( col.getName(2).isReference() );
}
}

View file

@ -98,5 +98,15 @@ public interface IProblemBinding extends IBinding, IScope, IType {
*/
public static final int SEMANTIC_BAD_SCOPE = 0x00A;
/**
* invalid redefinition of the name
*/
public static final int SEMANTIC_INVALID_REDEFINITION = 0x00B;
/**
* invalid redeclaration of the name
*/
public static final int SEMANTIC_INVALID_REDECLARATION = 0x00C;
public static final int LAST_PROBLEM = SEMANTIC_BAD_SCOPE;
}

View file

@ -52,6 +52,8 @@ public class ProblemBinding implements IProblemBinding, IType, IScope {
errorMessages[SEMANTIC_DEFINITION_NOT_FOUND - 1] = ParserMessages.getString("ASTProblemFactory.error.semantic.dom.definitionNotFound"); //$NON-NLS-1$
errorMessages[SEMANTIC_KNR_PARAMETER_DECLARATION_NOT_FOUND - 1] = ParserMessages.getString("ASTProblemFactory.error.semantic.dom.knrParameterDeclarationNotFound"); //$NON-NLS-1$
errorMessages[SEMANTIC_LABEL_STATEMENT_NOT_FOUND - 1] = ParserMessages.getString("ASTProblemFactory.error.semantic.dom.labelStatementNotFound"); //$NON-NLS-1$
errorMessages[SEMANTIC_INVALID_REDEFINITION - 1] = ParserMessages.getString("ASTProblemFactory.error.semantic.dom.invalidRedefinition"); //$NON-NLS-1$
errorMessages[SEMANTIC_INVALID_REDECLARATION - 1] = ParserMessages.getString("ASTProblemFactory.error.semantic.dom.invalidRedeclaration"); //$NON-NLS-1$
errorMessages[SEMANTIC_BAD_SCOPE - 1] = ParserMessages.getString("ASTProblemFactory.error.semantic.dom.badScope"); //$NON-NLS-1$
}

View file

@ -10,7 +10,12 @@
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
/**
@ -66,8 +71,25 @@ public class CASTElaboratedTypeSpecifier extends CASTBaseDeclSpecifier implement
* @see org.eclipse.cdt.core.dom.ast.IASTNameOwner#getRoleForName(org.eclipse.cdt.core.dom.ast.IASTName)
*/
public int getRoleForName(IASTName n ) {
if( this.name == n )
return r_declaration;
return r_unclear;
if( n != name ) return r_unclear;
IASTNode parent = getParent();
if( !( parent instanceof IASTDeclaration ) )
return r_reference;
if( parent instanceof IASTSimpleDeclaration ){
IASTDeclarator [] dtors = ((IASTSimpleDeclaration)parent).getDeclarators();
if( dtors.length == 0 )
return r_declaration;
}
//can't tell, resolve the binding
IBinding binding = name.resolveBinding();
if( binding instanceof ICInternalBinding ){
IASTNode node = ((ICInternalBinding)binding).getPhysicalNode();
if( node == this )
return r_declaration;
}
return r_reference;
}
}

View file

@ -37,7 +37,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
* Created on Nov 5, 2004
* @author aniefer
*/
public class CFunction implements IFunction, ICBinding {
public class CFunction implements IFunction, ICInternalBinding {
private IASTStandardFunctionDeclarator [] declarators = null;
private IASTFunctionDeclarator definition;

View file

@ -33,7 +33,7 @@ import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
* Created on Nov 8, 2004
* @author aniefer
*/
public class CStructure implements ICompositeType, ICBinding {
public class CStructure implements ICompositeType, ICInternalBinding {
private IASTElaboratedTypeSpecifier[] declarations = null;
private ICASTCompositeTypeSpecifier definition;

View file

@ -27,7 +27,7 @@ import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
* Created on Nov 5, 2004
* @author aniefer
*/
public class CVariable implements IVariable, ICBinding {
public class CVariable implements IVariable, ICInternalBinding {
public static class CVariableProblem extends ProblemBinding implements IVariable {
public CVariableProblem( int id, char[] arg ) {
super( id, arg );

View file

@ -19,7 +19,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
/**
* @author aniefer
*/
public interface ICBinding {
public interface ICInternalBinding {
//methods needed by CVisitor but not meant for public interface
public IASTNode getPhysicalNode();
}

View file

@ -11,8 +11,14 @@
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
/**
* @author jcamelon
@ -67,7 +73,25 @@ public class CPPASTElaboratedTypeSpecifier extends CPPASTBaseDeclSpecifier
* @see org.eclipse.cdt.core.dom.ast.IASTNameOwner#getRoleForName(org.eclipse.cdt.core.dom.ast.IASTName)
*/
public int getRoleForName(IASTName n) {
if( n == name ) return r_reference; //TODO is this right?
return r_unclear;
if( n != name ) return r_unclear;
IASTNode parent = getParent();
if( !( parent instanceof IASTDeclaration ) )
return r_reference;
if( parent instanceof IASTSimpleDeclaration ){
IASTDeclarator [] dtors = ((IASTSimpleDeclaration)parent).getDeclarators();
if( dtors.length == 0 )
return r_declaration;
}
//can't tell, resolve the binding
IBinding binding = name.resolveBinding();
if( binding instanceof ICPPInternalBinding ){
IASTNode [] decls = ((ICPPInternalBinding)binding).getDeclarations();
if( ArrayUtil.contains( decls, name ) )
return r_declaration;
}
return r_reference;
}
}

View file

@ -352,12 +352,14 @@ public class CPPFunction implements ICPPFunction, ICPPInternalBinding {
int state = ( bits & IS_STATIC ) >> 2;
if( state > 1 ) return (state % 2 != 0);
IASTDeclSpecifier declSpec = null;
IASTFunctionDeclarator dtor = (IASTFunctionDeclarator) getDefinition();
IASTDeclSpecifier declSpec = ((IASTFunctionDefinition)dtor.getParent()).getDeclSpecifier();
if( declSpec.getStorageClass() == IASTDeclSpecifier.sc_static ){
bits |= 3 << 2;
return true;
if( dtor != null ){
declSpec = ((IASTFunctionDefinition)dtor.getParent()).getDeclSpecifier();
if( declSpec.getStorageClass() == IASTDeclSpecifier.sc_static ){
bits |= 3 << 2;
return true;
}
}
IASTFunctionDeclarator[] dtors = (IASTFunctionDeclarator[]) getDeclarations();

View file

@ -294,7 +294,7 @@ public class CPPVisitor {
}
try {
binding = scope.getBinding( elabType.getName(), false );
if( binding == null ){
if( binding == null || !(binding instanceof ICPPClassType) ){
if( elabType.getKind() != IASTElaboratedTypeSpecifier.k_enum ){
binding = new CPPClassType( elabType.getName() );
scope.addName( elabType.getName() );
@ -468,8 +468,12 @@ public class CPPVisitor {
} catch ( DOMException e1 ) {
}
}
if( t1 != null && t2 != null && t1.equals( t2 ) ){
((CPPVariable)binding).addDeclaration( declarator.getName() );
if( t1 != null && t2 != null ){
if( t1.equals( t2 ) ){
((CPPVariable)binding).addDeclaration( declarator.getName() );
} else {
binding = new ProblemBinding( IProblemBinding.SEMANTIC_INVALID_REDECLARATION, declarator.getName().toCharArray() );
}
} else if( simpleDecl.getParent() instanceof ICPPASTCompositeTypeSpecifier ){
binding = new CPPField( declarator.getName() );
} else {

View file

@ -80,4 +80,6 @@ ASTProblemFactory.error.semantic.pst.recursiveTemplateInstantiation=Possible inf
ASTProblemFactory.error.semantic.dom.definitionNotFound=A definition was not found for {0}
ASTProblemFactory.error.semantic.dom.knrParameterDeclarationNotFound=A declaration was not found for the k&r parameter {0}
ASTProblemFactory.error.semantic.dom.labelStatementNotFound=A label statement was not found for the name {0}
ASTProblemFactory.error.semantic.dom.invalidRedefinition=Invalid redefinition of the name {0}
ASTProblemFactory.error.semantic.dom.invalidRedeclaration=Invalid redeclaration of the name {0}
ASTProblemFactory.error.semantic.dom.badScope=A scope could not be created to represent the name {0}