summaryrefslogtreecommitdiff
path: root/ext/coverage
diff options
context:
space:
mode:
Diffstat (limited to 'ext/coverage')
-rw-r--r--ext/coverage/coverage.c517
-rw-r--r--ext/coverage/depend194
-rw-r--r--ext/coverage/lib/coverage.rb5
3 files changed, 588 insertions, 128 deletions
diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c
index 8503c9d6c6..1fda8191cc 100644
--- a/ext/coverage/coverage.c
+++ b/ext/coverage/coverage.c
@@ -8,102 +8,223 @@
************************************************/
+#include "internal/gc.h"
+#include "internal/hash.h"
+#include "internal/thread.h"
+#include "internal/sanitizers.h"
#include "ruby.h"
#include "vm_core.h"
-#include "gc.h"
+static enum {
+ IDLE,
+ SUSPENDED,
+ RUNNING
+} current_state = IDLE;
static int current_mode;
static VALUE me2counter = Qnil;
/*
+ * call-seq: Coverage.supported?(mode) -> true or false
+ *
+ * Returns true if coverage measurement is supported for the given mode.
+ *
+ * The mode should be one of the following symbols:
+ * +:lines+, +:oneshot_lines+, +:branches+, +:methods+, +:eval+.
+ *
+ * Example:
+ *
+ * Coverage.supported?(:lines) #=> true
+ * Coverage.supported?(:all) #=> false
+ */
+static VALUE
+rb_coverage_supported(VALUE self, VALUE _mode)
+{
+ ID mode = RB_SYM2ID(_mode);
+
+ return RBOOL(
+ mode == rb_intern("lines") ||
+ mode == rb_intern("oneshot_lines") ||
+ mode == rb_intern("branches") ||
+ mode == rb_intern("methods") ||
+ mode == rb_intern("eval")
+ );
+}
+
+/*
* call-seq:
- * Coverage.start => nil
+ * Coverage.setup => nil
+ * Coverage.setup(:all) => nil
+ * Coverage.setup(lines: bool, branches: bool, methods: bool, eval: bool) => nil
+ * Coverage.setup(oneshot_lines: true) => nil
+ *
+ * Set up the coverage measurement.
*
- * Enables coverage measurement.
+ * Note that this method does not start the measurement itself.
+ * Use Coverage.resume to start the measurement.
+ *
+ * You may want to use Coverage.start to setup and then start the measurement.
*/
static VALUE
-rb_coverage_start(int argc, VALUE *argv, VALUE klass)
+rb_coverage_setup(int argc, VALUE *argv, VALUE klass)
{
VALUE coverages, opt;
int mode;
+ if (current_state != IDLE) {
+ rb_raise(rb_eRuntimeError, "coverage measurement is already setup");
+ }
+
rb_scan_args(argc, argv, "01", &opt);
if (argc == 0) {
- mode = 0; /* compatible mode */
+ mode = 0; /* compatible mode */
}
else if (opt == ID2SYM(rb_intern("all"))) {
- mode = COVERAGE_TARGET_LINES | COVERAGE_TARGET_BRANCHES | COVERAGE_TARGET_METHODS;
+ mode = COVERAGE_TARGET_LINES | COVERAGE_TARGET_BRANCHES | COVERAGE_TARGET_METHODS | COVERAGE_TARGET_EVAL;
}
else {
- mode = 0;
- opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
-
- if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("lines")))))
- mode |= COVERAGE_TARGET_LINES;
- if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("branches")))))
- mode |= COVERAGE_TARGET_BRANCHES;
- if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("methods")))))
- mode |= COVERAGE_TARGET_METHODS;
+ mode = 0;
+ opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
+
+ if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("lines")))))
+ mode |= COVERAGE_TARGET_LINES;
+ if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("branches")))))
+ mode |= COVERAGE_TARGET_BRANCHES;
+ if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("methods")))))
+ mode |= COVERAGE_TARGET_METHODS;
if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("oneshot_lines"))))) {
if (mode & COVERAGE_TARGET_LINES)
rb_raise(rb_eRuntimeError, "cannot enable lines and oneshot_lines simultaneously");
mode |= COVERAGE_TARGET_LINES;
mode |= COVERAGE_TARGET_ONESHOT_LINES;
}
+ if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("eval")))))
+ mode |= COVERAGE_TARGET_EVAL;
}
if (mode & COVERAGE_TARGET_METHODS) {
me2counter = rb_ident_hash_new();
}
else {
- me2counter = Qnil;
+ 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, me2counter);
+ coverages = rb_hash_new();
+ rb_obj_hide(coverages);
+ current_mode = mode;
+ if (mode == 0) mode = COVERAGE_TARGET_LINES;
+ rb_set_coverages(coverages, mode, me2counter);
+ current_state = SUSPENDED;
}
else if (current_mode != mode) {
- rb_raise(rb_eRuntimeError, "cannot change the measuring target during coverage measurement");
+ rb_raise(rb_eRuntimeError, "cannot change the measuring target during coverage measurement");
+ }
+
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * Coverage.resume => nil
+ *
+ * Start/resume the coverage measurement.
+ *
+ * Caveat: Currently, only process-global coverage measurement is supported.
+ * You cannot measure per-thread coverage. If your process has multiple thread,
+ * using Coverage.resume/suspend to capture code coverage executed from only
+ * a limited code block, may yield misleading results.
+ */
+VALUE
+rb_coverage_resume(VALUE klass)
+{
+ if (current_state == IDLE) {
+ rb_raise(rb_eRuntimeError, "coverage measurement is not set up yet");
+ }
+ if (current_state == RUNNING) {
+ rb_raise(rb_eRuntimeError, "coverage measurement is already running");
}
+ rb_resume_coverages();
+ current_state = RUNNING;
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * Coverage.start => nil
+ * Coverage.start(:all) => nil
+ * Coverage.start(lines: bool, branches: bool, methods: bool, eval: bool) => nil
+ * Coverage.start(oneshot_lines: true) => nil
+ *
+ * Enables the coverage measurement.
+ * See the documentation of Coverage class in detail.
+ * This is equivalent to Coverage.setup and Coverage.resume.
+ */
+static VALUE
+rb_coverage_start(int argc, VALUE *argv, VALUE klass)
+{
+ rb_coverage_setup(argc, argv, klass);
+ rb_coverage_resume(klass);
return Qnil;
}
+struct branch_coverage_result_builder
+{
+ int id;
+ VALUE result;
+ VALUE children;
+ VALUE counters;
+};
+
+static int
+branch_coverage_ii(VALUE _key, VALUE branch, VALUE v)
+{
+ struct branch_coverage_result_builder *b = (struct branch_coverage_result_builder *) v;
+
+ VALUE target_label = RARRAY_AREF(branch, 0);
+ VALUE target_first_lineno = RARRAY_AREF(branch, 1);
+ VALUE target_first_column = RARRAY_AREF(branch, 2);
+ VALUE target_last_lineno = RARRAY_AREF(branch, 3);
+ VALUE target_last_column = RARRAY_AREF(branch, 4);
+ long counter_idx = FIX2LONG(RARRAY_AREF(branch, 5));
+ rb_hash_aset(b->children, rb_ary_new_from_args(6, target_label, LONG2FIX(b->id++), target_first_lineno, target_first_column, target_last_lineno, target_last_column), RARRAY_AREF(b->counters, counter_idx));
+
+ return ST_CONTINUE;
+}
+
+static int
+branch_coverage_i(VALUE _key, VALUE branch_base, VALUE v)
+{
+ struct branch_coverage_result_builder *b = (struct branch_coverage_result_builder *) v;
+
+ VALUE base_type = RARRAY_AREF(branch_base, 0);
+ VALUE base_first_lineno = RARRAY_AREF(branch_base, 1);
+ VALUE base_first_column = RARRAY_AREF(branch_base, 2);
+ VALUE base_last_lineno = RARRAY_AREF(branch_base, 3);
+ VALUE base_last_column = RARRAY_AREF(branch_base, 4);
+ VALUE branches = RARRAY_AREF(branch_base, 5);
+ VALUE children = rb_hash_new();
+ rb_hash_aset(b->result, rb_ary_new_from_args(6, base_type, LONG2FIX(b->id++), base_first_lineno, base_first_column, base_last_lineno, base_last_column), children);
+ b->children = children;
+ rb_hash_foreach(branches, branch_coverage_ii, v);
+
+ return ST_CONTINUE;
+}
+
static VALUE
branch_coverage(VALUE branches)
{
- VALUE ret = rb_hash_new();
- VALUE structure = rb_ary_dup(RARRAY_AREF(branches, 0));
- VALUE counters = rb_ary_dup(RARRAY_AREF(branches, 1));
- int i, j;
- long id = 0;
-
- for (i = 0; i < RARRAY_LEN(structure); i++) {
- VALUE branches = RARRAY_AREF(structure, i);
- VALUE base_type = RARRAY_AREF(branches, 0);
- 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(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_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));
- }
- }
+ VALUE structure = RARRAY_AREF(branches, 0);
+
+ struct branch_coverage_result_builder b;
+ b.id = 0;
+ b.result = rb_hash_new();
+ b.counters = RARRAY_AREF(branches, 1);
- return ret;
+ rb_hash_foreach(structure, branch_coverage_i, (VALUE)&b);
+
+ return b.result;
}
static int
@@ -122,45 +243,52 @@ method_coverage_i(void *vstart, void *vend, size_t stride, void *data)
VALUE ncoverages = *(VALUE*)data, v;
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, first_lineno, first_column, last_lineno, last_column;
- VALUE data[5], 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];
- first_column = data[2];
- last_lineno = data[3];
- last_column = data[4];
- if (FIX2LONG(first_lineno) <= 0) continue;
- ncoverage = rb_hash_aref(ncoverages, path);
- if (NIL_P(ncoverage)) continue;
- methods = rb_hash_aref(ncoverage, methods_id);
-
- {
- VALUE method_id = ID2SYM(me->def->original_id);
- VALUE rcount = rb_hash_aref(me2counter, (VALUE) me);
- VALUE key = rb_ary_new_from_args(6, klass, method_id, first_lineno, first_column, last_lineno, last_column);
- 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);
- }
- }
+ void *poisoned = rb_asan_poisoned_object_p(v);
+ rb_asan_unpoison_object(v, false);
+
+ 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, first_lineno, first_column, last_lineno, last_column;
+ VALUE data[5], 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];
+ first_column = data[2];
+ last_lineno = data[3];
+ last_column = data[4];
+ if (FIX2LONG(first_lineno) <= 0) continue;
+ ncoverage = rb_hash_aref(ncoverages, path);
+ if (NIL_P(ncoverage)) continue;
+ methods = rb_hash_aref(ncoverage, methods_id);
+
+ {
+ VALUE method_id = ID2SYM(me->def->original_id);
+ VALUE rcount = rb_hash_aref(me2counter, (VALUE) me);
+ VALUE key = rb_ary_new_from_args(6, klass, method_id, first_lineno, first_column, last_lineno, last_column);
+ 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);
+ }
+ }
+
+ if (poisoned) {
+ rb_asan_poison_object(v);
+ }
}
return 0;
}
@@ -172,32 +300,32 @@ coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
VALUE coverage = (VALUE)val;
VALUE coverages = (VALUE)h;
if (current_mode == 0) {
- /* compatible mode */
- VALUE lines = rb_ary_dup(RARRAY_AREF(coverage, COVERAGE_INDEX_LINES));
- rb_ary_freeze(lines);
- coverage = lines;
+ /* compatible mode */
+ VALUE lines = rb_ary_dup(RARRAY_AREF(coverage, COVERAGE_INDEX_LINES));
+ rb_ary_freeze(lines);
+ coverage = lines;
}
else {
- VALUE h = rb_hash_new();
+ VALUE h = rb_hash_new();
- if (current_mode & COVERAGE_TARGET_LINES) {
- VALUE lines = RARRAY_AREF(coverage, COVERAGE_INDEX_LINES);
+ if (current_mode & COVERAGE_TARGET_LINES) {
+ VALUE lines = RARRAY_AREF(coverage, COVERAGE_INDEX_LINES);
const char *kw = (current_mode & COVERAGE_TARGET_ONESHOT_LINES) ? "oneshot_lines" : "lines";
- lines = rb_ary_dup(lines);
- rb_ary_freeze(lines);
+ lines = rb_ary_dup(lines);
+ rb_ary_freeze(lines);
rb_hash_aset(h, ID2SYM(rb_intern(kw)), lines);
- }
+ }
- 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 (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 (current_mode & COVERAGE_TARGET_METHODS) {
- rb_hash_aset(h, ID2SYM(rb_intern("methods")), rb_hash_new());
- }
+ if (current_mode & COVERAGE_TARGET_METHODS) {
+ rb_hash_aset(h, ID2SYM(rb_intern("methods")), rb_hash_new());
+ }
- coverage = h;
+ coverage = h;
}
rb_hash_aset(coverages, path, coverage);
@@ -209,7 +337,7 @@ coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
* Coverage.peek_result => hash
*
* Returns a hash that contains filename as key and coverage array as value.
- * This is the same as `Coverage.result(stop: false, clear: false)`.
+ * This is the same as <tt>Coverage.result(stop: false, clear: false)</tt>.
*
* {
* "file.rb" => [1, 2, nil],
@@ -222,12 +350,13 @@ rb_coverage_peek_result(VALUE klass)
VALUE coverages = rb_get_coverages();
VALUE ncoverages = rb_hash_new();
if (!RTEST(coverages)) {
- rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
+ rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
}
- st_foreach(RHASH_TBL(coverages), coverage_peek_result_i, ncoverages);
+
+ rb_hash_foreach(coverages, coverage_peek_result_i, ncoverages);
if (current_mode & COVERAGE_TARGET_METHODS) {
- rb_objspace_each_objects(method_coverage_i, &ncoverages);
+ rb_objspace_each_objects(method_coverage_i, &ncoverages);
}
rb_hash_freeze(ncoverages);
@@ -243,6 +372,24 @@ clear_me2counter_i(VALUE key, VALUE value, VALUE unused)
}
/*
+ * call-seq:
+ * Coverage.suspend => nil
+ *
+ * Suspend the coverage measurement.
+ * You can use Coverage.resume to restart the measurement.
+ */
+VALUE
+rb_coverage_suspend(VALUE klass)
+{
+ if (current_state != RUNNING) {
+ rb_raise(rb_eRuntimeError, "coverage measurement is not running");
+ }
+ rb_suspend_coverages();
+ current_state = SUSPENDED;
+ return Qnil;
+}
+
+/*
* call-seq:
* Coverage.result(stop: true, clear: true) => hash
*
@@ -257,6 +404,10 @@ rb_coverage_result(int argc, VALUE *argv, VALUE klass)
VALUE opt;
int stop = 1, clear = 1;
+ if (current_state == IDLE) {
+ rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
+ }
+
rb_scan_args(argc, argv, "01", &opt);
if (argc == 1) {
@@ -275,8 +426,12 @@ rb_coverage_result(int argc, VALUE *argv, VALUE klass)
if (!NIL_P(me2counter)) rb_hash_foreach(me2counter, clear_me2counter_i, Qnil);
}
if (stop) {
+ if (current_state == RUNNING) {
+ rb_coverage_suspend(klass);
+ }
rb_reset_coverages();
me2counter = Qnil;
+ current_state = IDLE;
}
return ncoverages;
}
@@ -284,6 +439,23 @@ rb_coverage_result(int argc, VALUE *argv, VALUE klass)
/*
* call-seq:
+ * Coverage.state => :idle, :suspended, :running
+ *
+ * Returns the state of the coverage measurement.
+ */
+static VALUE
+rb_coverage_state(VALUE klass)
+{
+ switch (current_state) {
+ case IDLE: return ID2SYM(rb_intern("idle"));
+ case SUSPENDED: return ID2SYM(rb_intern("suspended"));
+ case RUNNING: return ID2SYM(rb_intern("running"));
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
* Coverage.running? => bool
*
* Returns true if coverage stats are currently being collected (after
@@ -292,13 +464,15 @@ rb_coverage_result(int argc, VALUE *argv, VALUE klass)
static VALUE
rb_coverage_running(VALUE klass)
{
- VALUE coverages = rb_get_coverages();
- return RTEST(coverages) ? Qtrue : Qfalse;
+ return current_state == RUNNING ? Qtrue : Qfalse;
}
-/* Coverage provides coverage measurement feature for Ruby.
+/* \Coverage provides coverage measurement feature for Ruby.
* This feature is experimental, so these APIs may be changed in future.
*
+ * Caveat: Currently, only process-global coverage measurement is supported.
+ * You cannot measure per-thread coverage.
+ *
* = Usage
*
* 1. require "coverage"
@@ -309,7 +483,7 @@ rb_coverage_running(VALUE klass)
* number of line execution by the interpreter. A +nil+ value means
* coverage is disabled for this line (lines like +else+ and +end+).
*
- * = Example
+ * = Examples
*
* [foo.rb]
* s = 0
@@ -328,14 +502,131 @@ rb_coverage_running(VALUE klass)
* Coverage.start
* require "foo.rb"
* p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
+ *
+ * == Lines \Coverage
+ *
+ * If a coverage mode is not explicitly specified when starting coverage, lines
+ * coverage is what will run. It reports the number of line executions for each
+ * line.
+ *
+ * require "coverage"
+ * Coverage.start(lines: true)
+ * require "foo.rb"
+ * p Coverage.result #=> {"foo.rb"=>{:lines=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}}
+ *
+ * The value of the lines coverage result is an array containing how many times
+ * each line was executed. Order in this array is important. For example, the
+ * first item in this array, at index 0, reports how many times line 1 of this
+ * file was executed while coverage was run (which, in this example, is one
+ * time).
+ *
+ * A +nil+ value means coverage is disabled for this line (lines like +else+
+ * and +end+).
+ *
+ * == Oneshot Lines \Coverage
+ *
+ * Oneshot lines coverage tracks and reports on the executed lines while
+ * coverage is running. It will not report how many times a line was executed,
+ * only that it was executed.
+ *
+ * require "coverage"
+ * Coverage.start(oneshot_lines: true)
+ * require "foo.rb"
+ * p Coverage.result #=> {"foo.rb"=>{:oneshot_lines=>[1, 2, 3, 6, 7]}}
+ *
+ * The value of the oneshot lines coverage result is an array containing the
+ * line numbers that were executed.
+ *
+ * == Branches \Coverage
+ *
+ * Branches coverage reports how many times each branch within each conditional
+ * was executed.
+ *
+ * require "coverage"
+ * Coverage.start(branches: true)
+ * require "foo.rb"
+ * p Coverage.result #=> {"foo.rb"=>{:branches=>{[:if, 0, 6, 0, 10, 3]=>{[:then, 1, 7, 2, 7, 7]=>1, [:else, 2, 9, 2, 9, 7]=>0}}}}
+ *
+ * Each entry within the branches hash is a conditional, the value of which is
+ * another hash where each entry is a branch in that conditional. The values
+ * are the number of times the method was executed, and the keys are identifying
+ * information about the branch.
+ *
+ * The information that makes up each key identifying branches or conditionals
+ * is the following, from left to right:
+ *
+ * 1. A label for the type of branch or conditional.
+ * 2. A unique identifier.
+ * 3. The starting line number it appears on in the file.
+ * 4. The starting column number it appears on in the file.
+ * 5. The ending line number it appears on in the file.
+ * 6. The ending column number it appears on in the file.
+ *
+ * == Methods \Coverage
+ *
+ * Methods coverage reports how many times each method was executed.
+ *
+ * [foo_method.rb]
+ * class Greeter
+ * def greet
+ * "welcome!"
+ * end
+ * end
+ *
+ * def hello
+ * "Hi"
+ * end
+ *
+ * hello()
+ * Greeter.new.greet()
+ * [EOF]
+ *
+ * require "coverage"
+ * Coverage.start(methods: true)
+ * require "foo_method.rb"
+ * p Coverage.result #=> {"foo_method.rb"=>{:methods=>{[Object, :hello, 7, 0, 9, 3]=>1, [Greeter, :greet, 2, 2, 4, 5]=>1}}}
+ *
+ * Each entry within the methods hash represents a method. The values in this
+ * hash are the number of times the method was executed, and the keys are
+ * identifying information about the method.
+ *
+ * The information that makes up each key identifying a method is the following,
+ * from left to right:
+ *
+ * 1. The class.
+ * 2. The method name.
+ * 3. The starting line number the method appears on in the file.
+ * 4. The starting column number the method appears on in the file.
+ * 5. The ending line number the method appears on in the file.
+ * 6. The ending column number the method appears on in the file.
+ *
+ * == All \Coverage Modes
+ *
+ * You can also run all modes of coverage simultaneously with this shortcut.
+ * Note that running all coverage modes does not run both lines and oneshot
+ * lines. Those modes cannot be run simultaneously. Lines coverage is run in
+ * this case, because you can still use it to determine whether or not a line
+ * was executed.
+ *
+ * require "coverage"
+ * Coverage.start(:all)
+ * require "foo.rb"
+ * p Coverage.result #=> {"foo.rb"=>{:lines=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil], :branches=>{[:if, 0, 6, 0, 10, 3]=>{[:then, 1, 7, 2, 7, 7]=>1, [:else, 2, 9, 2, 9, 7]=>0}}, :methods=>{}}}
*/
void
Init_coverage(void)
{
VALUE rb_mCoverage = rb_define_module("Coverage");
+
+ rb_define_singleton_method(rb_mCoverage, "supported?", rb_coverage_supported, 1);
+
+ rb_define_module_function(rb_mCoverage, "setup", rb_coverage_setup, -1);
rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, -1);
+ rb_define_module_function(rb_mCoverage, "resume", rb_coverage_resume, 0);
+ rb_define_module_function(rb_mCoverage, "suspend", rb_coverage_suspend, 0);
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, -1);
rb_define_module_function(rb_mCoverage, "peek_result", rb_coverage_peek_result, 0);
+ rb_define_module_function(rb_mCoverage, "state", rb_coverage_state, 0);
rb_define_module_function(rb_mCoverage, "running?", rb_coverage_running, 0);
rb_global_variable(&me2counter);
}
diff --git a/ext/coverage/depend b/ext/coverage/depend
index ad8c071fbe..fb7f9a95af 100644
--- a/ext/coverage/depend
+++ b/ext/coverage/depend
@@ -1,25 +1,172 @@
-$(OBJS): $(HDRS) $(ruby_headers) \
- $(top_srcdir)/vm_core.h \
- $(top_srcdir)/node.h \
- $(top_srcdir)/vm_debug.h \
- $(top_srcdir)/vm_opts.h \
- {$(VPATH)}id.h \
- $(top_srcdir)/method.h \
- $(top_srcdir)/ruby_atomic.h \
- $(top_srcdir)/thread_pthread.h \
- $(top_srcdir)/internal.h \
- $(hdrdir)/ruby/thread_native.h
-
# AUTOGENERATED DEPENDENCIES START
coverage.o: $(RUBY_EXTCONF_H)
coverage.o: $(arch_hdrdir)/ruby/config.h
coverage.o: $(hdrdir)/ruby.h
coverage.o: $(hdrdir)/ruby/assert.h
+coverage.o: $(hdrdir)/ruby/atomic.h
coverage.o: $(hdrdir)/ruby/backward.h
+coverage.o: $(hdrdir)/ruby/backward/2/assume.h
+coverage.o: $(hdrdir)/ruby/backward/2/attributes.h
+coverage.o: $(hdrdir)/ruby/backward/2/bool.h
+coverage.o: $(hdrdir)/ruby/backward/2/gcc_version_since.h
+coverage.o: $(hdrdir)/ruby/backward/2/inttypes.h
+coverage.o: $(hdrdir)/ruby/backward/2/limits.h
+coverage.o: $(hdrdir)/ruby/backward/2/long_long.h
+coverage.o: $(hdrdir)/ruby/backward/2/stdalign.h
+coverage.o: $(hdrdir)/ruby/backward/2/stdarg.h
coverage.o: $(hdrdir)/ruby/defines.h
coverage.o: $(hdrdir)/ruby/encoding.h
coverage.o: $(hdrdir)/ruby/intern.h
-coverage.o: $(hdrdir)/ruby/io.h
+coverage.o: $(hdrdir)/ruby/internal/abi.h
+coverage.o: $(hdrdir)/ruby/internal/anyargs.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/char.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/double.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/fixnum.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/gid_t.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/int.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/intptr_t.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/long.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/long_long.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/mode_t.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/off_t.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/pid_t.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/short.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/size_t.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/st_data_t.h
+coverage.o: $(hdrdir)/ruby/internal/arithmetic/uid_t.h
+coverage.o: $(hdrdir)/ruby/internal/assume.h
+coverage.o: $(hdrdir)/ruby/internal/attr/alloc_size.h
+coverage.o: $(hdrdir)/ruby/internal/attr/artificial.h
+coverage.o: $(hdrdir)/ruby/internal/attr/cold.h
+coverage.o: $(hdrdir)/ruby/internal/attr/const.h
+coverage.o: $(hdrdir)/ruby/internal/attr/constexpr.h
+coverage.o: $(hdrdir)/ruby/internal/attr/deprecated.h
+coverage.o: $(hdrdir)/ruby/internal/attr/diagnose_if.h
+coverage.o: $(hdrdir)/ruby/internal/attr/enum_extensibility.h
+coverage.o: $(hdrdir)/ruby/internal/attr/error.h
+coverage.o: $(hdrdir)/ruby/internal/attr/flag_enum.h
+coverage.o: $(hdrdir)/ruby/internal/attr/forceinline.h
+coverage.o: $(hdrdir)/ruby/internal/attr/format.h
+coverage.o: $(hdrdir)/ruby/internal/attr/maybe_unused.h
+coverage.o: $(hdrdir)/ruby/internal/attr/noalias.h
+coverage.o: $(hdrdir)/ruby/internal/attr/nodiscard.h
+coverage.o: $(hdrdir)/ruby/internal/attr/noexcept.h
+coverage.o: $(hdrdir)/ruby/internal/attr/noinline.h
+coverage.o: $(hdrdir)/ruby/internal/attr/nonnull.h
+coverage.o: $(hdrdir)/ruby/internal/attr/noreturn.h
+coverage.o: $(hdrdir)/ruby/internal/attr/packed_struct.h
+coverage.o: $(hdrdir)/ruby/internal/attr/pure.h
+coverage.o: $(hdrdir)/ruby/internal/attr/restrict.h
+coverage.o: $(hdrdir)/ruby/internal/attr/returns_nonnull.h
+coverage.o: $(hdrdir)/ruby/internal/attr/warning.h
+coverage.o: $(hdrdir)/ruby/internal/attr/weakref.h
+coverage.o: $(hdrdir)/ruby/internal/cast.h
+coverage.o: $(hdrdir)/ruby/internal/compiler_is.h
+coverage.o: $(hdrdir)/ruby/internal/compiler_is/apple.h
+coverage.o: $(hdrdir)/ruby/internal/compiler_is/clang.h
+coverage.o: $(hdrdir)/ruby/internal/compiler_is/gcc.h
+coverage.o: $(hdrdir)/ruby/internal/compiler_is/intel.h
+coverage.o: $(hdrdir)/ruby/internal/compiler_is/msvc.h
+coverage.o: $(hdrdir)/ruby/internal/compiler_is/sunpro.h
+coverage.o: $(hdrdir)/ruby/internal/compiler_since.h
+coverage.o: $(hdrdir)/ruby/internal/config.h
+coverage.o: $(hdrdir)/ruby/internal/constant_p.h
+coverage.o: $(hdrdir)/ruby/internal/core.h
+coverage.o: $(hdrdir)/ruby/internal/core/rarray.h
+coverage.o: $(hdrdir)/ruby/internal/core/rbasic.h
+coverage.o: $(hdrdir)/ruby/internal/core/rbignum.h
+coverage.o: $(hdrdir)/ruby/internal/core/rclass.h
+coverage.o: $(hdrdir)/ruby/internal/core/rdata.h
+coverage.o: $(hdrdir)/ruby/internal/core/rfile.h
+coverage.o: $(hdrdir)/ruby/internal/core/rhash.h
+coverage.o: $(hdrdir)/ruby/internal/core/robject.h
+coverage.o: $(hdrdir)/ruby/internal/core/rregexp.h
+coverage.o: $(hdrdir)/ruby/internal/core/rstring.h
+coverage.o: $(hdrdir)/ruby/internal/core/rstruct.h
+coverage.o: $(hdrdir)/ruby/internal/core/rtypeddata.h
+coverage.o: $(hdrdir)/ruby/internal/ctype.h
+coverage.o: $(hdrdir)/ruby/internal/dllexport.h
+coverage.o: $(hdrdir)/ruby/internal/dosish.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/coderange.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/ctype.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/encoding.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/pathname.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/re.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/sprintf.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/string.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/symbol.h
+coverage.o: $(hdrdir)/ruby/internal/encoding/transcode.h
+coverage.o: $(hdrdir)/ruby/internal/error.h
+coverage.o: $(hdrdir)/ruby/internal/eval.h
+coverage.o: $(hdrdir)/ruby/internal/event.h
+coverage.o: $(hdrdir)/ruby/internal/fl_type.h
+coverage.o: $(hdrdir)/ruby/internal/gc.h
+coverage.o: $(hdrdir)/ruby/internal/glob.h
+coverage.o: $(hdrdir)/ruby/internal/globals.h
+coverage.o: $(hdrdir)/ruby/internal/has/attribute.h
+coverage.o: $(hdrdir)/ruby/internal/has/builtin.h
+coverage.o: $(hdrdir)/ruby/internal/has/c_attribute.h
+coverage.o: $(hdrdir)/ruby/internal/has/cpp_attribute.h
+coverage.o: $(hdrdir)/ruby/internal/has/declspec_attribute.h
+coverage.o: $(hdrdir)/ruby/internal/has/extension.h
+coverage.o: $(hdrdir)/ruby/internal/has/feature.h
+coverage.o: $(hdrdir)/ruby/internal/has/warning.h
+coverage.o: $(hdrdir)/ruby/internal/intern/array.h
+coverage.o: $(hdrdir)/ruby/internal/intern/bignum.h
+coverage.o: $(hdrdir)/ruby/internal/intern/class.h
+coverage.o: $(hdrdir)/ruby/internal/intern/compar.h
+coverage.o: $(hdrdir)/ruby/internal/intern/complex.h
+coverage.o: $(hdrdir)/ruby/internal/intern/cont.h
+coverage.o: $(hdrdir)/ruby/internal/intern/dir.h
+coverage.o: $(hdrdir)/ruby/internal/intern/enum.h
+coverage.o: $(hdrdir)/ruby/internal/intern/enumerator.h
+coverage.o: $(hdrdir)/ruby/internal/intern/error.h
+coverage.o: $(hdrdir)/ruby/internal/intern/eval.h
+coverage.o: $(hdrdir)/ruby/internal/intern/file.h
+coverage.o: $(hdrdir)/ruby/internal/intern/hash.h
+coverage.o: $(hdrdir)/ruby/internal/intern/io.h
+coverage.o: $(hdrdir)/ruby/internal/intern/load.h
+coverage.o: $(hdrdir)/ruby/internal/intern/marshal.h
+coverage.o: $(hdrdir)/ruby/internal/intern/numeric.h
+coverage.o: $(hdrdir)/ruby/internal/intern/object.h
+coverage.o: $(hdrdir)/ruby/internal/intern/parse.h
+coverage.o: $(hdrdir)/ruby/internal/intern/proc.h
+coverage.o: $(hdrdir)/ruby/internal/intern/process.h
+coverage.o: $(hdrdir)/ruby/internal/intern/random.h
+coverage.o: $(hdrdir)/ruby/internal/intern/range.h
+coverage.o: $(hdrdir)/ruby/internal/intern/rational.h
+coverage.o: $(hdrdir)/ruby/internal/intern/re.h
+coverage.o: $(hdrdir)/ruby/internal/intern/ruby.h
+coverage.o: $(hdrdir)/ruby/internal/intern/select.h
+coverage.o: $(hdrdir)/ruby/internal/intern/select/largesize.h
+coverage.o: $(hdrdir)/ruby/internal/intern/set.h
+coverage.o: $(hdrdir)/ruby/internal/intern/signal.h
+coverage.o: $(hdrdir)/ruby/internal/intern/sprintf.h
+coverage.o: $(hdrdir)/ruby/internal/intern/string.h
+coverage.o: $(hdrdir)/ruby/internal/intern/struct.h
+coverage.o: $(hdrdir)/ruby/internal/intern/thread.h
+coverage.o: $(hdrdir)/ruby/internal/intern/time.h
+coverage.o: $(hdrdir)/ruby/internal/intern/variable.h
+coverage.o: $(hdrdir)/ruby/internal/intern/vm.h
+coverage.o: $(hdrdir)/ruby/internal/interpreter.h
+coverage.o: $(hdrdir)/ruby/internal/iterator.h
+coverage.o: $(hdrdir)/ruby/internal/memory.h
+coverage.o: $(hdrdir)/ruby/internal/method.h
+coverage.o: $(hdrdir)/ruby/internal/module.h
+coverage.o: $(hdrdir)/ruby/internal/newobj.h
+coverage.o: $(hdrdir)/ruby/internal/scan_args.h
+coverage.o: $(hdrdir)/ruby/internal/special_consts.h
+coverage.o: $(hdrdir)/ruby/internal/static_assert.h
+coverage.o: $(hdrdir)/ruby/internal/stdalign.h
+coverage.o: $(hdrdir)/ruby/internal/stdbool.h
+coverage.o: $(hdrdir)/ruby/internal/stdckdint.h
+coverage.o: $(hdrdir)/ruby/internal/symbol.h
+coverage.o: $(hdrdir)/ruby/internal/value.h
+coverage.o: $(hdrdir)/ruby/internal/value_type.h
+coverage.o: $(hdrdir)/ruby/internal/variable.h
+coverage.o: $(hdrdir)/ruby/internal/warning_push.h
+coverage.o: $(hdrdir)/ruby/internal/xmalloc.h
coverage.o: $(hdrdir)/ruby/missing.h
coverage.o: $(hdrdir)/ruby/onigmo.h
coverage.o: $(hdrdir)/ruby/oniguruma.h
@@ -31,15 +178,32 @@ coverage.o: $(top_srcdir)/ccan/check_type/check_type.h
coverage.o: $(top_srcdir)/ccan/container_of/container_of.h
coverage.o: $(top_srcdir)/ccan/list/list.h
coverage.o: $(top_srcdir)/ccan/str/str.h
-coverage.o: $(top_srcdir)/gc.h
+coverage.o: $(top_srcdir)/constant.h
+coverage.o: $(top_srcdir)/id_table.h
coverage.o: $(top_srcdir)/internal.h
+coverage.o: $(top_srcdir)/internal/array.h
+coverage.o: $(top_srcdir)/internal/basic_operators.h
+coverage.o: $(top_srcdir)/internal/box.h
+coverage.o: $(top_srcdir)/internal/compilers.h
+coverage.o: $(top_srcdir)/internal/gc.h
+coverage.o: $(top_srcdir)/internal/hash.h
+coverage.o: $(top_srcdir)/internal/imemo.h
+coverage.o: $(top_srcdir)/internal/sanitizers.h
+coverage.o: $(top_srcdir)/internal/serial.h
+coverage.o: $(top_srcdir)/internal/set_table.h
+coverage.o: $(top_srcdir)/internal/static_assert.h
+coverage.o: $(top_srcdir)/internal/thread.h
+coverage.o: $(top_srcdir)/internal/variable.h
+coverage.o: $(top_srcdir)/internal/vm.h
+coverage.o: $(top_srcdir)/internal/warnings.h
coverage.o: $(top_srcdir)/method.h
coverage.o: $(top_srcdir)/node.h
coverage.o: $(top_srcdir)/ruby_assert.h
coverage.o: $(top_srcdir)/ruby_atomic.h
+coverage.o: $(top_srcdir)/rubyparser.h
+coverage.o: $(top_srcdir)/shape.h
coverage.o: $(top_srcdir)/thread_pthread.h
coverage.o: $(top_srcdir)/vm_core.h
-coverage.o: $(top_srcdir)/vm_debug.h
coverage.o: $(top_srcdir)/vm_opts.h
coverage.o: coverage.c
coverage.o: {$(VPATH)}id.h
diff --git a/ext/coverage/lib/coverage.rb b/ext/coverage/lib/coverage.rb
index f1923ef366..4bd20e22cb 100644
--- a/ext/coverage/lib/coverage.rb
+++ b/ext/coverage/lib/coverage.rb
@@ -1,6 +1,11 @@
require "coverage.so"
module Coverage
+ # call-seq:
+ # line_stub(file) -> array
+ #
+ # A simple helper function that creates the "stub" of line coverage
+ # from a given source code.
def self.line_stub(file)
lines = File.foreach(file).map { nil }
iseqs = [RubyVM::InstructionSequence.compile_file(file)]