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

ruby-changes:46075

From: nobu <ko1@a...>
Date: Mon, 27 Mar 2017 19:44:40 +0900 (JST)
Subject: [ruby-changes:46075] nobu:r58146 (trunk): error.c: refactor warning messages

nobu	2017-03-27 19:44:32 +0900 (Mon, 27 Mar 2017)

  New Revision: 58146

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58146

  Log:
    error.c: refactor warning messages
    
    * error.c (with_warning_string): extract building warning message
      string from variadic arguments.
    
    * error.c (syserr_warning): write warning message with the system
      error message.

  Modified files:
    trunk/error.c
Index: error.c
===================================================================
--- error.c	(revision 58145)
+++ error.c	(revision 58146)
@@ -200,62 +200,52 @@ warning_string(rb_encoding *enc, const c https://github.com/ruby/ruby/blob/trunk/error.c#L200
 			 fmt, args);
 }
 
+#define with_warning_string(mesg, enc, fmt) \
+    VALUE mesg; \
+    va_list args; va_start(args, fmt); \
+    mesg = warning_string(enc, fmt, args); \
+    va_end(args);
+
 void
 rb_warn(const char *fmt, ...)
 {
-    VALUE mesg;
-    va_list args;
-
-    if (NIL_P(ruby_verbose)) return;
-
-    va_start(args, fmt);
-    mesg = warning_string(0, fmt, args);
-    va_end(args);
-    rb_write_warning_str(mesg);
+    if (!NIL_P(ruby_verbose)) {
+	with_warning_string(mesg, 0, fmt) {
+	    rb_write_warning_str(mesg);
+	}
+    }
 }
 
 void
 rb_enc_warn(rb_encoding *enc, const char *fmt, ...)
 {
-    VALUE mesg;
-    va_list args;
-
-    if (NIL_P(ruby_verbose)) return;
-
-    va_start(args, fmt);
-    mesg = warning_string(enc, fmt, args);
-    va_end(args);
-    rb_write_warning_str(mesg);
+    if (!NIL_P(ruby_verbose)) {
+	with_warning_string(mesg, enc, fmt) {
+	    rb_write_warning_str(mesg);
+	}
+    }
 }
 
 /* rb_warning() reports only in verbose mode */
 void
 rb_warning(const char *fmt, ...)
 {
-    VALUE mesg;
-    va_list args;
-
-    if (!RTEST(ruby_verbose)) return;
-
-    va_start(args, fmt);
-    mesg = warning_string(0, fmt, args);
-    va_end(args);
-    rb_write_warning_str(mesg);
+    if (RTEST(ruby_verbose)) {
+	with_warning_string(mesg, 0, fmt) {
+	    rb_write_warning_str(mesg);
+	}
+    }
 }
 
 #if 0
 void
 rb_enc_warning(rb_encoding *enc, const char *fmt, ...)
 {
-    VALUE mesg;
-    va_list args;
-
-    if (!RTEST(ruby_verbose)) return;
-
-    va_start(args, fmt);
-    mesg = warning_string(enc, fmt, args);
-    va_end(args);
-    rb_write_warning_str(mesg);
+    if (RTEST(ruby_verbose)) {
+	with_warning_string(mesg, enc, fmt) {
+	    rb_write_warning_str(mesg);
+	}
+    }
 }
 #endif
 
@@ -2390,44 +2380,36 @@ rb_mod_syserr_fail_str(VALUE mod, int e, https://github.com/ruby/ruby/blob/trunk/error.c#L2380
     rb_exc_raise(exc);
 }
 
-void
-rb_sys_warning(const char *fmt, ...)
+static void
+syserr_warning(VALUE mesg, int err)
 {
-    VALUE mesg;
-    va_list args;
-    int errno_save;
-
-    errno_save = errno;
-
-    if (!RTEST(ruby_verbose)) return;
-
-    va_start(args, fmt);
-    mesg = warning_string(0, fmt, args);
-    va_end(args);
     rb_str_set_len(mesg, RSTRING_LEN(mesg)-1);
-    rb_str_catf(mesg, ": %s\n", strerror(errno_save));
+    rb_str_catf(mesg, ": %s\n", strerror(err));
     rb_write_warning_str(mesg);
-    errno = errno_save;
 }
 
 void
-rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...)
+rb_sys_warning(const char *fmt, ...)
 {
-    VALUE mesg;
-    va_list args;
-    int errno_save;
-
-    errno_save = errno;
-
-    if (!RTEST(ruby_verbose)) return;
+    if (RTEST(ruby_verbose)) {
+	int errno_save = errno;
+	with_warning_string(mesg, 0, fmt) {
+	    syserr_warning(mesg, errno_save);
+	}
+	errno = errno_save;
+    }
+}
 
-    va_start(args, fmt);
-    mesg = warning_string(enc, fmt, args);
-    va_end(args);
-    rb_str_set_len(mesg, RSTRING_LEN(mesg)-1);
-    rb_str_catf(mesg, ": %s\n", strerror(errno_save));
-    rb_write_warning_str(mesg);
-    errno = errno_save;
+void
+rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...)
+{
+    if (RTEST(ruby_verbose)) {
+	int errno_save = errno;
+	with_warning_string(mesg, enc, fmt) {
+	    syserr_warning(mesg, errno_save);
+	}
+	errno = errno_save;
+    }
 }
 
 void

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

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