Coverage Report

Created: 2026-11-10 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
src/errors_format.c
Line
Count
Source
1
#include "prism/errors_format.h"
2
3
#include "prism/internal/allocator.h"
4
#include "prism/internal/assert.h"
5
#include "prism/internal/buffer.h"
6
#include "prism/internal/diagnostic.h"
7
#include "prism/internal/encoding.h"
8
#include "prism/internal/line_offset_list.h"
9
#include "prism/internal/oom.h"
10
#include "prism/internal/parser.h"
11
12
#include <inttypes.h>
13
#include <stdbool.h>
14
#include <stdlib.h>
15
#include <string.h>
16
17
/**
18
 * Check if the source in the given range of lines is valid UTF-8. The indices
19
 * are zero-based indices into the line offsets list, and the range is
20
 * inclusive on both ends.
21
 */
22
static bool
23
2.26k
lines_are_utf8(const pm_parser_t *parser, size_t start_index, size_t end_index) {
24
2.26k
    const pm_line_offset_list_t *line_offsets = &parser->line_offsets;
25
2.26k
    const uint8_t *cursor = parser->start + line_offsets->offsets[start_index];
26
2.26k
    const uint8_t *end = (end_index + 1 == line_offsets->size) ? 
parser->end434
:
(parser->start + line_offsets->offsets[end_index + 1])1.83k
;
  Branch (26:26): [True: 434, False: 1.83k]
27
28
333k
    while (cursor < end) {
  Branch (28:12): [True: 331k, False: 2.26k]
29
331k
        size_t width = pm_encoding_utf_8_char_width(cursor, end - cursor);
30
331k
        if (width == 0) 
return 0
false0
;
  Branch (30:13): [True: 0, False: 331k]
31
32
331k
        cursor += width;
33
331k
    }
34
35
2.26k
    return true;
36
2.26k
}
37
38
typedef struct {
39
    pm_diagnostic_t *diagnostic;
40
    int32_t line_start;
41
    uint32_t column_start;
42
    uint32_t column_end;
43
    size_t idx;
44
} pm_rich_error_t;
45
46
static int
47
132k
pm_rich_error_compare(const void *a, const void *b) {
48
132k
    const pm_rich_error_t *left = (const pm_rich_error_t *) a;
49
132k
    const pm_rich_error_t *right = (const pm_rich_error_t *) b;
50
51
132k
    if (left->line_start != right->line_start) {
  Branch (51:9): [True: 38.2k, False: 94.2k]
52
38.2k
        return (left->line_start < right->line_start) ? 
-128.7k
:
19.58k
;
  Branch (52:16): [True: 28.7k, False: 9.58k]
53
38.2k
    }
54
55
94.2k
    if (left->column_start != right->column_start) {
  Branch (55:9): [True: 81.7k, False: 12.4k]
56
81.7k
        return (left->column_start < right->column_start) ? 
-166.7k
:
115.0k
;
  Branch (56:16): [True: 66.7k, False: 15.0k]
57
81.7k
    }
58
59
12.4k
    if (left->idx != right->idx) {
  Branch (59:9): [True: 12.4k, False: 0]
60
12.4k
        return (left->idx < right->idx) ? 1 : 
-10
;
  Branch (60:16): [True: 12.4k, False: 0]
61
12.4k
    }
62
63
0
    return 0;
64
12.4k
}
65
66
/**
67
 * Return the number of the last line of the source that can be displayed. When
68
 * the source ends with a newline, the empty line after it is not displayed.
69
 */
70
static int32_t
71
932
pm_rich_errors_final_line(const pm_parser_t *parser) {
72
932
    const pm_line_offset_list_t *line_offsets = &parser->line_offsets;
73
932
    int32_t final_line = parser->start_line + ((int32_t) line_offsets->size) - 1;
74
75
932
    if ((parser->start + line_offsets->offsets[line_offsets->size - 1]) == parser->end) {
  Branch (75:9): [True: 60, False: 872]
76
60
        final_line--;
77
60
    }
78
79
932
    return final_line;
80
932
}
81
82
/**
83
 * Find the range of lines that may be displayed around an error on the given
84
 * line: up to two lines of context on either side of it, limited to the lines
85
 * in the source. The error's own line is always included, even when it is the
86
 * empty line after a trailing newline.
87
 */
88
static void
89
35.5k
pm_rich_error_context(int32_t line, int32_t start_line, int32_t final_line, int32_t *first, int32_t *last) {
90
35.5k
    *first = (line - start_line > 2) ? 
(line - 2)26.2k
:
start_line9.25k
;
  Branch (90:14): [True: 26.2k, False: 9.25k]
91
35.5k
    *last = (final_line - line > 2) ? 
(line + 2)20.1k
:
final_line15.3k
;
  Branch (91:13): [True: 20.1k, False: 15.3k]
92
35.5k
    if (*last < line) 
*last = line26
;
  Branch (92:9): [True: 26, False: 35.4k]
93
35.5k
}
94
95
/**
96
 * Check if the source that will be displayed for the given rich errors is
97
 * valid UTF-8. That is each error's line and the lines of context around it.
98
 * The errors must be sorted by their line. Each line is checked at most once,
99
 * so that many errors on the same long line cost time proportional to the
100
 * line, not to the line times the number of errors.
101
 */
