mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Added -W: command line option
To manage `Warning[category]` flags. Only `-W:deprecated` and `-W:no-deprecated` are available now. [Feature #16345]
This commit is contained in:
parent
6876aa38f6
commit
a84ad24386
3 changed files with 45 additions and 1 deletions
7
error.c
7
error.c
|
@ -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)
|
||||
{
|
||||
|
|
30
ruby.c
30
ruby.c
|
@ -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) \
|
||||
|
@ -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;
|
||||
|
@ -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)"),
|
||||
|
@ -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"),
|
||||
|
@ -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]);
|
||||
|
@ -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 */
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
|
|
|
@ -75,6 +75,9 @@ class TestRubyOptions < Test::Unit::TestCase
|
|||
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
|
||||
|
@ -328,6 +331,12 @@ class TestRubyOptions < Test::Unit::TestCase
|
|||
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
|
||||
|
|
Loading…
Add table
Reference in a new issue