[llvm] r276832 - [llvm-cov] Escape '\' in strings when emitting JSON

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 26 21:08:33 PDT 2016


Author: vedantk
Date: Tue Jul 26 23:08:32 2016
New Revision: 276832

URL: http://llvm.org/viewvc/llvm-project?rev=276832&view=rev
Log:
[llvm-cov] Escape '\' in strings when emitting JSON

Test that Windows path separators are escaped properly. Add a round-trip
test to verify the JSON produced by the exporter.

Modified:
    llvm/trunk/test/tools/llvm-cov/showLineExecutionCounts.cpp
    llvm/trunk/tools/llvm-cov/CoverageExporterJson.cpp

Modified: llvm/trunk/test/tools/llvm-cov/showLineExecutionCounts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/showLineExecutionCounts.cpp?rev=276832&r1=276831&r2=276832&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/showLineExecutionCounts.cpp (original)
+++ llvm/trunk/test/tools/llvm-cov/showLineExecutionCounts.cpp Tue Jul 26 23:08:32 2016
@@ -70,4 +70,6 @@ int main() {
 // HTML-WHOLE-FILE: <td class='uncovered-line'></td><td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='code'><pre>// after
 // HTML-FILTER-NOT: <td class='uncovered-line'></td><td class='line-number'><a name='L[[@LINE-45]]'><pre>[[@LINE-45]]</pre></a></td><td class='code'><pre>// after
 
-// RUN: llvm-cov export %S/Inputs/lineExecutionCounts.covmapping -instr-profile %t.profdata -name=main 2>/dev/null | FileCheck %S/Inputs/lineExecutionCounts.json
+// RUN: llvm-cov export %S/Inputs/lineExecutionCounts.covmapping -instr-profile %t.profdata -name=main 2>/dev/null > %t.export.json
+// RUN: FileCheck -input-file %t.export.json %S/Inputs/lineExecutionCounts.json
+// RUN: cat %t.export.json | %python -c "import json, sys; json.loads(sys.stdin.read())"

Modified: llvm/trunk/tools/llvm-cov/CoverageExporterJson.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cov/CoverageExporterJson.cpp?rev=276832&r1=276831&r2=276832&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cov/CoverageExporterJson.cpp (original)
+++ llvm/trunk/tools/llvm-cov/CoverageExporterJson.cpp Tue Jul 26 23:08:32 2016
@@ -86,7 +86,16 @@ class CoverageExporterJson {
   void emitSerialized(const int64_t Value) { OS << Value; }
 
   /// \brief Emit a serialized string.
-  void emitSerialized(const std::string &Value) { OS << "\"" << Value << "\""; }
+  void emitSerialized(const std::string &Value) {
+    OS << "\"";
+    for (char C : Value) {
+      if (C != '\\')
+        OS << C;
+      else
+        OS << "\\\\";
+    }
+    OS << "\"";
+  }
 
   /// \brief Emit a comma if there is a previous element to delimit.
   void emitComma() {
@@ -109,7 +118,8 @@ class CoverageExporterJson {
   /// \brief Emit a dictionary/object key but no value.
   void emitDictKey(const std::string &Key) {
     emitComma();
-    OS << "\"" << Key << "\":";
+    emitSerialized(Key);
+    OS << ":";
     State.pop();
     assert((State.size() >= 1) && "Closed too many JSON elements");
 




More information about the llvm-commits mailing list