Skip to content

Commit c55170c

Browse files
author
Csaba Dabis
committed
[analyzer] JsonSupport: Escape escapes
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 llvm-svn: 364270
1 parent 14f4de9 commit c55170c

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

clang/include/clang/Basic/JsonSupport.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,26 @@ inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) {
3131
std::string Str = RawSR.trim().str();
3232
size_t Pos = 0;
3333

34+
// Escape backslashes.
35+
while (true) {
36+
Pos = Str.find('\\', Pos);
37+
if (Pos == std::string::npos)
38+
break;
39+
40+
// Prevent bad conversions.
41+
size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
42+
43+
// See whether the current backslash is not escaped.
44+
if (TempPos != Str.find("\\\\", Pos)) {
45+
Str.insert(Pos, "\\");
46+
++Pos; // As we insert the backslash move plus one.
47+
}
48+
49+
++Pos;
50+
}
51+
3452
// Escape double quotes.
53+
Pos = 0;
3554
while (true) {
3655
Pos = Str.find('\"', Pos);
3756
if (Pos == std::string::npos)
@@ -40,8 +59,8 @@ inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) {
4059
// Prevent bad conversions.
4160
size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
4261

43-
// See whether the current double quote is escaped.
44-
if (TempPos != Str.find("\\\"", TempPos)) {
62+
// See whether the current double quote is not escaped.
63+
if (TempPos != Str.find("\\\"", Pos)) {
4564
Str.insert(Pos, "\\");
4665
++Pos; // As we insert the escape-character move plus one.
4766
}

clang/test/Analysis/dump_egraph.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ int getJ();
1313

1414
int foo() {
1515
int *x = 0, *y = 0;
16+
char c = '\x13';
17+
1618
return *x + *y;
1719
}
1820

@@ -22,5 +24,7 @@ int foo() {
2224

2325
// CHECK: \"has_report\": true
2426

25-
// CHECK: \"pretty\": \"*x\", \"location\": \{ \"line\": 16, \"column\": 10, \"file\": \"{{(.+)}}dump_egraph.c\" \}
27+
// CHECK: \"pretty\": \"*x\", \"location\": \{ \"line\": 18, \"column\": 10, \"file\": \"{{(.+)}}dump_egraph.c\" \}
28+
29+
// CHECK: \"pretty\": \"'\\\\x13'\"
2630

clang/test/Analysis/exploded-graph-rewriter/escapes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void escapes() {
1515
// CHECK: <td align="left"><b>Environment: </b></td>
1616
// CHECK-SAME: <td align="left">"foo"</td>
1717
// CHECK-SAME: <td align="left">&amp;Element\{"foo",0 S64b,char\}</td>
18-
const char *const foo = "foo";
18+
const char *const foo = "\x66\x6f\x6f";
1919

2020
// CHECK: <font color="cyan3">BinaryOperator</font>
2121
// CHECK-SAME: <td align="left">1 \| 2</td>

0 commit comments

Comments
 (0)