* thread.c (rb_set_coverages, rb_reset_coverages): enable and disable

coverage measurement.

* thread.c (rb_get_coverages): rename and move from vm.c.

* vm.c (rb_vm_get_coverages): ditto.

* iseq.c (prepare_iseq_build): ditto.

* thread.c (clear_coverage): ditto.

* parse.y (coverage): ditto.

* ext/coverage/coverage.c: use above functions, add new method
  Coverage.start and fix rdoc .


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17961 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2008-07-08 15:13:22 +00:00
Родитель 4857543691
Коммит 91c07a5ac6
6 изменённых файлов: 97 добавлений и 45 удалений

Просмотреть файл

@ -1,3 +1,21 @@
Wed Jul 9 00:12:31 2008 Yusuke Endoh <mame@tsg.ne.jp>
* thread.c (rb_set_coverages, rb_reset_coverages): enable and disable
coverage measurement.
* thread.c (rb_get_coverages): rename and move from vm.c.
* vm.c (rb_vm_get_coverages): ditto.
* iseq.c (prepare_iseq_build): ditto.
* thread.c (clear_coverage): ditto.
* parse.y (coverage): ditto.
* ext/coverage/coverage.c: use above functions, add new method
Coverage.start and fix rdoc .
Tue Jul 8 23:02:35 2008 Masaki Suketa <masaki.suketa@nifty.ne.jp> Tue Jul 8 23:02:35 2008 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* ext/win32ole/win32ole.c (find_default_source): bug fix when * ext/win32ole/win32ole.c (find_default_source): bug fix when

Просмотреть файл

@ -10,15 +10,67 @@
#include "ruby.h" #include "ruby.h"
extern void rb_enable_coverages(void); static VALUE rb_mCoverage;
extern VALUE rb_get_coverages(void);
extern void rb_set_coverages(VALUE);
extern void rb_reset_coverages(void);
/*
* call-seq:
* Coverage.start => nil
*
* Enables coverage measurement.
*/
static VALUE
rb_coverage_start(VALUE klass)
{
if (!RTEST(rb_get_coverages())) {
VALUE coverages = rb_hash_new();
RBASIC(coverages)->klass = 0;
rb_set_coverages(coverages);
}
}
static int
coverage_result_i(st_data_t key, st_data_t val, st_data_t dummy)
{
VALUE coverage = (VALUE)val;
RBASIC(coverage)->klass = rb_cArray;
rb_ary_freeze(coverage);
return ST_CONTINUE;
}
/*
* call-seq:
* Coverage.result => hash
*
* Returns a hash that contains filename as key and coverage array as value
* and disables coverage measurement.
*/
static VALUE
rb_coverage_result(VALUE klass)
{
VALUE coverages = rb_get_coverages();
if (!RTEST(coverages)) {
rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
}
RBASIC(coverages)->klass = rb_cHash;
st_foreach(RHASH_TBL(coverages), coverage_result_i, 0);
rb_hash_freeze(coverages);
rb_reset_coverages();
return coverages;
}
/* 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.
* *
* = Usage * = Usage
* *
* (1) require "coverage.so" * (1) require "coverage.so"
* (2) require or load Ruby source file * (2) do Coverage.start
* (3) Coverage.result will return a hash that contains filename as key and * (3) require or load Ruby source file
* (4) Coverage.result will return a hash that contains filename as key and
* coverage array as value. * coverage array as value.
* *
* = Example * = Example
@ -37,11 +89,14 @@ extern void rb_enable_coverages(void);
* [EOF] * [EOF]
* *
* require "coverage.so" * require "coverage.so"
* Coverage.start
* require "foo.rb" * require "foo.rb"
* p COVERAGE__ #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]} * p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
*/ */
void void
Init_coverage(void) Init_coverage(void)
{ {
rb_enable_coverages(); rb_mCoverage = rb_define_module("Coverage");
rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0);
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
} }

4
iseq.c
Просмотреть файл

