Bug 482421 - Ensure enumerations stored in the index return their
authorNathan Ridge <zeratul976@hotmail.com>
Wed, 25 Nov 2015 07:48:53 +0000 (02:48 -0500)
committerSergey Prigogin <eclipse.sprigogin@gmail.com>
Thu, 24 Dec 2015 02:26:07 +0000 (18:26 -0800)
enumerators in the order of declaration

Change-Id: I49eec356a1b241b1dbe3eb9b7744adce5816a7c5
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPTemplateResolutionTest.java
core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/IPDOMCPPEnumType.java
core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPEnumScope.java
core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPEnumeration.java
core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPEnumerationSpecialization.java

index 9aa09ef..8700aba 100644 (file)
@@ -2213,6 +2213,68 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa
                assertNotNull(num);
                assertEquals(1, num.longValue());
        }
+       
+       //      template<typename T>
+       //      struct meta {
+       //        enum { 
+       //            a = T::value,
+       //            b = a
+       //        };
+       //      };
+       
+       //      struct S {
+       //          static const int value = 42;
+       //      };
+       //
+       //      template <int> struct waldo;
+       //
+       //      template <>
+       //      struct waldo<42> {
+       //          double operator()();
+       //      };
+       //
+       //      template <typename>
+       //      struct C : public waldo<meta<S>::b> {};
+       //
+       //      void bar(double);
+       //
+       //      void foo(C<S> x){
+       //          bar(x());
+       //      }
+       public void testDependentEnumerator_482421a() throws Exception {
+               checkBindings();
+       }
+
+       //      template<typename T>
+       //      struct meta {
+       //        enum { 
+       //            b = T::value,
+       //            a = b
+       //        };
+       //      };
+       
+       //      struct S {
+       //          static const int value = 42;
+       //      };
+       //
+       //      template <int> struct waldo;
+       //
+       //      template <>
+       //      struct waldo<42> {
+       //          double operator()();
+       //      };
+       //
+       //      template <typename>
+       //      struct C : public waldo<meta<S>::a> {};
+       //
+       //      void bar(double);
+       //
+       //      void foo(C<S> x){
+       //          bar(x());
+       //      }
+       public void testDependentEnumerator_482421b() throws Exception {
+               checkBindings();
+       }
 
        //      template<typename U>
        //      struct A {
index 807886f..78a3e89 100644 (file)
  *******************************************************************************/ 
 package org.eclipse.cdt.internal.core.pdom.dom.cpp;
 
+import java.util.List;
+
 import org.eclipse.cdt.core.dom.ast.IEnumerator;
 import org.eclipse.cdt.core.dom.ast.IScope;
 import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumeration;
 import org.eclipse.cdt.core.index.IIndexName;
-import org.eclipse.cdt.core.parser.util.CharArrayMap;
 import org.eclipse.cdt.internal.core.index.IIndexType;
 import org.eclipse.cdt.internal.core.pdom.dom.IPDOMBinding;
 
@@ -33,6 +34,7 @@ public interface IPDOMCPPEnumType extends ICPPEnumeration, IPDOMBinding, IIndexT
 
        /**
         * Called by the scope to access the enumerators.
+        * Returns the enumerators in the order of declaration.
         */
-       void loadEnumerators(CharArrayMap<IPDOMCPPEnumerator> map);
+       void loadEnumerators(List<IPDOMCPPEnumerator> enumerators);
 }
index 4ba004e..fef42c3 100644 (file)
@@ -14,7 +14,6 @@ package org.eclipse.cdt.internal.core.pdom.dom.cpp;
 import java.lang.ref.Reference;
 import java.lang.ref.SoftReference;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 
 import org.eclipse.cdt.core.CCorePlugin;
