warn on C<my($foo,$foo)>
[p5sagit/p5-mst-13.2.git] / op.c
diff --git a/op.c b/op.c
index d6836d9..9b2284d 100644 (file)
--- a/op.c
+++ b/op.c
@@ -92,7 +92,7 @@ void
 assertref(OP *o)
 {
     int type = o->op_type;
-    if (type != OP_AELEM && type != OP_HELEM) {
+    if (type != OP_AELEM && type != OP_HELEM && type != OP_GELEM) {
        yyerror(form("Can't use subscript on %s", op_desc[type]));
        if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) {
            dTHR;
@@ -126,15 +126,16 @@ pad_allocmy(char *name)
        }
        croak("Can't use global %s in \"my\"",name);
     }
-    if (PL_dowarn && AvFILLp(PL_comppad_name) >= 0) {
+    if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
        SV **svp = AvARRAY(PL_comppad_name);
        for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
            if ((sv = svp[off])
                && sv != &PL_sv_undef
-               && SvIVX(sv) == 999999999       /* var is in open scope */
                && strEQ(name, SvPVX(sv)))
            {
-               warn("\"my\" variable %s masks earlier declaration in same scope", name);
+               warner(WARN_UNSAFE,
+                       "\"my\" variable %s masks earlier declaration in same %s", 
+                       name, (SvIVX(sv) == 999999999 ? "scope" : "statement"));
                break;
            }
        }
@@ -231,8 +232,8 @@ pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix)
                                if (CvANON(bcv))
                                    CvCLONE_on(bcv);
                                else {
-                                   if (PL_dowarn && !CvUNIQUE(cv))
-                                       warn(
+                                   if (ckWARN(WARN_CLOSURE) && !CvUNIQUE(cv))
+                                       warner(WARN_CLOSURE,
                                          "Variable \"%s\" may be unavailable",
                                             name);
                                    break;
@@ -241,8 +242,9 @@ pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix)
                        }
                    }
                    else if (!CvUNIQUE(PL_compcv)) {
-                       if (PL_dowarn && !SvFAKE(sv) && !CvUNIQUE(cv))
-                           warn("Variable \"%s\" will not stay shared", name);
+                       if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
+                           warner(WARN_CLOSURE,
+                               "Variable \"%s\" will not stay shared", name);
                    }
                }
                av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
@@ -548,7 +550,7 @@ find_threadsv(char *name)
        default:
            sv_magic(sv, 0, 0, name, 1); 
        }
