From 59d0f15afec2a41b2637feb4c776d6ef89570e75 Mon Sep 17 00:00:00 2001 From: Jonah Graham Date: Wed, 6 Jan 2016 12:55:55 +0000 Subject: [PATCH] Bug 484894: Update generification of DisassemblyDocument This fix follows on from af49d7701ab719a7176ad941bc91b956931c8133 and redoes the generification changes brought on by the AbstractDocument changes for Neon M4. The earlier fix (af49d77) made a copy of the list to work around the casting problem, however many of the methods that work on the positions expected access to the real list, not a copy. For example, consider addModelPositionFirst(), it gets the CATEGORY_MODEL list and then adds to it. If getAddressRangePositions returned a copy, then the wrong list would be updated. Change-Id: I36ca589ba748b66541c632182aceaf0b0b64aea4 Signed-off-by: Jonah Graham --- .../model/DisassemblyDocument.java | 97 +++++++++---------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/dsf/org.eclipse.cdt.dsf.ui/src/org/eclipse/cdt/dsf/debug/internal/ui/disassembly/model/DisassemblyDocument.java b/dsf/org.eclipse.cdt.dsf.ui/src/org/eclipse/cdt/dsf/debug/internal/ui/disassembly/model/DisassemblyDocument.java index 984133c37f0..3b90b245d9f 100644 --- a/dsf/org.eclipse.cdt.dsf.ui/src/org/eclipse/cdt/dsf/debug/internal/ui/disassembly/model/DisassemblyDocument.java +++ b/dsf/org.eclipse.cdt.dsf.ui/src/org/eclipse/cdt/dsf/debug/internal/ui/disassembly/model/DisassemblyDocument.java @@ -178,7 +178,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu return (int)(fMeanSizeOfInstructions+.9); } - public Iterator getModelPositionIterator(BigInteger address) { + public Iterator getModelPositionIterator(BigInteger address) { try { return getPositionIterator(CATEGORY_MODEL, address); } catch (BadPositionCategoryException e) { @@ -196,39 +196,21 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu return positions.listIterator(idx); } - public Iterator getPositionIterator(String category, BigInteger address) throws BadPositionCategoryException { - List positions = getAddressRangePositions(category); + public Iterator getPositionIterator(String category, BigInteger address) throws BadPositionCategoryException { + List positions = getDocumentManagedPositions().get(category); + if (positions == null) { + throw new BadPositionCategoryException(); + } int idx = computeIndexInPositionListFirst(positions, address); return positions.listIterator(idx); } - private List getAddressRangePositions(String category) throws BadPositionCategoryException { - List tmpPositions = getDocumentManagedPositions().get(category); - if (tmpPositions == null) { + public int computeIndexInCategory(String category, BigInteger address) throws BadPositionCategoryException { + List c = getDocumentManagedPositions().get(category); + if (c == null) { throw new BadPositionCategoryException(); } - List positions = new ArrayList<>(); - for(Position position: tmpPositions) { - positions.add((AddressRangePosition) position); - } - return positions; - } - - private List getAddressRangePositionsRaw(String category) { - List tmpPositions = getDocumentManagedPositions().get(category); - if (tmpPositions == null) { - return null; - } - List positions = new ArrayList<>(); - for(Position position: tmpPositions) { - positions.add((AddressRangePosition) position); - } - return positions; - } - - public int computeIndexInCategory(String category, BigInteger address) throws BadPositionCategoryException { - List positions = getAddressRangePositions(category); - return computeIndexInPositionListFirst(positions, address); + return computeIndexInPositionListFirst(c, address); } /** @@ -241,7 +223,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu * @return the computed index * */ - protected int computeIndexInPositionListFirst(List positions, BigInteger address) { + protected int computeIndexInPositionListFirst(List positions, BigInteger address) { int size = positions.size(); if (size == 0) { return 0; @@ -251,7 +233,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu int mid = 0; while (low < high) { mid = (low + high) >>> 1; - AddressRangePosition range = positions.get(mid); + AddressRangePosition range = (AddressRangePosition)positions.get(mid); int compareSign = address.compareTo(range.fAddressOffset); if (compareSign < 0) { high = mid; @@ -265,14 +247,14 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu } int idx = mid; - AddressRangePosition p = positions.get(idx); + AddressRangePosition p = (AddressRangePosition)positions.get(idx); if (address.compareTo(p.fAddressOffset) == 0) { do { --idx; if (idx < 0) { break; } - p = positions.get(idx); + p = (AddressRangePosition)positions.get(idx); } while (address.compareTo(p.fAddressOffset) == 0); ++idx; } else if (address.compareTo(p.fAddressOffset.add(p.fAddressLength)) >= 0) { @@ -291,7 +273,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu * @return the computed index * */ - protected int computeIndexInPositionListLast(List positions, BigInteger address) { + protected int computeIndexInPositionListLast(List positions, BigInteger address) { int size = positions.size(); if (size == 0) { return 0; @@ -301,7 +283,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu int mid = 0; while (low < high) { mid = (low + high) >>> 1; - AddressRangePosition range = positions.get(mid); + AddressRangePosition range = (AddressRangePosition) positions.get(mid); if (address.compareTo(range.fAddressOffset) < 0) { high = mid; } else if (address.compareTo(range.fAddressOffset) == 0) { @@ -314,7 +296,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu } int idx = mid; - AddressRangePosition p = positions.get(idx); + AddressRangePosition p = (AddressRangePosition) positions.get(idx); if (address.compareTo(p.fAddressOffset) > 0) { ++idx; } else if (address.compareTo(p.fAddressOffset) == 0 && p.fAddressLength.compareTo(BigInteger.ZERO) == 0) { @@ -323,7 +305,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu if (idx == size) { break; } - p = positions.get(idx); + p = (AddressRangePosition) positions.get(idx); } while (address.compareTo(p.fAddressOffset) == 0 && p.fAddressLength.compareTo(BigInteger.ZERO) == 0); // --idx; } @@ -340,7 +322,10 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu * @return the computed index * * @see IDocument#computeIndexInCategory(String, int) + * @deprecated Use {@link #computeIndexInPositionListLast(List, BigInteger)} + * as it is for managing lists of AddressRangePositions */ + @Deprecated protected int computeIndexInPositionListLast(List positions, int offset) { if (positions.size() == 0) return 0; @@ -412,13 +397,13 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu } public AddressRangePosition getPositionOfAddress(String category, BigInteger address) { - List positions = getAddressRangePositionsRaw(category); + List positions = getDocumentManagedPositions().get(category); if (positions == null) { return null; } int index = computeIndexInPositionListFirst(positions, address); if (index < positions.size()) { - AddressRangePosition p = positions.get(index); + AddressRangePosition p = (AddressRangePosition) positions.get(index); if (address.compareTo(p.fAddressOffset) == 0 || p.containsAddress(address)) { return p; } @@ -432,7 +417,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu * @return */ public AddressRangePosition getPositionInAddressRange(String category, AddressRangePosition range) { - List positions = getAddressRangePositionsRaw(category); + List positions = getDocumentManagedPositions().get(category); if (positions == null) { return null; } @@ -440,7 +425,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu int index = computeIndexInPositionListFirst(positions, range.fAddressOffset); if (index < positions.size()) { do { - AddressRangePosition p = positions.get(index); + AddressRangePosition p = (AddressRangePosition) positions.get(index); if (p.fAddressOffset.compareTo(endAddress) >= 0) { --index; } else { @@ -692,11 +677,11 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu * @param pos */ public void addModelPositionFirst(AddressRangePosition pos) { - List list = getAddressRangePositionsRaw(CATEGORY_MODEL); + List list = getDocumentManagedPositions().get(CATEGORY_MODEL); int idx; idx = computeIndexInPositionListFirst(list, pos.fAddressOffset.add(pos.fAddressLength)); if (idx < list.size()) { - AddressRangePosition nextPos = list.get(idx); + AddressRangePosition nextPos = (AddressRangePosition) list.get(idx); assert nextPos.fAddressOffset.compareTo(pos.fAddressOffset.add(pos.fAddressLength)) == 0; } list.add(idx, pos); @@ -734,7 +719,10 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu * @throws BadPositionCategoryException */ public void addPositionLast(String category, AddressRangePosition pos) throws BadPositionCategoryException { - List list = getAddressRangePositions(category); + List list = getDocumentManagedPositions().get(category); + if (list == null) { + throw new BadPositionCategoryException(); + } if (DEBUG) System.out.println("Adding position to category <" + category + "> : " + pos); //$NON-NLS-1$ //$NON-NLS-2$ list.add(computeIndexInPositionListLast(list, pos.fAddressOffset), pos); } @@ -848,6 +836,11 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu } } + /** + * @deprecated Use {@link #addPositionLast(String, AddressRangePosition)} + * instead as DissemblyDocument's lists are AddressRangePositions + */ + @Deprecated public void addPositionLast(String category, Position position) throws BadLocationException, BadPositionCategoryException { @@ -892,9 +885,9 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu int delta = (text != null ? text.length() : 0) - replaceLength; if (delta != 0) { BigInteger address = insertPos.fAddressOffset; - Iterator it = getModelPositionIterator(address); + Iterator it = getModelPositionIterator(address); while (it.hasNext()) { - AddressRangePosition pos = it.next(); + AddressRangePosition pos = (AddressRangePosition) it.next(); assert pos.fAddressOffset.compareTo(address) >= 0; if (pos.fAddressOffset.compareTo(address) > 0) { break; @@ -907,7 +900,7 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu } } while (it.hasNext()) { - AddressRangePosition pos = it.next(); + AddressRangePosition pos = (AddressRangePosition) it.next(); pos.offset += delta; } } @@ -944,10 +937,10 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu int replaceLength = 0; if (length.compareTo(BigInteger.ONE) > 0 && !pos.containsAddress(address.add(length.subtract(BigInteger.ONE)))) { // merge with successor positions - Iterator it = getModelPositionIterator(pos.fAddressOffset.add(pos.fAddressLength)); + Iterator it = getModelPositionIterator(pos.fAddressOffset.add(pos.fAddressLength)); assert it.hasNext(); do { - AddressRangePosition overlap = it.next(); + AddressRangePosition overlap = (AddressRangePosition) it.next(); BigInteger posEndAddress= pos.fAddressOffset.add(pos.fAddressLength); assert pos.offset <= overlap.offset && overlap.fAddressOffset.compareTo(posEndAddress) == 0; if (overlap instanceof LabelPosition || overlap instanceof SourcePosition) { @@ -1312,9 +1305,9 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu int replaceLen = replacement != null ? replacement.length() : 0; AddressRangePosition lastPos = null; ArrayList toRemove = new ArrayList(); - Iterator it = getModelPositionIterator(startAddress); + Iterator it = getModelPositionIterator(startAddress); while (it.hasNext()) { - AddressRangePosition pos = it.next(); + AddressRangePosition pos = (AddressRangePosition) it.next(); BigInteger posEndAddress = pos.fAddressOffset.add(pos.fAddressLength); if (pos instanceof LabelPosition) { if (!invalidate && pos.length > 0 && posEndAddress.compareTo(endAddress) > 0) { @@ -1455,9 +1448,9 @@ public class DisassemblyDocument extends REDDocument implements IDisassemblyDocu BigInteger addressLength = BigInteger.ZERO; ArrayList toRemove = new ArrayList(); try { - Iterator it = getPositionIterator(DisassemblyDocument.CATEGORY_MODEL, startAddress); + Iterator it = getPositionIterator(DisassemblyDocument.CATEGORY_MODEL, startAddress); while (it.hasNext()) { - AddressRangePosition p = it.next(); + AddressRangePosition p = (AddressRangePosition) it.next(); addressLength = addressLength.add(p.fAddressLength); replaceLength += p.length; toRemove.add(p);