102
static bool
103
466
rich_errors_are_utf8(const pm_parser_t *parser, size_t nerrors, const pm_rich_error_t *rich_errors) {
104
466
    int32_t final_line = pm_rich_errors_final_line(parser);
105
106
    /* Every line from the first line of context of the most recently visited
107
     * error up to (but not including) this index has already been checked.
108
     * Because the errors are sorted, the first line of context never
109
     * decreases, so any later error only needs the lines from this index
110
     * onward. */
111
466
    size_t checked_index = 0;
112
113
35.0k
    for (size_t idx = 0; idx < nerrors; 
idx++34.5k
) {
  Branch (113:26): [True: 34.5k, False: 466]
114
34.5k
        int32_t first_line;
115
34.5k
        int32_t last_line;
116
34.5k
        pm_rich_error_context(rich_errors[idx].line_start, parser->start_line, final_line, &first_line, &last_line);
117
118
34.5k
        size_t start_index = (size_t) (first_line - parser->start_line);
119
34.5k
        size_t end_index = (size_t) (last_line - parser->start_line);
120
121
34.5k
        if (start_index < checked_index) 
start_index = checked_index34.0k
;
  Branch (121:13): [True: 34.0k, False: 490]
122
34.5k
        if (start_index > end_index) 
continue32.3k
;
  Branch (122:13): [True: 32.3k, False: 2.26k]
123
124
2.26k
        if (!lines_are_utf8(parser, start_index, end_index)) 
return 0
false0
;
  Branch (124:13): [True: 0, False: 2.26k]
125
2.26k
        checked_index = end_index + 1;
126
2.26k
    }
127
128
466
    return true;
129
466
}
130
131
#define COLOR_BOLD "\033[1m"
132
#define COLOR_GRAY "\033[2m"
133
#define COLOR_RED "\033[1;31m"
134
#define COLOR_RESET "\033[m"
135
18.2k
#define TRUNCATE 30
136
137
/* The maximum number of columns of a line that errors are displayed beneath
138
 * before the line is displayed again. This must be at least TRUNCATE so that
139
 * every window after the first on a line starts with a truncated line. Lines
140
 * of context are cut off at the same width. */
141
5.87k
#define WINDOW 80
142
143
typedef struct {
144
    /** The type of formatting to use when formatting the errors. */
145
    pm_errors_format_type_t type;
146
147
    /**
148
     * The number of columns taken up by the widest line number that will be
149
     * displayed, including the sign of a negative line number. The widest
150
     * possible is the 11 columns of -2147483648.
151
     */
152
    uint8_t width;
153
} pm_error_line_format_t;
154
155
/**
156
 * Write the decimal digits of the given line number, including the sign if it
157
 * is negative, so that they end at the end of the given buffer. The buffer is
158
 * large enough for the widest possible line number, -2147483648. Returns the
159
 * number of characters written.
160
 */
161
static uint8_t
162
7.27k
pm_error_line_digits(int32_t line, char digits[11]) {
163
    /* The magnitude is computed in unsigned arithmetic so that negating the
164
     * minimum value of a 32-bit integer is well-defined. */
165
7.27k
    uint32_t magnitude = (line < 0) ? 
(0U - (uint32_t) line)0
: (uint32_t) line;
  Branch (165:26): [True: 0, False: 7.27k]
166
7.27k
    uint8_t length = 0;
167
168
8.66k
    do {
169
8.66k
        digits[10 - length++] = (char) ('0' + (magnitude % 10));
170
8.66k
        magnitude /= 10;
171
8.66k
    } while (magnitude > 0);
  Branch (171:14): [True: 1.38k, False: 7.27k]
172
173
7.27k
    if (line < 0) 
digits[10 - length++] = '-'0
;
  Branch (173:9): [True: 0, False: 7.27k]
174
7.27k
    return length;
175
7.27k
}
176
177
/**
178
 * Append the given line number to the buffer, right aligned within the given
179
 * width.
180
 */
181
static void
182
6.34k
pm_error_append_line_number(pm_buffer_t *buffer, int32_t line, uint8_t width) {
183
6.34k
    char digits[11];
184
6.34k
    uint8_t length = pm_error_line_digits(line, digits);
185
186
6.34k
    if (width > length) 
pm_buffer_append_repeated(buffer, ' ', (size_t) (width - length))2.01k
;
  Branch (186:9): [True: 2.01k, False: 4.32k]
187
6.34k
    pm_buffer_append_string(buffer, digits + sizeof(digits) - length, length);
188
6.34k
}
189
190
/**
191
 * Initialize the given line format struct based on whether or not to highlight
192
 * the line and the first and last line numbers that will be displayed. Every
193
 * line number in between is no wider than one of these two.
194
 */
