1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

fix bug 84228 for C++

This commit is contained in:
Andrew Niefer 2005-02-04 21:57:48 +00:00
parent 1af4417856
commit ae97aa9b2a
8 changed files with 133 additions and 66 deletions

View file

@ -1054,5 +1054,33 @@ public class AST2CPPTests extends AST2BaseTest {
s = (ICompositeType) col.getName(0).resolveBinding(); s = (ICompositeType) col.getName(0).resolveBinding();
assertNotNull( s ); assertNotNull( s );
} }
public void testBug84228() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append( "void f( int m, int c[m][m] ); \n" ); //$NON-NLS-1$
buffer.append( "void f( int m, int c[m][m] ){ \n" ); //$NON-NLS-1$
buffer.append( " int x; \n" ); //$NON-NLS-1$
buffer.append( " { int x = x; } \n" ); //$NON-NLS-1$
buffer.append( "} \n" ); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
assertEquals(col.size(), 13);
IParameter m = (IParameter) col.getName(3).resolveBinding();
IVariable x3 = (IVariable) col.getName(12).resolveBinding();
IVariable x2 = (IVariable) col.getName(11).resolveBinding();
IVariable x1 = (IVariable) col.getName(10).resolveBinding();
assertSame( x2, x3 );
assertNotSame( x1, x2 );
assertInstances( col, m, 6 );
assertInstances( col, x1, 1 );
assertInstances( col, x2, 2 );
}
} }

View file

@ -2788,7 +2788,7 @@ public class AST2Tests extends AST2BaseTest {
assertEquals(col.size(), 13); assertEquals(col.size(), 13);
IParameter m = (IParameter) col.getName(1).resolveBinding(); IParameter m = (IParameter) col.getName(3).resolveBinding();
IVariable x3 = (IVariable) col.getName(12).resolveBinding(); IVariable x3 = (IVariable) col.getName(12).resolveBinding();
IVariable x2 = (IVariable) col.getName(11).resolveBinding(); IVariable x2 = (IVariable) col.getName(11).resolveBinding();
IVariable x1 = (IVariable) col.getName(10).resolveBinding(); IVariable x1 = (IVariable) col.getName(10).resolveBinding();

View file

@ -41,4 +41,5 @@ public interface ICPPASTFunctionDeclarator extends IASTStandardFunctionDeclarato
public ICPPASTConstructorChainInitializer[] getConstructorChain(); public ICPPASTConstructorChainInitializer[] getConstructorChain();
public void addConstructorToChain( ICPPASTConstructorChainInitializer initializer ); public void addConstructorToChain( ICPPASTConstructorChainInitializer initializer );
public ICPPFunctionScope getFunctionScope();
} }

View file

@ -10,10 +10,14 @@
**********************************************************************/ **********************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp; package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTypeId; import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope;
/** /**
* @author jcamelon * @author jcamelon
@ -23,7 +27,7 @@ public class CPPASTFunctionDeclarator extends CPPASTDeclarator implements
private IASTParameterDeclaration [] parameters = null; private IASTParameterDeclaration [] parameters = null;
private static final int DEFAULT_PARAMETERS_LIST_SIZE = 2; private static final int DEFAULT_PARAMETERS_LIST_SIZE = 2;
private ICPPFunctionScope scope = null;
private int currentIndex = 0; private int currentIndex = 0;
private boolean varArgs; private boolean varArgs;
private boolean pureVirtual; private boolean pureVirtual;
@ -232,6 +236,14 @@ public class CPPASTFunctionDeclarator extends CPPASTDeclarator implements
constructorChain[ currentConstructorChainIndex++ ] = initializer; constructorChain[ currentConstructorChainIndex++ ] = initializer;
} }
public ICPPFunctionScope getFunctionScope(){
if( scope != null )
return scope;
ASTNodeProperty prop = getPropertyInParent();
if( prop == IASTSimpleDeclaration.DECLARATOR || prop == IASTFunctionDefinition.DECLARATOR )
scope = new CPPFunctionScope( this );
return scope;
}
} }

View file

@ -15,7 +15,7 @@ import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
/** /**
* @author jcamelon * @author jcamelon
@ -26,7 +26,6 @@ public class CPPASTFunctionDefinition extends CPPASTNode implements
private IASTDeclSpecifier declSpecifier; private IASTDeclSpecifier declSpecifier;
private IASTFunctionDeclarator declarator; private IASTFunctionDeclarator declarator;
private IASTStatement bodyStatement; private IASTStatement bodyStatement;
private ICPPFunctionScope scope;
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition#getDeclSpecifier() * @see org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition#getDeclSpecifier()
@ -74,9 +73,7 @@ public class CPPASTFunctionDefinition extends CPPASTNode implements
* @see org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition#getScope() * @see org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition#getScope()
*/ */
public IScope getScope() { public IScope getScope() {
if( scope == null ) return ((ICPPASTFunctionDeclarator)declarator).getFunctionScope();
scope = new CPPFunctionScope( this );
return scope;
} }

