[llvm-cov] Escape '\' in strings when emitting JSON
authorVedant Kumar <vsk@apple.com>
Wed, 27 Jul 2016 04:08:32 +0000 (04:08 +0000)
committerVedant Kumar <vsk@apple.com>
Wed, 27 Jul 2016 04:08:32 +0000 (04:08 +0000)
Test that Windows path separators are escaped properly. Add a round-trip
test to verify the JSON produced by the exporter.

llvm-svn: 276832

llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp
llvm/tools/llvm-cov/CoverageExporterJson.cpp

index f754052..799f56b 100644 (file)
@@ -70,4 +70,6 @@ int main() {                              // TEXT:   161| [[@LINE]]|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())"
index 4ce07b5..7941f28 100644 (file)
@@ -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");