195
static void
196
466
pm_error_line_format_init(pm_error_line_format_t *format, pm_errors_format_type_t format_type, int32_t first_line, int32_t last_line) {
197
466
    char digits[11];
198
466
    uint8_t first_width = pm_error_line_digits(first_line, digits);
199
466
    uint8_t last_width = pm_error_line_digits(last_line, digits);
200
201
466
    *format = (pm_error_line_format_t) {
202
466
        .type = format_type,
203
466
        .width = (first_width > last_width) ? 
first_width0
: last_width
  Branch (203:18): [True: 0, False: 466]
204
466
    };
205
466
}
206
207
/**
208
 * Append the prefix for a line of source, which displays the line number right
209
 * aligned within the width of the widest line number.
210
 */
211
static void
212
5.87k
pm_error_line_format_number(const pm_error_line_format_t *format, pm_buffer_t *buffer, int32_t line) {
213
5.87k
    if (format->type > PM_ERRORS_FORMAT_PLAIN) 
PM_BUFFER_APPEND_LITERAL2.93k
(buffer, COLOR_GRAY);
  Branch (213:9): [True: 2.93k, False: 2.93k]
214
5.87k
    pm_error_append_line_number(buffer, line, format->width);
215
5.87k
    PM_BUFFER_APPEND_LITERAL(buffer, " | ");
216
5.87k
    if (format->type > PM_ERRORS_FORMAT_PLAIN) 
PM_BUFFER_APPEND_LITERAL2.93k
(buffer, COLOR_RESET);
  Branch (216:9): [True: 2.93k, False: 2.93k]
217
5.87k
}
218
219
/**
220
 * Append the prefix for a line beneath the source that displays an error, which
221
 * is blank where the line number would be so that the columns line up.
222
 */
223
static void
224
34.5k
pm_error_line_format_blank(const pm_error_line_format_t *format, pm_buffer_t *buffer) {
225
34.5k
    if (format->type > PM_ERRORS_FORMAT_PLAIN) 
PM_BUFFER_APPEND_LITERAL17.2k
(buffer, COLOR_GRAY);
  Branch (225:9): [True: 17.2k, False: 17.2k]
226
34.5k
    pm_buffer_append_repeated(buffer, ' ', (size_t) format->width + 1);
227
34.5k
    PM_BUFFER_APPEND_LITERAL(buffer, "| ");
228
34.5k
    if (format->type > PM_ERRORS_FORMAT_PLAIN) 
PM_BUFFER_APPEND_LITERAL17.2k
(buffer, COLOR_RESET);
  Branch (228:9): [True: 17.2k, False: 17.2k]
229
34.5k
}
230
231
/**
232
 * Append the divider that marks lines of source that are skipped between two
233
 * displayed snippets. It extends just past the line number prefix.
234
 */
235
static void
236
10
pm_error_line_format_divider(const pm_error_line_format_t *format, pm_buffer_t *buffer) {
237
10
    if (format->type > PM_ERRORS_FORMAT_PLAIN) 
PM_BUFFER_APPEND_LITERAL5
(buffer, COLOR_GRAY);
  Branch (237:9): [True: 5, False: 5]
238
10
    PM_BUFFER_APPEND_LITERAL(buffer, "  ");
239
10
    pm_buffer_append_repeated(buffer, '~', (size_t) format->width + 4);
240
10
    if (format->type > PM_ERRORS_FORMAT_PLAIN) 
PM_BUFFER_APPEND_LITERAL5
(buffer, COLOR_RESET);
  Branch (240:9): [True: 5, False: 5]
241
10
    pm_buffer_append_byte(buffer, '\n');
242
10
}
243
244
/**
245
 * Advance the given column of the line that begins at start by whole
246
 * characters until it reaches the given limit, and return the number of
247
 * characters passed over. Each character is displayed in one column. A byte
248
 * that does not begin a valid character, or that is past the end of the
249
 * source, counts as one character.
250
 */
