| 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. *) |
| 20 | module Diag; |
| 21 | import Sys, Rt, Heap, Mem, Buf, Fmt, Str; |
| 22 | import SYSTEM; |
| 23 | |
| 24 | type |
| 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 | |
| 41 | const |
| 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 | |
| 50 | var |
| 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. *) |
| 67 | proc setWerror*(yes: BOOL); |
| 68 | begin |
| 69 | werror := yes; |
| 70 | end; |
| 71 | |
| 72 | proc setQuiet*(yes: BOOL); |
| 73 | begin |
| 74 | quiet := yes; |
| 75 | end; |
| 76 | |
| 77 | proc setForm*(f: Form); |
| 78 | begin |
| 79 | form := f; |
| 80 | end; |
| 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. *) |
| 84 | proc shownPath*(path: []const U8): []const U8; |
| 85 | var d: []const U8; err: INT; c: []const U8; |
| 86 | begin |
| 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; |
| 98 | end; |
| 99 | |
| 100 | proc fatal*(msg: []const U8); forward; |
| 101 | |
| 102 | (* Memory, or the end of the compile when there is none *) |
| 103 | proc grab(size: INT): ^U8; |
| 104 | var p: ^U8; |
| 105 | begin |
| 106 | p := Mem.alloc(Heap.heap, size, 8); |
| 107 | if p = nil then fatal("out of memory"); end; |
| 108 | return p; |
| 109 | end; |
| 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. *) |
| 113 | proc addFile*(path: []const U8; buf: ^U8; len: INT): INT; |
| 114 | var more: ^File; i: INT; p: []const U8; ok: BOOL; |
| 115 | begin |
| 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; |
| 128 | end; |
| 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 *) |
| 132 | proc setOrigin*(f: INT; text: []const U8); |
| 133 | begin |
| 134 | files[f].origin := text; |
| 135 | end; |
| 136 | |
| 137 | proc pathOf*(f: INT): []const U8; |
| 138 | begin |
| 139 | return files[f].path; |
| 140 | end; |
| 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. *) |
| 146 | proc noColour: BOOL; |
| 147 | begin |
| 148 | return LEN(Sys.env("NO_COLOR")) > 0; |
| 149 | end; |
| 150 | |
| 151 | proc put(s: []const U8); |
| 152 | begin |
| 153 | Buf.str(out, s); |
| 154 | end; |
| 155 | |
| 156 | proc putc(c: U8); |
| 157 | begin |
| 158 | Buf.add(out, c); |
| 159 | end; |
| 160 | |
| 161 | proc putNum(n: INT); |
| 162 | begin |
| 163 | Fmt.write(out, "{}", [Fmt.int(n)]); |
| 164 | end; |
| 165 | |
| 166 | (* The text of a number *) |
| 167 | proc say*(f: []const U8; args: []const Fmt.Arg): []const U8; |
| 168 | var b: ^Buf.Buf; |
| 169 | begin |
| 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^); |
| 174 | end; |
| 175 | |
| 176 | (* c, when colour is on *) |
| 177 | proc paintOn(c: []const U8); |
| 178 | begin |
| 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; |
| 184 | end; |
| 185 | |
| 186 | (* s in colour c, when colour is on *) |
| 187 | proc paint(c, s: []const U8); |
| 188 | begin |
| 189 | paintOn(c); |
| 190 | put(s); |
| 191 | paintOn(Plain); |
| 192 | end; |
| 193 | |
| 194 | (* What has been made, written to standard error, or the rows to |
| 195 | standard output *) |
| 196 | proc flush; |
| 197 | var s: []const U8; n, fd: INT; |
| 198 | begin |
| 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); |
| 207 | end; |
| 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. *) |
| 213 | proc locate(p: Pos): (INT, INT); |
| 214 | var i, line, start: INT; buf: ^U8; |
| 215 | begin |
| 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; |
| 223 | end; |
| 224 | |
| 225 | proc lineOf*(p: Pos): INT; |
| 226 | var line: INT; |
| 227 | begin |
| 228 | line, _ := locate(p); |
| 229 | return line; |
| 230 | end; |
| 231 | |
| 232 | (* A byte that starts a character: every one but a UTF-8 continuation *) |
| 233 | proc starts(c: U8): BOOL; |
| 234 | begin |
| 235 | return (c & 0C0H) # 80H; |
| 236 | end; |
| 237 | |
| 238 | (* Characters from byte i to byte j of buf *) |
| 239 | proc chars(buf: ^U8; i, j: INT): INT; |
| 240 | var n: INT; |
| 241 | begin |
| 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; |
| 248 | end; |
| 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. *) |
| 253 | proc writePlace*(var b: Buf.Buf; p: Pos); |
| 254 | var line, start, endLine, endStart: INT; e: Pos; |
| 255 | begin |
| 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)]); |
| 266 | end; |
| 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 *) |
| 270 | proc row(p: Pos; kind, msg: []const U8); |
| 271 | begin |
| 272 | writePlace(out, p); |
| 273 | Fmt.write(out, "\t{}\t{}\n", [Fmt.str(kind), Fmt.str(msg)]); |
| 274 | flush; |
| 275 | end; |
| 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. *) |
| 280 | proc show(p: Pos; kind, kindColour, msg: []const U8); |
| 281 | var line, start, stop, lo, hi, i, n: INT; buf: ^U8; f: ^File; digits: []const U8; |
| 282 | begin |
| 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; |
| 347 | end; |
| 348 | |
| 349 | (* From the start of p to the end of q *) |
| 350 | proc join*(p, q: Pos): Pos; |
| 351 | begin |
| 352 | p.b := q.b; |
| 353 | return p; |
| 354 | end; |
| 355 | |
| 356 | (* The text at p, at most the 255 bytes a message can hold *) |
| 357 | proc source*(p: Pos): []const U8; |
| 358 | var n: INT; |
| 359 | begin |
| 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]; |
| 363 | end; |
| 364 | |
| 365 | (* Messages *) |
| 366 | |
| 367 | (* Line line of the file at path, as written, to at most 255 bytes *) |
| 368 | proc lineText*(path: []const U8; line: INT): []const U8; |
| 369 | var i, k, n, j: INT; f: ^File; want: []const U8; |
| 370 | begin |
| 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 ""; |
| 387 | end; |
| 388 | |
| 389 | proc error*(p: Pos; msg: []const U8); |
| 390 | begin |
| 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; |
| 408 | end; |
| 409 | |
| 410 | proc warning*(p: Pos; msg: []const U8); |
| 411 | begin |
| 412 | if werror then |
| 413 | error(p, msg); |
| 414 | else |
| 415 | show(p, "warning", Yellow, msg); |
| 416 | shown := true; |
| 417 | INC(warnings); |
| 418 | end; |
| 419 | end; |
| 420 | |
| 421 | (* Notes and help follow an error or a warning, and are shown with it *) |
| 422 | proc note*(p: Pos; msg: []const U8); |
| 423 | begin |
| 424 | if shown then show(p, "note", Cyan, msg); end; |
| 425 | end; |
| 426 | |
| 427 | proc help*(msg: []const U8); |
| 428 | begin |
| 429 | if shown and (form = Form.pretty) then |
| 430 | paint(Green, "help:"); |
| 431 | put(" "); |
| 432 | put(msg); |
| 433 | putc('\n'); |
| 434 | flush; |
| 435 | end; |
| 436 | end; |
| 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. *) |
| 440 | proc fatal*(msg: []const U8); |
| 441 | begin |
| 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); |
| 451 | end; |
| 452 | |
| 453 | proc count(n: INT; what: []const U8): []const U8; |
| 454 | begin |
| 455 | if n = 1 then return say("1 {}", [Fmt.str(what)]); end; |
| 456 | return say("{} {}s", [Fmt.int(n), Fmt.str(what)]); |
| 457 | end; |
| 458 | |
| 459 | (* Says how many errors and warnings there were, and stops the compiler |
| 460 | if there were errors. *) |
| 461 | proc finish*; |
| 462 | begin |
| 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; |
| 474 | end; |
| 475 | |
| 476 | begin |
| 477 | Buf.init(cwd, Heap.heap); |
| 478 | Buf.init(out, Heap.heap); |
| 479 | end Diag. |