| | @@ -62,7 +62,7 @@ |
| 62 | 62 | MaxNames = 64; (* names in one declaration, and the like *) |
| 63 | 63 | MaxFwd = 256; (* pointers to types not yet declared *) |
| 64 | 64 | MaxDefers = 64; (* defer statements in force *) |
| 65 | | - MaxDrops = 4096; (* calls that drop results, in one module *) |
| 65 | + MaxDoubts = 4096; (* places to warn of, noted while a module is read *) |
| 66 | 66 | MaxDeferred = 64; (* generic imports not yet made *) |
| 67 | 67 | MaxLabels = 1024; (* labels of one match *) |
| 68 | 68 | MaxElements = 1024; (* elements of an array literal *) |
| | @@ -108,12 +108,15 @@ |
| 108 | 108 | stray: BOOL; (* an "end" in this procedure stood left of its statement's line *) |
| 109 | 109 | strayAt: Diag.Pos; (* ... the first such statement *) |
| 110 | 110 | elseIf: BOOL; (* an "else if" in this procedure *) |
| 111 | | - drops: [MaxDrops]record |
| 112 | | - at: Diag.Pos; (* a call standing on its own that drops results *) |
| 113 | | - n: INT; (* ... how many *) |
| 111 | + doubts: [MaxDoubts]record |
| 112 | + at: Diag.Pos; (* a place the module's end warns of *) |
| 113 | + n: INT; (* a call standing on its own that drops this many results, or *) |
| 114 | + msg, hint: []U8; (* any other doubt, in words (msg is empty for a drop) *) |
| 114 | 115 | end; |
| 115 | | - numDrops: INT; (* the module being compiled has those from its |
| 116 | + numDoubts: INT; (* the module being compiled has those from its |
| 116 | 117 | first one, saved by compileFile, to here *) |
| 118 | + bareBoolRel: BOOL; (* the relation just read is an = or # of two BOOLs, |
| 119 | + not in parentheses *) |
| 117 | 120 | ends: BOOL; (* the statements just read never go on past |
| 118 | 121 | their end: each way through them returns *) |
| 119 | 122 | useAt: Diag.Pos; (* ... which is here *) |
| | @@ -1548,6 +1551,33 @@ |
| 1548 | 1551 | return nil; |
| 1549 | 1552 | end; |
| 1550 | 1553 | |
| 1554 | +(* A doubt other than a drop, to be warned of at p with msg and hint. |
| 1555 | + Text read twice, as a while's condition is, is noted once. *) |
| 1556 | +proc noteDoubt(p: Diag.Pos; msg, hint: []U8); |
| 1557 | +var i: INT; |
| 1558 | +begin |
| 1559 | + for i := 0 to numDoubts - 1 do |
| 1560 | + if (doubts[i].at.f = p.f) and (doubts[i].at.a = p.a) then return; end; |
| 1561 | + end; |
| 1562 | + if numDoubts < MaxDoubts then |
| 1563 | + doubts[numDoubts].at := p; |
| 1564 | + doubts[numDoubts].msg := msg; |
| 1565 | + doubts[numDoubts].hint := hint; |
| 1566 | + INC(numDoubts); |
| 1567 | + end; |
| 1568 | +end; |
| 1569 | + |
| 1570 | +(* Notes the string at the scanner if it has a \0 before a digit *) |
| 1571 | +proc noteZeroDigit; |
| 1572 | +var at: Diag.Pos; yes: BOOL; |
| 1573 | +begin |
| 1574 | + at, yes := Scan.zeroDigit(); |
| 1575 | + if yes then |
| 1576 | + noteDoubt(at, "\\0 is a zero byte and the digit after it is itself", |
| 1577 | + "write \\x00 to keep them apart"); |
| 1578 | + end; |
| 1579 | +end; |
| 1580 | + |
| 1551 | 1581 | proc factor(var x: TItem; want: PType): ERROR; |
| 1552 | 1582 | var called, vol, several: BOOL; t: PType; start: INT; s: []U8; |
| 1553 | 1583 | begin |
| | @@ -1565,6 +1595,7 @@ |
| 1565 | 1595 | Scan.next; |
| 1566 | 1596 | | sString: |
| 1567 | 1597 | Item.makeStr(x); |
| 1598 | + noteZeroDigit(); |
| 1568 | 1599 | Scan.next; |
| 1569 | 1600 | | sIdent: |
| 1570 | 1601 | try designator(x, called); |
| | @@ -1684,33 +1715,54 @@ |
| 1684 | 1715 | return nil; |
| 1685 | 1716 | end; |
| 1686 | 1717 | |
| 1718 | +(* A relation. It sets bareBoolRel, for the and or or that may follow: |
| 1719 | + a relation binds tighter than and and or, so the # in "a # b and c" |
| 1720 | + is read first, as (a # b) and c, which a Pascal reader doesn't expect. *) |
| 1687 | 1721 | proc relation(var x: TItem; want: PType): ERROR; |
| 1688 | | -var op: Scan.Sym; y: TItem; L: INT; |
| 1722 | +var op: Scan.Sym; y: TItem; L: INT; bools: BOOL; |
| 1689 | 1723 | begin |
| 1690 | 1724 | try simpleExpr(x, want); |
| 1725 | + bools := false; |
| 1691 | 1726 | if isRelOp() then |
| 1692 | 1727 | op := Scan.tok; |
| 1693 | 1728 | Scan.next; |
| 1694 | 1729 | try Item.op1(op, x, L); |
| 1695 | 1730 | if op = Scan.sIn then try simpleExpr(y, nil); else try simpleExpr(y, x.typ); end; |
| 1731 | + bools := ((op = Scan.sEql) or (op = Scan.sNeq)) and (x.typ = Sym.tBool) and (y.typ = Sym.tBool); |
| 1696 | 1732 | try Item.op2(op, x, y, L); |
| 1697 | 1733 | x.pos := Diag.join(x.pos, y.pos); |
| 1698 | 1734 | end; |
| 1735 | + bareBoolRel := bools; (* a relation in parentheses comes here as a factor, and sets it false *) |
| 1699 | 1736 | return nil; |
| 1700 | 1737 | end; |
| 1701 | 1738 | |
| 1739 | +(* Notes x, an operand of the and or or (word) just read, if it is an |
| 1740 | + unparenthesised = or # of two BOOLs *) |
| 1741 | +proc noteBoolRel(const x: TItem; word: []U8); |
| 1742 | +begin |
| 1743 | + if bareBoolRel then |
| 1744 | + noteDoubt(x.pos, say2("{} is read first: = and # bind tighter than {}", Diag.source(x.pos), word), |
| 1745 | + "put the = or # of two BOOLs in parentheses"); |
| 1746 | + end; |
| 1747 | +end; |
| 1748 | + |
| 1702 | 1749 | (* and binds tighter than or, and both below the relations *) |
| 1703 | 1750 | proc conjunct(var x: TItem; want: PType): ERROR; |
| 1704 | 1751 | var y: TItem; L: INT; start: INT; |
| 1705 | 1752 | begin |
| 1706 | 1753 | start := Scan.tokStart(); |
| 1707 | 1754 | try relation(x, want); |
| 1708 | | - while Scan.tok = Scan.sAnd do |
| 1709 | | - Scan.next; |
| 1710 | | - try Item.op1(Scan.sAnd, x, L); |
| 1711 | | - try relation(y, nil); |
| 1712 | | - try Item.op2(Scan.sAnd, x, y, L); |
| 1713 | | - x.pos := Scan.spanFromAt(start); |
| 1755 | + if Scan.tok = Scan.sAnd then |
| 1756 | + noteBoolRel(x, "and"); |
| 1757 | + repeat |
| 1758 | + Scan.next; |
| 1759 | + try Item.op1(Scan.sAnd, x, L); |
| 1760 | + try relation(y, nil); |
| 1761 | + noteBoolRel(y, "and"); |
| 1762 | + try Item.op2(Scan.sAnd, x, y, L); |
| 1763 | + x.pos := Scan.spanFromAt(start); |
| 1764 | + until Scan.tok # Scan.sAnd; |
| 1765 | + bareBoolRel := false; (* x is a conjunction now *) |
| 1714 | 1766 | end; |
| 1715 | 1767 | return nil; |
| 1716 | 1768 | end; |
| | @@ -1720,12 +1772,17 @@ |
| 1720 | 1772 | begin |
| 1721 | 1773 | start := Scan.tokStart(); |
| 1722 | 1774 | try conjunct(x, want); |
| 1723 | | - while Scan.tok = Scan.sOr do |
| 1724 | | - Scan.next; |
| 1725 | | - try Item.op1(Scan.sOr, x, L); |
| 1726 | | - try conjunct(y, nil); |
| 1727 | | - try Item.op2(Scan.sOr, x, y, L); |
| 1728 | | - x.pos := Scan.spanFromAt(start); |
| 1775 | + if Scan.tok = Scan.sOr then |
| 1776 | + noteBoolRel(x, "or"); |
| 1777 | + repeat |
| 1778 | + Scan.next; |
| 1779 | + try Item.op1(Scan.sOr, x, L); |
| 1780 | + try conjunct(y, nil); |
| 1781 | + noteBoolRel(y, "or"); |
| 1782 | + try Item.op2(Scan.sOr, x, y, L); |
| 1783 | + x.pos := Scan.spanFromAt(start); |
| 1784 | + until Scan.tok # Scan.sOr; |
| 1785 | + bareBoolRel := false; (* x is a disjunction now *) |
| 1729 | 1786 | end; |
| 1730 | 1787 | return nil; |
| 1731 | 1788 | end; |
| | @@ -2464,10 +2521,11 @@ |
| 2464 | 2521 | to be warned of *) |
| 2465 | 2522 | proc noteDrop(const x: TItem); |
| 2466 | 2523 | begin |
| 2467 | | - if (x.mode # Item.mNone) and (numDrops < MaxDrops) then |
| 2468 | | - drops[numDrops].at := x.pos; |
| 2469 | | - if x.typ^.tuple then drops[numDrops].n := INT(x.typ^.len); else drops[numDrops].n := 1; end; |
| 2470 | | - INC(numDrops); |
| 2524 | + if (x.mode # Item.mNone) and (numDoubts < MaxDoubts) then |
| 2525 | + doubts[numDoubts].at := x.pos; |
| 2526 | + doubts[numDoubts].msg := ""; |
| 2527 | + if x.typ^.tuple then doubts[numDoubts].n := INT(x.typ^.len); else doubts[numDoubts].n := 1; end; |
| 2528 | + INC(numDoubts); |
| 2471 | 2529 | end; |
| 2472 | 2530 | end; |
| 2473 | 2531 | |
| | @@ -2955,7 +3013,7 @@ |
| 2955 | 3013 | proc body(p: PObj; sig: PType; line: INT): ERROR; |
| 2956 | 3014 | var |
| 2957 | 3015 | q, params, last, o, before: PObj; old: PType; st: Scan.State; |
| 2958 | | - mark: Gen.CodeMark; drops0, errs: INT; |
| 3016 | + mark: Gen.CodeMark; doubts0, errs: INT; |
| 2959 | 3017 | begin |
| 2960 | 3018 | try Sym.openScope(Sym.newScope()); |
| 2961 | 3019 | params := nil; last := nil; |
| | @@ -2986,7 +3044,7 @@ |
| 2986 | 3044 | placeLocals(params, before); |
| 2987 | 3045 | (* the body, made again if a variable in a register must be in memory *) |
| 2988 | 3046 | Scan.saveScan(st); |
| 2989 | | - Gen.markCode(mark); drops0 := numDrops; errs := Diag.errors; |
| 3047 | + Gen.markCode(mark); doubts0 := numDoubts; errs := Diag.errors; |
| 2990 | 3048 | old := curResult; |
| 2991 | 3049 | repeat |
| 2992 | 3050 | Item.setDemoted(false); |
| | @@ -3008,7 +3066,7 @@ |
| 3008 | 3066 | o := o^.next; |
| 3009 | 3067 | end; |
| 3010 | 3068 | end; |
| 3011 | | - numDrops := drops0; |
| 3069 | + numDoubts := doubts0; |
| 3012 | 3070 | Gen.redo(mark); |
| 3013 | 3071 | placeLocals(params, before); |
| 3014 | 3072 | Scan.restoreScan(st); |
| | @@ -3798,20 +3856,25 @@ |
| 3798 | 3856 | |
| 3799 | 3857 | (* The warnings of module m, by where each declaration is: its own |
| 3800 | 3858 | declarations and its procedures' are two lists, each in order, and |
| 3801 | | - firstDrop on are the calls that drop results. The |
| 3859 | + firstDoubt on are the calls that drop results and the other doubts. The |
| 3802 | 3860 | instances of a generic module share its text, and so its warnings: |
| 3803 | 3861 | only the first is warned of. *) |
| 3804 | | -proc warnings(m: PModule; firstDrop: INT); |
| 3862 | +proc warnings(m: PModule; firstDoubt: INT); |
| 3805 | 3863 | var a, b: PObj; n: PModule; i: INT; |
| 3806 | 3864 | begin |
| 3807 | 3865 | n := firstModule; |
| 3808 | 3866 | while (n # nil) and (n^.name # m^.name) do n := n^.next; end; |
| 3809 | 3867 | if n # nil then return; end; |
| 3810 | 3868 | a := m^.first; b := m^.locals; |
| 3811 | | - i := firstDrop; |
| 3812 | | - while (a # nil) or (b # nil) or (i < numDrops) do |
| 3813 | | - if (i < numDrops) and ((a = nil) or (drops[i].at.a < a^.pos.a)) and ((b = nil) or (drops[i].at.a < b^.pos.a)) then |
| 3814 | | - dropped(drops[i].at, drops[i].n); |
| 3869 | + i := firstDoubt; |
| 3870 | + while (a # nil) or (b # nil) or (i < numDoubts) do |
| 3871 | + if (i < numDoubts) and ((a = nil) or (doubts[i].at.a < a^.pos.a)) and ((b = nil) or (doubts[i].at.a < b^.pos.a)) then |
| 3872 | + if LEN(doubts[i].msg) = 0 then |
| 3873 | + dropped(doubts[i].at, doubts[i].n); |
| 3874 | + else |
| 3875 | + Diag.warning(doubts[i].at, doubts[i].msg); |
| 3876 | + Diag.help(doubts[i].hint); |
| 3877 | + end; |
| 3815 | 3878 | INC(i); |
| 3816 | 3879 | elsif (b = nil) or ((a # nil) and (a^.pos.a < b^.pos.a)) then |
| 3817 | 3880 | warn(a); |
| | @@ -3869,9 +3932,9 @@ |
| 3869 | 3932 | st: Scan.State; saved: [Sym.MaxDepth]INT; d: INT; |
| 3870 | 3933 | oldMod: PModule; oldCheck, oldChecks: BOOL; m: PModule; |
| 3871 | 3934 | lib: ^Sym.Lib; start: Diag.Pos; alias, mname, line, oldPath: INT; o: PObj; bodyChunk: INT; iargs: Sym.Args; |
| 3872 | | - generic: BOOL; argsAt: Scan.State; why: []U8; firstDrop: INT; |
| 3935 | + generic: BOOL; argsAt: Scan.State; why: []U8; firstDoubt: INT; |
| 3873 | 3936 | begin |
| 3874 | | - firstDrop := numDrops; |
| 3937 | + firstDoubt := numDoubts; |
| 3875 | 3938 | Scan.saveScan(st); |
| 3876 | 3939 | d := Sym.saveScopes(saved[0..Sym.MaxDepth]); |
| 3877 | 3940 | oldMod := Sym.curModule; oldCheck := checkMode; oldPath := Chunk.curFilePath(); oldChecks := Item.checks; |
| | @@ -4020,8 +4083,8 @@ |
| 4020 | 4083 | end; |
| 4021 | 4084 | o := o^.chain; |
| 4022 | 4085 | end; |
| 4023 | | - if (Diag.errors = 0) and not checkOnly then warnings(m, firstDrop); end; |
| 4024 | | - numDrops := firstDrop; |
| 4086 | + if (Diag.errors = 0) and not checkOnly then warnings(m, firstDoubt); end; |
| 4087 | + numDoubts := firstDoubt; |
| 4025 | 4088 | if Diag.errors > 0 then Diag.finish(); end; (* a module with errors stops the compiler after it *) |
| 4026 | 4089 | Sym.closeScope(); |
| 4027 | 4090 | m^.done := true; |