mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* Add coverage measurement constant COVERAGE__. This constant is not
for casual use. Usage: (1) assign {} to COVERAGE__, (2) require or load Ruby source file, and (3) COVERAGE__["sourcefilepath"] will return an array whose elements represent number of executions per line of source code. * vm_core.h: add field of coverage array to iseq. * iseq.c (prepare_iseq_build): ditto. * insns.def (trace): update coverage array. * parse.y (coverage): create and initialize coverage array. * compile.h (ADD_TRACE): add trace instruction to update covearge array. * thread.c (clear_coverage): delete coverage array when forking. Otherwise, double count of coverage may occur. * lib/coverage.rb: sample coverage measurement tool. * error.c: distinguish explicitly between parse_in_eval and mild_compile_error. * load.c: ditto. * vm_eval.c: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17781 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
498324c5d3
commit
5874de95e8
11 changed files with 200 additions and 18 deletions
31
ChangeLog
31
ChangeLog
|
@ -1,3 +1,34 @@
|
|||
Wed Jul 2 01:53:40 2008 Yusuke Endoh <mame@tsg.ne.jp>
|
||||
|
||||
* Add coverage measurement constant COVERAGE__. This constant is not
|
||||
for casual use. Usage: (1) assign {} to COVERAGE__, (2) require or
|
||||
load Ruby source file, and (3) COVERAGE__["sourcefilepath"] will
|
||||
return an array whose elements represent number of executions per
|
||||
line of source code.
|
||||
|
||||
* vm_core.h: add field of coverage array to iseq.
|
||||
|
||||
* iseq.c (prepare_iseq_build): ditto.
|
||||
|
||||
* insns.def (trace): update coverage array.
|
||||
|
||||
* parse.y (coverage): create and initialize coverage array.
|
||||
|
||||
* compile.h (ADD_TRACE): add trace instruction to update covearge
|
||||
array.
|
||||
|
||||
* thread.c (clear_coverage): delete coverage array when forking.
|
||||
Otherwise, double count of coverage may occur.
|
||||
|
||||
* lib/coverage.rb: sample coverage measurement tool.
|
||||
|
||||
* error.c: distinguish explicitly between parse_in_eval and
|
||||
mild_compile_error.
|
||||
|
||||
* load.c: ditto.
|
||||
|
||||
* vm_eval.c: ditto.
|
||||
|
||||
Tue Jul 1 21:32:43 2008 Yusuke Endoh <mame@tsg.ne.jp>
|
||||
|
||||
* lib/test/unit/ui/console/testrunner.rb: prevent destructive
|
||||
|
|
13
compile.h
13
compile.h
|
@ -163,9 +163,16 @@ PRINTF_ARGS(void ruby_debug_printf(const char*, ...), 1, 2);
|
|||
(VALUE)id, (VALUE)argc, (VALUE)block, (VALUE)flag))
|
||||
|
||||
#define ADD_TRACE(seq, line, event) \
|
||||
if (iseq->compile_data->option->trace_instruction) { \
|
||||
ADD_INSN1(seq, line, trace, INT2FIX(event)); \
|
||||
}
|
||||
do { \
|
||||
VALUE coverage = Qfalse; \
|
||||
if ((event) == RUBY_EVENT_LINE && iseq->coverage && RARRAY_PTR(iseq->coverage)[(line) - 1] == Qnil) { \
|
||||
RARRAY_PTR(iseq->coverage)[(line) - 1] = INT2FIX(0); \
|
||||
coverage = iseq->coverage; \
|
||||
} \
|
||||
if (iseq->compile_data->option->trace_instruction || coverage) { \
|
||||
ADD_INSN2(seq, line, trace, INT2FIX(event), coverage); \
|
||||
} \
|
||||
}while(0);
|
||||
|
||||
/* add label */
|
||||
#define ADD_LABEL(seq, label) \
|
||||
|
|
2
error.c
2
error.c
|
@ -1546,7 +1546,7 @@ err_append(const char *s)
|
|||
rb_thread_t *th = GET_THREAD();
|
||||
VALUE err = th->errinfo;
|
||||
|
||||
if (th->parse_in_eval) {
|
||||
if (th->mild_compile_error) {
|
||||
if (!RTEST(err)) {
|
||||
err = rb_exc_new2(rb_eSyntaxError, s);
|
||||
th->errinfo = err;
|
||||
|
|
12
insns.def
12
insns.def
|
@ -847,11 +847,21 @@ defined
|
|||
*/
|
||||
DEFINE_INSN
|
||||
trace
|
||||
(rb_num_t nf)
|
||||
(rb_num_t nf, VALUE coverage)
|
||||
()
|
||||
()
|
||||
{
|
||||
rb_event_flag_t flag = nf;
|
||||
if (coverage) {
|
||||
long line = rb_sourceline() - 1;
|
||||
if (RARRAY_PTR(coverage)[line] == Qnil) {
|
||||
rb_bug("bug");
|
||||
}
|
||||
long count = FIX2LONG(RARRAY_PTR(coverage)[line]) + 1;
|
||||
if (POSFIXABLE(count)) {
|
||||
RARRAY_PTR(coverage)[line] = LONG2FIX(count);
|
||||
}
|
||||
}
|
||||
EXEC_EVENT_HOOK(th, flag, GET_SELF(), 0, 0 /* TODO: id, klass */);
|
||||
}
|
||||
|
||||
|
|
12
iseq.c
12
iseq.c
|
@ -81,6 +81,7 @@ iseq_mark(void *ptr)
|
|||
RUBY_MARK_UNLESS_NULL(iseq->filename);
|
||||
RUBY_MARK_UNLESS_NULL((VALUE)iseq->cref_stack);
|
||||
RUBY_MARK_UNLESS_NULL(iseq->klass);
|
||||
RUBY_MARK_UNLESS_NULL(iseq->coverage);
|
||||
/* RUBY_MARK_UNLESS_NULL((VALUE)iseq->node); */
|
||||
/* RUBY_MARK_UNLESS_NULL(iseq->cached_special_block); */
|
||||
|
||||
|
@ -191,6 +192,17 @@ prepare_iseq_build(rb_iseq_t *iseq,
|
|||
|
||||
set_relation(iseq, parent);
|
||||
|
||||
iseq->coverage = Qfalse;
|
||||
if (!GET_THREAD()->parse_in_eval) {
|
||||
if (rb_const_defined_at(rb_cObject, rb_intern("COVERAGE__"))) {
|
||||
VALUE hash = rb_const_get_at(rb_cObject, rb_intern("COVERAGE__"));
|
||||
if (TYPE(hash) == T_HASH) {
|
||||
iseq->coverage = rb_hash_aref(hash, filename);
|
||||
if (NIL_P(iseq->coverage)) iseq->coverage = Qfalse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Qtrue;
|
||||
}
|
||||
|
||||
|
|
57
lib/coverage.rb
Normal file
57
lib/coverage.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
COVERAGE__ ||= {}
|
||||
ext = ENV["COVERUBY_EXT"] || ".cov"
|
||||
accum = ENV["COVERUBY_ACCUM"]
|
||||
accum = !accum || accum == "" || !(%w(f n 0).include?(accum[0]))
|
||||
pwd = Dir.pwd
|
||||
|
||||
at_exit do
|
||||
Dir.chdir(pwd) do
|
||||
COVERAGE__.each do |sfile, covs|
|
||||
cfile = sfile + ext
|
||||
|
||||
writable = proc do |f|
|
||||
File.writable?(f) || File.writable?(File.dirname(f))
|
||||
end
|
||||
unless writable[cfile]
|
||||
cfile = cfile.gsub(File.PATH_SEPARATOR, "#")
|
||||
next unless writable[cfile]
|
||||
end
|
||||
|
||||
readlines = proc do |f|
|
||||
File.read(f).force_encoding("ASCII-8BIT").lines.to_a
|
||||
end
|
||||
|
||||
sources = (readlines[sfile] rescue [])
|
||||
|
||||
pcovs = []
|
||||
if accum
|
||||
pcovs = (readlines[cfile] rescue []).map.with_index do |line, idx|
|
||||
if line[/^\s*(?:(#####)|(\d+)|-):\s*\d+:(.*)$/n]
|
||||
cov, line = $1 ? 0 : ($2 ? $2.to_i : nil), $3
|
||||
if !sources[idx] || sources[idx].chomp != line.chomp
|
||||
warn("source file changed, ignoring: `#{ cfile }'")
|
||||
break []
|
||||
end
|
||||
cov
|
||||
else
|
||||
p line
|
||||
warn("coverage file corrupted, ignoring: #{ cfile }")
|
||||
break []
|
||||
end
|
||||
end
|
||||
unless pcovs.empty? || pcovs.size == covs.size
|
||||
warn("coverage file changed, ignoring: `#{ cfile }'")
|
||||
pcovs = []
|
||||
end
|
||||
end
|
||||
|
||||
open(cfile, "w") do |out|
|
||||
covs.zip(sources, pcovs).each_with_index do |(cov, line, pcov), idx|
|
||||
cov += pcov || 0 if cov
|
||||
cov = (cov ? (cov == 0 ? "#####" : cov.to_s) : "-").rjust(9)
|
||||
out.puts("%s:% 5d:%s" % [cov, idx + 1, line])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
10
load.c
10
load.c
|
@ -240,8 +240,8 @@ rb_load(VALUE fname, int wrap)
|
|||
rb_thread_t *th = GET_THREAD();
|
||||
volatile VALUE wrapper = th->top_wrapper;
|
||||
volatile VALUE self = th->top_self;
|
||||
volatile int parse_in_eval;
|
||||
volatile int loaded = Qfalse;
|
||||
volatile int mild_compile_error;
|
||||
#ifndef __GNUC__
|
||||
rb_thread_t *volatile th0 = th;
|
||||
#endif
|
||||
|
@ -267,19 +267,19 @@ rb_load(VALUE fname, int wrap)
|
|||
rb_extend_object(th->top_self, th->top_wrapper);
|
||||
}
|
||||
|
||||
parse_in_eval = th->parse_in_eval;
|
||||
mild_compile_error = th->mild_compile_error;
|
||||
PUSH_TAG();
|
||||
state = EXEC_TAG();
|
||||
if (state == 0) {
|
||||
NODE *node;
|
||||
VALUE iseq;
|
||||
|
||||
th->parse_in_eval++;
|
||||
th->mild_compile_error++;
|
||||
node = (NODE *)rb_load_file(RSTRING_PTR(fname));
|
||||
th->parse_in_eval--;
|
||||
loaded = Qtrue;
|
||||
iseq = rb_iseq_new(node, rb_str_new2("<top (required)>"),
|
||||
fname, Qfalse, ISEQ_TYPE_TOP);
|
||||
th->mild_compile_error--;
|
||||
rb_iseq_eval(iseq);
|
||||
}
|
||||
POP_TAG();
|
||||
|
@ -288,7 +288,7 @@ rb_load(VALUE fname, int wrap)
|
|||
th = th0;
|
||||
fname = RB_GC_GUARD(fname);
|
||||
#endif
|
||||
th->parse_in_eval = parse_in_eval;
|
||||
th->mild_compile_error = mild_compile_error;
|
||||
th->top_self = self;
|
||||
th->top_wrapper = wrapper;
|
||||
|
||||
|
|
47
parse.y
47
parse.y
|
@ -249,6 +249,7 @@ struct parser_params {
|
|||
NODE *parser_eval_tree_begin;
|
||||
NODE *parser_eval_tree;
|
||||
VALUE debug_lines;
|
||||
VALUE coverage;
|
||||
int nerr;
|
||||
#else
|
||||
/* Ripper only */
|
||||
|
@ -322,6 +323,7 @@ static int parser_yyerror(struct parser_params*, const char*);
|
|||
#define ruby_eval_tree (parser->parser_eval_tree)
|
||||
#define ruby_eval_tree_begin (parser->parser_eval_tree_begin)
|
||||
#define ruby_debug_lines (parser->debug_lines)
|
||||
#define ruby_coverage (parser->coverage)
|
||||
#endif
|
||||
|
||||
static int yylex(void*, void*);
|
||||
|
@ -4667,6 +4669,32 @@ debug_lines(const char *f)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
coverage(const char *f, int n)
|
||||
{
|
||||
if (rb_const_defined_at(rb_cObject, rb_intern("COVERAGE__"))) {
|
||||
VALUE hash = rb_const_get_at(rb_cObject, rb_intern("COVERAGE__"));
|
||||
if (TYPE(hash) == T_HASH) {
|
||||
VALUE fname = rb_str_new2(f);
|
||||
VALUE lines = rb_ary_new2(n);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) RARRAY_PTR(lines)[i] = Qnil;
|
||||
RARRAY(lines)->len = n;
|
||||
rb_hash_aset(hash, fname, lines);
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
e_option_supplied(struct parser_params *parser)
|
||||
{
|
||||
if (strcmp(ruby_sourcefile, "-e") == 0)
|
||||
return Qtrue;
|
||||
return Qfalse;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
yycompile0(VALUE arg, int tracing)
|
||||
{
|
||||
|
@ -4683,11 +4711,19 @@ yycompile0(VALUE arg, int tracing)
|
|||
rb_ary_push(ruby_debug_lines, str);
|
||||
} while (--n);
|
||||
}
|
||||
|
||||
if (!e_option_supplied(parser)) {
|
||||
ruby_coverage = coverage(ruby_sourcefile, ruby_sourceline);
|
||||
}
|
||||
}
|
||||
|
||||
parser_prepare(parser);
|
||||
n = yyparse((void*)parser);
|
||||
if (ruby_coverage) {
|
||||
rb_ary_freeze(ruby_coverage);
|
||||
}
|
||||
ruby_debug_lines = 0;
|
||||
ruby_coverage = 0;
|
||||
compile_for_eval = 0;
|
||||
|
||||
lex_strterm = 0;
|
||||
|
@ -4750,6 +4786,9 @@ lex_getline(struct parser_params *parser)
|
|||
if (ruby_debug_lines && !NIL_P(line)) {
|
||||
rb_ary_push(ruby_debug_lines, line);
|
||||
}
|
||||
if (ruby_coverage && !NIL_P(line)) {
|
||||
rb_ary_push(ruby_coverage, Qnil);
|
||||
}
|
||||
#endif
|
||||
return line;
|
||||
}
|
||||
|
@ -8126,14 +8165,6 @@ assign_in_cond(struct parser_params *parser, NODE *node)
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
e_option_supplied(struct parser_params *parser)
|
||||
{
|
||||
if (strcmp(ruby_sourcefile, "-e") == 0)
|
||||
return Qtrue;
|
||||
return Qfalse;
|
||||
}
|
||||
|
||||
static void
|
||||
warn_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
|
||||
{
|
||||
|
|
27
thread.c
27
thread.c
|
@ -2099,6 +2099,31 @@ rb_thread_start_timer_thread(void)
|
|||
rb_thread_create_timer_thread();
|
||||
}
|
||||
|
||||
static int
|
||||
clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
|
||||
{
|
||||
int i;
|
||||
VALUE lines = (VALUE)val;
|
||||
|
||||
for (i = 0; i < RARRAY_LEN(lines); i++) {
|
||||
if (RARRAY_PTR(lines)[i] != Qnil) {
|
||||
RARRAY_PTR(lines)[i] = INT2FIX(0);
|
||||
}
|
||||
}
|
||||
return ST_CONTINUE;
|
||||
}
|
||||
|
||||
static void
|
||||
clear_coverage(void)
|
||||
{
|
||||
if (rb_const_defined_at(rb_cObject, rb_intern("COVERAGE__"))) {
|
||||
VALUE hash = rb_const_get_at(rb_cObject, rb_intern("COVERAGE__"));
|
||||
if (TYPE(hash) == T_HASH) {
|
||||
st_foreach(RHASH_TBL(hash), clear_coverage_i, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
terminate_atfork_i(st_data_t key, st_data_t val, rb_thread_t *current_th)
|
||||
{
|
||||
|
@ -2124,6 +2149,7 @@ rb_thread_atfork(void)
|
|||
st_clear(vm->living_threads);
|
||||
st_insert(vm->living_threads, thval, (st_data_t) th->thread_id);
|
||||
vm->sleeper = 0;
|
||||
clear_coverage();
|
||||
rb_reset_random_seed();
|
||||
}
|
||||
|
||||
|
@ -2152,6 +2178,7 @@ rb_thread_atfork_before_exec(void)
|
|||
st_clear(vm->living_threads);
|
||||
st_insert(vm->living_threads, thval, (st_data_t) th->thread_id);
|
||||
vm->sleeper = 0;
|
||||
clear_coverage();
|
||||
}
|
||||
|
||||
struct thgroup {
|
||||
|
|
|
@ -201,6 +201,7 @@ struct rb_iseq_struct {
|
|||
VALUE *iseq_encoded; /* encoded iseq */
|
||||
unsigned long iseq_size;
|
||||
VALUE mark_ary; /* Array: includes operands which should be GC marked */
|
||||
VALUE coverage; /* coverage array */
|
||||
|
||||
/* insn info, must be freed */
|
||||
struct iseq_insn_info_entry *insn_info_table;
|
||||
|
@ -448,6 +449,7 @@ struct rb_thread_struct
|
|||
struct rb_vm_trap_tag *trap_tag;
|
||||
|
||||
int parse_in_eval;
|
||||
int mild_compile_error;
|
||||
|
||||
/* storage */
|
||||
st_table *local_storage;
|
||||
|
|
|
@ -667,6 +667,7 @@ eval_string_with_cref(VALUE self, VALUE src, VALUE scope, NODE *cref, const char
|
|||
rb_env_t *env = NULL;
|
||||
rb_block_t block;
|
||||
volatile int parse_in_eval;
|
||||
volatile int mild_compile_error;
|
||||
|
||||
if (file == 0) {
|
||||
file = rb_sourcefile();
|
||||
|
@ -674,6 +675,7 @@ eval_string_with_cref(VALUE self, VALUE src, VALUE scope, NODE *cref, const char
|
|||
}
|
||||
|
||||
parse_in_eval = th->parse_in_eval;
|
||||
mild_compile_error = th->mild_compile_error;
|
||||
PUSH_TAG();
|
||||
if ((state = EXEC_TAG()) == 0) {
|
||||
rb_iseq_t *iseq;
|
||||
|
@ -708,7 +710,9 @@ eval_string_with_cref(VALUE self, VALUE src, VALUE scope, NODE *cref, const char
|
|||
|
||||
/* make eval iseq */
|
||||
th->parse_in_eval++;
|
||||
th->mild_compile_error++;
|
||||
iseqval = rb_iseq_compile(src, rb_str_new2(file), INT2FIX(line));
|
||||
th->mild_compile_error--;
|
||||
th->parse_in_eval--;
|
||||
|
||||
vm_set_eval_stack(th, iseqval, cref);
|
||||
|
@ -730,6 +734,7 @@ eval_string_with_cref(VALUE self, VALUE src, VALUE scope, NODE *cref, const char
|
|||
result = vm_eval_body(th);
|
||||
}
|
||||
POP_TAG();
|
||||
th->mild_compile_error = mild_compile_error;
|
||||
th->parse_in_eval = parse_in_eval;
|
||||
|
||||
if (state) {
|
||||
|
|
Loading…
Add table
Reference in a new issue