Ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a25176072e02db9254f0e0c84c805cd)
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
15static int current_mode;
16static VALUE me2counter = Qnil;
17
18/*
19 * call-seq:
20 * Coverage.start => nil
21 *
22 * Enables coverage measurement.
23 */
24static VALUE
25rb_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"))) {
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")))))
44 if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("branches")))))
46 if (RTEST(rb_hash_lookup(opt, ID2SYM(rb_intern("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");
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
77static VALUE
78branch_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
109static int
110method_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) {
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
168static int
169coverage_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 */
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 */
219static VALUE
220rb_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
238static int
239clear_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 */
253static VALUE
254rb_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) {
275 if (!NIL_P(me2counter)) rb_hash_foreach(me2counter, clear_me2counter_i, Qnil);
276 }
277 if (stop) {
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 */
292static VALUE
293rb_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 */
332void
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}
void Init_coverage(void)
Definition: coverage.c:333
VALUE rb_define_module(const char *)
Definition: class.c:785
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2671
void rb_bug(const char *fmt,...)
Definition: error.c:636
VALUE rb_eRuntimeError
Definition: error.c:922
void rb_warn(const char *fmt,...)
Definition: error.c:315
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
Definition: object.c:2900
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition: object.c:78
#define RARRAY_LEN(a)
VALUE rb_hash_lookup(VALUE, VALUE)
Definition: hash.c:2063
void rb_hash_foreach(VALUE, int(*)(VALUE, VALUE, VALUE), VALUE)
VALUE rb_get_coverages(void)
Definition: thread.c:5476
void rb_objspace_each_objects(int(*callback)(void *start, void *end, size_t stride, void *data), void *data)
#define RTEST(v)
unsigned long st_data_t
VALUE rb_hash_aref(VALUE, VALUE)
Definition: hash.c:2037
#define LONG2FIX(i)
void rb_clear_coverages(void)
Definition: thread.c:4467
#define COVERAGE_TARGET_METHODS
#define T_IMEMO
VALUE rb_ident_hash_new(void)
Definition: hash.c:4278
#define NIL_P(v)
const rb_callable_method_entry_t * me
#define ID2SYM(x)
unsigned long VALUE
uint32_t i
void rb_global_variable(VALUE *)
Definition: gc.c:7128
void rb_define_module_function(VALUE, const char *, VALUE(*)(), int)
#define T_ICLASS
#define T_HASH
#define COVERAGE_TARGET_BRANCHES
VALUE rb_hash_freeze(VALUE)
Definition: hash.c:87
#define FIX2INT(x)
int VALUE v
#define rb_scan_args(argc, argvp, fmt,...)
#define rb_intern(str)
const rb_method_entry_t * rb_resolve_me_location(const rb_method_entry_t *, VALUE[5])
Definition: thread.c:5399
VALUE rb_ary_freeze(VALUE)
Definition: array.c:648
#define COVERAGE_INDEX_BRANCHES
#define COVERAGE_TARGET_ONESHOT_LINES
#define Qtrue
#define FIXNUM_MAX
#define POSFIXABLE(f)
VALUE rb_ary_dup(VALUE)
Definition: array.c:2238
#define Qnil
#define Qfalse
#define RB_TYPE_P(obj, type)
#define INT2FIX(i)
void rb_set_coverages(VALUE, int, VALUE)
Definition: thread.c:5488
const VALUE * argv
#define COVERAGE_TARGET_LINES
VALUE rb_hash_aset(VALUE, VALUE, VALUE)
Definition: hash.c:2852
#define COVERAGE_INDEX_LINES
#define RHASH_TBL(h)
size_t st_index_t h
#define rb_ary_new_from_args(n,...)
#define FIX2LONG(x)
#define RARRAY_AREF(a, i)
VALUE rb_hash_new(void)
Definition: hash.c:1523
void rb_reset_coverages(void)
Definition: thread.c:5503
int st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg)
Definition: st.c:1717
struct rb_method_definition_struct *const def
const VALUE owner