ruby-changes:52300
From: mame <ko1@a...>
Date: Wed, 22 Aug 2018 19:39:01 +0900 (JST)
Subject: [ruby-changes:52300] mame:r64508 (trunk): parse.y: remove coverage-related code fragments
mame 2018-08-22 19:38:56 +0900 (Wed, 22 Aug 2018) New Revision: 64508 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64508 Log: parse.y: remove coverage-related code fragments The code fragments that initializes coverage data were scattered into both parse.y and compile.c. parse.y allocated a coverage data, and compile.c initialize the data. To remove this cross-cutting concern, this change moves the allocation from "coverage" function of parse.y to "rb_iseq_new_top" of iseq.c. For the sake, parse.y just counts the line number of the original source code, and the number is passed via rb_ast_body_t. Modified files: trunk/compile.c trunk/iseq.c trunk/node.h trunk/parse.y trunk/test/coverage/test_coverage.rb trunk/vm.c Index: node.h =================================================================== --- node.h (revision 64507) +++ node.h (revision 64508) @@ -470,6 +470,7 @@ typedef struct node_buffer_struct node_b https://github.com/ruby/ruby/blob/trunk/node.h#L470 typedef struct rb_ast_body_struct { const NODE *root; VALUE compile_option; + int line_count; } rb_ast_body_t; typedef struct rb_ast_struct { VALUE flags; Index: parse.y =================================================================== --- parse.y (revision 64507) +++ parse.y (revision 64508) @@ -266,7 +266,6 @@ struct parser_params { https://github.com/ruby/ruby/blob/trunk/parse.y#L266 NODE *eval_tree; VALUE error_buffer; VALUE debug_lines; - VALUE coverage; const struct rb_block *base_block; #else /* Ripper only */ @@ -4845,21 +4844,6 @@ debug_lines(VALUE fname) https://github.com/ruby/ruby/blob/trunk/parse.y#L4844 return 0; } -static VALUE -coverage(VALUE fname, int n) -{ - VALUE coverages = rb_get_coverages(); - if (RTEST(coverages) && RBASIC(coverages)->klass == 0) { - VALUE coverage = rb_default_coverage(n); - VALUE lines = RARRAY_AREF(coverage, COVERAGE_INDEX_LINES); - - rb_hash_aset(coverages, fname, coverage); - - return lines == Qnil ? Qfalse : lines; - } - return 0; -} - static int e_option_supplied(struct parser_params *p) { @@ -4885,7 +4869,6 @@ yycompile0(VALUE arg) https://github.com/ruby/ruby/blob/trunk/parse.y#L4869 } if (!e_option_supplied(p)) { - p->coverage = coverage(p->ruby_sourcefile_string, p->ruby_sourceline); cov = Qtrue; } } @@ -4899,7 +4882,6 @@ yycompile0(VALUE arg) https://github.com/ruby/ruby/blob/trunk/parse.y#L4882 n = yyparse(p); RUBY_DTRACE_PARSE_HOOK(END); p->debug_lines = 0; - p->coverage = 0; p->lex.strterm = 0; p->lex.pcur = p->lex.pbeg = p->lex.pend = 0; @@ -4928,6 +4910,7 @@ yycompile0(VALUE arg) https://github.com/ruby/ruby/blob/trunk/parse.y#L4910 p->ast->body.compile_option = opt; } p->ast->body.root = tree; + p->ast->body.line_count = p->line_count; return TRUE; } @@ -4989,10 +4972,8 @@ lex_getline(struct parser_params *p) https://github.com/ruby/ruby/blob/trunk/parse.y#L4972 rb_enc_associate(line, p->enc); rb_ary_push(p->debug_lines, line); } - if (p->coverage) { - rb_ary_push(p->coverage, Qnil); - } #endif + p->line_count++; return line; } @@ -5173,7 +5154,6 @@ nextline(struct parser_params *p) https://github.com/ruby/ruby/blob/trunk/parse.y#L5154 p->heredoc_end = 0; } p->ruby_sourceline++; - p->line_count++; p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v); p->lex.pend = p->lex.pcur + RSTRING_LEN(v); token_flush(p); Index: compile.c =================================================================== --- compile.c (revision 64507) +++ compile.c (revision 64508) @@ -1244,6 +1244,7 @@ new_child_iseq(rb_iseq_t *iseq, const NO https://github.com/ruby/ruby/blob/trunk/compile.c#L1244 ast.root = node; ast.compile_option = 0; + ast.line_count = -1; debugs("[new_child_iseq]> ---------------------------------------\n"); ret_iseq = rb_iseq_new_with_opt(&ast, name, Index: iseq.c =================================================================== --- iseq.c (revision 64507) +++ iseq.c (revision 64508) @@ -647,6 +647,14 @@ rb_iseq_new(const rb_ast_body_t *ast, VA https://github.com/ruby/ruby/blob/trunk/iseq.c#L647 rb_iseq_t * rb_iseq_new_top(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent) { + VALUE coverages = rb_get_coverages(); + if (RTEST(coverages)) { + if (ast->line_count >= 0) { + VALUE coverage = rb_default_coverage(ast->line_count); + rb_hash_aset(coverages, path, coverage); + } + } + return rb_iseq_new_with_opt(ast, name, path, realpath, INT2FIX(0), parent, ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT); } Index: vm.c =================================================================== --- vm.c (revision 64507) +++ vm.c (revision 64508) @@ -970,6 +970,7 @@ rb_binding_add_dynavars(VALUE bindval, r https://github.com/ruby/ruby/blob/trunk/vm.c#L970 rb_node_init(&tmp_node, NODE_SCOPE, (VALUE)dyns, 0, 0); ast.root = &tmp_node; ast.compile_option = 0; + ast.line_count = -1; if (base_iseq) { iseq = rb_iseq_new(&ast, base_iseq->body->location.label, path, realpath, base_iseq, ISEQ_TYPE_EVAL); Index: test/coverage/test_coverage.rb =================================================================== --- test/coverage/test_coverage.rb (revision 64507) +++ test/coverage/test_coverage.rb (revision 64508) @@ -127,15 +127,6 @@ class TestCoverage < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/coverage/test_coverage.rb#L127 } end - def test_nonpositive_linenumber - bug12517 = '[ruby-core:76141] [Bug #12517]' - assert_in_out_err(%w[-W0 -rcoverage], <<-"end;", ['{"<compiled>"=>[nil]}'], [], bug12517) - Coverage.start - RubyVM::InstructionSequence.compile(":ok", nil, "<compiled>", 0) - p Coverage.result - end; - end - def test_eval bug13305 = '[ruby-core:80079] [Bug #13305]' -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/