Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

coverage.c
Go to the documentation of this file.
1 /************************************************
2
3 coverage.c -
4
5 $Author: $
6
7 Copyright (c) 2008 Yusuke Endoh
8
9 ************************************************/
10
11 #include "ruby.h"
12 #include "vm_core.h"
13 #include "gc.h"
14
15 static int current_mode;
16 static VALUE me2counter = Qnil;
17
18 /*
19 * call-seq:
20 * Coverage.start => nil
21 *
22 * Enables coverage measurement.
23 */
24 static VALUE
25 rb_coverage_start(int argc, VALUE *argv, VALUE klass)
26 {
27 VALUE coverages, opt;
28 int mode;
29
30 rb_scan_args(argc, argv, "01", &opt);
31
32 if (argc == 0) {
33 mode = 0; /* compatible mode */
34 }
35 else if (opt == ID2SYM(rb_intern("all"))) {
36 mode = COVERAGE_TARGET_LINES | COVERAGE_TARGET_BRANCHES | COVERAGE_TARGET_METHODS;
37 }
38 else {
39 mode = 0;
40 opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
41
42 if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("lines")))))
43 mode |= COVERAGE_TARGET_LINES;
44 if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("branches")))))
45 mode |= COVERAGE_TARGET_BRANCHES;
46 if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("methods")))))
47 mode |= COVERAGE_TARGET_METHODS;
48 if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("oneshot_lines"))))) {
49 if (mode & COVERAGE_TARGET_LINES)
50 rb_raise(rb_eRuntimeError, "cannot enable lines and oneshot_lines simultaneously");
51 mode |= COVERAGE_TARGET_LINES;
52 mode |= COVERAGE_TARGET_ONESHOT_LINES;
53 }
54 }
55
56 if (mode & COVERAGE_TARGET_METHODS) {
57 me2counter = rb_ident_hash_new();
58 }
59 else {
60 me2counter = Qnil;
61 }
62
63 coverages = rb_get_coverages();
64 if (!RTEST(coverages)) {
65 coverages = rb_hash_new();
66 rb_obj_hide(coverages);
67 current_mode = mode;
68 if (mode == 0) mode = COVERAGE_TARGET_LINES;
69 rb_set_coverages(coverages, mode, me2counter);
70 }
71 else if (current_mode != mode) {
72 rb_raise(rb_eRuntimeError, "cannot change the measuring target during coverage measurement");
73 }
74 return Qnil;
75 }
76
77 static VALUE
78 branch_coverage(VALUE branches)
79 {
80 VALUE ret = rb_hash_new();
81 VALUE structure = rb_ary_dup(RARRAY_AREF(branches, 0));
82 VALUE counters = rb_ary_dup(RARRAY_AREF(branches, 1));
83 int i, j;
84 long id = 0;
85
86 for (i = 0; i < RARRAY_LEN(structure); i++) {
87 VALUE branches = RARRAY_AREF(structure, i);
88 VALUE base_type = RARRAY_AREF(branches, 0);
89 VALUE base_first_lineno = RARRAY_AREF(branches, 1);
90 VALUE base_first_column = RARRAY_AREF(branches, 2);
91 VALUE base_last_lineno = RARRAY_AREF(branches, 3);
92 VALUE base_last_column = RARRAY_AREF(branches, 4);
93 VALUE children = rb_hash_new();
94 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);
95 for (j = 5; j < RARRAY_LEN(branches); j += 6) {
96 VALUE target_label = RARRAY_AREF(branches, j);
97 VALUE target_first_lineno = RARRAY_AREF(branches, j + 1);
98 VALUE target_first_column = RARRAY_AREF(branches, j + 2);
99 VALUE target_last_lineno = RARRAY_AREF(branches, j + 3);
100 VALUE target_last_column = RARRAY_AREF(branches, j + 4);
101 int idx = FIX2INT(RARRAY_AREF(branches, j + 5));
102 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));
103 }
104 }
105
106 return ret;
107 }
108
109 static int
110 method_coverage_i(void *vstart, void *vend, size_t stride, void *data)
111 {
112 /*
113 * ObjectSpace.each_object(Module){|mod|
114 * mod.instance_methods.each{|mid|
115 * m = mod.instance_method(mid)
116 * if loc = m.source_location
117 * p [m.name, loc, $g_method_cov_counts[m]]
118 * end
119 * }
120 * }
121 */
122 VALUE ncoverages = *(VALUE*)data, v;
123
124 for (v = (VALUE)vstart; v != (VALUE)vend; v += stride) {
125 if (RB_TYPE_P(v, T_IMEMO) && imemo_type(v) == imemo_ment) {
126 const rb_method_entry_t *me = (rb_method_entry_t *) v;
127 VALUE path, first_lineno, first_column, last_lineno, last_column;
128 VALUE data[5], ncoverage, methods;
129 VALUE methods_id = ID2SYM(rb_intern("methods"));
130 VALUE klass;
131 const rb_method_entry_t *me2 = rb_resolve_me_location(me, data);
132 if (me != me2) continue;
133 klass = me->owner;
134 if (RB_TYPE_P(klass, T_ICLASS)) {
135 rb_bug("T_ICLASS");
136 }
137 path = data[0];
138 first_lineno = data[1];
139 first_column = data[2];
140 last_lineno = data[3];
141 last_column = data[4];
142 if (FIX2LONG(first_lineno) <= 0) continue;
143 ncoverage = rb_hash_aref(ncoverages, path);
144 if (NIL_P(ncoverage)) continue;
145 methods = rb_hash_aref(ncoverage, methods_id);
146
147 {
148 VALUE method_id = ID2SYM(me->def->original_id);
149 VALUE rcount = rb_hash_aref(me2counter, (VALUE) me);
150 VALUE key = rb_ary_new_from_args(6, klass, method_id, first_lineno, first_column, last_lineno, last_column);
151 VALUE rcount2 = rb_hash_aref(methods, key);
152
153 if (NIL_P(rcount)) rcount = LONG2FIX(0);
154 if (NIL_P(rcount2)) rcount2 = LONG2FIX(0);
155 if (!POSFIXABLE(FIX2LONG(rcount) + FIX2LONG(rcount2))) {
156 rcount = LONG2FIX(FIXNUM_MAX);
157 }
158 else {
159 rcount = LONG2FIX(FIX2LONG(rcount) + FIX2LONG(rcount2));
160 }
161 rb_hash_aset(methods, key, rcount);
162 }
163 }
164 }
165 return 0;
166 }
167
168 static int
169 coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
170 {
171 VALUE path = (VALUE)key;
172 VALUE coverage = (VALUE)val;
173 VALUE coverages = (VALUE)h;
174 if (current_mode == 0) {
175 /* compatible mode */
176 VALUE lines = rb_ary_dup(RARRAY_AREF(coverage, COVERAGE_INDEX_LINES));
177 rb_ary_freeze(lines);
178 coverage = lines;
179 }
180 else {
181 VALUE h = rb_hash_new();
182
183 if (current_mode & COVERAGE_TARGET_LINES) {
184 VALUE lines = RARRAY_AREF(coverage, COVERAGE_INDEX_LINES);
185 const char *kw = (current_mode & COVERAGE_TARGET_ONESHOT_LINES) ? "oneshot_lines" : "lines";
186 lines = rb_ary_dup(lines);
187 rb_ary_freeze(lines);
188 rb_hash_aset(h, ID2SYM(rb_intern(kw)), lines);
189 }
190
191 if (current_mode & COVERAGE_TARGET_BRANCHES) {
192 VALUE branches = RARRAY_AREF(coverage, COVERAGE_INDEX_BRANCHES);
193 rb_hash_aset(h, ID2SYM(rb_intern("branches")), branch_coverage(branches));
194 }
195
196 if (current_mode & COVERAGE_TARGET_METHODS) {
197 rb_hash_aset(h, ID2SYM(rb_intern("methods")), rb_hash_new());
198 }
199
200 coverage = h;
201 }
202
203 rb_hash_aset(coverages, path, coverage);
204 return ST_CONTINUE;
205 }
206
207 /*
208 * call-seq:
209 * Coverage.peek_result => hash
210 *
211 * Returns a hash that contains filename as key and coverage array as value.
212 * This is the same as `Coverage.result(stop: false, clear: false)`.
213 *
214 * {
215 * "file.rb" => [1, 2, nil],
216 * ...
217 * }
218 */
219 static VALUE
220 rb_coverage_peek_result(VALUE klass)
221 {
222 VALUE coverages = rb_get_coverages();
223 VALUE ncoverages = rb_hash_new();
224 if (!RTEST(coverages)) {
225 rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
226 }
227 st_foreach(RHASH_TBL(coverages), coverage_peek_result_i, ncoverages);
228
229 if (current_mode & COVERAGE_TARGET_METHODS) {
230 rb_objspace_each_objects(method_coverage_i, &ncoverages);
231 }
232
233 rb_hash_freeze(ncoverages);
234 return ncoverages;
235 }
236
237
238 static int
239 clear_me2counter_i(VALUE key, VALUE value, VALUE unused)
240 {
241 rb_hash_aset(me2counter, key, INT2FIX(0));
242 return ST_CONTINUE;
243 }
244
245 /*
246 * call-seq:
247 * Coverage.result(stop: true, clear: true) => hash
248 *
249 * Returns a hash that contains filename as key and coverage array as value.
250 * If +clear+ is true, it clears the counters to zero.
251 * If +stop+ is true, it disables coverage measurement.
252 */
253 static VALUE
254 rb_coverage_result(int argc, VALUE *argv, VALUE klass)
255 {
256 VALUE ncoverages;
257 VALUE opt;
258 int stop = 1, clear = 1;
259
260 rb_scan_args(argc, argv, "01", &opt);
261
262 if (argc == 1) {
263 opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
264 stop = RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("stop"))));
265 clear = RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("clear"))));
266 }
267
268 ncoverages = rb_coverage_peek_result(klass);
269 if (stop && !clear) {
270 rb_warn("stop implies clear");
271 clear = 1;
272 }
273 if (clear) {
274 rb_clear_coverages();
275 if (!NIL_P(me2counter)) rb_hash_foreach(me2counter, clear_me2counter_i, Qnil);
276 }
277 if (stop) {
278 rb_reset_coverages();
279 me2counter = Qnil;
280 }
281 return ncoverages;
282 }
283
284
285 /*
286 * call-seq:
287 * Coverage.running? => bool
288 *
289 * Returns true if coverage stats are currently being collected (after
290 * Coverage.start call, but before Coverage.result call)
291 */
292 static VALUE
293 rb_coverage_running(VALUE klass)
294 {
295 VALUE coverages = rb_get_coverages();
296 return RTEST(coverages) ? Qtrue : Qfalse;
297 }
298
299 /* Coverage provides coverage measurement feature for Ruby.
300 * This feature is experimental, so these APIs may be changed in future.
301 *
302 * = Usage
303 *
304 * 1. require "coverage"
305 * 2. do Coverage.start
306 * 3. require or load Ruby source file
307 * 4. Coverage.result will return a hash that contains filename as key and
308 * coverage array as value. A coverage array gives, for each line, the
309 * number of line execution by the interpreter. A +nil+ value means
310 * coverage is disabled for this line (lines like +else+ and +end+).
311 *
312 * = Example
313 *
314 * [foo.rb]
315 * s = 0
316 * 10.times do |x|
317 * s += x
318 * end
319 *
320 * if s == 45
321 * p :ok
322 * else
323 * p :ng
324 * end
325 * [EOF]
326 *
327 * require "coverage"
328 * Coverage.start
329 * require "foo.rb"
330 * p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
331 */
332 void
333 Init_coverage(void)
334 {
335 VALUE rb_mCoverage = rb_define_module("Coverage");
336 rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, -1);
337 rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, -1);
338 rb_define_module_function(rb_mCoverage, "peek_result", rb_coverage_peek_result, 0);
339 rb_define_module_function(rb_mCoverage, "running?", rb_coverage_running, 0);
340 rb_global_variable(&me2counter);
341 }
rb_obj_hide
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition: object.c:78
FIX2INT
#define FIX2INT(x)
Definition: ruby.h:717
rb_ident_hash_new
VALUE rb_ident_hash_new(void)
Definition: hash.c:4203
rb_hash_new
VALUE rb_hash_new(void)
Definition: hash.c:1501
path
VALUE path
Definition: rb_mjit_min_header-2.7.0.h:7351
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:313
gc.h
rb_ary_new_from_args
#define rb_ary_new_from_args(n,...)
Definition: rb_mjit_min_header-2.7.0.h:7214
rb_scan_args
#define rb_scan_args(argc, argvp, fmt,...)
Definition: rb_mjit_min_header-2.7.0.h:6372
rb_clear_coverages
void rb_clear_coverages(void)
Definition: thread.c:4468
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:263
rb_callable_method_entry_struct::owner
const VALUE owner
Definition: method.h:64
rb_hash_aref
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:1964
COVERAGE_INDEX_BRANCHES
#define COVERAGE_INDEX_BRANCHES
Definition: internal.h:2212
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_intern
#define rb_intern(str)
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:560
rb_reset_coverages
void rb_reset_coverages(void)
Definition: thread.c:5504
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:772
Qfalse
#define Qfalse
Definition: ruby.h:467
FIX2LONG
#define FIX2LONG(x)
Definition: ruby.h:394
ID2SYM
#define ID2SYM(x)
Definition: ruby.h:414
rb_set_coverages
void rb_set_coverages(VALUE coverages, int mode, VALUE me2counter)
Definition: thread.c:5489
v
int VALUE v
Definition: rb_mjit_min_header-2.7.0.h:12332
rb_resolve_me_location
const RUBY_SYMBOL_EXPORT_BEGIN rb_method_entry_t * rb_resolve_me_location(const rb_method_entry_t *, VALUE[5])
Definition: thread.c:5400
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
T_ICLASS
#define T_ICLASS
Definition: ruby.h:525
imemo_type
imemo_type
Definition: internal.h:1132
POSFIXABLE
#define POSFIXABLE(f)
Definition: ruby.h:397
COVERAGE_TARGET_BRANCHES
#define COVERAGE_TARGET_BRANCHES
Definition: internal.h:2214
COVERAGE_TARGET_ONESHOT_LINES
#define COVERAGE_TARGET_ONESHOT_LINES
Definition: internal.h:2216
i
uint32_t i
Definition: rb_mjit_min_header-2.7.0.h:5464
rb_hash_lookup
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:1990
ruby.h
vm_core.h
COVERAGE_TARGET_METHODS
#define COVERAGE_TARGET_METHODS
Definition: internal.h:2215
rb_get_coverages
VALUE rb_get_coverages(void)
Definition: thread.c:5477
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:920
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition: ruby.h:1101
COVERAGE_INDEX_LINES
#define COVERAGE_INDEX_LINES
Definition: internal.h:2211
rb_hash_foreach
void rb_hash_foreach(VALUE hash, rb_foreach_func *func, VALUE farg)
Definition: hash.c:1461
me
const rb_callable_method_entry_t * me
Definition: rb_mjit_min_header-2.7.0.h:13226
key
key
Definition: openssl_missing.h:181
T_HASH
#define T_HASH
Definition: ruby.h:531
rb_global_variable
void rb_global_variable(VALUE *var)
Definition: gc.c:7112
Init_coverage
void Init_coverage(void)
Definition: coverage.c:333
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:1070
st_foreach
int st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg)
Definition: st.c:1718
imemo_ment
@ imemo_ment
Definition: internal.h:1139
rb_define_module_function
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1771
rb_objspace_each_objects
void rb_objspace_each_objects(each_obj_callback *callback, void *data)
Definition: gc.c:3025
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:634
argv
char ** argv
Definition: ruby.c:223
ST_CONTINUE
@ ST_CONTINUE
Definition: st.h:99
klass
VALUE klass
Definition: rb_mjit_min_header-2.7.0.h:13254
st_data_t
unsigned long st_data_t
Definition: rb_mjit_min_header-2.7.0.h:5363
rb_convert_type
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
Definition: object.c:2900
rb_hash_aset
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:2779
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
argc
int argc
Definition: ruby.c:222
T_IMEMO
#define T_IMEMO
Definition: ruby.h:543
rb_ary_freeze
VALUE rb_ary_freeze(VALUE ary)
Definition: array.c:648
rb_method_definition_struct::original_id
ID original_id
Definition: method.h:179
FIXNUM_MAX
#define FIXNUM_MAX
Definition: ruby.h:259
rb_callable_method_entry_struct::def
struct rb_method_definition_struct *const def
Definition: method.h:62
Qtrue
#define Qtrue
Definition: ruby.h:468
rb_method_entry_struct
Definition: method.h:51
rb_ary_dup
VALUE rb_ary_dup(VALUE ary)
Definition: array.c:2238
LONG2FIX
#define LONG2FIX(i)
Definition: ruby.h:265
RHASH_TBL
#define RHASH_TBL(h)
Definition: ruby.h:1127
COVERAGE_TARGET_LINES
#define COVERAGE_TARGET_LINES
Definition: internal.h:2213
Qnil
#define Qnil
Definition: ruby.h:469
h
size_t st_index_t h
Definition: rb_mjit_min_header-2.7.0.h:5462
rb_hash_freeze
VALUE rb_hash_freeze(VALUE hash)
Definition: hash.c:87
RTEST
#define RTEST(v)
Definition: ruby.h:481