Warnings: what is never used, or only assigned; make check builds with -Werror Objs are marked used and read where a name finds them; a var named whole before := stays unread. At a module's end, if it had no errors, its own decls and its procs' are merged by pos and warned of: an import, a const, type, var or proc not exported, a param, a var only assigned. _x never. A generic module is warned of once. lib/, examples/ and tools/ cleared (callbacks' params, results thrown away take a _); test/err takes X.args, for test/err/Unused. The summary is printed once.
| @@ -58,7 +58,7 @@ | ||
| 58 | 58 | 2026-10.2, approved): the terminal, files, directories and the |
| 59 | 59 | environment. `tools/Scan.raz` is theirs to use, as a tool, not part of |
| 60 | 60 | the library. Stage 4, diagnostics (steps 19-27), is under way: |
| 61 | -steps 19-24 are done. New work starts only on the owner's word. Keep | |
| 61 | +steps 19-25 are done. New work starts only on the owner's word. Keep | |
| 62 | 62 | `doc/compiler.md`'s table of lines and its routine names true when the |
| 63 | 63 | compiler changes. Left for later at the owner's word: a rewrite of |
| 64 | 64 | var parameters. |
| @@ -258,7 +258,7 @@ | ||
| 258 | 258 | for `:=`, `&&`, `||`, `!`, `elif`, a missing `then` or `do`, a |
| 259 | 259 | missing import. The machinery, *main*; the catalogue of slips |
| 260 | 260 | and their tests, *agent*. |
| 261 | -- [ ] 25. Warnings: an unused import; an unused constant, type, | |
| 261 | +- [x] 25. Warnings: an unused import; an unused constant, type, | |
| 262 | 262 | variable or procedure that isn't exported; an unused parameter |
| 263 | 263 | of a procedure written in raz; a variable assigned but never |
| 264 | 264 | read. A name starting with `_` is never warned about. `-Werror` |
| @@ -10,13 +10,13 @@ | ||
| 10 | 10 | |---|---|---|---| |
| 11 | 11 | | `src/diag.pas` | 267 | 191 | Messages: positions, source excerpts, colour, counts | |
| 12 | 12 | | `src/scan.pas` | 687 | 553 | Symbols, literals, comments, positions | |
| 13 | -| `src/sym.pas` | 578 | 479 | Types, objects, scopes, layout | | |
| 13 | +| `src/sym.pas` | 583 | 483 | Types, objects, scopes, layout | | |
| 14 | 14 | | `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder | |
| 15 | 15 | | `src/gen.pas` | 1,515 | 1,319 | Items: loading, storing, copying, frames, calls | |
| 16 | -| `src/parse.pas` | 2,151 | 1,955 | Declarations, statements, expressions, built-ins, recovery | | |
| 16 | +| `src/parse.pas` | 2,241 | 2,028 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 17 | 17 | | `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | |
| 18 | 18 | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | |
| 19 | -| `src/raz.pas` | 220 | 173 | The command line, the entry points | | |
| 19 | +| `src/raz.pas` | 221 | 174 | The command line, the entry points | | |
| 20 | 20 | |
| 21 | 21 | Every file opens with a comment that says what it is, what it depends |
| 22 | 22 | on and what it leaves out. Read those first; they are the map. |
| @@ -346,6 +346,17 @@ | ||
| 346 | 346 | aren't reported, since they follow from one that was. Nothing breaks |
| 347 | 347 | for the half-made code: once there is an error, no executable is |
| 348 | 348 | written. |
| 349 | + | |
| 350 | +**Warnings.** `Declare` keeps each module's declarations in order in | |
| 351 | +two lists, through `link`: its own and its procedures', parameters | |
| 352 | +and a match's names for fields among them. `Named` marks an object | |
| 353 | +`used` and `read` wherever a name finds it, except a procedure naming | |
| 354 | +itself; `LeftSide` puts `read` back as it was for a variable named | |
| 355 | +whole before `:=`, which is written, not read. At the end of a module | |
| 356 | +with no errors, `Warnings` merges the two lists by position and `Warn` | |
| 357 | +reports what isn't exported, doesn't start with `_`, and was never | |
| 358 | +used, or, for a variable, only assigned. A generic module's instances | |
| 359 | +share its text, so only the first is warned of. | |
| 349 | 360 | |
| 350 | 361 | The built-in procedures are in `Builtin` (and `New_`, `Atomic`): each |
| 351 | 362 | parses its own arguments, since `SIZE` takes a type and `INC` a |
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | proc main*; |
| 10 | 10 | var |
| 11 | 11 | counts: Count.Map; order: Names.List; n: ^Names.Node; |
| 12 | - i, start, k, total: I64; found, ok: BOOL; w: []U8; | |
| 12 | + i, start, k, total: I64; found, _ok: BOOL; w: []U8; | |
| 13 | 13 | begin |
| 14 | 14 | Count.init(ADR(counts), Heap.heap); |
| 15 | 15 | Names.init(ADR(order), Heap.heap); |
| @@ -19,8 +19,8 @@ | ||
| 19 | 19 | w := Text[start..i]; |
| 20 | 20 | INC(total); |
| 21 | 21 | k, found := Count.get(ADR(counts), w); |
| 22 | - if not found then ok := Names.push(ADR(order), w); end; | |
| 23 | - ok := Count.put(ADR(counts), w, k + 1); | |
| 22 | + if not found then _ok := Names.push(ADR(order), w); end; | |
| 23 | + _ok := Count.put(ADR(counts), w, k + 1); | |
| 24 | 24 | start := i + 1; |
| 25 | 25 | end; |
| 26 | 26 | end; |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | return s; |
| 22 | 22 | end; |
| 23 | 23 | |
| 24 | -proc work(arg: ^U8); | |
| 24 | +proc work(_arg: ^U8); | |
| 25 | 25 | var n, r: I64; |
| 26 | 26 | begin |
| 27 | 27 | n := XADD(ADR(next), 1); |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | return ar^.buf + at; |
| 22 | 22 | end; |
| 23 | 23 | |
| 24 | -proc free(a: ^Mem.Allocator; p: ^U8); | |
| 24 | +proc free(_a: ^Mem.Allocator; _p: ^U8); | |
| 25 | 25 | begin |
| 26 | 26 | end; |
| 27 | 27 |
| @@ -40,7 +40,7 @@ | ||
| 40 | 40 | |
| 41 | 41 | (* size bytes aligned to align, or nil: also for a size below zero or |
| 42 | 42 | too big, and an alignment that isn't a power of two up to a page. *) |
| 43 | -proc alloc(a: ^Mem.Allocator; size, align: I64): ^U8; | |
| 43 | +proc alloc(_a: ^Mem.Allocator; size, align: I64): ^U8; | |
| 44 | 44 | var c, n: I64; b: ^Block; p: ^U8; |
| 45 | 45 | begin |
| 46 | 46 | if (size < 0) or (size > Largest) or (align < 1) or (align > Mem.Page) |
| @@ -77,7 +77,7 @@ | ||
| 77 | 77 | return give(p, c); |
| 78 | 78 | end; |
| 79 | 79 | |
| 80 | -proc free(a: ^Mem.Allocator; p: ^U8); | |
| 80 | +proc free(_a: ^Mem.Allocator; p: ^U8); | |
| 81 | 81 | var h: ^Head; b: ^Block; c: I64; |
| 82 | 82 | begin |
| 83 | 83 | if p = nil then return; end; |
| @@ -60,9 +60,9 @@ | ||
| 60 | 60 | end; |
| 61 | 61 | |
| 62 | 62 | proc clear*(l: ^List); |
| 63 | -var ok: BOOL; v: T; | |
| 63 | +var _ok: BOOL; _v: T; | |
| 64 | 64 | begin |
| 65 | - while l^.head # nil do v, ok := pop(l); end; | |
| 65 | + while l^.head # nil do _v, _ok := pop(l); end; | |
| 66 | 66 | end; |
| 67 | 67 | |
| 68 | 68 | end List. |
| @@ -128,7 +128,7 @@ | ||
| 128 | 128 | testCount: I64; |
| 129 | 129 | wave: [1000]^Thread; |
| 130 | 130 | |
| 131 | -proc testWork(arg: ^U8); | |
| 131 | +proc testWork(_arg: ^U8); | |
| 132 | 132 | begin |
| 133 | 133 | lock(ADR(testLock)); |
| 134 | 134 | INC(testCount); |
| @@ -55,6 +55,7 @@ | ||
| 55 | 55 | fwdPos: array[0..255] of TPos; { where each names its type } |
| 56 | 56 | numFwd: longint; |
| 57 | 57 | lastCalled: PObj; { the procedure the last call was of, if it had a name } |
| 58 | + curProc: PObj; { the procedure being compiled, or nil } | |
| 58 | 59 | declExported: boolean; { the export mark of the declaration being read } |
| 59 | 60 | curLoop: PLoop; { the innermost loop, or nil } |
| 60 | 61 | defers: array[0..63] of longint; { the deferred statements in force } |
| @@ -110,8 +111,9 @@ | ||
| 110 | 111 | end; |
| 111 | 112 | end; |
| 112 | 113 | |
| 113 | -{ Declares o in the innermost scope; a module's own declarations are | |
| 114 | - also kept in order, for the interface check. } | |
| 114 | +{ Declares o in the innermost scope. Its module keeps its declarations | |
| 115 | + in order: its own, for the interface check, and its procedures', to | |
| 116 | + warn of those never used. } | |
| 115 | 117 | procedure Declare(o: PObj); |
| 116 | 118 | begin |
| 117 | 119 | Insert(o, TopScope); |
| @@ -119,9 +121,22 @@ | ||
| 119 | 121 | if curModule^.last = nil then curModule^.first := o |
| 120 | 122 | else curModule^.last^.link := o; |
| 121 | 123 | curModule^.last := o; |
| 124 | + end else begin | |
| 125 | + if curModule^.lastLocal = nil then curModule^.locals := o | |
| 126 | + else curModule^.lastLocal^.link := o; | |
| 127 | + curModule^.lastLocal := o; | |
| 122 | 128 | end; |
| 123 | 129 | end; |
| 124 | 130 | |
| 131 | +{ o is named: a procedure naming itself doesn't count } | |
| 132 | +procedure Named(o: PObj); | |
| 133 | +begin | |
| 134 | + if o <> curProc then begin | |
| 135 | + o^.used := true; | |
| 136 | + o^.read := true; | |
| 137 | + end; | |
| 138 | +end; | |
| 139 | + | |
| 125 | 140 | { Recovery |
| 126 | 141 | |
| 127 | 142 | After an error the parser goes on with the next statement or |
| @@ -284,6 +299,7 @@ | ||
| 284 | 299 | Fail; |
| 285 | 300 | end; |
| 286 | 301 | if o^.typ = tPoison then quiet := true; |
| 302 | + Named(o); | |
| 287 | 303 | Next; |
| 288 | 304 | if o^.cls = oModule then begin |
| 289 | 305 | Expect(sPeriod); |
| @@ -516,6 +532,7 @@ | ||
| 516 | 532 | o := NewObj(oConst, id, t); |
| 517 | 533 | o^.val := t^.len; |
| 518 | 534 | o^.exported := declExported; |
| 535 | + o^.used := true; { an enumeration's names are its type's, warned of with it } | |
| 519 | 536 | Declare(o); |
| 520 | 537 | if last = nil then t^.fields := o else last^.next := o; |
| 521 | 538 | last := o; |
| @@ -1177,6 +1194,7 @@ | ||
| 1177 | 1194 | if tok <> sIdent then Expected('a name'); |
| 1178 | 1195 | o := Lookup(id); |
| 1179 | 1196 | if o = nil then begin NotDeclared(TokPos, id); Fail end; |
| 1197 | + Named(o); | |
| 1180 | 1198 | if (o^.cls <> oVar) or not (o^.typ^.form in [fInt, fEnum]) then |
| 1181 | 1199 | if o^.cls = oVar then |
| 1182 | 1200 | Mark('a for loop needs an integer or enumeration variable, found ' + TypeName(o^.typ)) |
| @@ -1261,7 +1279,7 @@ | ||
| 1261 | 1279 | o := NewObj(oVar, id, f^.typ); |
| 1262 | 1280 | o^.lev := 1; o^.ind := true; o^.isVar := true; |
| 1263 | 1281 | o^.val := p; o^.off := f^.val; |
| 1264 | - Insert(o, TopScope); | |
| 1282 | + Declare(o); | |
| 1265 | 1283 | f := f^.next; |
| 1266 | 1284 | inc(n); |
| 1267 | 1285 | Next; |
| @@ -1299,6 +1317,20 @@ | ||
| 1299 | 1317 | else Subject := 'a value'; |
| 1300 | 1318 | end; |
| 1301 | 1319 | |
| 1320 | +{ The designator a statement starts with. A variable named whole | |
| 1321 | + before := is assigned, not read: naming it there leaves it as unread | |
| 1322 | + as it was. } | |
| 1323 | +procedure LeftSide(var x: TItem; var called: boolean); | |
| 1324 | +var o: PObj; wasRead: boolean; n: longint; | |
| 1325 | +begin | |
| 1326 | + n := id; | |
| 1327 | + o := Lookup(n); | |
| 1328 | + wasRead := (o <> nil) and o^.read; | |
| 1329 | + Designator(x, called); | |
| 1330 | + if (tok in [sBecomes, sComma]) and (x.pos.b - x.pos.a = length(NameOf(n))) then | |
| 1331 | + o^.read := wasRead; | |
| 1332 | +end; | |
| 1333 | + | |
| 1302 | 1334 | { a, b := f(x): the variables first, left to right, then the call; |
| 1303 | 1335 | then each result to its variable, in order. } |
| 1304 | 1336 | procedure MultiAssign(var x: TItem; first: longint); |
| @@ -1315,7 +1347,7 @@ | ||
| 1315 | 1347 | if n > High(d) then Mark('more than ' + IntStr(High(d) + 1) + ' variables in one assignment'); |
| 1316 | 1348 | Next; |
| 1317 | 1349 | dn[n] := id; |
| 1318 | - Designator(d[n], called); | |
| 1350 | + LeftSide(d[n], called); | |
| 1319 | 1351 | until false; |
| 1320 | 1352 | Expect(sBecomes); |
| 1321 | 1353 | Designator(y, called); |
| @@ -1378,7 +1410,7 @@ | ||
| 1378 | 1410 | m := TempMark; |
| 1379 | 1411 | case tok of |
| 1380 | 1412 | sIdent: begin |
| 1381 | - Designator(x, called); | |
| 1413 | + LeftSide(x, called); | |
| 1382 | 1414 | if tok = sBecomes then begin |
| 1383 | 1415 | if (x.mode <> mVar) or not x.isVar then MarkAt(x.pos, 'can''t assign to ' + Subject(x, first)); |
| 1384 | 1416 | Save(x); |
| @@ -1501,8 +1533,10 @@ | ||
| 1501 | 1533 | q := sig^.fields; |
| 1502 | 1534 | while q <> nil do begin |
| 1503 | 1535 | o := NewObj(oVar, q^.name, q^.typ); |
| 1536 | + o^.pos := q^.pos; | |
| 1504 | 1537 | o^.lev := 1; |
| 1505 | 1538 | o^.isVar := q^.isVar; |
| 1539 | + o^.param := true; | |
| 1506 | 1540 | if last = nil then params := o else last^.next := o; |
| 1507 | 1541 | last := o; |
| 1508 | 1542 | q := q^.next; |
| @@ -1512,9 +1546,10 @@ | ||
| 1512 | 1546 | o := params; |
| 1513 | 1547 | while o <> nil do begin |
| 1514 | 1548 | q := o^.next; |
| 1515 | - Insert(o, TopScope); | |
| 1549 | + Declare(o); | |
| 1516 | 1550 | o := q; |
| 1517 | 1551 | end; |
| 1552 | + curProc := p; | |
| 1518 | 1553 | Declarations(false); |
| 1519 | 1554 | Expect(sBegin); |
| 1520 | 1555 | old := curResult; |
| @@ -1525,6 +1560,7 @@ | ||
| 1525 | 1560 | Expect(sEnd); |
| 1526 | 1561 | EndProc(p^.chunk); |
| 1527 | 1562 | curResult := old; |
| 1563 | + curProc := nil; | |
| 1528 | 1564 | CloseScope; |
| 1529 | 1565 | end; |
| 1530 | 1566 | |
| @@ -1796,7 +1832,10 @@ | ||
| 1796 | 1832 | if (o = nil) or (o^.cls <> oType) then begin |
| 1797 | 1833 | NotDeclared(fwdPos[i], fwdTypes[i]^.fwd); |
| 1798 | 1834 | fwdTypes[i]^.base := tPoison; |
| 1799 | - end else fwdTypes[i]^.base := o^.typ; | |
| 1835 | + end else begin | |
| 1836 | + fwdTypes[i]^.base := o^.typ; | |
| 1837 | + Named(o); | |
| 1838 | + end; | |
| 1800 | 1839 | fwdTypes[i]^.fwd := -1; |
| 1801 | 1840 | end; |
| 1802 | 1841 | end; |
| @@ -1862,7 +1901,9 @@ | ||
| 1862 | 1901 | if tok <> sIdent then Expected('a name'); |
| 1863 | 1902 | if i >= m^.args.n then MarkAt(m^.args.at, NameOf(m^.name) + ' needs a type for ' + NameOf(id)); |
| 1864 | 1903 | if m^.args.o[i] <> nil then MarkAt(m^.args.pos[i], NameOf(m^.name) + ' needs a type for ' + NameOf(id)); |
| 1865 | - Declare(NewObj(oType, id, m^.args.t[i])); | |
| 1904 | + o := NewObj(oType, id, m^.args.t[i]); | |
| 1905 | + o^.used := true; { a parameter of the module, not a declaration } | |
| 1906 | + Declare(o); | |
| 1866 | 1907 | s := s + TypeName(m^.args.t[i]) + ','; |
| 1867 | 1908 | inc(i); |
| 1868 | 1909 | Next; |
| @@ -1887,6 +1928,7 @@ | ||
| 1887 | 1928 | Finish; |
| 1888 | 1929 | end; |
| 1889 | 1930 | o^.typ := a^.typ; o^.chunk := a^.chunk; o^.isCode := a^.isCode; o^.ext := a^.ext; |
| 1931 | + o^.used := true; | |
| 1890 | 1932 | Declare(o); |
| 1891 | 1933 | s := s + NameOf(a^.module^.name) + '.' + NameOf(a^.name) + ','; |
| 1892 | 1934 | inc(i); |
| @@ -1897,6 +1939,52 @@ | ||
| 1897 | 1939 | m^.sname := Name(s); |
| 1898 | 1940 | end; |
| 1899 | 1941 | |
| 1942 | +{ Warns of o if it is never used, or a variable only assigned. What | |
| 1943 | + is exported may be used elsewhere, and a name that starts with _ says | |
| 1944 | + it is meant to go unused. Writing a var parameter, a match's name for | |
| 1945 | + a field or C's variable is what it is for. } | |
| 1946 | +procedure Warn(o: PObj); | |
| 1947 | +var s: shortstring; | |
| 1948 | +begin | |
| 1949 | + s := NameOf(o^.name); | |
| 1950 | + if o^.exported or (s[1] = '_') then exit; | |
| 1951 | + if o^.used and (o^.read or (o^.cls <> oVar) or o^.isVar or o^.ext) then exit; | |
| 1952 | + case o^.cls of | |
| 1953 | + oConst: s := 'constant ' + s; | |
| 1954 | + oType: s := 'type ' + s; | |
| 1955 | + oVar: if o^.param then s := 'parameter ' + s else s := 'variable ' + s; | |
| 1956 | + oProc: s := 'procedure ' + s; | |
| 1957 | + oModule: s := 'module ' + s; | |
| 1958 | + else exit; | |
| 1959 | + end; | |
| 1960 | + if o^.used then Warning(o^.pos, s + ' is assigned but never read') | |
| 1961 | + else if o^.cls = oModule then Warning(o^.pos, s + ' is imported but never used') | |
| 1962 | + else Warning(o^.pos, s + ' is never used'); | |
| 1963 | + if not o^.used and (o^.param or o^.ind) then | |
| 1964 | + Help('if it must be there, call it _' + NameOf(o^.name)); | |
| 1965 | +end; | |
| 1966 | + | |
| 1967 | +{ The warnings of module m, by where each declaration is: its own | |
| 1968 | + declarations and its procedures' are two lists, each in order. The | |
| 1969 | + instances of a generic module share its text, and so its warnings: | |
| 1970 | + only the first is warned of. } | |
| 1971 | +procedure Warnings(m: PModule); | |
| 1972 | +var a, b: PObj; n: PModule; | |
| 1973 | +begin | |
| 1974 | + n := firstModule; | |
| 1975 | + while (n <> nil) and (n^.name <> m^.name) do n := n^.next; | |
| 1976 | + if n <> nil then exit; | |
| 1977 | + a := m^.first; b := m^.locals; | |
| 1978 | + while (a <> nil) or (b <> nil) do | |
| 1979 | + if (b = nil) or (a <> nil) and (a^.pos.a < b^.pos.a) then begin | |
| 1980 | + Warn(a); | |
| 1981 | + a := a^.link; | |
| 1982 | + end else begin | |
| 1983 | + Warn(b); | |
| 1984 | + b := b^.link; | |
| 1985 | + end; | |
| 1986 | +end; | |
| 1987 | + | |
| 1900 | 1988 | { Modules being compiled, not yet finished: for finding a cycle. } |
| 1901 | 1989 | var |
| 1902 | 1990 | pending: array[0..63] of PModule; |
| @@ -1964,6 +2052,7 @@ | ||
| 1964 | 2052 | iargs.at := SpanFrom(start); |
| 1965 | 2053 | im := ImportModule(mname, iargs); |
| 1966 | 2054 | o := NewObj(oModule, alias, nil); |
| 2055 | + o^.pos := start; | |
| 1967 | 2056 | o^.module := im; |
| 1968 | 2057 | Declare(o); |
| 1969 | 2058 | New(imp); |
| @@ -2014,6 +2103,7 @@ | ||
| 2014 | 2103 | if (o^.cls = oProc) and o^.fwd then Error(TokPos, NameOf(o^.name) + ' is declared forward but never defined'); |
| 2015 | 2104 | o := o^.link; |
| 2016 | 2105 | end; |
| 2106 | + if (errors = 0) and not checkOnly then Warnings(m); | |
| 2017 | 2107 | if errors > 0 then Finish; { a module with errors stops the compiler after it } |
| 2018 | 2108 | CloseScope; |
| 2019 | 2109 | m^.done := true; |
| @@ -142,7 +142,7 @@ | ||
| 142 | 142 | end; |
| 143 | 143 | |
| 144 | 144 | m := CompileMain(path); |
| 145 | - Finish; | |
| 145 | + if errors > 0 then Finish; | |
| 146 | 146 | |
| 147 | 147 | Use(m); |
| 148 | 148 | rt := firstModule; |
| @@ -155,9 +155,10 @@ | ||
| 155 | 155 | if (main^.cls <> oProc) or not main^.exported then Error(main^.pos, 'main must be an exported procedure') |
| 156 | 156 | else if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tI64)) then |
| 157 | 157 | Error(main^.pos, 'main must be proc main* or proc main*: I64'); |
| 158 | - Finish; | |
| 158 | + if errors > 0 then Finish; | |
| 159 | 159 | hasResult := main^.typ^.base <> nil; |
| 160 | 160 | end; |
| 161 | + Finish; | |
| 161 | 162 | |
| 162 | 163 | if rt = nil then Fatal('can''t find module Rt'); |
| 163 | 164 | dynamic := UsesC; |
| @@ -66,13 +66,17 @@ | ||
| 66 | 66 | ind: boolean; { oVar: the frame slot holds the variable's address } |
| 67 | 67 | off: int64; { oVar with ind: added to that address } |
| 68 | 68 | isVar: boolean; { a var parameter } |
| 69 | + param: boolean; { oVar: a parameter } | |
| 70 | + used: boolean; { named somewhere but in its own body } | |
| 71 | + read: boolean; { oVar: named other than as what := assigns } | |
| 69 | 72 | fwd: boolean; { oProc: declared forward, no body yet } |
| 70 | 73 | isCode: boolean; { oProc: a code procedure } |
| 71 | 74 | ext: boolean; { oProc, oVar: C's, reached through its chunk, which |
| 72 | 75 | holds its address } |
| 73 | 76 | fields: PObj; { oCase: its fields } |
| 74 | 77 | module: PModule; { oModule: the module; others: where declared } |
| 75 | - link: PObj; { the next top-level declaration of its module } | |
| 78 | + link: PObj; { the next declaration of its module, top-level or | |
| 79 | + local } | |
| 76 | 80 | end; |
| 77 | 81 | |
| 78 | 82 | { The arguments of a generic module's instance: a type, or a |
| @@ -103,6 +107,7 @@ | ||
| 103 | 107 | scope: longint; |
| 104 | 108 | path: shortstring; |
| 105 | 109 | first, last: PObj; { its top-level declarations, in order } |
| 110 | + locals, lastLocal: PObj; { its procedures' declarations, in order } | |
| 106 | 111 | imports: PImport; |
| 107 | 112 | body: longint; { the chunk of its body, or -1 } |
| 108 | 113 | done: boolean; { compiled to the end } |
| @@ -7,7 +7,9 @@ | ||
| 7 | 7 | # X.status its exit status (optional; 0 if missing) |
| 8 | 8 | # A program without X.expect is compiled, not run. test/libc puts |
| 9 | 9 | # lib/Sys.raz, over the C library, before the system call one. |
| 10 | -# Programs in test/err must not compile, and must print X.err. Then | |
| 10 | +# Programs in test/err must not compile, and must print X.err; there | |
| 11 | +# X.args holds the compiler's options. The examples, the tools and the | |
| 12 | +# library's tests are compiled with -Werror, so warnings fail them. Then | |
| 11 | 13 | # the version, tools/Doc.raz, the report's examples, tools/Pretty.raz, |
| 12 | 14 | # and the library's own tests. |
| 13 | 15 | |
| @@ -19,7 +21,9 @@ | ||
| 19 | 21 | |
| 20 | 22 | run() { # run dir name |
| 21 | 23 | dir=$1; name=$2 |
| 22 | - if ! $raz -I $dir/mods -o $out/$name $dir/$name.raz 2>$out/$name.cerr; then | |
| 24 | + flags= | |
| 25 | + [ $dir = examples ] && flags=-Werror | |
| 26 | + if ! $raz $flags -I $dir/mods -o $out/$name $dir/$name.raz 2>$out/$name.cerr; then | |
| 23 | 27 | echo "FAIL $dir/$name: doesn't compile: $(cat $out/$name.cerr)" |
| 24 | 28 | fail=$((fail + 1)); return |
| 25 | 29 | fi |
| @@ -61,7 +65,9 @@ | ||
| 61 | 65 | runs test/libc/Through.raz 2 |
| 62 | 66 | for f in test/err/*.raz; do |
| 63 | 67 | name=$(basename $f .raz) |
| 64 | - if $raz -I test/err/mods -I test/run/mods -o $out/$name $f 2>$out/$name.cerr; then | |
| 68 | + flags= | |
| 69 | + [ -f test/err/$name.args ] && flags=$(cat test/err/$name.args) | |
| 70 | + if $raz $flags -I test/err/mods -I test/run/mods -o $out/$name $f 2>$out/$name.cerr; then | |
| 65 | 71 | echo "FAIL $f: compiles" |
| 66 | 72 | fail=$((fail + 1)) |
| 67 | 73 | elif ! cmp -s test/err/$name.err $out/$name.cerr; then |
| @@ -77,7 +83,7 @@ | ||
| 77 | 83 | else echo "FAIL raz --version: not report $want"; fail=$((fail + 1)); fi |
| 78 | 84 | |
| 79 | 85 | # tools/Doc.raz: on every library module, and as test/doc expects |
| 80 | -if ./raz -o $out/Doc tools/Doc.raz 2>$out/Doc.cerr; then | |
| 86 | +if ./raz -Werror -o $out/Doc tools/Doc.raz 2>$out/Doc.cerr; then | |
| 81 | 87 | for f in lib/*.raz; do |
| 82 | 88 | if ! $out/Doc $f > $out/doc.out; then echo "FAIL Doc $f"; fail=$((fail + 1)); fi |
| 83 | 89 | done |
| @@ -105,7 +111,7 @@ | ||
| 105 | 111 | |
| 106 | 112 | # tools/Pretty.raz: each test/pretty/X.raz comes out as X.expect, which |
| 107 | 113 | # comes out as it is; so do the library, the examples and the tools |
| 108 | -if ./raz -o $out/Pretty tools/Pretty.raz 2>$out/Pretty.cerr; then | |
| 114 | +if ./raz -Werror -o $out/Pretty tools/Pretty.raz 2>$out/Pretty.cerr; then | |
| 109 | 115 | for f in test/pretty/*.raz; do |
| 110 | 116 | want=test/pretty/$(basename $f .raz).expect |
| 111 | 117 | if $out/Pretty $f | cmp -s - $want && $out/Pretty $want | cmp -s - $want; then |
| @@ -144,7 +150,7 @@ | ||
| 144 | 150 | fail=$((fail + 1)) |
| 145 | 151 | else pass=$((pass + 1)); fi |
| 146 | 152 | } |
| 147 | -if ./raz -o $out/Mutate tools/Mutate.raz 2>$out/Mutate.cerr; then | |
| 153 | +if ./raz -Werror -o $out/Mutate tools/Mutate.raz 2>$out/Mutate.cerr; then | |
| 148 | 154 | for f in examples/*.raz test/run/*.raz; do |
| 149 | 155 | $raz -I $(dirname $f)/mods -o $out/mutant $f 2>/dev/null || continue |
| 150 | 156 | for seed in ${FUZZ_SEEDS:-1 2 3}; do fuzz $f $seed; done |
| @@ -158,7 +164,7 @@ | ||
| 158 | 164 | [ $f = lib/Sys.raz ] && continue |
| 159 | 165 | grep -q '^proc main\*' $f || continue |
| 160 | 166 | name=lib_$(basename $f .raz) |
| 161 | - if ! $raz -o $out/$name $f 2>$out/$name.cerr; then | |
| 167 | + if ! $raz -Werror -o $out/$name $f 2>$out/$name.cerr; then | |
| 162 | 168 | echo "FAIL $f: doesn't compile: $(cat $out/$name.cerr)"; fail=$((fail + 1)) |
| 163 | 169 | elif ! $out/$name > $out/$name.out 2>&1; then |
| 164 | 170 | echo "FAIL $f: its test says $(cat $out/$name.out)"; fail=$((fail + 1)) |
| @@ -1,7 +1,10 @@ | ||
| 1 | +test/err/mods/DeclHidden.raz:3:7: warning: constant hidden is never used | |
| 2 | + 3 | const hidden = 2; | |
| 3 | + | ^^^^^^ | |
| 1 | 4 | test/err/DeclNotExported.raz:6:21: error: hidden isn't exported by DeclHidden |
| 2 | 5 | 6 | n := DeclHidden.hidden; |
| 3 | 6 | | ^^^^^^ |
| 4 | 7 | test/err/mods/DeclHidden.raz:3:7: note: hidden is declared here, without a * |
| 5 | 8 | 3 | const hidden = 2; |
| 6 | 9 | | ^^^^^^ |
| 7 | -raz: 1 error | |
| 10 | +raz: 1 error, 1 warning |
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | test/err/ScanMain.raz:2:6: error: main must be proc main* or proc main*: I64 |
| 2 | - 2 | proc main*(n: I64); | |
| 2 | + 2 | proc main*(_n: I64); | |
| 3 | 3 | | ^^^^ |
| 4 | 4 | raz: 1 error |
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | module ScanMain; |
| 2 | -proc main*(n: I64); | |
| 2 | +proc main*(_n: I64); | |
| 3 | 3 | begin |
| 4 | 4 | end; |
| 5 | 5 | end ScanMain. |
| @@ -1 +1,8 @@ | ||
| 1 | +test/err/ThreadC.raz:2:8: warning: module Thread is imported but never used | |
| 2 | + 2 | import Thread, C; | |
| 3 | + | ^^^^^^ | |
| 4 | +test/err/ThreadC.raz:2:16: warning: module C is imported but never used | |
| 5 | + 2 | import Thread, C; | |
| 6 | + | ^ | |
| 7 | +raz: 2 warnings | |
| 1 | 8 | raz: Thread says link static, but C uses C |
| @@ -0,0 +1 @@ | ||
| 1 | +-Werror |
| @@ -0,0 +1,23 @@ | ||
| 1 | +test/err/Unused.raz:3:13: error: module Sys is imported but never used | |
| 2 | + 3 | import Out, Sys; | |
| 3 | + | ^^^ | |
| 4 | +test/err/Unused.raz:6:5: error: constant N is never used | |
| 5 | + 6 | N = 3; M* = 4; _K = 5; | |
| 6 | + | ^ | |
| 7 | +test/err/Unused.raz:9:5: error: type Color is never used | |
| 8 | + 9 | Color = (red, green); | |
| 9 | + | ^^^^^ | |
| 10 | +test/err/Unused.raz:13:5: error: variable g is assigned but never read | |
| 11 | + 13 | g, h, seen: I64; | |
| 12 | + | ^ | |
| 13 | +test/err/Unused.raz:15:6: error: procedure fact is never used | |
| 14 | + 15 | proc fact(n: I64): I64; (* calling itself doesn't count *) | |
| 15 | + | ^^^^ | |
| 16 | +test/err/Unused.raz:21:14: error: parameter a is never used | |
| 17 | + 21 | proc helper*(a, _b: Used; var r: I64); | |
| 18 | + | ^ | |
| 19 | +help: if it must be there, call it _a | |
| 20 | +test/err/Unused.raz:22:5: error: variable x is assigned but never read | |
| 21 | + 22 | var x, y: I64; t: [4]I64; | |
| 22 | + | ^ | |
| 23 | +raz: 7 errors |
| @@ -0,0 +1,40 @@ | ||
| 1 | +module Unused; | |
| 2 | +(* What is never used is warned of, and -Werror makes it an error *) | |
| 3 | +import Out, Sys; | |
| 4 | + | |
| 5 | +const | |
| 6 | + N = 3; M* = 4; _K = 5; | |
| 7 | + | |
| 8 | +type | |
| 9 | + Color = (red, green); | |
| 10 | + Used = I64; | |
| 11 | + | |
| 12 | +var | |
| 13 | + g, h, seen: I64; | |
| 14 | + | |
| 15 | +proc fact(n: I64): I64; (* calling itself doesn't count *) | |
| 16 | +begin | |
| 17 | + if n = 0 then return 1; end; | |
| 18 | + return n * fact(n - 1); | |
| 19 | +end; | |
| 20 | + | |
| 21 | +proc helper*(a, _b: Used; var r: I64); | |
| 22 | +var x, y: I64; t: [4]I64; | |
| 23 | +begin | |
| 24 | + x := 1; | |
| 25 | + y := 2; | |
| 26 | + y := y + 1; | |
| 27 | + t[0] := 1; | |
| 28 | + r := 0; | |
| 29 | +end; | |
| 30 | + | |
| 31 | +proc main*; | |
| 32 | +var i: I64; | |
| 33 | +begin | |
| 34 | + g := 1; | |
| 35 | + seen := h; | |
| 36 | + for i := 0 to 2 do end; | |
| 37 | + Out.int(seen); | |
| 38 | +end; | |
| 39 | + | |
| 40 | +end Unused. |
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | module Rec; |
| 2 | 2 | type R* = record a*: I64; b: I64; end; |
| 3 | -proc f*(r: ^R); begin end; | |
| 3 | +proc f*(_r: ^R); begin end; | |
| 4 | 4 | end Rec. |