[前][次][番号順一覧][スレッド一覧]

ruby-changes:59345

From: Nobuyoshi <ko1@a...>
Date: Fri, 20 Dec 2019 23:19:12 +0900 (JST)
Subject: [ruby-changes:59345] a84ad24386 (master): Added -W: command line option

https://git.ruby-lang.org/ruby.git/commit/?id=a84ad24386

From a84ad24386d27269b90794146c2a351c1d79471b Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
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]

diff --git a/error.c b/error.c
index b261f2e..e662b4f 100644
--- a/error.c
+++ b/error.c
@@ -150,6 +150,13 @@ rb_warning_category_from_name(VALUE category) https://github.com/ruby/ruby/blob/trunk/error.c#L150
     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)
 {
diff --git a/ruby.c b/ruby.c
index dae928a..f6785e4 100644
--- a/ruby.c
+++ b/ruby.c
@@ -69,6 +69,8 @@ char *getenv(); https://github.com/ruby/ruby/blob/trunk/ruby.c#L69
 #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 { https://github.com/ruby/ruby/blob/trunk/ruby.c#L161
     } 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) https://github.com/ruby/ruby/blob/trunk/ruby.c#L269
 	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) https://github.com/ruby/ruby/blob/trunk/ruby.c#L300
 	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) https://github.com/ruby/ruby/blob/trunk/ruby.c#L330
     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) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1069
 	    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) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1598
 	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) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1609
 	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) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1803
         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) https://github.com/ruby/ruby/blob/trunk/ruby.c#L2042
     }
     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);
diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb
index 496a51b..04a7016 100644
--- a/test/ruby/test_rubyoptions.rb
+++ b/test/ruby/test_rubyoptions.rb
@@ -75,6 +75,9 @@ class TestRubyOptions < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L75
     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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L331
     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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]