251
static size_t
252
103k
pm_error_advance_columns(const pm_parser_t *parser, const uint8_t *start, size_t *column, size_t limit) {
253
103k
    size_t count = 0;
254
255
1.36M
    while (*column < limit) {
  Branch (255:12): [True: 1.26M, False: 103k]
256
1.26M
        const uint8_t *cursor = start + *column;
257
1.26M
        size_t char_width = 1;
258
259
        /* Every supported encoding is ASCII-compatible, so a byte below 0x80
260
         * at the start of a character is a character by itself. */
261
1.26M
        if ((cursor < parser->end) && 
(*cursor >= 0x80)1.26M
) {
  Branch (261:13): [True: 1.26M, False: 410]
  Branch (261:39): [True: 0, False: 1.26M]
262
0
            char_width = pm_parser_encoding_char_width(parser, cursor, parser->end - cursor);
263
0
            if (char_width == 0) char_width = 1;
  Branch (263:17): [True: 0, False: 0]
264
0
        }
265
266
1.26M
        *column += char_width;
267
1.26M
        count++;
268
1.26M
    }
269
270
103k
    return count;
271
103k
}
272
273
static void
274
5.87k
pm_error_format_line(const pm_parser_t *parser, pm_buffer_t *buffer, const pm_error_line_format_t *format, int32_t line, uint32_t column_start, uint32_t column_end) {
275
5.87k
    int32_t line_delta = line - parser->start_line;
276
5.87k
    PRISM_ASSERT(line_delta >= 0);
  Branch (276:5): [True: 0, False: 5.87k]
  Branch (276:5): [True: 5.87k, False: 0]
277
278
5.87k
    size_t index = (size_t) line_delta;
279
5.87k
    PRISM_ASSERT(index < parser->line_offsets.size);
  Branch (279:5): [True: 0, False: 5.87k]
  Branch (279:5): [True: 5.87k, False: 0]
280
281
5.87k
    const uint8_t *start = parser->start + parser->line_offsets.offsets[index];
282
5.87k
    const uint8_t *end;
283
284
5.87k
    if (index >= parser->line_offsets.size - 1) {
  Branch (284:9): [True: 724, False: 5.15k]
285
724
        end = parser->end;
286
5.15k
    } else {
287
5.15k
        end = parser->start + parser->line_offsets.offsets[index + 1];
288
5.15k
    }
289
290
5.87k
    pm_error_line_format_number(format, buffer, line);
291
292
    /* Here we determine if we should truncate the start of the line. The
293
     * column is the start of a diagnostic, so it is on a character boundary. */
294
5.87k
    bool truncate_start = column_start >= TRUNCATE;
295
5.87k
    const uint8_t *display_start = truncate_start ? 
(start + column_start)2.04k
:
start3.83k
;
  Branch (295:36): [True: 2.04k, False: 3.83k]
296
297
    /* Here we determine if we should truncate the end of the line. Note that
298
     * this is written to avoid computing start + column_end, which could be
299
     * more than one past the end of the source when the error is at the end
300
     * of the input. The walk to find a character boundary begins at the
301
     * displayed start so that its cost is bounded by the displayed width. */
302
5.87k
    bool truncate_end = false;
303
5.87k
    if ((column_end != 0) && ((end - start) - ((ptrdiff_t) column_end) >= TRUNCATE)) {
  Branch (303:9): [True: 5.87k, False: 0]
  Branch (303:30): [True: 1.78k, False: 4.09k]
304
1.78k
        const uint8_t *end_candidate = start + column_end + TRUNCATE;
305
306
177k
        for (const uint8_t *cursor = display_start; cursor < end_candidate;) {
  Branch (306:53): [True: 175k, False: 1.78k]
307
175k
            size_t char_width = pm_parser_encoding_char_width(parser, cursor, parser->end - cursor);
308
309
            /* If we failed to decode a character, then just bail out and
310
             * truncate at the fixed width. */
311
175k
            if (char_width == 0) 
break0
;
  Branch (311:17): [True: 0, False: 175k]
312
313
            /* If this next character would go past the end candidate,
314
             * then we need to truncate before it. */
315
175k
            if (cursor + char_width > end_candidate) {
  Branch (315:17): [True: 0, False: 175k]
316
0
                end_candidate = cursor;
317
0
                break;
318
0
            }
319
320
175k
            cursor += char_width;
321
175k
        }
322
323
1.78k
        end = end_candidate;
324
1.78k
        truncate_end = true;
325
1.78k
    }
326
327
5.87k
    if (truncate_start) 
PM_BUFFER_APPEND_LITERAL2.04k
(buffer, "... ");
  Branch (327:9): [True: 2.04k, False: 3.83k]
328
5.87k
    pm_buffer_append_string(buffer, (const char *) display_start, (size_t) (end - display_start));
329
330
5.87k
    if (truncate_end) {
  Branch (330:9): [True: 1.78k, False: 4.09k]
331
1.78k
        PM_BUFFER_APPEND_LITERAL(buffer, " ...\n");
332
4.09k
    } else if (end == parser->end && 
end > parser->start462
&&
end[-1] != '\n'462
) {
  Branch (332:16): [True: 462, False: 3.63k]
  Branch (332:38): [True: 462, False: 0]
  Branch (332:61): [True: 412, False: 50]
333
412
        pm_buffer_append_byte(buffer, '\n');
334
412
    }
335
5.87k
}
336
337
/**
338
 * Display a line of context around an error, which is cut off at the same width
339
 * as a window so that a long line does not dominate the output.
340
 */
