[clang-tools-extra] r359202 - [clangd] Use JSON streaming API for Trace rather than pasting strings. NFC

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 25 09:37:07 PDT 2019


Author: sammccall
Date: Thu Apr 25 09:37:07 2019
New Revision: 359202

URL: http://llvm.org/viewvc/llvm-project?rev=359202&view=rev
Log:
[clangd] Use JSON streaming API for Trace rather than pasting strings. NFC

Modified:
    clang-tools-extra/trunk/clangd/Trace.cpp
    clang-tools-extra/trunk/test/clangd/trace.test

Modified: clang-tools-extra/trunk/clangd/Trace.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Trace.cpp?rev=359202&r1=359201&r2=359202&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Trace.cpp (original)
+++ clang-tools-extra/trunk/clangd/Trace.cpp Thu Apr 25 09:37:07 2019
@@ -27,13 +27,14 @@ namespace {
 // Perhaps we should replace this by something that disturbs performance less.
 class JSONTracer : public EventTracer {
 public:
-  JSONTracer(llvm::raw_ostream &Out, bool Pretty)
-      : Out(Out), Sep(""), Start(std::chrono::system_clock::now()),
-        JSONFormat(Pretty ? "{0:2}" : "{0}") {
+  JSONTracer(llvm::raw_ostream &OS, bool Pretty)
+      : Out(OS, Pretty ? 2 : 0), Start(std::chrono::system_clock::now()) {
     // The displayTimeUnit must be ns to avoid low-precision overlap
     // calculations!
-    Out << R"({"displayTimeUnit":"ns","traceEvents":[)"
-        << "\n";
+    Out.objectBegin();
+    Out.attribute("displayTimeUnit", "ns");
+    Out.attributeBegin("traceEvents");
+    Out.arrayBegin();
     rawEvent("M", llvm::json::Object{
                       {"name", "process_name"},
                       {"args", llvm::json::Object{{"name", "clangd"}}},
@@ -41,7 +42,9 @@ public:
   }
 
   ~JSONTracer() {
-    Out << "\n]}";
+    Out.arrayEnd();
+    Out.attributeEnd();
+    Out.objectEnd();
     Out.flush();
   }
 
@@ -73,7 +76,7 @@ public:
     Contents["ts"] = Timestamp ? Timestamp : timestamp();
     Contents["tid"] = int64_t(TID);
     std::lock_guard<std::mutex> Lock(Mu);
-    rawEvent(Phase, std::move(Contents));
+    rawEvent(Phase, Contents);
   }
 
 private:
@@ -145,13 +148,14 @@ private:
   // Record an event. ph and pid are set.
   // Contents must be a list of the other JSON key/values.
   void rawEvent(llvm::StringRef Phase,
-                llvm::json::Object &&Event) /*REQUIRES(Mu)*/ {
+                const llvm::json::Object &Event) /*REQUIRES(Mu)*/ {
     // PID 0 represents the clangd process.
-    Event["pid"] = 0;
-    Event["ph"] = Phase;
-    Out << Sep
-        << llvm::formatv(JSONFormat, llvm::json::Value(std::move(Event)));
-    Sep = ",\n";
+    Out.object([&]{
+      Out.attribute("pid", 0);
+      Out.attribute("ph", Phase);
+      for (const auto& KV : Event)
+        Out.attribute(KV.first, KV.second);
+    });
   }
 
   // If we haven't already, emit metadata describing this thread.
@@ -177,11 +181,9 @@ private:
   }
 
   std::mutex Mu;
-  llvm::raw_ostream &Out /*GUARDED_BY(Mu)*/;
-  const char *Sep /*GUARDED_BY(Mu)*/;
+  llvm::json::OStream Out /*GUARDED_BY(Mu)*/;
   llvm::DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/;
   const llvm::sys::TimePoint<> Start;
-  const char *JSONFormat;
 };
 
 Key<std::unique_ptr<JSONTracer::JSONSpan>> JSONTracer::SpanKey;

Modified: clang-tools-extra/trunk/test/clangd/trace.test
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clangd/trace.test?rev=359202&r1=359201&r2=359202&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clangd/trace.test (original)
+++ clang-tools-extra/trunk/test/clangd/trace.test Thu Apr 25 09:37:07 2019
@@ -3,22 +3,25 @@
 ---
 {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.c","languageId":"c","version":1,"text":"void main() {}"}}}
 # These assertions are a bit loose, to avoid brittleness.
-# CHECK: {"displayTimeUnit":"ns","traceEvents":[
 # CHECK: {
-# CHECK:   "args": {
-# CHECK:     "File": "{{.*(/|\\)}}foo.c"
-# CHECK:   },
-# CHECK:   "name": "BuildPreamble",
-# CHECK:   "ph": "X",
+# CHECK:   "displayTimeUnit": "ns",
+# CHECK:   "traceEvents": [
+# CHECK:     {
+# CHECK:       "ph": "X",
+# CHECK:       "name": "BuildPreamble",
+# CHECK:       "args": {
+# CHECK:         "File": "{{.*(/|\\)}}foo.c"
+# CHECK:       },
+# CHECK:     }
+# CHECK:     {
+# CHECK:       "ph": "X",
+# CHECK:       "name": "BuildAST",
+# CHECK:       "args": {
+# CHECK:         "File": "{{.*(/|\\)}}foo.c"
+# CHECK:       },
+# CHECK:     }
+# CHECK:   ]
 # CHECK: }
-# CHECK: {
-# CHECK:   "args": {
-# CHECK:     "File": "{{.*(/|\\)}}foo.c"
-# CHECK:   },
-# CHECK:   "name": "BuildAST",
-# CHECK:   "ph": "X",
-# CHECK: }
-# CHECK: },
 ---
 {"jsonrpc":"2.0","id":5,"method":"shutdown"}
 ---




More information about the cfe-commits mailing list