GP-5742 Cleanup preferred CommentType enum use. Changed SARIF data component comment JSON serialization from int to String.

This commit is contained in:
ghidra1
2025-06-06 17:58:07 -04:00
parent 4a65e9af3b
commit 8c441250f5
211 changed files with 4627 additions and 4860 deletions
@@ -4,9 +4,9 @@
* 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.
@@ -548,7 +548,7 @@ public class DecompileCallback {
private void encodeHeaderComment(Encoder encoder, Function func) throws IOException {
Address addr = func.getEntryPoint();
String text = listing.getComment(CodeUnit.PLATE_COMMENT, addr);
String text = listing.getComment(CommentType.PLATE, addr);
if (text != null) {
encoder.openElement(ELEM_COMMENT);
encoder.writeString(ATTRIB_TYPE, "header");
@@ -574,19 +574,19 @@ public class DecompileCallback {
* @throws IOException for errors in the underlying stream
*/
private void encodeCommentsType(Encoder encoder, AddressSetView addrset, Address addr,
int commenttype) throws IOException {
CommentType commenttype) throws IOException {
String typename;
switch (commenttype) {
case CodeUnit.EOL_COMMENT:
case EOL:
typename = "user1";
break;
case CodeUnit.PRE_COMMENT:
case PRE:
typename = "user2";
break;
case CodeUnit.POST_COMMENT:
case POST:
typename = "user3";
break;
case CodeUnit.PLATE_COMMENT:
case PLATE:
typename = "header";
break;
default:
@@ -598,7 +598,7 @@ public class DecompileCallback {
Address commaddr = iter.next();
String text = listing.getComment(commenttype, commaddr);
if (text != null) {
if (commenttype == CodeUnit.PLATE_COMMENT) {
if (commenttype == CommentType.PLATE) {
// Plate comments on the function entry
// address are considered part of the header
if (commaddr.equals(addr)) {
@@ -626,16 +626,16 @@ public class DecompileCallback {
encodeHeaderComment(encoder, func);
}
if ((flags & 1) != 0) {
encodeCommentsType(encoder, addrset, addr, CodeUnit.EOL_COMMENT);
encodeCommentsType(encoder, addrset, addr, CommentType.EOL);
}
if ((flags & 2) != 0) {
encodeCommentsType(encoder, addrset, addr, CodeUnit.PRE_COMMENT);
encodeCommentsType(encoder, addrset, addr, CommentType.PRE);
}
if ((flags & 4) != 0) {
encodeCommentsType(encoder, addrset, addr, CodeUnit.POST_COMMENT);
encodeCommentsType(encoder, addrset, addr, CommentType.POST);
}
if ((flags & 8) != 0) {
encodeCommentsType(encoder, addrset, addr, CodeUnit.PLATE_COMMENT);
encodeCommentsType(encoder, addrset, addr, CommentType.PLATE);
}
encoder.closeElement(ELEM_COMMENTDB);
}
@@ -4,9 +4,9 @@
* 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.
@@ -194,7 +194,7 @@ public class ObjectiveC2_DecompilerMessageAnalyzer extends AbstractAnalyzer {
if (instruction == null) {
return;
}
if (instruction.getComment(CodeUnit.EOL_COMMENT) != null) {
if (instruction.getComment(CommentType.EOL) != null) {
return;
}
@@ -217,7 +217,7 @@ public class ObjectiveC2_DecompilerMessageAnalyzer extends AbstractAnalyzer {
builder.append(split[i]);
}
builder.delete(builder.length() - 2, builder.length() - 1);
instruction.setComment(CodeUnit.EOL_COMMENT, builder.toString());
instruction.setComment(CommentType.EOL, builder.toString());
}
private boolean isObjcCall(Program program, Varnode input, TaskMonitor monitor) {
@@ -26,120 +26,116 @@ import ghidra.app.plugin.core.decompile.DecompilerActionContext;
import ghidra.app.util.HelpTopics;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.CodeUnit;
import ghidra.program.model.listing.CommentType;
import ghidra.program.util.*;
import ghidra.util.HelpLocation;
public class DecompilerCommentsActionFactory extends CommentsActionFactory {
@Override
protected DockingAction doGetEditCommentsAction(CommentsDialog dialog,
String name) {
return new DecompilerEditCommentsAction(dialog, name);
}
@Override
protected DockingAction doGetEditCommentsAction(CommentsDialog dialog, String name) {
return new DecompilerEditCommentsAction(dialog, name);
}
@Override
protected DockingAction doGetSetCommentsAction(CommentsDialog dialog,
String name, String actionName, int commentType) {
return new DecompilerSetCommentsAction(dialog, name, actionName, commentType);
}
@Override
protected DockingAction doGetSetCommentsAction(CommentsDialog dialog, String name,
String actionName, CommentType commentType) {
return new DecompilerSetCommentsAction(dialog, name, actionName, commentType);
}
@Override
protected boolean doIsCommentSupported(ProgramLocation loc) {
if (loc == null || loc.getAddress() == null) {
return false;
}
return ((loc instanceof CodeUnitLocation)
|| (loc instanceof DecompilerLocation) || ((loc instanceof FunctionLocation) && !(loc instanceof VariableLocation)));
}
@Override
protected boolean doIsCommentSupported(ProgramLocation loc) {
if (loc == null || loc.getAddress() == null) {
return false;
}
return ((loc instanceof CodeUnitLocation) || (loc instanceof DecompilerLocation) ||
((loc instanceof FunctionLocation) && !(loc instanceof VariableLocation)));
}
private static class DecompilerSetCommentsAction extends DockingAction {
private final CommentsDialog dialog;
private final int commentType;
private static class DecompilerSetCommentsAction extends DockingAction {
private final CommentsDialog dialog;
private final CommentType commentType; // may be null for Generic Comment
DecompilerSetCommentsAction(CommentsDialog dialog, String name,
String actionName, int commentType) {
super(actionName, name);
this.dialog = dialog;
this.commentType = commentType;
setPopupMenuData(new MenuData(new String[] { "Comments",
actionName + "..." }, "comments"));
DecompilerSetCommentsAction(CommentsDialog dialog, String name, String actionName,
CommentType commentType) {
super(actionName, name);
this.dialog = dialog;
this.commentType = commentType;
setPopupMenuData(
new MenuData(new String[] { "Comments", actionName + "..." }, "comments"));
setHelpLocation(new HelpLocation(HelpTopics.DECOMPILER, "ActionComments"));
}
}
protected int getEditCommentType(ActionContext context) {
return commentType;
}
protected CommentType getEditCommentType(ActionContext context) {
return commentType;
}
@Override
public void actionPerformed(ActionContext context) {
CodeUnit cu = getCodeUnit(context);
int type = getEditCommentType(context);
dialog.showDialog(cu, type);
}
@Override
public void actionPerformed(ActionContext context) {
CodeUnit cu = getCodeUnit(context);
CommentType type = getEditCommentType(context);
dialog.showDialog(cu, type);
}
@Override
public boolean isEnabledForContext(ActionContext actionContext) {
ProgramLocation loc = getLocationForContext(actionContext);
if (!isCommentSupported(loc)) {
return false;
}
return CommentTypeUtils.isCommentAllowed(getCodeUnit(actionContext), loc);
}
@Override
public boolean isEnabledForContext(ActionContext actionContext) {
ProgramLocation loc = getLocationForContext(actionContext);
if (!isCommentSupported(loc)) {
return false;
}
return CommentTypeUtils.isCommentAllowed(getCodeUnit(actionContext), loc);
}
@Override
public boolean isValidContext(ActionContext context) {
return (context instanceof ListingActionContext)
|| (context instanceof DecompilerActionContext);
}
@Override
public boolean isValidContext(ActionContext context) {
return (context instanceof ListingActionContext) ||
(context instanceof DecompilerActionContext);
}
protected CodeUnit getCodeUnit(ActionContext actionContext) {
ProgramLocationActionContext context = (ProgramLocationActionContext) actionContext;
return context.getCodeUnit();
}
protected CodeUnit getCodeUnit(ActionContext actionContext) {
ProgramLocationActionContext context = (ProgramLocationActionContext) actionContext;
return context.getCodeUnit();
}
protected ProgramLocation getLocationForContext(
ActionContext actionContext) {
// only allow decompiler to have PRE, PLATE, and Generic Comment actions
if ((actionContext instanceof DecompilerActionContext)
&& commentType != CodeUnit.PRE_COMMENT
&& commentType != CodeUnit.PLATE_COMMENT
&& commentType != CodeUnit.NO_COMMENT) {
return null;
}
protected ProgramLocation getLocationForContext(ActionContext actionContext) {
// only allow decompiler to have PRE, PLATE, and Generic Comment actions
if ((actionContext instanceof DecompilerActionContext) &&
commentType != CommentType.PRE && commentType != CommentType.PLATE &&
commentType != null) {
return null;
}
if ( !(actionContext instanceof ProgramLocationActionContext) ) {
return null;
}
ProgramLocationActionContext context = (ProgramLocationActionContext) actionContext;
return context.getLocation();
}
}
if (!(actionContext instanceof ProgramLocationActionContext)) {
return null;
}
private static class DecompilerEditCommentsAction extends
DecompilerSetCommentsAction {
// Edit Comments Action info
private final static String[] EDIT_MENUPATH = new String[] {
"Comments", "Set..." };
ProgramLocationActionContext context = (ProgramLocationActionContext) actionContext;
return context.getLocation();
}
}
DecompilerEditCommentsAction(CommentsDialog dialog, String name) {
super(dialog, name, "Edit Comments", CodeUnit.NO_COMMENT);
setPopupMenuData(new MenuData(EDIT_MENUPATH, "comments"));
setKeyBindingData(new KeyBindingData(KeyEvent.VK_SEMICOLON, 0));
}
private static class DecompilerEditCommentsAction extends DecompilerSetCommentsAction {
// Edit Comments Action info
private final static String[] EDIT_MENUPATH = new String[] { "Comments", "Set..." };
@Override
protected int getEditCommentType(ActionContext context) {
if (context instanceof DecompilerActionContext) {
DecompilerActionContext decompContext = (DecompilerActionContext) context;
Address addr = decompContext.getAddress();
if (addr.equals(decompContext.getFunctionEntryPoint())) {
return CodeUnit.PLATE_COMMENT;
}
return CodeUnit.PRE_COMMENT;
}
CodeUnit cu = getCodeUnit(context);
return CommentTypeUtils.getCommentType(cu, getLocationForContext(context), CodeUnit.NO_COMMENT);
}
}
DecompilerEditCommentsAction(CommentsDialog dialog, String name) {
super(dialog, name, "Edit Comments", null);
setPopupMenuData(new MenuData(EDIT_MENUPATH, "comments"));
setKeyBindingData(new KeyBindingData(KeyEvent.VK_SEMICOLON, 0));
}
@Override
protected CommentType getEditCommentType(ActionContext context) {
if (context instanceof DecompilerActionContext) {
DecompilerActionContext decompContext = (DecompilerActionContext) context;
Address addr = decompContext.getAddress();
if (addr.equals(decompContext.getFunctionEntryPoint())) {
return CommentType.PLATE;
}
return CommentType.PRE;
}
CodeUnit cu = getCodeUnit(context);
return CommentTypeUtils.getCommentType(cu, getLocationForContext(context), null);
}
}
}