View file

@ -16,14 +16,15 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.ILabel; import org.eclipse.cdt.core.dom.ast.ILabel;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
/** /**
@ -36,7 +37,7 @@ public class CPPFunctionScope extends CPPScope implements ICPPFunctionScope {
/** /**
* @param physicalNode * @param physicalNode
*/ */
public CPPFunctionScope(IASTFunctionDefinition physicalNode) { public CPPFunctionScope(IASTFunctionDeclarator physicalNode) {
super(physicalNode); super(physicalNode);
} }
@ -71,12 +72,23 @@ public class CPPFunctionScope extends CPPScope implements ICPPFunctionScope {
} }
public IScope getParent() throws DOMException { public IScope getParent() throws DOMException {
IASTFunctionDefinition fn = (IASTFunctionDefinition) getPhysicalNode(); //we can't just resolve the function and get its parent scope, since there are cases where that
IFunction function = (IFunction) fn.getDeclarator().getName().resolveBinding(); //could loop since resolving functions requires resolving their parameter types
if( function instanceof ICPPMethod ){ IASTFunctionDeclarator fdtor = (IASTFunctionDeclarator) getPhysicalNode();
return function.getScope(); IASTName name = fdtor.getName();
} if( name instanceof ICPPASTQualifiedName ){
return super.getParent(); IASTName [] ns = ((ICPPASTQualifiedName)name).getNames();
if( ns.length > 1){
IBinding binding = ns[ ns.length - 2 ].resolveBinding();
if( binding instanceof ICPPClassType )
return ((ICPPClassType)binding).getCompositeScope();
else if( binding instanceof ICPPNamespace )
return ((ICPPNamespace)binding).getNamespaceScope();
return binding.getScope();
}
}
return CPPVisitor.getContainingScope( fdtor );
} }
} }

View file