-       DEBUG_L(PerlIO_printf(PerlIO_stderr(),
+       DEBUG_S(PerlIO_printf(PerlIO_stderr(),
                              "find_threadsv: new SV %p for $%s%c\n",
                              sv, (*name < 32) ? "^" : "",
                              (*name < 32) ? toCTRL(*name) : *name));
@@ -582,6 +584,10 @@ op_free(OP *o)
        o->op_targ = 0; /* Was holding hints. */
        break;
 #ifdef USE_THREADS
+    case OP_ENTERITER:
+       if (!(o->op_flags & OPf_SPECIAL))
+           break;
+       /* FALL THROUGH */
     case OP_THREADSV:
        o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
        break;
@@ -600,6 +606,8 @@ op_free(OP *o)
     case OP_DBSTATE:
        Safefree(cCOPo->cop_label);
        SvREFCNT_dec(cCOPo->cop_filegv);
+       if (cCOPo->cop_warnings != WARN_NONE && cCOPo->cop_warnings != WARN_ALL)
+           SvREFCNT_dec(cCOPo->cop_warnings);
        break;
     case OP_CONST:
        SvREFCNT_dec(cSVOPo->op_sv);
@@ -685,15 +693,16 @@ scalarkids(OP *o)
 STATIC OP *
 scalarboolean(OP *o)
 {
-    if (PL_dowarn &&
-       o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
+    if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
        dTHR;
-       line_t oldline = PL_curcop->cop_line;
+       if (ckWARN(WARN_SYNTAX)) {
+           line_t oldline = PL_curcop->cop_line;
 
-       if (PL_copline != NOLINE)
-           PL_curcop->cop_line = PL_copline;
-       warn("Found = in conditional, should be ==");
-       PL_curcop->cop_line = oldline;
+           if (PL_copline != NOLINE)
+               PL_curcop->cop_line = PL_copline;
+           warner(WARN_SYNTAX, "Found = in conditional, should be ==");
+           PL_curcop->cop_line = oldline;
+       }
     }
     return scalar(o);
 }
@@ -880,15 +889,18 @@ scalarvoid(OP *o)
 
     case OP_CONST:
        sv = cSVOPo->op_sv;
-       if (PL_dowarn) {
-           useless = "a constant";
-           if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
-               useless = 0;
-           else if (SvPOK(sv)) {
-               if (strnEQ(SvPVX(sv), "di", 2) ||
-                   strnEQ(SvPVX(sv), "ds", 2) ||
-                   strnEQ(SvPVX(sv), "ig", 2))
-                       useless = 0;
+       {
+           dTHR;
+           if (ckWARN(WARN_VOID)) {
+               useless = "a constant";
+               if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
+                   useless = 0;
+               else if (SvPOK(sv)) {
+                   if (strnEQ(SvPVX(sv), "di", 2) ||
+                       strnEQ(SvPVX(sv), "ds", 2) ||
+                       strnEQ(SvPVX(sv), "ig", 2))
+                           useless = 0;
+               }
            }
        }
        null(o);                /* don't execute a constant */
@@ -947,8 +959,11 @@ scalarvoid(OP *o)
        }
        break;
     }
-    if (useless && PL_dowarn)
-       warn("Useless use of %s in void context", useless);
+    if (useless) {
+       dTHR;
+       if (ckWARN(WARN_VOID))
+           warner(WARN_VOID, "Useless use of %s in void context", useless);
+    }
     return o;
 }
 
@@ -1119,7 +1134,8 @@ mod(OP *o, I32 type)
        if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
            break;
        yyerror(form("Can't modify %s in %s",
-                    op_desc[o->op_type],
+                    (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
+                     ? "do block" : op_desc[o->op_type]),
                     type ? op_desc[type] : "local"));
        return o;
 
@@ -1248,7 +1264,9 @@ mod(OP *o, I32 type)
        break;
 
     case OP_NULL:
-       if (!(o->op_flags & OPf_KIDS))
+       if (o->op_flags & OPf_SPECIAL)          /* do BLOCK */
+           goto nomod;
+       else if (!(o->op_flags & OPf_KIDS))
            break;
        if (o->op_targ != OP_LIST) {
            mod(cBINOPo->op_first, type);
@@ -1456,20 +1474,23 @@ sawparens(OP *o)
 OP *
 bind_match(I32 type, OP *left, OP *right)
 {
+    dTHR;
     OP *o;
 
-    if (PL_dowarn &&
-       (left->op_type == OP_RV2AV ||
-        left->op_type == OP_RV2HV ||
-        left->op_type == OP_PADAV ||
-        left->op_type == OP_PADHV)) {
-       char *desc = op_desc[(right->op_type == OP_SUBST ||
-                             right->op_type == OP_TRANS)
-                            ? right->op_type : OP_MATCH];
-       char *sample = ((left->op_type == OP_RV2AV ||
-                        left->op_type == OP_PADAV)
-                       ? "@array" : "%hash");
-       warn("Applying %s to %s will act on scalar(%s)", desc, sample, sample);
+    if (ckWARN(WARN_UNSAFE) &&
+      (left->op_type == OP_RV2AV ||
+       left->op_type == OP_RV2HV ||
+       left->op_type == OP_PADAV ||
+       left->op_type == OP_PADHV)) {
+      char *desc = op_desc[(right->op_type == OP_SUBST ||
+                            right->op_type == OP_TRANS)
+                           ? right->op_type : OP_MATCH];
+      char *sample = ((left->op_type == OP_RV2AV ||
+                       left->op_type == OP_PADAV)
+                      ? "@array" : "%hash");
+      warner(WARN_UNSAFE,
+             "Applying %s to %s will act on scalar(%s)", 
+             desc, sample, sample);
     }
 
     if (right->op_type == OP_MATCH ||
@@ -1558,6 +1579,14 @@ block_start(int full)
     PL_pad_reset_pending = FALSE;
     SAVEHINTS();
     PL_hints &= ~HINT_BLOCK_SCOPE;
+    SAVEPPTR(compiling.cop_warnings); 
+    if (PL_compiling.cop_warnings != WARN_ALL && 
+       PL_compiling.cop_warnings != WARN_NONE) {
+        PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
+        SAVEFREESV(PL_compiling.cop_warnings) ;
+    }
+
+
     return retval;
 }
 
@@ -1629,11 +1658,13 @@ localize(OP *o, I32 lex)
     if (o->op_flags & OPf_PARENS)
        list(o);
     else {
-       if (PL_dowarn && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
+       dTHR;
+       if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
            char *s;
            for (s = PL_bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
            if (*s == ';' || *s == '=')
-               warn("Parens missing around \"%s\" list", lex ? "my" : "local");
+               warner(WARN_PARENTHESIS, "Parens missing around \"%s\" list",
+                               lex ? "my" : "local");
        }
     }
     PL_in_my = FALSE;
@@ -2104,11 +2135,11 @@ pmtrans(OP *o, OP *expr, OP *repl)
                diff = val - nextmin;
                if (diff > 0) {
                    t = uv_to_utf8(tmpbuf,nextmin);
-                   sv_catpvn(transv, tmpbuf, t - tmpbuf);
+                   sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
                    if (diff > 1) {
                        t = uv_to_utf8(tmpbuf, val - 1);
                        sv_catpvn(transv, "\377", 1);
-                       sv_catpvn(transv, tmpbuf, t - tmpbuf);
+                       sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
                    }
                }
                if (*s == 0xff)
@@ -2117,18 +2148,27 @@ pmtrans(OP *o, OP *expr, OP *repl)
                    nextmin = val + 1;
            }
            t = uv_to_utf8(tmpbuf,nextmin);
-           sv_catpvn(transv, tmpbuf, t - tmpbuf);
+           sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
            t = uv_to_utf8(tmpbuf, 0x7fffffff);
            sv_catpvn(transv, "\377", 1);
-           sv_catpvn(transv, tmpbuf, t - tmpbuf);
-           t = SvPVX(transv);
+           sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
+           t = (U8*)SvPVX(transv);
            tlen = SvCUR(transv);
            tend = t + tlen;
        }
        else if (!rlen && !del) {
            r = t; rlen = tlen; rend = tend;
-           if (!squash && to_utf && from_utf)
-               o->op_private |= OPpTRANS_COUNTONLY;
+       }
+       if (!squash) {
+           if (to_utf && from_utf) {   /* only counting characters */
+               if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
+                   o->op_private |= OPpTRANS_IDENTICAL;
+           }
+           else {      /* straight latin-1 translation */
+               if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
+                   rlen == 4 && memEQ(r, "\0\377\303\277", 4))
+                   o->op_private |= OPpTRANS_IDENTICAL;
+           }
        }
 
        while (t < tend || tfirst <= tlast) {
@@ -2257,7 +2297,7 @@ pmtrans(OP *o, OP *expr, OP *repl)
        if (!rlen && !del) {
            r = t; rlen = tlen;
            if (!squash)
-               o->op_private |= OPpTRANS_COUNTONLY;
+               o->op_private |= OPpTRANS_IDENTICAL;
        }
        for (i = 0; i < 256; i++)
            tbl[i] = -1;
@@ -2816,6 +2856,12 @@ newSTATEOP(I32 flags, char *label, OP *o)
     }
     cop->cop_seq = seq;
     cop->cop_arybase = PL_curcop->cop_arybase;
+    if (PL_curcop->cop_warnings == WARN_NONE 
+       || PL_curcop->cop_warnings == WARN_ALL)
+        cop->cop_warnings = PL_curcop->cop_warnings ;
+    else 
+        cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
+
 
     if (PL_copline == NOLINE)
         cop->cop_line = PL_curcop->cop_line;
@@ -2896,8 +2942,9 @@ new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
        }
     }
     if (first->op_type == OP_CONST) {
-       if (PL_dowarn && (first->op_private & OPpCONST_BARE))
-           warn("Probable precedence problem on %s", op_desc[type]);
+       if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
+           warner(WARN_PRECEDENCE, "Probable precedence problem on %s", 
+                       op_desc[type]);
        if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
            op_free(first);
            *firstp = Nullop;
@@ -2915,7 +2962,7 @@ new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
        else
            scalar(other);
     }
