From 9613af6f34e46bc70528593b345824ccada18102 Mon Sep 17 00:00:00 2001 From: mame Date: Tue, 1 Jan 2019 03:03:17 +0000 Subject: [PATCH] compile.c (iseq_set_sequence): fix branch coverage table Not only TRACE_ELEMENT but also INSN_ELEMENT may have events. The old pc2branchindex was created using only events of TRACE_ELEMENTs. This change uses events of INSN_ELEMENTs too for pc2branchindex table. [Bug #15476] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66676 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- compile.c | 2 +- test/coverage/test_coverage.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compile.c b/compile.c index 24070cc2ad..15b74d23aa 100644 --- a/compile.c +++ b/compile.c @@ -2061,6 +2061,7 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *const anchor) /* update sp */ sp = calc_sp_depth(sp, iobj); insn_num++; + events = iobj->insn_info.events |= events; if (ISEQ_COVERAGE(iseq)) { if (ISEQ_LINE_COVERAGE(iseq) && (events & RUBY_EVENT_COVERAGE_LINE) && !(rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES)) { @@ -2077,7 +2078,6 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *const anchor) } } code_index += insn_data_length(iobj); - iobj->insn_info.events |= events; events = 0; data = 0; break; diff --git a/test/coverage/test_coverage.rb b/test/coverage/test_coverage.rb index 1797c898af..6a58706440 100644 --- a/test/coverage/test_coverage.rb +++ b/test/coverage/test_coverage.rb @@ -167,6 +167,20 @@ class TestCoverage < Test::Unit::TestCase end; end + def test_coverage_optimized_branch + result = { + :branches => { + [:"&.", 0, 1, 0, 1, 8] => { + [:then, 1, 1, 0, 1, 8] => 1, + [:else, 2, 1, 0, 1, 8] => 0, + }, + }, + } + assert_coverage(<<~"end;", { branches: true }, result) # Bug #15476 + nil&.foo + end; + end + def assert_coverage(code, opt, stdout) stdout = [stdout] unless stdout.is_a?(Array) stdout = stdout.map {|s| s.to_s }