[Mlir-commits] [mlir] [ViewOpGraph] Improve GraphViz output (PR #125509)

Fangrui Song llvmlistbot at llvm.org
Wed Feb 5 09:48:45 PST 2025


================
@@ -49,16 +49,27 @@ static std::string strFromOs(function_ref<void(raw_ostream &)> func) {
   return buf;
 }
 
-/// Escape special characters such as '\n' and quotation marks.
-static std::string escapeString(std::string str) {
-  return strFromOs([&](raw_ostream &os) { os.write_escaped(str); });
-}
-
 /// Put quotation marks around a given string.
 static std::string quoteString(const std::string &str) {
   return "\"" + str + "\"";
 }
 
+/// For Graphviz record nodes:
+/// " Braces, vertical bars and angle brackets must be escaped with a backslash
+/// character if you wish them to appear as a literal character "
+std::string escapeLabelString(const std::string &str) {
+  std::string buf;
+  llvm::raw_string_ostream os(buf);
+  llvm::DenseSet<char> shouldEscape = {'{', '|', '<', '}', '>', '\n', '"'};
+  for (char c : str) {
+    if (shouldEscape.contains(c)) {
----------------
MaskRay wrote:

`llvm::is_contained({...}, c)`

https://github.com/llvm/llvm-project/pull/125509


More information about the Mlir-commits mailing list