@@ -151,8 +150,12 @@ class PDOMCPPEnumScope implements ICPPEnumScope, IIndexScope {
 
                if (map == null) {
                        // there is no cache, build it:
-                       map= new CharArrayMap<IPDOMCPPEnumerator>();
-                       enumeration.loadEnumerators(map);
+                       List<IPDOMCPPEnumerator> enumerators = new ArrayList<>();
+                       enumeration.loadEnumerators(enumerators);
+                       map = new CharArrayMap<IPDOMCPPEnumerator>();
+                       for (IPDOMCPPEnumerator enumerator : enumerators) {
+                               map.put(enumerator.getNameCharArray(), enumerator);
+                       }
                        pdom.putCachedResult(key, new SoftReference<CharArrayMap<?>>(map));
                }
                return map;
@@ -171,14 +174,17 @@ class PDOMCPPEnumScope implements ICPPEnumScope, IIndexScope {
 
        public static IEnumerator[] getEnumerators(IPDOMCPPEnumType enumType) {
                try {
-                       CharArrayMap<IPDOMCPPEnumerator> map = getBindingMap(enumType);
+                       // We want to return the enumerators in order of declaration, so we don't
+                       // use the cache (getBindingsMap()) which stores them in a hash map and thus
+                       // loses the order.
+                       List<IPDOMCPPEnumerator> enumerators = new ArrayList<>();
+                       enumType.loadEnumerators(enumerators);
                        List<IEnumerator> result= new ArrayList<IEnumerator>();
-                       for (IEnumerator value : map.values()) {
+                       for (IEnumerator value : enumerators) {
                                if (IndexFilter.ALL_DECLARED.acceptBinding(value)) {
                                        result.add(value);
                                }
                        }
-                       Collections.reverse(result);
                        return result.toArray(new IEnumerator[result.size()]);
                } catch (CoreException e) {
                        CCorePlugin.log(e);
index 3f25a48..fecab62 100644 (file)
@@ -12,6 +12,8 @@
  *******************************************************************************/
 package org.eclipse.cdt.internal.core.pdom.dom.cpp;
 
+import java.util.List;
+
 import org.eclipse.cdt.core.CCorePlugin;
 import org.eclipse.cdt.core.dom.IPDOMNode;
 import org.eclipse.cdt.core.dom.IPDOMVisitor;
@@ -23,7 +25,6 @@ import org.eclipse.cdt.core.dom.ast.IType;
 import org.eclipse.cdt.core.dom.ast.ITypedef;
 import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumeration;
 import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope;
-import org.eclipse.cdt.core.parser.util.CharArrayMap;
 import org.eclipse.cdt.core.parser.util.CharArrayUtils;
 import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
 import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
@@ -217,15 +218,14 @@ class PDOMCPPEnumeration extends PDOMCPPBinding implements IPDOMCPPEnumType, IPD
        }
 
        @Override
-       public void loadEnumerators(final CharArrayMap<IPDOMCPPEnumerator> map) {
+       public void loadEnumerators(final List<IPDOMCPPEnumerator> enumerators) {
                try {
                        PDOMNodeLinkedList list = new PDOMNodeLinkedList(getLinkage(), record + OFFSET_ENUMERATOR_LIST);
                        list.accept(new IPDOMVisitor() {
                                @Override
                                public boolean visit(IPDOMNode node) throws CoreException {
                                        if (node instanceof IPDOMCPPEnumerator) {
-                                               final IPDOMCPPEnumerator item = (IPDOMCPPEnumerator) node;
-                                               map.put(item.getNameCharArray(), item);
+                                               enumerators.add((IPDOMCPPEnumerator) node);
                                        }
                                        return true;
                                }
index 39470ea..9ea8bb6 100644 (file)
@@ -11,6 +11,8 @@
  *******************************************************************************/
 package org.eclipse.cdt.internal.core.pdom.dom.cpp;
 
+import java.util.List;
+
 import org.eclipse.cdt.core.CCorePlugin;
 import org.eclipse.cdt.core.dom.IPDOMNode;
 import org.eclipse.cdt.core.dom.IPDOMVisitor;
@@ -24,7 +26,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumeration;
 import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumerationSpecialization;
 import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope;
 import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
-import org.eclipse.cdt.core.parser.util.CharArrayMap;
 import org.eclipse.cdt.core.parser.util.CharArrayUtils;
 import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
 import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
@@ -224,15 +225,14 @@ class PDOMCPPEnumerationSpecialization extends PDOMCPPSpecialization
        }
 
        @Override
-       public void loadEnumerators(final CharArrayMap<IPDOMCPPEnumerator> map) {
+       public void loadEnumerators(final List<IPDOMCPPEnumerator> enumerators) {
                try {
                        PDOMNodeLinkedList list = new PDOMNodeLinkedList(getLinkage(), record + OFFSET_ENUMERATOR_LIST);
                        list.accept(new IPDOMVisitor() {
                                @Override
                                public boolean visit(IPDOMNode node) throws CoreException {
                                        if (node instanceof IPDOMCPPEnumerator) {
-                                               final IPDOMCPPEnumerator item = (IPDOMCPPEnumerator) node;
-                                               map.put(item.getNameCharArray(), item);
+                                               enumerators.add((IPDOMCPPEnumerator) node);
                                        }
                                        return true;
                                }