raz / src / Diag.raz

479 lines · 13.5 KiB · raw · history · last change d70fd8f0

1(* Diag: messages.
2
3 The one place the compiler's messages are made. A message is an
4 error or a warning at a position, which it shows as the file, line
5 and column, then the line of source with the position underlined;
6 notes, each at a position of its own, and help, a line of advice,
7 may follow it. Colour is used when standard error is a terminal and
8 NO_COLOR isn't set. Diag keeps every source file it is given, so
9 that a note can show a line of a module compiled earlier.
10
11 For an editor (raz -q), messages are rows on standard output
12 instead, or not given at all (form).
13
14 Depends on: Sys, to write and to ask about the terminal; Buf and Fmt,
15 to build what it writes.
16 Leaves out: wording. The callers say what is wrong, with say; a
17 message is at most 255 characters, which every message so far keeps
18 well within. A message made by say is kept for good, since it is
19 made only where it is about to be shown. *)
20module Diag;
21import Sys, Rt, Heap, Mem, Buf, Fmt, Str;
22import SYSTEM;
23
24type
25 (* A stretch of a source file: bytes a up to but not including b.
26 a = b marks the place between two characters. *)
27 Pos* = record f*, a*, b*: INT; end;
28
29 (* How messages are given: shown to a person on standard error; as
30 rows on standard output, for an editor (raz -q check); or not at
31 all (raz -q words, which says only names) *)
32 Form* = (pretty, rows, silent);
33
34 File = record
35 path: []const U8;
36 buf: ^U8;
37 len: INT;
38 origin: []const U8; (* a line told once, under the first message about the file; "" for none *)
39 end;
40
41const
42 MaxErrors* = 20;
43 Bold = "\x1B[1m"; Red = "\x1B[1;31m"; Yellow = "\x1B[1;33m";
44 Cyan = "\x1B[1;36m"; Green = "\x1B[1;32m"; Blue = "\x1B[1;34m";
45 Plain = "\x1B[0m";
46 (* A long line is shown cut to this many bytes around the place, its
47 cut ends marked ... *)
48 ShownBytes = 100;
49
50var
51 errors*, warnings*: INT;
52 werror*: BOOL; (* -Werror: a warning counts as an error *)
53 (* The construct being read uses a name already reported as not
54 declared: its errors follow from that one, and aren't shown. *)
55 quiet*: BOOL;
56 form*: Form;
57
58 files: ^File; (* numFiles of them, in room for cap *)
59 numFiles, cap: INT;
60 colour, colourKnown, cwdKnown: BOOL;
61 cwd: Buf.Buf; (* the working directory and a /, or empty if unknown *)
62 out: Buf.Buf; (* the message being made *)
63 lastError: Pos; (* where the last error was *)
64 shown: BOOL; (* whether it was shown: its notes and help are only then *)
65
66(* Exported variables can't be set from outside, so these do it. *)
67proc setWerror*(yes: BOOL);
68begin
69 werror := yes;
70end;
71
72proc setQuiet*(yes: BOOL);
73begin
74 quiet := yes;
75end;
76
77proc setForm*(f: Form);
78begin
79 form := f;
80end;
81
82(* Paths are shown from the working directory where they are under it,
83 so messages read the same wherever the compiler or its library is. *)
84proc shownPath*(path: []const U8): []const U8;
85var d: []const U8; err: INT; c: []const U8;
86begin
87 if not cwdKnown then
88 d, err := Sys.workDir();
89 if err = 0 then
90 Buf.str(cwd, d);
91 Buf.add(cwd, '/');
92 end;
93 cwdKnown := true;
94 end;
95 c := Buf.bytes(cwd);
96 if (LEN(c) > 0) and Str.hasPrefix(path, c) then return path[LEN(c)..LEN(path)]; end;
97 return path;
98end;
99
100proc fatal*(msg: []const U8); forward;
101
102(* Memory, or the end of the compile when there is none *)
103proc grab(size: INT): ^U8;
104var p: ^U8;
105begin
106 p := Mem.alloc(Heap.heap, size, 8);
107 if p = nil then fatal("out of memory"); end;
108 return p;
109end;
110
111(* Keeps buf, len bytes of a file, under a number, which it gives back;
112 buf stays as it is, and its path is copied. *)
113proc addFile*(path: []const U8; buf: ^U8; len: INT): INT;
114var more: ^File; i: INT; p: []const U8; ok: BOOL;
115begin
116 if numFiles = cap then
117 more := ^File(grab((2 * cap + 8) * SIZE(File)));
118 for i := 0 to numFiles - 1 do more[i] := files[i]; end;
119 if files # nil then Mem.free(Heap.heap, ^U8(files)); end;
120 files := more;
121 cap := 2 * cap + 8;
122 end;
123 p, ok := Str.copy(Heap.heap, shownPath(path));
124 if not ok then fatal("out of memory"); end;
125 files[numFiles] := {path: p, buf: buf, len: len, origin: ""};
126 INC(numFiles);
127 return numFiles - 1;
128end;
129
130(* Gives file f a line to tell once, under the first message about it:
131 where a library came from and the raz it was written for *)
132proc setOrigin*(f: INT; text: []const U8);
133begin
134 files[f].origin := text;
135end;
136
137proc pathOf*(f: INT): []const U8;
138begin
139 return files[f].path;
140end;
141
142(* Writing *)
143
144(* Whether NO_COLOR is set. The library can't tell it set to nothing
145 from not set, so that counts as not set, as no-color.org has it. *)
146proc noColour: BOOL;
147begin
148 return LEN(Sys.env("NO_COLOR")) > 0;
149end;
150
151proc put(s: []const U8);
152begin
153 Buf.str(out, s);
154end;
155
156proc putc(c: U8);
157begin
158 Buf.add(out, c);
159end;
160
161proc putNum(n: INT);
162begin
163 Fmt.write(out, "{}", [Fmt.int(n)]);
164end;
165
166(* The text of a number *)
167proc say*(f: []const U8; args: []const Fmt.Arg): []const U8;
168var b: ^Buf.Buf;
169begin
170 b := ^Buf.Buf(grab(SIZE(Buf.Buf)));
171 Buf.init(b^, Heap.heap);
172 Fmt.write(b^, f, args);
173 return Buf.bytes(b^);
174end;
175
176(* c, when colour is on *)
177proc paintOn(c: []const U8);
178begin
179 if not colourKnown then
180 colour := Sys.isTerminal(2) and not noColour();
181 colourKnown := true;
182 end;
183 if colour then put(c); end;
184end;
185
186(* s in colour c, when colour is on *)
187proc paint(c, s: []const U8);
188begin
189 paintOn(c);
190 put(s);
191 paintOn(Plain);
192end;
193
194(* What has been made, written to standard error, or the rows to
195 standard output *)
196proc flush;
197var s: []const U8; n, fd: INT;
198begin
199 s := Buf.bytes(out);
200 if form = Form.rows then fd := Sys.stdout; else fd := Sys.stderr; end;
201 while LEN(s) > 0 do
202 n := Sys.write(fd, s);
203 if n <= 0 then break; end;
204 s := s[n..LEN(s)];
205 end;
206 Buf.reset(out);
207end;
208
209(* Positions *)
210
211(* The line of p, and the byte where it starts. Lines are counted from
212 the start of the file, which is slow only next to compiling it. *)
213proc locate(p: Pos): (INT, INT);
214var i, line, start: INT; buf: ^U8;
215begin
216 buf := files[p.f].buf;
217 line := 1;
218 start := 0;
219 for i := 0 to p.a - 1 do
220 if buf[i] = 10 then INC(line); start := i + 1; end;
221 end;
222 return line, start;
223end;
224
225proc lineOf*(p: Pos): INT;
226var line: INT;
227begin
228 line, _ := locate(p);
229 return line;
230end;
231
232(* A byte that starts a character: every one but a UTF-8 continuation *)
233proc starts(c: U8): BOOL;
234begin
235 return (c & 0C0H) # 80H;
236end;
237
238(* Characters from byte i to byte j of buf *)
239proc chars(buf: ^U8; i, j: INT): INT;
240var n: INT;
241begin
242 n := 0;
243 while i < j do
244 if starts(buf[i]) then INC(n); end;
245 INC(i);
246 end;
247 return n;
248end;
249
250(* p as raz -q gives a place, written to b: the file, the line and the
251 byte in the line where p starts, the same where it ends, split by
252 tabs; all from 1. A place in no file has no file, and zeros. *)
253proc writePlace*(var b: Buf.Buf; p: Pos);
254var line, start, endLine, endStart: INT; e: Pos;
255begin
256 if p.f >= numFiles then
257 Buf.str(b, "\t0\t0\t0\t0");
258 return;
259 end;
260 line, start := locate(p);
261 e := p;
262 e.a := p.b;
263 endLine, endStart := locate(e);
264 Fmt.write(b, "{}\t{}\t{}\t{}\t{}", [Fmt.str(files[p.f].path), Fmt.int(line), Fmt.int(p.a - start + 1),
265 Fmt.int(endLine), Fmt.int(p.b - endStart + 1)]);
266end;
267
268(* A message as raz -q check gives it, a row for an editor: its place,
269 its kind and its text, split by tabs *)
270proc row(p: Pos; kind, msg: []const U8);
271begin
272 writePlace(out, p);
273 Fmt.write(out, "\t{}\t{}\n", [Fmt.str(kind), Fmt.str(msg)]);
274 flush;
275end;
276
277(* "file:line:col: kind: msg", then the line with p underlined. A tab
278 before p is copied into the underline, so that the two line up
279 whatever the terminal makes of tabs. *)
280proc show(p: Pos; kind, kindColour, msg: []const U8);
281var line, start, stop, lo, hi, i, n: INT; buf: ^U8; f: ^File; digits: []const U8;
282begin
283 if form # Form.pretty then
284 if form = Form.rows then row(p, kind, msg); end;
285 return;
286 end;
287 if p.f >= numFiles then
288 (* about no place in a file, as for Rt, which the compiler
289 imports before it reads one *)
290 paint(Bold, "raz:");
291 put(" ");
292 paintOn(kindColour); put(kind); put(":"); paintOn(Plain);
293 put(" ");
294 paint(Bold, msg);
295 putc('\n');
296 flush;
297 return;
298 end;
299 line, start := locate(p);
300 f := SYSTEM.ADR(files[p.f]);
301 buf := f^.buf;
302 paintOn(Bold);
303 put(f^.path); put(":"); putNum(line); put(":"); putNum(chars(buf, start, p.a) + 1); put(":");
304 paintOn(Plain);
305 put(" ");
306 paintOn(kindColour); put(kind); put(":"); paintOn(Plain);
307 put(" ");
308 paint(Bold, msg);
309 putc('\n');
310 if LEN(f^.origin) > 0 then
311 put(" ("); put(f^.origin); put(")\n");
312 f^.origin := "";
313 end;
314 stop := start;
315 while (stop < f^.len) and (buf[stop] # 10) do INC(stop); end;
316 lo := start;
317 hi := stop;
318 if hi - lo > ShownBytes then
319 if p.a - lo > ShownBytes div 2 then lo := p.a - ShownBytes div 2; end;
320 while not starts(buf[lo]) do INC(lo); end;
321 if hi - lo > ShownBytes then hi := lo + ShownBytes; end;
322 while (hi < stop) and not starts(buf[hi]) do INC(hi); end;
323 end;
324 paintOn(Blue);
325 digits := say("{}", [Fmt.int(line)]);
326 for i := LEN(digits) to 4 do put(" "); end;
327 put(digits); put(" |");
328 paintOn(Plain);
329 put(" ");
330 if lo > start then put("..."); end;
331 put(buf[lo..hi]);
332 if hi < stop then put("..."); end;
333 putc('\n');
334 paint(Blue, " |");
335 put(" ");
336 if lo > start then put(" "); end;
337 for i := lo to p.a - 1 do
338 if buf[i] = '\t' then putc('\t'); elsif starts(buf[i]) then putc(' '); end;
339 end;
340 if p.b < hi then n := chars(buf, p.a, p.b); else n := chars(buf, p.a, hi); end;
341 if n < 1 then n := 1; end;
342 paintOn(kindColour);
343 for i := 1 to n do putc('^'); end;
344 paintOn(Plain);
345 putc('\n');
346 flush;
347end;
348
349(* From the start of p to the end of q *)
350proc join*(p, q: Pos): Pos;
351begin
352 p.b := q.b;
353 return p;
354end;
355
356(* The text at p, at most the 255 bytes a message can hold *)
357proc source*(p: Pos): []const U8;
358var n: INT;
359begin
360 n := p.b - p.a;
361 if n > 255 then n := 255; end;
362 return files[p.f].buf[p.a..p.a + n];
363end;
364
365(* Messages *)
366
367(* Line line of the file at path, as written, to at most 255 bytes *)
368proc lineText*(path: []const U8; line: INT): []const U8;
369var i, k, n, j: INT; f: ^File; want: []const U8;
370begin
371 want := shownPath(path);
372 for i := 0 to numFiles - 1 do
373 f := SYSTEM.ADR(files[i]);
374 if f^.path = want then
375 k := 0;
376 n := 1;
377 while (n < line) and (k < f^.len) do
378 if f^.buf[k] = 10 then INC(n); end;
379 INC(k);
380 end;
381 j := k;
382 while (j < f^.len) and (f^.buf[j] # 10) and (j - k < 255) do INC(j); end;
383 return f^.buf[k..j];
384 end;
385 end;
386 return "";
387end;
388
389proc error*(p: Pos; msg: []const U8);
390begin
391 (* a second error at one place follows from the first: when a symbol
392 ends a construct with an error, the one around it may stop there too *)
393 shown := not quiet and not ((errors > 0) and (p.f = lastError.f) and (p.a = lastError.a));
394 if not shown then return; end;
395 lastError := p;
396 show(p, "error", Red, msg);
397 INC(errors);
398 (* raz -q words reads on to the cursor, past any number of errors *)
399 if (errors >= MaxErrors) and (form # Form.silent) then
400 if form = Form.pretty then
401 put("raz: stopping after ");
402 putNum(MaxErrors);
403 put(" errors\n");
404 flush;
405 end;
406 Rt.exit(1);
407 end;
408end;
409
410proc warning*(p: Pos; msg: []const U8);
411begin
412 if werror then
413 error(p, msg);
414 else
415 show(p, "warning", Yellow, msg);
416 shown := true;
417 INC(warnings);
418 end;
419end;
420
421(* Notes and help follow an error or a warning, and are shown with it *)
422proc note*(p: Pos; msg: []const U8);
423begin
424 if shown then show(p, "note", Cyan, msg); end;
425end;
426
427proc help*(msg: []const U8);
428begin
429 if shown and (form = Form.pretty) then
430 paint(Green, "help:");
431 put(" ");
432 put(msg);
433 putc('\n');
434 flush;
435 end;
436end;
437
438(* An error with no place in the source, such as a file that can't be
439 read: it stops the compiler at once. *)
440proc fatal*(msg: []const U8);
441begin
442 if form = Form.pretty then
443 put("raz: ");
444 put(msg);
445 putc('\n');
446 flush;
447 elsif form = Form.rows then
448 row({f: MAX(INT), a: 0, b: 0}, "error", msg);
449 end;
450 Rt.exit(1);
451end;
452
453proc count(n: INT; what: []const U8): []const U8;
454begin
455 if n = 1 then return say("1 {}", [Fmt.str(what)]); end;
456 return say("{} {}s", [Fmt.int(n), Fmt.str(what)]);
457end;
458
459(* Says how many errors and warnings there were, and stops the compiler
460 if there were errors. *)
461proc finish*;
462begin
463 if form # Form.pretty then
464 (* no count: each message was a row of its own, or none *)
465 elsif (errors > 0) and (warnings > 0) then
466 put("raz: "); put(count(errors, "error")); put(", "); put(count(warnings, "warning")); putc('\n');
467 elsif errors > 0 then
468 put("raz: "); put(count(errors, "error")); putc('\n');
469 elsif warnings > 0 then
470 put("raz: "); put(count(warnings, "warning")); putc('\n');
471 end;
472 flush;
473 if errors > 0 then Rt.exit(1); end;
474end;
475
476begin
477 Buf.init(cwd, Heap.heap);
478 Buf.init(out, Heap.heap);
479end Diag.