[coverage] Improve source range precision

This CL improves reported source range precision in a couple of ways:

Source ranges are now standardized to consist of an inclusive start
index and an exclusive end index (similar to what's reported for
functions). For example:

0123456789  // Offset.
{ f(); }    // Block represented as range {0,8}.

Duplicate singleton ranges (i.e. same start and end offsets) are now
merged (this only becomes relevant once jump statement coverage is
added). For example:

for (.) break;  // Break- and loop continuation have same positions.

SourceRangeScope incorrectly collected starting position
(unconditionally) and end position (when no semi-colon was present).

01234567890123  // Offset.
for (.) break   // Loop body range is {8,13}, was {6,9}.

Bug: v8:6000
Change-Id: I62e7c70cc894a20f318330a2fbbcedc47da2b5db
Reviewed-on: https://chromium-review.googlesource.com/541358
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46095}
This commit is contained in:
jgruber 2017-06-21 14:14:47 +02:00 committed by Commit Bot
parent d1c2c8ed8f
commit 63a7fa5aa3
6 changed files with 237 additions and 146 deletions

View File

@ -181,13 +181,15 @@ class AstProperties final BASE_EMBEDDED {
DEFINE_OPERATORS_FOR_FLAGS(AstProperties::Flags)
// Specifies a range within the source code. {start} is 0-based and inclusive,
// {end} is 0-based and exclusive.
struct SourceRange {
SourceRange() : SourceRange(kNoSourcePosition, kNoSourcePosition) {}
SourceRange(int start, int end) : start(start), end(end) {}
bool IsEmpty() const { return start == kNoSourcePosition; }
static SourceRange ContinuationOf(const SourceRange& that) {
return that.IsEmpty() ? SourceRange()
: SourceRange(that.end + 1, kNoSourcePosition);
: SourceRange(that.end, kNoSourcePosition);
}
int32_t start, end;
};

View File

@ -1933,7 +1933,7 @@ void Shell::WriteLcovData(v8::Isolate* isolate, const char* file) {
for (size_t k = 0; k < function_data.BlockCount(); k++) {
debug::Coverage::BlockData block_data = function_data.GetBlockData(k);
int start_line = LineFromOffset(script, block_data.StartOffset());
int end_line = LineFromOffset(script, block_data.EndOffset());
int end_line = LineFromOffset(script, block_data.EndOffset() - 1);
uint32_t count = block_data.Count();
WriteLcovDataForRange(lines, start_line, end_line, count);
}

View File

@ -66,16 +66,50 @@ bool CompareCoverageBlock(const CoverageBlock& a, const CoverageBlock& b) {
return a.start < b.start;
}
bool HaveSameSourceRange(const CoverageBlock& lhs, const CoverageBlock& rhs) {
return lhs.start == rhs.start && lhs.end == rhs.end;
}
void MergeDuplicateSingletons(std::vector<CoverageBlock>& blocks) {
int from = 1;
int to = 1;
while (from < static_cast<int>(blocks.size())) {
CoverageBlock& prev_block = blocks[to - 1];
CoverageBlock& this_block = blocks[from];
// Identical ranges should only occur through singleton ranges. Consider the
// ranges for `for (.) break;`: continuation ranges for both the `break` and
// `for` statements begin after the trailing semicolon.
// Such ranges are merged and keep the maximal execution count.
if (HaveSameSourceRange(prev_block, this_block)) {
DCHECK_EQ(kNoSourcePosition, this_block.end); // Singleton range.
prev_block.count = std::max(prev_block.count, this_block.count);
from++; // Do not advance {to} cursor.
continue;
}
DCHECK(!HaveSameSourceRange(prev_block, this_block));
// Copy if necessary.
if (from != to) blocks[to] = blocks[from];
from++;
to++;
}
blocks.resize(to);
}
std::vector<CoverageBlock> GetSortedBlockData(Isolate* isolate,
SharedFunctionInfo* shared) {
DCHECK(FLAG_block_coverage);
DCHECK(shared->HasCoverageInfo());
std::vector<CoverageBlock> result;
CoverageInfo* coverage_info =
CoverageInfo::cast(shared->GetDebugInfo()->coverage_info());
std::vector<CoverageBlock> result;
if (coverage_info->SlotCount() == 0) return result;
for (int i = 0; i < coverage_info->SlotCount(); i++) {
const int start_pos = coverage_info->StartSourcePosition(i);
const int until_pos = coverage_info->EndSourcePosition(i);
@ -88,6 +122,12 @@ std::vector<CoverageBlock> GetSortedBlockData(Isolate* isolate,
// Sort according to the block nesting structure.
std::sort(result.begin(), result.end(), CompareCoverageBlock);
// Remove duplicate singleton ranges, keeping the max count.
MergeDuplicateSingletons(result);
// TODO(jgruber): Merge consecutive ranges with identical counts, remove empty
// ranges.
return result;
}
@ -103,19 +143,21 @@ void RewritePositionSingletonsToRanges(CoverageFunction* function) {
for (int i = 0; i < blocks_count; i++) {
CoverageBlock& block = function->blocks[i];
while (nesting_stack.back().end < block.start) {
while (nesting_stack.back().end <= block.start) {
nesting_stack.pop_back();
}
const SourceRange& parent_range = nesting_stack.back();
DCHECK(block.start != kNoSourcePosition);
DCHECK_NE(block.start, kNoSourcePosition);
DCHECK_LE(block.end, parent_range.end);
if (block.end == kNoSourcePosition) {
// The current block ends at the next sibling block (if it exists) or the
// end of the parent block otherwise.
if (i < blocks_count - 1 &&
function->blocks[i + 1].start <= parent_range.end) {
block.end = function->blocks[i + 1].start - 1;
function->blocks[i + 1].start < parent_range.end) {
block.end = function->blocks[i + 1].start;
} else {
block.end = parent_range.end;
}

View File

@ -18,6 +18,7 @@ class Isolate;
struct CoverageBlock {
CoverageBlock(int s, int e, uint32_t c) : start(s), end(e), count(c) {}
CoverageBlock() : CoverageBlock(kNoSourcePosition, kNoSourcePosition, 0) {}
int start;
int end;
uint32_t count;

View File

@ -87,13 +87,15 @@ struct FormalParametersBase {
class SourceRangeScope final {
public:
enum PositionKind {
POSITION,
PEEK_POS,
POSITION_BEG,
POSITION_END,
PEEK_POSITION_BEG,
PEEK_POSITION_END,
};
SourceRangeScope(Scanner* scanner, SourceRange* range,
PositionKind pre_kind = PEEK_POS,
PositionKind post_kind = POSITION)
PositionKind pre_kind = PEEK_POSITION_BEG,
PositionKind post_kind = POSITION_END)
: scanner_(scanner), range_(range), post_kind_(post_kind) {
range_->start = GetPosition(pre_kind);
DCHECK_NE(range_->start, kNoSourcePosition);
@ -106,11 +108,15 @@ class SourceRangeScope final {
private:
int32_t GetPosition(PositionKind kind) {
switch (post_kind_) {
case POSITION:
switch (kind) {
case POSITION_BEG:
return scanner_->location().beg_pos;
case PEEK_POS:
case POSITION_END:
return scanner_->location().end_pos;
case PEEK_POSITION_BEG:
return scanner_->peek_location().beg_pos;
case PEEK_POSITION_END:
return scanner_->peek_location().end_pos;
default:
UNREACHABLE();
}

View File

@ -26,6 +26,8 @@ function TestCoverage(name, source, expectation) {
assertEquals(stringified_expectation, stringified_result, name + " failed");
}
function nop() {}
%DebugToggleBlockCoverage(true);
TestCoverage(
@ -45,49 +47,47 @@ TestCoverage(
TestCoverage(
"if statements",
`
function g() {}
function f(x) {
if (x == 42) {
if (x == 43) g(); else g();
}
if (x == 42) { g(); } else { g(); }
if (x == 42) g(); else g();
if (false) g(); else g();
if (false) g();
if (true) g(); else g();
if (true) g();
}
f(42);
f(43);
function g() {} // 0000
function f(x) { // 0050
if (x == 42) { // 0100
if (x == 43) g(); else g(); // 0150
} // 0200
if (x == 42) { g(); } else { g(); } // 0250
if (x == 42) g(); else g(); // 0300
if (false) g(); else g(); // 0350
if (false) g(); // 0400
if (true) g(); else g(); // 0450
if (true) g(); // 0500
} // 0550
f(42); // 0600
f(43); // 0650
`,
[{"start":0,"end":258,"count":1},
[{"start":0,"end":699,"count":1},
{"start":0,"end":15,"count":11},
{"start":16,"end":244,"count":2},
{"start":45,"end":83,"count":1},
{"start":64,"end":69,"count":0},
{"start":71,"end":79,"count":1},
{"start":80,"end":83,"count":1},
{"start":84,"end":97,"count":2},
{"start":98,"end":107,"count":1},
{"start":109,"end":121,"count":1},
{"start":122,"end":135,"count":2},
{"start":136,"end":141,"count":1},
{"start":143,"end":151,"count":1},
{"start":152,"end":163,"count":2},
{"start":164,"end":169,"count":0},
{"start":171,"end":179,"count":2},
{"start":180,"end":191,"count":2},
{"start":192,"end":197,"count":0},
{"start":198,"end":208,"count":2},
{"start":209,"end":214,"count":2},
{"start":216,"end":224,"count":0},
{"start":225,"end":235,"count":2},
{"start":236,"end":241,"count":2},
{"start":242,"end":244,"count":2}]
{"start":50,"end":551,"count":2},
{"start":115,"end":203,"count":1},
{"start":167,"end":171,"count":0},
{"start":177,"end":181,"count":1},
{"start":181,"end":203,"count":1},
{"start":203,"end":265,"count":2},
{"start":265,"end":273,"count":1},
{"start":279,"end":287,"count":1},
{"start":287,"end":315,"count":2},
{"start":315,"end":319,"count":1},
{"start":325,"end":329,"count":1},
{"start":329,"end":363,"count":2},
{"start":363,"end":367,"count":0},
{"start":373,"end":377,"count":2},
{"start":377,"end":413,"count":2},
{"start":413,"end":417,"count":0},
{"start":417,"end":462,"count":2},
{"start":462,"end":466,"count":2},
{"start":472,"end":476,"count":0},
{"start":476,"end":512,"count":2},
{"start":512,"end":516,"count":2},
{"start":516,"end":551,"count":2}]
);
function nop() {}
TestCoverage(
"if statement (early return)",
`
@ -102,67 +102,106 @@ TestCoverage(
`,
[{"start":0,"end":399,"count":1},
{"start":1,"end":351,"count":1},
{"start":60,"end":252,"count":1},
{"start":62,"end":253,"count":1},
{"start":253,"end":351,"count":0}]
);
TestCoverage(
"if statement (no semi-colon)",
`
!function() { // 0000
if (true) nop() // 0050
if (true) nop(); else nop() // 0100
nop(); // 0150
}() // 0200
`,
[{"start":0,"end":249,"count":1},
{"start":1,"end":201,"count":1},
{"start":62,"end":67,"count":1},
{"start":67,"end":112,"count":1},
{"start":112,"end":118,"count":1},
{"start":124,"end":129,"count":0},
{"start":129,"end":201,"count":1}]
);
TestCoverage(
"for statements",
`
function g() {}
!function() {
for (var i = 0; i < 12; i++) g();
for (var i = 0; i < 12; i++) {
g();
}
for (var i = 0; false; i++) g();
for (var i = 0; true; i++) break;
for (var i = 0; i < 12; i++) {
if (i % 3 == 0) g(); else g();
}
}();
function g() {} // 0000
!function() { // 0050
for (var i = 0; i < 12; i++) g(); // 0100
for (var i = 0; i < 12; i++) { // 0150
g(); // 0200
} // 0250
for (var i = 0; false; i++) g(); // 0300
for (var i = 0; true; i++) break; // 0350
for (var i = 0; i < 12; i++) { // 0400
if (i % 3 == 0) g(); else g(); // 0450
} // 0500
}(); // 0550
`,
[{"start":0,"end":259,"count":1},
[{"start":0,"end":599,"count":1},
{"start":0,"end":15,"count":36},
{"start":17,"end":256,"count":1},
{"start":59,"end":64,"count":12},
{"start":65,"end":94,"count":1},
{"start":95,"end":110,"count":12},
{"start":111,"end":139,"count":1},
{"start":140,"end":145,"count":0},
{"start":146,"end":173,"count":1},
{"start":174,"end":181,"count":1},
{"start":182,"end":211,"count":1},
{"start":212,"end":253,"count":12},
{"start":234,"end":239,"count":4},
{"start":241,"end":249,"count":8},
{"start":250,"end":253,"count":12},
{"start":254,"end":256,"count":1}]
{"start":51,"end":551,"count":1},
{"start":131,"end":135,"count":12},
{"start":135,"end":181,"count":1},
{"start":181,"end":253,"count":12},
{"start":253,"end":330,"count":1},
{"start":330,"end":334,"count":0},
{"start":334,"end":379,"count":1},
{"start":379,"end":385,"count":1},
{"start":385,"end":431,"count":1},
{"start":431,"end":503,"count":12},
{"start":470,"end":474,"count":4},
{"start":480,"end":484,"count":8},
{"start":484,"end":503,"count":12},
{"start":503,"end":551,"count":1}]
);
TestCoverage(
"for statements pt. 2",
`
function g() {}
!function() {
let j = 0;
for (let i = 0; i < 12; i++) g();
for (const i = 0; j < 12; j++) g();
for (j = 0; j < 12; j++) g();
for (;;) break;
}();
function g() {} // 0000
!function() { // 0050
let j = 0; // 0100
for (let i = 0; i < 12; i++) g(); // 0150
for (const i = 0; j < 12; j++) g(); // 0200
for (j = 0; j < 12; j++) g(); // 0250
for (;;) break; // 0300
}(); // 0350
`,
[{"start":0,"end":171,"count":1},
[{"start":0,"end":399,"count":1},
{"start":0,"end":15,"count":36},
{"start":17,"end":168,"count":1},
{"start":72,"end":77,"count":12},
{"start":78,"end":109,"count":1},
{"start":110,"end":115,"count":12},
{"start":116,"end":141,"count":1},
{"start":142,"end":147,"count":12},
{"start":148,"end":157,"count":1},
{"start":158,"end":165,"count":1},
{"start":166,"end":168,"count":1}]
{"start":51,"end":351,"count":1},
{"start":181,"end":185,"count":12},
{"start":185,"end":233,"count":1},
{"start":233,"end":237,"count":12},
{"start":237,"end":277,"count":1},
{"start":277,"end":281,"count":12},
{"start":281,"end":311,"count":1},
{"start":311,"end":317,"count":1},
{"start":317,"end":351,"count":1}]
);
TestCoverage(
"for statements (no semicolon)",
`
function g() {} // 0000
!function() { // 0050
for (let i = 0; i < 12; i++) g() // 0100
for (let i = 0; i < 12; i++) break // 0150
for (let i = 0; i < 12; i++) break; g() // 0200
}(); // 0250
`,
[{"start":0,"end":299,"count":1},
{"start":0,"end":15,"count":13},
{"start":51,"end":251,"count":1},
{"start":131,"end":134,"count":12},
{"start":134,"end":181,"count":1},
{"start":181,"end":186,"count":1},
{"start":186,"end":231,"count":1},
{"start":231,"end":237,"count":1},
{"start":237,"end":251,"count":1}]
);
TestCoverage(
@ -191,50 +230,51 @@ TestCoverage(
`,
[{"start":0,"end":999,"count":1},
{"start":1,"end":951,"count":1},
{"start":79,"end":252,"count":10},
{"start":253,"end":358,"count":1},
{"start":359,"end":552,"count":1},
{"start":553,"end":658,"count":1},
{"start":659,"end":852,"count":1},
{"start":81,"end":253,"count":10},
{"start":253,"end":361,"count":1},
{"start":361,"end":553,"count":1},
{"start":553,"end":661,"count":1},
{"start":661,"end":853,"count":1},
{"start":853,"end":951,"count":0}]
);
TestCoverage(
"while and do-while statements",
`
function g() {}
!function() {
var i;
i = 0; while (i < 12) i++;
i = 0; while (i < 12) { g(); i++; }
i = 0; while (false) g();
i = 0; while (true) break;
i = 0; do i++; while (i < 12);
i = 0; do { g(); i++; } while (i < 12);
i = 0; do { g(); } while (false);
i = 0; do { break; } while (true);
}();
function g() {} // 0000
!function() { // 0050
var i; // 0100
i = 0; while (i < 12) i++; // 0150
i = 0; while (i < 12) { g(); i++; } // 0200
i = 0; while (false) g(); // 0250
i = 0; while (true) break; // 0300
// 0350
i = 0; do i++; while (i < 12); // 0400
i = 0; do { g(); i++; } // 0450
while (i < 12); // 0500
i = 0; do { g(); } while (false); // 0550
i = 0; do { break; } while (true); // 0600
}(); // 0650
`,
[{"start":0,"end":316,"count":1},
[{"start":0,"end":699,"count":1},
{"start":0,"end":15,"count":25},
{"start":17,"end":313,"count":1},
{"start":61,"end":66,"count":12},
{"start":67,"end":89,"count":1},
{"start":90,"end":104,"count":12},
{"start":105,"end":126,"count":1},
{"start":127,"end":132,"count":0},
{"start":133,"end":153,"count":1},
{"start":154,"end":161,"count":1},
{"start":162,"end":172,"count":1},
{"start":173,"end":179,"count":12},
{"start":180,"end":205,"count":1},
{"start":206,"end":221,"count":12},
{"start":222,"end":247,"count":1},
{"start":248,"end":258,"count":1},
{"start":259,"end":283,"count":1},
{"start":284,"end":296,"count":1},
{"start":297,"end":313,"count":1}]
{"start":51,"end":651,"count":1},
{"start":174,"end":178,"count":12},
{"start":178,"end":224,"count":1},
{"start":224,"end":237,"count":12},
{"start":237,"end":273,"count":1},
{"start":273,"end":277,"count":0},
{"start":277,"end":322,"count":1},
{"start":322,"end":328,"count":1},
{"start":328,"end":412,"count":1},
{"start":412,"end":416,"count":12},
{"start":416,"end":462,"count":1},
{"start":462,"end":475,"count":12},
{"start":475,"end":562,"count":1},
{"start":562,"end":570,"count":1},
{"start":570,"end":612,"count":1},
{"start":612,"end":622,"count":1},
{"start":622,"end":651,"count":1}]
);
TestCoverage(
@ -264,11 +304,11 @@ TestCoverage(
`,
[{"start":0,"end":1049,"count":1},
{"start":1,"end":1001,"count":1},
{"start":115,"end":302,"count":10},
{"start":303,"end":412,"count":1},
{"start":413,"end":602,"count":1},
{"start":603,"end":712,"count":1},
{"start":713,"end":902,"count":1},
{"start":117,"end":303,"count":10},
{"start":303,"end":415,"count":1},
{"start":415,"end":603,"count":1},
{"start":603,"end":715,"count":1},
{"start":715,"end":903,"count":1},
{"start":903,"end":1001,"count":0}]
);
@ -299,11 +339,11 @@ TestCoverage(
`,
[{"start":0,"end":1049,"count":1},
{"start":1,"end":1001,"count":1},
{"start":102,"end":302,"count":10},
{"start":303,"end":401,"count":1},
{"start":402,"end":602,"count":1},
{"start":603,"end":701,"count":1},
{"start":702,"end":902,"count":1},
{"start":105,"end":303,"count":10},
{"start":303,"end":405,"count":1},
{"start":405,"end":603,"count":1},
{"start":603,"end":705,"count":1},
{"start":705,"end":903,"count":1},
{"start":903,"end":1001,"count":0}]
);