You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by wu...@apache.org on 2022/04/11 17:16:38 UTC

[tvm] branch main updated: escape tvmscript's string literal (#10954)

This is an automated email from the ASF dual-hosted git repository.

wuwei pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 76ac68de73 escape tvmscript's string literal (#10954)
76ac68de73 is described below

commit 76ac68de73f5521f412fb5d279ef05fba4f3f0b9
Author: wrongtest <wr...@gmail.com>
AuthorDate: Tue Apr 12 01:16:33 2022 +0800

    escape tvmscript's string literal (#10954)
---
 src/printer/doc.cc                                |  5 +++--
 tests/python/unittest/test_tvmscript_roundtrip.py | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/printer/doc.cc b/src/printer/doc.cc
index 792c22420a..f7d9fdfd7d 100644
--- a/src/printer/doc.cc
+++ b/src/printer/doc.cc
@@ -30,6 +30,8 @@
 #include <sstream>
 #include <vector>
 
+#include "../support/str_escape.h"
+
 namespace tvm {
 
 /*!
@@ -129,9 +131,8 @@ Doc Doc::Indent(int indent, Doc doc) {
 }
 
 Doc Doc::StrLiteral(const std::string& value, std::string quote) {
-  // TODO(@M.K.): add escape.
   Doc doc;
-  return doc << quote << value << quote;
+  return doc << quote << support::StrEscape(value) << quote;
 }
 
 Doc Doc::PyBoolLiteral(bool value) {
diff --git a/tests/python/unittest/test_tvmscript_roundtrip.py b/tests/python/unittest/test_tvmscript_roundtrip.py
index 6ddbbe5a89..8f83df9c71 100644
--- a/tests/python/unittest/test_tvmscript_roundtrip.py
+++ b/tests/python/unittest/test_tvmscript_roundtrip.py
@@ -3223,6 +3223,24 @@ def int64_support():
     return elementwise_shape_int64
 
 
+def string_annotation_escaping():
+    @T.prim_func
+    def string_annotation_of_special_chars():
+        T.func_attr(
+            {
+                "key1": '"\'hello\t\r"',
+                "key2": """
+            %1 = add i32 %0, %0
+            %2 = add i32 %0, %1
+            %3 = add i32 %1, %2
+            """,
+            }
+        )
+        T.evaluate(0)
+
+    return string_annotation_of_special_chars
+
+
 ir_generator = tvm.testing.parameter(
     opt_gemm_normalize,
     opt_gemm_lower,
@@ -3256,6 +3274,7 @@ ir_generator = tvm.testing.parameter(
     llvm_intrin_call,
     parse_bufferslice_as_range_bound,
     int64_support,
+    string_annotation_escaping,
 )