diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/service/GDBPatternMatchingExpressions.java b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/service/GDBPatternMatchingExpressions.java index bee440d140f..e83df89332d 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/service/GDBPatternMatchingExpressions.java +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/service/GDBPatternMatchingExpressions.java @@ -689,6 +689,31 @@ public class GDBPatternMatchingExpressions extends AbstractDsfService implements Collections.sort(matches, new Comparator() { @Override public int compare(IExpressionDMContext o1, IExpressionDMContext o2) { + // For elements of the same array, we need to sort by index + if (isArrayPattern(o1.getExpression()) && isArrayPattern(o2.getExpression())) { + // Extract the array names and the array indices specification. + // The regex used will remove both [ and ] + String[] arrayExprParts1 = o1.getExpression().split("[\\[\\]]"); //$NON-NLS-1$ + assert arrayExprParts1 != null && arrayExprParts1.length == 2; + + String[] arrayExprParts2 = o2.getExpression().split("[\\[\\]]"); //$NON-NLS-1$ + assert arrayExprParts2 != null && arrayExprParts2.length == 2; + + // Compare array names + if (arrayExprParts1[0].compareTo(arrayExprParts2[0]) == 0) { + // We are dealing with the same array + try { + int arrayIndex1 = Integer.parseInt(arrayExprParts1[1]); + int arrayIndex2 = Integer.parseInt(arrayExprParts2[1]); + + if (arrayIndex1 == arrayIndex2) return 0; + if (arrayIndex1 > arrayIndex2) return 1; + return -1; + } catch (NumberFormatException e) { + // Invalid array index. Fall-back to sorting lexically. + } + } + } return o1.getExpression().compareTo(o2.getExpression()); } }); diff --git a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/GDBPatternMatchingExpressionsTest.java b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/GDBPatternMatchingExpressionsTest.java index 1ee87eecf07..5020794bf68 100644 --- a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/GDBPatternMatchingExpressionsTest.java +++ b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/GDBPatternMatchingExpressionsTest.java @@ -1226,6 +1226,37 @@ public class GDBPatternMatchingExpressionsTest extends BaseTestCase { checkChildren(exprDmc, -1, -1, children); checkChildrenCount(exprDmc, children.length); } + + /** + * Test that pattern-matched arrays are sorted properly by index instead + * of completely alphabetically. An alphabetical sorting would cause the + * following poor sorting: + * =a[1-11] + * a[10] + * a[11] + * a[1] + * a[2] + * ... + */ + @Test + public void testArraySorting() throws Throwable { + final String exprString = "=array[1-11];=arrayInt[1-2,11,20-22]"; + final String[] children = new String[] { + "array[1]","array[2]","array[3]","array[4]","array[5]","array[6]", + "array[7]","array[8]","array[9]","array[10]","array[11]", + "arrayInt[1]","arrayInt[2]","arrayInt[11]","arrayInt[20]","arrayInt[21]","arrayInt[22]"}; + + SyncUtil.runToLocation("testArrayMatching"); + MIStoppedEvent stoppedEvent = SyncUtil.step(6, StepType.STEP_OVER); + + IFrameDMContext frameDmc = SyncUtil.getStackFrame(stoppedEvent.getDMContext(), 0); + + final IExpressionDMContext exprDmc = SyncUtil.createExpression(frameDmc, exprString); + + checkChildren(exprDmc, -1, -1, children); + checkChildrenCount(exprDmc, children.length); + + } // Cannot use comma separator because of templates (bug 393474) // /**