Skip to content

Commit 79e7e43

Browse files
committed
[clangd] Use JSON streaming API for Trace rather than pasting strings. NFC
llvm-svn: 359202
1 parent f13b6a7 commit 79e7e43

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

clang-tools-extra/clangd/Trace.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@ namespace {
2727
// Perhaps we should replace this by something that disturbs performance less.
2828
class JSONTracer : public EventTracer {
2929
public:
30-
JSONTracer(llvm::raw_ostream &Out, bool Pretty)
31-
: Out(Out), Sep(""), Start(std::chrono::system_clock::now()),
32-
JSONFormat(Pretty ? "{0:2}" : "{0}") {
30+
JSONTracer(llvm::raw_ostream &OS, bool Pretty)
31+
: Out(OS, Pretty ? 2 : 0), Start(std::chrono::system_clock::now()) {
3332
// The displayTimeUnit must be ns to avoid low-precision overlap
3433
// calculations!
35-
Out << R"({"displayTimeUnit":"ns","traceEvents":[)"
36-
<< "\n";
34+
Out.objectBegin();
35+
Out.attribute("displayTimeUnit", "ns");
36+
Out.attributeBegin("traceEvents");
37+
Out.arrayBegin();
3738
rawEvent("M", llvm::json::Object{
3839
{"name", "process_name"},
3940
{"args", llvm::json::Object{{"name", "clangd"}}},
4041
});
4142
}
4243

4344
~JSONTracer() {
44-
Out << "\n]}";
45+
Out.arrayEnd();
46+
Out.attributeEnd();
47+
Out.objectEnd();
4548
Out.flush();
4649
}
4750

@@ -73,7 +76,7 @@ class JSONTracer : public EventTracer {
7376
Contents["ts"] = Timestamp ? Timestamp : timestamp();
7477
Contents["tid"] = int64_t(TID);
7578
std::lock_guard<std::mutex> Lock(Mu);
76-
rawEvent(Phase, std::move(Contents));
79+
rawEvent(Phase, Contents);
7780
}
7881

7982
private:
@@ -145,13 +148,14 @@ class JSONTracer : public EventTracer {
145148
// Record an event. ph and pid are set.
146149
// Contents must be a list of the other JSON key/values.
147150
void rawEvent(llvm::StringRef Phase,
148-
llvm::json::Object &&Event) /*REQUIRES(Mu)*/ {
151+
const llvm::json::Object &Event) /*REQUIRES(Mu)*/ {
149152
// PID 0 represents the clangd process.
150-
Event["pid"] = 0;
151-
Event["ph"] = Phase;
152-
Out << Sep
153-
<< llvm::formatv(JSONFormat, llvm::json::Value(std::move(Event)));
154-
Sep = ",\n";
153+
Out.object([&]{
154+
Out.attribute("pid", 0);
155+
Out.attribute("ph", Phase);
156+
for (const auto& KV : Event)
157+
Out.attribute(KV.first, KV.second);
158+
});
155159
}
156160

157161
// If we haven't already, emit metadata describing this thread.
@@ -177,11 +181,9 @@ class JSONTracer : public EventTracer {
177181
}
178182

179183
std::mutex Mu;
180-
llvm::raw_ostream &Out /*GUARDED_BY(Mu)*/;
181-
const char *Sep /*GUARDED_BY(Mu)*/;
184+
llvm::json::OStream Out /*GUARDED_BY(Mu)*/;
182185
llvm::DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/;
183186
const llvm::sys::TimePoint<> Start;
184-
const char *JSONFormat;
185187
};
186188

187189
Key<std::unique_ptr<JSONTracer::JSONSpan>> JSONTracer::SpanKey;

clang-tools-extra/test/clangd/trace.test

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33
---
44
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.c","languageId":"c","version":1,"text":"void main() {}"}}}
55
# These assertions are a bit loose, to avoid brittleness.
6-
# CHECK: {"displayTimeUnit":"ns","traceEvents":[
76
# CHECK: {
8-
# CHECK: "args": {
9-
# CHECK: "File": "{{.*(/|\\)}}foo.c"
10-
# CHECK: },
11-
# CHECK: "name": "BuildPreamble",
12-
# CHECK: "ph": "X",
7+
# CHECK: "displayTimeUnit": "ns",
8+
# CHECK: "traceEvents": [
9+
# CHECK: {
10+
# CHECK: "ph": "X",
11+
# CHECK: "name": "BuildPreamble",
12+
# CHECK: "args": {
13+
# CHECK: "File": "{{.*(/|\\)}}foo.c"
14+
# CHECK: },
15+
# CHECK: }
16+
# CHECK: {
17+
# CHECK: "ph": "X",
18+
# CHECK: "name": "BuildAST",
19+
# CHECK: "args": {
20+
# CHECK: "File": "{{.*(/|\\)}}foo.c"
21+
# CHECK: },
22+
# CHECK: }
23+
# CHECK: ]
1324
# CHECK: }
14-
# CHECK: {
15-
# CHECK: "args": {
16-
# CHECK: "File": "{{.*(/|\\)}}foo.c"
17-
# CHECK: },
18-
# CHECK: "name": "BuildAST",
19-
# CHECK: "ph": "X",
20-
# CHECK: }
21-
# CHECK: },
2225
---
2326
{"jsonrpc":"2.0","id":5,"method":"shutdown"}
2427
---

0 commit comments

Comments
 (0)