Ghidra is a software reverse engineering (SRE) framework
GP-7409 - Minor cleanup in SymbolPathParser
dragonmacherd84ce77b11af
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/util/SymbolPathParser.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/util/SymbolPathParser.javaindex 020f273ebf..8d43ab3772 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/util/SymbolPathParser.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/app/util/SymbolPathParser.java@@ -23,8 +23,7 @@ import org.apache.commons.lang3.StringUtils;import ghidra.program.model.symbol.Namespace;/**- * A parser for breaking down namespaces in the presence of complicating factors such- * as templates.+ * A parser for breaking down namespaces in the presence of complicating factors such as templates.* <P>* For example, if a SymbolPath is constructed with "foo<int, blah::hah>::bar::baz",* then "baz" is the name of a symbol in the "bar" namespace, which is in the@@ -69,10 +68,7 @@ public class SymbolPathParser {private static boolean skipParsing(String name, boolean ignoreLeaderParens) {- // if (name.indexOf(Namespace.DELIMITER) == -1) {- // following is temporary kludge due to struct (blah). TODO: figure/fix- // This particular test for starting with the open parenthesis is to work around a type- // seen in "Rust."+ // The check for starting with the open parenthesis is to work around a type seen in Rustif (ignoreLeaderParens && name.startsWith("(")) {return true;}@@ -87,82 +83,102 @@ public class SymbolPathParser {* @return List<String> containing the sequence of namespaces and trailing name.*/private static List<String> naiveParse(String name) {- // Only break on namespace delimiters that are found at templateLevel == 0.++ List<String> list = naiveParseWithTemplates(name);+ if (!list.isEmpty()) {+ return list;+ }++ return naiveParseIgnoringTemplates(name);+ }++ private static List<String> naiveParseWithTemplates(String name) {List<String> list = new ArrayList<>();int templateLevel = 0;- int parenthesesLevel = 0;- int startIndex = 0;+ int parenLevel = 0;+ int start = 0;for (int i = 0; i < name.length(); ++i) {- if (i == startIndex && templateLevel == 0 && parenthesesLevel == 0 &&- isTerminalAngleOperator(name.substring(i))) {- break;- }- if ((name.charAt(i) == ':') && (i != name.length() - 1) &&- (name.charAt(i + 1) == ':')) {- if ((templateLevel == 0) && (parenthesesLevel == 0)) {- int endIndex = i; // could be 0 if i == 0.- if (endIndex > startIndex) {- list.add(name.substring(startIndex, endIndex));- startIndex = i + 2;- i++; // Only increment one, because the loop also has an increment.++ char c = name.charAt(i);+ if (isDelimiter(i, name)) {+ if (templateLevel == 0 && parenLevel == 0) {+ int end = i; // 0 if i == 0; leading ':' chars+ if (end > start) {+ list.add(name.substring(start, end));+ start = i + 2;}}+ i++; // increment one, since the loop has an increment}- else if (name.charAt(i) == '<') {+ else if (c == '<') {++templateLevel;}- else if (name.charAt(i) == '>') {+ else if (c == '>') {--templateLevel;}- else if (name.charAt(i) == '(') {- ++parenthesesLevel;+ else if (c == '(') {+ ++parenLevel;}- else if (name.charAt(i) == ')') {- --parenthesesLevel;+ else if (c == ')') {+ --parenLevel;}}- if ((templateLevel != 0) || (parenthesesLevel != 0)) {- // Revert to no checking template level- startIndex = 0;- list = new ArrayList<>();- for (int i = 0; i < name.length(); ++i) {- if ((name.charAt(i) == ':') && (i != name.length() - 1) &&- name.charAt(i + 1) == ':') {- int endIndex = i; // could be 0 if i == 0.- if (endIndex > startIndex) {- list.add(name.substring(startIndex, endIndex));- startIndex = i + 2;- i++; // Only increment one, because the loop also has an increment.- }++ boolean unbalanced = templateLevel != 0 || parenLevel != 0;+ String segment = name.substring(start, name.length());+ if (!unbalanced) {+ list.add(segment);+ return list;+ }++ if (isAngleOperator(segment)) {+ list.add(segment);+ return list;+ }++ return List.of();+ }++ private static List<String> naiveParseIgnoringTemplates(String name) {++ int start = 0;+ List<String> list = new ArrayList<>();+ for (int i = 0; i < name.length(); ++i) {+ if (isDelimiter(i, name)) {+ int end = i; // 0 if i == 0; leading ':' chars+ if (end > start) {+ list.add(name.substring(start, end));+ start = i + 2;}+ i++; // increment one, since the loop has an increment}}- list.add(name.substring(startIndex, name.length()));-+ list.add(name.substring(start, name.length()));return list;}- private static boolean isTerminalAngleOperator(String component) {+ private static boolean isDelimiter(int i, String name) {+ if (i == name.length() - 1) {+ return false;+ }+ return name.charAt(i) == ':' && name.charAt(i + 1) == ':';+ }++ private static boolean isAngleOperator(String component) {+ //@formatter:offreturn switch (component) {- case "operator<", "operator>", "operator<=", "operator>=", "operator<<",- "operator>>", "operator<<=", "operator>>=", "operator<=>" -> true;+ case "operator<",+ "operator>",+ "operator<=",+ "operator>=",+ "operator<<",+ "operator>>",+ "operator<<=",+ "operator>>=",+ "operator<=>" -> true;default -> false;};+ //@formatter:on}- // TODO: in progress.-// /**-// * More complicated parsing that takes into account:-// * <LI> overloaded operators that use angle brackets-// * <LI> templated overloaded operators that use angle brackets-// * <LI> mismatched angle brackets (MSFT specialness)-// * <LI> MSFT interface namespaces (square brackets out of bounds of namespace delimiter)-// * @param name The input String to be parsed.-// * @return List<String> containing the sequence of namespaces and trailing name.-// */-// private static List<String> detailedParse(String name) {-// List<String> list = new ArrayList<>();-// return list;-// }-}diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/app/util/SymbolPathParserTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/app/util/SymbolPathParserTest.javaindex 3ad581be35..f38ff9e0cf 100644--- a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/app/util/SymbolPathParserTest.java+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/app/util/SymbolPathParserTest.java@@ -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.@@ -15,7 +15,7 @@*/package ghidra.app.util;-import static org.junit.Assert.fail;+import static org.junit.Assert.*;import java.util.ArrayList;import java.util.List;@@ -38,6 +38,14 @@ public class SymbolPathParserTest extends AbstractGTest {}}+ @Test+ public void testLeadingNamespaceSeparator() {+ List<String> list = SymbolPathParser.parse("::bob");+ List<String> expected = new ArrayList<>();+ expected.add("::bob");+ assertListEqualOrdered(expected, list);+ }+@Testpublic void testJustSymbolNameNoPath() {List<String> list = SymbolPathParser.parse("bob");@@ -146,4 +154,16 @@ public class SymbolPathParserTest extends AbstractGTest {assertListEqualOrdered(expected, list);}+ @Test+ public void testOverloadedOperator() {++ String name = "std::basic_ostream<char,std::char_traits<char>_>::operator<<";+ List<String> list = SymbolPathParser.parse(name);+ List<String> expected = new ArrayList<>();+ expected.add("std");+ expected.add("basic_ostream<char,std::char_traits<char>_>");+ expected.add("operator<<");+ assertListEqualOrdered(expected, list);+ }+}