From d24992f0a6fec633594890ed755effb2540aaafc Mon Sep 17 00:00:00 2001 From: Thomas Corbat Date: Mon, 25 Feb 2013 14:18:07 +0100 Subject: [PATCH] Bug 401661 - CPPClassType.getScope() at split definition and declaration Now CPPClassType.getScope() returns the scope of the surrounding class or namespace independent of the location of the definition. --- .../core/parser/tests/ast2/AST2CPPTests.java | 66 +++++++++++++++++++ .../core/dom/parser/cpp/CPPClassType.java | 16 +++-- 2 files changed, 77 insertions(+), 5 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 ef945c40951..17636abb0f8 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 @@ -10195,4 +10195,70 @@ public class AST2CPPTests extends AST2TestBase { long BSize = SizeofCalculator.getSizeAndAlignment(B, nameB).size; assertEquals(pointerSize, BSize); } + + // namespace NS { + // class Enclosing { + // class Inner {}; + // }; + // } + public void testNestedClassScopeInlineDefinition_401661() throws Exception { + String code = getAboveComment(); + parseAndCheckBindings(code); + + BindingAssertionHelper bh = new BindingAssertionHelper(code, true); + + ICPPClassType Enclosing = bh.assertNonProblem("Enclosing", 9); + ICPPClassType Inner = bh.assertNonProblem("Inner", 5); + + assertEquals(Enclosing.getCompositeScope(), Inner.getScope()); + } + + // namespace NS { + // class Enclosing { + // class Inner; + // }; + // } + // class NS::Enclosing::Inner{}; + public void testNestedClassScopeSeparateDefinition_401661() throws Exception { + String code = getAboveComment(); + parseAndCheckBindings(code); + + BindingAssertionHelper bh = new BindingAssertionHelper(code, true); + + ICPPClassType Enclosing = bh.assertNonProblem("Enclosing", 9); + ICPPClassType Inner = bh.assertNonProblem("Inner;", 5); + + assertEquals(Enclosing.getCompositeScope(), Inner.getScope()); + } + + // namespace NS { + // class Inner {}; + // } + public void testClassScopeInlineDefinition_401661() throws Exception { + String code = getAboveComment(); + parseAndCheckBindings(code); + + BindingAssertionHelper bh = new BindingAssertionHelper(code, true); + + ICPPNamespace NamespaceNS = bh.assertNonProblem("NS {", 2); + ICPPClassType Inner = bh.assertNonProblem("Inner", 5); + + assertEquals(NamespaceNS.getNamespaceScope(), Inner.getScope()); + } + + // namespace NS { + // class Inner; + // } + // class NS::Inner{}; + public void testClassScopeSeparateDefinition_401661() throws Exception { + String code = getAboveComment(); + parseAndCheckBindings(code); + + BindingAssertionHelper bh = new BindingAssertionHelper(code, true); + + ICPPNamespace NamespaceNS = bh.assertNonProblem("NS {", 2); + ICPPClassType Inner = bh.assertNonProblem("Inner;", 5); + + assertEquals(NamespaceNS.getNamespaceScope(), Inner.getScope()); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassType.java index 7d88b8900a2..d5cee05e031 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassType.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassType.java @@ -117,10 +117,7 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp private ICPPClassType typeInIndex; public CPPClassType(IASTName name, IBinding indexBinding) { - if (name instanceof ICPPASTQualifiedName) { - IASTName[] ns = ((ICPPASTQualifiedName) name).getNames(); - name = ns[ns.length - 1]; - } + name = stripQualifier(name); IASTNode parent = name.getParent(); while (parent instanceof IASTName) parent = parent.getParent(); @@ -195,7 +192,8 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp @Override public IScope getScope() { IASTName name = definition != null ? definition : declarations[0]; - + name = stripQualifier(name); + IScope scope = CPPVisitor.getContainingScope(name); if (definition == null && name.getPropertyInParent() != ICPPASTQualifiedName.SEGMENT_NAME) { IASTNode node = declarations[0].getParent().getParent(); @@ -406,4 +404,12 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp } return false; } + + private IASTName stripQualifier(IASTName name) { + if (name instanceof ICPPASTQualifiedName) { + IASTName[] ns = ((ICPPASTQualifiedName)name).getNames(); + name = ns[ ns.length - 1 ]; + } + return name; + } }