ruby-changes:54598
From: naruse <ko1@a...>
Date: Mon, 14 Jan 2019 15:34:08 +0900 (JST)
Subject: [ruby-changes:54598] naruse:r66813 (ruby_2_6): merge revision(s) 66670, 66676: [Backport #15476]
naruse 2019-01-14 15:34:02 +0900 (Mon, 14 Jan 2019) New Revision: 66813 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66813 Log: merge revision(s) 66670,66676: [Backport #15476] compile.c: support branch coverage for `a&.foo = 1` [Bug #15476] 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] Modified directories: branches/ruby_2_6/ Modified files: branches/ruby_2_6/compile.c branches/ruby_2_6/test/coverage/test_coverage.rb branches/ruby_2_6/version.h Index: ruby_2_6/test/coverage/test_coverage.rb =================================================================== --- ruby_2_6/test/coverage/test_coverage.rb (revision 66812) +++ ruby_2_6/test/coverage/test_coverage.rb (revision 66813) @@ -167,6 +167,20 @@ class TestCoverage < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/ruby_2_6/test/coverage/test_coverage.rb#L167 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 } @@ -348,15 +362,22 @@ class TestCoverage < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/ruby_2_6/test/coverage/test_coverage.rb#L362 def test_branch_coverage_for_safe_method_invocation result = { :branches=>{ - [:"&.", 0, 3, 0, 3, 6] => {[:then, 1, 3, 0, 3, 6]=>1, [:else, 2, 3, 0, 3, 6]=>0}, - [:"&.", 3, 4, 0, 4, 6] => {[:then, 4, 4, 0, 4, 6]=>0, [:else, 5, 4, 0, 4, 6]=>1}, + [:"&.", 0, 6, 0, 6, 6] => {[:then, 1, 6, 0, 6, 6]=>1, [:else, 2, 6, 0, 6, 6]=>0}, + [:"&.", 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) - a = 10 + class Dummy; def foo; end; def foo=(x); end; end + a = Dummy.new b = nil - a&.abs - b&.hoo + c = Dummy.new + d = nil + a&.foo + b&.foo + c&.foo = 1 + d&.foo = 1 end; end Index: ruby_2_6/compile.c =================================================================== --- ruby_2_6/compile.c (revision 66812) +++ ruby_2_6/compile.c (revision 66813) @@ -2061,6 +2061,7 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/ruby_2_6/compile.c#L2061 /* 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_ https://github.com/ruby/ruby/blob/trunk/ruby_2_6/compile.c#L2078 } } code_index += insn_data_length(iobj); - iobj->insn_info.events |= events; events = 0; data = 0; break; @@ -7459,8 +7459,10 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/ruby_2_6/compile.c#L7459 DECL_ANCHOR(args); unsigned int flag = 0; ID mid = node->nd_mid; - LABEL *lskip = 0; + LABEL *else_label = 0; + LABEL *end_label = 0; VALUE argc; + VALUE branches = 0; /* optimization shortcut * obj["literal"] = value -> opt_aset_with(obj, "literal", value) @@ -7500,8 +7502,11 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/ruby_2_6/compile.c#L7502 /* safe nav attr */ mid = rb_id_attrset(mid); ADD_INSN(recv, line, dup); - lskip = NEW_LABEL(line); - ADD_INSNL(recv, line, branchnil, lskip); + else_label = NEW_LABEL(line); + 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) { ADD_INSN(ret, line, putnil); @@ -7533,7 +7538,12 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/ruby_2_6/compile.c#L7538 ADD_SEQ(ret, args); } 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); break; Index: ruby_2_6/version.h =================================================================== --- ruby_2_6/version.h (revision 66812) +++ ruby_2_6/version.h (revision 66813) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_6/version.h#L1 #define RUBY_VERSION "2.6.0" #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 7 +#define RUBY_PATCHLEVEL 8 #define RUBY_RELEASE_YEAR 2019 #define RUBY_RELEASE_MONTH 1 Index: ruby_2_6 =================================================================== --- ruby_2_6 (revision 66812) +++ ruby_2_6 (revision 66813) Property changes on: ruby_2_6 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /trunk:r66670,66676 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/