diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 66f8a0823d5..2fe973f5a2f 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -25,7 +25,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.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTIfStatement; @@ -39,6 +38,7 @@ import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; 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.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression; @@ -67,6 +67,7 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTEnumerationSpecifier; import org.eclipse.cdt.core.dom.ast.c.ICASTFieldDesignator; import org.eclipse.cdt.core.dom.ast.c.ICASTPointer; import org.eclipse.cdt.core.dom.ast.c.ICArrayType; +import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding; import org.eclipse.cdt.core.dom.ast.c.ICFunctionScope; import org.eclipse.cdt.core.dom.ast.c.ICScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember; @@ -2129,4 +2130,49 @@ public class AST2Tests extends AST2BaseTest { assertFalse( mod.isVolatile() ); } + public void testExternalVariable() throws Exception + { + StringBuffer buffer = new StringBuffer(); + buffer.append( "void f() { \n" ); //$NON-NLS-1$ + buffer.append( " if( a == 0 ) \n" ); //$NON-NLS-1$ + buffer.append( " a = a + 3; \n" ); //$NON-NLS-1$ + buffer.append( "} \n" ); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C ); + CNameCollector col = new CNameCollector(); + CVisitor.visitTranslationUnit( tu, col ); + + IVariable a = (IVariable) col.getName( 1 ).resolveBinding(); + assertNotNull( a ); + assertTrue( a instanceof ICExternalBinding ); + assertInstances( col, a, 3 ); + } + + public void testExternalDefs() throws Exception + { + StringBuffer buffer = new StringBuffer(); + buffer.append( "void f() { \n" ); //$NON-NLS-1$ + buffer.append( " if( a == 0 ) \n" ); //$NON-NLS-1$ + buffer.append( " g( a ); \n" ); //$NON-NLS-1$ + buffer.append( " if( a < 0 ) \n" ); //$NON-NLS-1$ + buffer.append( " g( a >> 1 ); \n" ); //$NON-NLS-1$ + buffer.append( " if( a > 0 ) \n" ); //$NON-NLS-1$ + buffer.append( " g( *(&a + 2) ); \n" ); //$NON-NLS-1$ + buffer.append( "} \n" ); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C ); + CNameCollector col = new CNameCollector(); + CVisitor.visitTranslationUnit( tu, col ); + + IVariable a = (IVariable) col.getName( 1 ).resolveBinding(); + IFunction g = (IFunction) col.getName( 2 ).resolveBinding(); + assertNotNull( a ); + assertNotNull( g ); + assertTrue( a instanceof ICExternalBinding ); + assertTrue( g instanceof ICExternalBinding ); + + assertEquals( col.size(), 10 ); + assertInstances( col, a, 6 ); + assertInstances( col, g, 3 ); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/c/ICExternalBinding.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/c/ICExternalBinding.java new file mode 100644 index 00000000000..d8ac4afba4a --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/c/ICExternalBinding.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +/* + * Created on Jan 26, 2005 + */ +package org.eclipse.cdt.core.dom.ast.c; + +import org.eclipse.cdt.core.dom.ast.IBinding; + +/** + * @author aniefer + */ +public interface ICExternalBinding extends IBinding { + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalFunction.java new file mode 100644 index 00000000000..f972fdaaa86 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalFunction.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +/* + * Created on Jan 26, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.c; + +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.dom.ast.IFunctionType; +import org.eclipse.cdt.core.dom.ast.IParameter; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding; + +/** + * @author aniefer + */ +public class CExternalFunction implements IFunction, ICExternalBinding { + private IASTName name = null; + private IASTTranslationUnit tu = null; + + public CExternalFunction( IASTTranslationUnit tu, IASTName name ) { + this.name = name; + this.tu = tu; + } + + public IASTNode getPhysicalNode(){ + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IFunction#getParameters() + */ + public IParameter[] getParameters() { + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IFunction#getFunctionScope() + */ + public IScope getFunctionScope() { + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IFunction#getType() + */ + public IFunctionType getType() { + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getName() + */ + public String getName() { + return name.toString(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getNameCharArray() + */ + public char[] getNameCharArray() { + return name.toCharArray(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() + */ + public IScope getScope() { + return tu.getScope(); + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalVariable.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalVariable.java new file mode 100644 index 00000000000..cb3d4abfa48 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CExternalVariable.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +/* + * Created on Jan 26, 2005 + */ +package org.eclipse.cdt.internal.core.dom.parser.c; + +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.IType; +import org.eclipse.cdt.core.dom.ast.IVariable; +import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding; + +/** + * @author aniefer + */ +public class CExternalVariable implements ICExternalBinding, IVariable { + private IASTTranslationUnit tu; + private IASTName name; + /** + * @param name + */ + public CExternalVariable( IASTTranslationUnit tu, IASTName name ) { + this.name = name; + this.tu = tu; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getName() + */ + public String getName() { + return name.toString(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getNameCharArray() + */ + public char[] getNameCharArray() { + return name.toCharArray(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() + */ + public IScope getScope() { + return tu.getScope(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.dom.ast.IVariable#getType() + */ + public IType getType() { + return null; + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java index 1104f8c9eb6..6cb744b855d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java @@ -898,11 +898,33 @@ public class CVisitor { blockItem = parent; else blockItem = null; + + if( blockItem instanceof IASTTranslationUnit ) + break; } + if( blockItem != null) + return externalBinding( (IASTTranslationUnit) blockItem, name ); + return null; } + private static IBinding externalBinding( IASTTranslationUnit tu, IASTName name ){ + IASTNode parent = name.getParent(); + IBinding external = null; + if( parent instanceof IASTIdExpression ){ + if( parent.getPropertyInParent() == IASTFunctionCallExpression.FUNCTION_NAME ){ + //external function + external = new CExternalFunction( tu, name ); + } else { + //external variable + external = new CExternalVariable( tu, name ); + } + ((CScope)tu.getScope()).addBinding( external ); + } + return external; + } + private static IBinding checkForBinding( IASTDeclaration declaration, IASTName name ){ if( declaration instanceof IASTSimpleDeclaration ){ IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration;