compile.c: support branch coverage for a&.foo = 1

[Bug #15476]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66670 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2019-01-01 01:41:23 +00:00
parent 8423533c25
commit 67a257cac0
2 changed files with 26 additions and 9 deletions

View File

@ -7462,8 +7462,10 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
DECL_ANCHOR(args); DECL_ANCHOR(args);
unsigned int flag = 0; unsigned int flag = 0;
ID mid = node->nd_mid; ID mid = node->nd_mid;
LABEL *lskip = 0; LABEL *else_label = 0;
LABEL *end_label = 0;
VALUE argc; VALUE argc;
VALUE branches = 0;
/* optimization shortcut /* optimization shortcut
* obj["literal"] = value -> opt_aset_with(obj, "literal", value) * obj["literal"] = value -> opt_aset_with(obj, "literal", value)
@ -7503,8 +7505,11 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
/* safe nav attr */ /* safe nav attr */
mid = rb_id_attrset(mid); mid = rb_id_attrset(mid);
ADD_INSN(recv, line, dup); ADD_INSN(recv, line, dup);
lskip = NEW_LABEL(line); else_label = NEW_LABEL(line);
ADD_INSNL(recv, line, branchnil, lskip); end_label = NEW_LABEL(line);
DECL_BRANCH_BASE(branches, nd_first_lineno(node), nd_first_column(node), nd_last_lineno(node), nd_last_column(node), "&.");
ADD_INSNL(recv, line, branchnil, else_label);
ADD_TRACE_BRANCH_COVERAGE(recv, nd_first_lineno(node), nd_first_column(node), nd_last_lineno(node), nd_last_column(node), "then", branches);
} }
if (!popped) { if (!popped) {
ADD_INSN(ret, line, putnil); ADD_INSN(ret, line, putnil);
@ -7536,7 +7541,12 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
ADD_SEQ(ret, args); ADD_SEQ(ret, args);
} }
ADD_SEND_WITH_FLAG(ret, line, mid, argc, INT2FIX(flag)); ADD_SEND_WITH_FLAG(ret, line, mid, argc, INT2FIX(flag));
if (lskip) ADD_LABEL(ret, lskip); if (else_label && end_label) {
ADD_INSNL(ret, line, jump, end_label);
ADD_LABEL(ret, else_label);
ADD_TRACE_BRANCH_COVERAGE(ret, nd_first_lineno(node), nd_first_column(node), nd_last_lineno(node), nd_last_column(node), "else", branches);
ADD_LABEL(ret, end_label);
}
ADD_INSN(ret, line, pop); ADD_INSN(ret, line, pop);
break; break;

View File

@ -348,15 +348,22 @@ class TestCoverage < Test::Unit::TestCase
def test_branch_coverage_for_safe_method_invocation def test_branch_coverage_for_safe_method_invocation
result = { result = {
:branches=>{ :branches=>{
[:"&.", 0, 3, 0, 3, 6] => {[:then, 1, 3, 0, 3, 6]=>1, [:else, 2, 3, 0, 3, 6]=>0}, [:"&.", 0, 6, 0, 6, 6] => {[:then, 1, 6, 0, 6, 6]=>1, [:else, 2, 6, 0, 6, 6]=>0},
[:"&.", 3, 4, 0, 4, 6] => {[:then, 4, 4, 0, 4, 6]=>0, [:else, 5, 4, 0, 4, 6]=>1}, [:"&.", 3, 7, 0, 7, 6] => {[:then, 4, 7, 0, 7, 6]=>0, [:else, 5, 7, 0, 7, 6]=>1},
[:"&.", 6, 8, 0, 8, 10] => {[:then, 7, 8, 0, 8, 10]=>1, [:else, 8, 8, 0, 8, 10]=>0},
[:"&.", 9, 9, 0, 9, 10] => {[:then, 10, 9, 0, 9, 10]=>0, [:else, 11, 9, 0, 9, 10]=>1},
} }
} }
assert_coverage(<<~"end;", { branches: true }, result) assert_coverage(<<~"end;", { branches: true }, result)
a = 10 class Dummy; def foo; end; def foo=(x); end; end
a = Dummy.new
b = nil b = nil
a&.abs c = Dummy.new
b&.hoo d = nil
a&.foo
b&.foo
c&.foo = 1
d&.foo = 1
end; end;
end end