From a84ad24386d27269b90794146c2a351c1d79471b Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Fri, 20 Dec 2019 23:05:22 +0900 Subject: Added -W: command line option To manage `Warning[category]` flags. Only `-W:deprecated` and `-W:no-deprecated` are available now. [Feature #16345] --- ruby.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'ruby.c') diff --git a/ruby.c b/ruby.c index dae928a675..f6785e4736 100644 --- a/ruby.c +++ b/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); -- cgit v1.2.3