Skip to content

Commit

Permalink
Added -W: command line option
Browse files Browse the repository at this point in the history
To manage `Warning[category]` flags.  Only `-W:deprecated` and
`-W:no-deprecated` are available now.  [Feature #16345]
  • Loading branch information
nobu committed Dec 20, 2019
1 parent 6876aa3 commit a84ad24
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
7 changes: 7 additions & 0 deletions error.c
Expand Up @@ -150,6 +150,13 @@ rb_warning_category_from_name(VALUE category)
return cat;
}

void
rb_warning_category_update(unsigned int mask, unsigned int bits)
{
warning_disabled_categories &= ~mask;
warning_disabled_categories |= mask & ~bits;
}

MJIT_FUNC_EXPORTED bool
rb_warning_category_enabled_p(rb_warning_category_t category)
{
Expand Down
30 changes: 29 additions & 1 deletion ruby.c
Expand Up @@ -69,6 +69,8 @@ char *getenv();
#define DEFAULT_RUBYGEMS_ENABLED "enabled"
#endif

void rb_warning_category_update(unsigned int mask, unsigned int bits);

#define COMMA ,
#define FEATURE_BIT(bit) (1U << feature_##bit)
#define EACH_FEATURES(X, SEP) \
Expand Down Expand Up @@ -159,6 +161,7 @@ struct ruby_cmdline_options {
} src, ext, intern;
VALUE req_list;
ruby_features_t features;
ruby_features_t warn;
unsigned int dump;
#if USE_MJIT
struct mjit_options mjit;
Expand Down Expand Up @@ -266,7 +269,7 @@ usage(const char *name, int help)
M("-S", "", "look for the script using PATH environment variable"),
M("-v", "", "print the version number, then turn on verbose mode"),
M("-w", "", "turn warnings on for your script"),
M("-W[level=2]", "", "set warning level; 0=silence, 1=medium, 2=verbose"),
M("-W[level=2|:category]", "", "set warning level; 0=silence, 1=medium, 2=verbose"),
M("-x[directory]", "", "strip off text before #!ruby line and perhaps cd to directory"),
M("--jit", "", "enable JIT with default options (experimental)"),
M("--jit-[option]","", "enable JIT with an option (experimental)"),
Expand Down Expand Up @@ -297,6 +300,9 @@ usage(const char *name, int help)
M("frozen-string-literal", "", "freeze all string literals (default: disabled)"),
M("jit", "", "JIT compiler (default: disabled)"),
};
static const struct message warn_categories[] = {
M("deprecated", "", "deprecated features"),
};
static const struct message mjit_options[] = {
M("--jit-warnings", "", "Enable printing JIT warnings"),
M("--jit-debug", "", "Enable JIT debugging (very slow), or add cflags if specified"),
Expand Down Expand Up @@ -324,6 +330,9 @@ usage(const char *name, int help)
puts("Features:");
for (i = 0; i < numberof(features); ++i)
SHOW(features[i]);
puts("Warning categories:");
for (i = 0; i < numberof(warn_categories); ++i)
SHOW(warn_categories[i]);
puts("JIT options (experimental):");
for (i = 0; i < numberof(mjit_options); ++i)
SHOW(mjit_options[i]);
Expand Down Expand Up @@ -1060,6 +1069,21 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt)
goto reswitch;

case 'W':
if (s[1] == ':') {
unsigned int bits = 0;
static const char no_prefix[] = "no-";
int enable = strncmp(s += 2, no_prefix, sizeof(no_prefix)-1) != 0;
if (!enable) s += sizeof(no_prefix)-1;
size_t len = strlen(s);
if (NAME_MATCH_P("deprecated", s, len)) {
bits = 1U << RB_WARN_CATEGORY_DEPRECATED;
}
else {
rb_warn("unknown warning category: `%s'", s);
}
if (bits) FEATURE_SET_TO(opt->warn, bits, enable ? bits : 0);
break;
}
{
size_t numlen;
int v = 2; /* -W as -W2 */
Expand Down Expand Up @@ -1574,6 +1598,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
VALUE ext_enc_name = opt->ext.enc.name;
VALUE int_enc_name = opt->intern.enc.name;
ruby_features_t feat = opt->features;
ruby_features_t warn = opt->warn;

opt->src.enc.name = opt->ext.enc.name = opt->intern.enc.name = 0;
moreswitches(s, opt, 1);
Expand All @@ -1584,6 +1609,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
if (int_enc_name)
opt->intern.enc.name = int_enc_name;
FEATURE_SET_RESTORE(opt->features, feat);
FEATURE_SET_RESTORE(opt->warn, warn);
}

if (opt->src.enc.name)
Expand Down Expand Up @@ -1777,6 +1803,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
ruby_set_script_name(progname);
rb_parser_set_options(parser, opt->do_print, opt->do_loop,
opt->do_line, opt->do_split);
rb_warning_category_update(opt->warn.mask, opt->warn.set);
ast = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
}
else {
Expand Down Expand Up @@ -2015,6 +2042,7 @@ load_file_internal(VALUE argp_v)
}
rb_parser_set_options(parser, opt->do_print, opt->do_loop,
opt->do_line, opt->do_split);
rb_warning_category_update(opt->warn.mask, opt->warn.set);
if (NIL_P(f)) {
f = rb_str_new(0, 0);
rb_enc_associate(f, enc);
Expand Down
9 changes: 9 additions & 0 deletions test/ruby/test_rubyoptions.rb
Expand Up @@ -75,6 +75,9 @@ def test_warning
assert_in_out_err(%w(-Wx -e) + ['p $-W'], "", %w(1), [])
assert_in_out_err(%w(-W -e) + ['p $-W'], "", %w(2), [])
assert_in_out_err(%w(-w -W0 -e) + ['p $-W'], "", %w(0), [])
assert_in_out_err(%w(-W:deprecated -e) + ['p Warning[:deprecated]'], "", %w(true), [])
assert_in_out_err(%w(-W:no-deprecated -e) + ['p Warning[:deprecated]'], "", %w(false), [])
assert_in_out_err(%w(-W:qux), "", [], /unknown warning category: `qux'/)
ensure
ENV['RUBYOPT'] = save_rubyopt
end
Expand Down Expand Up @@ -328,6 +331,12 @@ def test_rubyopt
assert_in_out_err(%w(), "p $VERBOSE", ["true"])
assert_in_out_err(%w(-W1), "p $VERBOSE", ["false"])
assert_in_out_err(%w(-W0), "p $VERBOSE", ["nil"])
ENV['RUBYOPT'] = '-W:deprecated'
assert_in_out_err(%w(), "p Warning[:deprecated]", ["true"])
ENV['RUBYOPT'] = '-W:no-deprecated'
assert_in_out_err(%w(), "p Warning[:deprecated]", ["false"])
ENV['RUBYOPT'] = '-W:qux'
assert_in_out_err(%w(), "", [], /unknown warning category: `qux'/)
ensure
if rubyopt_orig
ENV['RUBYOPT'] = rubyopt_orig
Expand Down

0 comments on commit a84ad24

Please sign in to comment.