Skip to content

Commit

Permalink
[PRISM] Use correct warning encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed May 8, 2024
1 parent 5bb656e commit ba062a6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
31 changes: 26 additions & 5 deletions error.c
Expand Up @@ -386,18 +386,28 @@ warn_vsprintf(rb_encoding *enc, const char *file, int line, const char *fmt, va_
return rb_str_cat2(str, "\n");
}

#define with_warn_vsprintf(file, line, fmt) \
#define with_warn_vsprintf(enc, file, line, fmt) \
VALUE str; \
va_list args; \
va_start(args, fmt); \
str = warn_vsprintf(NULL, file, line, fmt, args); \
str = warn_vsprintf(enc, file, line, fmt, args); \
va_end(args);

void
rb_compile_warn(const char *file, int line, const char *fmt, ...)
{
if (!NIL_P(ruby_verbose)) {
with_warn_vsprintf(file, line, fmt) {
with_warn_vsprintf(NULL, file, line, fmt) {
rb_write_warning_str(str);
}
}
}

void
rb_enc_compile_warn(rb_encoding *enc, const char *file, int line, const char *fmt, ...)
{
if (!NIL_P(ruby_verbose)) {
with_warn_vsprintf(enc, file, line, fmt) {
rb_write_warning_str(str);
}
}
Expand All @@ -408,7 +418,18 @@ void
rb_compile_warning(const char *file, int line, const char *fmt, ...)
{
if (RTEST(ruby_verbose)) {
with_warn_vsprintf(file, line, fmt) {
with_warn_vsprintf(NULL, file, line, fmt) {
rb_write_warning_str(str);
}
}
}

/* rb_enc_compile_warning() reports only in verbose mode */
void
rb_enc_compile_warning(rb_encoding *enc, const char *file, int line, const char *fmt, ...)
{
if (RTEST(ruby_verbose)) {
with_warn_vsprintf(enc, file, line, fmt) {
rb_write_warning_str(str);
}
}
Expand All @@ -418,7 +439,7 @@ void
rb_category_compile_warn(rb_warning_category_t category, const char *file, int line, const char *fmt, ...)
{
if (!NIL_P(ruby_verbose)) {
with_warn_vsprintf(file, line, fmt) {
with_warn_vsprintf(NULL, file, line, fmt) {
rb_warn_category(str, rb_warning_category_to_name(category));
}
}
Expand Down
13 changes: 8 additions & 5 deletions prism_compile.c
Expand Up @@ -9280,6 +9280,9 @@ pm_parse_process_error(const pm_parse_result_t *result)
return error;
}

void rb_enc_compile_warning(rb_encoding *enc, const char *file, int line, const char *fmt, ...);
void rb_enc_compile_warn(rb_encoding *enc, const char *file, int line, const char *fmt, ...);

/**
* Parse the parse result and raise a Ruby error if there are any syntax errors.
* It returns an error if one should be raised. It is assumed that the parse
Expand All @@ -9298,6 +9301,9 @@ pm_parse_process(pm_parse_result_t *result, pm_node_t *node)
pm_scope_node_init(node, scope_node, NULL);
scope_node->filepath_encoding = filepath_encoding;

scope_node->encoding = rb_enc_find(parser->encoding->name);
if (!scope_node->encoding) rb_bug("Encoding not found %s!", parser->encoding->name);

// 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);
Expand All @@ -9306,10 +9312,10 @@ pm_parse_process(pm_parse_result_t *result, pm_node_t *node)
int line = pm_location_line_number(parser, &warning->location);

if (warning->level == PM_WARNING_LEVEL_VERBOSE) {
rb_compile_warning(warning_filepath, line, "%s", warning->message);
rb_enc_compile_warning(scope_node->encoding, warning_filepath, line, "%s", warning->message);
}
else {
rb_compile_warn(warning_filepath, line, "%s", warning->message);
rb_enc_compile_warn(scope_node->encoding, warning_filepath, line, "%s", warning->message);
}
}

Expand All @@ -9324,9 +9330,6 @@ pm_parse_process(pm_parse_result_t *result, pm_node_t *node)

// Now set up the constant pool and intern all of the various constants into
// their corresponding IDs.
scope_node->encoding = rb_enc_find(parser->encoding->name);
if (!scope_node->encoding) rb_bug("Encoding not found %s!", parser->encoding->name);

scope_node->parser = parser;
scope_node->constants = calloc(parser->constant_pool.size, sizeof(ID));

Expand Down
1 change: 0 additions & 1 deletion test/.excludes-prism/TestParse.rb
Expand Up @@ -8,5 +8,4 @@
exclude(:test_truncated_source_line, "unknown")
exclude(:test_unexpected_eof, "unknown")
exclude(:test_unexpected_token_after_numeric, "unknown")
exclude(:test_unused_variable, "missing warning")
exclude(:test_void_value_in_rhs, "unknown")

0 comments on commit ba062a6

Please sign in to comment.