Skip to content

Commit c9ed97f

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 c9ed97f

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-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: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,33 @@ 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 '"': printf("\\\""); break;
61+
case '\\': printf("\\\\"); break;
62+
case '\b': printf("\\b"); break;
63+
case '\f': printf("\\f"); break;
64+
case '\n': printf("\\n"); break;
65+
case '\r': printf("\\r"); break;
66+
case '\t': printf("\\t"); break;
67+
default:
68+
if ((unsigned char)*V < 0x20)
69+
printf("\\u%04x", (unsigned char)*V);
70+
else
71+
printf("%c", *V);
72+
}
73+
}
74+
}
75+
printf("\"");
76+
}
77+
5578
inline void print(JSONPrinter& Printer, int V) { printf("%d", V); }
5679
inline void print(JSONPrinter& Printer, unsigned V) { printf("%u", V); }
5780
inline void print(JSONPrinter& Printer, int64_t V) { printf("%ld", V); }
5881
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 : ""); }
6082
inline void print(JSONPrinter& Printer, const std::string& V) { print(Printer, V.c_str()); }
6183

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

0 commit comments

Comments
 (0)