@ -121,6 +121,16 @@ public class CPPSemantics {
astName = null; astName = null;
this.name = n; this.name = n;
} }
public boolean includeBlockItem( IASTNode item ){
if( astName == null ) return false;
if( astName.getParent() instanceof IASTIdExpression ||
item instanceof IASTNamespaceDefinition ||
(item instanceof IASTSimpleDeclaration && ((IASTSimpleDeclaration)item).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier ) )
{
return true;
}
return false;
}
public boolean typesOnly(){ public boolean typesOnly(){
if( astName == null ) return false; if( astName == null ) return false;
IASTNode parent = astName.getParent(); IASTNode parent = astName.getParent();
@ -649,8 +659,14 @@ public class CPPSemantics {
List found = null; List found = null;
if( parent instanceof IASTCompoundStatement ){ if( parent instanceof IASTCompoundStatement ){
IASTCompoundStatement compound = (IASTCompoundStatement) parent; if( parent.getParent() instanceof IASTFunctionDefinition ){
nodes = compound.getStatements(); ICPPASTFunctionDeclarator dtor = (ICPPASTFunctionDeclarator) ((IASTFunctionDefinition)parent.getParent()).getDeclarator();
nodes = dtor.getParameters();
}
if( nodes == null || nodes.length == 0 ){
IASTCompoundStatement compound = (IASTCompoundStatement) parent;
nodes = compound.getStatements();
}
} else if ( parent instanceof IASTTranslationUnit ){ } else if ( parent instanceof IASTTranslationUnit ){
IASTTranslationUnit translation = (IASTTranslationUnit) parent; IASTTranslationUnit translation = (IASTTranslationUnit) parent;
nodes = translation.getDeclarations(); nodes = translation.getDeclarations();
@ -664,6 +680,9 @@ public class CPPSemantics {
nodes = ((ICPPASTNamespaceDefinition)namespaceDefs[0].getParent()).getDeclarations(); nodes = ((ICPPASTNamespaceDefinition)namespaceDefs[0].getParent()).getDeclarations();
namespaceIdx = -1; namespaceIdx = -1;
} else if( parent instanceof ICPPASTFunctionDeclarator ){
ICPPASTFunctionDeclarator dtor = (ICPPASTFunctionDeclarator) parent;
nodes = dtor.getParameters();
} }
if( scope instanceof ICPPClassScope ){ if( scope instanceof ICPPClassScope ){
@ -676,14 +695,8 @@ public class CPPSemantics {
while( item != null ) { while( item != null ) {
if( item == null || ( blockItem != null && ((ASTNode)item).getOffset() > ((ASTNode) blockItem).getOffset() )) if( item == null || ( blockItem != null && ((ASTNode)item).getOffset() > ((ASTNode) blockItem).getOffset() ))
break; break;
if( item == blockItem ){ if( item == blockItem && !data.includeBlockItem( item ) )
if( !(item instanceof IASTNamespaceDefinition) && break;
!(item instanceof IASTSimpleDeclaration &&
((IASTSimpleDeclaration)item).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier) )
{
break;
}
}
if( item instanceof ICPPASTUsingDirective && !data.ignoreUsingDirectives ) { if( item instanceof ICPPASTUsingDirective && !data.ignoreUsingDirectives ) {
if( usingDirectives != null ) if( usingDirectives != null )
@ -696,14 +709,28 @@ public class CPPSemantics {
found.add( possible ); found.add( possible );
} }
} }
if( item == blockItem )
break;
if( idx > -1 && ++idx < nodes.length ){ if( idx > -1 && ++idx < nodes.length ){
item = nodes[idx]; item = nodes[idx];
} else { } else {
item = null; item = null;
while( namespaceIdx > -1 && namespaceDefs.length > ++namespaceIdx ){ if( namespaceIdx > -1 ) {
nodes = ((ICPPASTNamespaceDefinition)namespaceDefs[0].getParent()).getDeclarations(); //check all definitions of this namespace
if( nodes.length > 0 ){ while( namespaceIdx > -1 && namespaceDefs.length > ++namespaceIdx ){
nodes = ((ICPPASTNamespaceDefinition)namespaceDefs[0].getParent()).getDeclarations();
if( nodes.length > 0 ){
idx = 0;
item = nodes[0];
break;
}
}
} else if( parent instanceof IASTCompoundStatement && nodes instanceof IASTParameterDeclaration [] ){
//function body, we were looking at parameters, now check the body itself
IASTCompoundStatement compound = (IASTCompoundStatement) parent;
nodes = compound.getStatements();
if( nodes.length > 0 ){
idx = 0; idx = 0;
item = nodes[0]; item = nodes[0];
break; break;
@ -760,7 +787,13 @@ public class CPPSemantics {
declaration = ((IASTDeclarationStatement)node).getDeclaration(); declaration = ((IASTDeclarationStatement)node).getDeclaration();
else if( node instanceof IASTForStatement ) else if( node instanceof IASTForStatement )
declaration = ((IASTForStatement)node).getInitDeclaration(); declaration = ((IASTForStatement)node).getInitDeclaration();
else if( node instanceof IASTParameterDeclaration && !data.typesOnly() ){
IASTParameterDeclaration parameterDeclaration = (IASTParameterDeclaration) node;
IASTName declName = parameterDeclaration.getDeclarator().getName();
if( CharArrayUtils.equals( declName.toCharArray(), data.name ) ){
return declName;
}
}
if( declaration == null ) if( declaration == null )
return null; return null;

View file

@ -16,6 +16,7 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp;
import java.util.ArrayList; import java.util.ArrayList;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
@ -488,7 +489,7 @@ public class CPPVisitor {
} else if( parent instanceof IASTCompoundStatement ){ } else if( parent instanceof IASTCompoundStatement ){
return ((IASTCompoundStatement)parent).getScope(); return ((IASTCompoundStatement)parent).getScope();
} else if( parent instanceof ICPPASTConstructorChainInitializer ){ } else if( parent instanceof ICPPASTConstructorChainInitializer ){
IASTNode node = getContainingBlockItem( parent ); IASTNode node = getContainingBlockItem( parent.getParent() );
if( node instanceof IASTFunctionDefinition ){ if( node instanceof IASTFunctionDefinition ){
IASTCompoundStatement body = (IASTCompoundStatement) ((IASTFunctionDefinition)node).getBody(); IASTCompoundStatement body = (IASTCompoundStatement) ((IASTFunctionDefinition)node).getBody();
return body.getScope(); return body.getScope();
@ -555,44 +556,27 @@ public class CPPVisitor {
} else if( parent instanceof IASTStatement ){ } else if( parent instanceof IASTStatement ){
scope = getContainingScope( (IASTStatement)parent ); scope = getContainingScope( (IASTStatement)parent );
} else if( parent instanceof IASTFunctionDefinition ){ } else if( parent instanceof IASTFunctionDefinition ){
IASTFunctionDeclarator fnDeclarator = ((IASTFunctionDefinition) parent ).getDeclarator(); IASTFunctionDeclarator fnDeclarator = ((IASTFunctionDefinition) parent ).getDeclarator();
IFunction function = (IFunction) fnDeclarator.getName().resolveBinding(); IFunction function = (IFunction) fnDeclarator.getName().resolveBinding();
try { try {
scope = function.getFunctionScope(); scope = function.getScope();
} catch ( DOMException e ) { } catch ( DOMException e ) {
return e.getProblem();
} }
} }
if( statement instanceof IASTGotoStatement || statement instanceof IASTLabelStatement ){ if( statement instanceof IASTGotoStatement || statement instanceof IASTLabelStatement ){
//labels have function scope while( !(parent instanceof IASTFunctionDefinition) ){
while( scope != null && !(scope instanceof ICPPFunctionScope) ){ parent = parent.getParent();
try {
scope = scope.getParent();
} catch ( DOMException e ) {
return e.getProblem();
}
} }
IASTFunctionDefinition fdef = (IASTFunctionDefinition) parent;
return ((ICPPASTFunctionDeclarator)fdef.getDeclarator()).getFunctionScope();
} }
return scope; return scope;
} }
public static IScope getContainingScope( IASTDeclSpecifier typeSpec ){ public static IScope getContainingScope( IASTDeclSpecifier typeSpec ){
// if( typeSpec instanceof ICPPASTCompositeTypeSpecifier ){
// ICPPASTCompositeTypeSpecifier compTypeSpec = (ICPPASTCompositeTypeSpecifier) typeSpec;
// IASTName name = compTypeSpec.getName();
// if( name instanceof ICPPASTQualifiedName ){
// IASTName [] names = ((ICPPASTQualifiedName)name).getNames();
// if( names.length > 1 ){
// IBinding binding = names[ names.length - 2 ].resolveBinding();
// if( binding instanceof ICPPClassType )
// return ((ICPPClassType)binding).getCompositeScope();
// else if( binding instanceof ICPPNamespace )
// return ((ICPPNamespace)binding).getNamespaceScope();
// }
// }
// }
IASTNode parent = typeSpec.getParent(); IASTNode parent = typeSpec.getParent();
if( parent instanceof IASTSimpleDeclaration ) if( parent instanceof IASTSimpleDeclaration )
return getContainingScope( (IASTSimpleDeclaration) parent ); return getContainingScope( (IASTSimpleDeclaration) parent );
@ -607,12 +591,10 @@ public class CPPVisitor {
*/ */
public static IScope getContainingScope(IASTParameterDeclaration parameterDeclaration) { public static IScope getContainingScope(IASTParameterDeclaration parameterDeclaration) {
ICPPASTFunctionDeclarator dtor = (ICPPASTFunctionDeclarator) parameterDeclaration.getParent(); ICPPASTFunctionDeclarator dtor = (ICPPASTFunctionDeclarator) parameterDeclaration.getParent();
IASTName name = dtor.getName(); ASTNodeProperty prop = dtor.getPropertyInParent();
if( name instanceof ICPPASTQualifiedName ) { if( prop == IASTSimpleDeclaration.DECLARATOR || prop == IASTFunctionDefinition.DECLARATOR )
IASTName[] ns = ((ICPPASTQualifiedName) name).getNames(); return dtor.getFunctionScope();
if( ns.length > 0 )
return getContainingScope( ns [ ns.length - 1 ] );
}
return getContainingScope( dtor ); return getContainingScope( dtor );
} }
@ -632,6 +614,8 @@ public class CPPVisitor {
return p; return p;
} else if ( parent instanceof IASTStatement || parent instanceof IASTTranslationUnit ) { } else if ( parent instanceof IASTStatement || parent instanceof IASTTranslationUnit ) {
return parent; return parent;
} else if( parent instanceof IASTFunctionDeclarator ){
return node;
} }
return getContainingBlockItem( parent ); return getContainingBlockItem( parent );