341
static void
342
1.21k
pm_error_format_context_line(const pm_parser_t *parser, pm_buffer_t *buffer, const pm_error_line_format_t *format, int32_t line) {
343
1.21k
    PM_BUFFER_APPEND_LITERAL(buffer, "  ");
344
1.21k
    pm_error_format_line(parser, buffer, format, line, 0, WINDOW);
345
1.21k
}
346
347
static void
348
466
pm_rich_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, pm_errors_format_type_t format_type, size_t nerrors, pm_rich_error_t *rich_errors, bool inline_messages) {
349
466
    int32_t final_line = pm_rich_errors_final_line(parser);
350
351
    /* The line numbers that are displayed range from the first line of context
352
     * of the first error to the last line of context of the last error. */
353
466
    int32_t first_displayed_line;
354
466
    int32_t last_displayed_line;
355
466
    int32_t unused_line;
356
466
    pm_rich_error_context(rich_errors[0].line_start, parser->start_line, final_line, &first_displayed_line, &unused_line);
357
466
    pm_rich_error_context(rich_errors[nerrors - 1].line_start, parser->start_line, final_line, &unused_line, &last_displayed_line);
358
359
466
    pm_error_line_format_t format;
360
466
    pm_error_line_format_init(&format, format_type, first_displayed_line, last_displayed_line);
361
362
    /* We are going to iterate through every error in our error list and display
363
     * it. While we are iterating, we will display some padding lines of the
364
     * source before the error to give some context. We will be careful not to
365
     * display the same line twice in case the errors are close enough in the
366
     * source.
367
     *
368
     * The errors on a line are split into windows of at most WINDOW columns.
369
     * Each window displays its own slice of the line, and the markers beneath
370
     * it are padded relative to the start of that slice and cut off at its
371
     * end. This bounds the length of every marker line, so the output grows
372
     * linearly with the number of errors no matter how long the line is. */
373
466
    int32_t last_line = parser->start_line - 1;
374
466
    uint32_t window_column_start = 0;
375
466
    uint32_t window_column_end = 0;
376
466
    size_t window_end_idx = 0;
377
378
35.0k
    for (size_t idx = 0; idx < nerrors; 
idx++34.5k
) {
  Branch (378:26): [True: 34.5k, False: 466]
379
34.5k
        pm_rich_error_t *rich_error = rich_errors + idx;
380
381
        /* Here we determine how many lines of padding of the source to display,
382
         * based on the difference from the last line that was displayed. */
383
34.5k
        if (rich_error->line_start - last_line > 1) {
  Branch (383:13): [True: 348, False: 34.2k]
384
348
            if (rich_error->line_start - last_line > 2) {
  Branch (384:17): [True: 172, False: 176]
385
172
                if ((idx != 0) && 
(rich_error->line_start - last_line > 3)24
) {
  Branch (385:21): [True: 24, False: 148]
  Branch (385:35): [True: 10, False: 14]
386
10
                    pm_error_line_format_divider(&format, buffer);
387
10
                }
388
389
172
                pm_error_format_context_line(parser, buffer, &format, rich_error->line_start - 2);
390
172
            }
391
392
348
            pm_error_format_context_line(parser, buffer, &format, rich_error->line_start - 1);
393
348
        }
394
395
        /* If this error is not in the current window (because it is the first
396
         * error, it is on a new line, or it is too far to the right), then we
397
         * start a new window and display the line that has the error in it. */
398
34.5k
        if (idx == window_end_idx) {
  Branch (398:13): [True: 4.66k, False: 29.9k]
399
4.66k
            switch (format_type) {
  Branch (399:21): [True: 4.66k, False: 0]
400
2.33k
                case PM_ERRORS_FORMAT_PLAIN:
  Branch (400:17): [True: 2.33k, False: 2.33k]
401
2.33k
                    PM_BUFFER_APPEND_LITERAL(buffer, "> ");
402
2.33k
                    break;
403
0
                case PM_ERRORS_FORMAT_STYLE:
  Branch (403:17): [True: 0, False: 4.66k]
404
0
                    PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD "> " COLOR_RESET);
405
0
                    break;
406
2.33k
                case PM_ERRORS_FORMAT_COLOR:
  Branch (406:17): [True: 2.33k, False: 2.33k]
407
2.33k
                    PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED "> " COLOR_RESET);
408
2.33k
                    break;
409
4.66k
            }
410
411
            /* The displayed slice of the line starts at this error, unless it
412
             * is close enough to the start of the line to show it all. */
413
4.66k
            window_column_start = (rich_error->column_start >= TRUNCATE) ? 
rich_error->column_start2.04k
:
02.62k
;
  Branch (413:35): [True: 2.04k, False: 2.62k]
414
4.66k
            uint32_t window_limit = window_column_start + WINDOW;
415
416
            /* Find the errors on this line that start within the window, and
417
             * the maximum column end of all of them, capped at the window. */
418
4.66k
            window_column_end = rich_error->column_end;
419
34.5k
            for (window_end_idx = idx + 1; window_end_idx < nerrors; 
window_end_idx++29.9k
) {
  Branch (419:44): [True: 34.1k, False: 466]
420
34.1k
                const pm_rich_error_t *next_error = &rich_errors[window_end_idx];
421
34.1k
                if ((next_error->line_start != rich_error->line_start) || 
(next_error->column_start >= window_limit)31.6k
)
break4.19k
;
  Branch (421:21): [True: 2.42k, False: 31.6k]
  Branch (421:75): [True: 1.77k, False: 29.9k]
422
29.9k
                if (next_error->column_end > window_column_end) 
window_column_end = next_error->column_end13.9k
;
  Branch (422:21): [True: 13.9k, False: 15.9k]
423
29.9k
            }
424
425
4.66k
            if (window_column_end > window_limit) 
window_column_end = window_limit996
;
  Branch (425:17): [True: 996, False: 3.66k]
426
4.66k
            pm_error_format_line(parser, buffer, &format, rich_error->line_start, rich_error->column_start, window_column_end);
427
4.66k
        }
