Skip to content

Commit f4a4cd0

Browse files
committed
Merge branch 'GP-421-dragonmacher-plate-comments-on-structure-fields'
2 parents e293ab2 + 6ea07f8 commit f4a4cd0

File tree

3 files changed

+62
-32
lines changed

3 files changed

+62
-32
lines changed

Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/commentwindow/CommentTableModel.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,30 @@ protected void doLoad(Accumulator<CommentRowObject> accumulator, TaskMonitor mon
8484
listing.getCommentAddressIterator(getProgram().getMemory(), true);
8585
while (commentIterator.hasNext()) {
8686
Address commentAddr = commentIterator.next();
87-
CodeUnit cu = listing.getCodeUnitAt(commentAddr);
88-
if (cu != null) {
89-
if (cu.getComment(CodeUnit.PRE_COMMENT) != null) {
90-
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.PRE_COMMENT));
91-
}
92-
if (cu.getComment(CodeUnit.POST_COMMENT) != null) {
93-
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.POST_COMMENT));
94-
}
95-
if (cu.getComment(CodeUnit.EOL_COMMENT) != null) {
96-
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.EOL_COMMENT));
97-
}
98-
if (cu.getComment(CodeUnit.PLATE_COMMENT) != null) {
99-
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.PLATE_COMMENT));
100-
}
101-
if (cu.getComment(CodeUnit.REPEATABLE_COMMENT) != null) {
102-
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.REPEATABLE_COMMENT));
103-
}
87+
CodeUnit cu = listing.getCodeUnitContaining(commentAddr);
88+
if (!(cu instanceof Data)) {
89+
// avoid too many comments in the table by not showing offcut instruction comments
90+
cu = listing.getCodeUnitAt(commentAddr);
91+
}
92+
93+
if (cu == null) {
94+
continue;
95+
}
96+
97+
if (cu.getComment(CodeUnit.PRE_COMMENT) != null) {
98+
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.PRE_COMMENT));
99+
}
100+
if (cu.getComment(CodeUnit.POST_COMMENT) != null) {
101+
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.POST_COMMENT));
102+
}
103+
if (cu.getComment(CodeUnit.EOL_COMMENT) != null) {
104+
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.EOL_COMMENT));
105+
}
106+
if (cu.getComment(CodeUnit.PLATE_COMMENT) != null) {
107+
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.PLATE_COMMENT));
108+
}
109+
if (cu.getComment(CodeUnit.REPEATABLE_COMMENT) != null) {
110+
accumulator.add(new CommentRowObject(commentAddr, CodeUnit.REPEATABLE_COMMENT));
104111
}
105112
}
106113

Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/PlateFieldFactory.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import ghidra.app.util.viewer.format.FieldFormatModel;
2828
import ghidra.app.util.viewer.listingpanel.ListingModel;
2929
import ghidra.app.util.viewer.options.OptionsGui;
30+
import ghidra.app.util.viewer.proxy.DataProxy;
3031
import ghidra.app.util.viewer.proxy.ProxyObj;
3132
import ghidra.framework.options.Options;
3233
import ghidra.framework.options.ToolOptions;
@@ -126,10 +127,10 @@ private PlateFieldFactory(FieldFormatModel model, HighlightProvider hlProvider,
126127
nLinesBeforeLabels = fieldOptions.getInt(LINES_BEFORE_LABELS_OPTION, 1);
127128
nLinesBeforePlates = fieldOptions.getInt(LINES_BEFORE_PLATES_OPTION, 0);
128129

129-
showExternalFunctionPointerPlates = fieldOptions.getBoolean(
130-
ListingModel.DISPLAY_EXTERNAL_FUNCTION_POINTER_OPTION_NAME, true);
131-
showNonExternalFunctionPointerPlates = fieldOptions.getBoolean(
132-
ListingModel.DISPLAY_NONEXTERNAL_FUNCTION_POINTER_OPTION_NAME, false);
130+
showExternalFunctionPointerPlates = fieldOptions
131+
.getBoolean(ListingModel.DISPLAY_EXTERNAL_FUNCTION_POINTER_OPTION_NAME, true);
132+
showNonExternalFunctionPointerPlates = fieldOptions
133+
.getBoolean(ListingModel.DISPLAY_NONEXTERNAL_FUNCTION_POINTER_OPTION_NAME, false);
133134

134135
}
135136

