summaryrefslogtreecommitdiff
path: root/ext/coverage
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-05 07:16:42 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-12-05 07:16:42 +0000
commit0a6816ecd79fac5dfb32eb237f4c31bb45c9460d (patch)
tree63c3def6ba4957dace4d3fba56f1da964b9750de /ext/coverage
parentaa87ae7a04b9c4845ef4326b63150027ed1036a9 (diff)
Revamp method coverage to support define_method
Traditionally, method coverage measurement was implemented by inserting `trace2` instruction to the head of method iseq. So, it just measured methods defined by `def` keyword. This commit drastically changes the measuring mechanism of method coverage; at `RUBY_EVENT_CALL`, it keeps a hash from rb_method_entry_t* to runs (i.e., it counts the runs per method entry), and at `Coverage.result`, it creates the result hash by enumerating all `rb_method_entry_t*` objects (by `ObjectSpace.each_object`). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61023 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/coverage')
-rw-r--r--ext/coverage/coverage.c95
1 files changed, 74 insertions, 21 deletions
diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c
index 368f7b1..08c386e 100644
--- a/ext/coverage/coverage.c
+++ b/ext/coverage/coverage.c
@@ -10,8 +10,10 @@
#include "ruby.h"
#include "vm_core.h"
+#include "gc.h"
static int current_mode;
+static VALUE me2counter = Qnil;
/*
* call-seq:
@@ -55,13 +57,20 @@ rb_coverage_start(int argc, VALUE *argv, VALUE klass)
}
}
+ if (mode & COVERAGE_TARGET_METHODS) {
+ me2counter = rb_hash_new_compare_by_id();
+ }
+ else {
+ me2counter = Qnil;
+ }
+
coverages = rb_get_coverages();
if (!RTEST(coverages)) {
coverages = rb_hash_new();
rb_obj_hide(coverages);
current_mode = mode;
if (mode == 0) mode = COVERAGE_TARGET_LINES;
- rb_set_coverages(coverages, mode);
+ rb_set_coverages(coverages, mode, me2counter);
}
else if (current_mode != mode) {
rb_raise(rb_eRuntimeError, "cannot change the measuring target during coverage measurement");
@@ -101,21 +110,60 @@ branch_coverage(VALUE branches)
return ret;
}
-static VALUE
-method_coverage(VALUE methods)
+static int
+method_coverage_i(void *vstart, void *vend, size_t stride, void *data)
{
- VALUE ret = rb_hash_new();
- int i;
- long id = 0;
+ /*
+ * ObjectSpace.each_object(Module){|mod|
+ * mod.instance_methods.each{|mid|
+ * m = mod.instance_method(mid)
+ * if loc = m.source_location
+ * p [m.name, loc, $g_method_cov_counts[m]]
+ * end
+ * }
+ * }
+ */
+ VALUE ncoverages = *(VALUE*)data, v;
- for (i = 0; i < RARRAY_LEN(methods); ) {
- VALUE method_name = RARRAY_AREF(methods, i++);
- VALUE lineno = RARRAY_AREF(methods, i++);
- VALUE counter = RARRAY_AREF(methods, i++);
- rb_hash_aset(ret, rb_ary_new_from_args(3, method_name, LONG2FIX(id++), lineno), counter);
- }
+ for (v = (VALUE)vstart; v != (VALUE)vend; v += stride) {
+ if (RB_TYPE_P(v, T_IMEMO) && imemo_type(v) == imemo_ment) {
+ const rb_method_entry_t *me = (rb_method_entry_t *) v;
+ VALUE path = Qundef, first_lineno = Qundef;
+ VALUE data[2], ncoverage, methods;
+ VALUE methods_id = ID2SYM(rb_intern("methods"));
+ VALUE klass;
+ const rb_method_entry_t *me2 = rb_resolve_me_location(me, data);
+ if (me != me2) continue;
+ klass = me->owner;
+ if (RB_TYPE_P(klass, T_ICLASS)) {
+ rb_bug("T_ICLASS");
+ }
+ path = data[0];
+ first_lineno = data[1];
+ if (FIX2LONG(first_lineno) <= 0) continue;
+ ncoverage = rb_hash_aref(ncoverages, path);
+ if (NIL_P(ncoverage)) continue;
+ methods = rb_hash_aref(ncoverage, methods_id);
- return ret;
+ {
+ VALUE method_id = ID2SYM(me->def->original_id);
+ VALUE rcount = rb_hash_aref(me2counter, (VALUE) me);
+ VALUE key = rb_ary_new_from_args(3, klass, method_id, first_lineno);
+ VALUE rcount2 = rb_hash_aref(methods, key);
+
+ if (NIL_P(rcount)) rcount = LONG2FIX(0);
+ if (NIL_P(rcount2)) rcount2 = LONG2FIX(0);
+ if (!POSFIXABLE(FIX2LONG(rcount) + FIX2LONG(rcount2))) {
+ rcount = LONG2FIX(FIXNUM_MAX);
+ }
+ else {
+ rcount = LONG2FIX(FIX2LONG(rcount) + FIX2LONG(rcount2));
+ }
+ rb_hash_aset(methods, key, rcount);
+ }
+ }
+ }
+ return 0;
}
static int
@@ -132,25 +180,23 @@ coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
}
else {
VALUE h = rb_hash_new();
- VALUE lines = RARRAY_AREF(coverage, COVERAGE_INDEX_LINES);
- VALUE branches = RARRAY_AREF(coverage, COVERAGE_INDEX_BRANCHES);
- VALUE methods = RARRAY_AREF(coverage, COVERAGE_INDEX_METHODS);
- if (lines) {
+ if (current_mode & COVERAGE_TARGET_LINES) {
+ VALUE lines = RARRAY_AREF(coverage, COVERAGE_INDEX_LINES);
lines = rb_ary_dup(lines);
rb_ary_freeze(lines);
rb_hash_aset(h, ID2SYM(rb_intern("lines")), lines);
}
- if (branches) {
+ if (current_mode & COVERAGE_TARGET_BRANCHES) {
+ VALUE branches = RARRAY_AREF(coverage, COVERAGE_INDEX_BRANCHES);
rb_hash_aset(h, ID2SYM(rb_intern("branches")), branch_coverage(branches));
}
- if (methods) {
- rb_hash_aset(h, ID2SYM(rb_intern("methods")), method_coverage(methods));
+ if (current_mode & COVERAGE_TARGET_METHODS) {
+ rb_hash_aset(h, ID2SYM(rb_intern("methods")), rb_hash_new());
}
- rb_hash_freeze(h);
coverage = h;
}
@@ -178,6 +224,11 @@ rb_coverage_peek_result(VALUE klass)
rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
}
st_foreach(RHASH_TBL(coverages), coverage_peek_result_i, ncoverages);
+
+ if (current_mode & COVERAGE_TARGET_METHODS) {
+ rb_objspace_each_objects(method_coverage_i, &ncoverages);
+ }
+
rb_hash_freeze(ncoverages);
return ncoverages;
}
@@ -194,6 +245,7 @@ rb_coverage_result(VALUE klass)
{
VALUE ncoverages = rb_coverage_peek_result(klass);
rb_reset_coverages();
+ me2counter = Qnil;
return ncoverages;
}
@@ -252,4 +304,5 @@ Init_coverage(void)
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
rb_define_module_function(rb_mCoverage, "peek_result", rb_coverage_peek_result, 0);
rb_define_module_function(rb_mCoverage, "running?", rb_coverage_running, 0);
+ rb_global_variable(&me2counter);
}