Retry: [llvm-cov] Add support for exporting coverage data to JSON
This enables users to export coverage information as portable JSON for use by
analysis tools and storage in document based databases.
The export sub-command is invoked just like the others:
llvm-cov export -instr-profile path/to/foo.profdata path/to/foo.binary
The resulting JSON contains a list of files and functions. Every file object
contains a list of segments, expansions, and a summary of the file's region,
function, and line coverage. Every function object contains the function's name
and regions. There is also a total summary for the entire object file.
Changes since the initial commit (r276813):
- Fixed the regexes in the tests to handle Windows filepaths.
Patch by Eddie Hurtig!
Differential Revision: https://reviews.llvm.org/D22651
llvm-svn: 276818
diff --git a/llvm/test/tools/llvm-cov/Inputs/binary-formats.canonical.json b/llvm/test/tools/llvm-cov/Inputs/binary-formats.canonical.json
new file mode 100644
index 0000000..cd2b052
--- /dev/null
+++ b/llvm/test/tools/llvm-cov/Inputs/binary-formats.canonical.json
@@ -0,0 +1,38 @@
+// Metadata section
+// CHECK: {"version":"1.0.0","type":"llvm.coverage.json.export","data":[
+
+// Open Export
+// CHECK-SAME: {"object":"{{[^"]+}}","files":[
+
+// File Object
+// CHECK-SAME: {"filename":"{{[^"]+}}binary-formats.c",
+// CHECK-SAME: "segments":[
+// CHECK-SAME: [4,40,100,1,1],[4,42,0,0,0]],
+// CHECK-SAME: "expansions":[],
+
+// Verify the Summary Section for the first file
+// CHECK-SAME: "summary":{
+// CHECK-SAME: "lines":{"count":1,"covered":1,"percent":100,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":1,"covered":1,"notcovered":0,"percent":100}}}
+
+// Close Files Array
+// CHECK-SAME: ],
+
+// Functions List
+// CHECK-SAME: "functions":[
+// CHECK-SAME: {"name":"main","count":100,"regions":[
+// CHECK-SAME: [4,40,4,42,100,0,0,0]
+// CHECK-SAME: ],
+// CHECK-SAME: "filenames":["{{[^"]+}}binary-formats.c"]
+// CHECK-SAME: }],
+
+
+// Full Export Summary
+// CHECK-SAME: "totals":{
+// CHECK-SAME: "lines":{"count":1,"covered":1,"percent":100,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":1,"covered":1,"notcovered":0,"percent":100}}
+
+// Close the export object, data array, and root object
+// CHECK-SAME: }]}
diff --git a/llvm/test/tools/llvm-cov/Inputs/highlightedRanges.json b/llvm/test/tools/llvm-cov/Inputs/highlightedRanges.json
new file mode 100644
index 0000000..4b441ab
--- /dev/null
+++ b/llvm/test/tools/llvm-cov/Inputs/highlightedRanges.json
@@ -0,0 +1,53 @@
+// Metadata section
+// CHECK: {"version":"1.0.0","type":"llvm.coverage.json.export","data":[
+
+// Open Export
+// CHECK-SAME: {"object":"{{[^"]+}}","files":[
+
+// File Object
+// CHECK-SAME: {"filename":"{{[^"]+}}showHighlightedRanges.cpp",
+// CHECK-SAME: "segments":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}],
+// CHECK-SAME: "expansions":[],
+
+// Verify the Summary Section for the first file
+// CHECK-SAME: "summary":{
+// CHECK-SAME: "lines":{"count":40,"covered":26,"percent":65,"noncode":0},
+// CHECK-SAME: "functions":{"count":4,"covered":4,"percent":100},
+// CHECK-SAME: "regions":{"count":19,"covered":11,"notcovered":8,"percent":57}}}
+
+// Close Files Array
+// CHECK-SAME: ],
+
+// Functions List
+// CHECK-SAME: "functions":[
+// CHECK-SAME: {"name":"_Z4funcv","count":1,"regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}
+// CHECK-SAME: ],
+// CHECK-SAME: "filenames":["{{[^"]+}}showHighlightedRanges.cpp"]
+// CHECK-SAME: },
+// CHECK-SAME: {"name":"_Z5func2i","count":1,"regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}
+// CHECK-SAME: ],
+// CHECK-SAME: "filenames":["{{[^"]+}}showHighlightedRanges.cpp"]
+// CHECK-SAME: }
+// CHECK-SAME: {"name":"_Z4testv","count":1,"regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}
+// CHECK-SAME: ],
+// CHECK-SAME: "filenames":["{{[^"]+}}showHighlightedRanges.cpp"]
+// CHECK-SAME: }
+// CHECK-SAME: {"name":"main","count":1,"regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}
+// CHECK-SAME: ],
+// CHECK-SAME: "filenames":["{{.*}}showHighlightedRanges.cpp"]
+// CHECK-SAME: }],
+
+
+// Full Export Summary
+// CHECK-SAME: "totals":{
+// CHECK-SAME: "lines":{"count":40,"covered":26,"percent":65,"noncode":0},
+// CHECK-SAME: "functions":{"count":4,"covered":4,"percent":100},
+// CHECK-SAME: "regions":{"count":19,"covered":11,"notcovered":8,"percent":57}}
+
+// Close the export object, data array, and root object
+// CHECK-SAME: }]}
diff --git a/llvm/test/tools/llvm-cov/Inputs/lineExecutionCounts.json b/llvm/test/tools/llvm-cov/Inputs/lineExecutionCounts.json
new file mode 100644
index 0000000..4beab24
--- /dev/null
+++ b/llvm/test/tools/llvm-cov/Inputs/lineExecutionCounts.json
@@ -0,0 +1,38 @@
+// Metadata section
+// CHECK: {"version":"1.0.0","type":"llvm.coverage.json.export","data":[
+
+// Open Export
+// CHECK-SAME: {"object":"{{[^"]+}}","files":[
+
+// File Object
+// CHECK-SAME: {"filename":"{{[^"]+}}showLineExecutionCounts.cpp",
+// CHECK-SAME: "segments":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}],
+// CHECK-SAME: "expansions":[],
+
+// Verify the Summary Section for the first file
+// CHECK-SAME: "summary":{
+// CHECK-SAME: "lines":{"count":20,"covered":16,"percent":80,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":10,"covered":7,"notcovered":3,"percent":70}}}
+
+// Close Files Array
+// CHECK-SAME: ],
+
+// Functions List
+// CHECK-SAME: "functions":[
+// CHECK-SAME: {"name":"main","count":161,"regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}
+// CHECK-SAME: ],
+// CHECK-SAME: "filenames":["{{[^"]+}}showLineExecutionCounts.cpp"]
+// CHECK-SAME: }],
+
+
+// Full Export Summary
+// CHECK-SAME: "totals":{
+// CHECK-SAME: "lines":{"count":20,"covered":16,"percent":80,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":10,"covered":7,"notcovered":3,"percent":70}}
+
+// Close the export object, data array, and root object
+// CHECK-SAME: }]}
diff --git a/llvm/test/tools/llvm-cov/Inputs/regionMarkers.json b/llvm/test/tools/llvm-cov/Inputs/regionMarkers.json
new file mode 100644
index 0000000..7565c9f
--- /dev/null
+++ b/llvm/test/tools/llvm-cov/Inputs/regionMarkers.json
@@ -0,0 +1,37 @@
+// Metadata section
+// CHECK: {"version":"1.0.0","type":"llvm.coverage.json.export","data":[
+
+// Open Export
+// CHECK-SAME: {"object":"{{[^"]+}}","files":[
+
+// File Object
+// CHECK-SAME: {"filename":"{{[^"]+}}showRegionMarkers.cpp",
+// CHECK-SAME: "segments":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}],
+// CHECK-SAME: "expansions":[],
+
+// Verify the Summary Section for the first file
+// CHECK-SAME: "summary":{
+// CHECK-SAME: "lines":{"count":21,"covered":17,"percent":80,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":10,"covered":7,"notcovered":3,"percent":70}}
+
+// Close Files Array
+// CHECK-SAME: ],
+
+// Functions List
+// CHECK-SAME: "functions":[
+// CHECK-SAME: {"name":"main","count":1111000,"regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}
+// CHECK-SAME: ],
+// CHECK-SAME: "filenames":["{{[^"]+}}showRegionMarkers.cpp"]
+// CHECK-SAME: }],
+
+// Full Export Summary
+// CHECK-SAME: "totals":{
+// CHECK-SAME: "lines":{"count":21,"covered":17,"percent":80,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":10,"covered":7,"notcovered":3,"percent":70}}
+
+// Close the export object, data array, and root object
+// CHECK-SAME: }]}
diff --git a/llvm/test/tools/llvm-cov/Inputs/showExpansions.json b/llvm/test/tools/llvm-cov/Inputs/showExpansions.json
new file mode 100644
index 0000000..738c5e1
--- /dev/null
+++ b/llvm/test/tools/llvm-cov/Inputs/showExpansions.json
@@ -0,0 +1,51 @@
+// Metadata section
+// CHECK: {"version":"1.0.0","type":"llvm.coverage.json.export","data":[
+
+// Open Export
+// CHECK-SAME: {"object":"{{[^"]+}}","files":[
+
+// File Object
+// CHECK-SAME: {"filename":"{{[^"]+}}showExpansions.cpp",
+// CHECK-SAME: "segments":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}],
+// CHECK-SAME: "expansions":[
+// CHECK-SAME: {"source_region":[24,5,24,17,100,0,1,1],
+// CHECK-SAME: "target_regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}
+// CHECK-SAME: ],
+
+// Yes, 4 of the same filename in a row
+// CHECK-SAME: "filenames":[
+// CHECK-SAME: "{{[^"]+}}showExpansions.cpp","{{[^"]+}}showExpansions.cpp",
+// CHECK-SAME: "{{[^"]+}}showExpansions.cpp","{{[^"]+}}showExpansions.cpp"]
+// CHECK-SAME: }],
+
+// Verify the Summary Section for the first file
+// CHECK-SAME: "summary":{
+// CHECK-SAME: "lines":{"count":17,"covered":15,"percent":88,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":13,"covered":12,"notcovered":1,"percent":92}}
+
+// Close Files Array
+// CHECK-SAME: ],
+
+// Functions List
+// CHECK-SAME: "functions":[
+// CHECK-SAME: {"name":"main","count":1,"regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}
+// CHECK-SAME: ],
+// CHECK-SAME: "filenames":[
+// CHECK-SAME: "{{[^"]+}}showExpansions.cpp",
+// CHECK-SAME: "{{[^"]+}}showExpansions.cpp",
+// CHECK-SAME: "{{[^"]+}}showExpansions.cpp",
+// CHECK-SAME: "{{[^"]+}}showExpansions.cpp"]
+// CHECK-SAME: }],
+
+// Full Export Summary
+// CHECK-SAME: "totals":{
+// CHECK-SAME: "lines":{"count":17,"covered":15,"percent":88,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":13,"covered":12,"notcovered":1,"percent":92}}
+
+// Close the export object, data array, and root object
+// CHECK-SAME: }]}
diff --git a/llvm/test/tools/llvm-cov/Inputs/universal-binary.json b/llvm/test/tools/llvm-cov/Inputs/universal-binary.json
new file mode 100644
index 0000000..75d7a2f
--- /dev/null
+++ b/llvm/test/tools/llvm-cov/Inputs/universal-binary.json
@@ -0,0 +1,36 @@
+// Metadata section
+// CHECK: {"version":"1.0.0","type":"llvm.coverage.json.export","data":[
+
+// Open Export
+// CHECK-SAME: {"object":"{{[^"]+}}","files":[
+
+// File Object
+// CHECK-SAME: {"filename":"{{[^"]+}}universal-binary.c",
+// CHECK-SAME: "segments":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}],
+// CHECK-SAME: "expansions":[],
+
+// Verify the Summary Section for the first file
+// CHECK-SAME: "summary":{
+// CHECK-SAME: "lines":{"count":1,"covered":1,"percent":100,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":1,"covered":1,"notcovered":0,"percent":100}}
+
+// Close Files Array
+// CHECK-SAME: ],
+
+// Functions List
+// CHECK-SAME: "functions":[
+// CHECK-SAME: {"name":"main","count":100,"regions":[
+// CHECK-SAME: {{(\[[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+\],?)+}}],
+// CHECK-SAME: "filenames":["{{[^"]+}}universal-binary.c"]
+// CHECK-SAME: }],
+
+// Full Export Summary
+// CHECK-SAME: "totals":{
+// CHECK-SAME: "lines":{"count":1,"covered":1,"percent":100,"noncode":0},
+// CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
+// CHECK-SAME: "regions":{"count":1,"covered":1,"notcovered":0,"percent":100}
+
+// Close the export object, data array, and root object
+// CHECK-SAME: }]}
diff --git a/llvm/test/tools/llvm-cov/binary-formats.c b/llvm/test/tools/llvm-cov/binary-formats.c
index 31c6c4c..cc9e4e0 100644
--- a/llvm/test/tools/llvm-cov/binary-formats.c
+++ b/llvm/test/tools/llvm-cov/binary-formats.c
@@ -7,3 +7,7 @@
// RUN: llvm-cov show %S/Inputs/binary-formats.macho32l -instr-profile %t.profdata -filename-equivalence %s | FileCheck %s
// RUN: llvm-cov show %S/Inputs/binary-formats.macho64l -instr-profile %t.profdata -filename-equivalence %s | FileCheck %s
// RUN: llvm-cov show %S/Inputs/binary-formats.macho32b -instr-profile %t.profdata -filename-equivalence %s | FileCheck %s
+
+// RUN: llvm-cov export %S/Inputs/binary-formats.macho32l -instr-profile %t.profdata | FileCheck %S/Inputs/binary-formats.canonical.json
+// RUN: llvm-cov export %S/Inputs/binary-formats.macho64l -instr-profile %t.profdata | FileCheck %S/Inputs/binary-formats.canonical.json
+// RUN: llvm-cov export %S/Inputs/binary-formats.macho32b -instr-profile %t.profdata | FileCheck %S/Inputs/binary-formats.canonical.json
diff --git a/llvm/test/tools/llvm-cov/showExpansions.cpp b/llvm/test/tools/llvm-cov/showExpansions.cpp
index de3898f..818c0e1 100644
--- a/llvm/test/tools/llvm-cov/showExpansions.cpp
+++ b/llvm/test/tools/llvm-cov/showExpansions.cpp
@@ -24,3 +24,4 @@
DO_SOMETHING(i); // CHECK-DAG: Expansion at line [[@LINE]], 5 -> 17
return 0;
}
+// RUN: llvm-cov export %S/Inputs/showExpansions.covmapping -instr-profile %S/Inputs/showExpansions.profdata 2>&1 | FileCheck %S/Inputs/showExpansions.json
diff --git a/llvm/test/tools/llvm-cov/showHighlightedRanges.cpp b/llvm/test/tools/llvm-cov/showHighlightedRanges.cpp
index b5209d40..dddbc2b 100644
--- a/llvm/test/tools/llvm-cov/showHighlightedRanges.cpp
+++ b/llvm/test/tools/llvm-cov/showHighlightedRanges.cpp
@@ -43,3 +43,5 @@
func2(9);
return 0;
}
+
+// RUN: llvm-cov export %S/Inputs/highlightedRanges.covmapping -instr-profile %S/Inputs/highlightedRanges.profdata 2>&1 | FileCheck %S/Inputs/highlightedRanges.json
diff --git a/llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp b/llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp
index 7a33c31..f754052 100644
--- a/llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp
+++ b/llvm/test/tools/llvm-cov/showLineExecutionCounts.cpp
@@ -69,3 +69,5 @@
// HTML: <td class='covered-line'><pre>161</pre></td><td class='line-number'><a name='L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='code'><pre>}
// 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
diff --git a/llvm/test/tools/llvm-cov/showRegionMarkers.cpp b/llvm/test/tools/llvm-cov/showRegionMarkers.cpp
index 5507367..6c25cdb 100644
--- a/llvm/test/tools/llvm-cov/showRegionMarkers.cpp
+++ b/llvm/test/tools/llvm-cov/showRegionMarkers.cpp
@@ -23,3 +23,5 @@
}
// RUN: llvm-cov show %S/Inputs/regionMarkers.covmapping -instr-profile %t.profdata -show-regions -dump -filename-equivalence %s 2>&1 | FileCheck %s
+
+// RUN: llvm-cov export %S/Inputs/regionMarkers.covmapping -instr-profile %t.profdata 2>&1 | FileCheck %S/Inputs/regionMarkers.json
diff --git a/llvm/test/tools/llvm-cov/universal-binary.c b/llvm/test/tools/llvm-cov/universal-binary.c
index 0d3eb4c..d2701ba 100644
--- a/llvm/test/tools/llvm-cov/universal-binary.c
+++ b/llvm/test/tools/llvm-cov/universal-binary.c
@@ -5,6 +5,8 @@
// RUN: llvm-profdata merge %S/Inputs/universal-binary.proftext -o %t.profdata
// RUN: llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -filename-equivalence %s -arch x86_64 | FileCheck %s
+// RUN: llvm-cov export %S/Inputs/universal-binary -instr-profile %t.profdata -arch x86_64 2>&1 | FileCheck %S/Inputs/universal-binary.json
+
// RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -filename-equivalence %s -arch i386 2>&1 | FileCheck --check-prefix=WRONG-ARCH %s
// WRONG-ARCH: Failed to load coverage