@@ -160,11 +161,32 @@ public ListingField getField(ProxyObj<?> proxy, int varWidth) {
160161
FieldElement[] fields = new FieldElement[elementList.size()];
161162
elementList.toArray(fields);
162163

164+
if (isNestedDataAtSameAddressAsParent(proxy)) {
165+
// This is data at the same address as the parent, which happens with the first
166+
// element in a structure. We do not want to the plate comment here, but only at the
167+
// parent topmost address.
168+
return null;
169+
}
170+
163171
PlateFieldTextField textField =
164172
new PlateFieldTextField(fields, this, proxy, startX, width, commentText, isClipped);
165173
return new PlateListingTextField(proxy, textField);
166174
}
167175

176+
private boolean isNestedDataAtSameAddressAsParent(ProxyObj<?> proxy) {
177+
if (proxy instanceof DataProxy) {
178+
DataProxy dp = (DataProxy) proxy;
179+
Data data = dp.getObject();
180+
int[] cpath = data.getComponentPath();
181+
if (cpath.length > 0) {
182+
if (cpath[cpath.length - 1] == 0) {
183+
return true;
184+
}
185+
}
186+
}
187+
return false;
188+
}
189+
168190
private String getCommentText(CodeUnit cu) {
169191
String[] comments = cu.getCommentAsArray(CodeUnit.PLATE_COMMENT);
170192
if (comments == null) {
@@ -199,8 +221,8 @@ private boolean generateFormattedPlateComment(List<FieldElement> elementList, Co
199221
AttributedString prototype = new AttributedString(EMPTY_STRING, color, getMetrics());
200222

201223
for (int i = 0; i < comments.length; i++) {
202-
elementList.add(
203-
CommentUtils.parseTextForAnnotations(comments[i], program, prototype, i));
224+
elementList
225+
.add(CommentUtils.parseTextForAnnotations(comments[i], program, prototype, i));
204226
}
205227

206228
if (isWordWrap) {
@@ -499,7 +521,10 @@ public boolean acceptsType(int category, Class<?> proxyObjectClass) {
499521
if (!CodeUnit.class.isAssignableFrom(proxyObjectClass)) {
500522
return false;
501523
}
502-
return (category == FieldFormatModel.PLATE);
524+
525+
// some users like the look of plate comments and would like them in many places
526+
return (category == FieldFormatModel.PLATE || category == FieldFormatModel.OPEN_DATA ||
527+
category == FieldFormatModel.INSTRUCTION_OR_DATA);
503528
}
504529

505530
@Override

Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/PreCommentFieldFactory.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,14 @@ public ListingField getField(ProxyObj<?> proxy, int varWidth) {
120120

121121
private String[] getDefinedPreComments(CodeUnit cu) {
122122

123-
// If this code unit is the outside of a data
124-
// container, then do not display any comments.
125-
// If this was allowed, then the comment would appear
126-
// on the outside data container and on the 1st
127-
// internal member
128-
//
123+
// Do not show comments for nested components that share the same address as their parent
129124
if (cu instanceof Data) {
130125
Data data = (Data) cu;
131-
if (data.getNumComponents() > 0) {
132-
return null;
126+
int[] cpath = data.getComponentPath();
127+
if (cpath.length > 0) {
128+
if (cpath[cpath.length - 1] == 0) {
129+
return null;
130+
}
133131
}
134132
}
135133

0 commit comments

Comments
 (0)