Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3292 - Decompiler - Secondary Highlights - Checkpoint 2 - Base features implemented with tests; still need color persistence; help

dragonmacher19823c5db5c5
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractProgramBasedTest.java b/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractProgramBasedTest.java
index 8c267a3a36..230ad1ff3b 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractProgramBasedTest.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/test/AbstractProgramBasedTest.java
@@ -15,8 +15,7 @@
*/
package ghidra.test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
@@ -78,7 +77,6 @@ public abstract class AbstractProgramBasedTest extends AbstractGhidraHeadedInteg
@After
public void tearDown() throws Exception {
- env.release(program);
env.dispose();
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/CDisplayPanel.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/CDisplayPanel.java
index d9f9be66de..940158a67a 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/CDisplayPanel.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/CDisplayPanel.java
@@ -29,6 +29,7 @@ import ghidra.program.model.listing.Program;
import ghidra.program.util.ProgramLocation;
import ghidra.program.util.ProgramSelection;
import ghidra.util.bean.field.AnnotatedTextFieldElement;
+import utility.function.Callback;
public class CDisplayPanel extends JPanel implements DecompilerCallbackHandler {
@@ -157,6 +158,11 @@ public class CDisplayPanel extends JPanel implements DecompilerCallbackHandler {
// stub
}
+ @Override
+ public void doWheNotBusy(Callback c) {
+ // stub
+ }
+
public void clearAndShowMessage(String message) {
controller.setDecompileData(new EmptyDecompileData(message));
paintImmediately(getBounds());
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangFieldElement.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangFieldElement.java
index 920475991c..a0430cce7d 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangFieldElement.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangFieldElement.java
@@ -15,8 +15,7 @@
*/
package ghidra.app.decompiler.component;
-import java.awt.Color;
-import java.awt.Graphics;
+import java.awt.*;
import javax.swing.JComponent;
@@ -38,9 +37,9 @@ public class ClangFieldElement extends AbstractTextFieldElement {
@Override
public void paint(JComponent c, Graphics g, int x, int y) {
- Color highlightColor = token.getHighlight();
+ Paint highlightColor = token.getHighlight();
if (highlightColor != null) {
- g.setColor(highlightColor);
+ ((Graphics2D) g).setPaint(highlightColor);
g.fillRect(x, y - getHeightAbove(), getStringWidth(),
getHeightAbove() + getHeightBelow());
}
@@ -49,7 +48,7 @@ public class ClangFieldElement extends AbstractTextFieldElement {
if (token.isMatchingToken()) {
// paint a bounding box around the token
- g.setColor(Color.GRAY);
+ ((Graphics2D) g).setPaint(Color.GRAY);
int offset = 1;
g.drawRect(x - offset, y - getHeightAbove() - offset, getStringWidth() + (offset * 2),
getHeightAbove() + getHeightBelow() + (offset * 2));
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangHighlightController.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangHighlightController.java
index 2314a6360f..d5b1bcd1ea 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangHighlightController.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangHighlightController.java
@@ -20,20 +20,26 @@ import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
-import org.apache.commons.collections4.IterableUtils;
-
import docking.widgets.EventTrigger;
import docking.widgets.fieldpanel.field.Field;
import docking.widgets.fieldpanel.support.FieldLocation;
import ghidra.app.decompiler.*;
import ghidra.app.plugin.core.decompile.actions.TokenHighlightColorProvider;
import ghidra.program.model.pcode.PcodeOp;
+import ghidra.util.ColorUtils;
+import util.CollectionUtils;
/**
- * Class to handle highlights for a decompiled function
+ * Class to handle highlights for a decompiled function.
+ *
+ * <p>This class does not painting directly. Rather, this class tracks the currently highlighted
+ * tokens and then sets the highlight color on the token when it is highlighted and clears the
+ * highlight color when the highlight is removed.
*/
public abstract class ClangHighlightController {
+ public static Color DEFAULT_HIGHLIGHT_COLOR = new Color(255, 255, 0, 128);
+
public static ClangHighlightController dummyIfNull(ClangHighlightController c) {
if (c == null) {
return new NullClangHighlightController();
@@ -44,49 +50,22 @@ public abstract class ClangHighlightController {
// Note: Most of the methods in this class were extracted from the ClangLayoutController class
// and the DecompilerPanel class.
- protected Color defaultHighlightColor = new Color(255, 255, 0, 128); // Default color for highlighting tokens
- protected Color defaultSpecialColor = new Color(255, 100, 0, 128); // Default color for specially highlighted tokens
- protected Color defaultParenColor = new Color(255, 255, 0, 128); // Default color for highlighting parentheses
+ protected Color defaultHighlightColor = DEFAULT_HIGHLIGHT_COLOR;
+ protected Color defaultParenColor = DEFAULT_HIGHLIGHT_COLOR;
+
+ private TokenHighlights primaryHighlightTokens = new TokenHighlights();
+ private TokenHighlights secondaryHighlightTokens = new TokenHighlights();
+
+ // TODO rename 'secondary highlight color manager'
+ private TokenHighlightColors secondaryHighlightColors = new TokenHighlightColors();
- private Set<ClangToken> primaryHighlightTokens = new HashSet<>();
- private Set<ClangToken> secondaryHighlightTokens = new HashSet<>();
private List<ClangHighlightListener> listeners = new ArrayList<>();
public abstract void fieldLocationChanged(FieldLocation location, Field field,
EventTrigger trigger);
- void loadOptions(DecompileOptions options) {
- Color currentVariableHighlightColor = options.getCurrentVariableHighlightColor();
- if (currentVariableHighlightColor != null) {
- setDefaultHighlightColor(currentVariableHighlightColor);
- }
- }
-
- public void setDefaultHighlightColor(Color highlightColor) {
- defaultHighlightColor = highlightColor;
- notifyListeners();
- }
-
- public void setDefaultSpecialColor(Color specialColor) {
- defaultSpecialColor = specialColor;
- notifyListeners();
- }
-
- public void setDefaultParenColor(Color parenColor) {
- defaultParenColor = parenColor;
- notifyListeners();
- }
-
- public Color getDefaultHighlightColor() {
- return defaultHighlightColor;
- }
-
- public Color getDefaultSpecialColor() {
- return defaultSpecialColor;
- }
-
- public Color getDefaultParenColor() {
- return defaultParenColor;
+ void setHighlightColor(Color c) {
+ defaultHighlightColor = c;
}
public String getHighlightedText() {
@@ -97,15 +76,18 @@ public abstract class ClangHighlightController {
return null;
}
+ public TokenHighlights getSecondaryHighlightedTokens() {
+ return secondaryHighlightTokens;
+ }
+
/**
* Return the current highlighted token (if exists and unique)
* @return token or null
*/
private ClangToken getHighlightedToken() {
if (primaryHighlightTokens.size() == 1) {
- ClangToken[] tokenArray =
- primaryHighlightTokens.toArray(new ClangToken[primaryHighlightTokens.size()]);
- return tokenArray[0];
+ HighlightToken hlToken = CollectionUtils.any(primaryHighlightTokens);
+ return hlToken.getToken();
}
return null;
}
@@ -126,38 +108,87 @@ public abstract class ClangHighlightController {
public void clearPrimaryHighlights() {
doClearHighlights(primaryHighlightTokens);
+ notifyListeners();
+ }
+
+ public void clearSecondaryHighlights() {
+ doClearHighlights(secondaryHighlightTokens);
+ notifyListeners();
}
public void clearAllHighlights() {
- Iterable<ClangToken> allTokens =
- IterableUtils.chainedIterable(primaryHighlightTokens, secondaryHighlightTokens);
- for (ClangToken clangToken : allTokens) {
- clangToken.setHighlight(null);
- clangToken.setMatchingToken(false);
+ doClearHighlights(primaryHighlightTokens);
+ doClearHighlights(secondaryHighlightTokens);
+ notifyListeners();
+ }
+
+ // TODO try to reconcile 'tokenhighlights' vs 'highlighttokens' ( and highlighttoken)
+ private void doClearHighlights(TokenHighlights highlightTokens) {
+
+ Iterator<HighlightToken> it = highlightTokens.iterator();
+ while (it.hasNext()) {
+ HighlightToken highlight = it.next();
+ it.remove();
+ ClangToken token = highlight.getToken();
+ token.setMatchingToken(false);
+ updateHighlightColor(token);
}
+ highlightTokens.clear();
+ }
- primaryHighlightTokens.clear();
- secondaryHighlightTokens.clear();
- notifyListeners();
+ public void toggleSecondaryHighlight(ClangToken token,
+ Supplier<? extends Collection<ClangToken>> lazyTokens) {
+
+ String text = token.getText();
+
+ // toggle the highlight
+ HighlightToken highlight = secondaryHighlightTokens.get(token);
+ if (highlight == null) {
+ Color highlightColor = secondaryHighlightColors.getColor(text);
+ addSecondaryHighlights(lazyTokens, highlightColor);
+ }
+ else {
+ removeSecondaryHighlights(lazyTokens);
+ }
}
- private void doClearHighlights(Set<ClangToken> highlightTokens) {
+ public boolean hasSecondaryHighlight(ClangToken token) {
+ return secondaryHighlightTokens.contains(token);
+ }
- for (ClangToken clangToken : highlightTokens) {
- clangToken.setHighlight(null);
- clangToken.setMatchingToken(false);
+ public void removeSecondaryHighlights(ghidra.program.model.listing.Function function) {
+ Set<HighlightToken> oldHighlights =
+ secondaryHighlightTokens.removeTokensByFunction(function);
+ for (HighlightToken hl : oldHighlights) {
+ ClangToken token = hl.getToken();
+ updateHighlightColor(token);
}
+ notifyListeners();
+ }
- highlightTokens.clear();
+ public HighlightToken removeSecondaryHighlight(ClangToken token) {
+ HighlightToken hlToken = secondaryHighlightTokens.get(token);
+ secondaryHighlightTokens.remove(token);
+ return hlToken;
+ }
+
+ private void removeSecondaryHighlights(Supplier<? extends Collection<ClangToken>> tokens) {
+ for (ClangToken clangToken : tokens.get()) {
+ secondaryHighlightTokens.remove(clangToken);
+ updateHighlightColor(clangToken);
+ }
notifyListeners();
}
- public void addPrimaryHighlight() {
- // TODO
+ public void addSecondaryHighlights(Supplier<? extends Collection<ClangToken>> tokens,
+ Color hlColor) {
+ Function<ClangToken, Color> colorProvider = token -> hlColor;
+ addTokensToHighlights(tokens.get(), colorProvider, secondaryHighlightTokens);
}
- public void addSecondaryHighlight() {
- // TODO
+ @Deprecated // for use by subclasses
+ protected void addHighlight(ClangToken tok, Color hlColor) {
+ addPrimaryHighlight(tok, hlColor);
}
public void addPrimaryHighlights(Supplier<? extends Collection<ClangToken>> tokens,
@@ -171,7 +202,7 @@ public abstract class ClangHighlightController {
Set<ClangToken> tokens = new HashSet<>();
gatherAllTokens(parentNode, tokens);
- addTokensToHighlights(tokens, colorProvider::getColor, tokens);
+ addTokensToHighlights(tokens, colorProvider::getColor, primaryHighlightTokens);
}
public void addPrimaryHighlights(ClangNode parentNode, Set<PcodeOp> ops, Color hlColor) {
@@ -188,7 +219,7 @@ public abstract class ClangHighlightController {
}
private void addTokensToHighlights(Collection<ClangToken> tokens,
- Function<ClangToken, Color> colorProvider, Set<ClangToken> currentHighlights) {
+ Function<ClangToken, Color> colorProvider, TokenHighlights currentHighlights) {
for (ClangToken clangToken : tokens) {
Color color = colorProvider.apply(clangToken);
doAddHighlight(clangToken, color, currentHighlights);
@@ -201,20 +232,42 @@ public abstract class ClangHighlightController {
}
private void doAddHighlight(ClangToken clangToken, Color highlightColor,
- Set<ClangToken> currentHighlights) {
+ TokenHighlights currentHighlights) {
if (highlightColor == null) {
return;
}
- clangToken.setHighlight(highlightColor);
- currentHighlights.add(clangToken);
+ // store the actual requested color
+ currentHighlights.add(new HighlightToken(clangToken, highlightColor));
+ updateHighlightColor(clangToken);
}
- public void clearHighlight(ClangToken clangToken) {
- clangToken.setHighlight(null);
- primaryHighlightTokens.remove(clangToken);
- notifyListeners();
+ private void updateHighlightColor(ClangToken t) {
+ // set the color to the current combined value of both highlight types
+ Color combinedColor = getCombinedColor(t);
+ t.setHighlight(combinedColor);
+ }
+
+ public Color getCombinedColor(ClangToken t) {
+
+ HighlightToken primaryHl = primaryHighlightTokens.get(t);
+ HighlightToken secondaryHl = secondaryHighlightTokens.get(t);
+ Color primary = primaryHl == null ? null : primaryHl.getColor();
+ Color secondary = secondaryHl == null ? null : secondaryHl.getColor();
+
+ if (primary == null) {
+ if (secondary == null) {
+ return null;
+ }
+ return secondary;
+ }
+
+ if (secondary == null) {
+ return primary;
+ }
+
+ return ColorUtils.blend(primary, secondary, .8f);
}
/**
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerCallbackHandler.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerCallbackHandler.java
index 9432f5b8fe..0a739f83a9 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerCallbackHandler.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerCallbackHandler.java
@@ -20,6 +20,7 @@ import ghidra.program.model.listing.Function;
import ghidra.program.util.ProgramLocation;
import ghidra.program.util.ProgramSelection;
import ghidra.util.bean.field.AnnotatedTextFieldElement;
+import utility.function.Callback;
public interface DecompilerCallbackHandler {
@@ -44,4 +45,6 @@ public interface DecompilerCallbackHandler {
void exportLocation();
void goToFunction(Function function, boolean newWindow);
+
+ void doWheNotBusy(Callback c);
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerController.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerController.java
index 97c9c2cfce..43a52b3da4 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerController.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerController.java
@@ -30,6 +30,7 @@ import ghidra.program.model.pcode.HighFunction;
import ghidra.program.util.ProgramLocation;
import ghidra.program.util.ProgramSelection;
import ghidra.util.bean.field.AnnotatedTextFieldElement;
+import utility.function.Callback;
/**
* Coordinates the interactions between the DecompilerProvider, DecompilerPanel, and the DecompilerManager
@@ -88,11 +89,12 @@ public class DecompilerController {
* Shows the function containing the given location in the decompilerPanel. Also, positions the
* decompilerPanel's cursor to the closest equivalent position. If the decompilerPanel is
* already displaying the function, then only the cursor is repositioned. To force a
- * re-decompile use {@link #refreshDisplay(Program, ProgramLocation)}.
+ * re-decompile use {@link #refreshDisplay(Program, ProgramLocation, File)}.
*
* @param program the program for the given location
* @param location the location containing the function to be displayed and the location in
* that function to position the cursor.
+ * @param viewerPosition the viewer position
*/
public void display(Program program, ProgramLocation location, ViewerPosition viewerPosition) {
if (!decompilerMgr.isBusy() && decompilerPanel.containsLocation(location)) {
@@ -135,7 +137,7 @@ public class DecompilerController {
/**
* Sets new decompiler options and triggers a new decompile.
- * @param decompilerOptions
+ * @param decompilerOptions the options
*/
public void setOptions(DecompileOptions decompilerOptions) {
clearCache();
@@ -168,7 +170,8 @@ public class DecompilerController {
//==================================================================================================
/**
- * Called by the DecompilerManager to update the currently displayed DecompileData.
+ * Called by the DecompilerManager to update the currently displayed DecompileData
+ * @param decompileData the new data
*/
public void setDecompileData(DecompileData decompileData) {
updateCache(decompileData);
@@ -193,12 +196,18 @@ public class DecompilerController {
//==================================================================================================
// Methods called by actions and other miscellaneous classes
//==================================================================================================
+
+ public void doWhenNotBusy(Callback c) {
+ callbackHandler.doWheNotBusy(c);
+ }
+
/**
* Always decompiles the function containing the given location before positioning the
* decompilerPanel's cursor to the closest equivalent position.
* @param program the program for the given location
* @param location the location containing the function to be displayed and the location in
* that function to position the cursor.
+ * @param debugFile the debug file
*/
public void refreshDisplay(Program program, ProgramLocation location, File debugFile) {
clearCache();
@@ -304,5 +313,4 @@ public class DecompilerController {
}
}
}
-
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerPanel.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerPanel.java
index 7d3a873d3a..08b50619fd 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerPanel.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerPanel.java
@@ -50,6 +50,7 @@ import ghidra.program.util.ProgramLocation;
import ghidra.program.util.ProgramSelection;
import ghidra.util.*;
import ghidra.util.bean.field.AnnotatedTextFieldElement;
+import ghidra.util.task.SwingUpdateManager;
/**
* Class to handle the display of a decompiled function
@@ -67,14 +68,22 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
private DecompilerFieldPanel fieldPanel;
private ClangLayoutController layoutMgr;
+
private HighlightFactory hlFactory;
private ClangHighlightController highlightController;
-
- private int currentMiddleMouseHighlightButton;
- private Color currentSearchHighlightColor;
- private Color currentHighlightColor;
+ private PendingHighlightUpdate pendingHighlightUpdate;
+ private SwingUpdateManager highlighCursorUpdater = new SwingUpdateManager(() -> {
+ if (pendingHighlightUpdate != null) {
+ pendingHighlightUpdate.doUpdate();
+ pendingHighlightUpdate = null;
+ }
+ });
+
+ private int middleMouseHighlightButton;
+ private Color middleMouseHighlightColor;
+ private Color currentVariableHighlightColor;
+ private Color searchHighlightColor;
private SearchLocation currentSearchLocation;
- private TokenHighlights secondaryHighlights = new TokenHighlights(this);
private DecompileData decompileData = new EmptyDecompileData("No Function");
private final DecompilerClipboardProvider clipboard;
@@ -107,9 +116,10 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
decompilerHoverProvider = new DecompilerHoverProvider();
- currentSearchHighlightColor = options.getSearchHighlightColor();
- currentHighlightColor = options.getMiddleMouseHighlightColor();
- currentMiddleMouseHighlightButton = options.getMiddleMouseHighlightButton();
+ searchHighlightColor = options.getSearchHighlightColor();
+ currentVariableHighlightColor = options.getCurrentVariableHighlightColor();
+ middleMouseHighlightColor = options.getMiddleMouseHighlightColor();
+ middleMouseHighlightButton = options.getMiddleMouseHighlightButton();
setLayout(new BorderLayout());
add(scroller);
@@ -119,10 +129,6 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
setDecompileData(new EmptyDecompileData("No Function"));
}
- public TokenHighlights getHighlightedTokens() {
- return secondaryHighlights;
- }
-
public List<ClangLine> getLines() {
return layoutMgr.getLines();
}
@@ -140,7 +146,24 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
* @param newTokens the new data
*/
public void setHighlightedTokens(TokenHighlights newTokens) {
- secondaryHighlights = newTokens;
+
+ // TODO pass these in or copy ourselves?
+ //highlightController.setSecondaryHighlightedTokens(newTokens);
+
+ throw new RuntimeException("Fixme");
+ }
+
+ public TokenHighlights getSecondaryHighlightedTokens() {
+ return highlightController.getSecondaryHighlightedTokens();
+ }
+
+ public void removeSecondaryHighlights() {
+ Function function = controller.getFunction();
+ highlightController.removeSecondaryHighlights(function);
+ }
+
+ public void toggleSecondaryHighlight(ClangToken token, Supplier<List<ClangToken>> lazyTokens) {
+ highlightController.toggleSecondaryHighlight(token, lazyTokens);
}
@Override
@@ -439,9 +462,9 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
public void dispose() {
setDecompileData(new EmptyDecompileData("Disposed"));
layoutMgr = null;
-
decompilerHoverProvider.dispose();
- secondaryHighlights.clear();
+ highlighCursorUpdater.dispose();
+ highlightController.clearAllHighlights();
}
private FontMetrics getFontMetrics(DecompileOptions decompileOptions) {
@@ -479,8 +502,8 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
}
}
- if (buttonState == currentMiddleMouseHighlightButton && clickCount == 1) {
- setPrimaryHighlight(location, field, currentHighlightColor);
+ if (buttonState == middleMouseHighlightButton && clickCount == 1) {
+ setPrimaryHighlight(location, field, middleMouseHighlightColor);
}
}
@@ -648,28 +671,6 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
repaint();
}
- /**
- * This function is used to add highlighting to a token. The function behaves differently
- * from highlightVariable in that it does not erase existing highlights.
- *
- * @param highlight the new highlight
- */
- private void addSecondaryHighlight(HighlightToken highlight) {
- ClangToken token = highlight.getToken();
- Supplier<List<ClangToken>> lazyTokens = () -> findTokensByName(token.getText());
- highlightController.addPrimaryHighlights(lazyTokens, highlight.getColor());
- repaint();
- }
-
- public void rebuildSecondaryHighlightTokens() {
-
- Function function = decompileData.getFunction();
- Collection<HighlightToken> highlights = secondaryHighlights.get(function);
- highlights.forEach(this::addSecondaryHighlight);
-
- repaint();
- }
-
Program getProgram() {
return decompileData.getProgram();
}
@@ -689,7 +690,8 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
return;
}
- highlightController.fieldLocationChanged(location, field, trigger);
+ pendingHighlightUpdate = new PendingHighlightUpdate(location, field, trigger);
+ highlighCursorUpdater.update();
if (!(field instanceof ClangTextField)) {
return;
@@ -776,8 +778,12 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
// End Search Methods
//==================================================================================================
- public Color getPrimaryHighlightColor() {
- return currentHighlightColor;
+ public Color getCurrentVariableHighlightColor() {
+ return currentVariableHighlightColor;
+ }
+
+ public Color getMiddleMouseHighlightColor() {
+ return middleMouseHighlightColor;
}
/**
@@ -832,6 +838,10 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
return ((ClangTextField) field).getToken(cursorPosition);
}
+ public DecompileOptions getOptions() {
+ return options;
+ }
+
public void addHoverService(DecompilerHoverService hoverService) {
decompilerHoverProvider.addHoverService(hoverService);
}
@@ -854,17 +864,6 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
return decompilerHoverProvider.isShowing();
}
- // TODO delete and replace with clearStandardHiglights();
- public void clearHighlights() {
- clearAllHighlights();
- }
-
- public void clearAllHighlights() {
- // TODO
- highlightController.clearAllHighlights();
- repaint();
- }
-
public void clearPrimaryHighlights() {
highlightController.clearPrimaryHighlights();
}
@@ -924,16 +923,19 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
FieldSelection selection = new FieldSelection();
selection.addRange(BigInteger.ZERO, numIndexes);
fieldPanel.setSelection(selection);
+
// fake it out that the selection was caused by the field panel GUI.
selectionChanged(selection, EventTrigger.GUI_ACTION);
}
public void optionsChanged(DecompileOptions decompilerOptions) {
setBackground(decompilerOptions.getCodeViewerBackgroundColor());
- currentHighlightColor = decompilerOptions.getMiddleMouseHighlightColor();
- currentMiddleMouseHighlightButton = decompilerOptions.getMiddleMouseHighlightButton();
- currentSearchHighlightColor = decompilerOptions.getSearchHighlightColor();
- highlightController.loadOptions(decompilerOptions);
+ currentVariableHighlightColor = options.getCurrentVariableHighlightColor();
+ middleMouseHighlightColor = decompilerOptions.getMiddleMouseHighlightColor();
+ middleMouseHighlightButton = decompilerOptions.getMiddleMouseHighlightButton();
+ searchHighlightColor = decompilerOptions.getSearchHighlightColor();
+
+ highlightController.setHighlightColor(currentVariableHighlightColor);
}
public void setHighlightController(ClangHighlightController highlightController) {
@@ -942,7 +944,7 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
}
this.highlightController = ClangHighlightController.dummyIfNull(highlightController);
- highlightController.loadOptions(options);
+ highlightController.setHighlightColor(currentVariableHighlightColor);
highlightController.addListener(this);
}
@@ -951,38 +953,33 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
repaint();
}
- public void tokenHighlightsAdded(HighlightToken highlight) {
- addSecondaryHighlight(highlight);
- }
-
- public void tokenHighlightsRemoved(HighlightToken highlight) {
- highlightController.clearHighlight(highlight.getToken());
- repaint();
- }
-
/**
* This is function is used to alert the panel that a token was renamed.
- * If the token that is being renamed had a special highlight, we must re-apply the highlight
+ * If the token that is being renamed had a secondary highlight, we must re-apply the highlight
* to the new token.
*
* @param token the token being renamed
* @param name the new name of the token
*/
public void tokenRenamed(ClangToken token, String name) {
- HighlightToken oldHighlight = secondaryHighlights.get(token);
- if (oldHighlight == null) {
+
+ if (!highlightController.hasSecondaryHighlight(token)) {
return;
}
- secondaryHighlights.remove(oldHighlight);
- ClangToken ct = new ClangToken(token, name);
+ controller.doWhenNotBusy(() -> {
- // TODO replace this with a 'create' method?
- secondaryHighlights.add(new HighlightToken(ct, oldHighlight.getColor()));
- Supplier<List<ClangToken>> lazyTokens = () -> findTokensByName(name);
- Color color = getPrimaryHighlightColor();
- highlightController.addPrimaryHighlights(lazyTokens, color);
- repaint();
+ HighlightToken hlToken = highlightController.removeSecondaryHighlight(token);
+
+ Supplier<List<ClangToken>> lazyTokens = () -> findTokensByName(name);
+ Color color = hlToken.getColor();
+ highlightController.addSecondaryHighlights(lazyTokens, color);
+ repaint();
+ });
+ }
+
+ public ClangHighlightController getHighlightController() {
+ return highlightController;
}
//==================================================================================================
@@ -1009,7 +1006,7 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
}
return new Highlight[] { new Highlight(currentSearchLocation.getStartIndexInclusive(),
- currentSearchLocation.getEndIndexInclusive(), currentSearchHighlightColor) };
+ currentSearchLocation.getEndIndexInclusive(), searchHighlightColor) };
}
}
@@ -1083,4 +1080,25 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
EventTrigger.GUI_ACTION);
}
}
+
+ /**
+ * A class to track pending location updates. This allows us to buffer updates, only sending
+ * the last one received.
+ */
+ private class PendingHighlightUpdate {
+
+ private FieldLocation location;
+ private Field field;
+ private EventTrigger trigger;
+
+ PendingHighlightUpdate(FieldLocation location, Field field, EventTrigger trigger) {
+ this.location = location;
+ this.field = field;
+ this.trigger = trigger;
+ }
+
+ void doUpdate() {
+ highlightController.fieldLocationChanged(location, field, trigger);
+ }
+ }
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/LocationClangHighlightController.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/LocationClangHighlightController.java
index 4a07b38fb7..35a4a88d2d 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/LocationClangHighlightController.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/LocationClangHighlightController.java
@@ -40,9 +40,6 @@ public class LocationClangHighlightController extends ClangHighlightController {
return;
}
- // TODO revisit cursor location highlight
- // TODO it should probably darken the color
-
addPrimaryHighlight(tok, defaultHighlightColor);
if (tok instanceof ClangSyntaxToken) {
addPrimaryHighlightToTokensForParenthesis((ClangSyntaxToken) tok, defaultParenColor);
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/NullClangHighlightController.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/NullClangHighlightController.java
index 2fb8886836..af6952e965 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/NullClangHighlightController.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/NullClangHighlightController.java
@@ -27,6 +27,9 @@ import ghidra.app.decompiler.*;
import ghidra.app.plugin.core.decompile.actions.TokenHighlightColorProvider;
import ghidra.program.model.pcode.PcodeOp;
+/**
+ * A stub implementation of the highlight controller that allows clients to avoid null checks
+ */
public class NullClangHighlightController extends ClangHighlightController {
@Override
@@ -34,41 +37,6 @@ public class NullClangHighlightController extends ClangHighlightController {
// stub
}
- @Override
- void loadOptions(DecompileOptions options) {
- // stub
- }
-
- @Override
- public void setDefaultHighlightColor(Color highlightColor) {
- // stub
- }
-
- @Override
- public void setDefaultSpecialColor(Color specialColor) {
- // stub
- }
-
- @Override
- public void setDefaultParenColor(Color parenColor) {
- // stub
- }
-
- @Override
- public Color getDefaultHighlightColor() {
- return null;
- }
-
- @Override
- public Color getDefaultSpecialColor() {
- return null;
- }
-
- @Override
- public Color getDefaultParenColor() {
- return null;
- }
-
@Override
public String getHighlightedText() {
return null;
@@ -96,11 +64,6 @@ public class NullClangHighlightController extends ClangHighlightController {
// stub
}
- @Override
- public void clearHighlight(ClangToken clangToken) {
- // stub
- }
-
@Override
public void addHighlightBrace(ClangSyntaxToken token, Color highlightColor) {
// stub
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenHighlightColors.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenHighlightColors.java
new file mode 100644
index 0000000000..2535562995
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenHighlightColors.java
@@ -0,0 +1,42 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.app.decompiler.component;
+
+import java.awt.Color;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.collections4.map.LazyMap;
+
+// TODO docme
+public class TokenHighlightColors {
+
+ private int minColorSaturation = 100;
+ private int defaultColorAlpha = 100;
+ private Map<String, Color> colorsByName =
+ LazyMap.lazyMap(new HashMap<>(), s -> generateColor());
+
+ private Color generateColor() {
+ return new Color((int) (minColorSaturation + Math.random() * (256 - minColorSaturation)),
+ (int) (minColorSaturation + Math.random() * (256 - minColorSaturation)),
+ (int) (minColorSaturation + Math.random() * (256 - minColorSaturation)),
+ defaultColorAlpha);
+ }
+
+ public Color getColor(String text) {
+ return colorsByName.get(text);
+ }
+}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenHighlights.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenHighlights.java
index f49b77e0f4..a315ee2a2d 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenHighlights.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenHighlights.java
@@ -15,51 +15,33 @@
*/
package ghidra.app.decompiler.component;
-import java.awt.Color;
import java.util.*;
-
-import org.apache.commons.collections4.map.LazyMap;
+import java.util.Map.Entry;
import ghidra.app.decompiler.*;
import ghidra.program.model.listing.Function;
import ghidra.program.model.pcode.HighFunction;
-// TODO revisit having the panel's live here... we need this if clients interact with this
-// object directly. If they go through the panel, then we do not need them here
-public class TokenHighlights {
-
- private int minColorSaturation = 100;
- private int defaultColorAlpha = 100;
-
- // TODO private HighlightColorSource
-
- private DecompilerPanel panel;
- // TODO private Map<TokenKey, HighlightToken> highlightsByToken = new HashMap<>();
-
- private Map<TokenKey, HighlightToken> nullMap = Collections.unmodifiableMap(new HashMap<>());
- private Map<Function, Map<TokenKey, HighlightToken>> highlightsByFunction =
- LazyMap.lazyMap(new HashMap<>(), f -> f == null ? nullMap : new HashMap<>());
-
- public TokenHighlights(DecompilerPanel panel) {
- this.panel = panel;
- }
-
- public TokenHighlights copyHighlights(DecompilerPanel otherPanel, Function function) {
+/**
+ * A simple class to manage {@link HighlightToken}s used to create secondary highlights in the
+ * Decompiler
+ */
+public class TokenHighlights implements Iterable<HighlightToken> {
- TokenHighlights newHighlights = new TokenHighlights(otherPanel);
+ private Map<TokenKey, HighlightToken> highlightsByToken = new HashMap<>();
- Map<TokenKey, HighlightToken> highlightsByToken = highlightsByFunction.get(function);
- Map<TokenKey, HighlightToken> newHighlightsByToken = new HashMap<>(highlightsByToken);
- newHighlights.highlightsByFunction.put(function, newHighlightsByToken);
+ public TokenHighlights copyHighlights() {
+ TokenHighlights newHighlights = new TokenHighlights();
+ newHighlights.highlightsByToken = new HashMap<>(highlightsByToken);
return newHighlights;
}
- private Map<TokenKey, HighlightToken> getHighlightsByToken(HighlightToken t) {
- return highlightsByFunction.get(getFunction(t.getToken()));
+ private TokenKey getKey(HighlightToken ht) {
+ return new TokenKey(ht);
}
- private Map<TokenKey, HighlightToken> getHighlightsByToken(ClangToken t) {
- return highlightsByFunction.get(getFunction(t));
+ private TokenKey getKey(ClangToken t) {
+ return new TokenKey(t);
}
private Function getFunction(ClangToken t) {
@@ -78,68 +60,81 @@ public class TokenHighlights {
return highFunction.getFunction();
}
+ public int size() {
+ return highlightsByToken.size();
+ }
+
public void add(HighlightToken t) {
- Function f = t.getFunction();
- Map<TokenKey, HighlightToken> highlightsByToken = highlightsByFunction.get(f);
- highlightsByToken.put(new TokenKey(t), t);
- notifyListeners();
+ highlightsByToken.put(getKey(t), t);
}
public HighlightToken get(ClangToken t) {
- Map<TokenKey, HighlightToken> highlightsByToken = getHighlightsByToken(t);
- TokenKey key = new TokenKey(t);
- return highlightsByToken.get(key);
+ return highlightsByToken.get(getKey(t));
}
- public Collection<HighlightToken> get(Function f) {
- return highlightsByFunction.get(f).values();
+ public boolean contains(ClangToken t) {
+ return highlightsByToken.containsKey(getKey(t));
}
public void clear() {
- highlightsByFunction.clear();
- notifyListeners();
+ highlightsByToken.clear();
}
public boolean contains(HighlightToken t) {
- TokenKey key = new TokenKey(t);
- Map<TokenKey, HighlightToken> highlightsByToken = getHighlightsByToken(t);
- return highlightsByToken.containsKey(key);
+ return highlightsByToken.containsKey(getKey(t));
}
+ // TODO examine this method and others for removal
public void remove(HighlightToken t) {
- TokenKey key = new TokenKey(t);
- Map<TokenKey, HighlightToken> highlightsByToken = getHighlightsByToken(t);
- highlightsByToken.remove(key);
- notifyListeners();
+ highlightsByToken.remove(getKey(t));
+ }
+
+ public void remove(ClangToken t) {
+ highlightsByToken.remove(getKey(t));
}
- public void remove(Function function) {
- highlightsByFunction.remove(function);
- notifyListeners();
+ public Set<HighlightToken> removeTokensByFunction(Function function) {
+ Set<HighlightToken> oldHighlights = new HashSet<>();
+ Set<TokenKey> keys = getHighlightKeys(function);
+ for (TokenKey key : keys) {
+ HighlightToken hl = highlightsByToken.remove(key);
+ oldHighlights.add(hl);
+ }
+
+ return oldHighlights;
}
- // TODO remove this
- public void notifyListeners() {
+ private Set<TokenKey> getHighlightKeys(Function function) {
+ Set<TokenKey> results = new HashSet<>();
- //
- // TODO to remove this, the clients that change this class would have to forcibly rebuild the highlights
- // -it seems simpler to have one place manager notifications
- // -OTOH, it seems wasteful to completely rebuild all secondary highlights when adding
- // or removing a single one
- //
+ Set<Entry<TokenKey, HighlightToken>> entries = highlightsByToken.entrySet();
+ for (Entry<TokenKey, HighlightToken> entry : entries) {
+ HighlightToken highlight = entry.getValue();
+ ClangToken token = highlight.getToken();
+ Function tokenFunction = getFunction(token);
+ if (function.equals(tokenFunction)) {
+ results.add(entry.getKey());
+ }
+ }
- panel.rebuildSecondaryHighlightTokens();
+ return results;
+ }
+
+ @Override
+ public Iterator<HighlightToken> iterator() {
+ return highlightsByToken.values().iterator();
}
@Override
public String toString() {
- return highlightsByFunction.values().toString();
+ return highlightsByToken.values().toString();
}
//==================================================================================================
// Inner Classes
//==================================================================================================
+ // a key that allows us to equate tokens that are not the same instance
private class TokenKey {
private ClangToken token;
@@ -210,20 +205,4 @@ public class TokenHighlights {
return token.toString();
}
}
-
- // TODO move up; clean up
-
- private Map<String, Color> colorsByName =
- LazyMap.lazyMap(new HashMap<>(), s -> generateColor());
-
- private Color generateColor() {
- return new Color((int) (minColorSaturation + Math.random() * (256 - minColorSaturation)),
- (int) (minColorSaturation + Math.random() * (256 - minColorSaturation)),
- (int) (minColorSaturation + Math.random() * (256 - minColorSaturation)),
- defaultColorAlpha);
- }
-
- public Color getColor(String text) {
- return colorsByName.get(text);
- }
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java
index 16dd370acb..d6d021d010 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java
@@ -18,8 +18,8 @@ package ghidra.app.plugin.core.decompile;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.math.BigInteger;
-import java.util.Arrays;
-import java.util.List;
+import java.util.*;
+import java.util.concurrent.ConcurrentLinkedQueue;
import javax.swing.*;
@@ -50,6 +50,7 @@ import ghidra.util.bean.field.AnnotatedTextFieldElement;
import ghidra.util.task.SwingUpdateManager;
import resources.Icons;
import resources.ResourceManager;
+import utility.function.Callback;
public class DecompilerProvider extends NavigatableComponentProviderAdapter
implements DomainObjectListener, OptionsChangeListener, DecompilerCallbackHandler {
@@ -105,6 +106,12 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
private ViewerPosition pendingViewerPosition;
private SwingUpdateManager redecompileUpdater;
+
+ // Follow-up work can be items that need to happen after a pending decompile is finished, such
+ // as updating highlights after a variable rename
+ private SwingUpdateManager followUpWorkUpdater;
+ private Queue<Callback> followUpWork = new ConcurrentLinkedQueue<>();
+
private ServiceListener serviceListener = new ServiceListener() {
@Override
@@ -132,9 +139,11 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
decompilerOptions = new DecompileOptions();
initializeDecompilerOptions();
- highlightController = new LocationClangHighlightController();
controller = new DecompilerController(this, decompilerOptions, clipboardProvider);
DecompilerPanel decompilerPanel = controller.getDecompilerPanel();
+
+ // TODO move the hl controller into the panel
+ highlightController = new LocationClangHighlightController();
decompilerPanel.setHighlightController(highlightController);
decorationPanel = new DecoratorPanel(decompilerPanel, isConnected);
@@ -157,6 +166,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
addToTool();
redecompileUpdater = new SwingUpdateManager(500, 5000, () -> doRefresh());
+ followUpWorkUpdater = new SwingUpdateManager(() -> doFollowUpWork());
plugin.getTool().addServiceListener(serviceListener);
}
@@ -301,6 +311,20 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
}
}
+ private void doFollowUpWork() {
+ if (isBusy()) {
+ // try again later
+ followUpWorkUpdater.updateLater();
+ return;
+ }
+
+ Callback work = followUpWork.poll();
+ while (work != null) {
+ work.call();
+ work = followUpWork.poll();
+ }
+ }
+
//==================================================================================================
// OptionsListener methods
//==================================================================================================
@@ -334,6 +358,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
super.dispose();
redecompileUpdater.dispose();
+ followUpWorkUpdater.dispose();
if (clipboardService != null) {
clipboardService.deRegisterClipboardContentProvider(clipboardProvider);
@@ -421,8 +446,8 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
return null;
}
- boolean isDecompiling() {
- return controller.isDecompiling();
+ boolean isBusy() {
+ return redecompileUpdater.isBusy() || controller.isDecompiling();
}
/**
@@ -471,10 +496,6 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
tool.setStatusInfo(message);
}
- /**
- * Called from the DecompilerController to indicate that the decompilerData has changed.
- * @param decompileData the new DecompilerData
- */
@Override
public void decompileDataChanged(DecompileData decompileData) {
updateTitle();
@@ -606,6 +627,12 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
}
}
+ @Override
+ public void doWheNotBusy(Callback c) {
+ followUpWork.offer(c);
+ followUpWorkUpdater.update();
+ }
+
//==================================================================================================
// methods called from other members
//==================================================================================================
@@ -624,11 +651,10 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
// Any change in the HighlightTokens should be delivered to the new panel
DecompilerPanel panel = getDecompilerPanel();
- TokenHighlights highlightedTokens = panel.getHighlightedTokens();
+ TokenHighlights highlightedTokens = panel.getSecondaryHighlightedTokens();
DecompilerPanel newPanel = newProvider.getDecompilerPanel();
- TokenHighlights copiedTokens =
- highlightedTokens.copyHighlights(newPanel, controller.getFunction());
+ TokenHighlights copiedTokens = highlightedTokens.copyHighlights();
// Transfer the highlighted tokens
newPanel.setHighlightedTokens(copiedTokens);
@@ -757,7 +783,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
forwardSliceAction = new ForwardSliceAction(controller);
setGroupInfo(forwardSliceAction, variableGroup, subGroupPosition++);
- backwardSliceAction = new BackwardsSliceAction(controller);
+ backwardSliceAction = new BackwardsSliceAction();
setGroupInfo(backwardSliceAction, variableGroup, subGroupPosition++);
forwardSliceToOpsAction = new ForwardSliceToPCodeOpsAction(controller);
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceAction.java
index 4d9f7a2836..a7540daeb0 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceAction.java
@@ -19,23 +19,24 @@ import java.util.Set;
import docking.action.MenuData;
import ghidra.app.decompiler.ClangToken;
-import ghidra.app.decompiler.component.*;
+import ghidra.app.decompiler.component.DecompilerPanel;
+import ghidra.app.decompiler.component.DecompilerUtils;
import ghidra.app.plugin.core.decompile.DecompilerActionContext;
import ghidra.program.model.pcode.PcodeOp;
import ghidra.program.model.pcode.Varnode;
public class BackwardsSliceAction extends AbstractDecompilerAction {
- private final DecompilerController controller;
- public BackwardsSliceAction(DecompilerController controller) {
+ public static final String NAME = "Highlight Backward Slice";
+
+ public BackwardsSliceAction() {
super("Highlight Backward Slice");
- this.controller = controller;
- setPopupMenuData(new MenuData(new String[] { "Highlight Backward Slice" }, "Decompile"));
+ setPopupMenuData(new MenuData(new String[] { NAME }, "Decompile"));
}
@Override
protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {
- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();
+ DecompilerPanel decompilerPanel = context.getDecompilerPanel();
ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();
Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);
return varnode != null;
@@ -43,18 +44,18 @@ public class BackwardsSliceAction extends AbstractDecompilerAction {
@Override
protected void decompilerActionPerformed(DecompilerActionContext context) {
- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();
+
+ DecompilerPanel decompilerPanel = context.getDecompilerPanel();
ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();
Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);
if (varnode == null) {
return;
}
- decompilerPanel.clearHighlights();
+ decompilerPanel.clearPrimaryHighlights();
PcodeOp op = tokenAtCursor.getPcodeOp();
Set<Varnode> backwardSlice = DecompilerUtils.getBackwardSlice(varnode);
-
SliceHighlightColorProvider colorProvider =
new SliceHighlightColorProvider(decompilerPanel, backwardSlice, varnode, op);
decompilerPanel.addVarnodeHighlights(backwardSlice, colorProvider);
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceToPCodeOpsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceToPCodeOpsAction.java
index 0f8c9e8b97..1e6b50c648 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceToPCodeOpsAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceToPCodeOpsAction.java
@@ -51,10 +51,9 @@ public class BackwardsSliceToPCodeOpsAction extends AbstractDecompilerAction {
PcodeOp op = tokenAtCursor.getPcodeOp();
Set<PcodeOp> backwardSlice = DecompilerUtils.getBackwardSliceToPCodeOps(varnode);
backwardSlice.add(op);
- decompilerPanel.clearHighlights();
+ decompilerPanel.clearPrimaryHighlights();
decompilerPanel.addPcodeOpHighlights(backwardSlice,
- decompilerPanel.getPrimaryHighlightColor());
- decompilerPanel.repaint();
+ decompilerPanel.getCurrentVariableHighlightColor());
}
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceToPCodeOpsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceToPCodeOpsAction.java
index ed7d418641..7ec1bb5cc7 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceToPCodeOpsAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceToPCodeOpsAction.java
@@ -51,10 +51,9 @@ public class ForwardSliceToPCodeOpsAction extends AbstractDecompilerAction {
PcodeOp op = tokenAtCursor.getPcodeOp();
Set<PcodeOp> forwardSlice = DecompilerUtils.getForwardSliceToPCodeOps(varnode);
forwardSlice.add(op);
- decompilerPanel.clearHighlights();
+ decompilerPanel.clearPrimaryHighlights();
decompilerPanel.addPcodeOpHighlights(forwardSlice,
- decompilerPanel.getPrimaryHighlightColor());
- decompilerPanel.repaint();
+ decompilerPanel.getCurrentVariableHighlightColor());
}
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightsAction.java
index 4988d350f2..b9cb1c632d 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightsAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightsAction.java
@@ -17,10 +17,8 @@ package ghidra.app.plugin.core.decompile.actions;
import docking.action.MenuData;
import ghidra.app.decompiler.component.DecompilerPanel;
-import ghidra.app.decompiler.component.TokenHighlights;
import ghidra.app.plugin.core.decompile.DecompilerActionContext;
import ghidra.app.util.HelpTopics;
-import ghidra.program.model.listing.Function;
import ghidra.util.HelpLocation;
public class RemoveSecondaryHighlightsAction extends AbstractDecompilerAction {
@@ -41,9 +39,7 @@ public class RemoveSecondaryHighlightsAction extends AbstractDecompilerAction {
@Override
protected void decompilerActionPerformed(DecompilerActionContext context) {
- Function function = context.getFunction();
DecompilerPanel panel = context.getDecompilerPanel();
- TokenHighlights highlightTokens = panel.getHighlightedTokens();
- highlightTokens.remove(function);
+ panel.removeSecondaryHighlights();
}
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SliceHighlightColorProvider.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SliceHighlightColorProvider.java
index f53a3cfdf9..2cb63ee58a 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SliceHighlightColorProvider.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SliceHighlightColorProvider.java
@@ -39,7 +39,7 @@ public class SliceHighlightColorProvider implements TokenHighlightColorProvider
this.specialVn = specialVn;
this.specialOp = specialOp;
- hlColor = panel.getPrimaryHighlightColor();
+ hlColor = panel.getCurrentVariableHighlightColor();
specialHlColor = panel.getSpecialHighlightColor();
}
@@ -47,18 +47,23 @@ public class SliceHighlightColorProvider implements TokenHighlightColorProvider
public Color getColor(ClangToken token) {
Varnode vn = DecompilerUtils.getVarnodeRef(token);
+ if (vn == null) {
+ return null;
+ }
+
+ Color c = null;
if (varnodes.contains(vn)) {
- return hlColor;
+ c = hlColor;
}
if (specialOp == null) {
- return null;
+ return c;
}
// look for specific varnode to label with special color
if (vn == specialVn && token.getPcodeOp() == specialOp) {
- return specialHlColor;
+ c = specialHlColor;
}
- return null;
+ return c;
}
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ToggleSecondaryHighlightAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ToggleSecondaryHighlightAction.java
index 9e5bdbf221..c2ed41a421 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ToggleSecondaryHighlightAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ToggleSecondaryHighlightAction.java
@@ -15,13 +15,14 @@
*/
package ghidra.app.plugin.core.decompile.actions;
-import java.awt.Color;
import java.awt.event.KeyEvent;
+import java.util.List;
+import java.util.function.Supplier;
import docking.action.KeyBindingData;
import docking.action.MenuData;
import ghidra.app.decompiler.ClangToken;
-import ghidra.app.decompiler.component.*;
+import ghidra.app.decompiler.component.DecompilerPanel;
import ghidra.app.plugin.core.decompile.DecompilerActionContext;
import ghidra.app.util.HelpTopics;
import ghidra.program.model.pcode.HighVariable;
@@ -74,23 +75,8 @@ public class ToggleSecondaryHighlightAction extends AbstractDecompilerAction {
protected void decompilerActionPerformed(DecompilerActionContext context) {
DecompilerPanel panel = context.getDecompilerPanel();
- TokenHighlights highlights = panel.getHighlightedTokens();
- ClangToken tokenAtCursor = panel.getTokenAtCursor();
- String text = tokenAtCursor.getText();
-
- // toggle the highlight
- HighlightToken highlight = highlights.get(tokenAtCursor);
- if (highlight == null) {
- // TODO maybe move this to a 'createHighlight()' method on the highlights
- Color highlightColor = highlights.getColor(text);
- HighlightToken newHiglight = new HighlightToken(tokenAtCursor, highlightColor);
- highlights.add(highlight);
- panel.tokenHighlightsAdded(newHiglight);
- }
- else {
- highlights.remove(highlight);
- panel.tokenHighlightsRemoved(highlight);
-
- }
+ ClangToken token = panel.getTokenAtCursor();
+ Supplier<List<ClangToken>> lazyTokens = () -> panel.findTokensByName(token.getText());
+ panel.toggleSecondaryHighlight(token, lazyTokens);
}
}
diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerTest.java
index a369aee9be..ccdf8caea0 100644
--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerTest.java
+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerTest.java
@@ -70,7 +70,7 @@ public abstract class AbstractDecompilerTest extends AbstractProgramBasedTest {
protected void waitForDecompiler() {
waitForSwing();
- waitForCondition(() -> !provider.isDecompiling());
+ waitForCondition(() -> !provider.isBusy());
waitForSwing();
}
@@ -80,6 +80,7 @@ public abstract class AbstractDecompilerTest extends AbstractProgramBasedTest {
DecompilerPanel panel = provider.getDecompilerPanel();
FieldPanel fp = panel.getFieldPanel();
click(fp, 1, true);
+ waitForSwing();
}
protected void doubleClick() {
@@ -102,6 +103,15 @@ public abstract class AbstractDecompilerTest extends AbstractProgramBasedTest {
return (ClangTextField) line;
}
+ // note: the index is 0-based; use getFieldForLine() when using 1-based line numbers
+ protected ClangTextField getFieldForIndex(int lineIndex) {
+
+ DecompilerPanel panel = provider.getDecompilerPanel();
+ List<Field> fields = panel.getFields();
+ Field line = fields.get(lineIndex);
+ return (ClangTextField) line;
+ }
+
protected ClangToken getToken(int line, int col) {
FieldLocation loc = loc(line, col);
ClangTextField field = getFieldForLine(line);
diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerClangTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerClangTest.java
index 59e673b3a8..8b084811e2 100644
--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerClangTest.java
+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerClangTest.java
@@ -18,8 +18,9 @@ package ghidra.app.plugin.core.decompile;
import static org.junit.Assert.*;
import java.awt.Color;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
import org.junit.Test;
@@ -29,8 +30,7 @@ import docking.widgets.fieldpanel.support.FieldLocation;
import ghidra.app.cmd.comments.SetCommentCmd;
import ghidra.app.decompiler.ClangToken;
import ghidra.app.decompiler.component.*;
-import ghidra.app.plugin.core.decompile.actions.RemoveSecondaryHighlightsAction;
-import ghidra.app.plugin.core.decompile.actions.ToggleSecondaryHighlightAction;
+import ghidra.app.plugin.core.decompile.actions.*;
import ghidra.program.model.listing.CodeUnit;
public class DecompilerClangTest extends AbstractDecompilerTest {
@@ -43,6 +43,36 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
@Test
public void testClangTextField_getTokenIndex() {
+ /*
+ 1|
+ 2| int _main(int argc,char **argv)
+ 3|
+ 4| {
+ 5| _a.id = 1;
+ 6| _a.name = "A";
+ 7| _b.id = 2;
+ 8| _b.name = "B";
+ 9| _c.id = 3;
+ 10| _c.name = "C";
+ 11| _d.id = 4;
+ 12| _d.name = "D";
+ 13| _e.id = 5;
+ 14| _e.name = "E";
+ 15| _c.d.e._0_8_ = CONCAT44(_e._4_4_,5);
+ 16| _d.e.name = "E";
+ 17| _c.d._0_8_ = CONCAT44(_d._4_4_,4);
+ 18| _c.d.name = "D";
+ 19| _c.d.e.name = "E";
+ 20| _d.e._0_8_ = _c.d.e._0_8_;
+ 21| _memcpy(&_b.c,&_c,0x30);
+ 22| _memcpy(&_a.b,&_b,0x40);
+ 23| _call_structure_A(&_a);
+ 24| return 0;
+ 25| }
+ 16|
+
+ */
+
decompile("100000bf0"); // 'main'
/*
@@ -62,6 +92,36 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
@Test
public void testClangTextField_getNextTokenIndex() {
+ /*
+ 1|
+ 2| int _main(int argc,char **argv)
+ 3|
+ 4| {
+ 5| _a.id = 1;
+ 6| _a.name = "A";
+ 7| _b.id = 2;
+ 8| _b.name = "B";
+ 9| _c.id = 3;
+ 10| _c.name = "C";
+ 11| _d.id = 4;
+ 12| _d.name = "D";
+ 13| _e.id = 5;
+ 14| _e.name = "E";
+ 15| _c.d.e._0_8_ = CONCAT44(_e._4_4_,5);
+ 16| _d.e.name = "E";
+ 17| _c.d._0_8_ = CONCAT44(_d._4_4_,4);
+ 18| _c.d.name = "D";
+ 19| _c.d.e.name = "E";
+ 20| _d.e._0_8_ = _c.d.e._0_8_;
+ 21| _memcpy(&_b.c,&_c,0x30);
+ 22| _memcpy(&_a.b,&_b,0x40);
+ 23| _call_structure_A(&_a);
+ 24| return 0;
+ 25| }
+ 16|
+
+ */
+
decompile("100000bf0"); // 'main'
/*
@@ -82,6 +142,25 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
@Test
public void testCommentAnnotation() {
+ /*
+
+ Decomp of '_call_structure_A':
+
+ 1|
+ 2| void _call_structure_A(A *a)
+ 3|
+ 4| {
+ 5| _printf("call_structure_A: %s\n",a->name);
+ 6| _printf("call_structure_A: %s\n",(a->b).name);
+ 7| _printf("call_structure_A: %s\n",(a->b).c.name);
+ 8| _printf("call_structure_A: %s\n",(a->b).c.d.name);
+ 9| _printf("call_structure_A: %s\n",(a->b).c.d.e.name);
+ 10| _call_structure_B(&a->b);
+ 11| return;
+ 12| }
+
+ */
+
String commentAddress = "100000d80";
String linkDisplayText = "function _call_structure_A()";
String linkAddress = "100000e10";
@@ -110,7 +189,7 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
setDecompilerLocation(line, charPosition);
decompile("100000d60"); // _call_structure_A()
- line = 0; // function signature
+ line = 2; // function signature
charPosition = 0; // start of signature
assertCurrentLocation(line, charPosition);
}
@@ -156,7 +235,7 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
}
@Test
- public void testMultiColorHighlighting() {
+ public void testSecondaryHighlighting() {
/*
@@ -184,13 +263,12 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
int charPosition = 2;
setDecompilerLocation(line, charPosition);
- ClangToken token = getToken();
- String text = token.getText();
- assertEquals("_printf", text);
+ ClangToken token1 = getToken();
+ String text1 = token1.getText();
+ assertEquals("_printf", text1);
Color color = highlight();
-
- assertAllFieldsHighlighted(text, color);
+ assertAllFieldsHighlightedExceptForToken(token1, color);
// 5:30 "a->name"
line = 5;
@@ -201,8 +279,8 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
assertEquals("name", text2);
Color color2 = highlight();
- assertAllFieldsHighlighted(text, color);
- assertAllFieldsHighlighted(text2, color2);
+ assertAllFieldsHighlighted(text1, color);
+ assertAllFieldsHighlightedExceptForToken(token2, color2);
// 2:1 "void"
line = 2;
@@ -213,13 +291,13 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
assertEquals("void", text3);
Color color3 = highlight();
- assertAllFieldsHighlighted(text, color);
+ assertAllFieldsHighlighted(text1, color);
assertAllFieldsHighlighted(text2, color2);
- assertAllFieldsHighlighted(text3, color3);
+ assertAllFieldsHighlightedExceptForToken(token3, color3);
}
@Test
- public void testMultiColorHighlighting_ClearHighlight_WithMultipleHighlights() {
+ public void testSecondaryHighlighting_ClearHighlight_WithMultipleHighlights() {
/*
@@ -267,15 +345,14 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
line = 5;
charPosition = 2;
setDecompilerLocation(line, charPosition);
- clearHighlight();
+ clearSecondaryHighlight();
- Color noHighlight = null;
- assertAllFieldsHighlighted(text, noHighlight);
+ assertNoFieldsSecondaryHighlighted(text);
assertAllFieldsHighlighted(text2, color2);
}
@Test
- public void testMultiColorHighlighting_ClearAll() {
+ public void testSecondaryHighlighting_ClearAll() {
/*
@@ -321,13 +398,12 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
clearAllHighlights();
- Color noHighlight = null;
- assertAllFieldsHighlighted(text, noHighlight);
- assertAllFieldsHighlighted(text2, noHighlight);
+ assertNoFieldsSecondaryHighlighted(text);
+ assertNoFieldsSecondaryHighlighted(text2);
}
@Test
- public void testMultiColorHighlighting_RenameHighlightedVariable() {
+ public void testSecondaryHighlighting_RenameHighlightedVariable() {
/*
@@ -366,11 +442,11 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
token = getToken();
text = token.getText();
assertEquals("bob", text);
- assertAllFieldsHighlighted(text, color);
+ assertAllFieldsHighlightedExceptForToken(token, color);
}
@Test
- public void testMultiColorHighlighting_HighlightColorGetsReused() {
+ public void testSecondaryHighlighting_HighlightColorGetsReused() {
/*
@@ -404,36 +480,244 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
Color color = highlight();
- clearHighlight();
+ clearSecondaryHighlight();
- Color noHighlight = null;
- assertAllFieldsHighlighted(text, noHighlight);
+ assertNoFieldsSecondaryHighlighted(text);
Color secondColor = highlight();
assertEquals(color, secondColor);
}
+ @Test
+ public void testSecondaryHighlighting_InteractionWithPrimaryHighlighting_LeftClick() {
+
+ /*
+
+ Decomp of '_call_structure_A':
+
+ 1|
+ 2| void _call_structure_A(A *a)
+ 3|
+ 4| {
+ 5| _printf("call_structure_A: %s\n",a->name);
+ 6| _printf("call_structure_A: %s\n",(a->b).name);
+ 7| _printf("call_structure_A: %s\n",(a->b).c.name);
+ 8| _printf("call_structure_A: %s\n",(a->b).c.d.name);
+ 9| _printf("call_structure_A: %s\n",(a->b).c.d.e.name);
+ 10| _call_structure_B(&a->b);
+ 11| return;
+ 12| }
+
+ */
+
+ decompile("100000d60"); // '_call_structure_A'
+
+ // 5:2 "_printf"
+ int line = 5;
+ int charPosition = 2;
+ setDecompilerLocation(line, charPosition);
+
+ ClangToken token = getToken();
+ String text = token.getText();
+ assertEquals("_printf", text);
+
+ Color color = highlight();
+ assertAllFieldsHighlightedExceptForToken(token, color);
+
+ setDecompilerLocation(line, charPosition + 1);
+ assertCombinedHighlightColor(token);
+
+ // 2:1 "void"
+ line = 2;
+ charPosition = 1;
+ setDecompilerLocation(line, charPosition);
+ ClangToken token3 = getToken();
+ String text3 = token3.getText();
+ assertEquals("void", text3);
+ assertPrimaryHighlight(token3);
+
+ // secondary highlight was restored
+ assertAllFieldsHighlighted(text, color);
+ }
+
+ @Test
+ public void testSecondaryHighlighting_ForwardSliceDoesNotClearSecondaryHighlight() {
+
+ /*
+
+ Decomp of '_call_structure_A':
+
+ 1|
+ 2| void _call_structure_A(A *a)
+ 3|
+ 4| {
+ 5| _printf("call_structure_A: %s\n",a->name);
+ 6| _printf("call_structure_A: %s\n",(a->b).name);
+ 7| _printf("call_structure_A: %s\n",(a->b).c.name);
+ 8| _printf("call_structure_A: %s\n",(a->b).c.d.name);
+ 9| _printf("call_structure_A: %s\n",(a->b).c.d.e.name);
+ 10| _call_structure_B(&a->b);
+ 11| return;
+ 12| }
+
+ */
+
+ decompile("100000d60"); // '_call_structure_A'
+
+ // 5:2 "_printf"
+ int line = 5;
+ int charPosition = 2;
+ setDecompilerLocation(line, charPosition);
+
+ ClangToken token = getToken();
+ String secondaryHighlightText = token.getText();
+ assertEquals("_printf", secondaryHighlightText);
+
+ Color color = highlight();
+
+ // 10:19 "&a"
+ line = 10;
+ charPosition = 19;
+ setDecompilerLocation(line, charPosition);
+
+ backwardSlice();
+ token = getToken();
+ assertAllFieldsPrimaryHighlighted(token.getText());
+ assertAllFieldsHighlighted(secondaryHighlightText, color);
+ }
+
+ @Test
+ public void testSecondaryHighlighting_CombinedWithPrimaryHighlight() {
+
+ /*
+
+ Decomp of '_call_structure_A':
+
+ 1|
+ 2| void _call_structure_A(A *a)
+ 3|
+ 4| {
+ 5| _printf("call_structure_A: %s\n",a->name);
+ 6| _printf("call_structure_A: %s\n",(a->b).name);
+ 7| _printf("call_structure_A: %s\n",(a->b).c.name);
+ 8| _printf("call_structure_A: %s\n",(a->b).c.d.name);
+ 9| _printf("call_structure_A: %s\n",(a->b).c.d.e.name);
+ 10| _call_structure_B(&a->b);
+ 11| return;
+ 12| }
+
+ */
+
+ decompile("100000d60"); // '_call_structure_A'
+
+ // 5:2 "_printf"
+ int line = 5;
+ int charPosition = 2;
+ setDecompilerLocation(line, charPosition);
+
+ ClangToken token = getToken();
+ String secondaryHighlightText = token.getText();
+ assertEquals("_printf", secondaryHighlightText);
+
+ Color color = highlight();
+
+ // 6:2 "_printf"
+ line = 6;
+ charPosition = 2;
+ setDecompilerLocation(line, charPosition);
+
+ token = getToken();
+ assertCombinedHighlightColor(token);
+ assertAllFieldsHighlightedExceptForToken(token, color);
+
+ // no click away and make sure the secondary highlight color returns
+ // 10:19 "&a"
+ line = 10;
+ charPosition = 19;
+ setDecompilerLocation(line, charPosition);
+ assertAllFieldsHighlighted(secondaryHighlightText, color);
+ }
+
// TODO how does highlight interplay with slice highlight?
// -toggling a color highlight should not remove the other highlights
// --test on and off
// -test setting primary highlight does not clear secondary highlights
-
- // TODO test color chooser
-
- // TODO test persistence
+ // -- secondary remains after clicking away
+ // --middle-mouse
+ // --slicing
// TODO test highlights passed to clone
- // TODO allow for any text, not just variables?=
+ // TODO allow for any text, not just variables?
// TODO - follow-up test that 2 variables with the same name do not get highlighted
+ // TODO test color chooser
+
+ // TODO test persistence
// TODO Energy
// -highlight tokens between parens
+ // TODO test clone copies highlights
+
//==================================================================================================
// Private Methods
//==================================================================================================
+ private void assertCombinedHighlightColor(ClangToken token) {
+
+ Color combinedColor = getCombinedHighlightColor(token);
+ Color actual = token.getHighlight();
+ assertEquals(combinedColor, actual);
+ }
+
+ private Color getDefaultHighlightColor() {
+
+ DecompilerController controller = provider.getController();
+ DecompilerPanel panel = controller.getDecompilerPanel();
+ Color c = panel.getCurrentVariableHighlightColor();
+ return c;
+ }
+
+ private Color getSpecialHighlightColor() {
+
+ DecompilerController controller = provider.getController();
+ DecompilerPanel panel = controller.getDecompilerPanel();
+ Color c = panel.getSpecialHighlightColor();
+ return c;
+ }
+
+ private Color getMiddleMouseHighlightColor() {
+
+ DecompilerController controller = provider.getController();
+ DecompilerPanel panel = controller.getDecompilerPanel();
+ Color c = panel.getMiddleMouseHighlightColor();
+ return c;
+ }
+
+ private Color getCombinedHighlightColor(ClangToken token) {
+ DecompilerController controller = provider.getController();
+ DecompilerPanel panel = controller.getDecompilerPanel();
+ ClangHighlightController highlightController = panel.getHighlightController();
+ return highlightController.getCombinedColor(token);
+ }
+
+ private void backwardSlice() {
+ DockingActionIf action = getAction(decompiler, BackwardsSliceAction.NAME);
+ performAction(action);
+ }
+
+ private void assertPrimaryHighlight(ClangToken token) {
+ DecompilerController controller = provider.getController();
+ DecompilerPanel panel = controller.getDecompilerPanel();
+ Color hlColor = panel.getCurrentVariableHighlightColor();
+ assertEquals(hlColor, token.getHighlight());
+ }
+
+ private void assertPrimaryAndSecondaryHighlight(ClangToken token) {
+ // TODO Auto-generated method stub
+ fail();
+ }
+
private void rename(String newName) {
DockingActionIf action = getAction(decompiler, "Rename Variable");
performAction(action, false);
@@ -447,7 +731,8 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
private void clearAllHighlights() {
- DockingActionIf highlightAction = getAction(decompiler, RemoveSecondaryHighlightsAction.NAME);
+ DockingActionIf highlightAction =
+ getAction(decompiler, RemoveSecondaryHighlightsAction.NAME);
performAction(highlightAction);
}
@@ -455,41 +740,112 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
ClangToken token = getToken();
- DockingActionIf highlightAction = getAction(decompiler, ToggleSecondaryHighlightAction.NAME);
+ DockingActionIf highlightAction =
+ getAction(decompiler, ToggleSecondaryHighlightAction.NAME);
performAction(highlightAction);
DecompilerController controller = provider.getController();
DecompilerPanel panel = controller.getDecompilerPanel();
- TokenHighlights highlights = panel.getHighlightedTokens();
+ TokenHighlights highlights = panel.getSecondaryHighlightedTokens();
HighlightToken ht = highlights.get(token);
assertNotNull("No highlight for token: " + token, ht);
return ht.getColor();
}
- private void clearHighlight() {
+ private void clearSecondaryHighlight() {
ClangToken token = getToken();
- DockingActionIf highlightAction = getAction(decompiler, ToggleSecondaryHighlightAction.NAME);
+ DockingActionIf highlightAction =
+ getAction(decompiler, ToggleSecondaryHighlightAction.NAME);
performAction(highlightAction);
DecompilerController controller = provider.getController();
DecompilerPanel panel = controller.getDecompilerPanel();
- TokenHighlights highlights = panel.getHighlightedTokens();
+ TokenHighlights highlights = panel.getSecondaryHighlightedTokens();
HighlightToken ht = highlights.get(token);
assertNull("Token should not be highlighted: " + token, ht);
}
- private void assertAllFieldsHighlighted(String name, Color color) {
+ private void assertAllFieldsPrimaryHighlighted(String name) {
+
+ Color hlColor = getDefaultHighlightColor();
+ Color specialColor = getSpecialHighlightColor();
+
+ DecompilerController controller = provider.getController();
+ DecompilerPanel panel = controller.getDecompilerPanel();
+ List<ClangToken> tokensWithName = panel.findTokensByName(name);
+ assertFalse(tokensWithName.isEmpty());
+ for (ClangToken otherToken : tokensWithName) {
+
+ Color tokenColor = otherToken.getHighlight();
+ boolean yes = hlColor.equals(tokenColor) || specialColor.equals(tokenColor);
+
+ assertTrue("Token is not highlighted: '" + otherToken + "'" + "\n\texpected: " +
+ toString(hlColor) + "; found: " + toString(otherToken.getHighlight()), yes);
+ }
+ }
+
+ private void assertAllFieldsHighlightedExceptForToken(ClangToken token, Color color) {
+ Predicate<ClangToken> ignores = t -> t == token;
+ String name = token.getText();
+ assertAllFieldsHighlighted(name, color, ignores);
+ }
+
+ private void assertNoFieldsSecondaryHighlighted(String name) {
+ Color defaultHlColor = getDefaultHighlightColor();
+ Color specialHlColor = getSpecialHighlightColor();
+ Color middleMouseHlColor = getMiddleMouseHighlightColor();
+ ColorMatcher allowedColors =
+ new ColorMatcher(defaultHlColor, specialHlColor, middleMouseHlColor, null);
+ Predicate<ClangToken> noIgnores = t -> false;
+ assertAllFieldsHighlighted(name, allowedColors, noIgnores);
+ }
+
+ private void assertAllFieldsHighlighted(String name, Color hlColor) {
+
+ Predicate<ClangToken> noIgnores = t -> false;
+ assertAllFieldsHighlighted(name, hlColor, noIgnores);
+ }
+
+ private void assertAllFieldsHighlighted(String name, Color color,
+ Predicate<ClangToken> ignore) {
+
+ ColorMatcher cm = new ColorMatcher(color);
+ assertAllFieldsHighlighted(name, cm, ignore);
+ }
+
+ private void assertAllFieldsHighlighted(String name, ColorMatcher colorMatcher,
+ Predicate<ClangToken> ignore) {
DecompilerController controller = provider.getController();
DecompilerPanel panel = controller.getDecompilerPanel();
List<ClangToken> tokensWithName = panel.findTokensByName(name);
assertFalse(tokensWithName.isEmpty());
for (ClangToken otherToken : tokensWithName) {
- assertEquals(color, otherToken.getHighlight());
+ if (ignore.test(otherToken)) {
+ continue;
+ }
+
+ Color actual = otherToken.getHighlight();
+ assertTrue("Token is not highlighted: '" + otherToken + "'" + "\n\texpected: " +
+ colorMatcher + "; found: " + toString(actual), colorMatcher.matches(actual));
}
}
+ private String toString(Color c) {
+ if (c == null) {
+ return "Color{null}";
+ }
+ int r = c.getRed();
+ int g = c.getGreen();
+ int b = c.getBlue();
+ int a = c.getAlpha();
+ // "Color[r=" + r +",g=" + g +",b=" + b + ",a="+ a + "]"
+ // "Color[r=%s,g=%s,b=%s,a=%s]"
+ String formatted = String.format("Color{%s, %s, %s, %s}", r, g, b, a);
+ return formatted;
+ }
+
private void copy() {
Set<DockingActionIf> actions = getActionsByOwnerAndName(tool, "ClipboardPlugin", "Copy");
@@ -525,22 +881,21 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
}
private ClangTextField getTextField(FieldLocation loc) {
- ClangTextField field = getFieldForLine(loc.getIndex().intValue());
+ ClangTextField field = getFieldForIndex(loc.getIndex().intValue());
return field;
}
private void assertDisplayText(String expected, int line) {
FieldLocation loc = loc(line, 0 /*column*/);
- ClangTextField field = getFieldForLine(loc.getIndex().intValue());
+ ClangTextField field = getFieldForIndex(loc.getIndex().intValue());
String actual = field.getText();
assertEquals("Line text not as expected at line " + line, expected, actual);
}
private void assertCurrentLocation(int line, int col) {
- int oneBasedLine = line + 1;
DecompilerPanel panel = provider.getDecompilerPanel();
FieldLocation actual = panel.getCursorPosition();
- FieldLocation expected = loc(oneBasedLine, col);
+ FieldLocation expected = loc(line, col);
assertEquals("Decompiler cursor is not at the expected location", expected, actual);
}
@@ -556,4 +911,36 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
return index;
}
+ private class ColorMatcher {
+
+ private Set<Color> myColors = new HashSet<>();
+
+ ColorMatcher(Color... colors) {
+ // note: we allow null
+
+ for (Color c : colors) {
+ myColors.add(c);
+ }
+ }
+
+ public boolean matches(Color otherColor) {
+ for (Color c : myColors) {
+ if (Objects.equals(c, otherColor)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ //@formatter:off
+ return myColors
+ .stream()
+ .map(c -> DecompilerClangTest.this.toString(c))
+ .collect(Collectors.joining(","))
+ ;
+ //@formatter:on
+ }
+ }
}
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/fieldpanel/FieldPanel.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/fieldpanel/FieldPanel.java
index 819dfead1c..a498fa5217 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/fieldpanel/FieldPanel.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/fieldpanel/FieldPanel.java
@@ -1633,8 +1633,8 @@ public class FieldPanel extends JPanel
if (e.getButton() != MouseEvent.BUTTON1) {
return;
}
- cursorHandler.setCursorPos(e.getX(), e.getY(), null);
- cursorHandler.notifyCursorChanged(EventTrigger.GUI_ACTION);
+
+ cursorHandler.setCursorPos(e.getX(), e.getY(), EventTrigger.GUI_ACTION);
if (!selectionHandler.isInProgress() && !didDrag) {
selectionHandler.clearSelection();
}