428
429
34.5k
        const uint8_t *start = parser->start + parser->line_offsets.offsets[rich_error->line_start - parser->start_line];
430
34.5k
        if (start == parser->end) 
pm_buffer_append_byte(buffer, '\n')18
;
  Branch (430:13): [True: 18, False: 34.5k]
431
432
        /* Now we will display the actual error message. We will do this by
433
         * first putting the prefix to the line, then a bunch of blank spaces
434
         * depending on the column, then as many tildes as we need to display
435
         * the width of the error, then the error message itself.
436
         *
437
         * Note that this doesn't take into account the width of the actual
438
         * character when displayed in the terminal. For some east-asian
439
         * languages or emoji, this means it can be thrown off pretty badly. We
440
         * will need to solve this eventually. */
441
34.5k
        PM_BUFFER_APPEND_LITERAL(buffer, "  ");
442
34.5k
        pm_error_line_format_blank(&format, buffer);
443
444
34.5k
        size_t column = 0;
445
34.5k
        if (window_column_start > 0) {
  Branch (445:13): [True: 16.7k, False: 17.7k]
446
16.7k
            PM_BUFFER_APPEND_LITERAL(buffer, "    ");
447
16.7k
            column = window_column_start;
448
16.7k
        }
449
450
34.5k
        pm_buffer_append_repeated(buffer, ' ', pm_error_advance_columns(parser, start, &column, rich_error->column_start));
451
452
34.5k
        switch (format_type) {
  Branch (452:17): [True: 34.5k, False: 0]
453
17.2k
            case PM_ERRORS_FORMAT_PLAIN:
  Branch (453:13): [True: 17.2k, False: 17.2k]
454
17.2k
                pm_buffer_append_byte(buffer, '^');
455
17.2k
                break;
456
0
            case PM_ERRORS_FORMAT_STYLE:
  Branch (456:13): [True: 0, False: 34.5k]
457
0
                PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD "^");
458
0
                break;
459
17.2k
            case PM_ERRORS_FORMAT_COLOR:
  Branch (459:13): [True: 17.2k, False: 17.2k]
460
17.2k
                PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED "^");
461
17.2k
                break;
462
34.5k
        }
463
464
        /* The caret marks the first character of the error, and the tildes
465
         * mark the rest of it, up to the end of the window. */
466
34.5k
        pm_error_advance_columns(parser, start, &column, column + 1);
467
34.5k
        size_t tilde_limit = (rich_error->column_end < window_column_end) ? 
rich_error->column_end26.7k
:
window_column_end7.84k
;
  Branch (467:30): [True: 26.7k, False: 7.84k]
468
34.5k
        pm_buffer_append_repeated(buffer, '~', pm_error_advance_columns(parser, start, &column, tilde_limit));
469
470
34.5k
        if (format_type != PM_ERRORS_FORMAT_PLAIN) {
  Branch (470:13): [True: 17.2k, False: 17.2k]
471
17.2k
            PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RESET);
472
17.2k
        }
473
474
34.5k
        if (inline_messages) {
  Branch (474:13): [True: 34.5k, False: 0]
475
34.5k
            const char *message = pm_diagnostic_message(rich_error->diagnostic);
476
477
34.5k
            pm_buffer_append_byte(buffer, ' ');
478
34.5k
            pm_buffer_append_string(buffer, message, strlen(message));
479
34.5k
        }
480
481
34.5k
        pm_buffer_append_byte(buffer, '\n');
