[PRISM] Respect eval coverage setting

This commit is contained in:
Kevin Newton 2024-05-20 11:29:50 -04:00
Родитель 78e504f21d
Коммит a708b6aa65
6 изменённых файлов: 25 добавлений и 3 удалений

7
iseq.c
Просмотреть файл

@ -1029,8 +1029,13 @@ pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpa
ISEQ_BODY(iseq)->prism = true;
ISEQ_BODY(iseq)->param.flags.use_block = true; // unused block warning is not supported yet
rb_compile_option_t next_option;
if (!option) option = &COMPILE_OPTION_DEFAULT;
next_option = *option;
next_option.coverage_enabled = node->coverage_enabled < 0 ? 0 : node->coverage_enabled > 0;
option = &next_option;
pm_location_t *location = &node->base.location;
int32_t start_line = node->parser->start_line;
@ -1273,6 +1278,7 @@ pm_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, V
pm_parse_result_t result = { 0 };
pm_options_line_set(&result.options, NUM2INT(line));
result.node.coverage_enabled = 1;
switch (option.frozen_string_literal) {
case ISEQ_FROZEN_STRING_LITERAL_UNSET:
@ -1708,6 +1714,7 @@ iseqw_s_compile_file_prism(int argc, VALUE *argv, VALUE self)
pm_parse_result_t result = { 0 };
result.options.line = 1;
result.node.coverage_enabled = 1;
VALUE error = pm_load_parse_file(&result, file);

1
load.c
Просмотреть файл

@ -746,6 +746,7 @@ load_iseq_eval(rb_execution_context_t *ec, VALUE fname)
if (*rb_ruby_prism_ptr()) {
pm_parse_result_t result = { 0 };
result.options.line = 1;
result.node.coverage_enabled = 1;
VALUE error = pm_load_parse_file(&result, fname);

Просмотреть файл

@ -2868,6 +2868,7 @@ pm_scope_node_init(const pm_node_t *node, pm_scope_node_t *scope, pm_scope_node_
scope->encoding = previous->encoding;
scope->filepath_encoding = previous->filepath_encoding;
scope->constants = previous->constants;
scope->coverage_enabled = previous->coverage_enabled;
}
switch (PM_NODE_TYPE(node)) {
@ -9299,6 +9300,7 @@ pm_parse_process(pm_parse_result_t *result, pm_node_t *node)
// freed regardless of whether or we return an error.
pm_scope_node_t *scope_node = &result->node;
rb_encoding *filepath_encoding = scope_node->filepath_encoding;
int coverage_enabled = scope_node->coverage_enabled;
pm_scope_node_init(node, scope_node, NULL);
scope_node->filepath_encoding = filepath_encoding;
@ -9306,6 +9308,8 @@ pm_parse_process(pm_parse_result_t *result, pm_node_t *node)
scope_node->encoding = rb_enc_find(parser->encoding->name);
if (!scope_node->encoding) rb_bug("Encoding not found %s!", parser->encoding->name);
scope_node->coverage_enabled = coverage_enabled;
// Emit all of the various warnings from the parse.
const pm_diagnostic_t *warning;
const char *warning_filepath = (const char *) pm_string_source(&parser->filepath);

Просмотреть файл

@ -36,14 +36,16 @@ typedef struct pm_scope_node {
*/
rb_encoding *filepath_encoding;
// The size of the local table
// on the iseq which includes
// locals and hidden variables
// The size of the local table on the iseq which includes locals and hidden
// variables.
int local_table_for_iseq_size;
ID *constants;
st_table *index_lookup_table;
// The current coverage setting, passed down through the various scopes.
int coverage_enabled;
/**
* This will only be set on the top-level scope node. It will contain all of
* the instructions pertaining to BEGIN{} nodes.

1
ruby.c
Просмотреть файл

@ -2155,6 +2155,7 @@ prism_script(ruby_cmdline_options_t *opt, pm_parse_result_t *result)
pm_options_command_line_set(options, command_line);
prism_opt_init(opt);
result->node.coverage_enabled = 0;
error = pm_parse_string(result, opt->e_script, rb_str_new2("-e"));
}
else {

Просмотреть файл

@ -1629,6 +1629,10 @@ pm_eval_make_iseq(VALUE src, VALUE fname, int line,
const rb_iseq_t *const parent = vm_block_iseq(base_block);
const rb_iseq_t *iseq = parent;
VALUE name = rb_fstring_lit("<compiled>");
// Conditionally enable coverage depending on the current mode:
int coverage_enabled = ((rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) != 0) ? 1 : 0;
if (!fname) {
fname = rb_source_location(&line);
}
@ -1638,10 +1642,12 @@ pm_eval_make_iseq(VALUE src, VALUE fname, int line,
}
else {
fname = get_eval_default_path();
coverage_enabled = 0;
}
pm_parse_result_t result = { 0 };
pm_options_line_set(&result.options, line);
result.node.coverage_enabled = coverage_enabled;
// Cout scopes, one for each parent iseq, plus one for our local scope
int scopes_count = 0;
@ -1703,6 +1709,7 @@ pm_eval_make_iseq(VALUE src, VALUE fname, int line,
RUBY_ASSERT(parent_scope != NULL);
pm_options_scope_t *options_scope = &result.options.scopes[scopes_count - scopes_index - 1];
parent_scope->coverage_enabled = coverage_enabled;
parent_scope->parser = &result.parser;
parent_scope->index_lookup_table = st_init_numtable();