-    else if (PL_dowarn && (first->op_flags & OPf_KIDS)) {
+    else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
        OP *k1 = ((UNOP*)first)->op_first;
        OP *k2 = k1->op_sibling;
        OPCODE warnop = 0;
@@ -2938,7 +2985,8 @@ new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
        if (warnop) {
            line_t oldline = PL_curcop->cop_line;
            PL_curcop->cop_line = PL_copline;
-           warn("Value of %s%s can be \"0\"; test with defined()",
+           warner(WARN_UNSAFE,
+                "Value of %s%s can be \"0\"; test with defined()",
                 op_desc[warnop],
                 ((warnop == OP_READLINE || warnop == OP_GLOB)
                  ? " construct" : "() operator"));
@@ -3687,14 +3735,16 @@ newSUB(I32 floor, OP *o, OP *proto, OP *block)
                croak("Can't redefine active sort subroutine %s", name);
            if(const_sv = cv_const_sv(cv))
                const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
-           if ((const_sv && const_changed) || PL_dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
+           if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE) 
+                                       && !(CvGV(cv) && GvSTASH(CvGV(cv))
                                        && HvNAME(GvSTASH(CvGV(cv)))
                                        && strEQ(HvNAME(GvSTASH(CvGV(cv))),
                                                 "autouse"))) {
                line_t oldline = PL_curcop->cop_line;
                PL_curcop->cop_line = PL_copline;
-               warn(const_sv ? "Constant subroutine %s redefined"
-                    : "Subroutine %s redefined", name);
+               warner(WARN_REDEFINE,
+                       const_sv ? "Constant subroutine %s redefined"
+                                : "Subroutine %s redefined", name);
                PL_curcop->cop_line = oldline;
            }
            SvREFCNT_dec(cv);
@@ -3915,12 +3965,12 @@ newXS(char *name, void (*subaddr) (CV * _CPERLproto), char *filename)
        }
        else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
            /* already defined (or promised) */
-           if (PL_dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
+           if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
                            && HvNAME(GvSTASH(CvGV(cv)))
                            && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
                line_t oldline = PL_curcop->cop_line;
                PL_curcop->cop_line = PL_copline;
-               warn("Subroutine %s redefined",name);
+               warner(WARN_REDEFINE, "Subroutine %s redefined",name);
                PL_curcop->cop_line = oldline;
            }
            SvREFCNT_dec(cv);
@@ -3971,6 +4021,7 @@ newXS(char *name, void (*subaddr) (CV * _CPERLproto), char *filename)
            if (!PL_initav)
                PL_initav = newAV();
            av_push(PL_initav, (SV *)cv);
+           GvCV(gv) = 0;
        }
     }
     else
