Merge remote-tracking branch 'origin/GP-1602-dragonmacher-decompiler-brace-stack-trace'

This commit is contained in:
Ryan Kurtz
2021-12-15 07:31:03 -05:00
@@ -31,7 +31,7 @@ public class DecompilerUtils {
/** /**
* If the token refers to an individual Varnode, return it. Otherwise return null * If the token refers to an individual Varnode, return it. Otherwise return null
* *
* @param token the token to check * @param token the token to check
* @return the Varnode or null otherwise * @return the Varnode or null otherwise
*/ */
@@ -215,9 +215,9 @@ public class DecompilerUtils {
} }
/** /**
* Returns the function represented by the given token. This will be either the * Returns the function represented by the given token. This will be either the
* decompiled function or a function referenced within the decompiled function. * decompiled function or a function referenced within the decompiled function.
* *
* @param program the program * @param program the program
* @param token the token * @param token the token
* @return the function * @return the function
@@ -270,10 +270,10 @@ public class DecompilerUtils {
/** /**
* Similar to {@link #getTokens(ClangNode, AddressSetView)}, but uses the tokens from * Similar to {@link #getTokens(ClangNode, AddressSetView)}, but uses the tokens from
* the given view fields. Sometimes the tokens in the model (represented by the * the given view fields. Sometimes the tokens in the model (represented by the
* {@link ClangNode}) are different than the fields in the view (such as when a list of * {@link ClangNode}) are different than the fields in the view (such as when a list of
* comment tokens are condensed into a single comment token). * comment tokens are condensed into a single comment token).
* *
* @param fields the fields to check * @param fields the fields to check
* @param address the address each returned token must match * @param address the address each returned token must match
* @return the matching tokens * @return the matching tokens
@@ -354,8 +354,7 @@ public class DecompilerUtils {
public static AddressSet findClosestAddressSet(Program program, AddressSpace functionSpace, public static AddressSet findClosestAddressSet(Program program, AddressSpace functionSpace,
List<ClangToken> tokenList) { List<ClangToken> tokenList) {
AddressSet addressSet = new AddressSet(); AddressSet addressSet = new AddressSet();
for (int i = 0; i < tokenList.size(); ++i) { for (ClangToken tok : tokenList) {
ClangToken tok = tokenList.get(i);
addTokenAddressRangeToSet(addressSet, tok, functionSpace); addTokenAddressRangeToSet(addressSet, tok, functionSpace);
} }
@@ -574,33 +573,39 @@ public class DecompilerUtils {
} }
Stack<ClangSyntaxToken> braceStack = new Stack<>(); Stack<ClangSyntaxToken> braceStack = new Stack<>();
for (int i = 0; i < list.size(); ++i) { for (ClangNode element : list) {
ClangToken token = (ClangToken) list.get(i); ClangToken token = (ClangToken) element;
if (token instanceof ClangSyntaxToken) { if (!(token instanceof ClangSyntaxToken)) {
ClangSyntaxToken syntaxToken = (ClangSyntaxToken) token; continue;
}
if (startToken == syntaxToken) { ClangSyntaxToken syntaxToken = (ClangSyntaxToken) token;
// found our starting token, take the current value on the stack if (startToken == syntaxToken) {
ClangSyntaxToken matchingBrace = braceStack.pop();
return matchingBrace;
}
if (!isBrace(syntaxToken)) {
continue;
}
if (braceStack.isEmpty()) { if (braceStack.isEmpty()) {
braceStack.push(syntaxToken); return null; // this can happen if the start token has a bad parent values
continue;
} }
ClangSyntaxToken lastToken = braceStack.peek(); // found our starting token, take the current value on the stack
if (isMatchingBrace(lastToken, syntaxToken)) { ClangSyntaxToken matchingBrace = braceStack.pop();
braceStack.pop(); return matchingBrace;
} }
else {
braceStack.push(syntaxToken); if (!isBrace(syntaxToken)) {
} continue;
}
if (braceStack.isEmpty()) {
braceStack.push(syntaxToken);
continue;
}
ClangSyntaxToken lastToken = braceStack.peek();
if (isMatchingBrace(lastToken, syntaxToken)) {
braceStack.pop();
}
else {
braceStack.push(syntaxToken);
} }
} }
return null; return null;
@@ -637,7 +642,7 @@ public class DecompilerUtils {
* sequence of tokens that are part of the comment and group them into a single * 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. * ClangCommentToken. This makes post processing on the full comment string easier.
* A single comment string can contain white space that manifests as ClangSyntaxTokens * A single comment string can contain white space that manifests as ClangSyntaxTokens
* with white space as text. * with white space as text.
* @param alltoks is the token stream * @param alltoks is the token stream
* @param i is the position of the initial comment token * @param i is the position of the initial comment token
* @param first is the initial comment token * @param first is the initial comment token
@@ -736,7 +741,7 @@ public class DecompilerUtils {
/** /**
* Returns the data type for the given context if the context pertains to a data type * Returns the data type for the given context if the context pertains to a data type
* *
* @param context the context * @param context the context
* @return the data type or null * @return the data type or null
*/ */