Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3292 - Decompiler - Secondary Highlights - Checkpoint 1 - More tests needed; more design refactor needed; help needed

dragonmachere619d07ee18d
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/cparser/ParseDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/cparser/ParseDialog.java
index 092aa0238b..61a8977002 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/cparser/ParseDialog.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/cparser/ParseDialog.java
@@ -31,7 +31,6 @@ import docking.action.*;
import docking.widgets.OptionDialog;
import docking.widgets.combobox.GhidraComboBox;
import docking.widgets.dialogs.InputDialog;
-import docking.widgets.dialogs.InputDialogListener;
import docking.widgets.filechooser.GhidraFileChooser;
import docking.widgets.filechooser.GhidraFileChooserMode;
import docking.widgets.pathmanager.PathnameTablePanel;
@@ -446,8 +445,7 @@ class ParseDialog extends DialogComponentProvider {
private void saveAs(ComboBoxItem item) {
- InputDialog d =
- new InputDialog("Enter Profile Name", "Profile Name", (InputDialogListener) null);
+ InputDialog d = new InputDialog("Enter Profile Name", "Profile Name");
plugin.getTool().showDialog(d, getComponent());
String name = d.getValue();
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/ClangLine.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/ClangLine.java
index 600a3c11a4..7ca6f813bb 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/ClangLine.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/ClangLine.java
@@ -67,6 +67,10 @@ public class ClangLine {
return tokens.get(i);
}
+ public int indexOfToken(ClangToken token) {
+ return tokens.indexOf(token);
+ }
+
public String toDebugString(List<ClangToken> calloutTokens) {
return toDebugString(calloutTokens, "[", "]");
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/ClangToken.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/ClangToken.java
index 954010ec0c..acb1c75595 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/ClangToken.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/ClangToken.java
@@ -245,6 +245,9 @@ public class ClangToken implements ClangNode {
@Override
public String toString() {
+ if (lineparent != null) {
+ return lineparent.getLineNumber() + ": " + text;
+ }
return text;
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileOptions.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileOptions.java
index 9abf26d7ee..79709d8c40 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileOptions.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileOptions.java
@@ -158,11 +158,6 @@ public class DecompileOptions { //
private final static CommentStyleEnum COMMENTSTYLE_OPTIONDEFAULT = CommentStyleEnum.CStyle;
private CommentStyleEnum commentStyle;
- private final static String HIGHLIGHT_OPTIONSTRING = "Display.Custom Highlights";
- private final static String HIGHLIGHT_OPTIONDESCRIPTION = "If set, custom highlights is enabled";
- private final static boolean HIGHLIGHT_OPTIONDEFAULT = false;
- private boolean highlightInclude;
-
private final static String COMMENTPRE_OPTIONSTRING = "Display.Display PRE comments";
private final static String COMMENTPRE_OPTIONDESCRIPTION =
"If set, disassembly pre-instruction (PRE) comments are displayed " +
@@ -371,7 +366,6 @@ public class DecompileOptions { //
indentwidth = opt.getInt(INDENTWIDTH_OPTIONSTRING, INDENTWIDTH_OPTIONDEFAULT);
commentindent = opt.getInt(COMMENTINDENT_OPTIONSTRING, COMMENTINDENT_OPTIONDEFAULT);
commentStyle = opt.getEnum(COMMENTSTYLE_OPTIONSTRING, COMMENTSTYLE_OPTIONDEFAULT);
- highlightInclude = opt.getBoolean(HIGHLIGHT_OPTIONSTRING, HIGHLIGHT_OPTIONDEFAULT);
commentEOLInclude = opt.getBoolean(COMMENTEOL_OPTIONSTRING, COMMENTEOL_OPTIONDEFAULT);
commentPREInclude = opt.getBoolean(COMMENTPRE_OPTIONSTRING, COMMENTPRE_OPTIONDEFAULT);
commentPOSTInclude = opt.getBoolean(COMMENTPOST_OPTIONSTRING, COMMENTPOST_OPTIONDEFAULT);
@@ -458,7 +452,7 @@ public class DecompileOptions { //
* @param ownerPlugin the plugin to which the options should be registered
* @param opt the options object to register with
* @param program the program
- * @param hlep
+ * @param help the help
*/
public void registerOptions(Plugin ownerPlugin, ToolOptions opt, Program program,
HelpLocation help) {
@@ -493,8 +487,6 @@ public class DecompileOptions { //
COMMENTSTYLE_OPTIONDESCRIPTION);
opt.registerOption(COMMENTEOL_OPTIONSTRING, COMMENTEOL_OPTIONDEFAULT, help,
COMMENTEOL_OPTIONDESCRIPTION);
- opt.registerOption(HIGHLIGHT_OPTIONSTRING, HIGHLIGHT_OPTIONDEFAULT, help,
- HIGHLIGHT_OPTIONDESCRIPTION);
opt.registerOption(COMMENTPRE_OPTIONSTRING, COMMENTPRE_OPTIONDEFAULT, help,
COMMENTPRE_OPTIONDESCRIPTION);
opt.registerOption(COMMENTPOST_OPTIONSTRING, COMMENTPOST_OPTIONDEFAULT, help,
@@ -689,10 +681,6 @@ public class DecompileOptions { //
return middleMouseHighlightButton;
}
- public boolean isHighlightIncluded() {
- return highlightInclude;
- }
-
public boolean isPRECommentIncluded() {
return commentPREInclude;
}
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 4783afe98a..2314a6360f 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
@@ -17,34 +17,40 @@ package ghidra.app.decompiler.component;
import java.awt.Color;
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.program.model.pcode.Varnode;
/**
- * Class to handle highlights for a decompiled function.
+ * Class to handle highlights for a decompiled function
*/
-
public abstract class ClangHighlightController {
+ public static ClangHighlightController dummyIfNull(ClangHighlightController c) {
+ if (c == null) {
+ return new NullClangHighlightController();
+ }
+ return c;
+ }
+
// Note: Most of the methods in this class were extracted from the ClangLayoutController class
// and the DecompilerPanel class.
- protected Color defaultNonFunctionBackgroundColor = new Color(220, 220, 220);
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 HashSet<ClangToken> highlightTokenSet = new HashSet<>();
-
- protected ArrayList<ClangHighlightListener> highlightListenerList = new ArrayList<>();
-
- public ClangHighlightController() {
- }
+ 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);
@@ -95,140 +101,188 @@ public abstract class ClangHighlightController {
* Return the current highlighted token (if exists and unique)
* @return token or null
*/
- public ClangToken getHighlightedToken() {
- if (highlightTokenSet.size() == 1) {
+ private ClangToken getHighlightedToken() {
+ if (primaryHighlightTokens.size() == 1) {
ClangToken[] tokenArray =
- highlightTokenSet.toArray(new ClangToken[highlightTokenSet.size()]);
+ primaryHighlightTokens.toArray(new ClangToken[primaryHighlightTokens.size()]);
return tokenArray[0];
}
return null;
}
- public void addVarnodesToHighlight(ClangNode parentNode, Set<Varnode> varnodes,
- Color highlightColor, Varnode specificvn, PcodeOp specificop, Color specialColor) {
- int nchild = parentNode.numChildren();
- for (int i = 0; i < nchild; ++i) {
+ private void gatherAllTokens(ClangNode parentNode, Set<ClangToken> results) {
+
+ int n = parentNode.numChildren();
+ for (int i = 0; i < n; i++) {
ClangNode node = parentNode.Child(i);
if (node.numChildren() > 0) {
- addVarnodesToHighlight(node, varnodes, highlightColor, specificvn, specificop,
- specialColor);
+ gatherAllTokens(node, results);
}
else if (node instanceof ClangToken) {
- ClangToken tok = (ClangToken) node;
- Varnode vn = DecompilerUtils.getVarnodeRef(tok);
- if (varnodes.contains(vn)) {
- addHighlight(tok, highlightColor);
- }
- if (vn == specificvn) { // Look for specific varnode to label with specialColor
- if ((specificop != null) && (tok.getPcodeOp() == specificop)) {
- addHighlight(tok, specialColor);
- }
- }
+ results.add((ClangToken) node);
}
}
- notifyListeners();
}
- public void addPcodeOpsToHighlight(ClangNode parentNode, Set<PcodeOp> ops,
- Color highlightColor) {
- int nchild = parentNode.numChildren();
- for (int i = 0; i < nchild; ++i) {
- ClangNode node = parentNode.Child(i);
- if (node.numChildren() > 0) {
- addPcodeOpsToHighlight(node, ops, highlightColor);
- }
- else if (node instanceof ClangToken) {
- ClangToken tok = (ClangToken) node;
- PcodeOp op = tok.getPcodeOp();
- if (ops.contains(op)) {
- addHighlight(tok, highlightColor);
- }
- }
- }
- notifyListeners();
+ public void clearPrimaryHighlights() {
+ doClearHighlights(primaryHighlightTokens);
}
- public void addTokensToHighlights(List<ClangToken> tokenList, Color highlightColor) {
- for (ClangToken clangToken : tokenList) {
- doAddHighlight(clangToken, highlightColor);
+ public void clearAllHighlights() {
+ Iterable<ClangToken> allTokens =
+ IterableUtils.chainedIterable(primaryHighlightTokens, secondaryHighlightTokens);
+ for (ClangToken clangToken : allTokens) {
+ clangToken.setHighlight(null);
+ clangToken.setMatchingToken(false);
}
+
+ primaryHighlightTokens.clear();
+ secondaryHighlightTokens.clear();
notifyListeners();
}
- public void clearHighlights() {
- for (ClangToken clangToken : highlightTokenSet) {
+ private void doClearHighlights(Set<ClangToken> highlightTokens) {
+
+ for (ClangToken clangToken : highlightTokens) {
clangToken.setHighlight(null);
- if (clangToken.isMatchingToken()) {
- clangToken.setMatchingToken(false);
- }
+ clangToken.setMatchingToken(false);
}
- highlightTokenSet.clear();
+
+ highlightTokens.clear();
notifyListeners();
}
- public void addHighlight(ClangToken clangToken, Color highlightColor) {
- doAddHighlight(clangToken, highlightColor);
+ public void addPrimaryHighlight() {
+ // TODO
+ }
+
+ public void addSecondaryHighlight() {
+ // TODO
+ }
+
+ public void addPrimaryHighlights(Supplier<? extends Collection<ClangToken>> tokens,
+ Color hlColor) {
+ Function<ClangToken, Color> colorProvider = token -> hlColor;
+ addTokensToHighlights(tokens.get(), colorProvider, primaryHighlightTokens);
+ }
+
+ public void addPrimaryHighlights(ClangNode parentNode,
+ TokenHighlightColorProvider colorProvider) {
+
+ Set<ClangToken> tokens = new HashSet<>();
+ gatherAllTokens(parentNode, tokens);
+ addTokensToHighlights(tokens, colorProvider::getColor, tokens);
+ }
+
+ public void addPrimaryHighlights(ClangNode parentNode, Set<PcodeOp> ops, Color hlColor) {
+
+ addPrimaryHighlights(parentNode, token -> {
+ PcodeOp op = token.getPcodeOp();
+ return ops.contains(op) ? hlColor : null;
+ });
+ }
+
+ private void addPrimaryHighlights(Collection<ClangToken> tokens, Color hlColor) {
+ Function<ClangToken, Color> colorProvider = token -> hlColor;
+ addTokensToHighlights(tokens, colorProvider, primaryHighlightTokens);
+ }
+
+ private void addTokensToHighlights(Collection<ClangToken> tokens,
+ Function<ClangToken, Color> colorProvider, Set<ClangToken> currentHighlights) {
+ for (ClangToken clangToken : tokens) {
+ Color color = colorProvider.apply(clangToken);
+ doAddHighlight(clangToken, color, currentHighlights);
+ }
notifyListeners();
}
- public void doAddHighlight(ClangToken clangToken, Color highlightColor) {
+ protected void addPrimaryHighlight(ClangToken token, Color highlightColor) {
+ addPrimaryHighlights(Set.of(token), highlightColor);
+ }
+
+ private void doAddHighlight(ClangToken clangToken, Color highlightColor,
+ Set<ClangToken> currentHighlights) {
+
+ if (highlightColor == null) {
+ return;
+ }
+
clangToken.setHighlight(highlightColor);
- highlightTokenSet.add(clangToken);
+ currentHighlights.add(clangToken);
}
public void clearHighlight(ClangToken clangToken) {
clangToken.setHighlight(null);
- highlightTokenSet.remove(clangToken);
+ primaryHighlightTokens.remove(clangToken);
notifyListeners();
}
- public boolean isHighlighted(ClangToken clangToken) {
- return highlightTokenSet.contains(clangToken);
- }
-
/**
* If input token is a parenthesis, highlight all
* tokens between it and its match
- * @param tok = potential parenthesis token
+ * @param tok potential parenthesis token
+ * @param highlightColor the highlight color
* @return a list of all tokens that were highlighted.
*/
- public List<ClangToken> addHighlightParen(ClangSyntaxToken tok, Color highlightColor) {
- ArrayList<ClangToken> tokenList = new ArrayList<>();
+ protected List<ClangToken> addPrimaryHighlightToTokensForParenthesis(ClangSyntaxToken tok,
+ Color highlightColor) {
+
int paren = tok.getOpen();
if (paren == -1) {
paren = tok.getClose();
}
+
if (paren == -1) {
- return tokenList; // Not a parenthesis
+ return new ArrayList<>(); // Not a parenthesis
}
+
+ List<ClangToken> results = gatherContentsOfParenthesis(tok, paren);
+ addPrimaryHighlights(results, highlightColor);
+ return results;
+ }
+
+ private List<ClangToken> gatherContentsOfParenthesis(ClangSyntaxToken tok, int parenId) {
+
+ List<ClangToken> results = new ArrayList<>();
+ int parenCount = 0;
ClangNode par = tok.Parent();
while (par != null) {
boolean outside = true;
- if (par instanceof ClangTokenGroup) {
- ArrayList<ClangNode> list = new ArrayList<>();
- ((ClangTokenGroup) par).flatten(list);
- for (int i = 0; i < list.size(); ++i) {
- ClangToken tk = (ClangToken) list.get(i);
- if (tk instanceof ClangSyntaxToken) {
- ClangSyntaxToken syn = (ClangSyntaxToken) tk;
- if (syn.getOpen() == paren) {
- outside = false;
- }
- else if (syn.getClose() == paren) {
- outside = true;
- addHighlight(syn, highlightColor);
- tokenList.add(syn);
- }
+ if (!(par instanceof ClangTokenGroup)) {
+ par = par.Parent();
+ continue;
+ }
+
+ List<ClangNode> list = new ArrayList<>();
+ ((ClangTokenGroup) par).flatten(list);
+
+ for (ClangNode node : list) {
+ ClangToken tk = (ClangToken) node;
+ if (tk instanceof ClangSyntaxToken) {
+ ClangSyntaxToken syn = (ClangSyntaxToken) tk;
+ if (syn.getOpen() == parenId) {
+ parenCount++;
+ outside = false;
}
- if (!outside) {
- addHighlight(tk, highlightColor);
- tokenList.add(tk);
+ else if (syn.getClose() == parenId) {
+ parenCount++;
+ outside = true;
+ results.add(syn);
}
}
+
+ if (!outside) {
+ results.add(tk);
+ }
+
+ if (parenCount == 2) {
+ return results; // found both parens; break out early
+ }
}
par = par.Parent();
}
- return tokenList;
+
+ return results;
}
public void addHighlightBrace(ClangSyntaxToken token, Color highlightColor) {
@@ -244,76 +298,20 @@ public abstract class ClangHighlightController {
ClangSyntaxToken matchingBrace = DecompilerUtils.getMatchingBrace(startToken);
if (matchingBrace != null) {
matchingBrace.setMatchingToken(true); // this is a signal to the painter
- addHighlight(matchingBrace, highlightColor);
+ addPrimaryHighlights(Set.of(matchingBrace), highlightColor);
}
}
- /**
- * Add highlighting to tokens that are surrounded by
- * highlighted tokens, but which have no address
- */
- public void addHighlightFill() {
- ClangTokenGroup lastgroup = null;
- ArrayList<ClangNode> newhi = new ArrayList<>();
- ArrayList<Color> newcolor = new ArrayList<>();
- for (ClangToken tok : highlightTokenSet) {
- if (tok.Parent() instanceof ClangTokenGroup) {
- ClangTokenGroup par = (ClangTokenGroup) tok.Parent();
- if (par == lastgroup) {
- continue;
- }
- lastgroup = par;
- int beg = -1;
- int end = par.numChildren();
- for (int j = 0; j < par.numChildren(); ++j) {
- if (par.Child(j) instanceof ClangToken) {
- ClangToken token = (ClangToken) par.Child(j);
- Color curcolor = token.getHighlight();
- if (curcolor != null) {
- if (beg == -1) {
- beg = j;
- }
- else {
- end = j;
- for (int k = beg + 1; k < end; ++k) {
- if (par.Child(k) instanceof ClangToken) {
- newhi.add(par.Child(k));
- newcolor.add(curcolor);
- }
- }
- beg = j;
- }
- }
- else if (token.getMinAddress() != null) {
- beg = -1;
- }
- }
- else {
- beg = -1;
- }
- }
- }
- }
- for (int i = 0; i < newhi.size(); ++i) {
- ClangToken tok = (ClangToken) newhi.get(i);
- if (tok.getHighlight() != null) {
- continue;
- }
- addHighlight(tok, newcolor.get(i));
- }
- notifyListeners();
- }
-
- public boolean addListener(ClangHighlightListener listener) {
- return highlightListenerList.add(listener);
+ public void addListener(ClangHighlightListener listener) {
+ listeners.add(listener);
}
- public boolean removeListener(ClangHighlightListener listener) {
- return highlightListenerList.remove(listener);
+ public void removeListener(ClangHighlightListener listener) {
+ listeners.remove(listener);
}
private void notifyListeners() {
- for (ClangHighlightListener listener : highlightListenerList) {
+ for (ClangHighlightListener listener : listeners) {
listener.tokenHighlightsChanged();
}
}
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 5da7eb5466..7d3a873d3a 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
@@ -20,6 +20,7 @@ import java.awt.event.MouseEvent;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
+import java.util.function.Supplier;
import javax.swing.JComponent;
import javax.swing.JPanel;
@@ -40,6 +41,7 @@ import ghidra.app.decompiler.*;
import ghidra.app.decompiler.component.hover.DecompilerHoverService;
import ghidra.app.plugin.core.decompile.DecompilerClipboardProvider;
import ghidra.app.plugin.core.decompile.actions.FieldBasedSearchLocation;
+import ghidra.app.plugin.core.decompile.actions.TokenHighlightColorProvider;
import ghidra.program.model.address.*;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
@@ -72,7 +74,7 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
private Color currentSearchHighlightColor;
private Color currentHighlightColor;
private SearchLocation currentSearchLocation;
- private HighlightTokenObservableList<HighlightToken> highlightedTokens = new HighlightTokenObservableList<HighlightToken>(this);
+ private TokenHighlights secondaryHighlights = new TokenHighlights(this);
private DecompileData decompileData = new EmptyDecompileData("No Function");
private final DecompilerClipboardProvider clipboard;
@@ -117,8 +119,8 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
setDecompileData(new EmptyDecompileData("No Function"));
}
- public HighlightTokenObservableList<HighlightToken> getHighlightedTokens() {
- return highlightedTokens;
+ public TokenHighlights getHighlightedTokens() {
+ return secondaryHighlights;
}
public List<ClangLine> getLines() {
@@ -137,8 +139,8 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
* This function replace the highlighting tokens with the new list
* @param newTokens the new data
*/
- public void setHighlightedTokens(HighlightTokenObservableList<HighlightToken> newTokens) {
- highlightedTokens = newTokens;
+ public void setHighlightedTokens(TokenHighlights newTokens) {
+ secondaryHighlights = newTokens;
}
@Override
@@ -439,7 +441,7 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
layoutMgr = null;
decompilerHoverProvider.dispose();
-
+ secondaryHighlights.clear();
}
private FontMetrics getFontMetrics(DecompileOptions decompileOptions) {
@@ -478,7 +480,7 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
}
if (buttonState == currentMiddleMouseHighlightButton && clickCount == 1) {
- highlightVariable(location, field, currentHighlightColor);
+ setPrimaryHighlight(location, field, currentHighlightColor);
}
}
@@ -637,52 +639,35 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
}
}
- private void highlightVariable(FieldLocation location, Field field, Color highlightColor) {
- if (highlightController != null) {
- ClangToken token = ((ClangTextField) field).getToken(location);
- List<ClangToken> tokenList = new ArrayList<>();
- findTokensByName(tokenList, layoutMgr.getRoot(), token.getText());
- highlightController.clearHighlights();
- highlightController.addTokensToHighlights(tokenList, highlightColor);
- repaint();
- }
+ private void setPrimaryHighlight(FieldLocation location, Field field, Color highlightColor) {
+ ClangToken token = ((ClangTextField) field).getToken(location);
+ Supplier<List<ClangToken>> lazyTokens = () -> findTokensByName(token.getText());
+
+ clearPrimaryHighlights();
+ highlightController.addPrimaryHighlights(lazyTokens, highlightColor);
+ 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
+ * 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 token the token to search for
- * @param highlightColor the color to apply
+ * @param highlight the new highlight
*/
- public void addTokenHighlight(ClangToken token, Color highlightColor) {
- if (highlightController != null) {
- List<ClangToken> tokenList = new ArrayList<>();
- findTokensByName(tokenList, layoutMgr.getRoot(), token.getText());
- highlightController.addTokensToHighlights(tokenList, highlightColor);
- repaint();
- }
+ private void addSecondaryHighlight(HighlightToken highlight) {
+ ClangToken token = highlight.getToken();
+ Supplier<List<ClangToken>> lazyTokens = () -> findTokensByName(token.getText());
+ highlightController.addPrimaryHighlights(lazyTokens, highlight.getColor());
+ repaint();
}
- /**
- * Add highlighting to all the tokens in highlightedTokens,
- * the highlighting accounting structure
- */
- public void repaintHighlightTokens(boolean clearHighlights) {
- if (clearHighlights) {
- clearHighlights();
- }
- highlightedTokens.forEach(ht -> {
- ClangToken token = ht.getToken();
- Function tokenFunc = ht.getFunction();
- Function decompiledFunc = decompileData.getFunction();
- if ((token != null) && (tokenFunc != null) && (decompiledFunc != null) &&
- (tokenFunc.getName().equals(decompileData.getFunction().getName()))) {
- addTokenHighlight(token, ht.getColor());
- }
- });
- tokenHighlightsChanged();
+ public void rebuildSecondaryHighlightTokens() {
+
+ Function function = decompileData.getFunction();
+ Collection<HighlightToken> highlights = secondaryHighlights.get(function);
+ highlights.forEach(this::addSecondaryHighlight);
+
+ repaint();
}
Program getProgram() {
@@ -704,12 +689,7 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
return;
}
- if (highlightController != null) {
- highlightController.fieldLocationChanged(location, field, trigger);
-
- // We need this because fieldLocationChanged clears the highlighting.
- repaintHighlightTokens(false);
- }
+ highlightController.fieldLocationChanged(location, field, trigger);
if (!(field instanceof ClangTextField)) {
return;
@@ -796,19 +776,21 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
// End Search Methods
//==================================================================================================
- public Color getDefaultHighlightColor() {
+ public Color getPrimaryHighlightColor() {
return currentHighlightColor;
}
- public Color getDefaultSpecialColor() {
+ /**
+ * The color used in a primary highlight to mark the token that was clicked. This is used
+ * in 'slice' actions to mark the source of the slice.
+ * @return the color
+ */
+ public Color getSpecialHighlightColor() {
return SPECIAL_COLOR_DEF;
}
public String getHighlightedText() {
- if (highlightController != null) {
- return highlightController.getHighlightedText();
- }
- return null;
+ return highlightController.getHighlightedText();
}
public FieldLocation getCursorPosition() {
@@ -872,38 +854,52 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
return decompilerHoverProvider.isShowing();
}
+ // TODO delete and replace with clearStandardHiglights();
public void clearHighlights() {
- if (highlightController != null) {
- highlightController.clearHighlights();
- }
+ clearAllHighlights();
}
- public void addVarnodeHighlights(Set<Varnode> varnodes, Color highlightColor,
- Varnode specificvn, PcodeOp specificop, Color specialColor) {
- if (highlightController != null) {
- ClangTokenGroup root = layoutMgr.getRoot();
- highlightController.addVarnodesToHighlight(root, varnodes, highlightColor, specificvn,
- specificop, specialColor);
- }
+ public void clearAllHighlights() {
+ // TODO
+ highlightController.clearAllHighlights();
+ repaint();
}
- public void addPcodeOpHighlights(Set<PcodeOp> ops, Color highlightColor) {
- if (highlightController != null) {
- ClangTokenGroup root = layoutMgr.getRoot();
- highlightController.addPcodeOpsToHighlight(root, ops, highlightColor);
- }
+ public void clearPrimaryHighlights() {
+ highlightController.clearPrimaryHighlights();
+ }
+
+ public void addVarnodeHighlights(Set<Varnode> varnodes,
+ TokenHighlightColorProvider colorProvider) {
+
+ ClangTokenGroup root = layoutMgr.getRoot();
+ highlightController.addPrimaryHighlights(root, colorProvider);
+ }
+
+ public void addPcodeOpHighlights(Set<PcodeOp> ops, Color hlColor) {
+ ClangTokenGroup root = layoutMgr.getRoot();
+ highlightController.addPrimaryHighlights(root, ops, hlColor);
+ }
+
+ public List<ClangToken> findTokensByName(String name) {
+ List<ClangToken> tokens = new ArrayList<>();
+ doFindTokensByName(tokens, layoutMgr.getRoot(), name);
+ return tokens;
}
- private void findTokensByName(List<ClangToken> tokenList, ClangTokenGroup group, String name) {
+ private void doFindTokensByName(List<ClangToken> tokens, ClangTokenGroup group, String name) {
+
+ // TODO is 'name' sufficient, or will that allow for conflicts
+
for (int i = 0; i < group.numChildren(); ++i) {
ClangNode child = group.Child(i);
if (child instanceof ClangTokenGroup) {
- findTokensByName(tokenList, (ClangTokenGroup) child, name);
+ doFindTokensByName(tokens, (ClangTokenGroup) child, name);
}
else if (child instanceof ClangToken) {
ClangToken token = (ClangToken) child;
if (name.equals(token.getText())) {
- tokenList.add(token);
+ tokens.add(token);
}
}
}
@@ -937,13 +933,15 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
currentHighlightColor = decompilerOptions.getMiddleMouseHighlightColor();
currentMiddleMouseHighlightButton = decompilerOptions.getMiddleMouseHighlightButton();
currentSearchHighlightColor = decompilerOptions.getSearchHighlightColor();
- if (highlightController != null) {
- highlightController.loadOptions(decompilerOptions);
- }
+ highlightController.loadOptions(decompilerOptions);
}
public void setHighlightController(ClangHighlightController highlightController) {
- this.highlightController = highlightController;
+ if (this.highlightController != null) {
+ this.highlightController.removeListener(this);
+ }
+
+ this.highlightController = ClangHighlightController.dummyIfNull(highlightController);
highlightController.loadOptions(options);
highlightController.addListener(this);
}
@@ -953,6 +951,15 @@ 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
@@ -962,21 +969,20 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
* @param name the new name of the token
*/
public void tokenRenamed(ClangToken token, String name) {
- HighlightToken htoken = new HighlightToken(token, null, null);
- int idx = highlightedTokens.indexOf(htoken);
- if (idx >= 0) {
- HighlightToken hltok = highlightedTokens.get(idx);
- highlightedTokens.remove(hltok);
- ClangToken ct = new ClangToken((ClangNode)token, name);
- HighlightToken ht = new HighlightToken(ct, hltok.getColor(), null);
- highlightedTokens.add(ht);
- if (highlightController != null) {
- List<ClangToken> tokenList = new ArrayList<>();
- findTokensByName(tokenList, layoutMgr.getRoot(), name);
- highlightController.addTokensToHighlights(tokenList, getDefaultHighlightColor());
- repaint();
- }
+ HighlightToken oldHighlight = secondaryHighlights.get(token);
+ if (oldHighlight == null) {
+ return;
}
+
+ secondaryHighlights.remove(oldHighlight);
+ ClangToken ct = new ClangToken(token, name);
+
+ // 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();
}
//==================================================================================================
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/HighlightToken.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/HighlightToken.java
index a3c50a598e..3d16368367 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/HighlightToken.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/HighlightToken.java
@@ -1,78 +1,45 @@
+/* ###
+ * 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 ghidra.app.decompiler.ClangFunction;
import ghidra.app.decompiler.ClangToken;
-import ghidra.app.decompiler.ClangVariableToken;
import ghidra.program.model.listing.Function;
+// TODO better name: TokenHighlight
public class HighlightToken {
+
private ClangToken token;
private Color color;
- private DecompilerPanel panel;
- public HighlightToken(ClangToken token, Color color, Function func) {
+ public HighlightToken(ClangToken token, Color color) {
this.token = token;
this.color = color;
}
- /**
- * Compares two HighlightTokens.
- *
- * Two HighlightTokens are equal if their text is equal
- * and if they reside in the same function.
- */
- @Override
- public boolean equals(Object obj) {
- ClangToken otherToken = null;
-
- if (obj.getClass() == HighlightToken.class) {
- HighlightToken hltoken = (HighlightToken)obj;
- otherToken = hltoken.token;
- }
-
- if (obj.getClass() == ClangToken.class) {
- otherToken = ((ClangToken) obj);
- }
-
- if (obj.getClass() == ClangVariableToken.class) {
- otherToken = ((ClangToken) obj);
- }
-
- if (otherToken == null) {
- return false;
- }
- Function func1 = otherToken.getClangFunction().getHighFunction().getFunction();
- Function func2 = this.token.getClangFunction().getHighFunction().getFunction();
- return otherToken.getText().equals(this.token.getText()) &&
- (func1.equals(func2));
- }
-
public ClangToken getToken() {
return token;
}
- public void setToken(ClangToken token) {
- this.token = token;
- }
-
public Color getColor() {
return color;
}
- public void setColor(Color color) {
- this.color = color;
- }
-
- public DecompilerPanel getPanel() {
- return panel;
- }
-
- public void setPanel(DecompilerPanel panel) {
- this.panel = panel;
- }
-
public Function getFunction() {
if (token == null) {
return null;
@@ -83,4 +50,9 @@ public class HighlightToken {
}
return clangFunc.getHighFunction().getFunction();
}
+
+ @Override
+ public String toString() {
+ return token.toString() + "; color=" + color;
+ }
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/HighlightTokenObservableList.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/HighlightTokenObservableList.java
deleted file mode 100644
index 25dce3983e..0000000000
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/HighlightTokenObservableList.java
+++ /dev/null
@@ -1,49 +0,0 @@
-package ghidra.app.decompiler.component;
-
-import java.util.ArrayList;
-
-public class HighlightTokenObservableList<T> extends ArrayList<T> {
- private ArrayList<DecompilerPanel> panels = new ArrayList<DecompilerPanel>();
-
- public HighlightTokenObservableList(DecompilerPanel panel) {
- super();
- panels.add(panel);
- }
-
- public HighlightTokenObservableList() {
- super();
- }
-
- @Override
- public boolean add(T t){
- boolean rslt = super.add(t);
- notifyListeners();
- return rslt;
- }
-
- @Override
- public void clear() {
- super.clear();
- notifyListeners();
- }
-
- @Override
- public boolean remove(Object t){
- boolean rslt = super.remove(t);
- notifyListeners();
- return rslt;
- }
-
- public void addListener(DecompilerPanel panel) {
- this.panels.add(panel);
- }
-
- public void notifyListeners() {
- if (panels != null) {
- panels.forEach(p -> {
- p.repaintHighlightTokens(true);
- p.repaint();
- });
- }
- }
-}
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 af36c5094a..4a07b38fb7 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
@@ -15,49 +15,37 @@
*/
package ghidra.app.decompiler.component;
-import ghidra.app.decompiler.ClangSyntaxToken;
-import ghidra.app.decompiler.ClangToken;
import docking.widgets.EventTrigger;
import docking.widgets.fieldpanel.field.Field;
import docking.widgets.fieldpanel.support.FieldLocation;
+import ghidra.app.decompiler.ClangSyntaxToken;
+import ghidra.app.decompiler.ClangToken;
/**
* Class to handle location based highlights for a decompiled function.
*/
-
public class LocationClangHighlightController extends ClangHighlightController {
- public LocationClangHighlightController() {
- }
-
@Override
public void fieldLocationChanged(FieldLocation location, Field field, EventTrigger trigger) {
+ clearPrimaryHighlights();
+
if (!(field instanceof ClangTextField)) {
- clearHighlights();
return;
}
ClangToken tok = ((ClangTextField) field).getToken(location);
if (tok == null) {
- clearHighlights();
- return;
- }
-
-// // clear any highlighted searchResults
-// decompilerPanel.setSearchResults(null);
-
- // Token is already highlighted, break out
- if (tok.getHighlight() != null) {
return;
}
- // Token is not highlighted, clear and re-apply highlighting
- clearHighlights();
+ // TODO revisit cursor location highlight
+ // TODO it should probably darken the color
- addHighlight(tok, defaultHighlightColor);
+ addPrimaryHighlight(tok, defaultHighlightColor);
if (tok instanceof ClangSyntaxToken) {
- addHighlightParen((ClangSyntaxToken) tok, defaultParenColor);
+ addPrimaryHighlightToTokensForParenthesis((ClangSyntaxToken) tok, defaultParenColor);
addHighlightBrace((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
new file mode 100644
index 0000000000..2fb8886836
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/NullClangHighlightController.java
@@ -0,0 +1,118 @@
+/* ###
+ * 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.Collection;
+import java.util.Set;
+import java.util.function.Supplier;
+
+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;
+
+public class NullClangHighlightController extends ClangHighlightController {
+
+ @Override
+ public void fieldLocationChanged(FieldLocation location, Field field, EventTrigger trigger) {
+ // 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;
+ }
+
+ @Override
+ public void addPrimaryHighlights(ClangNode parentNode,
+ TokenHighlightColorProvider colorProvider) {
+ // stub
+ }
+
+ @Override
+ public void addPrimaryHighlights(ClangNode parentNode, Set<PcodeOp> ops, Color highlightColor) {
+ // stub
+ }
+
+ @Override
+ public void addPrimaryHighlights(Supplier<? extends Collection<ClangToken>> tokens,
+ Color highlightColor) {
+ // stub
+ }
+
+ @Override
+ public void clearAllHighlights() {
+ // stub
+ }
+
+ @Override
+ public void clearHighlight(ClangToken clangToken) {
+ // stub
+ }
+
+ @Override
+ public void addHighlightBrace(ClangSyntaxToken token, Color highlightColor) {
+ // stub
+ }
+
+ @Override
+ public void addListener(ClangHighlightListener listener) {
+ // stub
+ }
+
+ @Override
+ public void removeListener(ClangHighlightListener listener) {
+ // stub
+ }
+}
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
new file mode 100644
index 0000000000..f49b77e0f4
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenHighlights.java
@@ -0,0 +1,229 @@
+/* ###
+ * 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.*;
+
+import org.apache.commons.collections4.map.LazyMap;
+
+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) {
+
+ TokenHighlights newHighlights = new TokenHighlights(otherPanel);
+
+ Map<TokenKey, HighlightToken> highlightsByToken = highlightsByFunction.get(function);
+ Map<TokenKey, HighlightToken> newHighlightsByToken = new HashMap<>(highlightsByToken);
+ newHighlights.highlightsByFunction.put(function, newHighlightsByToken);
+ return newHighlights;
+ }
+
+ private Map<TokenKey, HighlightToken> getHighlightsByToken(HighlightToken t) {
+ return highlightsByFunction.get(getFunction(t.getToken()));
+ }
+
+ private Map<TokenKey, HighlightToken> getHighlightsByToken(ClangToken t) {
+ return highlightsByFunction.get(getFunction(t));
+ }
+
+ private Function getFunction(ClangToken t) {
+
+ // TODO verify that we can always get a function
+
+ ClangFunction cFunction = t.getClangFunction();
+ if (cFunction == null) {
+ return null;
+ }
+
+ HighFunction highFunction = cFunction.getHighFunction();
+ if (highFunction == null) {
+ return null;
+ }
+ return highFunction.getFunction();
+ }
+
+ public void add(HighlightToken t) {
+ Function f = t.getFunction();
+ Map<TokenKey, HighlightToken> highlightsByToken = highlightsByFunction.get(f);
+ highlightsByToken.put(new TokenKey(t), t);
+ notifyListeners();
+ }
+
+ public HighlightToken get(ClangToken t) {
+ Map<TokenKey, HighlightToken> highlightsByToken = getHighlightsByToken(t);
+ TokenKey key = new TokenKey(t);
+ return highlightsByToken.get(key);
+ }
+
+ public Collection<HighlightToken> get(Function f) {
+ return highlightsByFunction.get(f).values();
+ }
+
+ public void clear() {
+ highlightsByFunction.clear();
+ notifyListeners();
+ }
+
+ public boolean contains(HighlightToken t) {
+ TokenKey key = new TokenKey(t);
+ Map<TokenKey, HighlightToken> highlightsByToken = getHighlightsByToken(t);
+ return highlightsByToken.containsKey(key);
+ }
+
+ public void remove(HighlightToken t) {
+ TokenKey key = new TokenKey(t);
+ Map<TokenKey, HighlightToken> highlightsByToken = getHighlightsByToken(t);
+ highlightsByToken.remove(key);
+ notifyListeners();
+ }
+
+ public void remove(Function function) {
+ highlightsByFunction.remove(function);
+ notifyListeners();
+ }
+
+ // TODO remove this
+ public void notifyListeners() {
+
+ //
+ // 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
+ //
+
+ panel.rebuildSecondaryHighlightTokens();
+ }
+
+ @Override
+ public String toString() {
+ return highlightsByFunction.values().toString();
+ }
+
+//==================================================================================================
+// Inner Classes
+//==================================================================================================
+
+ private class TokenKey {
+ private ClangToken token;
+
+ TokenKey(ClangToken token) {
+ this.token = Objects.requireNonNull(token);
+ }
+
+ public TokenKey(HighlightToken t) {
+ this(t.getToken());
+ }
+
+ @Override
+ public int hashCode() {
+ String text = token.getText();
+ return text == null ? 0 : text.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+
+ ClangToken otherToken = ((TokenKey) obj).token;
+ if (token.getClass() != otherToken.getClass()) {
+ return false;
+ }
+
+ if (!Objects.equals(token.getText(), otherToken.getText())) {
+ return false;
+ }
+
+ ClangLine lineParent = token.getLineParent();
+ ClangLine otherLineParent = otherToken.getLineParent();
+ if (!sameLines(lineParent, otherLineParent)) {
+ return false;
+ }
+ if (lineParent == null) {
+ return false;
+ }
+
+ int positionInLine = lineParent.indexOfToken(token);
+ int otherPositionInLine = otherLineParent.indexOfToken(otherToken);
+ return positionInLine == otherPositionInLine;
+ }
+
+ private boolean sameLines(ClangLine l1, ClangLine l2) {
+
+ if (l1 == null) {
+ if (l2 != null) {
+ return false;
+ }
+ return true;
+ }
+ else if (l2 == null) {
+ return false;
+ }
+
+ return l1.getLineNumber() == l2.getLineNumber();
+ }
+
+ @Override
+ public String toString() {
+ 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/DecompilePlugin.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilePlugin.java
index 9c063f51a3..884e839200 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilePlugin.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilePlugin.java
@@ -20,7 +20,6 @@ import java.util.*;
import org.jdom.Element;
import ghidra.app.CorePluginPackage;
-import ghidra.app.decompiler.component.DecompilerHighlightService;
import ghidra.app.decompiler.component.hover.DecompilerHoverService;
import ghidra.app.events.*;
import ghidra.app.plugin.PluginCategoryNames;
@@ -49,7 +48,6 @@ import ghidra.util.task.SwingUpdateManager;
GoToService.class, NavigationHistoryService.class, ClipboardService.class,
DataTypeManagerService.class /*, ProgramManager.class */
},
- servicesProvided = { DecompilerHighlightService.class },
eventsConsumed = {
ProgramActivatedPluginEvent.class, ProgramOpenedPluginEvent.class,
ProgramLocationPluginEvent.class, ProgramSelectionPluginEvent.class,
@@ -83,12 +81,6 @@ public class DecompilePlugin extends Plugin {
disconnectedProviders = new ArrayList<>();
connectedProvider = new PrimaryDecompilerProvider(this);
-
- registerServices();
- }
-
- private void registerServices() {
- registerServiceProvided(DecompilerHighlightService.class, connectedProvider);
}
@Override
@@ -102,10 +94,6 @@ public class DecompilePlugin extends Plugin {
}
}
- /**
- * Tells the Plugin to write any data-dependent state to the
- * output stream.
- */
@Override
public void writeDataState(SaveState saveState) {
if (connectedProvider != null) {
@@ -128,11 +116,6 @@ public class DecompilePlugin extends Plugin {
}
}
- /**
- * Read data state; called after readConfigState(). Events generated
- * by plugins we depend on should have been already been thrown by the
- * time this method is called.
- */
@Override
public void readDataState(SaveState saveState) {
ProgramManager programManagerService = tool.getService(ProgramManager.class);
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerActionContext.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerActionContext.java
index 9d4b948ff3..5be4eca9c6 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerActionContext.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerActionContext.java
@@ -23,7 +23,9 @@ import ghidra.app.context.NavigatableActionContext;
import ghidra.app.context.RestrictedAddressSetContext;
import ghidra.app.decompiler.component.DecompilerPanel;
import ghidra.program.model.address.Address;
+import ghidra.program.model.listing.Function;
import ghidra.util.Msg;
+import ghidra.util.UndefinedFunction;
import utility.function.Callback;
public class DecompilerActionContext extends NavigatableActionContext
@@ -55,6 +57,15 @@ public class DecompilerActionContext extends NavigatableActionContext
return getComponentProvider().getDecompilerPanel();
}
+ public Function getFunction() {
+ return getComponentProvider().getController().getFunction();
+ }
+
+ public boolean hasRealFunction() {
+ Function f = getFunction();
+ return f != null && !(f instanceof UndefinedFunction);
+ }
+
/**
* The companion method of {@link #checkActionEnablement(Supplier)}. Decompiler actions
* must call this method from their {@link DockingActionIf#actionPerformed(ActionContext)}
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 0da7df2319..16dd370acb 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
@@ -25,7 +25,6 @@ import javax.swing.*;
import docking.*;
import docking.action.*;
-import docking.widgets.fieldpanel.LayoutModel;
import docking.widgets.fieldpanel.support.FieldLocation;
import docking.widgets.fieldpanel.support.ViewerPosition;
import ghidra.GhidraOptions;
@@ -53,8 +52,7 @@ import resources.Icons;
import resources.ResourceManager;
public class DecompilerProvider extends NavigatableComponentProviderAdapter
- implements DomainObjectListener, OptionsChangeListener, DecompilerCallbackHandler,
- DecompilerHighlightService {
+ implements DomainObjectListener, OptionsChangeListener, DecompilerCallbackHandler {
final static String OPTIONS_TITLE = "Decompiler";
private static Icon REFRESH_ICON = Icons.REFRESH_ICON;
@@ -84,6 +82,13 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
private DockingAction renameFunctionAction;
private DockingAction findReferencesAction;
+ private CloneDecompilerAction cloneDecompilerAction;
+
+ private SelectAllAction selectAllAction;
+
+ private ToggleSecondaryHighlightAction setHighlightAction;
+ private RemoveSecondaryHighlightsAction removePanelHighlightsAction;
+
private final DecompilePlugin plugin;
private ClipboardService clipboardService;
private DecompilerClipboardProvider clipboardProvider;
@@ -97,17 +102,9 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
private DecoratorPanel decorationPanel;
private ClangHighlightController highlightController;
- private CloneDecompilerAction cloneDecompilerAction;
-
- private SelectAllAction selectAllAction;
-
- private SetHighlightAction setHighlightAction;
- private RemovePanelHighlightsAction removePanelHighlightsAction;
- private RemoveAllHighlightsAction removeAllHighlightsAction;
-
private ViewerPosition pendingViewerPosition;
- private SwingUpdateManager redecompilerUpdater;
+ private SwingUpdateManager redecompileUpdater;
private ServiceListener serviceListener = new ServiceListener() {
@Override
@@ -159,7 +156,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
setHelpLocation(new HelpLocation(plugin.getName(), "Decompiler"));
addToTool();
- redecompilerUpdater = new SwingUpdateManager(500, 5000, () -> doRefresh());
+ redecompileUpdater = new SwingUpdateManager(500, 5000, () -> doRefresh());
plugin.getTool().addServiceListener(serviceListener);
}
@@ -291,7 +288,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
controller.resetDecompiler();
}
- redecompilerUpdater.update();
+ redecompileUpdater.update();
}
@@ -315,16 +312,6 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
return;
}
- // SetHighlightAction was toggled on/off - notify the handler
- if ((setHighlightAction != null) && optionName.equals(setHighlightAction.getName())) {
- setHighlightAction.setEnabled((boolean)newValue);
- removePanelHighlightsAction.setEnabled((boolean) newValue);
- removeAllHighlightsAction.setEnabled((boolean) newValue);
- if ((boolean)newValue == false) {
- setHighlightAction.reset();
- }
- }
-
if (options.getName().equals(OPTIONS_TITLE) ||
options.getName().equals(GhidraOptions.CATEGORY_BROWSER_FIELDS)) {
doRefresh();
@@ -346,7 +333,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
public void dispose() {
super.dispose();
- redecompilerUpdater.dispose();
+ redecompileUpdater.dispose();
if (clipboardService != null) {
clipboardService.deRegisterClipboardContentProvider(clipboardProvider);
@@ -471,6 +458,10 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
decompilerPanel.setCursorPosition(location);
}
+ DecompilerController getController() {
+ return controller;
+ }
+
//==================================================================================================
// methods called from the controller
//==================================================================================================
@@ -632,13 +623,16 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
newProvider.controller.setDecompileData(controller.getDecompileData());
// Any change in the HighlightTokens should be delivered to the new panel
- getDecompilerPanel().getHighlightedTokens().addListener(newProvider.getDecompilerPanel());
+ DecompilerPanel panel = getDecompilerPanel();
+ TokenHighlights highlightedTokens = panel.getHighlightedTokens();
- // Transfer the highlighted tokens
- newProvider.getDecompilerPanel().setHighlightedTokens(getDecompilerPanel().getHighlightedTokens());
+ DecompilerPanel newPanel = newProvider.getDecompilerPanel();
+ TokenHighlights copiedTokens =
+ highlightedTokens.copyHighlights(newPanel, controller.getFunction());
- newProvider.setLocation(currentLocation,
- controller.getDecompilerPanel().getViewerPosition());
+ // Transfer the highlighted tokens
+ newPanel.setHighlightedTokens(copiedTokens);
+ newProvider.setLocation(currentLocation, panel.getViewerPosition());
});
}
@@ -792,27 +786,25 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
String highlightGroup = "4 - Highlight Group";
subGroupPosition = 0; // reset for the next group
- setHighlightAction = new SetHighlightAction(owner, controller, decompilerOptions.isHighlightIncluded());
+ //
+ // Highlight
+ //
+ setHighlightAction = new ToggleSecondaryHighlightAction();
setGroupInfo(setHighlightAction, highlightGroup, subGroupPosition++);
- removePanelHighlightsAction = new RemovePanelHighlightsAction(owner, controller, decompilerOptions.isHighlightIncluded());
+ removePanelHighlightsAction = new RemoveSecondaryHighlightsAction();
setGroupInfo(removePanelHighlightsAction, highlightGroup, subGroupPosition++);
- removeAllHighlightsAction = new RemoveAllHighlightsAction(owner, controller, decompilerOptions.isHighlightIncluded());
- setGroupInfo(removeAllHighlightsAction, highlightGroup, subGroupPosition++);
-
//
// Comments
//
// NOTE: this is just a placeholder to represent where the comment actions should appear
- // in relation to our local actions. We cannot control the comment action
- // arrangement by setting the values, we can
+ // in relation to our local actions.
//
//
// Search
//
-
String searchGroup = "comment2 - Search Group";
subGroupPosition = 0; // reset for the next group
@@ -870,7 +862,6 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
addLocalAction(renameVarAction);
addLocalAction(setHighlightAction);
addLocalAction(removePanelHighlightsAction);
- addLocalAction(removeAllHighlightsAction);
addLocalAction(retypeVarAction);
addLocalAction(decompilerCreateStructureAction);
tool.addAction(listingCreateStructureAction);
@@ -957,16 +948,6 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
}
- @Override
- public LayoutModel getLayoutModel() {
- return controller.getDecompilerPanel().getLayoutModel();
- }
-
- @Override
- public void clearHighlights() {
- controller.getDecompilerPanel().clearHighlights();
- }
-
public void programClosed(Program closedProgram) {
controller.programClosed(closedProgram);
}
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 21e97161a6..4d9f7a2836 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
@@ -46,15 +46,18 @@ public class BackwardsSliceAction extends AbstractDecompilerAction {
DecompilerPanel decompilerPanel = controller.getDecompilerPanel();
ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();
Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);
- if (varnode != null) {
- PcodeOp op = tokenAtCursor.getPcodeOp();
- Set<Varnode> backwardSlice = DecompilerUtils.getBackwardSlice(varnode);
- decompilerPanel.clearHighlights();
- decompilerPanel.addVarnodeHighlights(backwardSlice,
- decompilerPanel.getDefaultHighlightColor(), varnode, op,
- decompilerPanel.getDefaultSpecialColor());
- decompilerPanel.repaint();
+ if (varnode == null) {
+ return;
}
+
+ decompilerPanel.clearHighlights();
+
+ 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 ae3a4c126a..0f8c9e8b97 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
@@ -53,7 +53,7 @@ public class BackwardsSliceToPCodeOpsAction extends AbstractDecompilerAction {
backwardSlice.add(op);
decompilerPanel.clearHighlights();
decompilerPanel.addPcodeOpHighlights(backwardSlice,
- decompilerPanel.getDefaultHighlightColor());
+ decompilerPanel.getPrimaryHighlightColor());
decompilerPanel.repaint();
}
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitLocalsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitLocalsAction.java
index 06c91889e8..f4580eebc5 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitLocalsAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitLocalsAction.java
@@ -22,12 +22,10 @@ import ghidra.app.decompiler.component.DecompilerController;
import ghidra.app.decompiler.component.DecompilerPanel;
import ghidra.app.plugin.core.decompile.DecompilerActionContext;
import ghidra.framework.plugintool.PluginTool;
-import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
import ghidra.program.model.pcode.HighFunction;
import ghidra.program.model.pcode.HighFunctionDBUtil;
import ghidra.program.model.symbol.SourceType;
-import ghidra.util.UndefinedFunction;
public class CommitLocalsAction extends AbstractDecompilerAction {
private final DecompilerController controller;
@@ -55,8 +53,7 @@ public class CommitLocalsAction extends AbstractDecompilerAction {
@Override
protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {
- Function function = controller.getFunction();
- if (function == null || function instanceof UndefinedFunction) {
+ if (!context.hasRealFunction()) {
return false;
}
return getHighFunction() != null;
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitParamsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitParamsAction.java
index b00fd7ed94..6f3dd6bed6 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitParamsAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitParamsAction.java
@@ -25,13 +25,11 @@ import ghidra.app.decompiler.component.DecompilerController;
import ghidra.app.decompiler.component.DecompilerPanel;
import ghidra.app.plugin.core.decompile.DecompilerActionContext;
import ghidra.framework.plugintool.PluginTool;
-import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
import ghidra.program.model.pcode.HighFunction;
import ghidra.program.model.pcode.HighFunctionDBUtil;
import ghidra.program.model.symbol.SourceType;
import ghidra.util.Msg;
-import ghidra.util.UndefinedFunction;
import ghidra.util.exception.*;
public class CommitParamsAction extends AbstractDecompilerAction {
@@ -65,9 +63,7 @@ public class CommitParamsAction extends AbstractDecompilerAction {
@Override
protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {
-
- Function function = controller.getFunction();
- if (function == null || function instanceof UndefinedFunction) {
+ if (!context.hasRealFunction()) {
return false;
}
return getHighFunction() != null;
@@ -83,7 +79,7 @@ public class CommitParamsAction extends AbstractDecompilerAction {
if (hfunc.getFunction().getSignatureSource() == SourceType.USER_DEFINED) {
source = SourceType.USER_DEFINED;
}
-
+
HighFunctionDBUtil.commitReturnToDatabase(hfunc, source);
HighFunctionDBUtil.commitParamsToDatabase(hfunc, true, source);
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceAction.java
index 8ba4a8a8ee..25add9c3dd 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceAction.java
@@ -46,15 +46,17 @@ public class ForwardSliceAction extends AbstractDecompilerAction {
DecompilerPanel decompilerPanel = controller.getDecompilerPanel();
ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();
Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);
- if (varnode != null) {
- PcodeOp op = tokenAtCursor.getPcodeOp();
- Set<Varnode> forwardSlice = DecompilerUtils.getForwardSlice(varnode);
- decompilerPanel.clearHighlights();
- decompilerPanel.addVarnodeHighlights(forwardSlice,
- decompilerPanel.getDefaultHighlightColor(), varnode, op,
- decompilerPanel.getDefaultSpecialColor());
- decompilerPanel.repaint();
+ if (varnode == null) {
+ return;
}
+
+ PcodeOp op = tokenAtCursor.getPcodeOp();
+ Set<Varnode> forwardSlice = DecompilerUtils.getForwardSlice(varnode);
+ decompilerPanel.clearPrimaryHighlights();
+
+ SliceHighlightColorProvider colorProvider =
+ new SliceHighlightColorProvider(decompilerPanel, forwardSlice, varnode, op);
+ decompilerPanel.addVarnodeHighlights(forwardSlice, colorProvider);
}
}
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 aaab730b1f..ed7d418641 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
@@ -53,7 +53,7 @@ public class ForwardSliceToPCodeOpsAction extends AbstractDecompilerAction {
forwardSlice.add(op);
decompilerPanel.clearHighlights();
decompilerPanel.addPcodeOpHighlights(forwardSlice,
- decompilerPanel.getDefaultHighlightColor());
+ decompilerPanel.getPrimaryHighlightColor());
decompilerPanel.repaint();
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/HighlightDefinedUseAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/HighlightDefinedUseAction.java
index 231ee51afa..486bf89d22 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/HighlightDefinedUseAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/HighlightDefinedUseAction.java
@@ -15,12 +15,13 @@
*/
package ghidra.app.plugin.core.decompile.actions;
-import java.util.HashSet;
+import java.util.Set;
import docking.action.MenuData;
import ghidra.app.decompiler.ClangToken;
import ghidra.app.decompiler.component.*;
import ghidra.app.plugin.core.decompile.DecompilerActionContext;
+import ghidra.program.model.pcode.PcodeOp;
import ghidra.program.model.pcode.Varnode;
public class HighlightDefinedUseAction extends AbstractDecompilerAction {
@@ -45,15 +46,17 @@ public class HighlightDefinedUseAction extends AbstractDecompilerAction {
DecompilerPanel decompilerPanel = controller.getDecompilerPanel();
ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();
Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);
- if (varnode != null) {
- HashSet<Varnode> varnodes = new HashSet<Varnode>();
- varnodes.add(varnode);
- decompilerPanel.clearHighlights();
- decompilerPanel.addVarnodeHighlights(varnodes,
- decompilerPanel.getDefaultHighlightColor(), varnode, varnode.getDef(),
- decompilerPanel.getDefaultSpecialColor());
- decompilerPanel.repaint();
+ if (varnode == null) {
+ return;
}
+
+ decompilerPanel.clearPrimaryHighlights();
+
+ Set<Varnode> varnodes = Set.of(varnode);
+ PcodeOp op = varnode.getDef();
+ SliceHighlightColorProvider colorProvider =
+ new SliceHighlightColorProvider(decompilerPanel, varnodes, varnode, op);
+ decompilerPanel.addVarnodeHighlights(varnodes, colorProvider);
}
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveAllHighlightsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveAllHighlightsAction.java
deleted file mode 100644
index 90e54a3382..0000000000
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveAllHighlightsAction.java
+++ /dev/null
@@ -1,63 +0,0 @@
-package ghidra.app.plugin.core.decompile.actions;
-
-import docking.ActionContext;
-import docking.action.DockingAction;
-import docking.action.MenuData;
-import ghidra.app.decompiler.component.DecompilerController;
-import ghidra.app.decompiler.component.DecompilerPanel;
-import ghidra.app.decompiler.component.HighlightToken;
-import ghidra.app.decompiler.component.HighlightTokenObservableList;
-import ghidra.app.plugin.core.decompile.DecompilerActionContext;
-import ghidra.app.util.HelpTopics;
-import ghidra.program.model.listing.Function;
-import ghidra.util.HelpLocation;
-import ghidra.util.UndefinedFunction;
-
-public class RemoveAllHighlightsAction extends DockingAction {
- private DecompilerPanel panel;
- private DecompilerController controller;
-
- public RemoveAllHighlightsAction(String owner, DecompilerController controller) {
- this(owner, controller, false);
- }
-
- public RemoveAllHighlightsAction(String owner, DecompilerController controller, boolean isEnabled) {
- super("Remove All Highlights", owner);
- this.panel = controller.getDecompilerPanel();
- this.controller = controller;
-
- setPopupMenuData(new MenuData(new String[] { "Remove All Highlights" }, "Decompile"));
- setHelpLocation(new HelpLocation(HelpTopics.SELECTION, getName()));
-
- setEnabled(isEnabled);
- }
-
- @Override
- public boolean isEnabledForContext(ActionContext context) {
- if (!(context instanceof DecompilerActionContext)) {
- return false;
- }
-
- Function function = controller.getFunction();
- if (function instanceof UndefinedFunction) {
- return false;
- }
-
- DecompilerActionContext decompilerActionContext = (DecompilerActionContext) context;
- if (decompilerActionContext.isDecompiling()) {
- return false;
- }
-
- return true;
- }
-
- @Override
- public void actionPerformed(ActionContext context) {
- if (panel != null) {
- HighlightTokenObservableList<HighlightToken> highlightTokens = panel.getHighlightedTokens();
- highlightTokens.clear();
- panel.clearHighlights();
- panel.tokenHighlightsChanged();
- }
- }
-}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemovePanelHighlightsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemovePanelHighlightsAction.java
deleted file mode 100644
index 6aa49b5a1c..0000000000
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemovePanelHighlightsAction.java
+++ /dev/null
@@ -1,111 +0,0 @@
-package ghidra.app.plugin.core.decompile.actions;
-
-import java.util.Iterator;
-
-import docking.ActionContext;
-import docking.action.DockingAction;
-import docking.action.MenuData;
-import ghidra.app.decompiler.ClangFunction;
-import ghidra.app.decompiler.ClangToken;
-import ghidra.app.decompiler.component.DecompilerController;
-import ghidra.app.decompiler.component.DecompilerPanel;
-import ghidra.app.decompiler.component.HighlightToken;
-import ghidra.app.decompiler.component.HighlightTokenObservableList;
-import ghidra.app.plugin.core.decompile.DecompilerActionContext;
-import ghidra.app.util.HelpTopics;
-import ghidra.program.model.listing.Function;
-import ghidra.program.model.pcode.HighFunction;
-import ghidra.util.HelpLocation;
-import ghidra.util.UndefinedFunction;
-
-public class RemovePanelHighlightsAction extends DockingAction{
-
- private DecompilerPanel panel;
- private DecompilerController controller;
-
- public RemovePanelHighlightsAction(String owner, DecompilerController controller) {
- this(owner, controller, false);
- }
-
- public RemovePanelHighlightsAction(String owner, DecompilerController controller, boolean isEnabled) {
- super("Remove Highlights", owner);
- this.panel = controller.getDecompilerPanel();
- this.controller = controller;
-
- setPopupMenuData(new MenuData(new String[] { "Remove Panel Highlights" }, "Decompile"));
- setHelpLocation(new HelpLocation(HelpTopics.SELECTION, getName()));
-
- setEnabled(isEnabled);
- }
-
- @Override
- public boolean isEnabledForContext(ActionContext context) {
- if (!(context instanceof DecompilerActionContext)) {
- return false;
- }
-
- Function function = controller.getFunction();
- if (function instanceof UndefinedFunction) {
- return false;
- }
-
- DecompilerActionContext decompilerActionContext = (DecompilerActionContext) context;
- if (decompilerActionContext.isDecompiling()) {
- return false;
- }
-
- return true;
- }
-
- @Override
- public void actionPerformed(ActionContext context) {
- if (panel != null) {
- ClangToken tokenAtCursor = panel.getTokenAtCursor();
- removeTokensFromFunction(tokenAtCursor.getClangFunction());
- panel.clearHighlights();
- panel.tokenHighlightsChanged();
- }
- }
-
- private Function getFunctionFromToken(ClangToken token) {
- if (token == null) {
- return null;
- }
- ClangFunction curClangFunction = token.getClangFunction();
- if (curClangFunction == null) {
- return null;
- }
- HighFunction curHighFunc = curClangFunction.getHighFunction();
- if (curHighFunc == null) {
- return null;
- }
- return curHighFunc.getFunction();
- }
-
- private void removeTokensFromFunction(ClangFunction function) {
- if (panel != null && function != null) {
- HighlightTokenObservableList<HighlightToken> highlightTokens = panel.getHighlightedTokens();
- ClangToken curToken = panel.getTokenAtCursor();
-
- Function curFunction = getFunctionFromToken(curToken);
-
- if (curFunction == null) {
- return;
- }
-
- Iterator<HighlightToken> it = highlightTokens.iterator();
-
- while (it.hasNext()) {
- HighlightToken tok = it.next();
- Function iterFunction = getFunctionFromToken(tok.getToken());
- if (iterFunction.equals(curFunction)) {
- it.remove();
- }
- }
-
- highlightTokens.notifyListeners();
-
- }
- }
-
-}
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
new file mode 100644
index 0000000000..4988d350f2
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightsAction.java
@@ -0,0 +1,49 @@
+/* ###
+ * 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.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 {
+
+ public static final String NAME = "Remove Secondary Highlights";
+
+ public RemoveSecondaryHighlightsAction() {
+ super(NAME);
+
+ setPopupMenuData(new MenuData(new String[] { NAME }, "Decompile"));
+ setHelpLocation(new HelpLocation(HelpTopics.SELECTION, getName()));
+ }
+
+ @Override
+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {
+ return context.hasRealFunction();
+ }
+
+ @Override
+ protected void decompilerActionPerformed(DecompilerActionContext context) {
+ Function function = context.getFunction();
+ DecompilerPanel panel = context.getDecompilerPanel();
+ TokenHighlights highlightTokens = panel.getHighlightedTokens();
+ highlightTokens.remove(function);
+ }
+}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetHighlightAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetHighlightAction.java
deleted file mode 100644
index 3e24a35166..0000000000
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetHighlightAction.java
+++ /dev/null
@@ -1,133 +0,0 @@
-package ghidra.app.plugin.core.decompile.actions;
-
-import java.awt.Color;
-import java.awt.event.KeyEvent;
-
-import docking.ActionContext;
-import docking.action.DockingAction;
-import docking.action.KeyBindingData;
-import docking.action.MenuData;
-import ghidra.app.decompiler.ClangToken;
-import ghidra.app.decompiler.component.DecompilerController;
-import ghidra.app.decompiler.component.DecompilerPanel;
-import ghidra.app.decompiler.component.HighlightToken;
-import ghidra.app.decompiler.component.HighlightTokenObservableList;
-import ghidra.app.plugin.core.decompile.DecompilerActionContext;
-import ghidra.app.util.HelpTopics;
-import ghidra.program.model.listing.Function;
-import ghidra.program.model.pcode.HighVariable;
-import ghidra.util.HelpLocation;
-import ghidra.util.UndefinedFunction;
-
-public class SetHighlightAction extends DockingAction {
- private DecompilerPanel panel;
- private DecompilerController controller;
-
- private int minColorSaturation = 100;
- private int defaultColorAlpha = 100;
-
- public SetHighlightAction(String owner, DecompilerController controller) {
- this(owner, controller, false);
- }
-
- /**
- * Initialize the menu items, hotkeys and set the option to enabled according
- * to the decompileOptions
- */
- public SetHighlightAction(String owner, DecompilerController controller, boolean isEnabled) {
- super("Display.Custom Highlights", owner, false);
- this.panel = controller.getDecompilerPanel();
- this.controller = controller;
-
- setPopupMenuData(new MenuData(new String[] { "Highlight Token" }, "Decompile"));
- setKeyBindingData(new KeyBindingData(KeyEvent.VK_C, 0));
- setHelpLocation(new HelpLocation(HelpTopics.SELECTION, getName()));
-
- setEnabled(isEnabled);
- }
-
- /**
- * Reset the highlighting accounting - This removes all highlighting.
- */
- public void reset() {
- if (panel != null) {
- panel.getHighlightedTokens().clear();
- panel.tokenHighlightsChanged();
- }
- }
-
- @Override
- public boolean isEnabledForContext(ActionContext context) {
- if (!(context instanceof DecompilerActionContext)) {
- return false;
- }
-
- Function function = controller.getFunction();
- if (function instanceof UndefinedFunction) {
- return false;
- }
-
- DecompilerActionContext decompilerActionContext = (DecompilerActionContext) context;
- if (decompilerActionContext.isDecompiling()) {
- return false;
- }
-
- ClangToken tokenAtCursor = panel.getTokenAtCursor();
- if (tokenAtCursor == null) {
- return false;
- }
-
- HighVariable variable = tokenAtCursor.getHighVariable();
- tokenAtCursor.getSyntaxType();
- if (variable == null) {
- return false;
- }
-
- return true;
- }
-
- @Override
- public boolean setEnabled(boolean newValue) {
-
- if (newValue == false) {
- panel.repaintHighlightTokens(true);
- }
-
- return super.setEnabled(newValue);
- }
-
- @Override
- public void actionPerformed(ActionContext context) {
- if (panel != null) {
- ClangToken tokenAtCursor = panel.getTokenAtCursor();
-
- HighlightTokenObservableList<HighlightToken> highlightTokens = panel.getHighlightedTokens();
-
- Function func = controller.getDecompileData().getFunction();
-
- Color highlightColor = getNewColor();
- HighlightToken htoken = new HighlightToken(tokenAtCursor, highlightColor, func);
-
- // If token was already highlighted (in the list), we remove it.
- if (highlightTokens.remove(htoken)) {
- panel.clearHighlights();
- panel.setHighlightedTokens(highlightTokens);
- panel.repaintHighlightTokens(true);
- panel.tokenHighlightsChanged();
- } else {
- // Token was not in the list, this is a new request to set highlighting
- highlightTokens.add(htoken);
- panel.setHighlightedTokens(highlightTokens);
- panel.addTokenHighlight(tokenAtCursor, highlightColor);
- panel.tokenHighlightsChanged();
- }
- }
- }
-
- private Color getNewColor() {
- return new Color((int)(minColorSaturation + Math.random() * (256 - minColorSaturation)),
- (int)(minColorSaturation + Math.random() * (256 - minColorSaturation)),
- (int)(minColorSaturation + Math.random() * (256 - minColorSaturation)), defaultColorAlpha);
- }
-
-}
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
new file mode 100644
index 0000000000..f53a3cfdf9
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SliceHighlightColorProvider.java
@@ -0,0 +1,64 @@
+/* ###
+ * 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.plugin.core.decompile.actions;
+
+import java.awt.Color;
+import java.util.Set;
+
+import ghidra.app.decompiler.ClangToken;
+import ghidra.app.decompiler.component.DecompilerPanel;
+import ghidra.app.decompiler.component.DecompilerUtils;
+import ghidra.program.model.pcode.PcodeOp;
+import ghidra.program.model.pcode.Varnode;
+
+// TODO docme
+public class SliceHighlightColorProvider implements TokenHighlightColorProvider {
+
+ private Set<Varnode> varnodes;
+ private Varnode specialVn;
+ private PcodeOp specialOp;
+ private Color hlColor;
+ private Color specialHlColor;
+
+ SliceHighlightColorProvider(DecompilerPanel panel, Set<Varnode> varnodes, Varnode specialVn,
+ PcodeOp specialOp) {
+ this.varnodes = varnodes;
+ this.specialVn = specialVn;
+ this.specialOp = specialOp;
+
+ hlColor = panel.getPrimaryHighlightColor();
+ specialHlColor = panel.getSpecialHighlightColor();
+ }
+
+ @Override
+ public Color getColor(ClangToken token) {
+
+ Varnode vn = DecompilerUtils.getVarnodeRef(token);
+ if (varnodes.contains(vn)) {
+ return hlColor;
+ }
+
+ if (specialOp == null) {
+ return null;
+ }
+
+ // look for specific varnode to label with special color
+ if (vn == specialVn && token.getPcodeOp() == specialOp) {
+ return specialHlColor;
+ }
+ return null;
+ }
+}
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
new file mode 100644
index 0000000000..9e5bdbf221
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ToggleSecondaryHighlightAction.java
@@ -0,0 +1,96 @@
+/* ###
+ * 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.plugin.core.decompile.actions;
+
+import java.awt.Color;
+import java.awt.event.KeyEvent;
+
+import docking.action.KeyBindingData;
+import docking.action.MenuData;
+import ghidra.app.decompiler.ClangToken;
+import ghidra.app.decompiler.component.*;
+import ghidra.app.plugin.core.decompile.DecompilerActionContext;
+import ghidra.app.util.HelpTopics;
+import ghidra.program.model.pcode.HighVariable;
+import ghidra.util.HelpLocation;
+
+public class ToggleSecondaryHighlightAction extends AbstractDecompilerAction {
+
+ public static String NAME = "Highlight Token";
+
+ public ToggleSecondaryHighlightAction() {
+ super(NAME);
+
+ setPopupMenuData(new MenuData(new String[] { "Highlight Toggle" }, "Decompile"));
+
+ // TODO new key binding: 'h'
+ // TODO how about mouse actions?
+ setKeyBindingData(new KeyBindingData(KeyEvent.VK_C, 0));
+
+ // TODO new help
+ setHelpLocation(new HelpLocation(HelpTopics.SELECTION, getName()));
+ }
+
+ @Override
+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {
+ if (!context.hasRealFunction()) {
+ return false;
+ }
+
+ DecompilerPanel panel = context.getDecompilerPanel();
+ ClangToken tokenAtCursor = panel.getTokenAtCursor();
+ if (tokenAtCursor == null) {
+ return false;
+ }
+
+ //
+ // TODO Deal Breaker?? How to disable this action for Diff Decompilers??
+ //
+ // OR, just let it work
+ //
+
+ HighVariable variable = tokenAtCursor.getHighVariable();
+
+ // TODO does not work if behind a variable name; works in front
+
+ // TODO restrict to variable, or allow any text?
+ return variable != null;
+ }
+
+ @Override
+ 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);
+
+ }
+ }
+}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerHighlightService.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/TokenHighlightColorProvider.java
similarity index 66%
rename from Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerHighlightService.java
rename to Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/TokenHighlightColorProvider.java
index 990fae0641..17c0152d38 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerHighlightService.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/TokenHighlightColorProvider.java
@@ -13,14 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package ghidra.app.decompiler.component;
+package ghidra.app.plugin.core.decompile.actions;
-import docking.widgets.fieldpanel.LayoutModel;
+import java.awt.Color;
-public interface DecompilerHighlightService {
-
- public LayoutModel getLayoutModel();
-
- public void clearHighlights();
+import ghidra.app.decompiler.ClangToken;
+/**
+ * Provides highlight color for the given token
+ */
+public interface TokenHighlightColorProvider {
+
+ // TODO null if no match; d0cme
+ public Color getColor(ClangToken token);
}
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 9381d7cbe7..a369aee9be 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
@@ -69,6 +69,7 @@ public abstract class AbstractDecompilerTest extends AbstractProgramBasedTest {
}
protected void waitForDecompiler() {
+ waitForSwing();
waitForCondition(() -> !provider.isDecompiling());
waitForSwing();
}
@@ -89,7 +90,7 @@ public abstract class AbstractDecompilerTest extends AbstractProgramBasedTest {
}
protected FieldLocation loc(int lineNumber, int col) {
- FieldLocation loc = new FieldLocation(lineNumber, 0, 0, col);
+ FieldLocation loc = new FieldLocation(lineNumber - 1, 0, 0, col);
return loc;
}
@@ -97,20 +98,47 @@ public abstract class AbstractDecompilerTest extends AbstractProgramBasedTest {
DecompilerPanel panel = provider.getDecompilerPanel();
List<Field> fields = panel.getFields();
- Field line = fields.get(lineNumber - 1); // 0-based
+ Field line = fields.get(lineNumber - 1); // -1 for 1-based line number
return (ClangTextField) line;
}
- protected String getTokenText(FieldLocation loc) {
- ClangTextField field = getFieldForLine(loc.getIndex().intValue());
+ protected ClangToken getToken(int line, int col) {
+ FieldLocation loc = loc(line, col);
+ ClangTextField field = getFieldForLine(line);
+ ClangToken token = field.getToken(loc);
+ return token;
+ }
+
+ protected ClangToken getToken(FieldLocation loc) {
+ int lineNumber = loc.getIndex().intValue() + 1; // 0-based
+ ClangTextField field = getFieldForLine(lineNumber);
ClangToken token = field.getToken(loc);
+ return token;
+ }
+
+ protected DecompilerPanel getDecompilerPanel() {
+ return provider.getDecompilerPanel();
+ }
+
+ /**
+ * Returns the token under the cursor
+ * @return the token under the cursor
+ */
+ protected ClangToken getToken() {
+ DecompilerPanel panel = getDecompilerPanel();
+ FieldLocation loc = panel.getCursorPosition();
+ return getToken(loc);
+ }
+
+ protected String getTokenText(FieldLocation loc) {
+ ClangToken token = getToken(loc);
return token.getText();
}
protected void assertToken(String tokenText, int line, int... cols) {
for (int col : cols) {
- FieldLocation loc = loc(line, col);
- String text = getTokenText(loc);
+ ClangToken token = getToken(line, col);
+ String text = token.getText();
assertEquals(tokenText, text);
}
}
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 6eee08b88a..59e673b3a8 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
@@ -15,18 +15,22 @@
*/
package ghidra.app.plugin.core.decompile;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
+import java.awt.Color;
+import java.util.List;
import java.util.Set;
import org.junit.Test;
import docking.action.DockingActionIf;
+import docking.widgets.dialogs.InputDialog;
import docking.widgets.fieldpanel.support.FieldLocation;
import ghidra.app.cmd.comments.SetCommentCmd;
-import ghidra.app.decompiler.component.ClangTextField;
-import ghidra.app.decompiler.component.DecompilerPanel;
+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.program.model.listing.CodeUnit;
public class DecompilerClangTest extends AbstractDecompilerTest {
@@ -143,7 +147,7 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
assertEquals("void", copiedText);
line = 5; // _printf
- charPosition = 2; //
+ charPosition = 2;
setDecompilerLocation(line, charPosition);
copy();
@@ -151,10 +155,341 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
assertEquals("_printf", copiedText);
}
+ @Test
+ public void testMultiColorHighlighting() {
+
+ /*
+
+ 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();
+
+ assertAllFieldsHighlighted(text, color);
+
+ // 5:30 "a->name"
+ line = 5;
+ charPosition = 38;
+ setDecompilerLocation(line, charPosition);
+ ClangToken token2 = getToken();
+ String text2 = token2.getText();
+ assertEquals("name", text2);
+
+ Color color2 = highlight();
+ assertAllFieldsHighlighted(text, color);
+ assertAllFieldsHighlighted(text2, color2);
+
+ // 2:1 "void"
+ line = 2;
+ charPosition = 1;
+ setDecompilerLocation(line, charPosition);
+ ClangToken token3 = getToken();
+ String text3 = token3.getText();
+ assertEquals("void", text3);
+
+ Color color3 = highlight();
+ assertAllFieldsHighlighted(text, color);
+ assertAllFieldsHighlighted(text2, color2);
+ assertAllFieldsHighlighted(text3, color3);
+ }
+
+ @Test
+ public void testMultiColorHighlighting_ClearHighlight_WithMultipleHighlights() {
+
+ /*
+
+ 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);
+
+ highlight();
+
+ // 5:30 "a->name"
+ line = 5;
+ charPosition = 38;
+ setDecompilerLocation(line, charPosition);
+ ClangToken token2 = getToken();
+ String text2 = token2.getText();
+ assertEquals("name", text2);
+
+ Color color2 = highlight();
+
+ // 5:2 "_printf"
+ line = 5;
+ charPosition = 2;
+ setDecompilerLocation(line, charPosition);
+ clearHighlight();
+
+ Color noHighlight = null;
+ assertAllFieldsHighlighted(text, noHighlight);
+ assertAllFieldsHighlighted(text2, color2);
+ }
+
+ @Test
+ public void testMultiColorHighlighting_ClearAll() {
+
+ /*
+
+ 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);
+
+ highlight();
+
+ // 5:30 "a->name"
+ line = 5;
+ charPosition = 38;
+ setDecompilerLocation(line, charPosition);
+ ClangToken token2 = getToken();
+ String text2 = token2.getText();
+ assertEquals("name", text2);
+
+ highlight();
+
+ clearAllHighlights();
+
+ Color noHighlight = null;
+ assertAllFieldsHighlighted(text, noHighlight);
+ assertAllFieldsHighlighted(text2, noHighlight);
+ }
+
+ @Test
+ public void testMultiColorHighlighting_RenameHighlightedVariable() {
+
+ /*
+
+ 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'
+
+ // 2:26 "a"
+ int line = 2;
+ int charPosition = 26;
+ setDecompilerLocation(line, charPosition);
+
+ ClangToken token = getToken();
+ String text = token.getText();
+ assertEquals("a", text);
+
+ Color color = highlight();
+
+ rename("bob");
+
+ token = getToken();
+ text = token.getText();
+ assertEquals("bob", text);
+ assertAllFieldsHighlighted(text, color);
+ }
+
+ @Test
+ public void testMultiColorHighlighting_HighlightColorGetsReused() {
+
+ /*
+
+ 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();
+
+ clearHighlight();
+
+ Color noHighlight = null;
+ assertAllFieldsHighlighted(text, noHighlight);
+
+ Color secondColor = highlight();
+ assertEquals(color, secondColor);
+ }
+
+ // 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
+
+ // TODO test highlights passed to clone
+
+ // TODO allow for any text, not just variables?=
+ // TODO - follow-up test that 2 variables with the same name do not get highlighted
+
+ // TODO Energy
+ // -highlight tokens between parens
+
//==================================================================================================
// Private Methods
//==================================================================================================
+ private void rename(String newName) {
+ DockingActionIf action = getAction(decompiler, "Rename Variable");
+ performAction(action, false);
+
+ InputDialog dialog = waitForDialogComponent(InputDialog.class);
+ runSwing(() -> dialog.setValue(newName));
+
+ pressButtonByText(dialog, "OK");
+ waitForDecompiler();
+ }
+
+ private void clearAllHighlights() {
+
+ DockingActionIf highlightAction = getAction(decompiler, RemoveSecondaryHighlightsAction.NAME);
+ performAction(highlightAction);
+ }
+
+ private Color highlight() {
+
+ ClangToken token = getToken();
+
+ DockingActionIf highlightAction = getAction(decompiler, ToggleSecondaryHighlightAction.NAME);
+ performAction(highlightAction);
+
+ DecompilerController controller = provider.getController();
+ DecompilerPanel panel = controller.getDecompilerPanel();
+ TokenHighlights highlights = panel.getHighlightedTokens();
+ HighlightToken ht = highlights.get(token);
+ assertNotNull("No highlight for token: " + token, ht);
+ return ht.getColor();
+ }
+
+ private void clearHighlight() {
+
+ ClangToken token = getToken();
+
+ DockingActionIf highlightAction = getAction(decompiler, ToggleSecondaryHighlightAction.NAME);
+ performAction(highlightAction);
+
+ DecompilerController controller = provider.getController();
+ DecompilerPanel panel = controller.getDecompilerPanel();
+ TokenHighlights highlights = panel.getHighlightedTokens();
+ HighlightToken ht = highlights.get(token);
+ assertNull("Token should not be highlighted: " + token, ht);
+ }
+
+ private void assertAllFieldsHighlighted(String name, Color color) {
+ 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());
+ }
+ }
+
private void copy() {
Set<DockingActionIf> actions = getActionsByOwnerAndName(tool, "ClipboardPlugin", "Copy");
diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerFindReferencesToNestedStructureActionTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerFindReferencesToNestedStructureActionTest.java
index a0b6af182d..e891ba9b47 100644
--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerFindReferencesToNestedStructureActionTest.java
+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/DecompilerFindReferencesToNestedStructureActionTest.java
@@ -48,10 +48,10 @@ public class DecompilerFindReferencesToNestedStructureActionTest
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| }
- 13|
+ 10| _call_structure_B(&a->b);
+ 11| return;
+ 12| }
+ 13|
*/
decompile(CALL_STRUCTURE_A_ADDRESS);
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/dialogs/InputDialog.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/dialogs/InputDialog.java
index 6e2efae6f7..9ff8d037de 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/dialogs/InputDialog.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/dialogs/InputDialog.java
@@ -56,21 +56,6 @@ public class InputDialog extends DialogComponentProvider {
this(dialogTitle, new String[] { label }, new String[] { DEFAULT_VALUE }, true, null);
}
- /**
- * Creates a generic input dialog with the specified title, a text field,
- * labeled by the specified label. The user should check the value of
- * "isCanceled()" to know whether or not the user canceled the operation.
- * Otherwise, use the "getValue()" or "getValues()" to get the value(s)
- * entered by the user. Use the tool's "showDialog()" to display the dialog.
- * <P>
- * @param dialogTitle used as the name of the dialog's title bar
- * @param label value to use for the label of the text field
- * @param listener listener that is called when the OK button is hit
- */
- public InputDialog(String dialogTitle, String label, InputDialogListener listener) {
- this(dialogTitle, new String[] { label }, new String[] { DEFAULT_VALUE }, true, listener);
- }
-
/**
* Creates a generic input dialog with the specified title, a text field,
* labeled by the specified label. The user should check the value of
@@ -96,27 +81,13 @@ public class InputDialog extends DialogComponentProvider {
* @param dialogTitle used as the name of the dialog's title bar
* @param label value to use for the label of the text field
* @param initialValue initial value to use for the text field
+ * @param listener the dialog listener (may be null)
*/
public InputDialog(String dialogTitle, String label, String initialValue,
InputDialogListener listener) {
this(dialogTitle, new String[] { label }, new String[] { initialValue }, true, listener);
}
- /**
- * Creates a generic input dialog with the specified title, a text field,
- * labeled by the specified label. The user should check the value of
- * "isCanceled()" to know whether or not the user canceled the operation.
- * Otherwise, use the "getValue()" or "getValues()" to get the value(s)
- * entered by the user. Use the tool's "showDialog()" to display the dialog.
- * <P>
- * @param dialogTitle used as the name of the dialog's title bar
- * @param label values to use for the label of the text field
- * @param isModal whether or not the dialog is to be modal
- */
- public InputDialog(String dialogTitle, String label, boolean isModal) {
- this(dialogTitle, new String[] { label }, new String[] { DEFAULT_VALUE }, isModal, null);
- }
-
/**
* Creates a generic input dialog with the specified title, a text field,
* labeled by the specified label. The user should check the value of
@@ -163,8 +134,8 @@ public class InputDialog extends DialogComponentProvider {
*/
public InputDialog(String dialogTitle, String[] labels, String[] initialValues, boolean isModal,
InputDialogListener listener) {
- super(dialogTitle, isModal, (listener != null), // status area needed?
- true, false); // do need button panel
+ super(dialogTitle, isModal, (listener != null) /* status */, true /* buttons */,
+ false /* no tasks */);
this.listener = listener;
// create the key listener all the text fields will use
@@ -253,40 +224,36 @@ public class InputDialog extends DialogComponentProvider {
close();
}
- //***************************************************************************
- //* API methods
- //**************************************************************************/
-
/**
- * Returns if this dialog is cancelled.
+ * Returns if this dialog is cancelled
+ * @return true if cancelled
*/
public boolean isCanceled() {
return isCanceled;
}
/**
- * return the value of the first (and maybe only) text field
+ * Return the value of the first (and maybe only) text field
+ * @return the text field value
*/
public String getValue() {
return inputValues[0];
}
/**
- * return the values for all the text field(s)
+ * Sets the text of the primary text field
+ * @param text the text
*/
- public String[] getValues() {
- return inputValues;
+ public void setValue(String text) {
+ textFields[0].setText(text);
}
/**
- * reset all the text fields to their initial values
+ * Return the values for all the text field(s)
+ * @return the text field values
*/
- public void resetValues() {
- for (int v = 0; v < inputValues.length; v++) {
- String value = initialValues[v];
- inputValues[v] = value;
- textFields[v].setText(value);
- }
+ public String[] getValues() {
+ return inputValues;
}
private class MyTextField extends JTextField {