Skip to content

Ghidra is a software reverse engineering (SRE) framework

Refactor collection of comment tokens in decompiler window

caheckman05f94870d513
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangLayoutController.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangLayoutController.java
index b21480654b..861053f422 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangLayoutController.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangLayoutController.java
@@ -18,7 +18,6 @@ package ghidra.app.decompiler.component;
import java.awt.*;
import java.math.BigInteger;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.regex.*;
@@ -183,10 +182,6 @@ public class ClangLayoutController implements LayoutModel, LayoutModelListener {
ClangFieldElement lineNumberFieldElement =
createLineNumberFieldElement(line, lineCount, paintLineNumbers);
- if (isComment(tokens)) {
- return createCommentField(tokens, lineNumberFieldElement, line.getIndent());
- }
-
FieldElement[] elements = createFieldElementsForLine(tokens);
int indent = line.getIndent() * indentWidth;
@@ -196,64 +191,27 @@ public class ClangLayoutController implements LayoutModel, LayoutModelListener {
hlFactory);
}
- private ClangTextField createCommentField(List<ClangToken> tokens,
- ClangFieldElement lineNumberFieldElement, int indentCount) {
-
- StringBuilder buffy = new StringBuilder();
- for (ClangToken t : tokens) {
- buffy.append(t.getText());
- }
-
- String text = buffy.toString();
- ClangCommentToken token = getFirstCommentToken(tokens);
- Color color = syntax_color[token.getSyntaxType()];
- AttributedString prototype = new AttributedString("prototype", color, metrics);
- Program program = decompilerPanel.getProgram();
- FieldElement element = CommentUtils.parseTextForAnnotations(text, program, prototype, 0);
-
- FieldElement[] elements = new FieldElement[] { element };
- ClangCommentToken newCommentToken = ClangCommentToken.derive(token, text);
- List<ClangToken> newTokens = Arrays.asList(newCommentToken);
-
- int indent = indentCount * indentWidth;
- int lineNumberWidth = lineNumberFieldElement.getStringWidth();
- int updatedMaxWidth = maxWidth + lineNumberWidth;
- return new ClangTextField(newTokens, elements, lineNumberFieldElement, indent,
- updatedMaxWidth, hlFactory);
- }
-
private FieldElement[] createFieldElementsForLine(List<ClangToken> tokens) {
- ClangFieldElement[] elements = new ClangFieldElement[tokens.size()];
+ FieldElement[] elements = new FieldElement[tokens.size()];
int columnPosition = 0;
for (int i = 0; i < tokens.size(); ++i) {
ClangToken token = tokens.get(i);
- AttributedString as =
- new AttributedString(token.getText(), syntax_color[token.getSyntaxType()], metrics);
- elements[i] = new ClangFieldElement(token, as, columnPosition);
- columnPosition += as.length();
- }
- return elements;
- }
-
- private ClangCommentToken getFirstCommentToken(List<ClangToken> tokens) {
- for (ClangToken t : tokens) {
- if (t instanceof ClangCommentToken) {
- return (ClangCommentToken) t;
+ Color color = syntax_color[token.getSyntaxType()];
+ if (token instanceof ClangCommentToken) {
+ AttributedString prototype = new AttributedString("prototype", color, metrics);
+ Program program = decompilerPanel.getProgram();
+ elements[i] =
+ CommentUtils.parseTextForAnnotations(token.getText(), program, prototype, 0);
+ columnPosition += elements[i].length();
}
- }
- return null;
- }
-
- private boolean isComment(List<ClangToken> tokens) {
- for (ClangToken t : tokens) {
- if (t instanceof ClangCommentToken) {
- // for now, I believe all comments are on a line by themselves, so if we find
- // a comment token, then these are all comments
- return true;
+ else {
+ AttributedString as = new AttributedString(token.getText(), color, metrics);
+ elements[i] = new ClangFieldElement(token, as, columnPosition);
+ columnPosition += as.length();
}
}
- return false;
+ return elements;
}
private ClangFieldElement createLineNumberFieldElement(ClangLine line, int lineCount,
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java
index 57a21c1bb3..4f8e89354e 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java
@@ -435,6 +435,9 @@ public class DecompilerUtils {
FieldSelection fieldSelection = new FieldSelection();
for (ClangToken clangToken : tokens) {
ClangLine lineParent = clangToken.getLineParent();
+ if (lineParent == null) {
+ continue;
+ }
int lineNumber = lineParent.getLineNumber();
// lineNumber is one-based, we need zero-based
fieldSelection.addRange(lineNumber - 1, lineNumber);
@@ -629,6 +632,62 @@ public class DecompilerUtils {
return text.startsWith("goto");
}
+ /**
+ * Within a token stream, after seeing an initial comment token, collect the contiguous
+ * sequence of tokens that are part of the comment and group them into a single
+ * ClangCommentToken. This makes post processing on the full comment string easier.
+ * A single comment string can contain white space that manifests as ClangSyntaxTokens
+ * with white space as text.
+ * @param alltoks is the token stream
+ * @param i is the position of the initial comment token
+ * @param first is the initial comment token
+ * @param current is the ClangLine object currently being scanned
+ * @param builder is used to collect the full comment string
+ * @return the position of the first token after the comment string
+ */
+ private static int consumeCommentTokens(List<ClangNode> alltoks, int i, ClangCommentToken first,
+ ClangLine current, StringBuilder builder) {
+ builder.setLength(0);
+ builder.append(first.getText());
+ i += 1;
+ while (i < alltoks.size()) {
+ ClangToken tok = (ClangToken) alltoks.get(i);
+ if (tok instanceof ClangCommentToken) {
+ if (first.getSyntaxType() != tok.getSyntaxType()) {
+ break;
+ }
+ builder.append(tok.getText());
+ }
+ else if (tok instanceof ClangSyntaxToken) {
+ // Comments can have blank space tokens embedded in them
+ String val = tok.getText();
+ if (val.isBlank()) {
+ builder.append(val);
+ }
+ else {
+ break;
+ }
+ }
+ else {
+ break;
+ }
+ i += 1;
+ }
+ ClangCommentToken commentToken = ClangCommentToken.derive(first, builder.toString());
+ commentToken.setLineParent(current);
+ current.addToken(commentToken);
+
+ return i;
+ }
+
+ /**
+ * A token hierarchy is flattened and then split into individual lines at the
+ * ClangBreak tokens. An array of the lines, each as a ClangLine object that owns
+ * its respective tokens, is returned. Sequences of comment tokens are collapsed into
+ * a single ClangCommentToken.
+ * @param group is the token hierarchy
+ * @return the array of ClangLine objects
+ */
public static ArrayList<ClangLine> toLines(ClangTokenGroup group) {
List<ClangNode> alltoks = new ArrayList<>();
@@ -650,13 +709,21 @@ public class DecompilerUtils {
else {
current = new ClangLine(lineNumber++, 0); // otherwise use zero indent
}
+
+ StringBuilder commentBuilder = new StringBuilder();
for (; i < alltoks.size(); ++i) {
+
ClangToken tok = (ClangToken) alltoks.get(i);
if (tok instanceof ClangBreak) {
lines.add(current);
brk = (ClangBreak) tok;
current = new ClangLine(lineNumber++, brk.getIndent());
}
+ else if (tok instanceof ClangCommentToken) {
+ i = consumeCommentTokens(alltoks, i, (ClangCommentToken) tok, current,
+ commentBuilder);
+ i -= 1;
+ }
else {
tok.setLineParent(current);
current.addToken(tok);