Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1370 - Find References to - Fixed bug that caused markup to appear in the column filter text field

dragonmacher61486201ed5d
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReference.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReference.java
index cd66de726b..d714294b27 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReference.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReference.java
@@ -15,6 +15,8 @@
*/
package ghidra.app.plugin.core.navigation.locationreferences;
+import static ghidra.app.plugin.core.navigation.locationreferences.LocationReferenceContext.*;
+
import java.util.Objects;
import ghidra.program.model.address.Address;
@@ -22,7 +24,7 @@ import ghidra.program.model.symbol.Reference;
import ghidra.program.util.ProgramLocation;
/**
- * A simple container object to provide clients with a reference and an address when both are
+ * A simple container object to provide clients with a reference and an address when both are
* available. If no reference exists, then only the {@link #getLocationOfUse()} address is
* available.
*/
@@ -31,7 +33,7 @@ public class LocationReference implements Comparable<LocationReference> {
private final boolean isOffcutReference;
private final Address locationOfUseAddress;
private final String refType;
- private final String context;
+ private final LocationReferenceContext context;
private final ProgramLocation location;
private int hashCode = -1;
@@ -40,36 +42,41 @@ public class LocationReference implements Comparable<LocationReference> {
return r != null ? r.getReferenceType().getName() : "";
}
- // Note: the address is the location that item backed by this class is used. For
- // references, this represents the 'from' address for a reference; for parameters
- // and variables of a function, this represents the address of that variable.
+ // Note: the address is the location of the item passed to this class. For references, this
+ // represents the 'from' address for a reference; for parameters and variables of a
+ // function, this represents the address of that variable.
private LocationReference(Address address, ProgramLocation location, String refType,
- String context, boolean isOffcut) {
+ LocationReferenceContext context, boolean isOffcut) {
this.locationOfUseAddress = Objects.requireNonNull(address);
this.location = location;
this.refType = refType == null ? "" : refType;
- this.context = context == null ? "" : context;
+ this.context = context == null ? EMPTY_CONTEXT : context;
this.isOffcutReference = isOffcut;
}
LocationReference(Reference reference, boolean isOffcutReference) {
- this(reference.getFromAddress(), null, getRefType(reference), null, isOffcutReference);
+ this(reference.getFromAddress(), null, getRefType(reference), EMPTY_CONTEXT,
+ isOffcutReference);
}
LocationReference(Address locationOfUseAddress, String refType, boolean isOffcutReference) {
- this(locationOfUseAddress, null, refType, null, isOffcutReference);
+ this(locationOfUseAddress, null, refType, EMPTY_CONTEXT, isOffcutReference);
}
LocationReference(Address locationOfUseAddress) {
- this(locationOfUseAddress, null, null, null, false);
+ this(locationOfUseAddress, null, null, EMPTY_CONTEXT, false);
}
LocationReference(Address locationOfUseAddress, String context) {
- this(locationOfUseAddress, null, null, context, false);
+ this(locationOfUseAddress, null, null, LocationReferenceContext.get(context), false);
+ }
+
+ LocationReference(Address locationOfUseAddress, LocationReferenceContext context) {
+ this(locationOfUseAddress, null, null, LocationReferenceContext.get(context), false);
}
LocationReference(Address locationOfUseAddress, String context, ProgramLocation location) {
- this(locationOfUseAddress, location, null, context, false);
+ this(locationOfUseAddress, location, null, LocationReferenceContext.get(context), false);
}
/**
@@ -82,18 +89,18 @@ public class LocationReference implements Comparable<LocationReference> {
/**
* Returns true if the corresponding reference is to an offcut address
- * @return
+ * @return true if offcut
*/
public boolean isOffcutReference() {
return isOffcutReference;
}
/**
- * Returns the address where the item described by this object is used. For example, for
+ * Returns the address where the item described by this object is used. For example, for
* data types, the address is where a data type is applied; for references, this value is the
* <tt>from</tt> address.
*
- * @return the address where the item described by this object is used.
+ * @return the address where the item described by this object is used.
*/
public Address getLocationOfUse() {
return locationOfUseAddress;
@@ -106,7 +113,15 @@ public class LocationReference implements Comparable<LocationReference> {
*
* @return the context
*/
- public String getContext() {
+
+ /**
+ * Returns the context associated with this location. The context may be a simple plain string
+ * or may be String that highlights part of a function signature the location matches or
+ * a line from the Decompiler that matches.
+ *
+ * @return the context
+ */
+ public LocationReferenceContext getContext() {
return context;
}
@@ -175,11 +190,11 @@ public class LocationReference implements Comparable<LocationReference> {
@Override
public String toString() {
//@formatter:off
- return "{\n" +
+ return "{\n" +
"\taddress: " + locationOfUseAddress + ",\n" +
((refType.equals("")) ? "" : "\trefType: " + refType + ",\n") +
"\tisOffcut: " + isOffcutReference + ",\n" +
- ((context.equals("")) ? "" : "\tcontext: " + context + ",") +
+ ((context == EMPTY_CONTEXT) ? "" : "\tcontext: " + context + ",") +
"}";
//@formatter:off
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferenceContext.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferenceContext.java
new file mode 100644
index 0000000000..d85dc2ee37
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferenceContext.java
@@ -0,0 +1,201 @@
+/* ###
+ * 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.navigation.locationreferences;
+
+import java.util.*;
+
+import generic.json.Json;
+import ghidra.util.HTMLUtilities;
+
+/**
+ * A class to hold context representation for {@link LocationReference}s.
+ *
+ * @see LocationReferenceContextBuilder
+ */
+public class LocationReferenceContext {
+
+ private static final String EMBOLDEN_START =
+ "<span style=\"background-color: #a3e4d7; color: black;\"><b><font size=4>";
+ private static final String EMBOLDEN_END = "</font></b></span>";
+
+ public static final LocationReferenceContext EMPTY_CONTEXT = new LocationReferenceContext();
+
+ private final List<Part> parts;
+
+ /**
+ * A factory method to create a context instance with the given text. The context created this
+ * way will have no special HTML formatting applied by {@link #getBoldMatchingText()}, as no
+ * matching parts will be defined.
+ * @param text the text
+ * @return the context
+ */
+ public static LocationReferenceContext get(String text) {
+ return text == null ? EMPTY_CONTEXT : new LocationReferenceContext(text);
+ }
+
+ /**
+ * A factory method to provided as a convenience to handle null context objects.
+ * @param context the context to verify is not null
+ * @return the given context or the {@link #EMPTY_CONTEXT} if the given context is null
+ */
+ public static LocationReferenceContext get(LocationReferenceContext context) {
+ return context == null ? EMPTY_CONTEXT : context;
+ }
+
+ /**
+ * Creates an empty context object
+ */
+ private LocationReferenceContext() {
+ this.parts = List.of(new BasicPart(""));
+ }
+
+ /**
+ * Creates a context with the raw and decorated context being the same.
+ * @param context the context; cannot be null
+ */
+ private LocationReferenceContext(String context) {
+ Objects.requireNonNull(context);
+ this.parts = List.of(new BasicPart(context));
+ }
+
+ /**
+ * Constructor used to create this context by providing the given text parts
+ * @param parts the parts
+ * @see LocationReferenceContextBuilder
+ */
+ LocationReferenceContext(List<Part> parts) {
+ this.parts = parts;
+ }
+
+ /**
+ * The full plain text of this context.
+ * @return the text
+ */
+ public String getPlainText() {
+ StringBuilder buffy = new StringBuilder();
+ for (Part part : parts) {
+ buffy.append(part.getText());
+ }
+ return buffy.toString();
+ }
+
+ /**
+ * Returns HTML text for this context. Any matching items embedded in the returned string will
+ * be bold.
+ * @return the text
+ */
+ public String getBoldMatchingText() {
+ StringBuilder buffy = new StringBuilder();
+ for (Part part : parts) {
+ buffy.append(part.getHtmlText());
+ }
+ return HTMLUtilities.HTML + buffy.toString();
+ }
+
+ /**
+ * Returns any sub-strings of this context's overall text that match client-defined input
+ *
+ * See the {@link LocationReferenceContextBuilder} for how to define matching text pieces
+ * @return the matching strings
+ */
+ public List<String> getMatches() {
+ List<String> matches = new ArrayList<>();
+ for (Part part : parts) {
+ if (part instanceof MatchPart) {
+ matches.add(part.getText());
+ }
+ }
+ return matches;
+ }
+
+ @Override
+ public String toString() {
+ return Json.toString(this);
+ }
+
+ /**
+ * A class that represents one or more characters within the full text of this context class
+ */
+ static abstract class Part {
+ protected String text;
+
+ Part(String text) {
+ this.text = text;
+ }
+
+ String getText() {
+ return text;
+ }
+
+ abstract String getHtmlText();
+
+ abstract String getText(String start, String end);
+
+ static String fixBreakingSpaces(String s) {
+ String updated = s.replaceAll("\\s", "&nbsp;");
+ return updated;
+ }
+
+ @Override
+ public String toString() {
+ return Json.toString(this);
+ }
+ }
+
+ /**
+ * A basic string part that has no decoration
+ */
+ static class BasicPart extends Part {
+
+ BasicPart(String text) {
+ super(text);
+ }
+
+ @Override
+ String getText(String start, String end) {
+ return text; // we don't decorate
+ }
+
+ @Override
+ String getHtmlText() {
+ String escaped = HTMLUtilities.escapeHTML(text);
+ String updated = fixBreakingSpaces(escaped);
+ return updated;
+ }
+ }
+
+ /**
+ * A string part of the overall text of this context that matches client-defined text
+ */
+ static class MatchPart extends Part {
+
+ MatchPart(String text) {
+ super(text);
+ }
+
+ @Override
+ String getText(String start, String end) {
+ return start + text + end;
+ }
+
+ @Override
+ String getHtmlText() {
+ String escaped = HTMLUtilities.escapeHTML(text);
+ String updated = fixBreakingSpaces(escaped);
+ return EMBOLDEN_START + updated + EMBOLDEN_END;
+ }
+ }
+}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferenceContextBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferenceContextBuilder.java
new file mode 100644
index 0000000000..2240778b50
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferenceContextBuilder.java
@@ -0,0 +1,74 @@
+/* ###
+ * 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.navigation.locationreferences;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import generic.json.Json;
+import ghidra.app.plugin.core.navigation.locationreferences.LocationReferenceContext.*;
+
+/**
+ * A builder for {@link LocationReferenceContext} objects. Use {@link #append(String)} for normal
+ * text pieces. Use {@link #appendMatch(String)} for text that is meant to be rendered specially
+ * by the context class.
+ */
+public class LocationReferenceContextBuilder {
+
+ private List<Part> parts = new ArrayList<>();
+
+ /**
+ * Appends the given text to this builder.
+ * @param text the text
+ * @return this builder
+ */
+ public LocationReferenceContextBuilder append(String text) {
+ if (text == null) {
+ text = "";
+ }
+ parts.add(new BasicPart(text));
+ return this;
+ }
+
+ /**
+ * Appends the given text to this builder. This text represents a client-defined 'match' that
+ * will be rendered with markup when {@link LocationReferenceContext#getBoldMatchingText()} is
+ * called.
+ * @param text the text
+ * @return this builder
+ */
+ public LocationReferenceContextBuilder appendMatch(String text) {
+ if (text == null) {
+ throw new NullPointerException("Match text cannot be null");
+ }
+ parts.add(new MatchPart(text));
+ return this;
+ }
+
+ /**
+ * Builds a {@link LocationReferenceContext} using the text supplied via the {@code append}
+ * methods.
+ * @return the context
+ */
+ public LocationReferenceContext build() {
+ return new LocationReferenceContext(parts);
+ }
+
+ @Override
+ public String toString() {
+ return Json.toString(this);
+ }
+}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferencesTableModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferencesTableModel.java
index ffdacee361..3f77196d72 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferencesTableModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/LocationReferencesTableModel.java
@@ -127,23 +127,15 @@ class LocationReferencesTableModel extends AddressBasedTableModel<LocationRefere
//==================================================================================================
private class ContextTableColumn
- extends AbstractProgramBasedDynamicTableColumn<LocationReference, String> {
+ extends
+ AbstractProgramBasedDynamicTableColumn<LocationReference, LocationReference> {
- private static final String OFFCUT_STRING = "<< OFFCUT >>";
- private OffcutTableCellRenderer renderer = new OffcutTableCellRenderer();
+ private ContextCellRenderer renderer = new ContextCellRenderer();
@Override
- public String getValue(LocationReference rowObject, Settings settings, Program p,
+ public LocationReference getValue(LocationReference rowObject, Settings settings, Program p,
ServiceProvider sp) throws IllegalArgumentException {
-
- String refType = rowObject.getRefTypeString();
- if (!StringUtils.isBlank(refType)) {
- return refType + (rowObject.isOffcutReference() ? OFFCUT_STRING : "");
- }
-
- // when the row object does not represent an applied reference, then it may have
- // some context associated with it
- return rowObject.getContext();
+ return rowObject;
}
@Override
@@ -159,12 +151,14 @@ class LocationReferencesTableModel extends AddressBasedTableModel<LocationRefere
}
@Override
- public GColumnRenderer<String> getColumnRenderer() {
+ public GColumnRenderer<LocationReference> getColumnRenderer() {
return renderer;
}
}
- private class OffcutTableCellRenderer extends AbstractGhidraColumnRenderer<String> {
+ private class ContextCellRenderer extends AbstractGhidraColumnRenderer<LocationReference> {
+
+ private static final String OFFCUT_STRING = "<< OFFCUT >>";
{
setHTMLRenderingEnabled(true);
@@ -176,18 +170,42 @@ class LocationReferencesTableModel extends AddressBasedTableModel<LocationRefere
// initialize
super.getTableCellRendererComponent(data);
- Object value = data.getValue();
-
- if (value.toString().indexOf(ContextTableColumn.OFFCUT_STRING) >= 0) {
- setForeground(Color.RED);
+ LocationReference rowObject = (LocationReference) data.getRowObject();
+ String refTypeString = getRefTypeString(rowObject);
+ if (refTypeString != null) {
+ setText(refTypeString);
+ return this;
}
+ // when the row object does not represent an applied reference, then it may have context
+ LocationReferenceContext context = rowObject.getContext();
+ String text = context.getBoldMatchingText();
+ setText(text);
return this;
}
+ private String getRefTypeString(LocationReference rowObject) {
+ String refType = rowObject.getRefTypeString();
+ if (!StringUtils.isBlank(refType)) {
+ String trailingText = "";
+ if (rowObject.isOffcutReference()) {
+ setForeground(Color.RED);
+ trailingText = OFFCUT_STRING;
+ }
+ return refType + trailingText;
+ }
+ return null;
+ }
+
@Override
- public String getFilterString(String t, Settings settings) {
- return t;
+ public String getFilterString(LocationReference rowObject, Settings settings) {
+ String refTypeString = getRefTypeString(rowObject);
+ if (refTypeString != null) {
+ return refTypeString;
+ }
+
+ LocationReferenceContext context = rowObject.getContext();
+ return context.getPlainText();
}
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/ReferenceUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/ReferenceUtils.java
index 0744d31c07..79e641aae9 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/ReferenceUtils.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/navigation/locationreferences/ReferenceUtils.java
@@ -37,16 +37,6 @@ import ghidra.util.task.TaskMonitor;
public final class ReferenceUtils {
- private static final String EMBOLDEN_START =
- "<span style=\"background-color: #a3e4d7; color: black;\"><b><font size=4>";
- private static final String EMBOLDEN_END = "</font></b></span>";
-
- public static final String CONTEXT_CALLOUT_START = "[|";
- public static final String CONTEXT_CALLOUT_END = "|]";
-
- public static final String CONTEXT_CALLOUT_START_REGEX = "\\[\\|";
- public static final String CONTEXT_CALLOUT_END_REGEX = "\\|\\]";
-
private ReferenceUtils() {
// utility class
}
@@ -79,7 +69,7 @@ public final class ReferenceUtils {
* @param accumulator The Accumulator into which LocationReferences will be placed.
* @param location The location for which to find references
* @param monitor the task monitor used to track progress and cancel the work
- * @throws CancelledException if the operation was cancelled
+ * @throws CancelledException if the operation was cancelled
*/
public static void getReferences(Accumulator<LocationReference> accumulator,
ProgramLocation location, TaskMonitor monitor) throws CancelledException {
@@ -205,8 +195,8 @@ public final class ReferenceUtils {
* @param fieldName optional field name for which to search; the <tt>dataType</tt> must be
* a {@link Composite} to search for a field
* @param program The program from within which to find references.
- * @param discoverTypes if true, the {@link DataTypeReferenceFinder} service
- * will be used to search for data types that are not applied in memory.
+ * @param discoverTypes if true, the {@link DataTypeReferenceFinder} service
+ * will be used to search for data types that are not applied in memory.
* Using the service will be slower, but will recover type usage that could
* not be found by examining the Listing.
* @param monitor A task monitor to be updated as data is searched; if this is null, then a
@@ -249,7 +239,7 @@ public final class ReferenceUtils {
FunctionIterator iterator = listing.getFunctions(false);
findDataTypeMatchesInFunctionHeaders(asSet, iterator, dataType, localsOnly, monitor);
- // external functions don't get searched by type discovery
+ // external functions don't get searched by type discovery
localsOnly = false;
iterator = listing.getExternalFunctions();
findDataTypeMatchesInFunctionHeaders(asSet, iterator, dataType, localsOnly, monitor);
@@ -288,7 +278,7 @@ public final class ReferenceUtils {
Consumer<DataTypeReference> callback = ref -> {
- String context = emboldenBracketedText(ref.getContext());
+ LocationReferenceContext context = ref.getContext();
LocationReference locationReference = new LocationReference(ref.getAddress(), context);
accumulator.add(locationReference);
};
@@ -566,7 +556,7 @@ public final class ReferenceUtils {
}
/*
- * Creates a location descriptor from a field location
+ * Creates a location descriptor from a field location
* (which has more info than just an address). A
* {@link FieldNameFieldLocation} can be tied to a {@link Composite} field or it can
* be an array index subscript.
@@ -631,8 +621,8 @@ public final class ReferenceUtils {
}
/*
- * Creates a location descriptor from just an address (which has less info than an
- * actual location) A {@link FieldNameFieldLocation} can be tied to
+ * Creates a location descriptor from just an address (which has less info than an
+ * actual location) A {@link FieldNameFieldLocation} can be tied to
* a {@link Composite} field or it can be an array index subscript.
*
* Since this method is handed only an address and not a ProgramLocation, we have to do
@@ -656,11 +646,11 @@ public final class ReferenceUtils {
String fieldPath = getFieldPath(location);
if (!fieldPath.contains(".")) {
- // no field reference, so don't create a structure member reference, but just
+ // no field reference, so don't create a structure member reference, but just
// a generic data type reference
DataType type = outermostData.getDataType();
if (type == DataType.DEFAULT || Undefined.isUndefined(type)) {
- // nobody wants to search for undefined usage; too many (this is the case
+ // nobody wants to search for undefined usage; too many (this is the case
// where the user is not on an actual data type)
return null;
}
@@ -672,7 +662,8 @@ public final class ReferenceUtils {
String fieldName = getFieldName(location);
Address parentAddress = outermostData.getMinAddress();
- Data subData = outermostData.getComponentAt((int) refAddress.subtract(parentAddress));
+ int componentAddress = (int) refAddress.subtract(parentAddress);
+ Data subData = outermostData.getComponentContaining(componentAddress);
if (subData != null) {
int[] componentPath = subData.getComponentPath();
@@ -697,7 +688,7 @@ public final class ReferenceUtils {
}
/*
- * Creates a location descriptor using the String display markup and type information
+ * Creates a location descriptor using the String display markup and type information
* found inside of the VariableOffset object.
*
* This method differs from createDataMemberLocationDescriptor() in that this method
@@ -757,7 +748,7 @@ public final class ReferenceUtils {
// field inside of the 'bar' composite type. We need to find the type of 'bar'.
//
// examples: foo.bar.baz
- //
+ //
Stack<String> path = new Stack<>();
for (int i = parts.length - 1; i >= 0; i--) {
path.push(parts[i]);
@@ -803,7 +794,7 @@ public final class ReferenceUtils {
/*
* Finds the data type represented by the lowest-level value in the stack. This is done
- * by using the given parent and the item on the top of the stack to find a matching
+ * by using the given parent and the item on the top of the stack to find a matching
* field (the parent must be a Composite).
*/
private static DataType findLeafDataType(DataType parent, Stack<String> path) {
@@ -848,7 +839,7 @@ public final class ReferenceUtils {
if (reference == null) {
- // Prefer using the reference, for consistency. Without that, the
+ // Prefer using the reference, for consistency. Without that, the
// VariableOffset object contains markup and type information we can use.
// Having a VariableOffset without a reference occurs when a
// register variable reference is inferred during instruction operand formatting.
@@ -867,7 +858,7 @@ public final class ReferenceUtils {
// Using the reference, we can heck for the 'Extended Markup' style reference, such as:
// instruction ...=>Foo.bar.baz
// -------------
- // Note: these references are to labels (not sure why the reference isn't to a data
+ // Note: these references are to labels (not sure why the reference isn't to a data
// symbol)
//
@@ -957,7 +948,7 @@ public final class ReferenceUtils {
* @param dataMatcher the predicate that determines a successful match
* @param fieldName the optional field name for which to search
* @param monitor the task monitor used to track progress and cancel the work
- * @throws CancelledException if the operation was cancelled
+ * @throws CancelledException if the operation was cancelled
*/
public static void findDataTypeMatchesInDefinedData(Accumulator<LocationReference> accumulator,
Program program, Predicate<Data> dataMatcher, String fieldName, TaskMonitor monitor)
@@ -1133,19 +1124,6 @@ public final class ReferenceUtils {
}
}
- private static String emboldenBracketedText(String context) {
- String escaped = HTMLUtilities.escapeHTML(context);
- String updated = fixBreakingSpaces(escaped);
- updated = updated.replaceFirst(CONTEXT_CALLOUT_START_REGEX, EMBOLDEN_START);
- updated = updated.replaceFirst(CONTEXT_CALLOUT_END_REGEX, EMBOLDEN_END);
- return "<html>" + updated;
- }
-
- private static String fixBreakingSpaces(String s) {
- String updated = s.replaceAll("\\s", "&nbsp;");
- return updated;
- }
-
private static boolean dataTypesMatch(DataType searchType, DataType possibleType) {
if (isBuiltIn(searchType)) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/services/DataTypeReference.java b/Ghidra/Features/Base/src/main/java/ghidra/app/services/DataTypeReference.java
index 3aea8e2374..5e40ed861e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/services/DataTypeReference.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/services/DataTypeReference.java
@@ -15,6 +15,7 @@
*/
package ghidra.app.services;
+import ghidra.app.plugin.core.navigation.locationreferences.LocationReferenceContext;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType;
import ghidra.program.model.listing.Function;
@@ -30,10 +31,10 @@ public class DataTypeReference {
private Address address;
/** A preview of how the reference was used */
- private String context;
+ private LocationReferenceContext context;
public DataTypeReference(DataType dataType, String fieldName, Function function,
- Address address, String context) {
+ Address address, LocationReferenceContext context) {
this.dataType = dataType;
this.fieldName = fieldName;
this.function = function;
@@ -52,22 +53,22 @@ public class DataTypeReference {
public Address getAddress() {
return address;
}
-
- public String getContext() {
+
+ public LocationReferenceContext getContext() {
return context;
}
@Override
public String toString() {
String fieldNameText = fieldName == null ? "" : "\tfieldName: " + fieldName + "\n";
-
+
//@formatter:off
- return "{\n" +
+ return "{\n" +
"\tdataType: " + dataType.getName() + "\n" +
- fieldNameText +
+ fieldNameText +
"\tfunction: " + function.getName() + "\n" +
"\taddress: " + address + "\n" +
- "\tcontext: " + context + "\n" +
+ "\tcontext: " + context + "\n" +
"}";
//@formatter:on
}
diff --git a/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/DecompilerReference.java b/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/DecompilerReference.java
index 076ee9c9c7..44c67b600c 100644
--- a/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/DecompilerReference.java
+++ b/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/DecompilerReference.java
@@ -15,11 +15,11 @@
*/
package ghidra.app.extension.datatype.finder;
-import java.util.Arrays;
import java.util.List;
import ghidra.app.decompiler.*;
-import ghidra.app.plugin.core.navigation.locationreferences.ReferenceUtils;
+import ghidra.app.plugin.core.navigation.locationreferences.LocationReferenceContext;
+import ghidra.app.plugin.core.navigation.locationreferences.LocationReferenceContextBuilder;
import ghidra.app.services.DataTypeReference;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.*;
@@ -46,11 +46,11 @@ public abstract class DecompilerReference {
}
/**
- * Scan this reference for any data type matches.
+ * Scan this reference for any data type matches.
* <p>
* The <tt>fieldName</tt> is optional. If not included, then only data type matches will
- * be sought. If it is included, then a match is only included when it is a reference
- * to the given data type where that type is being accessed by the given field name.
+ * be sought. If it is included, then a match is only included when it is a reference
+ * to the given data type where that type is being accessed by the given field name.
*
* @param dt the data type to find
* @param fieldName the optional field name used to restrict matches.
@@ -86,15 +86,25 @@ public abstract class DecompilerReference {
return line;
}
- protected String getContext() {
- String context = getContext(variable);
+ protected LocationReferenceContext getContext() {
+ LocationReferenceContext context = getContext(variable);
return context;
}
- protected String getContext(DecompilerVariable var) {
- String context = line.toDebugString(Arrays.asList(var.variable),
- ReferenceUtils.CONTEXT_CALLOUT_START, ReferenceUtils.CONTEXT_CALLOUT_END);
- return context;
+ protected LocationReferenceContext getContext(DecompilerVariable var) {
+
+ LocationReferenceContextBuilder builder = new LocationReferenceContextBuilder();
+ builder.append(line.getLineNumber() + ": ");
+ List<ClangToken> tokens = line.getAllTokens();
+ for (ClangToken token : tokens) {
+ if (token.equals(var.variable)) {
+ builder.appendMatch(token.getText());
+ }
+ else {
+ builder.append(token.getText());
+ }
+ }
+ return builder.build();
}
// Note: using isEquivalent() allows different data types to match. I don't think we want
diff --git a/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/VariableAccessDR.java b/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/VariableAccessDR.java
index 5f6ab9bbfc..344fe9aa03 100644
--- a/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/VariableAccessDR.java
+++ b/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/VariableAccessDR.java
@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.List;
import ghidra.app.decompiler.*;
+import ghidra.app.plugin.core.navigation.locationreferences.LocationReferenceContext;
import ghidra.app.services.DataTypeReference;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType;
@@ -69,7 +70,7 @@ public class VariableAccessDR extends DecompilerReference {
}
//
- // Walk each pair of type and variables in order to see if any of them match our
+ // Walk each pair of type and variables in order to see if any of them match our
// criteria
//
DecompilerVariable start = variable;
@@ -88,7 +89,7 @@ public class VariableAccessDR extends DecompilerReference {
//
// Handle the last variable by itself (for the case where we are matching just on the
// type, with no field name)
- //
+ //
if (fieldName != null) {
return;
}
@@ -146,10 +147,10 @@ public class VariableAccessDR extends DecompilerReference {
//
// Unusual Code Alert!
// It is a bit odd to check the field when you are looking for the type that contains
- // the field. BUT, in the Decompiler, SOMETIMES the 'field' happens to have the
+ // the field. BUT, in the Decompiler, SOMETIMES the 'field' happens to have the
// data type of the thing that contains it. So, if you have:
// foo.bar
- // then the 'bar' field will have a data type of Foo. Unfortunately, this is not
+ // then the 'bar' field will have a data type of Foo. Unfortunately, this is not
// always the case. For now, when the variable is global, we need to check the field
// Sad face emoji.
//
@@ -191,7 +192,7 @@ public class VariableAccessDR extends DecompilerReference {
protected DataTypeReference createReference(DecompilerVariable var) {
DataType dataType = var.getDataType();
- String context = getContext(var);
+ LocationReferenceContext context = getContext(var);
Function function = var.getFunction();
Address address = getAddress(var);
return new DataTypeReference(dataType, null, function, address, context);
@@ -199,26 +200,26 @@ public class VariableAccessDR extends DecompilerReference {
private DataTypeReference createReference(DecompilerVariable var, DecompilerVariable field) {
DataType dataType = var.getDataType();
- String context = getContext(var);
+ LocationReferenceContext context = getContext(var);
Function function = var.getFunction();
Address address = getAddress(var);
return new DataTypeReference(dataType, field.getName(), function, address, context);
}
@Override
- protected String getContext(DecompilerVariable var) {
+ protected LocationReferenceContext getContext(DecompilerVariable var) {
DecompilerVariable field = findFieldFor(var);
- String context = super.getContext(field);
+ LocationReferenceContext context = super.getContext(field);
return context;
}
private DecompilerVariable findFieldFor(DecompilerVariable var) {
- //
+ //
// Unusual Code Alert!
//
// The fact that we need to locate the given variable is a bit odd. But, elsewhere in
- // this file we figured out which variable (out of the casts, the variable and the
+ // this file we figured out which variable (out of the casts, the variable and the
// accesses) is the type we seek. So, now we have a variable, but we don't know
// if it is the field or if one of the accesses is the field. So, this method walks
// all variables in order to find the given variable, then returns the next variable, as
@@ -270,7 +271,7 @@ public class VariableAccessDR extends DecompilerReference {
"\tline " + getContext() + ",\n" +
"\tfunction: " + getFunction() + "\n" +
"\tvariable: " + StringUtilities.toStringWithIndent(variable) + ",\n" +
- "\tdata type: " + getDataType() + ",\n"+
+ "\tdata type: " + getDataType() + ",\n"+
subFieldsString +
"}";
//@formatter:on
diff --git a/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/VariableDR.java b/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/VariableDR.java
index 4a7c095574..181fdd4c0e 100644
--- a/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/VariableDR.java
+++ b/Ghidra/Features/DecompilerDependent/src/main/java/ghidra/app/extension/datatype/finder/VariableDR.java
@@ -18,6 +18,7 @@ package ghidra.app.extension.datatype.finder;
import java.util.List;
import ghidra.app.decompiler.*;
+import ghidra.app.plugin.core.navigation.locationreferences.LocationReferenceContext;
import ghidra.app.services.DataTypeReference;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType;
@@ -64,7 +65,7 @@ public abstract class VariableDR extends DecompilerReference {
return;
}
- String context = getContext();
+ LocationReferenceContext context = getContext();
Function function = getFunction();
Address address = getAddress();
if (fieldName == null) {