This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add PERL_UNUSED_RESULT() and use that instead of the V_Gconvert().
authorJarkko Hietaniemi <jhi@iki.fi>
Mon, 28 Apr 2014 00:52:54 +0000 (20:52 -0400)
committerSteffen Mueller <smueller@cpan.org>
Wed, 28 May 2014 10:49:25 +0000 (12:49 +0200)
perl.h
sv.c

diff --git a/perl.h b/perl.h
index 6da39f3..b740d9a 100644 (file)
--- a/perl.h
+++ b/perl.h
 #  define PERL_UNUSED_CONTEXT
 #endif
 
+/* Use PERL_UNUSED_RESULT() to suppress the warnings about unused results
+ * of function calls, e.g. PERL_UNUSED_RESULT(foo(a, b)).  Use it sparingly,
+ * though, since usually the warning is there for a good reason,
+ * e.g. for realloc(): the new pointer is not necessarily the old pointer.
+ *
+ * But sometimes you just want to ignore the return value, e.g. on
+ * codepaths soon ending up in abort, or in "best effort" attempts.
+ * Sometimes you can capture the return value and use PERL_UNUSED_VAR
+ * on that.
+ *
+ * The combination of gcc -Wunused-result (part of -Wall) and the gcc
+ * warn_unused_result attribute cannot be silenced with (void).
+ *
+ * The __typeof__() is unused instead of typeof() since typeof() is
+ * not available under stricter ANSI modes, and because of compilers
+ * masquerading as gcc (clang and icc), we want exactly the gcc
+ * extension __typeof__ and nothing else.
+ */
+#ifndef PERL_UNUSED_RESULT
+#  ifdef __GNUC__
+#    define PERL_UNUSED_RESULT(v) ({ __typeof__(v) z = (v); (void)sizeof(z); })
+#  else
+#    define PERL_UNUSED_RESULT(v) ((void)(v))
+#  endif
+#endif
+
 /* on gcc (and clang), specify that a warning should be temporarily
  * ignored; e.g.
  *
diff --git a/sv.c b/sv.c
index d748d56..1dbd3fe 100644 (file)
--- a/sv.c
+++ b/sv.c
  * has a mandatory return value, even though that value is just the same
  * as the buf arg */
 
-#define V_Gconvert(x,n,t,b) \
-{ \
-    char *rc = (char *)Gconvert(x,n,t,b); \
-    PERL_UNUSED_VAR(rc); \
-}
-
-
 #ifdef PERL_UTF8_CACHE_ASSERT
 /* if adding more checks watch out for the following tests:
  *   t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
@@ -2988,12 +2981,12 @@ Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const I32 flags)
            /* some Xenix systems wipe out errno here */
 
 #ifndef USE_LOCALE_NUMERIC
-            V_Gconvert(SvNVX(sv), NV_DIG, 0, s);
+            PERL_UNUSED_RESULT(Gconvert(SvNVX(sv), NV_DIG, 0, s));
             SvPOK_on(sv);
 #else
             {
                 DECLARE_STORE_LC_NUMERIC_SET_TO_NEEDED();
-                V_Gconvert(SvNVX(sv), NV_DIG, 0, s);
+                PERL_UNUSED_RESULT(Gconvert(SvNVX(sv), NV_DIG, 0, s));
 
                 /* If the radix character is UTF-8, and actually is in the
                  * output, turn on the UTF-8 flag for the scalar */
@@ -10679,7 +10672,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
                if (digits && digits < sizeof(ebuf) - NV_DIG - 10) {
                     /* 0, point, slack */
                     STORE_LC_NUMERIC_SET_TO_NEEDED();
-                   V_Gconvert(nv, (int)digits, 0, ebuf);
+                   PERL_UNUSED_RESULT(Gconvert(nv, (int)digits, 0, ebuf));
                    sv_catpv_nomg(sv, ebuf);
                    if (*ebuf)  /* May return an empty string for digits==0 */
                        return;
@@ -11537,7 +11530,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
                   aka precis is 0  */
                if ( c == 'g' && precis) {
                     STORE_LC_NUMERIC_SET_TO_NEEDED();
-                   V_Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf);
+                   PERL_UNUSED_RESULT(Gconvert((NV)nv, (int)precis, 0, PL_efloatbuf));
                    /* May return an empty string for digits==0 */
                    if (*PL_efloatbuf) {
                        elen = strlen(PL_efloatbuf);
@@ -11968,7 +11961,6 @@ Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
     DIR *ret;
 
 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
-    int rc = 0;
     DIR *pwd;
     const Direntry_t *dirent;
     char smallbuf[256];
@@ -12005,9 +11997,8 @@ Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
     /* Now we should have two dir handles pointing to the same dir. */
 
     /* Be nice to the calling code and chdir back to where we were. */
-    rc = fchdir(my_dirfd(pwd));
     /* XXX If this fails, then what? */
-    PERL_UNUSED_VAR(rc);
+    PERL_UNUSED_RESULT(fchdir(my_dirfd(pwd)));
 
     /* We have no need of the pwd handle any more. */
     PerlDir_close(pwd);