Skip to content

Commit 02bac03

Browse files
FlorentRevestdvyukov
authored andcommitted
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 40acda8 commit 02bac03

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
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+
35: void function_with_quotes_in_type(void __attribute__((btf_type_tag("user"))) *)
6+
36: {
7+
37: }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ file source0.c defines the following entities:
55
function close
66
function func_accepting_a_struct
77
function function_with_comment_in_header
8+
function function_with_quotes_in_type
89
function open
910
struct struct_in_c_file

pkg/codesearch/testdata/source0.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ int func_accepting_a_struct(struct some_struct* p)
3131
return ((some_struct_t*)p)->x +
3232
((union some_union*)p)->x;
3333
}
34+
35+
void function_with_quotes_in_type(void __attribute__((btf_type_tag("user"))) *)
36+
{
37+
}

pkg/codesearch/testdata/source0.c.json

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
{
22
"definitions": [
3-
{
4-
"name": "some_enum",
5-
"kind": "enum",
6-
"body": {
7-
"file": "source0.h",
8-
"start_line": 45,
9-
"end_line": 48
10-
},
11-
"comment": {}
12-
},
133
{
144
"name": "close",
155
"type": "int ()",
@@ -89,6 +79,17 @@
8979
}
9080
]
9181
},
82+
{
83+
"name": "function_with_quotes_in_type",
84+
"type": "void (void __attribute__((btf_type_tag(\"user\")))*)",
85+
"kind": "function",
86+
"body": {
87+
"file": "source0.c",
88+
"start_line": 35,
89+
"end_line": 37
90+
},
91+
"comment": {}
92+
},
9293
{
9394
"name": "open",
9495
"type": "int ()",
@@ -148,6 +149,26 @@
148149
},
149150
"comment": {}
150151
},
152+
{
153+
"name": "some_union",
154+
"kind": "union",
155+
"body": {
156+
"file": "source0.h",
157+
"start_line": 40,
158+
"end_line": 43
159+
},
160+
"comment": {}
161+
},
162+
{
163+
"name": "some_enum",
164+
"kind": "enum",
165+
"body": {
166+
"file": "source0.h",
167+
"start_line": 45,
168+
"end_line": 48
169+
},
170+
"comment": {}
171+
},
151172
{
152173
"name": "another_struct_t",
153174
"kind": "typedef",
@@ -187,16 +208,6 @@
187208
"end_line": 34
188209
},
189210
"comment": {}
190-
},
191-
{
192-
"name": "some_union",
193-
"kind": "union",
194-
"body": {
195-
"file": "source0.h",
196-
"start_line": 40,
197-
"end_line": 43
198-
},
199-
"comment": {}
200211
}
201212
]
202213
}

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)