-
Notifications
You must be signed in to change notification settings - Fork 0
merge coverage across llvm-cov data[] entries #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cb896ee
merge coverage across llvm-cov data[] entries to fix per-binary doubl…
mcharytoniuk 8009ff3
make segment merge order deterministic for tied positions
mcharytoniuk aae83fd
sort region-entry segments first to preserve skipped-region semantics
mcharytoniuk 53133b6
fix test name grammar
mcharytoniuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // llvm-cov export emits one `data[]` entry per profiled binary. The static | ||
| // region table for a given source file is identical across entries, so a file | ||
| // touched by multiple binaries appears with the same segment coordinates but | ||
| // potentially different `count` and `hasCount` values. Aggregating segments by | ||
| // position lets `lineCoverageFromSegments` and `regionCoverageFromSegments` | ||
| // score the union of coverage across binaries without double-counting the | ||
| // denominator. | ||
| // | ||
| // llvm-cov segment tuple: [line, column, count, hasCount, isRegionEntry, isGapRegion] | ||
|
|
||
| const LINE = 0; | ||
| const COLUMN = 1; | ||
| const COUNT = 2; | ||
| const HAS_COUNT = 3; | ||
| const IS_REGION_ENTRY = 4; | ||
| const IS_GAP_REGION = 5; | ||
|
|
||
| /** | ||
| * @param {import("./line-coverage-from-segments.mjs").Segment} segment | ||
| * @returns {string} | ||
| */ | ||
| function segmentPositionKey(segment) { | ||
| return `${segment[LINE]}:${segment[COLUMN]}:${segment[IS_REGION_ENTRY]}:${segment[IS_GAP_REGION]}`; | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("./line-coverage-from-segments.mjs").Segment} left | ||
| * @param {import("./line-coverage-from-segments.mjs").Segment} right | ||
| * @returns {number} | ||
| */ | ||
| function compareSegmentsByPosition(left, right) { | ||
| if (left[LINE] !== right[LINE]) { | ||
| return left[LINE] - right[LINE]; | ||
| } | ||
|
|
||
| if (left[COLUMN] !== right[COLUMN]) { | ||
| return left[COLUMN] - right[COLUMN]; | ||
| } | ||
|
|
||
| if (left[IS_REGION_ENTRY] !== right[IS_REGION_ENTRY]) { | ||
| return Number(right[IS_REGION_ENTRY]) - Number(left[IS_REGION_ENTRY]); | ||
| } | ||
|
|
||
| return Number(left[IS_GAP_REGION]) - Number(right[IS_GAP_REGION]); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("./line-coverage-from-segments.mjs").Segment[][]} segmentArrays | ||
| * @returns {import("./line-coverage-from-segments.mjs").Segment[]} | ||
| */ | ||
| export function mergeSegmentsAcrossEntries(segmentArrays) { | ||
| /** @type {Map<string, import("./line-coverage-from-segments.mjs").Segment>} */ | ||
| const mergedByPosition = new Map(); | ||
|
|
||
| for (const segments of segmentArrays) { | ||
| for (const segment of segments) { | ||
| const key = segmentPositionKey(segment); | ||
| const existing = mergedByPosition.get(key); | ||
|
|
||
| if (existing) { | ||
| existing[COUNT] = Math.max(existing[COUNT], segment[COUNT]); | ||
| existing[HAS_COUNT] = existing[HAS_COUNT] || segment[HAS_COUNT]; | ||
| } else { | ||
| mergedByPosition.set(key, [ | ||
| segment[LINE], | ||
| segment[COLUMN], | ||
| segment[COUNT], | ||
| segment[HAS_COUNT], | ||
| segment[IS_REGION_ENTRY], | ||
| segment[IS_GAP_REGION], | ||
| ]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return [...mergedByPosition.values()].sort(compareSegmentsByPosition); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| { | ||
| "data": [ | ||
| { | ||
| "files": [ | ||
| { | ||
| "filename": "/workspace/shared_crate/src/lib.rs", | ||
| "segments": [ | ||
| [10, 5, 3, true, true, false], | ||
| [12, 6, 0, false, false, false], | ||
| [20, 5, 0, true, true, false], | ||
| [22, 6, 0, false, false, false] | ||
| ] | ||
| } | ||
| ], | ||
| "functions": [ | ||
| { | ||
| "name": "_RNvCsbinary0_shared_crate_foo", | ||
| "count": 3, | ||
| "filenames": ["/workspace/shared_crate/src/lib.rs"], | ||
| "regions": [[10, 5, 12, 6, 3, 0, 0, 0]] | ||
| }, | ||
| { | ||
| "name": "_RNvCsbinary0_shared_crate_bar", | ||
| "count": 0, | ||
| "filenames": ["/workspace/shared_crate/src/lib.rs"], | ||
| "regions": [[20, 5, 22, 6, 0, 0, 0, 0]] | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "files": [ | ||
| { | ||
| "filename": "/workspace/shared_crate/src/lib.rs", | ||
| "segments": [ | ||
| [10, 5, 0, true, true, false], | ||
| [12, 6, 0, false, false, false], | ||
| [20, 5, 0, true, true, false], | ||
| [22, 6, 0, false, false, false] | ||
| ] | ||
| } | ||
| ], | ||
| "functions": [ | ||
| { | ||
| "name": "_RNvCsbinary1_shared_crate_foo", | ||
| "count": 0, | ||
| "filenames": ["/workspace/shared_crate/src/lib.rs"], | ||
| "regions": [[10, 5, 12, 6, 0, 0, 0, 0]] | ||
| }, | ||
| { | ||
| "name": "_RNvCsbinary1_shared_crate_bar", | ||
| "count": 0, | ||
| "filenames": ["/workspace/shared_crate/src/lib.rs"], | ||
| "regions": [[20, 5, 22, 6, 0, 0, 0, 0]] | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "type": "llvm.coverage.json.export", | ||
| "version": "3.0.1", | ||
| "cargo_llvm_cov": {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { strict as assert } from "node:assert"; | ||
| import { test } from "node:test"; | ||
|
|
||
| import { mergeSegmentsAcrossEntries } from "../src/merge-segments-across-entries.mjs"; | ||
|
|
||
| // llvm-cov segment tuple: [line, column, count, hasCount, isRegionEntry, isGapRegion] | ||
|
|
||
| test("keeps the highest count when the same position appears in multiple entries", () => { | ||
| const merged = mergeSegmentsAcrossEntries([ | ||
| [[10, 5, 0, true, true, false]], | ||
| [[10, 5, 7, true, true, false]], | ||
| ]); | ||
|
|
||
| assert.deepEqual(merged, [[10, 5, 7, true, true, false]]); | ||
| }); | ||
|
|
||
| test("ORs hasCount across entries when one entry lacks a counted segment at the position", () => { | ||
| const merged = mergeSegmentsAcrossEntries([ | ||
| [[10, 5, 0, false, true, false]], | ||
| [[10, 5, 0, true, true, false]], | ||
| ]); | ||
|
|
||
| assert.deepEqual(merged, [[10, 5, 0, true, true, false]]); | ||
| }); | ||
|
|
||
| test("preserves distinct positions and returns them sorted by line then column", () => { | ||
| const merged = mergeSegmentsAcrossEntries([ | ||
| [ | ||
| [20, 1, 0, true, true, false], | ||
| [10, 5, 0, true, true, false], | ||
| ], | ||
| [[10, 9, 2, true, true, false]], | ||
| ]); | ||
|
|
||
| assert.deepEqual(merged, [ | ||
| [10, 5, 0, true, true, false], | ||
| [10, 9, 2, true, true, false], | ||
| [20, 1, 0, true, true, false], | ||
| ]); | ||
| }); | ||
|
|
||
| test("treats a gap-region segment as distinct from a non-gap segment at the same position", () => { | ||
| const merged = mergeSegmentsAcrossEntries([ | ||
| [[10, 5, 0, true, true, false]], | ||
| [[10, 5, 0, true, true, true]], | ||
| ]); | ||
|
|
||
| assert.deepEqual(merged, [ | ||
| [10, 5, 0, true, true, false], | ||
| [10, 5, 0, true, true, true], | ||
| ]); | ||
| }); | ||
|
|
||
| test("returns an empty array when no entries are provided", () => { | ||
| assert.deepEqual(mergeSegmentsAcrossEntries([]), []); | ||
| }); | ||
|
|
||
| test("orders region-entry segments before non-region segments at the same position so startsSkippedRegion sees the structural marker first", () => { | ||
| const skippedRegionMarker = [10, 5, 0, false, true, false]; | ||
| const nonRegionSegment = [10, 5, 5, true, false, false]; | ||
|
|
||
| const merged = mergeSegmentsAcrossEntries([ | ||
| [nonRegionSegment], | ||
| [skippedRegionMarker], | ||
| ]); | ||
|
|
||
| assert.deepEqual(merged, [skippedRegionMarker, nonRegionSegment]); | ||
| }); | ||
|
|
||
| test("produces identical output regardless of the order of data[] entries with distinct-flag segments at the same line and column", () => { | ||
| const regionEntrySegment = [10, 5, 3, true, true, false]; | ||
| const nonRegionSegment = [10, 5, 0, true, false, false]; | ||
|
|
||
| const forward = mergeSegmentsAcrossEntries([ | ||
| [regionEntrySegment], | ||
| [nonRegionSegment], | ||
| ]); | ||
| const reverse = mergeSegmentsAcrossEntries([ | ||
| [nonRegionSegment], | ||
| [regionEntrySegment], | ||
| ]); | ||
|
|
||
| assert.deepEqual(forward, reverse); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.