Skip to content

Commit b98f4ca

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 b98f4ca

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

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)