482
483
        /* Here we determine how many lines of padding to display after the
484
         * diagnostic, depending on where the next diagnostic is in source. The
485
         * line after the last error is one past the last line that can be
486
         * displayed. */
487
34.5k
        last_line = rich_error->line_start;
488
34.5k
        int32_t next_line = (idx == nerrors - 1) ? 
(final_line + 1)466
:
rich_errors[idx + 1].line_start34.1k
;
  Branch (488:29): [True: 466, False: 34.1k]
489
490
34.5k
        if (next_line - last_line > 1) {
  Branch (490:13): [True: 494, False: 34.0k]
491
494
            pm_error_format_context_line(parser, buffer, &format, ++last_line);
492
494
        }
493
494
34.5k
        if (next_line - last_line > 1) {
  Branch (494:13): [True: 200, False: 34.3k]
495
200
            pm_error_format_context_line(parser, buffer, &format, ++last_line);
496
200
        }
497
34.5k
    }
498
466
}
499
500
static void
501
34.5k
pm_rich_error_init(const pm_parser_t *parser, pm_rich_error_t *rich_error, pm_diagnostic_t *diagnostic, pm_location_t *location) {
502
34.5k
    const pm_line_offset_list_t *line_offsets = &parser->line_offsets;
503
34.5k
    pm_line_column_t start_line_column = pm_line_offset_list_line_column(line_offsets, location->start, parser->start_line);
504
34.5k
    pm_line_column_t end_line_column = pm_line_offset_list_line_column(line_offsets, location->start + location->length, parser->start_line);
505
506
34.5k
    uint32_t column_end;
507
34.5k
    if (start_line_column.line == end_line_column.line) {
  Branch (507:9): [True: 33.4k, False: 1.17k]
508
33.4k
        column_end = end_line_column.column;
509
33.4k
    } else {
510
1.17k
        column_end = (uint32_t) (line_offsets->offsets[start_line_column.line - parser->start_line + 1] - line_offsets->offsets[start_line_column.line - parser->start_line] - 1);
511
1.17k
    }
512
513
    /* Ensure we have at least one column of error. */
514
34.5k
    if (start_line_column.column == column_end) 
column_end++1.89k
;
  Branch (514:9): [True: 1.89k, False: 32.6k]
515
516
34.5k
    *rich_error = (pm_rich_error_t) {
517
34.5k
        .diagnostic = diagnostic,
518
34.5k
        .line_start = start_line_column.line,
519
34.5k
        .column_start = start_line_column.column,
520
34.5k
        .column_end = column_end
521
34.5k
    };
522
34.5k
}
523
524
/**
525
 * Append a line that names the file and line of an error along with its
526
 * message, which is how an error is displayed without a source snippet.
527
 */