@@ -3995,11 +4046,11 @@ newFORM(I32 floor, OP *o, OP *block)
     gv = gv_fetchpv(name,TRUE, SVt_PVFM);
     GvMULTI_on(gv);
     if (cv = GvFORM(gv)) {
-       if (PL_dowarn) {
+       if (ckWARN(WARN_REDEFINE)) {
            line_t oldline = PL_curcop->cop_line;
 
            PL_curcop->cop_line = PL_copline;
-           warn("Format %s redefined",name);
+           warner(WARN_REDEFINE, "Format %s redefined",name);
            PL_curcop->cop_line = oldline;
        }
        SvREFCNT_dec(cv);
@@ -4051,7 +4102,7 @@ oopsAV(OP *o)
     case OP_PADSV:
        o->op_type = OP_PADAV;
        o->op_ppaddr = ppaddr[OP_PADAV];
-       return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
+       return ref(o, OP_RV2AV);
        
     case OP_RV2SV:
        o->op_type = OP_RV2AV;
@@ -4074,7 +4125,7 @@ oopsHV(OP *o)
     case OP_PADAV:
        o->op_type = OP_PADHV;
        o->op_ppaddr = ppaddr[OP_PADHV];
-       return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
+       return ref(o, OP_RV2HV);
 
     case OP_RV2SV:
     case OP_RV2AV:
@@ -4467,8 +4518,9 @@ ck_fun(OP *o)
                    char *name = SvPVx(((SVOP*)kid)->op_sv, PL_na);
                    OP *newop = newAVREF(newGVOP(OP_GV, 0,
                        gv_fetchpv(name, TRUE, SVt_PVAV) ));
-                   if (PL_dowarn)
-                       warn("Array @%s missing the @ in argument %ld of %s()",
+                   if (ckWARN(WARN_SYNTAX))
+                       warner(WARN_SYNTAX,
+                           "Array @%s missing the @ in argument %ld of %s()",
                            name, (long)numargs, op_desc[type]);
                    op_free(kid);
                    kid = newop;
@@ -4485,8 +4537,9 @@ ck_fun(OP *o)
                    char *name = SvPVx(((SVOP*)kid)->op_sv, PL_na);
                    OP *newop = newHVREF(newGVOP(OP_GV, 0,
                        gv_fetchpv(name, TRUE, SVt_PVHV) ));
-                   if (PL_dowarn)
-                       warn("Hash %%%s missing the %% in argument %ld of %s()",
+                   if (ckWARN(WARN_SYNTAX))
+                       warner(WARN_SYNTAX,
+                           "Hash %%%s missing the %% in argument %ld of %s()",
                            name, (long)numargs, op_desc[type]);
                    op_free(kid);
                    kid = newop;
@@ -4645,6 +4698,8 @@ ck_index(OP *o)
 {
     if (o->op_flags & OPf_KIDS) {
        OP *kid = cLISTOPo->op_first->op_sibling;       /* get past pushmark */
+       if (kid)
+           kid = kid->op_sibling;                      /* get past "big" */
        if (kid && kid->op_type == OP_CONST)
            fbm_compile(((SVOP*)kid)->op_sv, 0);
     }
@@ -5187,24 +5242,6 @@ peep(register OP *o)
            o->op_seq = PL_op_seqmax++;
            break;
 
-       case OP_PADAV:
-           if (o->op_next->op_type == OP_RV2AV
-               && (o->op_next->op_flags & OPf_REF))
-           {
-               null(o->op_next);
-               o->op_next = o->op_next->op_next;
-           }
-           break;
-       
-       case OP_PADHV:
-           if (o->op_next->op_type == OP_RV2HV
-               && (o->op_next->op_flags & OPf_REF))
-           {
-               null(o->op_next);
-               o->op_next = o->op_next->op_next;
-           }
-           break;
-
        case OP_MAPWHILE:
        case OP_GREPWHILE:
        case OP_AND:
@@ -5237,7 +5274,8 @@ peep(register OP *o)
 
        case OP_EXEC:
            o->op_seq = PL_op_seqmax++;
-           if (PL_dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
+           if (ckWARN(WARN_SYNTAX) && o->op_next 
+               && o->op_next->op_type == OP_NEXTSTATE) {
                if (o->op_next->op_sibling &&
                        o->op_next->op_sibling->op_type != OP_EXIT &&
                        o->op_next->op_sibling->op_type != OP_WARN &&
@@ -5245,8 +5283,8 @@ peep(register OP *o)
                    line_t oldline = PL_curcop->cop_line;
 
                    PL_curcop->cop_line = ((COP*)o->op_next)->cop_line;
-                   warn("Statement unlikely to be reached");
-                   warn("(Maybe you meant system() when you said exec()?)\n");
+                   warner(WARN_SYNTAX, "Statement unlikely to be reached");
+                   warner(WARN_SYNTAX, "(Maybe you meant system() when you said exec()?)\n");
                    PL_curcop->cop_line = oldline;
                }
            }