@ -194,8 +194,8 @@ prepare_iseq_build(rb_iseq_t *iseq,
iseq->coverage = Qfalse; iseq->coverage = Qfalse;
if (!GET_THREAD()->parse_in_eval) { if (!GET_THREAD()->parse_in_eval) {
extern VALUE rb_vm_get_coverages(void); extern VALUE rb_get_coverages(void);
VALUE coverages = rb_vm_get_coverages(); VALUE coverages = rb_get_coverages();
if (RTEST(coverages)) { if (RTEST(coverages)) {
iseq->coverage = rb_hash_lookup(coverages, filename); iseq->coverage = rb_hash_lookup(coverages, filename);
if (NIL_P(iseq->coverage)) iseq->coverage = Qfalse; if (NIL_P(iseq->coverage)) iseq->coverage = Qfalse;

Просмотреть файл

@ -4672,8 +4672,8 @@ debug_lines(const char *f)
static VALUE static VALUE
coverage(const char *f, int n) coverage(const char *f, int n)
{ {
extern VALUE rb_vm_get_coverages(void); extern VALUE rb_get_coverages(void);
VALUE coverages = rb_vm_get_coverages(); VALUE coverages = rb_get_coverages();
if (RTEST(coverages) && RBASIC(coverages)->klass == 0) { if (RTEST(coverages) && RBASIC(coverages)->klass == 0) {
VALUE fname = rb_str_new2(f); VALUE fname = rb_str_new2(f);
VALUE lines = rb_ary_new2(n); VALUE lines = rb_ary_new2(n);

Просмотреть файл

@ -2117,8 +2117,8 @@ clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
static void static void
clear_coverage(void) clear_coverage(void)
{ {
extern VALUE rb_vm_get_coverages(void); extern VALUE rb_get_coverages(void);
VALUE coverages = rb_vm_get_coverages(); VALUE coverages = rb_get_coverages();
if (RTEST(coverages)) { if (RTEST(coverages)) {
st_foreach(RHASH_TBL(coverages), clear_coverage_i, 0); st_foreach(RHASH_TBL(coverages), clear_coverage_i, 0);
} }
@ -3548,37 +3548,22 @@ update_coverage(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klas
} }
} }
static int VALUE
coverage_result_i(st_data_t key, st_data_t val, st_data_t dummy) rb_get_coverages(void)
{ {
VALUE coverage = (VALUE)val; return GET_VM()->coverages;
RBASIC(coverage)->klass = rb_cArray;
rb_ary_freeze(coverage);
return ST_CONTINUE;
}
static VALUE
rb_coverage_result(VALUE klass)
{
extern VALUE rb_vm_get_coverages(void);
VALUE coverages = rb_vm_get_coverages();
st_foreach(RHASH_TBL(coverages), coverage_result_i, 0);
RBASIC(coverages)->klass = rb_cHash;
rb_hash_freeze(coverages);
return coverages;
} }
void void
rb_enable_coverages(void) rb_set_coverages(VALUE coverages)
{ {
VALUE rb_mCoverage; GET_VM()->coverages = coverages;
rb_add_event_hook(update_coverage, RUBY_EVENT_COVERAGE, Qnil);
if (!RTEST(GET_VM()->coverages)) { }
VALUE coverages = rb_hash_new();
RBASIC(coverages)->klass = 0; void
GET_VM()->coverages = coverages; rb_reset_coverages(void)
rb_add_event_hook(update_coverage, RUBY_EVENT_COVERAGE, Qnil); {
rb_mCoverage = rb_define_module("Coverage"); GET_VM()->coverages = Qfalse;
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0); rb_remove_event_hook(update_coverage);
}
} }

6
vm.c
Просмотреть файл

@ -1909,9 +1909,3 @@ rb_ruby_debug_ptr(void)
{ {
return ruby_vm_debug_ptr(GET_VM()); return ruby_vm_debug_ptr(GET_VM());
} }
VALUE
rb_vm_get_coverages(void)
{
return GET_VM()->coverages;
}