]> granicus.if.org Git - clang/commitdiff
[analyzer] JsonSupport: Escape escapes
authorCsaba Dabis <dabis.csaba98@gmail.com>
Tue, 25 Jun 2019 03:08:32 +0000 (03:08 +0000)
committerCsaba Dabis <dabis.csaba98@gmail.com>
Tue, 25 Jun 2019 03:08:32 +0000 (03:08 +0000)
Summary: -

Reviewers: NoQ

Reviewed By: NoQ

Subscribers: xazax.hun, baloghadamsoftware, szepet, a.sidorin,
             mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D63462

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@364270 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/JsonSupport.h
test/Analysis/dump_egraph.c
test/Analysis/exploded-graph-rewriter/escapes.c

index f235daa1689e7cba510793d9d76e864563e634e7..a3d12cbec6db710f48d148a9361b717ffd3de853 100644 (file)
@@ -31,7 +31,26 @@ inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) {
   std::string Str = RawSR.trim().str();
   size_t Pos = 0;
 
+  // Escape backslashes.
+  while (true) {
+    Pos = Str.find('\\', Pos);
+    if (Pos == std::string::npos)
+      break;
+
+    // Prevent bad conversions.
+    size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
+
+    // See whether the current backslash is not escaped.
+    if (TempPos != Str.find("\\\\", Pos)) {
+      Str.insert(Pos, "\\");
+      ++Pos; // As we insert the backslash move plus one.
+    }
+
+    ++Pos;
+  }
+
   // Escape double quotes.
+  Pos = 0;
   while (true) {
     Pos = Str.find('\"', Pos);
     if (Pos == std::string::npos)
@@ -40,8 +59,8 @@ inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) {
     // Prevent bad conversions.
     size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
 
-    // See whether the current double quote is escaped.
-    if (TempPos != Str.find("\\\"", TempPos)) {
+    // See whether the current double quote is not escaped.
+    if (TempPos != Str.find("\\\"", Pos)) {
       Str.insert(Pos, "\\");
       ++Pos; // As we insert the escape-character move plus one.
     }
index 5544d9fae2e26fb7ee72c2510a0219e92febcaf6..11343753037b24536ede4ae6fa49f38fdbaf3644 100644 (file)
@@ -13,6 +13,8 @@ int getJ();
 
 int foo() {
   int *x = 0, *y = 0;
+  char c = '\x13';
+
   return *x + *y;
 }
 
@@ -22,5 +24,7 @@ int foo() {
 
 // CHECK: \"has_report\": true
 
-// CHECK: \"pretty\": \"*x\", \"location\": \{ \"line\": 16, \"column\": 10, \"file\": \"{{(.+)}}dump_egraph.c\" \}
+// CHECK: \"pretty\": \"*x\", \"location\": \{ \"line\": 18, \"column\": 10, \"file\": \"{{(.+)}}dump_egraph.c\" \}
+
+// CHECK: \"pretty\": \"'\\\\x13'\"
 
index 32a7713561f879c384d9e716e3e4ed56df27f23b..140ec676fb0fb38b426aa9f7ee04bc9d8be4fff1 100644 (file)
@@ -15,7 +15,7 @@ void escapes() {
   // CHECK: <td align="left"><b>Environment: </b></td>
   // CHECK-SAME: <td align="left">"foo"</td>
   // CHECK-SAME: <td align="left">&amp;Element\{"foo",0 S64b,char\}</td>
-  const char *const foo = "foo";
+  const char *const foo = "\x66\x6f\x6f";
 
   // CHECK: <font color="cyan3">BinaryOperator</font>
   // CHECK-SAME: <td align="left">1 \| 2</td>