From 1362e388a1e7d7a2662a25cc5ac954b73d7f341e Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Mon, 7 Feb 2005 20:32:54 +0000 Subject: [PATCH] fix bug 84615 --- .../core/parser/tests/ast2/AST2CPPTests.java | 27 ++++++++ .../core/dom/parser/cpp/CPPSemantics.java | 62 +++++++++++++------ 2 files changed, 69 insertions(+), 20 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index 5eb5171fc59..8f016a59728 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -28,6 +28,7 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IArrayType; import org.eclipse.cdt.core.dom.ast.IBasicType; +import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.IEnumeration; import org.eclipse.cdt.core.dom.ast.IEnumerator; @@ -1082,5 +1083,31 @@ public class AST2CPPTests extends AST2BaseTest { assertInstances( col, x2, 2 ); } + public void testBug84615() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("class A { public : static int n; }; \n" ); //$NON-NLS-1$ + buffer.append("int main() { \n" ); //$NON-NLS-1$ + buffer.append(" int A; \n" ); //$NON-NLS-1$ + buffer.append(" A::n = 42; \n" ); //$NON-NLS-1$ + buffer.append(" A b; \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(), 9); + + ICPPClassType A = (ICPPClassType) col.getName(0).resolveBinding(); + ICPPField n = (ICPPField) col.getName(1).resolveBinding(); + IBinding Aref = col.getName(5).resolveBinding(); + IBinding nref = col.getName(6).resolveBinding(); + IProblemBinding prob = (IProblemBinding) col.getName(7).resolveBinding(); + + assertSame( A, Aref ); + assertSame( n, nref ); + assertNotNull( prob ); + + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java index c4aa791f899..5035988a639 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java @@ -34,6 +34,7 @@ import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; @@ -100,7 +101,7 @@ public class CPPSemantics { static protected class LookupData { - private IASTName astName; + protected IASTName astName; public char[] name; public ObjectMap usingDirectives = ObjectMap.EMPTY_MAP; public ObjectSet visited = ObjectSet.EMPTY_SET; //used to ensure we don't visit things more than once @@ -134,7 +135,13 @@ public class CPPSemantics { public boolean typesOnly(){ if( astName == null ) return false; IASTNode parent = astName.getParent(); - return ( parent instanceof ICPPASTBaseSpecifier || parent instanceof ICPPASTElaboratedTypeSpecifier); + if( parent instanceof ICPPASTBaseSpecifier || parent instanceof ICPPASTElaboratedTypeSpecifier ) + return true; + if( parent instanceof ICPPASTQualifiedName ){ + IASTName [] ns = ((ICPPASTQualifiedName)parent).getNames(); + return ( astName != ns[ ns.length -1 ] ); + } + return false; } public boolean forUsingDeclaration(){ if( astName == null ) return false; @@ -342,27 +349,42 @@ public class CPPSemantics { binding = e2.getProblem(); } //4: post processing - if( binding instanceof ICPPClassType && data.considerConstructors() ){ + binding = postResolution( binding, data ); + return binding; + } + + /** + * @param binding + * @param data + * @param name + * @return + */ + private static IBinding postResolution( IBinding binding, LookupData data ) { + if( binding instanceof ICPPClassType && data.considerConstructors() ){ ICPPClassType cls = (ICPPClassType) binding; try { //force resolution of constructor bindings cls.getConstructors(); //then use the class scope to resolve which one. - binding = ((ICPPClassScope)cls.getCompositeScope()).getBinding( name ); + binding = ((ICPPClassScope)cls.getCompositeScope()).getBinding( data.astName ); } catch ( DOMException e ) { binding = e.getProblem(); } - } + } + if( data.astName.getPropertyInParent() == IASTNamedTypeSpecifier.NAME && !( binding instanceof IType || binding instanceof ICPPConstructor) ){ + binding = new ProblemBinding( IProblemBinding.SEMANTIC_INVALID_TYPE, data.name ); + } + if( binding != null && data.forDefinition() && !( binding instanceof IProblemBinding ) ){ - addDefinition( binding, name ); + addDefinition( binding, data.astName ); } if( binding == null ) - binding = new ProblemBinding(IProblemBinding.SEMANTIC_NAME_NOT_FOUND, name.toCharArray() ); - return binding; - } + binding = new ProblemBinding(IProblemBinding.SEMANTIC_NAME_NOT_FOUND, data.name ); + return binding; + } - static private CPPSemantics.LookupData createLookupData( IASTName name ){ + static private CPPSemantics.LookupData createLookupData( IASTName name ){ CPPSemantics.LookupData data = new CPPSemantics.LookupData( name ); IASTNode parent = name.getParent(); if( parent instanceof ICPPASTQualifiedName ){ @@ -853,8 +875,16 @@ public class CPPSemantics { if( CharArrayUtils.equals( name.toCharArray(), data.name ) ){ return name; } - + } else if( declaration instanceof ICPPASTNamespaceDefinition ){ + IASTName namespaceName = ((ICPPASTNamespaceDefinition) declaration).getName(); + if( CharArrayUtils.equals( namespaceName.toCharArray(), data.name ) ) + return namespaceName; + } else if( declaration instanceof ICPPASTNamespaceAlias ){ + IASTName alias = ((ICPPASTNamespaceAlias) declaration).getAlias(); + if( CharArrayUtils.equals( alias.toCharArray(), data.name ) ) + return alias; } + if( data.typesOnly() ) return null; @@ -883,15 +913,7 @@ public class CPPSemantics { } } } - } else if( declaration instanceof ICPPASTNamespaceDefinition ){ - IASTName namespaceName = ((ICPPASTNamespaceDefinition) declaration).getName(); - if( CharArrayUtils.equals( namespaceName.toCharArray(), data.name ) ) - return namespaceName; - } else if( declaration instanceof ICPPASTNamespaceAlias ){ - IASTName alias = ((ICPPASTNamespaceAlias) declaration).getAlias(); - if( CharArrayUtils.equals( alias.toCharArray(), data.name ) ) - return alias; - } + } return null; }