Skip to content

Commit b4c5473

Browse files
committed
tools/clang/json: escape strings properly
When preparing a codesearch index, I encountered errors which I narrowed down to lines like the following in the json output of codesearch: "type": "void (void __attribute__((btf_type_tag("user")))*, const void *, size_t, size_t)", After this change, the line gets formatted like this: "type": "void (void __attribute__((btf_type_tag(\"user\")))*, const void *, size_t, size_t)", This fixes the errors I encountered
1 parent 8fc3779 commit b4c5473

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def-source source0.c function_with_quotes_in_type yes
2+
3+
function function_with_quotes_in_type is defined in source0.c:
4+
5+
29: void function_with_quotes_in_type(void __attribute__((btf_type_tag("user"))) *)
6+
30: {
7+
31: }

pkg/codesearch/testdata/query-file-index-source

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ file source0.c defines the following entities:
44

55
function close
66
function function_with_comment_in_header
7+
function function_with_quotes_in_type
78
function open
89
struct struct_in_c_file

pkg/codesearch/testdata/source0.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ void function_with_comment_in_header()
2525
{
2626
same_name_in_several_files();
2727
}
28+
29+
void function_with_quotes_in_type(void __attribute__((btf_type_tag("user"))) *)
30+
{
31+
}

pkg/codesearch/testdata/source0.c.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@
5252
}
5353
]
5454
},
55+
{
56+
"kind": "function",
57+
"name": "function_with_quotes_in_type",
58+
"type": "void (void __attribute__((btf_type_tag(\"user\")))*)",
59+
"body": {
60+
"file": "source0.c",
61+
"start_line": 29,
62+
"end_line": 31
63+
},
64+
"comment": {}
65+
},
5566
{
5667
"kind": "function",
5768
"name": "open",

tools/clang/json.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,47 @@ class JSONPrinter {
5252
Scope Top;
5353
};
5454

55+
inline void print(JSONPrinter& Printer, const char* V) {
56+
printf("\"");
57+
if (V) {
58+
for (; *V; ++V) {
59+
switch (*V) {
60+
case '"':
61+
printf("\\\"");
62+
break;
63+
case '\\':
64+
printf("\\\\");
65+
break;
66+
case '\b':
67+
printf("\\b");
68+
break;
69+
case '\f':
70+
printf("\\f");
71+
break;
72+
case '\n':
73+
printf("\\n");
74+
break;
75+
case '\r':
76+
printf("\\r");
77+
break;
78+
case '\t':
79+
printf("\\t");
80+
break;
81+
default:
82+
if ((unsigned char)*V < 0x20)
83+
printf("\\u%04x", (unsigned char)*V);
84+
else
85+
printf("%c", *V);
86+
}
87+
}
88+
}
89+
printf("\"");
90+
}
91+
5592
inline void print(JSONPrinter& Printer, int V) { printf("%d", V); }
5693
inline void print(JSONPrinter& Printer, unsigned V) { printf("%u", V); }
5794
inline void print(JSONPrinter& Printer, int64_t V) { printf("%ld", V); }
5895
inline void print(JSONPrinter& Printer, bool V) { printf("%s", V ? "true" : "false"); }
59-
inline void print(JSONPrinter& Printer, const char* V) { printf("\"%s\"", V ? V : ""); }
6096
inline void print(JSONPrinter& Printer, const std::string& V) { print(Printer, V.c_str()); }
6197

6298
template <typename E> void print(JSONPrinter& Printer, const std::unique_ptr<E>& V) {

0 commit comments

Comments
 (0)