mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-30 21:55:31 +02:00
for statements, field references
This commit is contained in:
parent
b0f2b85991
commit
b6fa19a6f7
5 changed files with 87 additions and 36 deletions
|
@ -541,5 +541,58 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
assertInstances( collector, f, 2 );
|
||||
assertInstances( collector, a, 3 );
|
||||
}
|
||||
public void testSimpleFunctionCall() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append( "void f(); \n" ); //$NON-NLS-1$
|
||||
buffer.append( "void g() { \n" ); //$NON-NLS-1$
|
||||
buffer.append( " f(); \n" ); //$NON-NLS-1$
|
||||
buffer.append( "} \n" ); //$NON-NLS-1$
|
||||
buffer.append( "void f(){ } \n" ); //$NON-NLS-1$
|
||||
|
||||
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
|
||||
CPPNameCollector collector = new CPPNameCollector();
|
||||
CPPVisitor.visitTranslationUnit( tu, collector );
|
||||
|
||||
IFunction f = (IFunction) collector.getName(0).resolveBinding();
|
||||
IFunction g = (IFunction) collector.getName( 1 ).resolveBinding();
|
||||
|
||||
assertInstances( collector, f, 3 );
|
||||
assertInstances( collector, g, 1 );
|
||||
}
|
||||
|
||||
public void testForLoop() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append( "void f() { \n"); //$NON-NLS-1$
|
||||
buffer.append( " for( int i = 0; i < 5; i++ ) { \n"); //$NON-NLS-1$
|
||||
buffer.append( " i; \n"); //$NON-NLS-1$
|
||||
buffer.append( " } \n"); //$NON-NLS-1$
|
||||
buffer.append( "} \n"); //$NON-NLS-1$
|
||||
|
||||
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
|
||||
CPPNameCollector collector = new CPPNameCollector();
|
||||
CPPVisitor.visitTranslationUnit( tu, collector );
|
||||
|
||||
IVariable i = (IVariable) collector.getName(1).resolveBinding();
|
||||
|
||||
assertInstances( collector, i, 4 );
|
||||
}
|
||||
|
||||
public void testExpressionFieldReference() throws Exception{
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append( "struct A { int x; }; \n"); //$NON-NLS-1$
|
||||
buffer.append( "void f(){ \n"); //$NON-NLS-1$
|
||||
buffer.append( " ((struct A *) 1)->x; \n"); //$NON-NLS-1$
|
||||
buffer.append( "} \n"); //$NON-NLS-1$
|
||||
|
||||
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP );
|
||||
CPPNameCollector collector = new CPPNameCollector();
|
||||
CPPVisitor.visitTranslationUnit( tu, collector );
|
||||
|
||||
ICPPClassType A = (ICPPClassType) collector.getName(0).resolveBinding();
|
||||
IField x = (IField) collector.getName(1).resolveBinding();
|
||||
|
||||
assertInstances( collector, A, 2 );
|
||||
assertInstances( collector, x, 2 );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,8 @@ import org.eclipse.cdt.core.dom.ast.IScope;
|
|||
* @author jcamelon
|
||||
*/
|
||||
public class CPPASTForStatement extends CPPASTNode implements IASTForStatement {
|
||||
|
||||
private IScope scope = null;
|
||||
|
||||
private IASTExpression initialExpression;
|
||||
private IASTDeclaration initDeclaration;
|
||||
private IASTExpression condition;
|
||||
|
@ -102,8 +103,9 @@ public class CPPASTForStatement extends CPPASTNode implements IASTForStatement {
|
|||
* @see org.eclipse.cdt.core.dom.ast.IASTForStatement#getScope()
|
||||
*/
|
||||
public IScope getScope() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
if( scope == null )
|
||||
scope = new CPPBlockScope( this );
|
||||
return scope;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,45 +13,14 @@
|
|||
*/
|
||||
package org.eclipse.cdt.internal.core.dom.parser.cpp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope;
|
||||
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
|
||||
|
||||
/**
|
||||
* @author aniefer
|
||||
*/
|
||||
public class CPPBlockScope extends CPPNamespaceScope implements ICPPBlockScope {
|
||||
private CharArrayObjectMap bindings = CharArrayObjectMap.EMPTY_MAP;
|
||||
|
||||
public CPPBlockScope( IASTNode physicalNode ){
|
||||
super( physicalNode );
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPScope#addBinding(org.eclipse.cdt.core.dom.ast.IBinding)
|
||||
*/
|
||||
public void addBinding(IBinding binding) {
|
||||
if( bindings == CharArrayObjectMap.EMPTY_MAP )
|
||||
bindings = new CharArrayObjectMap(1);
|
||||
bindings.put( binding.getNameCharArray(), binding );
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPScope#getBinding(int, char[])
|
||||
*/
|
||||
public IBinding getBinding(int namespaceType, char[] name) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
|
||||
*/
|
||||
public List find(String name) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -223,6 +223,9 @@ public class CPPSemantics {
|
|||
}
|
||||
}
|
||||
static protected IBinding resolveBinding( IASTName name ){
|
||||
if( name.toCharArray().length == 0 )
|
||||
return null;
|
||||
|
||||
//1: get some context info off of the name to figure out what kind of lookup we want
|
||||
LookupData data = createLookupData( name );
|
||||
|
||||
|
@ -260,7 +263,8 @@ public class CPPSemantics {
|
|||
}
|
||||
}
|
||||
} else if( parent instanceof IASTDeclarator ){
|
||||
data.forDefinition = true;
|
||||
if( parent.getParent() instanceof IASTSimpleDeclaration )
|
||||
data.forDefinition = true;
|
||||
} else if ( parent instanceof ICPPASTBaseSpecifier ||
|
||||
parent instanceof ICPPASTElaboratedTypeSpecifier)
|
||||
{
|
||||
|
|
|
@ -147,6 +147,8 @@ public class CPPVisitor {
|
|||
if( binding != null )
|
||||
return binding;
|
||||
}
|
||||
} else if( parent instanceof IASTTypeId ){
|
||||
return CPPSemantics.resolveBinding( elabType.getName() );
|
||||
}
|
||||
|
||||
ICPPScope scope = (ICPPScope) getContainingScope( elabType );
|
||||
|
@ -191,6 +193,10 @@ public class CPPVisitor {
|
|||
private static IBinding createBinding( IASTDeclarator declarator ){
|
||||
|
||||
IASTNode parent = declarator.getParent();
|
||||
|
||||
if( parent instanceof IASTTypeId )
|
||||
return CPPSemantics.resolveBinding( declarator.getName() );
|
||||
|
||||
ICPPScope scope = (ICPPScope) getContainingScope( parent );
|
||||
IBinding binding = ( scope != null ) ? scope.getBinding( declarator.getName() ) : null;
|
||||
|
||||
|
@ -249,6 +255,8 @@ public class CPPVisitor {
|
|||
return getContainingScope( (IASTDeclSpecifier) node );
|
||||
else if( node instanceof IASTParameterDeclaration )
|
||||
return getContainingScope( (IASTParameterDeclaration) node );
|
||||
else if( node instanceof IASTExpression )
|
||||
return getContainingScope( (IASTExpression) node );
|
||||
else if( node instanceof IASTEnumerator ){
|
||||
//put the enumerators in the same scope as the enumeration
|
||||
return getContainingScope( (IASTEnumerationSpecifier) node.getParent() );
|
||||
|
@ -257,6 +265,15 @@ public class CPPVisitor {
|
|||
return getContainingScope( node.getParent() );
|
||||
}
|
||||
|
||||
public static IScope getContainingScope( IASTExpression expression ){
|
||||
IASTNode parent = expression.getParent();
|
||||
if( parent instanceof IASTForStatement ){
|
||||
return ((IASTForStatement)parent).getScope();
|
||||
} else if( parent instanceof IASTCompoundStatement ){
|
||||
return ((IASTCompoundStatement)parent).getScope();
|
||||
}
|
||||
return getContainingScope( parent );
|
||||
}
|
||||
public static IScope getContainingScope( IASTName name ){
|
||||
IASTNode parent = name.getParent();
|
||||
if( parent instanceof ICPPASTQualifiedName ){
|
||||
|
@ -329,7 +346,11 @@ public class CPPVisitor {
|
|||
|
||||
public static IScope getContainingScope( IASTDeclSpecifier compTypeSpec ){
|
||||
IASTNode parent = compTypeSpec.getParent();
|
||||
return getContainingScope( (IASTSimpleDeclaration) parent );
|
||||
if( parent instanceof IASTSimpleDeclaration )
|
||||
return getContainingScope( (IASTSimpleDeclaration) parent );
|
||||
else if( parent instanceof IASTTypeId )
|
||||
return getContainingScope( parent.getParent() );
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -837,6 +858,8 @@ public class CPPVisitor {
|
|||
declSpec = ((IASTParameterDeclaration) parent).getDeclSpecifier();
|
||||
else if( parent instanceof IASTSimpleDeclaration )
|
||||
declSpec = ((IASTSimpleDeclaration)parent).getDeclSpecifier();
|
||||
else if( parent instanceof IASTTypeId )
|
||||
declSpec = ((IASTTypeId)parent).getDeclSpecifier();
|
||||
|
||||
IType type = createType( declSpec );
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue