2008-07-03 18:27:43 +04:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
coverage.c -
|
|
|
|
|
|
|
|
$Author: $
|
|
|
|
|
|
|
|
Copyright (c) 2008 Yusuke Endoh
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
2008-07-03 16:55:12 +04:00
|
|
|
#include "ruby.h"
|
2010-08-14 14:12:13 +04:00
|
|
|
#include "vm_core.h"
|
2008-07-08 19:13:22 +04:00
|
|
|
|
2011-07-04 19:55:06 +04:00
|
|
|
static VALUE rb_coverages = Qundef;
|
|
|
|
|
2008-07-08 19:13:22 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Coverage.start => nil
|
|
|
|
*
|
|
|
|
* Enables coverage measurement.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_coverage_start(VALUE klass)
|
|
|
|
{
|
|
|
|
if (!RTEST(rb_get_coverages())) {
|
2011-07-04 19:55:06 +04:00
|
|
|
if (rb_coverages == Qundef) {
|
|
|
|
rb_coverages = rb_hash_new();
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 14:49:11 +04:00
|
|
|
rb_obj_hide(rb_coverages);
|
2011-07-04 19:55:06 +04:00
|
|
|
}
|
|
|
|
rb_set_coverages(rb_coverages);
|
2008-07-08 19:13:22 +04:00
|
|
|
}
|
2008-07-10 09:09:08 +04:00
|
|
|
return Qnil;
|
2008-07-08 19:13:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-02-13 18:53:47 +03:00
|
|
|
coverage_clear_result_i(st_data_t key, st_data_t val, st_data_t h)
|
|
|
|
{
|
|
|
|
VALUE coverage = (VALUE)val;
|
2015-02-14 02:26:26 +03:00
|
|
|
rb_ary_clear(coverage);
|
2015-02-13 18:53:47 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
|
2008-07-08 19:13:22 +04:00
|
|
|
{
|
2011-07-04 19:55:06 +04:00
|
|
|
VALUE path = (VALUE)key;
|
2008-07-08 19:13:22 +04:00
|
|
|
VALUE coverage = (VALUE)val;
|
2011-07-04 19:55:06 +04:00
|
|
|
VALUE coverages = (VALUE)h;
|
|
|
|
coverage = rb_ary_dup(coverage);
|
2008-07-08 19:13:22 +04:00
|
|
|
rb_ary_freeze(coverage);
|
2011-07-04 19:55:06 +04:00
|
|
|
rb_hash_aset(coverages, path, coverage);
|
2008-07-08 19:13:22 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2015-02-13 18:53:47 +03:00
|
|
|
* Coverage.peek_result => hash
|
2008-07-08 19:13:22 +04:00
|
|
|
*
|
2015-02-13 18:53:47 +03:00
|
|
|
* Returns a hash that contains filename as key and coverage array as value.
|
2008-07-08 19:13:22 +04:00
|
|
|
*/
|
|
|
|
static VALUE
|
2015-02-13 18:53:47 +03:00
|
|
|
rb_coverage_peek_result(VALUE klass)
|
2008-07-08 19:13:22 +04:00
|
|
|
{
|
|
|
|
VALUE coverages = rb_get_coverages();
|
2011-07-04 19:55:06 +04:00
|
|
|
VALUE ncoverages = rb_hash_new();
|
2008-07-08 19:13:22 +04:00
|
|
|
if (!RTEST(coverages)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
|
|
|
|
}
|
2015-02-13 18:53:47 +03:00
|
|
|
st_foreach(RHASH_TBL(coverages), coverage_peek_result_i, ncoverages);
|
2011-07-04 19:55:06 +04:00
|
|
|
rb_hash_freeze(ncoverages);
|
2015-02-13 18:53:47 +03:00
|
|
|
return ncoverages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 ncoverages = rb_coverage_peek_result(klass);
|
|
|
|
VALUE coverages = rb_get_coverages();
|
|
|
|
st_foreach(RHASH_TBL(coverages), coverage_clear_result_i, ncoverages);
|
2008-07-08 19:13:22 +04:00
|
|
|
rb_reset_coverages();
|
2011-07-04 19:55:06 +04:00
|
|
|
return ncoverages;
|
2008-07-08 19:13:22 +04:00
|
|
|
}
|
2008-07-03 16:55:12 +04:00
|
|
|
|
2008-07-03 18:27:43 +04:00
|
|
|
/* Coverage provides coverage measurement feature for Ruby.
|
2008-07-08 19:13:22 +04:00
|
|
|
* This feature is experimental, so these APIs may be changed in future.
|
2008-07-03 18:27:43 +04:00
|
|
|
*
|
|
|
|
* = Usage
|
|
|
|
*
|
2011-10-11 01:06:39 +04:00
|
|
|
* 1. require "coverage.so"
|
|
|
|
* 2. do Coverage.start
|
|
|
|
* 3. require or load Ruby source file
|
|
|
|
* 4. Coverage.result will return a hash that contains filename as key and
|
|
|
|
* coverage array as value. A coverage array gives, for each line, the
|
|
|
|
* number of line execution by the interpreter. A +nil+ value means
|
|
|
|
* coverage is disabled for this line (lines like +else+ and +end+).
|
2008-07-03 18:27:43 +04:00
|
|
|
*
|
|
|
|
* = Example
|
|
|
|
*
|
|
|
|
* [foo.rb]
|
|
|
|
* s = 0
|
|
|
|
* 10.times do |x|
|
|
|
|
* s += x
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* if s == 45
|
|
|
|
* p :ok
|
|
|
|
* else
|
|
|
|
* p :ng
|
|
|
|
* end
|
|
|
|
* [EOF]
|
|
|
|
*
|
|
|
|
* require "coverage.so"
|
2008-07-08 19:13:22 +04:00
|
|
|
* Coverage.start
|
2008-07-03 18:27:43 +04:00
|
|
|
* require "foo.rb"
|
2008-07-08 19:13:22 +04:00
|
|
|
* p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
|
2008-07-03 18:27:43 +04:00
|
|
|
*/
|
2008-07-03 16:55:12 +04:00
|
|
|
void
|
|
|
|
Init_coverage(void)
|
|
|
|
{
|
2008-08-31 08:48:02 +04:00
|
|
|
VALUE rb_mCoverage = rb_define_module("Coverage");
|
2008-07-08 19:13:22 +04:00
|
|
|
rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0);
|
|
|
|
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
|
2015-02-13 18:53:47 +03:00
|
|
|
rb_define_module_function(rb_mCoverage, "peek_result", rb_coverage_peek_result, 0);
|
2011-07-04 19:55:06 +04:00
|
|
|
rb_gc_register_address(&rb_coverages);
|
2008-07-03 16:55:12 +04:00
|
|
|
}
|