[coverage] add coverage for binary expressions

Adds block-level coverage tracking for binary && and ||
expressions. Introduces a BinaryOperation source-range
for tracking the operations themselves and an Expression
source-range, used for tracking NaryLogical expressions.

This builds on work by jgruber@chromium.org in
the issue.

TBR=marja@chromium.org
R=jgruber@chromium.org, rmcilroy@chromium.org

Bug: v8:6660
Change-Id: I83a81f13a3514a734c06948b2d3e91138fb00e18
Reviewed-on: https://chromium-review.googlesource.com/754564
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49304}
This commit is contained in:
Benjamin 2017-11-09 22:43:16 -08:00 committed by Commit Bot
parent e4b394a1f4
commit 4d3bc552b5
7 changed files with 267 additions and 28 deletions

View File

@ -46,6 +46,7 @@ Andrew Paprocki <andrew@ishiboo.com>
Andrei Kashcha <anvaka@gmail.com>
Anna Henningsen <addaleax@gmail.com>
Bangfu Tao <bangfu.tao@samsung.com>
Ben Coe <ben@npmjs.com>
Ben Noordhuis <info@bnoordhuis.nl>
Benjamin Tan <demoneaux@gmail.com>
Bert Belder <bertbelder@gmail.com>

View File

@ -33,6 +33,7 @@ struct SourceRange {
V(Block) \
V(CaseClause) \
V(Conditional) \
V(Expression) \
V(IfStatement) \
V(IterationStatement) \
V(JumpStatement) \
@ -113,6 +114,20 @@ class ConditionalSourceRanges final : public AstNodeSourceRanges {
SourceRange else_range_;
};
class ExpressionSourceRanges final : public AstNodeSourceRanges {
public:
explicit ExpressionSourceRanges(const SourceRange& body_range)
: body_range_(body_range) {}
SourceRange GetRange(SourceRangeKind kind) {
DCHECK_EQ(kind, SourceRangeKind::kBody);
return body_range_;
}
private:
SourceRange body_range_;
};
class IfStatementSourceRanges final : public AstNodeSourceRanges {
public:
explicit IfStatementSourceRanges(const SourceRange& then_range,

View File

@ -494,6 +494,32 @@ class BytecodeGenerator::ControlScopeForTryFinally final
DeferredCommands* commands_;
};
// Allocate and fetch the coverage indices tracking NaryLogical Expressions.
class BytecodeGenerator::NArayCodeCoverageSlots {
public:
explicit NArayCodeCoverageSlots(BytecodeGenerator* generator,
NaryOperation* expr)
: generator_(generator) {
if (generator_->block_coverage_builder_ == nullptr) return;
for (size_t i = 0; i < expr->subsequent_length(); i++) {
coverage_slots_.push_back(generator_->AllocateBlockCoverageSlotIfEnabled(
expr->subsequent(i), SourceRangeKind::kBody));
}
}
int GetSlotFor(size_t subsequent_expr_index) const {
if (generator_->block_coverage_builder_ == nullptr) {
return BlockCoverageBuilder::kNoCoverageArraySlot;
}
DCHECK(coverage_slots_.size() > subsequent_expr_index);
return coverage_slots_[subsequent_expr_index];
}
private:
BytecodeGenerator* generator_;
std::vector<int> coverage_slots_;
};
void BytecodeGenerator::ControlScope::PerformCommand(Command command,
Statement* statement,
int source_position) {
@ -4096,7 +4122,7 @@ void BytecodeGenerator::VisitNaryCommaExpression(NaryOperation* expr) {
void BytecodeGenerator::VisitLogicalTestSubExpression(
Token::Value token, Expression* expr, BytecodeLabels* then_labels,
BytecodeLabels* else_labels) {
BytecodeLabels* else_labels, int coverage_slot) {
DCHECK(token == Token::OR || token == Token::AND);
BytecodeLabels test_next(zone());
@ -4107,23 +4133,28 @@ void BytecodeGenerator::VisitLogicalTestSubExpression(
VisitForTest(expr, &test_next, else_labels, TestFallthrough::kThen);
}
test_next.Bind(builder());
BuildIncrementBlockCoverageCounterIfEnabled(coverage_slot);
}
void BytecodeGenerator::VisitLogicalTest(Token::Value token, Expression* left,
Expression* right) {
Expression* right,
int right_coverage_slot) {
DCHECK(token == Token::OR || token == Token::AND);
TestResultScope* test_result = execution_result()->AsTest();
BytecodeLabels* then_labels = test_result->then_labels();
BytecodeLabels* else_labels = test_result->else_labels();
TestFallthrough fallthrough = test_result->fallthrough();
VisitLogicalTestSubExpression(token, left, then_labels, else_labels);
VisitLogicalTestSubExpression(token, left, then_labels, else_labels,
right_coverage_slot);
// The last test has the same then, else and fallthrough as the parent test.
VisitForTest(right, then_labels, else_labels, fallthrough);
}
void BytecodeGenerator::VisitNaryLogicalTest(Token::Value token,
NaryOperation* expr) {
void BytecodeGenerator::VisitNaryLogicalTest(
Token::Value token, NaryOperation* expr,
const NArayCodeCoverageSlots* coverage_slots) {
DCHECK(token == Token::OR || token == Token::AND);
DCHECK_GT(expr->subsequent_length(), 0);
@ -4132,18 +4163,21 @@ void BytecodeGenerator::VisitNaryLogicalTest(Token::Value token,
BytecodeLabels* else_labels = test_result->else_labels();
TestFallthrough fallthrough = test_result->fallthrough();
VisitLogicalTestSubExpression(token, expr->first(), then_labels, else_labels);
VisitLogicalTestSubExpression(token, expr->first(), then_labels, else_labels,
coverage_slots->GetSlotFor(0));
for (size_t i = 0; i < expr->subsequent_length() - 1; ++i) {
VisitLogicalTestSubExpression(token, expr->subsequent(i), then_labels,
else_labels);
else_labels,
coverage_slots->GetSlotFor(i + 1));
}
// The last test has the same then, else and fallthrough as the parent test.
VisitForTest(expr->subsequent(expr->subsequent_length() - 1), then_labels,
else_labels, fallthrough);
}
bool BytecodeGenerator::VisitLogicalOrSubExpression(
Expression* expr, BytecodeLabels* end_labels) {
bool BytecodeGenerator::VisitLogicalOrSubExpression(Expression* expr,
BytecodeLabels* end_labels,
int coverage_slot) {
if (expr->ToBooleanIsTrue()) {
VisitForAccumulatorValue(expr);
end_labels->Bind(builder());
@ -4153,11 +4187,15 @@ bool BytecodeGenerator::VisitLogicalOrSubExpression(
builder()->JumpIfTrue(ToBooleanModeFromTypeHint(type_hint),
end_labels->New());
}
BuildIncrementBlockCoverageCounterIfEnabled(coverage_slot);
return false;
}
bool BytecodeGenerator::VisitLogicalAndSubExpression(
Expression* expr, BytecodeLabels* end_labels) {
bool BytecodeGenerator::VisitLogicalAndSubExpression(Expression* expr,
BytecodeLabels* end_labels,
int coverage_slot) {
if (expr->ToBooleanIsFalse()) {
VisitForAccumulatorValue(expr);
end_labels->Bind(builder());
@ -4167,6 +4205,9 @@ bool BytecodeGenerator::VisitLogicalAndSubExpression(
builder()->JumpIfFalse(ToBooleanModeFromTypeHint(type_hint),
end_labels->New());
}
BuildIncrementBlockCoverageCounterIfEnabled(coverage_slot);
return false;
}
@ -4174,19 +4215,25 @@ void BytecodeGenerator::VisitLogicalOrExpression(BinaryOperation* binop) {
Expression* left = binop->left();
Expression* right = binop->right();
int right_coverage_slot =
AllocateBlockCoverageSlotIfEnabled(right, SourceRangeKind::kBody);
if (execution_result()->IsTest()) {
TestResultScope* test_result = execution_result()->AsTest();
if (left->ToBooleanIsTrue()) {
builder()->Jump(test_result->NewThenLabel());
} else if (left->ToBooleanIsFalse() && right->ToBooleanIsFalse()) {
BuildIncrementBlockCoverageCounterIfEnabled(right_coverage_slot);
builder()->Jump(test_result->NewElseLabel());
} else {
VisitLogicalTest(Token::OR, left, right);
VisitLogicalTest(Token::OR, left, right, right_coverage_slot);
}
test_result->SetResultConsumedByTest();
} else {
BytecodeLabels end_labels(zone());
if (VisitLogicalOrSubExpression(left, &end_labels)) return;
if (VisitLogicalOrSubExpression(left, &end_labels, right_coverage_slot)) {
return;
}
VisitForAccumulatorValue(right);
end_labels.Bind(builder());
}
@ -4196,19 +4243,25 @@ void BytecodeGenerator::VisitNaryLogicalOrExpression(NaryOperation* expr) {
Expression* first = expr->first();
DCHECK_GT(expr->subsequent_length(), 0);
NArayCodeCoverageSlots coverage_slots(this, expr);
if (execution_result()->IsTest()) {
TestResultScope* test_result = execution_result()->AsTest();
if (first->ToBooleanIsTrue()) {
builder()->Jump(test_result->NewThenLabel());
} else {
VisitNaryLogicalTest(Token::OR, expr);
VisitNaryLogicalTest(Token::OR, expr, &coverage_slots);
}
test_result->SetResultConsumedByTest();
} else {
BytecodeLabels end_labels(zone());
if (VisitLogicalOrSubExpression(first, &end_labels)) return;
if (VisitLogicalOrSubExpression(first, &end_labels,
coverage_slots.GetSlotFor(0))) {
return;
}
for (size_t i = 0; i < expr->subsequent_length() - 1; ++i) {
if (VisitLogicalOrSubExpression(expr->subsequent(i), &end_labels)) {
if (VisitLogicalOrSubExpression(expr->subsequent(i), &end_labels,
coverage_slots.GetSlotFor(i + 1))) {
return;
}
}
@ -4223,19 +4276,25 @@ void BytecodeGenerator::VisitLogicalAndExpression(BinaryOperation* binop) {
Expression* left = binop->left();
Expression* right = binop->right();
int right_coverage_slot =
AllocateBlockCoverageSlotIfEnabled(right, SourceRangeKind::kBody);
if (execution_result()->IsTest()) {
TestResultScope* test_result = execution_result()->AsTest();
if (left->ToBooleanIsFalse()) {
builder()->Jump(test_result->NewElseLabel());
} else if (left->ToBooleanIsTrue() && right->ToBooleanIsTrue()) {
BuildIncrementBlockCoverageCounterIfEnabled(right_coverage_slot);
builder()->Jump(test_result->NewThenLabel());
} else {
VisitLogicalTest(Token::AND, left, right);
VisitLogicalTest(Token::AND, left, right, right_coverage_slot);
}
test_result->SetResultConsumedByTest();
} else {
BytecodeLabels end_labels(zone());
if (VisitLogicalAndSubExpression(left, &end_labels)) return;
if (VisitLogicalAndSubExpression(left, &end_labels, right_coverage_slot)) {
return;
}
VisitForAccumulatorValue(right);
end_labels.Bind(builder());
}
@ -4245,19 +4304,25 @@ void BytecodeGenerator::VisitNaryLogicalAndExpression(NaryOperation* expr) {
Expression* first = expr->first();
DCHECK_GT(expr->subsequent_length(), 0);
NArayCodeCoverageSlots coverage_slots(this, expr);
if (execution_result()->IsTest()) {
TestResultScope* test_result = execution_result()->AsTest();
if (first->ToBooleanIsFalse()) {
builder()->Jump(test_result->NewElseLabel());
} else {
VisitNaryLogicalTest(Token::AND, expr);
VisitNaryLogicalTest(Token::AND, expr, &coverage_slots);
}
test_result->SetResultConsumedByTest();
} else {
BytecodeLabels end_labels(zone());
if (VisitLogicalAndSubExpression(first, &end_labels)) return;
if (VisitLogicalAndSubExpression(first, &end_labels,
coverage_slots.GetSlotFor(0))) {
return;
}
for (size_t i = 0; i < expr->subsequent_length() - 1; ++i) {
if (VisitLogicalAndSubExpression(expr->subsequent(i), &end_labels)) {
if (VisitLogicalAndSubExpression(expr->subsequent(i), &end_labels,
coverage_slots.GetSlotFor(i + 1))) {
return;
}
}

View File

@ -55,6 +55,7 @@ class BytecodeGenerator final : public AstVisitor<BytecodeGenerator> {
class EffectResultScope;
class FeedbackSlotCache;
class GlobalDeclarationsBuilder;
class NArayCodeCoverageSlots;
class RegisterAllocationScope;
class TestResultScope;
class ValueResultScope;
@ -173,20 +174,23 @@ class BytecodeGenerator final : public AstVisitor<BytecodeGenerator> {
// Visit a logical OR/AND within a test context, rewiring the jumps based
// on the expression values.
void VisitLogicalTest(Token::Value token, Expression* left,
Expression* right);
void VisitNaryLogicalTest(Token::Value token, NaryOperation* expr);
void VisitLogicalTest(Token::Value token, Expression* left, Expression* right,
int right_coverage_slot);
void VisitNaryLogicalTest(Token::Value token, NaryOperation* expr,
const NArayCodeCoverageSlots* coverage_slots);
// Visit a (non-RHS) test for a logical op, which falls through if the test
// fails or jumps to the appropriate labels if it succeeds.
void VisitLogicalTestSubExpression(Token::Value token, Expression* expr,
BytecodeLabels* then_labels,
BytecodeLabels* else_labels);
BytecodeLabels* else_labels,
int coverage_slot);
// Helpers for binary and nary logical op value expressions.
bool VisitLogicalOrSubExpression(Expression* expr,
BytecodeLabels* end_labels);
bool VisitLogicalOrSubExpression(Expression* expr, BytecodeLabels* end_labels,
int coverage_slot);
bool VisitLogicalAndSubExpression(Expression* expr,
BytecodeLabels* end_labels);
BytecodeLabels* end_labels,
int coverage_slot);
// Visit the header/body of a loop iteration.
void VisitIterationHeader(IterationStatement* stmt,
@ -203,6 +207,7 @@ class BytecodeGenerator final : public AstVisitor<BytecodeGenerator> {
void BuildLoadPropertyKey(LiteralProperty* property, Register out_reg);
int AllocateBlockCoverageSlotIfEnabled(AstNode* node, SourceRangeKind kind);
void BuildIncrementBlockCoverageCounterIfEnabled(AstNode* node,
SourceRangeKind kind);
void BuildIncrementBlockCoverageCounterIfEnabled(int coverage_array_slot);

View File

@ -3079,6 +3079,7 @@ template <typename Impl>
typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseBinaryExpression(
int prec, bool accept_IN, bool* ok) {
DCHECK_GE(prec, 4);
SourceRange right_range;
ExpressionT x = ParseUnaryExpression(CHECK_OK);
for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) {
// prec1 >= 4
@ -3086,18 +3087,25 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseBinaryExpression(
impl()->RewriteNonPattern(CHECK_OK);
BindingPatternUnexpectedToken();
ArrowFormalParametersUnexpectedToken();
SourceRangeScope right_range_scope(scanner(), &right_range);
Token::Value op = Next();
int pos = position();
const bool is_right_associative = op == Token::EXP;
const int next_prec = is_right_associative ? prec1 : prec1 + 1;
ExpressionT y = ParseBinaryExpression(next_prec, accept_IN, CHECK_OK);
right_range_scope.Finalize();
impl()->RewriteNonPattern(CHECK_OK);
if (impl()->ShortcutNumericLiteralBinaryExpression(&x, y, op, pos)) {
continue;
}
if (op == Token::OR || op == Token::AND) {
impl()->RecordExpressionSourceRange(y, right_range);
}
// For now we distinguish between comparisons and other binary
// operations. (We could combine the two and get rid of this
// code and AST node eventually.)

View File

@ -1033,6 +1033,13 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
new (zone()) ConditionalSourceRanges(then_range, else_range));
}
V8_INLINE void RecordExpressionSourceRange(Expression* node,
const SourceRange& body_range) {
if (source_range_map_ == nullptr) return;
source_range_map_->Insert(node,
new (zone()) ExpressionSourceRanges(body_range));
}
V8_INLINE void RecordJumpStatementSourceRange(Statement* node,
int32_t continuation_position) {
if (source_range_map_ == nullptr) return;

View File

@ -666,4 +666,142 @@ f(); // 0200
{"start":61,"end":150,"count":1}]
);
TestCoverage(
"LogicalOrExpression assignment",
`
const a = true || 99 // 0000
function b () { // 0050
const b = a || 2 // 0100
} // 0150
b() // 0200
b() // 0250
`,
[{"start":0,"end":299,"count":1},
{"start":15,"end":20,"count":0},
{"start":50,"end":151,"count":2},
{"start":114,"end":118,"count":0}]);
TestCoverage(
"LogicalOrExpression IsTest()",
`
true || false // 0000
const a = 99 // 0050
a || 50 // 0100
const b = false // 0150
if (b || true) {} // 0200
`,
[{"start":0,"end":249,"count":1},
{"start":5,"end":13,"count":0},
{"start":102,"end":107,"count":0}]);
TestCoverage(
"LogicalAndExpression assignment",
`
const a = false && 99 // 0000
function b () { // 0050
const b = a && 2 // 0100
} // 0150
b() // 0200
b() // 0250
const c = true && 50 // 0300
`,
[{"start":0,"end":349,"count":1},
{"start":16,"end":21,"count":0},
{"start":50,"end":151,"count":2},
{"start":114,"end":118,"count":0}]);
TestCoverage(
"LogicalAndExpression IsTest()",
`
false && true // 0000
const a = 0 // 0050
a && 50 // 0100
const b = true // 0150
if (b && true) {} // 0200
true && true // 0250
`,
[{"start":0,"end":299,"count":1},
{"start":6,"end":13,"count":0},
{"start":102,"end":107,"count":0}]);
TestCoverage(
"NaryLogicalOr assignment",
`
const a = true // 0000
const b = false // 0050
const c = false || false || 99 // 0100
const d = false || true || 99 // 0150
const e = true || true || 99 // 0200
const f = b || b || 99 // 0250
const g = b || a || 99 // 0300
const h = a || a || 99 // 0350
const i = a || (b || c) || d // 0400
`,
[{"start":0,"end":449,"count":1},
{"start":174,"end":179,"count":0},
{"start":215,"end":222,"count":0},
{"start":223,"end":228,"count":0},
{"start":317,"end":322,"count":0},
{"start":362,"end":366,"count":0},
{"start":367,"end":372,"count":0},
{"start":412,"end":423,"count":0},
{"start":424,"end":428,"count":0}]);
TestCoverage(
"NaryLogicalOr IsTest()",
`
const a = true // 0000
const b = false // 0050
false || false || 99 // 0100
false || true || 99 // 0150
true || true || 99 // 0200
b || b || 99 // 0250
b || a || 99 // 0300
a || a || 99 // 0350
`,
[{"start":0,"end":399,"count":1},
{"start":164,"end":169,"count":0},
{"start":205,"end":212,"count":0},
{"start":213,"end":218,"count":0},
{"start":307,"end":312,"count":0},
{"start":352,"end":356,"count":0},
{"start":357,"end":362,"count":0}]);
TestCoverage(
"NaryLogicalAnd assignment",
`
const a = true // 0000
const b = false // 0050
const c = false && false && 99 // 0100
const d = false && true && 99 // 0150
const e = true && true && 99 // 0200
const f = true && false || true // 0250
const g = true || false && true // 0300
`,
[{"start":0,"end":349,"count":1},
{"start":116,"end":124,"count":0},
{"start":125,"end":130,"count":0},
{"start":166,"end":173,"count":0},
{"start":174,"end":179,"count":0},
{"start":315,"end":331,"count":0}
]);
TestCoverage(
"NaryLogicalAnd IsTest()",
`
const a = true // 0000
const b = false // 0050
false && false && 99 // 0100
false && true && 99 // 0150
true && true && 99 // 0200
true && false || true // 0250
true || false && true // 0300
`,
[{"start":0,"end":349,"count":1},
{"start":106,"end":114,"count":0},
{"start":115,"end":120,"count":0},
{"start":156,"end":163,"count":0},
{"start":164,"end":169,"count":0},
{"start":305,"end":321,"count":0}]);
%DebugToggleBlockCoverage(false);