Skip to content
← Back

src/logging/logger.ghul

1
namespace Logging is
2
use System.Exception
3
use System.NotImplementedException
4
use Ghul.Disposable
5
6
use Collections.LIST
7
use Collections.STACK
8
9
use IO.Std
10
use IO.TextWriter
11
use IO.StringWriter
12
13
use Source
14
15
struct DEBUG_THEN_EXIT: Disposable is
16
_initialized: bool
17
_depth: int
18
19
init(depth: int) is
20
_initialized = true
21
_depth = depth
22
si
23
24
init() is
25
// not debugging, so dispose doesn't need to do anything
26
si
27
28
dispose() is
29
if _initialized then
30
while _debug_depth > _depth do
31
debug_exit()
32
od
33
fi
34
35
_initialized = false
36
si
37
si
38
39
_debug_depth: int
40
41
debug_enter() -> DEBUG_THEN_EXIT is
42
debug_always(">>>")
43
let result = DEBUG_THEN_EXIT(_debug_depth)
44
45
_debug_depth = _debug_depth + 1
46
47
return result
48
si
49
50
in_debug() -> bool => _debug_depth > 0
51
52
debug_enter(want_debug: bool) -> DEBUG_THEN_EXIT =>
53
if want_debug then
54
debug_enter()
55
else
56
DEBUG_THEN_EXIT()
57
fi
58
59
debug_exit() is
60
_debug_depth = _debug_depth - 1
61
debug_always("<<<")
62
si
63
64
debug_exit(want_debug: bool) is
65
if want_debug then
66
debug_exit()
67
fi
68
si
69
70
debug_reset() is
71
_debug_depth = 0
72
si
73
74
debug_indent() is
75
if _debug_depth > 0 then
76
_debug_depth = _debug_depth + 1
77
fi
78
si
79
80
debug_unindent() is
81
if _debug_depth > 0 then
82
_debug_depth = _debug_depth - 1
83
fi
84
si
85
86
debug(message: string) is
87
if _debug_depth > 0 then
88
debug_always(message)
89
fi
90
si
91
92
debug_always(message: string) is
93
for m in message.replace_line_endings("\n").split(['\n']) do
94
for i in 0.._debug_depth do
95
IO.Std.error.write(" ")
96
od
97
98
IO.Std.error.write_line(m)
99
od
100
101
IO.Std.error.flush()
102
si
103
104
trait Logger is
105
is_poisoned: bool
106
has_consumed_error: bool
107
has_consumed_any: bool
108
109
error_count: int
110
any_errors: bool
111
112
// The source paths that currently hold at least one error-severity
113
// diagnostic. The analysis-mode incremental interface path uses
114
// this as a conservative dependent set: a body that failed to bind
115
// a name recorded no reference, so reference-based dependency
116
// scans cannot see it, yet an interface edit may be exactly what
117
// cures (or should re-report) its error.
118
paths_with_errors: Collections.Iterable[string]
119
120
// Every source path with a retained diagnostics list at the
121
// current speculation depth — includes a path checked and found
122
// clean, since `clear` creates the (possibly empty) list before
123
// any diagnostic is added for it.
124
diagnostic_paths: Collections.Iterable[string]
125
126
// Every diagnostic message recorded for `path` at the current
127
// speculation depth, in report order. Walked directly by
128
// DIAGNOSTICS_COLLECTOR — no text formatter in between.
129
diagnostics_for(path: string) -> Collections.Iterable[DIAGNOSTIC_MESSAGE]
130
131
// True iff this speculation level recorded no errors and didn't
132
// consume any wild / inferred / placeholder types. Used by the
133
// iterative inference loop in COMPILE_EXPRESSIONS to detect
134
// convergence: a body walk that was clean is one we don't need
135
// to retry.
136
is_clean: bool
137
138
depth: int
139
140
speculate()
141
roll_back() -> DIAGNOSTICS_STATE
142
commit()
143
mark() -> int
144
release(mark: int)
145
146
speculate_then_commit() -> LOGGER_SPECULATE_THEN_COMMIT
147
speculate_then_backtrack() -> LOGGER_SPECULATE_THEN_BACKTRACK
148
mark_then_release() -> MARK_THEN_RELEASE
149
150
// True while an analysis-mode (language-service) request is
151
// being served, false during batch compilation.
152
is_analysis: bool
153
154
// The set of files the client currently has open. Replaced whole by
155
// each client update. Editor-only hints are suppressed at the source
156
// for any file not in this set, since they are invisible for a file
157
// the user is not viewing. Returns the paths whose open state
158
// changed, so the caller can invalidate the expression walk of each
159
// - and only of each: no other file's hint output was gated on the
160
// part of the set that moved.
161
set_open_files(paths: Collections.Iterable[string]) -> Collections.List[string]
162
is_file_open(path: string) -> bool
163
164
start_analysis()
165
end_analysis()
166
167
set_is_compiling_expressions(value: bool)
168
169
merge(state: DIAGNOSTICS_STATE)
170
171
exception(location: LOCATION, exception: Exception, message: string)
172
// Report an exception thrown by work that may have left
173
// speculation states stacked. The stack is unwound to `mark`
174
// first, so the diagnostic lands in the state the caller keeps
175
// rather than in one it is about to discard.
176
exception(mark: int, location: LOCATION, exception: Exception, message: string)
177
fatal(location: LOCATION, message: string)
178
error(location: LOCATION, message: string)
179
// Same as error(location, message), plus a second location this
180
// diagnostic refers to (a prior declaration, an overridden
181
// member) - rendered by a capable client as a jump-to link, and
182
// appended to the line by the text formatters. The primary
183
// `message` must still name the thing the related location
184
// points at; the related location says where, not what.
185
error(location: LOCATION, message: string, related_location: LOCATION, related_message: string)
186
// Same as error(location, message, related_location,
187
// related_message), plus several related locations - for a
188
// diagnostic whose counterpart sites are peers (a duplicate
189
// declaration reported at every site) rather than one
190
// authoritative prior declaration.
191
error(location: LOCATION, message: string, related: LIST[RELATED_LOCATION])
192
// A lexer-produced error, written to the base diagnostics state so it
193
// survives parser speculation rollback (see DIAGNOSTICS_STORE).
194
lexer_error(location: LOCATION, message: string)
195
// An error about the compilation environment rather than the source,
196
// written to the base diagnostics state like a lexer error and for the
197
// same reason: it is produced once, by work that is cached, so it
198
// cannot duplicate, and whichever speculative branch happened to
199
// trigger that work must not be able to discard it.
200
environment_error(location: LOCATION, message: string)
201
warn(location: LOCATION, message: string)
202
warn(location: LOCATION, code: string, message: string)
203
// Same as warn(location, code, message), plus a related location -
204
// see error(location, message, related_location, related_message).
205
warn(location: LOCATION, code: string, message: string, related_location: LOCATION, related_message: string)
206
// Same as warn(location, code, message), plus several related
207
// locations - for a diagnostic that points at more than one other
208
// declaration at once (every field a constructor leaves
209
// unassigned, say).
210
warn(location: LOCATION, code: string, message: string, related: LIST[RELATED_LOCATION])
211
poison(location: LOCATION, message: string)
212
info(location: LOCATION, message: string)
213
info(location: LOCATION, code: string, message: string)
214
hint(location: LOCATION, message: string)
215
hint(location: LOCATION, code: string, message: string)
216
// Same as hint(location, code, message), plus a related location -
217
// see error(location, message, related_location, related_message).
218
hint(location: LOCATION, code: string, message: string, related_location: LOCATION, related_message: string)
219
220
// Record an editor-only inlay hint at `location`: a terse `label`
221
// (the ghost text) plus an optional `detail` (hover tooltip). Never
222
// published as a diagnostic; surfaced only by the INLAY_HINTS query.
223
// Callers gate on `want_hint_for` first, exactly as for a hint.
224
inlay(location: LOCATION, code: string, label: string, detail: string?)
225
226
// Every inlay recorded for `path` in the current state, in
227
// emission order. Consulted by the INLAY_HINTS handler.
228
inlays_for(path: string) -> Collections.Iterable[INLAY]
229
230
// Drop the inlay carriers recorded for `path` whose code begins
231
// with `code`. A pass re-run against one file calls this first so
232
// it replaces its carriers rather than stacking a second copy;
233
// a prefix because one pass emits suffixed codes for variants of
234
// one carrier (an accessor's half) alongside its base code.
235
clear_inlays(path: string, code: string)
236
237
238
// Whether an inlay kind (an INLAY_KINDS name) is collected.
239
// Kinds are enabled and disabled process-wide by the driver's
240
// `--inlay <kind>` / `--no-inlay <kind>` flags; a disabled kind
241
// is never recorded, so it costs nothing to collect or serve.
242
// The default is "on", so a logger that does not track kinds
243
// keeps every hint.
244
is_inlay_kind_enabled(kind: string) -> bool => true
245
246
// Turn a kind on or off for this process. A logger without
247
// per-kind state ignores these.
248
enable_inlay_kind(kind: string) is si
249
250
disable_inlay_kind(kind: string) is si
251
252
// Record a statement boundary the parser inferred - a terminator
253
// left off at end of line - at `location`. Called from the
254
// parser's terminator sites; a logger that does not serve the
255
// terminator inlay kind (it is disabled, or the logger has no
256
// per-file state) records nothing.
257
note_inferred_terminator(location: LOCATION) is si
258
259
// The inferred-terminator positions recorded for `path`, as
260
// LOCATION-packed ints in source order. Served by the
261
// INLAY_HINTS handler when the terminator kind is enabled.
262
inferred_terminators_for(path: string) -> Collections.List[int] =>
263
Collections.LIST[int]()
264
265
// Suppress every subsequent warn/info/hint carrying this code.
266
// Accumulating — calling twice with different codes silences both.
267
// Populated by `--suppress` / `--no-warn-*` from the driver.
268
suppress(code: string)
269
270
// Remove a code from the global suppression set. Populated by
271
// `--warn <slug,slug,...>` from the driver; the way to enable a
272
// warning that is suppressed by default (redundant-semicolon).
273
unsuppress(code: string)
274
275
is_suppressed(code: string?) -> bool
276
277
// Files whose warnings, infos and hints are not reported: those
278
// that belong to a source library, which are the library's own
279
// concern. Errors in them are reported as anywhere else.
280
set_quiet_files(is_quiet: (string) -> bool)
281
282
// Promote warnings to errors. `set_all_warnings_are_errors(true)`
283
// routes every subsequent `warn(...)` to error level; `promote_to_error(code)`
284
// does the same only for warns carrying that slug. Populated by
285
// `--warn-as-error <slug,slug,...>` from the driver, with the literal
286
// slug `all` mapping to `set_all_warnings_are_errors(true)`.
287
// Suppression wins: a warning silenced by `suppress` or by an
288
// `@suppress(...)` region is never resurrected as an error.
289
set_all_warnings_are_errors(value: bool)
290
promote_to_error(code: string)
291
292
// Demote warnings carrying this code to editor-only hints.
293
// Populated by `--warn-as-hint <slug,slug,...>` from the driver.
294
// A demoted warn is emitted as a hint under the same gate as a
295
// native hint: dropped entirely in batch compilation, surfaced in
296
// analysis mode only for a file the client has open. Suppression
297
// still wins: a code that is both suppressed and demoted is
298
// dropped. Demotion wins over promotion to error.
299
demote_to_hint(code: string)
300
301
// Demote warnings carrying this code to info-level diagnostics.
302
// Populated by `--warn-as-info <slug,slug,...>` from the driver.
303
// Unlike a hint, info stays a normal batch-visible diagnostic:
304
// it is not subject to the editor-only gate. Suppression still
305
// wins; a code demoted to a hint takes precedence over info.
306
demote_to_info(code: string)
307
308
// Whether a hint at `location` should be generated: true while
309
// serving an analysis request for a file the client has open. The
310
// single gate every hint site consults before doing any
311
// hint-specific work.
312
want_hint_for(location: LOCATION?) -> bool
313
314
// Lexical-scope suppression: anything emitted at a location
315
// covered by `region` carrying `code` is dropped, on top of the
316
// global `suppress` set. Populated by the `@suppress(...)`
317
// pragma collector. The location-aware `is_suppressed` consults
318
// both the global set and the registered regions. Regions are
319
// stored per source file so analysis-mode re-walks can clear
320
// exactly the file being re-walked.
321
register_suppression_region(region: LOCATION, code: string)
322
clear_suppression_regions(path: string)
323
clear_suppression_regions()
324
is_suppressed(code: string?, location: LOCATION) -> bool
325
326
poison(location: LOCATION)
327
mark_consumed_error()
328
mark_consumed_any()
329
330
// Convenience: mark consumed-any iff `consumed` is true.
331
// Removes the `if add_*(...) then logger.mark_consumed_any() fi`
332
// boilerplate at sites that record progress against a
333
// Variable accumulator.
334
mark_consumed_any_if(consumed: bool)
335
336
clear_consumed_error()
337
clear_consumed_any()
338
339
write_poison_messages()
340
341
clear(path: string, analysis_only: bool)
342
343
clear_global_declaration_diagnostics(path: string)
344
clear_expression_diagnostics(path: string)
345
clear_expression_diagnostics_within(path: string, span: Source.LOCATION)
346
347
write_all_diagnostics(writer: TextWriter, formatter: DiagnosticFormatter)
348
si
349
350
struct LOGGER_SPECULATE_THEN_COMMIT: Disposable is
351
_logger: Logger?
352
353
has_consumed_any: bool => _logger!.has_consumed_any
354
has_consumed_error: bool => _logger!.has_consumed_error
355
356
any_errors: bool => _logger!.any_errors
357
358
is_speculating: bool => _logger != null
359
360
init(logger: Logger) is
361
_logger = logger
362
_logger.speculate()
363
si
364
365
backtrack() -> DIAGNOSTICS_STATE is
366
let result = _logger!.roll_back()
367
_logger = null
368
return result
369
si
370
371
backtrack_if_speculating() -> DIAGNOSTICS_STATE? is
372
if _logger != null then
373
return backtrack()
374
else
375
return null
376
fi
377
si
378
379
backtrack_and_restart() -> DIAGNOSTICS_STATE is
380
let logger = _logger!
381
let result = logger.roll_back()
382
logger.speculate()
383
return result
384
si
385
386
commit() is
387
_logger!.commit()
388
_logger = null
389
si
390
391
cancel() is
392
_logger = null
393
si
394
395
dispose() is
396
if _logger != null then
397
_logger.commit()
398
_logger = null
399
fi
400
si
401
si
402
403
struct LOGGER_SPECULATE_THEN_BACKTRACK: Disposable is
404
_logger: Logger?
405
406
has_consumed_any: bool => _logger!.has_consumed_any
407
has_consumed_error: bool => _logger!.has_consumed_error
408
409
any_errors: bool => _logger!.any_errors
410
411
is_speculating: bool => _logger != null
412
413
init(logger: Logger) is
414
_logger = logger
415
_logger.speculate()
416
si
417
418
backtrack() -> DIAGNOSTICS_STATE is
419
let result = _logger!.roll_back()
420
_logger = null
421
return result
422
si
423
424
backtrack_and_restart() -> DIAGNOSTICS_STATE is
425
let logger = _logger!
426
let result = logger.roll_back()
427
logger.speculate()
428
return result
429
si
430
431
commit() is
432
_logger!.commit()
433
_logger = null
434
si
435
436
cancel() is
437
_logger = null
438
si
439
440
dispose() is
441
if _logger != null then
442
_logger.roll_back()
443
_logger = null
444
fi
445
si
446
si
447
448
struct MARK_THEN_RELEASE: Disposable is
449
_logger: Logger
450
_mark: int
451
452
init(logger: Logger) is
453
_logger = logger
454
_mark = _logger.mark()
455
si
456
457
dispose() is
458
_logger.release(_mark)
459
si
460
si
461
si