This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add macro to free and set cop_warnings
authorNicolas R <atoomic@cpan.org>
Tue, 10 Mar 2020 17:14:04 +0000 (11:14 -0600)
committerTony Cook <tony@develop-help.com>
Sun, 15 Mar 2020 23:37:17 +0000 (10:37 +1100)
This is avoiding the boilerplate to free
the cop_warning string when setting it.

mg.c
perl.c
regen/warnings.pl
scope.c
utf8.c
warnings.h

diff --git a/mg.c b/mg.c
index cd1f57d..b21f4d3 100644 (file)
--- a/mg.c
+++ b/mg.c
@@ -2966,9 +2966,7 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
        else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) {
            if ( ! (PL_dowarn & G_WARN_ALL_MASK)) {
                if (!SvPOK(sv)) {
-                    if (!specialWARN(PL_compiling.cop_warnings))
-                        PerlMemShared_free(PL_compiling.cop_warnings);
-                   PL_compiling.cop_warnings = pWARN_STD;
+            free_and_set_cop_warnings(PL_compiling, pWARN_STD);
                    break;
                }
                {
@@ -2980,26 +2978,22 @@ Perl_magic_set(pTHX_ SV *sv, MAGIC *mg)
                        not_all |= ptr[i] ^ 0x55;
                    }
                    if (!not_none) {
-                       if (!specialWARN(PL_compiling.cop_warnings))
-                           PerlMemShared_free(PL_compiling.cop_warnings);
-                       PL_compiling.cop_warnings = pWARN_NONE;
+                free_and_set_cop_warnings(PL_compiling, pWARN_NONE);
                    } else if (len >= WARNsize && !not_all) {
-                       if (!specialWARN(PL_compiling.cop_warnings))
-                           PerlMemShared_free(PL_compiling.cop_warnings);
-                       PL_compiling.cop_warnings = pWARN_ALL;
-                       PL_dowarn |= G_WARN_ONCE ;
-                   }
-                    else {
-                       STRLEN len;
-                       const char *const p = SvPV_const(sv, len);
-
-                       PL_compiling.cop_warnings
-                           = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
+                free_and_set_cop_warnings(PL_compiling, pWARN_ALL);
+                   PL_dowarn |= G_WARN_ONCE ;
+               }
+            else {
+                            STRLEN len;
+                            const char *const p = SvPV_const(sv, len);
+
+                            PL_compiling.cop_warnings
+                                = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings,
                                                         p, len);
 
-                       if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
+                    if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE))
                            PL_dowarn |= G_WARN_ONCE ;
-                   }
+              }
 
                }
            }
diff --git a/perl.c b/perl.c
index df672f5..fdfd1cd 100644 (file)
--- a/perl.c
+++ b/perl.c
@@ -1245,9 +1245,7 @@ perl_destruct(pTHXx)
         }
     }
 
-    if (!specialWARN(PL_compiling.cop_warnings))
-       PerlMemShared_free(PL_compiling.cop_warnings);
-    PL_compiling.cop_warnings = NULL;
+    free_and_set_cop_warnings(PL_compiling, NULL);
     cophh_free(CopHINTHASH_get(&PL_compiling));
     CopHINTHASH_set(&PL_compiling, cophh_new_empty());
     CopFILE_free(&PL_compiling);
@@ -3740,16 +3738,12 @@ Perl_moreswitches(pTHX_ const char *s)
        return s;
     case 'W':
        PL_dowarn = G_WARN_ALL_ON|G_WARN_ON;
-        if (!specialWARN(PL_compiling.cop_warnings))
-            PerlMemShared_free(PL_compiling.cop_warnings);
-       PL_compiling.cop_warnings = pWARN_ALL ;
+    free_and_set_cop_warnings(PL_compiling, pWARN_ALL);
        s++;
        return s;
     case 'X':
        PL_dowarn = G_WARN_ALL_OFF;
-        if (!specialWARN(PL_compiling.cop_warnings))
-            PerlMemShared_free(PL_compiling.cop_warnings);
-       PL_compiling.cop_warnings = pWARN_NONE ;
+    free_and_set_cop_warnings(PL_compiling, pWARN_NONE);
        s++;
        return s;
     case '*':
index 93e6763..a73ca9e 100644 (file)
@@ -392,6 +392,11 @@ EOM
 
 #define DUP_WARNINGS(p) Perl_dup_warnings(aTHX_ p)
 
+#define free_and_set_cop_warnings(cmp,w) STMT_START { \
+  if (!specialWARN(cmp.cop_warnings)) PerlMemShared_free(cmp.cop_warnings); \
+  cmp.cop_warnings = w; \
+} STMT_END
+
 /*
 
 =head1 Warning and Dieing
diff --git a/scope.c b/scope.c
index e6a6237..ad8f8e7 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -1448,9 +1448,7 @@ Perl_leave_scope(pTHX_ I32 base)
 
        case SAVEt_COMPILE_WARNINGS:
             a0 = ap[0];
-           if (!specialWARN(PL_compiling.cop_warnings))
-               PerlMemShared_free(PL_compiling.cop_warnings);
-           PL_compiling.cop_warnings = (STRLEN*)a0.any_ptr;
+        free_and_set_cop_warnings(PL_compiling, (STRLEN*) a0.any_ptr);
            break;
 
        case SAVEt_PARSER:
diff --git a/utf8.c b/utf8.c
index 9b1595c..ac07507 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -55,9 +55,8 @@ within non-zero characters.
 static void
 S_restore_cop_warnings(pTHX_ void *p)
 {
-    if (!specialWARN(PL_curcop->cop_warnings))
-        PerlMemShared_free(PL_curcop->cop_warnings);
-    PL_curcop->cop_warnings = (STRLEN*)p;
+    COP curcop = *PL_curcop;
+    free_and_set_cop_warnings(curcop, (STRLEN*) p);
 }
 
 
index cf3d363..37ab205 100644 (file)
 
 #define DUP_WARNINGS(p) Perl_dup_warnings(aTHX_ p)
 
+#define free_and_set_cop_warnings(cmp,w) STMT_START { \
+  if (!specialWARN(cmp.cop_warnings)) PerlMemShared_free(cmp.cop_warnings); \
+  cmp.cop_warnings = w; \
+} STMT_END
+
 /*
 
 =head1 Warning and Dieing