relevance-rcov 0.8.3.2 → 0.8.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,28 +4,20 @@
4
4
  #include <st.h>
5
5
  #include <stdlib.h>
6
6
 
7
-
8
7
  static char callsite_hook_set_p;
9
8
 
10
9
  typedef struct {
11
- char *sourcefile;
12
- unsigned int sourceline;
13
- VALUE curr_meth;
10
+ char *sourcefile;
11
+ unsigned int sourceline;
12
+ VALUE curr_meth;
14
13
  } type_def_site;
14
+
15
15
  static VALUE caller_info = 0;
16
16
  static VALUE method_def_site_info = 0;
17
17
 
18
18
  static caller_stack_len = 1;
19
19
 
20
- /*
21
- *
22
- * callsite hook and associated functions
23
- *
24
- * */
25
-
26
- static VALUE
27
- record_callsite_info(VALUE args)
28
- {
20
+ static VALUE record_callsite_info(VALUE args) {
29
21
  VALUE caller_ary;
30
22
  VALUE curr_meth;
31
23
  VALUE count_hash;
@@ -35,15 +27,19 @@ record_callsite_info(VALUE args)
35
27
  caller_ary = pargs[0];
36
28
  curr_meth = pargs[1];
37
29
  count_hash = rb_hash_aref(caller_info, curr_meth);
30
+
38
31
  if(TYPE(count_hash) != T_HASH) {
39
- /* Qnil, anything else should be impossible unless somebody's been
40
- * messing with ObjectSpace */
41
- count_hash = rb_hash_new();
42
- rb_hash_aset(caller_info, curr_meth, count_hash);
32
+ /* Qnil, anything else should be impossible unless somebody's been
33
+ * messing with ObjectSpace */
34
+ count_hash = rb_hash_new();
35
+ rb_hash_aset(caller_info, curr_meth, count_hash);
43
36
  }
37
+
44
38
  count = rb_hash_aref(count_hash, caller_ary);
39
+
45
40
  if(count == Qnil)
46
41
  count = INT2FIX(0);
42
+
47
43
  count = INT2FIX(FIX2UINT(count) + 1);
48
44
  rb_hash_aset(count_hash, caller_ary, count);
49
45
  /*
@@ -54,16 +50,13 @@ record_callsite_info(VALUE args)
54
50
  return Qnil;
55
51
  }
56
52
 
57
-
58
- static VALUE
59
- record_method_def_site(VALUE args)
60
- {
53
+ static VALUE record_method_def_site(VALUE args) {
61
54
  type_def_site *pargs = (type_def_site *)args;
62
55
  VALUE def_site_info;
63
56
  VALUE hash;
64
57
 
65
- if( RTEST(rb_hash_aref(method_def_site_info, pargs->curr_meth)) )
66
- return Qnil;
58
+ if(RTEST(rb_hash_aref(method_def_site_info, pargs->curr_meth)))
59
+ return Qnil;
67
60
  def_site_info = rb_ary_new();
68
61
  rb_ary_push(def_site_info, rb_str_new2(pargs->sourcefile));
69
62
  rb_ary_push(def_site_info, INT2NUM(pargs->sourceline+1));
@@ -76,9 +69,7 @@ record_method_def_site(VALUE args)
76
69
  return Qnil;
77
70
  }
78
71
 
79
- static VALUE
80
- callsite_custom_backtrace(int lev)
81
- {
72
+ static VALUE callsite_custom_backtrace(int lev) {
82
73
  struct FRAME *frame = ruby_frame;
83
74
  VALUE ary;
84
75
  NODE *n;
@@ -86,103 +77,96 @@ callsite_custom_backtrace(int lev)
86
77
  VALUE klass;
87
78
 
88
79
  ary = rb_ary_new();
80
+
89
81
  if (frame->last_func == ID_ALLOCATOR) {
90
- frame = frame->prev;
82
+ frame = frame->prev;
91
83
  }
84
+
92
85
  for (; frame && (n = frame->node); frame = frame->prev) {
93
- if (frame->prev && frame->prev->last_func) {
94
- if (frame->prev->node == n) continue;
95
- level = rb_ary_new();
96
- klass = frame->prev->last_class ? frame->prev->last_class : Qnil;
97
- if(TYPE(klass) == T_ICLASS) {
98
- klass = CLASS_OF(klass);
99
- }
100
- rb_ary_push(level, klass);
101
- rb_ary_push(level, ID2SYM(frame->prev->last_func));
102
- rb_ary_push(level, rb_str_new2(n->nd_file));
103
- rb_ary_push(level, INT2NUM(nd_line(n)));
104
- }
105
- else {
106
- level = rb_ary_new();
107
- rb_ary_push(level, Qnil);
108
- rb_ary_push(level, Qnil);
109
- rb_ary_push(level, rb_str_new2(n->nd_file));
110
- rb_ary_push(level, INT2NUM(nd_line(n)));
111
- }
112
- rb_ary_push(ary, level);
113
- if(--lev == 0)
114
- break;
86
+ if (frame->prev && frame->prev->last_func) {
87
+ if (frame->prev->node == n) continue;
88
+ level = rb_ary_new();
89
+ klass = frame->prev->last_class ? frame->prev->last_class : Qnil;
90
+ if(TYPE(klass) == T_ICLASS) {
91
+ klass = CLASS_OF(klass);
92
+ }
93
+ rb_ary_push(level, klass);
94
+ rb_ary_push(level, ID2SYM(frame->prev->last_func));
95
+ rb_ary_push(level, rb_str_new2(n->nd_file));
96
+ rb_ary_push(level, INT2NUM(nd_line(n)));
97
+ }
98
+ else {
99
+ level = rb_ary_new();
100
+ rb_ary_push(level, Qnil);
101
+ rb_ary_push(level, Qnil);
102
+ rb_ary_push(level, rb_str_new2(n->nd_file));
103
+ rb_ary_push(level, INT2NUM(nd_line(n)));
104
+ }
105
+ rb_ary_push(ary, level);
106
+ if(--lev == 0)
107
+ break;
115
108
  }
116
-
109
+
117
110
  return ary;
118
111
  }
119
112
 
120
- static void
121
- coverage_event_callsite_hook(rb_event_t event, NODE *node, VALUE self,
122
- ID mid, VALUE klass)
123
- {
124
- VALUE caller_ary;
125
- VALUE curr_meth;
126
- VALUE args[2];
127
- int status;
128
-
129
- caller_ary = callsite_custom_backtrace(caller_stack_len);
130
-
131
- if(TYPE(klass) == T_ICLASS) {
132
- klass = CLASS_OF(klass);
133
- }
134
- curr_meth = rb_ary_new();
135
- rb_ary_push(curr_meth, klass);
136
- rb_ary_push(curr_meth, ID2SYM(mid));
137
-
138
- args[0] = caller_ary;
139
- args[1] = curr_meth;
140
- rb_protect(record_callsite_info, (VALUE)args, &status);
141
- if(!status && node) {
142
- type_def_site args;
143
-
144
- args.sourcefile = node->nd_file;
145
- args.sourceline = nd_line(node) - 1;
146
- args.curr_meth = curr_meth;
147
- rb_protect(record_method_def_site, (VALUE)&args, NULL);
148
- }
149
- if(status)
150
- rb_gv_set("$!", Qnil);
151
- }
113
+ static void coverage_event_callsite_hook(rb_event_t event, NODE *node, VALUE self, ID mid, VALUE klass) {
114
+ VALUE caller_ary;
115
+ VALUE curr_meth;
116
+ VALUE args[2];
117
+ int status;
152
118
 
119
+ caller_ary = callsite_custom_backtrace(caller_stack_len);
120
+
121
+ if(TYPE(klass) == T_ICLASS) {
122
+ klass = CLASS_OF(klass);
123
+ }
153
124
 
154
- static VALUE
155
- cov_install_callsite_hook(VALUE self)
156
- {
157
- if(!callsite_hook_set_p) {
158
- if(TYPE(caller_info) != T_HASH)
159
- caller_info = rb_hash_new();
160
- callsite_hook_set_p = 1;
161
- rb_add_event_hook(coverage_event_callsite_hook,
162
- RUBY_EVENT_CALL);
163
-
164
- return Qtrue;
165
- } else
166
- return Qfalse;
167
- }
125
+ curr_meth = rb_ary_new();
126
+ rb_ary_push(curr_meth, klass);
127
+ rb_ary_push(curr_meth, ID2SYM(mid));
128
+
129
+ args[0] = caller_ary;
130
+ args[1] = curr_meth;
131
+ rb_protect(record_callsite_info, (VALUE)args, &status);
132
+
133
+ if(!status && node) {
134
+ type_def_site args;
135
+
136
+ args.sourcefile = node->nd_file;
137
+ args.sourceline = nd_line(node) - 1;
138
+ args.curr_meth = curr_meth;
139
+ rb_protect(record_method_def_site, (VALUE)&args, NULL);
140
+ }
168
141
 
142
+ if(status)
143
+ rb_gv_set("$!", Qnil);
144
+ }
169
145
 
170
- static VALUE
171
- cov_remove_callsite_hook(VALUE self)
172
- {
173
- if(!callsite_hook_set_p)
174
- return Qfalse;
175
- else {
176
- rb_remove_event_hook(coverage_event_callsite_hook);
177
- callsite_hook_set_p = 0;
178
- return Qtrue;
179
- }
146
+ static VALUE cov_install_callsite_hook(VALUE self) {
147
+ if(!callsite_hook_set_p) {
148
+ if(TYPE(caller_info) != T_HASH)
149
+ caller_info = rb_hash_new();
150
+ callsite_hook_set_p = 1;
151
+ rb_add_event_hook(coverage_event_callsite_hook, RUBY_EVENT_CALL);
152
+
153
+ return Qtrue;
154
+ }
155
+ else
156
+ return Qfalse;
180
157
  }
181
158
 
159
+ static VALUE cov_remove_callsite_hook(VALUE self) {
160
+ if(!callsite_hook_set_p)
161
+ return Qfalse;
162
+ else {
163
+ rb_remove_event_hook(coverage_event_callsite_hook);
164
+ callsite_hook_set_p = 0;
165
+ return Qtrue;
166
+ }
167
+ }
182
168
 
183
- static VALUE
184
- cov_generate_callsite_info(VALUE self)
185
- {
169
+ static VALUE cov_generate_callsite_info(VALUE self) {
186
170
  VALUE ret;
187
171
 
188
172
  ret = rb_ary_new();
@@ -191,14 +175,10 @@ cov_generate_callsite_info(VALUE self)
191
175
  return ret;
192
176
  }
193
177
 
194
-
195
- static VALUE
196
- cov_reset_callsite(VALUE self)
197
- {
178
+ static VALUE cov_reset_callsite(VALUE self) {
198
179
  if(callsite_hook_set_p) {
199
- rb_raise(rb_eRuntimeError,
200
- "Cannot reset the callsite info in the middle of a traced run.");
201
- return Qnil;
180
+ rb_raise(rb_eRuntimeError, "Cannot reset the callsite info in the middle of a traced run.");
181
+ return Qnil;
202
182
  }
203
183
 
204
184
  caller_info = rb_hash_new();
@@ -206,37 +186,31 @@ cov_reset_callsite(VALUE self)
206
186
  return Qnil;
207
187
  }
208
188
 
209
- void
210
- Init_rcov_callsite()
211
- {
212
- VALUE mRcov;
213
- VALUE mRCOV__;
214
- ID id_rcov = rb_intern("Rcov");
215
- ID id_coverage__ = rb_intern("RCOV__");
216
- ID id_script_lines__ = rb_intern("SCRIPT_LINES__");
217
-
218
- if(rb_const_defined(rb_cObject, id_rcov))
219
- mRcov = rb_const_get(rb_cObject, id_rcov);
220
- else
221
- mRcov = rb_define_module("Rcov");
222
-
223
- if(rb_const_defined(mRcov, id_coverage__))
224
- mRCOV__ = rb_const_get_at(mRcov, id_coverage__);
225
- else
226
- mRCOV__ = rb_define_module_under(mRcov, "RCOV__");
227
-
228
- callsite_hook_set_p = 0;
229
- caller_info = rb_hash_new();
230
- method_def_site_info = rb_hash_new();
231
- rb_gc_register_address(&caller_info);
232
- rb_gc_register_address(&method_def_site_info);
189
+ void Init_rcov_callsite() {
190
+ VALUE mRcov;
191
+ VALUE mRCOV__;
192
+ ID id_rcov = rb_intern("Rcov");
193
+ ID id_coverage__ = rb_intern("RCOV__");
194
+ ID id_script_lines__ = rb_intern("SCRIPT_LINES__");
195
+
196
+ if(rb_const_defined(rb_cObject, id_rcov))
197
+ mRcov = rb_const_get(rb_cObject, id_rcov);
198
+ else
199
+ mRcov = rb_define_module("Rcov");
200
+
201
+ if(rb_const_defined(mRcov, id_coverage__))
202
+ mRCOV__ = rb_const_get_at(mRcov, id_coverage__);
203
+ else
204
+ mRCOV__ = rb_define_module_under(mRcov, "RCOV__");
205
+
206
+ callsite_hook_set_p = 0;
207
+ caller_info = rb_hash_new();
208
+ method_def_site_info = rb_hash_new();
209
+ rb_gc_register_address(&caller_info);
210
+ rb_gc_register_address(&method_def_site_info);
233
211
 
234
- rb_define_singleton_method(mRCOV__, "install_callsite_hook",
235
- cov_install_callsite_hook, 0);
236
- rb_define_singleton_method(mRCOV__, "remove_callsite_hook",
237
- cov_remove_callsite_hook, 0);
238
- rb_define_singleton_method(mRCOV__, "generate_callsite_info",
239
- cov_generate_callsite_info, 0);
240
- rb_define_singleton_method(mRCOV__, "reset_callsite", cov_reset_callsite, 0);
212
+ rb_define_singleton_method(mRCOV__, "install_callsite_hook", cov_install_callsite_hook, 0);
213
+ rb_define_singleton_method(mRCOV__, "remove_callsite_hook", cov_remove_callsite_hook, 0);
214
+ rb_define_singleton_method(mRCOV__, "generate_callsite_info", cov_generate_callsite_info, 0);
215
+ rb_define_singleton_method(mRCOV__, "reset_callsite", cov_reset_callsite, 0);
241
216
  }
242
- /* vim: set sw=8 expandtab: */
@@ -1,4 +1,3 @@
1
-
2
1
  #include <ruby.h>
3
2
  #include <env.h>
4
3
  #include <node.h>
@@ -20,249 +19,216 @@ static st_table* coverinfo = 0;
20
19
  static char coverage_hook_set_p;
21
20
 
22
21
  struct cov_array {
23
- unsigned int len;
24
- unsigned int *ptr;
22
+ unsigned int len;
23
+ unsigned int *ptr;
25
24
  };
26
25
 
27
26
  static struct cov_array *cached_array = 0;
28
27
  static char *cached_file = 0;
29
28
 
30
-
31
- /*
32
- *
33
- * coverage hook and associated functions
34
- *
35
- * */
36
-
37
- static struct cov_array *
38
- coverage_increase_counter_uncached(char *sourcefile, unsigned int sourceline,
39
- char mark_only)
40
- {
29
+ static struct cov_array * coverage_increase_counter_uncached(char *sourcefile, unsigned int sourceline, char mark_only) {
41
30
  struct cov_array *carray = NULL;
42
31
 
43
32
  if(sourcefile == NULL) {
44
- /* "can't happen", just ignore and avoid segfault */
45
- return NULL;
46
- } else if(!st_lookup(coverinfo, (st_data_t)sourcefile, (st_data_t*)&carray)) {
47
- VALUE arr;
48
-
49
- arr = rb_hash_aref(oSCRIPT_LINES__, rb_str_new2(sourcefile));
50
- if(NIL_P(arr))
51
- return 0;
52
- rb_check_type(arr, T_ARRAY);
53
- carray = calloc(1, sizeof(struct cov_array));
54
- carray->ptr = calloc(RARRAY(arr)->len, sizeof(unsigned int));
55
- carray->len = RARRAY(arr)->len;
56
- st_insert(coverinfo, (st_data_t)strdup(sourcefile),
57
- (st_data_t) carray);
58
- } else {
59
- /* recovered carray, sanity check */
60
- assert(carray && "failed to create valid carray");
33
+ /* "can't happen", just ignore and avoid segfault */
34
+ return NULL;
35
+ }
36
+ else if(!st_lookup(coverinfo, (st_data_t)sourcefile, (st_data_t*)&carray)) {
37
+ VALUE arr;
38
+
39
+ arr = rb_hash_aref(oSCRIPT_LINES__, rb_str_new2(sourcefile));
40
+ if(NIL_P(arr))
41
+ return 0;
42
+ rb_check_type(arr, T_ARRAY);
43
+ carray = calloc(1, sizeof(struct cov_array));
44
+ carray->ptr = calloc(RARRAY(arr)->len, sizeof(unsigned int));
45
+ carray->len = RARRAY(arr)->len;
46
+ st_insert(coverinfo, (st_data_t)strdup(sourcefile), (st_data_t) carray);
47
+ }
48
+ else {
49
+ /* recovered carray, sanity check */
50
+ assert(carray && "failed to create valid carray");
61
51
  }
62
52
 
63
53
  if(mark_only) {
64
- if(!carray->ptr[sourceline])
65
- carray->ptr[sourceline] = 1;
54
+ if(!carray->ptr[sourceline])
55
+ carray->ptr[sourceline] = 1;
66
56
  } else {
67
- if (carray && carray->len > sourceline) {
68
- carray->ptr[sourceline]++;
69
- }
57
+ if (carray && carray->len > sourceline) {
58
+ carray->ptr[sourceline]++;
59
+ }
70
60
  }
71
61
 
72
62
  return carray;
73
63
  }
74
64
 
75
-
76
- static void
77
- coverage_mark_caller()
78
- {
65
+ static void coverage_mark_caller() {
79
66
  struct FRAME *frame = ruby_frame;
80
67
  NODE *n;
81
68
 
82
69
  if (frame->last_func == ID_ALLOCATOR) {
83
- frame = frame->prev;
70
+ frame = frame->prev;
84
71
  }
85
72
  for (; frame && (n = frame->node); frame = frame->prev) {
86
- if (frame->prev && frame->prev->last_func) {
87
- if (frame->prev->node == n) {
88
- if (frame->prev->last_func == frame->last_func) continue;
89
- }
90
- coverage_increase_counter_uncached(n->nd_file, nd_line(n) - 1, 1);
91
- }
92
- else {
93
- coverage_increase_counter_uncached(n->nd_file, nd_line(n) - 1, 1);
94
- }
95
- break;
73
+ if (frame->prev && frame->prev->last_func) {
74
+ if (frame->prev->node == n) {
75
+ if (frame->prev->last_func == frame->last_func) continue;
76
+ }
77
+ coverage_increase_counter_uncached(n->nd_file, nd_line(n) - 1, 1);
78
+ }
79
+ else {
80
+ coverage_increase_counter_uncached(n->nd_file, nd_line(n) - 1, 1);
81
+ }
82
+ break;
96
83
  }
97
84
  }
98
85
 
99
-
100
- static void
101
- coverage_increase_counter_cached(char *sourcefile, int sourceline)
102
- {
103
- if(cached_file == sourcefile && cached_array && cached_array->len > sourceline) {
104
- cached_array->ptr[sourceline]++;
105
- return;
106
- }
107
- cached_file = sourcefile;
108
- cached_array = coverage_increase_counter_uncached(sourcefile, sourceline, 0);
86
+ static void coverage_increase_counter_cached(char *sourcefile, int sourceline) {
87
+ if(cached_file == sourcefile && cached_array && cached_array->len > sourceline) {
88
+ cached_array->ptr[sourceline]++;
89
+ return;
90
+ }
91
+ cached_file = sourcefile;
92
+ cached_array = coverage_increase_counter_uncached(sourcefile, sourceline, 0);
109
93
  }
110
94
 
111
-
112
- static void
113
- coverage_event_coverage_hook(rb_event_t event, NODE *node, VALUE self,
114
- ID mid, VALUE klass)
115
- {
116
- char *sourcefile;
117
- unsigned int sourceline;
118
- static unsigned int in_hook = 0;
95
+ static void coverage_event_coverage_hook(rb_event_t event, NODE *node, VALUE self, ID mid, VALUE klass) {
96
+ char *sourcefile;
97
+ unsigned int sourceline;
98
+ static unsigned int in_hook = 0;
119
99
 
120
- if(in_hook) {
121
- return;
122
- }
123
-
124
- in_hook++;
125
-
126
- #if COVERAGE_DEBUG_EVENTS
127
- do {
128
- int status;
129
- VALUE old_exception;
130
- old_exception = rb_gv_get("$!");
131
- rb_protect(rb_inspect, klass, &status);
132
- if(!status) {
133
- printf("EVENT: %d %s %s %s %d\n", event,
134
- klass ? RSTRING(rb_inspect(klass))->ptr : "",
135
- mid ? (mid == ID_ALLOCATOR ? "ID_ALLOCATOR" : rb_id2name(mid))
136
- : "unknown",
137
- node ? node->nd_file : "", node ? nd_line(node) : 0);
138
- } else {
139
- printf("EVENT: %d %s %s %d\n", event,
140
- mid ? (mid == ID_ALLOCATOR ? "ID_ALLOCATOR" : rb_id2name(mid))
141
- : "unknown",
142
- node ? node->nd_file : "", node ? nd_line(node) : 0);
143
- }
144
- rb_gv_set("$!", old_exception);
145
- } while (0);
146
- #endif
147
-
148
- if(event & RUBY_EVENT_C_CALL) {
149
- coverage_mark_caller();
150
- }
151
- if(event & (RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN | RUBY_EVENT_CLASS)) {
152
- in_hook--;
153
- return;
154
- }
155
-
156
- if(node == NULL) {
157
- in_hook--;
158
- return;
159
- }
160
-
161
- sourcefile = node->nd_file;
162
- sourceline = nd_line(node) - 1;
163
-
164
- coverage_increase_counter_cached(sourcefile, sourceline);
165
- if(event & RUBY_EVENT_CALL)
166
- coverage_mark_caller();
167
- in_hook--;
168
- }
100
+ if(in_hook) {
101
+ return;
102
+ }
169
103
 
104
+ in_hook++;
105
+
106
+ #if COVERAGE_DEBUG_EVENTS
107
+ do {
108
+ int status;
109
+ VALUE old_exception;
110
+ old_exception = rb_gv_get("$!");
111
+ rb_protect(rb_inspect, klass, &status);
112
+ if(!status) {
113
+ printf("EVENT: %d %s %s %s %d\n", event,
114
+ klass ? RSTRING(rb_inspect(klass))->ptr : "",
115
+ mid ? (mid == ID_ALLOCATOR ? "ID_ALLOCATOR" : rb_id2name(mid))
116
+ : "unknown",
117
+ node ? node->nd_file : "", node ? nd_line(node) : 0);
118
+ }
119
+ else {
120
+ printf("EVENT: %d %s %s %d\n", event,
121
+ mid ? (mid == ID_ALLOCATOR ? "ID_ALLOCATOR" : rb_id2name(mid))
122
+ : "unknown",
123
+ node ? node->nd_file : "", node ? nd_line(node) : 0);
124
+ }
125
+
126
+ rb_gv_set("$!", old_exception);
127
+ } while (0);
128
+ #endif
129
+
130
+ if(event & RUBY_EVENT_C_CALL) {
131
+ coverage_mark_caller();
132
+ }
133
+
134
+ if(event & (RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN | RUBY_EVENT_CLASS)) {
135
+ in_hook--;
136
+ return;
137
+ }
138
+
139
+ if(node == NULL) {
140
+ in_hook--;
141
+ return;
142
+ }
143
+
144
+ sourcefile = node->nd_file;
145
+ sourceline = nd_line(node) - 1;
146
+
147
+ coverage_increase_counter_cached(sourcefile, sourceline);
148
+
149
+ if(event & RUBY_EVENT_CALL)
150
+ coverage_mark_caller();
151
+ in_hook--;
152
+ }
170
153
 
171
- static VALUE
172
- cov_install_coverage_hook(VALUE self)
173
- {
154
+ static VALUE cov_install_coverage_hook(VALUE self) {
174
155
  if(!coverage_hook_set_p) {
175
- if(!coverinfo)
176
- coverinfo = st_init_strtable();
177
- coverage_hook_set_p = 1;
178
- /* TODO: allow C_CALL too, since it's supported already
179
- * the overhead is around ~30%, tested on typo */
180
- rb_add_event_hook(coverage_event_coverage_hook,
181
- RUBY_EVENT_ALL & ~RUBY_EVENT_C_CALL &
182
- ~RUBY_EVENT_C_RETURN & ~RUBY_EVENT_CLASS);
183
-
184
- return Qtrue;
156
+ if(!coverinfo)
157
+ coverinfo = st_init_strtable();
158
+ coverage_hook_set_p = 1;
159
+ /* TODO: allow C_CALL too, since it's supported already
160
+ * the overhead is around ~30%, tested on typo */
161
+ rb_add_event_hook(coverage_event_coverage_hook, RUBY_EVENT_ALL & ~RUBY_EVENT_C_CALL & ~RUBY_EVENT_C_RETURN & ~RUBY_EVENT_CLASS);
162
+
163
+ return Qtrue;
185
164
  }
186
165
  else
187
- return Qfalse;
166
+ return Qfalse;
188
167
  }
189
168
 
190
-
191
- static int
192
- populate_cover(st_data_t key, st_data_t value, st_data_t cover)
193
- {
194
- VALUE rcover;
195
- VALUE rkey;
196
- VALUE rval;
197
- struct cov_array *carray;
198
- unsigned int i;
169
+ static int populate_cover(st_data_t key, st_data_t value, st_data_t cover) {
170
+ VALUE rcover;
171
+ VALUE rkey;
172
+ VALUE rval;
173
+ struct cov_array *carray;
174
+ unsigned int i;
199
175
 
200
- rcover = (VALUE)cover;
201
- carray = (struct cov_array *) value;
202
- rkey = rb_str_new2((char*) key);
203
- rval = rb_ary_new2(carray->len);
204
- for(i = 0; i < carray->len; i++)
205
- RARRAY(rval)->ptr[i] = UINT2NUM(carray->ptr[i]);
206
- RARRAY(rval)->len = carray->len;
207
-
208
- rb_hash_aset(rcover, rkey, rval);
209
-
210
- return ST_CONTINUE;
176
+ rcover = (VALUE)cover;
177
+ carray = (struct cov_array *) value;
178
+ rkey = rb_str_new2((char*) key);
179
+ rval = rb_ary_new2(carray->len);
180
+
181
+ for(i = 0; i < carray->len; i++)
182
+ RARRAY(rval)->ptr[i] = UINT2NUM(carray->ptr[i]);
183
+
184
+ RARRAY(rval)->len = carray->len;
185
+ rb_hash_aset(rcover, rkey, rval);
186
+
187
+ return ST_CONTINUE;
211
188
  }
212
189
 
213
-
214
- static int
215
- free_table(st_data_t key, st_data_t value, st_data_t ignored)
216
- {
217
- struct cov_array *carray;
190
+ static int free_table(st_data_t key, st_data_t value, st_data_t ignored) {
191
+ struct cov_array *carray;
218
192
 
219
- carray = (struct cov_array *) value;
220
- free((char *)key);
221
- free(carray->ptr);
222
- free(carray);
193
+ carray = (struct cov_array *) value;
194
+ free((char *)key);
195
+ free(carray->ptr);
196
+ free(carray);
223
197
 
224
- return ST_CONTINUE;
198
+ return ST_CONTINUE;
225
199
  }
226
200
 
227
-
228
- static VALUE
229
- cov_remove_coverage_hook(VALUE self)
230
- {
231
- if(!coverage_hook_set_p)
232
- return Qfalse;
233
- else {
234
- rb_remove_event_hook(coverage_event_coverage_hook);
235
- coverage_hook_set_p = 0;
236
- return Qtrue;
237
- }
201
+ static VALUE cov_remove_coverage_hook(VALUE self) {
202
+ if(!coverage_hook_set_p)
203
+ return Qfalse;
204
+ else {
205
+ rb_remove_event_hook(coverage_event_coverage_hook);
206
+ coverage_hook_set_p = 0;
207
+ return Qtrue;
208
+ }
238
209
  }
239
210
 
240
-
241
- static VALUE
242
- cov_generate_coverage_info(VALUE self)
243
- {
211
+ static VALUE cov_generate_coverage_info(VALUE self) {
244
212
  VALUE cover;
245
213
 
246
214
  if(rb_const_defined_at(mRCOV__, id_cover)) {
247
- rb_mod_remove_const(mRCOV__, ID2SYM(id_cover));
215
+ rb_mod_remove_const(mRCOV__, ID2SYM(id_cover));
248
216
  }
249
217
 
250
218
  cover = rb_hash_new();
219
+
251
220
  if(coverinfo)
252
- st_foreach(coverinfo, populate_cover, cover);
221
+ st_foreach(coverinfo, populate_cover, cover);
222
+
253
223
  rb_define_const(mRCOV__, "COVER", cover);
254
224
 
255
225
  return cover;
256
226
  }
257
227
 
258
-
259
- static VALUE
260
- cov_reset_coverage(VALUE self)
261
- {
228
+ static VALUE cov_reset_coverage(VALUE self) {
262
229
  if(coverage_hook_set_p) {
263
- rb_raise(rb_eRuntimeError,
264
- "Cannot reset the coverage info in the middle of a traced run.");
265
- return Qnil;
230
+ rb_raise(rb_eRuntimeError, "Cannot reset the coverage info in the middle of a traced run.");
231
+ return Qnil;
266
232
  }
267
233
 
268
234
  cached_array = 0;
@@ -274,58 +240,48 @@ cov_reset_coverage(VALUE self)
274
240
  return Qnil;
275
241
  }
276
242
 
277
-
278
- static VALUE
279
- cov_ABI(VALUE self)
280
- {
243
+ static VALUE cov_ABI(VALUE self) {
281
244
  VALUE ret;
282
-
245
+
283
246
  ret = rb_ary_new();
284
247
  rb_ary_push(ret, INT2FIX(RCOVRT_VERSION_MAJOR));
285
248
  rb_ary_push(ret, INT2FIX(RCOVRT_VERSION_MINOR));
286
249
  rb_ary_push(ret, INT2FIX(RCOVRT_VERSION_REV));
287
-
250
+
288
251
  return ret;
289
252
  }
290
253
 
291
-
292
- void
293
- Init_rcovrt()
294
- {
295
- ID id_rcov = rb_intern("Rcov");
296
- ID id_coverage__ = rb_intern("RCOV__");
297
- ID id_script_lines__ = rb_intern("SCRIPT_LINES__");
254
+ void Init_rcovrt() {
255
+ ID id_rcov = rb_intern("Rcov");
256
+ ID id_coverage__ = rb_intern("RCOV__");
257
+ ID id_script_lines__ = rb_intern("SCRIPT_LINES__");
298
258
 
299
- id_cover = rb_intern("COVER");
300
-
301
- if(rb_const_defined(rb_cObject, id_rcov))
302
- mRcov = rb_const_get(rb_cObject, id_rcov);
303
- else
304
- mRcov = rb_define_module("Rcov");
305
-
306
- if(rb_const_defined(mRcov, id_coverage__))
307
- mRCOV__ = rb_const_get_at(mRcov, id_coverage__);
308
- else
309
- mRCOV__ = rb_define_module_under(mRcov, "RCOV__");
310
-
311
- if(rb_const_defined(rb_cObject, id_script_lines__))
312
- oSCRIPT_LINES__ = rb_const_get(rb_cObject, rb_intern("SCRIPT_LINES__"));
313
- else {
314
- oSCRIPT_LINES__ = rb_hash_new();
315
- rb_const_set(rb_cObject, id_script_lines__, oSCRIPT_LINES__);
316
- }
317
-
318
- coverage_hook_set_p = 0;
319
-
320
- rb_define_singleton_method(mRCOV__, "install_coverage_hook",
321
- cov_install_coverage_hook, 0);
322
- rb_define_singleton_method(mRCOV__, "remove_coverage_hook",
323
- cov_remove_coverage_hook, 0);
324
- rb_define_singleton_method(mRCOV__, "generate_coverage_info",
325
- cov_generate_coverage_info, 0);
326
- rb_define_singleton_method(mRCOV__, "reset_coverage", cov_reset_coverage, 0);
327
- rb_define_singleton_method(mRCOV__, "ABI", cov_ABI, 0);
328
-
329
- Init_rcov_callsite();
259
+ id_cover = rb_intern("COVER");
260
+
261
+ if(rb_const_defined(rb_cObject, id_rcov))
262
+ mRcov = rb_const_get(rb_cObject, id_rcov);
263
+ else
264
+ mRcov = rb_define_module("Rcov");
265
+
266
+ if(rb_const_defined(mRcov, id_coverage__))
267
+ mRCOV__ = rb_const_get_at(mRcov, id_coverage__);
268
+ else
269
+ mRCOV__ = rb_define_module_under(mRcov, "RCOV__");
270
+
271
+ if(rb_const_defined(rb_cObject, id_script_lines__))
272
+ oSCRIPT_LINES__ = rb_const_get(rb_cObject, rb_intern("SCRIPT_LINES__"));
273
+ else {
274
+ oSCRIPT_LINES__ = rb_hash_new();
275
+ rb_const_set(rb_cObject, id_script_lines__, oSCRIPT_LINES__);
276
+ }
277
+
278
+ coverage_hook_set_p = 0;
279
+
280
+ rb_define_singleton_method(mRCOV__, "install_coverage_hook", cov_install_coverage_hook, 0);
281
+ rb_define_singleton_method(mRCOV__, "remove_coverage_hook", cov_remove_coverage_hook, 0);
282
+ rb_define_singleton_method(mRCOV__, "generate_coverage_info", cov_generate_coverage_info, 0);
283
+ rb_define_singleton_method(mRCOV__, "reset_coverage", cov_reset_coverage, 0);
284
+ rb_define_singleton_method(mRCOV__, "ABI", cov_ABI, 0);
285
+
286
+ Init_rcov_callsite();
330
287
  }
331
- /* vim: set sw=8 expandtab: */