summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compile.c106
-rw-r--r--ext/coverage/coverage.c20
-rw-r--r--test/coverage/test_coverage.rb34
3 files changed, 110 insertions, 50 deletions
diff --git a/compile.c b/compile.c
index ca998bef15..691271abe9 100644
--- a/compile.c
+++ b/compile.c
@@ -262,32 +262,36 @@ struct iseq_compile_data_ensure_node_stack {
ADD_INSN2((seq), (line), trace2, INT2FIX(RUBY_EVENT_COVERAGE), INT2FIX(COVERAGE_INDEX_LINES)); \
} \
} while (0)
-#define DECL_BRANCH_BASE(branches, line, column, type) \
+#define DECL_BRANCH_BASE(branches, first_line, first_column, last_line, last_column, type) \
do { \
if (ISEQ_COVERAGE(iseq) && \
ISEQ_BRANCH_COVERAGE(iseq) && \
- (line) > 0) { \
+ (first_line) > 0) { \
VALUE structure = RARRAY_AREF(ISEQ_BRANCH_COVERAGE(iseq), 0); \
branches = rb_ary_tmp_new(0); \
rb_ary_push(structure, branches); \
rb_ary_push(branches, ID2SYM(rb_intern(type))); \
- rb_ary_push(branches, INT2FIX(line)); \
- rb_ary_push(branches, INT2FIX(column)); \
+ rb_ary_push(branches, INT2FIX(first_line)); \
+ rb_ary_push(branches, INT2FIX(first_column)); \
+ rb_ary_push(branches, INT2FIX(last_line)); \
+ rb_ary_push(branches, INT2FIX(last_column)); \
} \
} while (0)
-#define ADD_TRACE_BRANCH_COVERAGE(seq, line, column, type, branches) \
+#define ADD_TRACE_BRANCH_COVERAGE(seq, first_line, first_column, last_line, last_column, type, branches) \
do { \
if (ISEQ_COVERAGE(iseq) && \
ISEQ_BRANCH_COVERAGE(iseq) && \
- (line) > 0) { \
+ (first_line) > 0) { \
VALUE counters = RARRAY_AREF(ISEQ_BRANCH_COVERAGE(iseq), 1); \
long counter_idx = RARRAY_LEN(counters); \
rb_ary_push(counters, INT2FIX(0)); \
rb_ary_push(branches, ID2SYM(rb_intern(type))); \
- rb_ary_push(branches, INT2FIX(line)); \
- rb_ary_push(branches, INT2FIX(column)); \
+ rb_ary_push(branches, INT2FIX(first_line)); \
+ rb_ary_push(branches, INT2FIX(first_column)); \
+ rb_ary_push(branches, INT2FIX(last_line)); \
+ rb_ary_push(branches, INT2FIX(last_column)); \
rb_ary_push(branches, INT2FIX(counter_idx)); \
- ADD_INSN2((seq), (line), trace2, INT2FIX(RUBY_EVENT_COVERAGE), INT2FIX(counter_idx * 16 + COVERAGE_INDEX_BRANCHES)); \
+ ADD_INSN2((seq), (first_line), trace2, INT2FIX(RUBY_EVENT_COVERAGE), INT2FIX(counter_idx * 16 + COVERAGE_INDEX_BRANCHES)); \
} \
} while (0)
#define ADD_TRACE_METHOD_COVERAGE(seq, line, method_name) \
@@ -4526,6 +4530,8 @@ compile_if(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int
const int line = nd_line(node);
const int lineno = nd_lineno(node);
const int column = nd_column(node);
+ const int last_lineno = nd_last_lineno(node);
+ const int last_column = nd_last_column(node);
DECL_ANCHOR(cond_seq);
DECL_ANCHOR(then_seq);
DECL_ANCHOR(else_seq);
@@ -4547,13 +4553,20 @@ compile_if(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int
ADD_SEQ(ret, cond_seq);
if (then_label->refcnt && else_label->refcnt) {
- DECL_BRANCH_BASE(branches, lineno, column, type == NODE_IF ? "if" : "unless");
+ DECL_BRANCH_BASE(branches, lineno, column, last_lineno, last_column, type == NODE_IF ? "if" : "unless");
}
if (then_label->refcnt) {
ADD_LABEL(ret, then_label);
if (else_label->refcnt) {
- ADD_TRACE_BRANCH_COVERAGE(ret, node_body ? nd_lineno(node_body) : lineno, node_body ? nd_column(node_body) : column, type == NODE_IF ? "then" : "else", branches);
+ ADD_TRACE_BRANCH_COVERAGE(
+ ret,
+ node_body ? nd_lineno(node_body) : lineno,
+ node_body ? nd_column(node_body) : column,
+ node_body ? nd_last_lineno(node_body) : last_lineno,
+ node_body ? nd_last_column(node_body) : last_column,
+ type == NODE_IF ? "then" : "else",
+ branches);
}
ADD_SEQ(ret, then_seq);
end_label = NEW_LABEL(line);
@@ -4563,7 +4576,14 @@ compile_if(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int
if (else_label->refcnt) {
ADD_LABEL(ret, else_label);
if (then_label->refcnt) {
- ADD_TRACE_BRANCH_COVERAGE(ret, node_else ? nd_lineno(node_else) : lineno, node_else ? nd_column(node_else) : column, type == NODE_IF ? "else" : "then", branches);
+ ADD_TRACE_BRANCH_COVERAGE(
+ ret,
+ node_else ? nd_lineno(node_else) : lineno,
+ node_else ? nd_column(node_else) : column,
+ node_else ? nd_last_lineno(node_else) : last_lineno,
+ node_else ? nd_last_column(node_else) : last_column,
+ type == NODE_IF ? "else" : "then",
+ branches);
}
ADD_SEQ(ret, else_seq);
}
@@ -4586,7 +4606,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_nod
DECL_ANCHOR(cond_seq);
int only_special_literals = 1;
VALUE literals = rb_hash_new();
- int line, lineno, column;
+ int line, lineno, column, last_lineno, last_column;
enum node_type type;
VALUE branches = 0;
@@ -4598,13 +4618,15 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_nod
CHECK(COMPILE(head, "case base", node->nd_head));
- DECL_BRANCH_BASE(branches, nd_lineno(node), nd_column(node), "case");
+ DECL_BRANCH_BASE(branches, nd_lineno(node), nd_column(node), nd_last_lineno(node), nd_last_column(node), "case");
node = node->nd_body;
type = nd_type(node);
line = nd_line(node);
lineno = nd_lineno(node);
column = nd_column(node);
+ last_lineno = nd_last_lineno(node);
+ last_column = nd_last_column(node);
if (type != NODE_WHEN) {
COMPILE_ERROR(ERROR_ARGS "NODE_CASE: unexpected node. must be NODE_WHEN, but %s", ruby_node_name(type));
@@ -4622,7 +4644,14 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_nod
l1 = NEW_LABEL(line);
ADD_LABEL(body_seq, l1);
ADD_INSN(body_seq, line, pop);
- ADD_TRACE_BRANCH_COVERAGE(body_seq, node->nd_body ? nd_lineno(node->nd_body) : lineno, node->nd_body ? nd_column(node->nd_body) : column, "when", branches);
+ ADD_TRACE_BRANCH_COVERAGE(
+ body_seq,
+ node->nd_body ? nd_lineno(node->nd_body) : lineno,
+ node->nd_body ? nd_column(node->nd_body) : column,
+ node->nd_body ? nd_last_lineno(node->nd_body) : last_lineno,
+ node->nd_body ? nd_last_column(node->nd_body) : last_column,
+ "when",
+ branches);
CHECK(COMPILE_(body_seq, "when body", node->nd_body, popped));
ADD_INSNL(body_seq, line, jump, endlabel);
@@ -4657,12 +4686,14 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_nod
line = nd_line(node);
lineno = nd_lineno(node);
column = nd_column(node);
+ last_lineno = nd_last_lineno(node);
+ last_column = nd_last_column(node);
}
/* else */
if (node) {
ADD_LABEL(cond_seq, elselabel);
ADD_INSN(cond_seq, line, pop);
- ADD_TRACE_BRANCH_COVERAGE(cond_seq, nd_lineno(node), nd_column(node), "else", branches);
+ ADD_TRACE_BRANCH_COVERAGE(cond_seq, nd_lineno(node), nd_column(node), nd_last_lineno(node), nd_last_column(node), "else", branches);
CHECK(COMPILE_(cond_seq, "else", node, popped));
ADD_INSNL(cond_seq, line, jump, endlabel);
}
@@ -4670,7 +4701,7 @@ compile_case(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_nod
debugs("== else (implicit)\n");
ADD_LABEL(cond_seq, elselabel);
ADD_INSN(cond_seq, nd_line(orig_node), pop);
- ADD_TRACE_BRANCH_COVERAGE(cond_seq, nd_lineno(orig_node), nd_column(orig_node), "else", branches);
+ ADD_TRACE_BRANCH_COVERAGE(cond_seq, nd_lineno(orig_node), nd_column(orig_node), nd_last_lineno(orig_node), nd_last_column(orig_node), "else", branches);
if (!popped) {
ADD_INSN(cond_seq, nd_line(orig_node), putnil);
}
@@ -4701,7 +4732,7 @@ compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_no
DECL_ANCHOR(body_seq);
VALUE branches = 0;
- DECL_BRANCH_BASE(branches, nd_lineno(orig_node), nd_column(orig_node), "case");
+ DECL_BRANCH_BASE(branches, nd_lineno(orig_node), nd_column(orig_node), nd_last_lineno(orig_node), nd_last_column(orig_node), "case");
INIT_ANCHOR(body_seq);
endlabel = NEW_LABEL(nd_line(node));
@@ -4710,9 +4741,18 @@ compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_no
const int line = nd_line(node);
const int lineno = nd_lineno(node);
const int column = nd_column(node);
+ const int last_lineno = nd_last_lineno(node);
+ const int last_column = nd_last_column(node);
LABEL *l1 = NEW_LABEL(line);
ADD_LABEL(body_seq, l1);
- ADD_TRACE_BRANCH_COVERAGE(body_seq, node->nd_body ? nd_lineno(node->nd_body) : lineno, node->nd_body ? nd_column(node->nd_body) : column, "when", branches);
+ ADD_TRACE_BRANCH_COVERAGE(
+ body_seq,
+ node->nd_body ? nd_lineno(node->nd_body) : lineno,
+ node->nd_body ? nd_column(node->nd_body) : column,
+ node->nd_body ? nd_last_lineno(node->nd_body) : last_lineno,
+ node->nd_body ? nd_last_column(node->nd_body) : last_column,
+ "when",
+ branches);
CHECK(COMPILE_(body_seq, "when", node->nd_body, popped));
ADD_INSNL(body_seq, line, jump, endlabel);
@@ -4744,7 +4784,14 @@ compile_case2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const orig_no
node = node->nd_next;
}
/* else */
- ADD_TRACE_BRANCH_COVERAGE(ret, node ? nd_lineno(node) : nd_lineno(orig_node), node ? nd_column(node) : nd_column(orig_node), "else", branches);
+ ADD_TRACE_BRANCH_COVERAGE(
+ ret,
+ node ? nd_lineno(node) : nd_lineno(orig_node),
+ node ? nd_column(node) : nd_column(orig_node),
+ node ? nd_last_lineno(node) : nd_last_lineno(orig_node),
+ node ? nd_last_column(node) : nd_last_column(orig_node),
+ "else",
+ branches);
CHECK(COMPILE_(ret, "else", node, popped));
ADD_INSNL(ret, nd_line(orig_node), jump, endlabel);
@@ -4759,6 +4806,8 @@ compile_loop(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, in
const int line = (int)nd_line(node);
const int lineno = nd_lineno(node);
const int column = nd_column(node);
+ const int last_lineno = nd_last_lineno(node);
+ const int last_column = nd_last_column(node);
LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
@@ -4794,8 +4843,15 @@ compile_loop(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, in
if (tmp_label) ADD_LABEL(ret, tmp_label);
ADD_LABEL(ret, redo_label);
- DECL_BRANCH_BASE(branches, lineno, column, type == NODE_WHILE ? "while" : "until");
- ADD_TRACE_BRANCH_COVERAGE(ret, node->nd_body ? nd_lineno(node->nd_body) : lineno, node->nd_body ? nd_column(node->nd_body) : column, "body", branches);
+ DECL_BRANCH_BASE(branches, lineno, column, last_lineno, last_column, type == NODE_WHILE ? "while" : "until");
+ ADD_TRACE_BRANCH_COVERAGE(
+ ret,
+ node->nd_body ? nd_lineno(node->nd_body) : lineno,
+ node->nd_body ? nd_column(node->nd_body) : column,
+ node->nd_body ? nd_last_lineno(node->nd_body) : last_lineno,
+ node->nd_body ? nd_last_column(node->nd_body) : last_column,
+ "body",
+ branches);
CHECK(COMPILE_POPPED(ret, "while body", node->nd_body));
ADD_LABEL(ret, next_label); /* next */
@@ -6040,10 +6096,10 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
else_label = NEW_LABEL(line);
end_label = NEW_LABEL(line);
- DECL_BRANCH_BASE(branches, nd_lineno(node), nd_column(node), "&.");
+ DECL_BRANCH_BASE(branches, nd_lineno(node), nd_column(node), nd_last_lineno(node), nd_last_column(node), "&.");
ADD_INSN(recv, line, dup);
ADD_INSNL(recv, line, branchnil, else_label);
- ADD_TRACE_BRANCH_COVERAGE(recv, nd_lineno(node), nd_column(node), "then", branches);
+ ADD_TRACE_BRANCH_COVERAGE(recv, nd_lineno(node), nd_column(node), nd_last_lineno(node), nd_last_column(node), "then", branches);
}
}
else if (type == NODE_FCALL || type == NODE_VCALL) {
@@ -6078,7 +6134,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
if (else_label && end_label) {
ADD_INSNL(ret, line, jump, end_label);
ADD_LABEL(ret, else_label);
- ADD_TRACE_BRANCH_COVERAGE(ret, nd_lineno(node), nd_column(node), "else", branches);
+ ADD_TRACE_BRANCH_COVERAGE(ret, nd_lineno(node), nd_column(node), nd_last_lineno(node), nd_last_column(node), "else", branches);
ADD_LABEL(ret, end_label);
}
if (popped) {
diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c
index c4070faf56..368f7b1f8c 100644
--- a/ext/coverage/coverage.c
+++ b/ext/coverage/coverage.c
@@ -81,16 +81,20 @@ branch_coverage(VALUE branches)
for (i = 0; i < RARRAY_LEN(structure); i++) {
VALUE branches = RARRAY_AREF(structure, i);
VALUE base_type = RARRAY_AREF(branches, 0);
- VALUE base_lineno = RARRAY_AREF(branches, 1);
- VALUE base_column = RARRAY_AREF(branches, 2);
+ VALUE base_first_lineno = RARRAY_AREF(branches, 1);
+ VALUE base_first_column = RARRAY_AREF(branches, 2);
+ VALUE base_last_lineno = RARRAY_AREF(branches, 3);
+ VALUE base_last_column = RARRAY_AREF(branches, 4);
VALUE children = rb_hash_new();
- rb_hash_aset(ret, rb_ary_new_from_args(4, base_type, LONG2FIX(id++), base_lineno, base_column), children);
- for (j = 3; j < RARRAY_LEN(branches); j += 4) {
+ rb_hash_aset(ret, rb_ary_new_from_args(6, base_type, LONG2FIX(id++), base_first_lineno, base_first_column, base_last_lineno, base_last_column), children);
+ for (j = 5; j < RARRAY_LEN(branches); j += 6) {
VALUE target_label = RARRAY_AREF(branches, j);
- VALUE target_lineno = RARRAY_AREF(branches, j + 1);
- VALUE target_column = RARRAY_AREF(branches, j + 2);
- int idx = FIX2INT(RARRAY_AREF(branches, j + 3));
- rb_hash_aset(children, rb_ary_new_from_args(4, target_label, LONG2FIX(id++), target_lineno, target_column), RARRAY_AREF(counters, idx));
+ VALUE target_first_lineno = RARRAY_AREF(branches, j + 1);
+ VALUE target_first_column = RARRAY_AREF(branches, j + 2);
+ VALUE target_last_lineno = RARRAY_AREF(branches, j + 3);
+ VALUE target_last_column = RARRAY_AREF(branches, j + 4);
+ int idx = FIX2INT(RARRAY_AREF(branches, j + 5));
+ rb_hash_aset(children, rb_ary_new_from_args(6, target_label, LONG2FIX(id++), target_first_lineno, target_first_column, target_last_lineno, target_last_column), RARRAY_AREF(counters, idx));
}
}
diff --git a/test/coverage/test_coverage.rb b/test/coverage/test_coverage.rb
index 432089216b..14e58ee64c 100644
--- a/test/coverage/test_coverage.rb
+++ b/test/coverage/test_coverage.rb
@@ -197,13 +197,13 @@ class TestCoverage < Test::Unit::TestCase
def test_branch_coverage_for_if_statement
result = {
:branches => {
- [:if , 0, 2, 2] => {[:then, 1, 3, 4]=>2, [:else, 2, 5, 4]=>1},
- [:unless, 3, 8, 2] => {[:else, 4, 11, 4]=>2, [:then, 5, 9, 4]=>1},
- [:if , 6, 14, 2] => {[:then, 7, 15, 4]=>2, [:else, 8, 14, 2]=>1},
- [:unless, 9, 18, 2] => {[:else, 10, 18, 2]=>2, [:then, 11, 19, 4]=>1},
- [:if , 12, 22, 2] => {[:then, 13, 22, 2]=>2, [:else, 14, 22, 2]=>1},
- [:unless, 15, 23, 2] => {[:else, 16, 23, 2]=>2, [:then, 17, 23, 2]=>1},
- [:if , 18, 25, 2] => {[:then, 19, 25, 11]=>2, [:else, 20, 25, 15]=>1},
+ [:if , 0, 2, 2, 6, 5] => {[:then, 1, 3, 4, 3, 5]=>2, [:else, 2, 5, 4, 5, 5]=>1},
+ [:unless, 3, 8, 2, 12, 5] => {[:else, 4, 11, 4, 11, 5]=>2, [:then, 5, 9, 4, 9, 5]=>1},
+ [:if , 6, 14, 2, 16, 5] => {[:then, 7, 15, 4, 15, 5]=>2, [:else, 8, 14, 2, 16, 5]=>1},
+ [:unless, 9, 18, 2, 20, 5] => {[:else, 10, 18, 2, 20, 5]=>2, [:then, 11, 19, 4, 19, 5]=>1},
+ [:if , 12, 22, 2, 22, 13] => {[:then, 13, 22, 2, 22, 3]=>2, [:else, 14, 22, 2, 22, 13]=>1},
+ [:unless, 15, 23, 2, 23, 17] => {[:else, 16, 23, 2, 23, 17]=>2, [:then, 17, 23, 2, 23, 3]=>1},
+ [:if , 18, 25, 2, 25, 16] => {[:then, 19, 25, 11, 25, 12]=>2, [:else, 20, 25, 15, 25, 16]=>1},
}
}
assert_coverage(<<~"end;", { branches: true }, result)
@@ -243,10 +243,10 @@ class TestCoverage < Test::Unit::TestCase
def test_branch_coverage_for_while_statement
result = {
:branches => {
- [:while, 0, 2, 0] => {[:body, 1, 3, 2]=> 3},
- [:until, 2, 5, 0] => {[:body, 3, 6, 2]=>10},
- [:while, 4, 10, 0] => {[:body, 5, 10, 0]=> 3},
- [:until, 6, 11, 0] => {[:body, 7, 11, 0]=>10},
+ [:while, 0, 2, 0, 4, 3] => {[:body, 1, 3, 2, 3, 8]=> 3},
+ [:until, 2, 5, 0, 7, 3] => {[:body, 3, 6, 2, 6, 8]=>10},
+ [:while, 4, 10, 0, 10, 18] => {[:body, 5, 10, 0, 10, 6]=> 3},
+ [:until, 6, 11, 0, 11, 20] => {[:body, 7, 11, 0, 11, 6]=>10},
}
}
assert_coverage(<<~"end;", { branches: true }, result)
@@ -267,10 +267,10 @@ class TestCoverage < Test::Unit::TestCase
def test_branch_coverage_for_case_statement
result = {
:branches => {
- [:case, 0, 2, 2] => {[:when, 1, 4, 4]=>2, [:when, 2, 6, 4]=>0, [:else, 3, 2, 2]=>1},
- [:case, 4, 9, 2] => {[:when, 5, 11, 4]=>2, [:when, 6, 13, 4]=>0, [:else, 7, 9, 2]=>1},
- [:case, 8, 16, 2] => {[:when, 9, 18, 4]=>2, [:when, 10, 20, 4]=>0, [:else, 11, 22, 4]=>1},
- [:case, 12, 25, 2] => {[:when, 13, 27, 4]=>2, [:when, 14, 29, 4]=>0, [:else, 15, 31, 4]=>1},
+ [:case, 0, 2, 2, 7, 5] => {[:when, 1, 4, 4, 4, 5]=>2, [:when, 2, 6, 4, 6, 5]=>0, [:else, 3, 2, 2, 7, 5]=>1},
+ [:case, 4, 9, 2, 14, 5] => {[:when, 5, 11, 4, 11, 5]=>2, [:when, 6, 13, 4, 13, 5]=>0, [:else, 7, 9, 2, 14, 5]=>1},
+ [:case, 8, 16, 2, 23, 5] => {[:when, 9, 18, 4, 18, 5]=>2, [:when, 10, 20, 4, 20, 5]=>0, [:else, 11, 22, 4, 22, 10]=>1},
+ [:case, 12, 25, 2, 32, 5] => {[:when, 13, 27, 4, 27, 5]=>2, [:when, 14, 29, 4, 29, 5]=>0, [:else, 15, 31, 4, 31, 10]=>1},
}
}
assert_coverage(<<~"end;", { branches: true }, result)
@@ -317,8 +317,8 @@ class TestCoverage < Test::Unit::TestCase
def test_branch_coverage_for_safe_method_invocation
result = {
:branches=>{
- [:"&.", 0, 3, 0] => {[:then, 1, 3, 0]=>1, [:else, 2, 3, 0]=>0},
- [:"&.", 3, 4, 0] => {[:then, 4, 4, 0]=>0, [:else, 5, 4, 0]=>1},
+ [:"&.", 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},
}
}
assert_coverage(<<~"end;", { branches: true }, result)