1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-05 08:46:02 +02:00

Bug 394408: Support enhanced-expressions for local variables. Proper

sorting of enhanced-expressions representing arrays.

Change-Id: I87e9ca5f3422fa51a32d2b80e6c0c0c62fdf8b2b
Reviewed-on: https://git.eclipse.org/r/9297
Reviewed-by: Mikhail Khodjaiants <mikhailkhod@googlemail.com>
IP-Clean: Mikhail Khodjaiants <mikhailkhod@googlemail.com>
Tested-by: Mikhail Khodjaiants <mikhailkhod@googlemail.com>
Reviewed-by: Marc Khouzam <marc.khouzam@ericsson.com>
IP-Clean: Marc Khouzam <marc.khouzam@ericsson.com>
Tested-by: Marc Khouzam <marc.khouzam@ericsson.com>
This commit is contained in:
Marc Khouzam 2012-12-18 17:12:45 -05:00
parent cd7ffb2dbf
commit ce371e5f5c
2 changed files with 56 additions and 0 deletions

View file

@ -689,6 +689,31 @@ public class GDBPatternMatchingExpressions extends AbstractDsfService implements
Collections.sort(matches, new Comparator<IExpressionDMContext>() {
@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());
}
});

View file

@ -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)
// /**