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 | 67.1k | lines_are_utf8(const pm_parser_t *parser, size_t start_index, size_t end_index) { |
24 | 67.1k | const pm_line_offset_list_t *line_offsets = &parser->line_offsets; |
25 | 67.1k | const uint8_t *cursor = parser->start + line_offsets->offsets[start_index]; |
26 | 67.1k | const uint8_t *end = (end_index + 1 == line_offsets->size) ? parser->end3.41k : (parser->start + line_offsets->offsets[end_index + 1])63.7k ; Branch (26:26): [True: 3.41k, False: 63.7k]
|
27 | | |
28 | 2.37M | while (cursor < end) { Branch (28:12): [True: 2.32M, False: 53.5k]
|
29 | 2.32M | size_t width = pm_encoding_utf_8_char_width(cursor, end - cursor); |
30 | 2.32M | if (width == 0) return 13.6k false13.6k ; Branch (30:13): [True: 13.6k, False: 2.30M]
|
31 | | |
32 | 2.30M | cursor += width; |
33 | 2.30M | } |
34 | | |
35 | 53.5k | return true; |
36 | 67.1k | } |
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 | 1.66M | pm_rich_error_compare(const void *a, const void *b) { |
48 | 1.66M | const pm_rich_error_t *left = (const pm_rich_error_t *) a; |
49 | 1.66M | const pm_rich_error_t *right = (const pm_rich_error_t *) b; |
50 | | |
51 | 1.66M | if (left->line_start != right->line_start) { Branch (51:9): [True: 622k, False: 1.04M]
|
52 | 622k | return (left->line_start < right->line_start) ? -1556k : 165.3k ; Branch (52:16): [True: 556k, False: 65.3k]
|
53 | 622k | } |
54 | | |
55 | 1.04M | if (left->column_start != right->column_start) { Branch (55:9): [True: 780k, False: 265k]
|
56 | 780k | return (left->column_start < right->column_start) ? -1716k : 164.0k ; Branch (56:16): [True: 716k, False: 64.0k]
|
57 | 780k | } |
58 | | |
59 | 265k | if (left->idx != right->idx) { Branch (59:9): [True: 265k, False: 0]
|
60 | 265k | return (left->idx < right->idx) ? 1 : -10 ; Branch (60:16): [True: 265k, False: 0]
|
61 | 265k | } |
62 | | |
63 | 0 | return 0; |
64 | 265k | } |
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 | 31.2k | pm_rich_errors_final_line(const pm_parser_t *parser) { |
72 | 31.2k | const pm_line_offset_list_t *line_offsets = &parser->line_offsets; |
73 | 31.2k | int32_t final_line = parser->start_line + ((int32_t) line_offsets->size) - 1; |
74 | | |
75 | 31.2k | if ((parser->start + line_offsets->offsets[line_offsets->size - 1]) == parser->end) { Branch (75:9): [True: 22.6k, False: 8.59k]
|
76 | 22.6k | final_line--; |
77 | 22.6k | } |
78 | | |
79 | 31.2k | return final_line; |
80 | 31.2k | } |
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 | 218k | pm_rich_error_context(int32_t line, int32_t start_line, int32_t final_line, int32_t *first, int32_t *last) { |
90 | 218k | *first = (line - start_line > 2) ? (line - 2)154k : start_line64.2k ; Branch (90:14): [True: 154k, False: 64.2k]
|
91 | 218k | *last = (final_line - line > 2) ? (line + 2)173k : final_line44.9k ; Branch (91:13): [True: 173k, False: 44.9k]
|
92 | 218k | if (*last < line) *last = line2.18k ; Branch (92:9): [True: 2.18k, False: 216k]
|
93 | 218k | } |
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 | 22.4k | rich_errors_are_utf8(const pm_parser_t *parser, size_t nerrors, const pm_rich_error_t *rich_errors) { |
104 | 22.4k | 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 | 22.4k | size_t checked_index = 0; |
112 | | |
113 | 209k | for (size_t idx = 0; idx < nerrors; idx++187k ) { Branch (113:26): [True: 200k, False: 8.80k]
|
114 | 200k | int32_t first_line; |
115 | 200k | int32_t last_line; |
116 | 200k | pm_rich_error_context(rich_errors[idx].line_start, parser->start_line, final_line, &first_line, &last_line); |
117 | | |
118 | 200k | size_t start_index = (size_t) (first_line - parser->start_line); |
119 | 200k | size_t end_index = (size_t) (last_line - parser->start_line); |
120 | | |
121 | 200k | if (start_index < checked_index) start_index = checked_index164k ; Branch (121:13): [True: 164k, False: 35.8k]
|
122 | 200k | if (start_index > end_index) continue133k ; Branch (122:13): [True: 133k, False: 67.1k]
|
123 | | |
124 | 67.1k | if (!lines_are_utf8(parser, start_index, end_index)) return 13.6k false13.6k ; Branch (124:13): [True: 13.6k, False: 53.5k]
|
125 | 53.5k | checked_index = end_index + 1; |
126 | 53.5k | } |
127 | | |
128 | 8.80k | return true; |
129 | 22.4k | } |
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 | 291k | #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 | 124k | #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 | 595k | 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 | 595k | uint32_t magnitude = (line < 0) ? (0U - (uint32_t) line)38.4k : (uint32_t) line557k ; Branch (165:26): [True: 38.4k, False: 557k]
|
166 | 595k | uint8_t length = 0; |
167 | | |
168 | 1.01M | do { |
169 | 1.01M | digits[10 - length++] = (char) ('0' + (magnitude % 10)); |
170 | 1.01M | magnitude /= 10; |
171 | 1.01M | } while (magnitude > 0); Branch (171:14): [True: 418k, False: 595k]
|
172 | | |
173 | 595k | if (line < 0) digits[10 - length++] = '-'38.4k ; Branch (173:9): [True: 38.4k, False: 557k]
|
174 | 595k | return length; |
175 | 595k | } |
176 | | |
177 | | /** |
178 | | * Append the given line number to the buffer, right aligned within the given |
179 | | * width. |
180 | | */ |
181 | | static void |
182 | 578k | pm_error_append_line_number(pm_buffer_t *buffer, int32_t line, uint8_t width) { |
183 | 578k | char digits[11]; |
184 | 578k | uint8_t length = pm_error_line_digits(line, digits); |
185 | | |
186 | 578k | if (width > length) pm_buffer_append_repeated(buffer, ' ', (size_t) (width - length))33.1k ; Branch (186:9): [True: 33.1k, False: 544k]
|
187 | 578k | pm_buffer_append_string(buffer, digits + sizeof(digits) - length, length); |
188 | 578k | } |
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 | 8.80k | 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 | 8.80k | char digits[11]; |
198 | 8.80k | uint8_t first_width = pm_error_line_digits(first_line, digits); |
199 | 8.80k | uint8_t last_width = pm_error_line_digits(last_line, digits); |
200 | | |
201 | 8.80k | *format = (pm_error_line_format_t) { |
202 | 8.80k | .type = format_type, |
203 | 8.80k | .width = (first_width > last_width) ? first_width22 : last_width8.77k Branch (203:18): [True: 22, False: 8.77k]
|
204 | 8.80k | }; |
205 | 8.80k | } |
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 | 124k | pm_error_line_format_number(const pm_error_line_format_t *format, pm_buffer_t *buffer, int32_t line) { |
213 | 124k | if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL62.2k (buffer, COLOR_GRAY); Branch (213:9): [True: 62.2k, False: 62.2k]
|
214 | 124k | pm_error_append_line_number(buffer, line, format->width); |
215 | 124k | PM_BUFFER_APPEND_LITERAL(buffer, " | "); |
216 | 124k | if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL62.2k (buffer, COLOR_RESET); Branch (216:9): [True: 62.2k, False: 62.2k]
|
217 | 124k | } |
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 | 122k | pm_error_line_format_blank(const pm_error_line_format_t *format, pm_buffer_t *buffer) { |
225 | 122k | if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL61.3k (buffer, COLOR_GRAY); Branch (225:9): [True: 61.3k, False: 61.3k]
|
226 | 122k | pm_buffer_append_repeated(buffer, ' ', (size_t) format->width + 1); |
227 | 122k | PM_BUFFER_APPEND_LITERAL(buffer, "| "); |
228 | 122k | if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL61.3k (buffer, COLOR_RESET); Branch (228:9): [True: 61.3k, False: 61.3k]
|
229 | 122k | } |
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 | 4.21k | pm_error_line_format_divider(const pm_error_line_format_t *format, pm_buffer_t *buffer) { |
237 | 4.21k | if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL2.10k (buffer, COLOR_GRAY); Branch (237:9): [True: 2.10k, False: 2.10k]
|
238 | 4.21k | PM_BUFFER_APPEND_LITERAL(buffer, " "); |
239 | 4.21k | pm_buffer_append_repeated(buffer, '~', (size_t) format->width + 4); |
240 | 4.21k | if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL2.10k (buffer, COLOR_RESET); Branch (240:9): [True: 2.10k, False: 2.10k]
|
241 | 4.21k | pm_buffer_append_byte(buffer, '\n'); |
242 | 4.21k | } |
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 | 368k | pm_error_advance_columns(const pm_parser_t *parser, const uint8_t *start, size_t *column, size_t limit) { |
253 | 368k | size_t count = 0; |
254 | | |
255 | 2.42M | while (*column < limit) { Branch (255:12): [True: 2.05M, False: 368k]
|
256 | 2.05M | const uint8_t *cursor = start + *column; |
257 | 2.05M | 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 | 2.05M | if ((cursor < parser->end) && (*cursor >= 0x80)2.05M ) { Branch (261:13): [True: 2.05M, False: 2.57k]
Branch (261:39): [True: 308, False: 2.05M]
|
262 | 308 | char_width = pm_parser_encoding_char_width(parser, cursor, parser->end - cursor); |
263 | 308 | if (char_width == 0) char_width = 198 ; Branch (263:17): [True: 98, False: 210]
|
264 | 308 | } |
265 | | |
266 | 2.05M | *column += char_width; |
267 | 2.05M | count++; |
268 | 2.05M | } |
269 | | |
270 | 368k | return count; |
271 | 368k | } |
272 | | |
273 | | static void |
274 | 124k | 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 | 124k | int32_t line_delta = line - parser->start_line; |
276 | 124k | PRISM_ASSERT(line_delta >= 0); Branch (276:5): [True: 0, False: 124k]
Branch (276:5): [True: 124k, False: 0]
|
277 | | |
278 | 124k | size_t index = (size_t) line_delta; |
279 | 124k | PRISM_ASSERT(index < parser->line_offsets.size); Branch (279:5): [True: 0, False: 124k]
Branch (279:5): [True: 124k, False: 0]
|
280 | | |
281 | 124k | const uint8_t *start = parser->start + parser->line_offsets.offsets[index]; |
282 | 124k | const uint8_t *end; |
283 | | |
284 | 124k | if (index >= parser->line_offsets.size - 1) { Branch (284:9): [True: 2.04k, False: 122k]
|
285 | 2.04k | end = parser->end; |
286 | 122k | } else { |
287 | 122k | end = parser->start + parser->line_offsets.offsets[index + 1]; |
288 | 122k | } |
289 | | |
290 | 124k | 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 | 124k | bool truncate_start = column_start >= TRUNCATE; |
295 | 124k | const uint8_t *display_start = truncate_start ? (start + column_start)792 : start123k ; Branch (295:36): [True: 792, False: 123k]
|
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 | 124k | bool truncate_end = false; |
303 | 124k | if ((column_end != 0) && ((end - start) - ((ptrdiff_t) column_end) >= TRUNCATE)) { Branch (303:9): [True: 124k, False: 0]
Branch (303:30): [True: 1.02k, False: 123k]
|
304 | 1.02k | const uint8_t *end_candidate = start + column_end + TRUNCATE; |
305 | | |
306 | 55.6k | for (const uint8_t *cursor = display_start; cursor < end_candidate;) { Branch (306:53): [True: 54.5k, False: 1.02k]
|
307 | 54.5k | 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 | 54.5k | if (char_width == 0) break0 ; Branch (311:17): [True: 0, False: 54.5k]
|
312 | | |
313 | | /* If this next character would go past the end candidate, |
314 | | * then we need to truncate before it. */ |
315 | 54.5k | if (cursor + char_width > end_candidate) { Branch (315:17): [True: 0, False: 54.5k]
|
316 | 0 | end_candidate = cursor; |
317 | 0 | break; |
318 | 0 | } |
319 | | |
320 | 54.5k | cursor += char_width; |
321 | 54.5k | } |
322 | | |
323 | 1.02k | end = end_candidate; |
324 | 1.02k | truncate_end = true; |
325 | 1.02k | } |
326 | | |
327 | 124k | if (truncate_start) PM_BUFFER_APPEND_LITERAL792 (buffer, "... "); Branch (327:9): [True: 792, False: 123k]
|
328 | 124k | pm_buffer_append_string(buffer, (const char *) display_start, (size_t) (end - display_start)); |
329 | | |
330 | 124k | if (truncate_end) { Branch (330:9): [True: 1.02k, False: 123k]
|
331 | 1.02k | PM_BUFFER_APPEND_LITERAL(buffer, " ...\n"); |
332 | 123k | } else if (end == parser->end && end > parser->start5.47k && end[-1] != '\n'5.47k ) { Branch (332:16): [True: 5.47k, False: 118k]
Branch (332:38): [True: 5.47k, False: 0]
Branch (332:61): [True: 800, False: 4.67k]
|
333 | 800 | pm_buffer_append_byte(buffer, '\n'); |
334 | 800 | } |
335 | 124k | } |
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 | 83.6k | 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 | 83.6k | PM_BUFFER_APPEND_LITERAL(buffer, " "); |
344 | 83.6k | pm_error_format_line(parser, buffer, format, line, 0, WINDOW); |
345 | 83.6k | } |
346 | | |
347 | | static void |
348 | 8.80k | 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 | 8.80k | 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 | 8.80k | int32_t first_displayed_line; |
354 | 8.80k | int32_t last_displayed_line; |
355 | 8.80k | int32_t unused_line; |
356 | 8.80k | pm_rich_error_context(rich_errors[0].line_start, parser->start_line, final_line, &first_displayed_line, &unused_line); |
357 | 8.80k | pm_rich_error_context(rich_errors[nerrors - 1].line_start, parser->start_line, final_line, &unused_line, &last_displayed_line); |
358 | | |
359 | 8.80k | pm_error_line_format_t format; |
360 | 8.80k | 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 | 8.80k | int32_t last_line = parser->start_line - 1; |
374 | 8.80k | uint32_t window_column_start = 0; |
375 | 8.80k | uint32_t window_column_end = 0; |
376 | 8.80k | size_t window_end_idx = 0; |
377 | | |
378 | 131k | for (size_t idx = 0; idx < nerrors; idx++122k ) { Branch (378:26): [True: 122k, False: 8.80k]
|
379 | 122k | 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 | 122k | if (rich_error->line_start - last_line > 1) { Branch (383:13): [True: 20.4k, False: 102k]
|
384 | 20.4k | if (rich_error->line_start - last_line > 2) { Branch (384:17): [True: 15.5k, False: 4.92k]
|
385 | 15.5k | if ((idx != 0) && (rich_error->line_start - last_line > 3)9.71k ) { Branch (385:21): [True: 9.71k, False: 5.84k]
Branch (385:35): [True: 4.21k, False: 5.50k]
|
386 | 4.21k | pm_error_line_format_divider(&format, buffer); |
387 | 4.21k | } |
388 | | |
389 | 15.5k | pm_error_format_context_line(parser, buffer, &format, rich_error->line_start - 2); |
390 | 15.5k | } |
391 | | |
392 | 20.4k | pm_error_format_context_line(parser, buffer, &format, rich_error->line_start - 1); |
393 | 20.4k | } |
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 | 122k | if (idx == window_end_idx) { Branch (398:13): [True: 40.9k, False: 81.7k]
|
399 | 40.9k | switch (format_type) { Branch (399:21): [True: 40.9k, False: 0]
|
400 | 20.4k | case PM_ERRORS_FORMAT_PLAIN: Branch (400:17): [True: 20.4k, False: 20.4k]
|
401 | 20.4k | PM_BUFFER_APPEND_LITERAL(buffer, "> "); |
402 | 20.4k | break; |
403 | 0 | case PM_ERRORS_FORMAT_STYLE: Branch (403:17): [True: 0, False: 40.9k]
|
404 | 0 | PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD "> " COLOR_RESET); |
405 | 0 | break; |
406 | 20.4k | case PM_ERRORS_FORMAT_COLOR: Branch (406:17): [True: 20.4k, False: 20.4k]
|
407 | 20.4k | PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED "> " COLOR_RESET); |
408 | 20.4k | break; |
409 | 40.9k | } |
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 | 40.9k | window_column_start = (rich_error->column_start >= TRUNCATE) ? rich_error->column_start792 : 040.1k ; Branch (413:35): [True: 792, False: 40.1k]
|
414 | 40.9k | 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 | 40.9k | window_column_end = rich_error->column_end; |
419 | 122k | for (window_end_idx = idx + 1; window_end_idx < nerrors; window_end_idx++81.7k ) { Branch (419:44): [True: 113k, False: 8.80k]
|
420 | 113k | const pm_rich_error_t *next_error = &rich_errors[window_end_idx]; |
421 | 113k | if ((next_error->line_start != rich_error->line_start) || (next_error->column_start >= window_limit)81.9k ) break32.1k ; Branch (421:21): [True: 31.9k, False: 81.9k]
Branch (421:75): [True: 218, False: 81.7k]
|
422 | 81.7k | if (next_error->column_end > window_column_end) window_column_end = next_error->column_end42.1k ; Branch (422:21): [True: 42.1k, False: 39.5k]
|
423 | 81.7k | } |
424 | | |
425 | 40.9k | if (window_column_end > window_limit) window_column_end = window_limit46 ; Branch (425:17): [True: 46, False: 40.9k]
|
426 | 40.9k | pm_error_format_line(parser, buffer, &format, rich_error->line_start, rich_error->column_start, window_column_end); |
427 | 40.9k | } |
428 | | |
429 | 122k | const uint8_t *start = parser->start + parser->line_offsets.offsets[rich_error->line_start - parser->start_line]; |
430 | 122k | if (start == parser->end) pm_buffer_append_byte(buffer, '\n')1.48k ; Branch (430:13): [True: 1.48k, False: 121k]
|
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 | 122k | PM_BUFFER_APPEND_LITERAL(buffer, " "); |
442 | 122k | pm_error_line_format_blank(&format, buffer); |
443 | | |
444 | 122k | size_t column = 0; |
445 | 122k | if (window_column_start > 0) { Branch (445:13): [True: 4.61k, False: 118k]
|
446 | 4.61k | PM_BUFFER_APPEND_LITERAL(buffer, " "); |
447 | 4.61k | column = window_column_start; |
448 | 4.61k | } |
449 | | |
450 | 122k | pm_buffer_append_repeated(buffer, ' ', pm_error_advance_columns(parser, start, &column, rich_error->column_start)); |
451 | | |
452 | 122k | switch (format_type) { Branch (452:17): [True: 122k, False: 0]
|
453 | 61.3k | case PM_ERRORS_FORMAT_PLAIN: Branch (453:13): [True: 61.3k, False: 61.3k]
|
454 | 61.3k | pm_buffer_append_byte(buffer, '^'); |
455 | 61.3k | break; |
456 | 0 | case PM_ERRORS_FORMAT_STYLE: Branch (456:13): [True: 0, False: 122k]
|
457 | 0 | PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD "^"); |
458 | 0 | break; |
459 | 61.3k | case PM_ERRORS_FORMAT_COLOR: Branch (459:13): [True: 61.3k, False: 61.3k]
|
460 | 61.3k | PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED "^"); |
461 | 61.3k | break; |
462 | 122k | } |
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 | 122k | pm_error_advance_columns(parser, start, &column, column + 1); |
467 | 122k | size_t tilde_limit = (rich_error->column_end < window_column_end) ? rich_error->column_end63.2k : window_column_end59.5k ; Branch (467:30): [True: 63.2k, False: 59.5k]
|
468 | 122k | pm_buffer_append_repeated(buffer, '~', pm_error_advance_columns(parser, start, &column, tilde_limit)); |
469 | | |
470 | 122k | if (format_type != PM_ERRORS_FORMAT_PLAIN) { Branch (470:13): [True: 61.3k, False: 61.3k]
|
471 | 61.3k | PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RESET); |
472 | 61.3k | } |
473 | | |
474 | 122k | if (inline_messages) { Branch (474:13): [True: 122k, False: 262]
|
475 | 122k | const char *message = pm_diagnostic_message(rich_error->diagnostic); |
476 | | |
477 | 122k | pm_buffer_append_byte(buffer, ' '); |
478 | 122k | pm_buffer_append_string(buffer, message, strlen(message)); |
479 | 122k | } |
480 | | |
481 | 122k | 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 | 122k | last_line = rich_error->line_start; |
488 | 122k | int32_t next_line = (idx == nerrors - 1) ? (final_line + 1)8.80k : rich_errors[idx + 1].line_start113k ; Branch (488:29): [True: 8.80k, False: 113k]
|
489 | | |
490 | 122k | if (next_line - last_line > 1) { Branch (490:13): [True: 26.9k, False: 95.7k]
|
491 | 26.9k | pm_error_format_context_line(parser, buffer, &format, ++last_line); |
492 | 26.9k | } |
493 | | |
494 | 122k | if (next_line - last_line > 1) { Branch (494:13): [True: 20.6k, False: 102k]
|
495 | 20.6k | pm_error_format_context_line(parser, buffer, &format, ++last_line); |
496 | 20.6k | } |
497 | 122k | } |
498 | 8.80k | } |
499 | | |
500 | | static void |
501 | 554k | pm_rich_error_init(const pm_parser_t *parser, pm_rich_error_t *rich_error, pm_diagnostic_t *diagnostic, pm_location_t *location) { |
502 | 554k | const pm_line_offset_list_t *line_offsets = &parser->line_offsets; |
503 | 554k | pm_line_column_t start_line_column = pm_line_offset_list_line_column(line_offsets, location->start, parser->start_line); |
504 | 554k | pm_line_column_t end_line_column = pm_line_offset_list_line_column(line_offsets, location->start + location->length, parser->start_line); |
505 | | |
506 | 554k | uint32_t column_end; |
507 | 554k | if (start_line_column.line == end_line_column.line) { Branch (507:9): [True: 541k, False: 12.6k]
|
508 | 541k | column_end = end_line_column.column; |
509 | 541k | } else { |
510 | 12.6k | 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 | 12.6k | } |
512 | | |
513 | | /* Ensure we have at least one column of error. */ |
514 | 554k | if (start_line_column.column == column_end) column_end++103k ; Branch (514:9): [True: 103k, False: 450k]
|
515 | | |
516 | 554k | *rich_error = (pm_rich_error_t) { |
517 | 554k | .diagnostic = diagnostic, |
518 | 554k | .line_start = start_line_column.line, |
519 | 554k | .column_start = start_line_column.column, |
520 | 554k | .column_end = column_end |
521 | 554k | }; |
522 | 554k | } |
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 | 453k | pm_error_append_summary(const pm_parser_t *parser, pm_buffer_t *buffer, int32_t line, const char *message) { |
530 | 453k | pm_buffer_append_string(buffer, (const char *) pm_string_source(&parser->filepath), pm_string_length(&parser->filepath)); |
531 | 453k | pm_buffer_append_byte(buffer, ':'); |
532 | 453k | pm_error_append_line_number(buffer, line, 0); |
533 | 453k | PM_BUFFER_APPEND_LITERAL(buffer, ": "); |
534 | 453k | pm_buffer_append_string(buffer, message, strlen(message)); |
535 | 453k | } |
536 | | |
537 | | pm_error_level_t |
538 | 25.3k | pm_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, pm_errors_format_type_t format_type) { |
539 | 25.3k | size_t nerrors = parser->error_list.size; |
540 | 25.3k | PRISM_ASSERT(nerrors > 0); Branch (540:5): [True: 0, False: 25.3k]
Branch (540:5): [True: 25.3k, False: 0]
|
541 | | |
542 | 25.3k | size_t rich_errors_size = nerrors * sizeof(pm_rich_error_t); |
543 | 25.3k | if (rich_errors_size / sizeof(pm_rich_error_t) != nerrors) PRISM_OOM0 (); Branch (543:9): [True: 0, False: 25.3k]
|
544 | | |
545 | 25.3k | pm_rich_error_t *rich_errors = xmalloc(rich_errors_size); |
546 | 25.3k | if (rich_errors == NULL) PRISM_OOM0 (); Branch (546:9): [True: 0, False: 25.3k]
|
547 | | |
548 | 25.3k | size_t rich_errors_idx = 0; |
549 | | |
550 | 578k | for (pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) parser->error_list.head; diagnostic != NULL; diagnostic = (pm_diagnostic_t *) diagnostic->node.next553k ) { Branch (550:85): [True: 556k, False: 21.9k]
|
551 | 556k | pm_location_t location = pm_diagnostic_location(diagnostic); |
552 | | |
553 | 556k | switch (pm_diagnostic_error_level(diagnostic)) { Branch (553:17): [True: 556k, False: 0]
|
554 | 553k | case PM_ERROR_LEVEL_SYNTAX: { Branch (554:11): [True: 553k, False: 3.34k]
|
555 | 553k | pm_rich_error_init(parser, &rich_errors[rich_errors_idx], diagnostic, &location); |
556 | 553k | rich_errors[rich_errors_idx].idx = rich_errors_idx; |
557 | 553k | rich_errors_idx++; |
558 | 553k | break; |
559 | 0 | } |
560 | 526 | case PM_ERROR_LEVEL_ARGUMENT: { Branch (560:11): [True: 526, False: 556k]
|
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 | 526 | pm_rich_error_init(parser, rich_errors, diagnostic, &location); |
565 | 526 | pm_error_append_summary(parser, buffer, rich_errors[0].line_start, pm_diagnostic_message(diagnostic)); |
566 | | |
567 | 526 | if (rich_errors_are_utf8(parser, 1, rich_errors)) { Branch (567:17): [True: 262, False: 264]
|
568 | 262 | pm_buffer_append_byte(buffer, '\n'); |
569 | 262 | pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors, false); |
570 | 262 | } |
571 | | |
572 | 526 | xfree_sized(rich_errors, rich_errors_size); |
573 | 526 | return PM_ERROR_LEVEL_ARGUMENT; |
574 | 0 | } |
575 | 2.82k | case PM_ERROR_LEVEL_LOAD: { Branch (575:11): [True: 2.82k, False: 554k]
|
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 | 2.82k | const char *message = pm_diagnostic_message(diagnostic); |
579 | 2.82k | pm_buffer_append_string(buffer, message, strlen(message)); |
580 | | |
581 | 2.82k | xfree_sized(rich_errors, rich_errors_size); |
582 | 2.82k | return PM_ERROR_LEVEL_LOAD; |
583 | 0 | } |
584 | 0 | case PM_ERROR_LEVEL_STACK: { Branch (584:11): [True: 0, False: 556k]
|
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 | 556k | } |
602 | 556k | } |
603 | | |
604 | 21.9k | PRISM_ASSERT(rich_errors_idx == nerrors); Branch (604:5): [True: 0, False: 21.9k]
Branch (604:5): [True: 21.9k, 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 | 21.9k | pm_error_append_summary(parser, buffer, rich_errors[0].line_start, (nerrors > 1) ? "syntax errors found\n"20.9k : "syntax error found\n"1.05k ); Branch (608:72): [True: 20.9k, False: 1.05k]
|
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 | 21.9k | qsort(rich_errors, nerrors, sizeof(pm_rich_error_t), pm_rich_error_compare); |
614 | | |
615 | 21.9k | if (rich_errors_are_utf8(parser, nerrors, rich_errors)) { Branch (615:9): [True: 8.53k, False: 13.4k]
|
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 | 8.53k | pm_rich_errors_format(parser, buffer, format_type, nerrors, rich_errors, true); |
620 | 13.4k | } 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 | 444k | for (size_t idx = 0; idx < nerrors; idx++431k ) { Branch (626:30): [True: 431k, False: 13.4k]
|
627 | 431k | if (idx > 0) pm_buffer_append_byte(buffer, '\n')417k ; Branch (627:17): [True: 417k, False: 13.4k]
|
628 | 431k | pm_error_append_summary(parser, buffer, rich_errors[idx].line_start, pm_diagnostic_message(rich_errors[idx].diagnostic)); |
629 | 431k | } |
630 | 13.4k | } |
631 | | |
632 | 21.9k | xfree_sized(rich_errors, rich_errors_size); |
633 | 21.9k | return PM_ERROR_LEVEL_SYNTAX; |
634 | 21.9k | } |