528
static void
529
466
pm_error_append_summary(const pm_parser_t *parser, pm_buffer_t *buffer, int32_t line, const char *message) {
530
466
    pm_buffer_append_string(buffer, (const char *) pm_string_source(&parser->filepath), pm_string_length(&parser->filepath));
531
466
    pm_buffer_append_byte(buffer, ':');
532
466
    pm_error_append_line_number(buffer, line, 0);
533
466
    PM_BUFFER_APPEND_LITERAL(buffer, ": ");
534
466
    pm_buffer_append_string(buffer, message, strlen(message));
535
466
}
536
537
pm_error_level_t
538
466
pm_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, pm_errors_format_type_t format_type) {
539
466
    size_t nerrors = parser->error_list.size;
540
466
    PRISM_ASSERT(nerrors > 0);
  Branch (540:5): [True: 0, False: 466]
  Branch (540:5): [True: 466, False: 0]
541
542
466
    size_t rich_errors_size = nerrors * sizeof(pm_rich_error_t);
543
466
    if (rich_errors_size / sizeof(pm_rich_error_t) != nerrors) 
PRISM_OOM0
();
  Branch (543:9): [True: 0, False: 466]
544
545
466
    pm_rich_error_t *rich_errors = xmalloc(rich_errors_size);
546
466
    if (rich_errors == NULL) 
PRISM_OOM0
();
  Branch (546:9): [True: 0, False: 466]
547
548
466
    size_t rich_errors_idx = 0;
549
550
35.0k
    for (pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) parser->error_list.head; diagnostic != NULL; 
diagnostic = (pm_diagnostic_t *) diagnostic->node.next34.5k
) {
  Branch (550:85): [True: 34.5k, False: 466]
551
34.5k
        pm_location_t location = pm_diagnostic_location(diagnostic);
552
553
34.5k
        switch (pm_diagnostic_error_level(diagnostic)) {
  Branch (553:17): [True: 34.5k, False: 0]
554
34.5k
          case PM_ERROR_LEVEL_SYNTAX: {
  Branch (554:11): [True: 34.5k, False: 0]
555
34.5k
            pm_rich_error_init(parser, &rich_errors[rich_errors_idx], diagnostic, &location);
556
34.5k
            rich_errors[rich_errors_idx].idx = rich_errors_idx;
557
34.5k
            rich_errors_idx++;
558
34.5k
            break;
559
0
          }
560
0
          case PM_ERROR_LEVEL_ARGUMENT: {
  Branch (560:11): [True: 0, False: 34.5k]
561
            /* If we get an argument error, we want to format it and return it
562
             * immediately. They should include a snippet of the source if
563
             * possible. */
564
0
            pm_rich_error_init(parser, rich_errors, diagnostic, &location);
565
0
            pm_error_append_summary(parser, buffer, rich_errors[0].line_start, pm_diagnostic_message(diagnostic));
566
567
0
            if (rich_errors_are_utf8(parser, 1, rich_errors)) {
  Branch (567:17): [True: 0, False: 0]
568
0
                pm_buffer_append_byte(buffer, '\n');
569
0
                pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors, false);
570
0
            }
571
572
0
            xfree_sized(rich_errors, rich_errors_size);
573
0
            return PM_ERROR_LEVEL_ARGUMENT;
574
0
          }
575
0
          case PM_ERROR_LEVEL_LOAD: {
  Branch (575:11): [True: 0, False: 34.5k]
576
            /* If we get a load error, we want to format it and return it
577
             * immediately. It should only include the diagnostic message. */
578
0
            const char *message = pm_diagnostic_message(diagnostic);
579
0
            pm_buffer_append_string(buffer, message, strlen(message));
580
581
0
            xfree_sized(rich_errors, rich_errors_size);
582
0
            return PM_ERROR_LEVEL_LOAD;
583
0
          }
584
0
          case PM_ERROR_LEVEL_STACK: {
  Branch (584:11): [True: 0, False: 34.5k]
585
            /* If we get a stack error, we want to format it and return it
586
             * immediately. Once the stack is exhausted, the syntax errors
587
             * around it come from recovering from that point rather than from
588
             * the source, so they are left out. It should include a snippet of
589
             * the source if possible. */
590
0
            pm_rich_error_init(parser, rich_errors, diagnostic, &location);
591
0
            pm_error_append_summary(parser, buffer, rich_errors[0].line_start, pm_diagnostic_message(diagnostic));
592
593
0
            if (rich_errors_are_utf8(parser, 1, rich_errors)) {
  Branch (593:17): [True: 0, False: 0]
594
0
                pm_buffer_append_byte(buffer, '\n');
595
0
                pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors, false);
596
0
            }
597
598
0
            xfree_sized(rich_errors, rich_errors_size);
599
0
            return PM_ERROR_LEVEL_STACK;
600
0
          }
601
34.5k
        }
602
34.5k
    }
603
604
466
    PRISM_ASSERT(rich_errors_idx == nerrors);
  Branch (604:5): [True: 0, False: 466]
  Branch (604:5): [True: 466, False: 0]
605
606
    /* The header displays the line of the first error in parse order, which
607
     * is the first entry in the array because it has not been sorted yet. */
608
466
    pm_error_append_summary(parser, buffer, rich_errors[0].line_start, (nerrors > 1) ? 
"syntax errors found\n"464
:
"syntax error found\n"2
);
  Branch (608:72): [True: 464, False: 2]
609
610
    /* Sort the errors so that they are in the order that they appear in the
611
     * source. This is the order they are displayed in, and it also lets the
612
     * UTF-8 check visit each line only once. */
613
466
    qsort(rich_errors, nerrors, sizeof(pm_rich_error_t), pm_rich_error_compare);
614
615
466
    if (rich_errors_are_utf8(parser, nerrors, rich_errors)) {
  Branch (615:9): [True: 466, False: 0]
616
        /* In here, we have all of the rich diagnostic information and we know
617
         * the snippets are valid UTF-8, so we format them all together with
618
         * the source code snippets. */
619
466
        pm_rich_errors_format(parser, buffer, format_type, nerrors, rich_errors, true);
620
466
    } else {
621
        /* In here, we have content in the source that is not valid UTF-8. In
622
         * that case, we do not want to attempt to format the source code
623
         * snippets because we do not know how the terminal will handle it. The
624
         * diagnostics are displayed in the order that they appear in the
625
         * source, as they are when the snippets are displayed. */
626
0
        for (size_t idx = 0; idx < nerrors; idx++) {
  Branch (626:30): [True: 0, False: 0]
627
0
            if (idx > 0) pm_buffer_append_byte(buffer, '\n');
  Branch (627:17): [True: 0, False: 0]
628
0
            pm_error_append_summary(parser, buffer, rich_errors[idx].line_start, pm_diagnostic_message(rich_errors[idx].diagnostic));
629
0
        }
630
0
    }
631
632
466
    xfree_sized(rich_errors, rich_errors_size);
633
466
    return PM_ERROR_LEVEL_SYNTAX;
634
466
}