Coverage Report

Created: 2026-11-10 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
src/comments.c
Line
Count
Source
1
#include "prism/internal/comments.h"
2
3
#include "prism/internal/arena.h"
4
#include "prism/internal/assert.h"
5
#include "prism/internal/char.h"
6
#include "prism/internal/node.h"
7
#include "prism/internal/parser.h"
8
9
#include <stdlib.h>
10
#include <string.h>
11
12
/* The state used while walking the tree to find the owner of each comment. */
13
typedef struct {
14
    /*
15
     * The arena for memory that is only needed during the walk, which is the
16
     * scratch arena of the parser.
17
     */
18
    pm_arena_t *arena;
19
20
    /* The start of the source that was parsed. */
21
    const uint8_t *source;
22
23
    /* The comments found while parsing, sorted by start offset. */
24
    const pm_comment_t **comments;
25
26
    /* The number of comments. */
27
    uint32_t size;
28
29
    /* The start offset of each comment. */
30
    uint32_t *starts;
31
32
    /*
33
     * The result of the most recent search for the first comment after the end
34
     * of a node. This is a hint to the search to avoid fully binary searching
35
     * the comments list if possible.
36
     */
37
    uint32_t cursor;
38
39
    /* The node each comment is attached to. */
40
    const pm_node_t **attached;
41
42
    /*
43
     * A disjoint set over comment indices that maps each index to the next
44
     * index at or after it that has not yet been claimed. It has one extra
45
     * entry at the end that is never claimed, so that every lookup ends. This
46
     * allows quickly finding the next unclaimed comment without having to check
47
     * the state of each comment individually.
48
     */
49
    uint32_t *unclaimed;
50
51
    /*
52
     * A stack of nodes whose children are being visited. Each level of the
53
     * walk pushes its children, visits them, then pops back to where it
54
     * started.
55
     */
56
    struct {
57
        const pm_node_t **nodes;
58
        size_t size;
59
        size_t capacity;
60
    } stack;
61
} pm_comments_walk_t;
62
63
/* Compare two comments by start offset for qsort. */
64
static int
65
966
pm_comments_compare(const void *left, const void *right) {
66
966
    uint32_t left_start = (*(const pm_comment_t *const *) left)->location.start;
67
966
    uint32_t right_start = (*(const pm_comment_t *const *) right)->location.start;
68
966
    return (left_start > right_start) - (left_start < right_start);
69
966
}
70
71
/*
72
 * Copy the linked list of comments from the parser sorted by start offset, and
73
 * relink the list in that order. Usually comments arrive in source order, but
74
 * unfortunately because of heredocs they can occasionally be out of order.
75
 */
76
static const pm_comment_t **
77
399
pm_comments_sort(pm_parser_t *parser, uint32_t size) {
78
399
    pm_comment_t **comments = (pm_comment_t **) pm_arena_alloc(&parser->metadata_arena, sizeof(pm_comment_t *) * size, PRISM_ALIGNOF(pm_comment_t *));
79
80
399
    uint32_t index = 0;
81
1.36k
    for (pm_list_node_t *node = parser->comment_list.head; node != NULL; 
node = node->next967
) {
  Branch (81:60): [True: 967, False: 399]
82
967
        comments[index++] = (pm_comment_t *) node;
83
967
    }
84
85
399
    qsort((void *) comments, size, sizeof(pm_comment_t *), pm_comments_compare);
86
87
967
    for (index = 0; index + 1 < size; 
index++568
) {
  Branch (87:21): [True: 568, False: 399]
88
568
        comments[index]->node.next = &comments[index + 1]->node;
89
568
    }
90
91
399
    comments[size - 1]->node.next = NULL;
92
399
    parser->comment_list.head = &comments[0]->node;
93
399
    parser->comment_list.tail = &comments[size - 1]->node;
94
95
399
    return (const pm_comment_t **) comments;
96
399
}
97
98
/*
99
 * Return the index of the first comment in [left, right) that starts at or
100
 * after the given offset, or right if there is none.
101
 */
102
static uint32_t
103
17.7k
pm_comments_lower_bound(const pm_comments_walk_t *walk, uint32_t left, uint32_t right, uint32_t offset) {
104
20.9k
    while (left < right) {
  Branch (104:12): [True: 3.21k, False: 17.7k]
105
3.21k
        uint32_t middle = left + (right - left) / 2;
106
107
3.21k
        if (walk->starts[middle] < offset) {
  Branch (107:13): [True: 619, False: 2.59k]
108
619
            left = middle + 1;
109
2.59k
        } else {
110
2.59k
            right = middle;
111
2.59k
        }
112
3.21k
    }
113
114
17.7k
    return left;
115
17.7k
}
116
117
/*
118
 * Return the index of the first comment that starts at or after the given
119
 * offset, or the number of comments if there is none. This gallops outward
120
 * from the previous result to find a range to search, so that a run of nearby
121
 * offsets costs a constant number of comparisons each.
122
 */
123
static uint32_t
124
16.0k
pm_comments_seek(pm_comments_walk_t *walk, uint32_t offset) {
125
16.0k
    const uint32_t *starts = walk->starts;
126
127
16.0k
    uint32_t size = walk->size;
128
16.0k
    uint32_t cursor = walk->cursor;
129
16.0k
    uint32_t left;
130
16.0k
    uint32_t right;
131
16.0k
    uint32_t step = 1;
132
133
16.0k
    if (cursor == size || 
starts[cursor] >= offset10.0k
) {
  Branch (133:9): [True: 6.02k, False: 10.0k]
  Branch (133:27): [True: 9.05k, False: 972]
134
        /*
135
         * The answer is at or before the cursor. Widen the range backward
136
         * until the comment before it starts before the offset.
137
         */
138
15.0k
        right = cursor;
139
15.0k
        left = cursor;
140
141
15.3k
        while (left > 0 && 
starts[left - 1] >= offset8.24k
) {
  Branch (141:16): [True: 8.24k, False: 7.09k]
  Branch (141:28): [True: 256, False: 7.98k]
142
256
            right = left - 1;
143
256
            left = (left - 1 > step) ? 
left - 1 - step64
:
0192
;
  Branch (143:20): [True: 64, False: 192]
144
256
            step *= 2;
145
256
        }
146
15.0k
    } else {
147
        /*
148
         * The answer is after the cursor. Widen the range forward until it
149
         * ends at a comment that starts at or after the offset.
150
         */
151
972
        left = cursor + 1;
152
972
        right = left;
153
154
1.06k
        while (right < size && 
starts[right] < offset604
) {
  Branch (154:16): [True: 604, False: 463]
  Branch (154:32): [True: 95, False: 509]
155
95
            left = right + 1;
156
95
            right = (size - left > step) ? 
left + step34
:
size61
;
  Branch (156:21): [True: 34, False: 61]
157
95
            step *= 2;
158
95
        }
159
972
    }
160
161
16.0k
    walk->cursor = pm_comments_lower_bound(walk, left, right, offset);
162
16.0k
    return walk->cursor;
163
16.0k
}
164
165
/*
166
 * The first comment index at or after the given index that has not yet been
167
 * claimed, compressing the path along the way.
168
 */
169
static uint32_t
170
2.54k
pm_comments_unclaimed(pm_comments_walk_t *walk, uint32_t index) {
171
2.54k
    uint32_t *unclaimed = walk->unclaimed;
172
173
4.10k
    while (unclaimed[index] != index) {
  Branch (173:12): [True: 1.56k, False: 2.54k]
174
1.56k
        unclaimed[index] = unclaimed[unclaimed[index]];
175
1.56k
        index = unclaimed[index];
176
1.56k
    }
177
178
2.54k
    return index;
179
2.54k
}
180
181
/*
182
 * Return the earliest offset before the given one from which the source up to
183
 * it holds only whitespace and comments. Any comment that starts at or after
184
 * the returned offset is separated from the given offset by only whitespace and
185
 * other comments.
186
 */
187
static uint32_t
188
18
pm_comments_blank_before(const pm_comments_walk_t *walk, uint32_t offset) {
189
18
    uint32_t index = pm_comments_lower_bound(walk, 0, walk->size, offset);
190
191
35
    while (
true)18
{
  Branch (191:12): [True: 35, Folded]
192
35
        offset -= (uint32_t) pm_strrspn_whitespace(walk->source, (ptrdiff_t) offset);
193
35
        if (index == 0) 
return offset10
;
  Branch (193:13): [True: 10, False: 25]
194
195
        /* An embedded document's location includes its trailing newline, so
196
         * the whitespace that was just skipped can end inside a comment. */
197
25
        const pm_location_t *location = &walk->comments[index - 1]->location;
198
25
        if (location->start >= offset || PM_LOCATION_END(location) < offset) 
return offset8
;
  Branch (198:13): [True: 0, False: 25]
  Branch (198:42): [True: 8, False: 17]
199
200
17
        offset = location->start;
201
17
        index--;
202
17
    }
203
18
}
204
205
/*
206
 * Return the latest offset after the given one, and no later than the given
207
 * end, up to which the source from the given offset holds only whitespace and
208
 * comments. Any comment that ends at or before the returned offset is
209
 * separated from the given offset by only whitespace and other comments.
210
 */
211
static uint32_t
212
10
pm_comments_blank_after(const pm_comments_walk_t *walk, uint32_t offset, uint32_t end) {
213
10
    uint32_t index = pm_comments_lower_bound(walk, 0, walk->size, offset);
214
215
18
    while (
true)10
{
  Branch (215:12): [True: 18, Folded]
216
18
        offset += (uint32_t) pm_strspn_whitespace(walk->source + offset, (ptrdiff_t) end - (ptrdiff_t) offset);
217
18
        if (index == walk->size || 
walk->starts[index] != offset15
)
return offset10
;
  Branch (217:13): [True: 3, False: 15]
  Branch (217:36): [True: 7, False: 8]
218
219
8
        offset = PM_LOCATION_END(&walk->comments[index]->location);
220
8
        index++;
221
8
    }
222
10
}
223
224
/*
225
 * Push a child node onto the walk stack without descending into it. The walk
226
 * descends itself, so that it can visit children in source order and handle
227
 * each node after all of its descendants.
228
 */
229
static pm_visit_result_t
230
15.6k
pm_comments_push(const pm_node_t *node, void *data) {
231
15.6k
    pm_comments_walk_t *walk = (pm_comments_walk_t *) data;
232
233
15.6k
    if (walk->stack.size == walk->stack.capacity) {
  Branch (233:9): [True: 416, False: 15.2k]
234
416
        size_t capacity = walk->stack.capacity == 0 ? 
64399
:
walk->stack.capacity * 217
;
  Branch (234:27): [True: 399, False: 17]
235
416
        const pm_node_t **nodes = (const pm_node_t **) pm_arena_alloc(walk->arena, sizeof(pm_node_t *) * capacity, PRISM_ALIGNOF(pm_node_t *));
236
416
        if (walk->stack.size > 0) 
memcpy((void *) nodes, (const void *) walk->stack.nodes, sizeof(pm_node_t *) * walk->stack.size)17
;
  Branch (236:13): [True: 17, False: 399]
237
238
416
        walk->stack.nodes = nodes;
239
416
        walk->stack.capacity = capacity;
240
416
    }
241
242
15.6k
    walk->stack.nodes[walk->stack.size++] = node;
243
15.6k
    return PM_VISIT_SKIP;
244
15.6k
}
245
246
/*
247
 * Visit the given node and its descendants in post-order, attaching comments as
248
 * the nodes are visited.
249
 */
250
static void
251
16.0k
pm_comments_visit(pm_comments_walk_t *walk, const pm_node_t *node) {
252
    /* First, push all of the children of this node onto the stack. */
253
16.0k
    size_t base = walk->stack.size;
254
16.0k
    pm_visit_child_nodes(node, pm_comments_push, walk);
255
16.0k
    size_t top = walk->stack.size;
256
257
    /* Next, in-place sort the children by their starting location. */
258
23.0k
    for (size_t index = base + 1; index < top; 
index++7.04k
) {
  Branch (258:35): [True: 7.04k, False: 16.0k]
259
7.04k
        const pm_node_t *child = walk->stack.nodes[index];
260
7.04k
        size_t insert = index;
261
262
7.99k
        while (insert > base && 
walk->stack.nodes[insert - 1]->location.start > child->location.start7.85k
) {
  Branch (262:16): [True: 7.85k, False: 135]
  Branch (262:33): [True: 950, False: 6.90k]
263
950
            walk->stack.nodes[insert] = walk->stack.nodes[insert - 1];
264
950
            insert--;
265
950
        }
266
267
7.04k
        walk->stack.nodes[insert] = child;
268
7.04k
    }
269
270
    /* Next, visit all of the children so that they can claim comments. */
271
31.7k
    for (size_t index = base; index < top; 
index++15.6k
) {
  Branch (271:31): [True: 15.6k, False: 16.0k]
272
15.6k
        pm_comments_visit(walk, walk->stack.nodes[index]);
273
15.6k
    }
274
275
16.0k
    uint32_t start = node->location.start;
276
16.0k
    uint32_t end = PM_LOCATION_END(&node->location);
277
16.0k
    uint32_t after = pm_comments_seek(walk, end);
278
279
    /* If this node is followed by a comment on the same line, attach it if
280
     * appropriate. Appropriate in this case means if no other node has claimed
281
     * it or if we are closer in distance or equal in distance to the current
282
     * claimant. */
283
16.0k
    if (
284
16.0k
        after < walk->size &&
  Branch (284:9): [True: 9.76k, False: 6.28k]
285
16.0k
        
!9.76k
PM_NODE_TYPE_P9.76k
(node, PM_PROGRAM_NODE) &&
  Branch (285:9): [True: 9.62k, False: 138]
286
16.0k
        
!9.62k
PM_NODE_TYPE_P9.62k
(node, PM_STATEMENTS_NODE) &&
  Branch (286:9): [True: 8.75k, False: 873]
287
16.0k
        
(8.75k
node->location.length == 08.75k
||
walk->source[end - 1] != '\n'7.36k
)
  Branch (287:10): [True: 1.39k, False: 7.36k]
  Branch (287:40): [True: 6.46k, False: 905]
288
16.0k
    ) {
289
7.85k
        uint32_t comment_start = walk->starts[after];
290
291
7.85k
        if (memchr(walk->source + end, '\n', comment_start - end) == NULL) {
  Branch (291:13): [True: 1.81k, False: 6.03k]
292
1.81k
            const pm_node_t *attached = walk->attached[after];
293
1.81k
            uint32_t attached_end = attached == NULL ? 
0420
:
PM_LOCATION_END1.39k
(&attached->location);
  Branch (293:37): [True: 420, False: 1.39k]
294
295
1.81k
            if (end >= attached_end || 
attached_end > comment_start139
) {
  Branch (295:17): [True: 1.67k, False: 139]
  Branch (295:40): [True: 63, False: 76]
296
1.73k
                walk->attached[after] = node;
297
1.73k
            }
298
1.81k
        }
299
7.85k
    }
300
301
    /* Grab up the remaining comments within the range of this node that were
302
     * not claimed by any of its descendants. A comment that is separated by
303
     * only whitespace and other comments from a statements node child, either
304
     * the child after it or the child before it, is attached to the statements
305
     * instead, so that the comments before the first statement and after the
306
     * last statement in a body belong to the body. The claimed comments are in
307
     * order, so the children around each one are found by moving forward
308
     * through the sorted children. */
309
16.0k
    if (after > 0 && 
walk->starts[after - 1] >= start8.96k
) {
  Branch (309:9): [True: 8.96k, False: 7.08k]
  Branch (309:22): [True: 1.66k, False: 7.30k]
310
1.66k
        uint32_t index = pm_comments_unclaimed(walk, pm_comments_lower_bound(walk, 0, after, start));
311
1.66k
        size_t child_index = base;
312
313
        /* The boundaries around the statements children are each found once,
314
         * when the first comment that needs them is reached, since the
315
         * comments that follow reach the same children. */
316
1.66k
        const pm_node_t *leading = NULL;
317
1.66k
        uint32_t leading_boundary = 0;
318
1.66k
        const pm_node_t *trailing = NULL;
319
1.66k
        uint32_t trailing_boundary = 0;
320
321
2.54k
        while (index < after && 
PM_LOCATION_END886
(&walk->comments[index]->location) <= end886
) {
  Branch (321:16): [True: 886, False: 1.65k]
  Branch (321:33): [True: 879, False: 7]
322
879
            if (walk->attached[index] == NULL) {
  Branch (322:17): [True: 491, False: 388]
323
491
                uint32_t comment_end = PM_LOCATION_END(&walk->comments[index]->location);
324
491
                const pm_node_t *attached = node;
325
326
1.75k
                while (child_index < top && 
walk->stack.nodes[child_index]->location.start < comment_end1.61k
)
child_index++1.25k
;
  Branch (326:24): [True: 1.61k, False: 132]
  Branch (326:45): [True: 1.25k, False: 359]
327
328
491
                if (child_index < top) {
  Branch (328:21): [True: 359, False: 132]
329
359
                    const pm_node_t *child = walk->stack.nodes[child_index];
330
331
359
                    if (PM_NODE_TYPE_P(child, PM_STATEMENTS_NODE)) {
332
20
                        if (child != leading) {
  Branch (332:29): [True: 18, False: 2]
333
18
                            leading = child;
334
18
                            leading_boundary = pm_comments_blank_before(walk, child->location.start);
335
18
                        }
336
337
20
                        if (walk->starts[index] >= leading_boundary) 
attached = child17
;
  Branch (337:29): [True: 17, False: 3]
338
20
                    }
339
359
                }
340
341
491
                if (attached == node && 
child_index > base474
) {
  Branch (341:21): [True: 474, False: 17]
  Branch (341:41): [True: 453, False: 21]
342
453
                    const pm_node_t *child = walk->stack.nodes[child_index - 1];
343
344
                    /* A rescue clause ends at its last statement rather than at
345
                     * the keyword that follows it, and the first clause in a
346
                     * body contains the clauses after it. So a comment after
347
                     * the statements of the last clause follows the first
348
                     * clause, and belongs to the statements of the last. */
349
453
                    if (PM_NODE_TYPE_P(child, PM_RESCUE_NODE)) {
350
0
                        const pm_rescue_node_t *rescue = (const pm_rescue_node_t *) child;
351
0
                        while (rescue->subsequent != NULL) rescue = rescue->subsequent;
  Branch (351:32): [True: 0, False: 0]
352
0
                        child = (const pm_node_t *) rescue->statements;
353
0
                    }
354
355
453
                    if (child != NULL && PM_NODE_TYPE_P(child, PM_STATEMENTS_NODE) && 
PM_LOCATION_END10
(&child->location) <= walk->starts[index]10
) {
  Branch (355:25): [True: 453, False: 0]
  Branch (355:87): [True: 10, False: 0]
356
10
                        if (child != trailing) {
  Branch (356:29): [True: 10, False: 0]
357
10
                            trailing = child;
358
10
                            trailing_boundary = pm_comments_blank_after(walk, PM_LOCATION_END(&child->location), end);
359
10
                        }
360
361
10
                        if (comment_end <= trailing_boundary) 
attached = child8
;
  Branch (361:29): [True: 8, False: 2]
362
10
                    }
363
453
                }
364
365
491
                walk->attached[index] = attached;
366
491
            }
367
368
879
            walk->unclaimed[index] = index + 1;
369
879
            index = pm_comments_unclaimed(walk, index + 1);
370
879
        }
371
1.66k
    }
372
373
    /* Truncate the stack back to the base size. */
374
16.0k
    walk->stack.size = base;
375
16.0k
}
376
377
/*
378
 * Sort the parser's comment list in place by start offset, then assign every
379
 * comment to the node that owns it. A comment is owned by:
380
 *
381
 * 1. the node that ends latest on the comment's line before the comment starts,
382
 *    with ties going to the node that is higher in the tree;
383
 * 2. otherwise, the innermost node whose location contains the comment, or the
384
 *    statements node child of that node if only whitespace and other comments
385
 *    come between the statements and the comment, on either side;
386
 * 3. otherwise, the top-level statements node of the program.
387
 */
388
void
389
922
pm_comments_attach(pm_parser_t *parser, const pm_node_t *root) {
390
922
    PRISM_ASSERT(PM_NODE_TYPE_P(root, PM_PROGRAM_NODE));
  Branch (390:5): [True: 0, False: 922]
  Branch (390:5): [True: 922, False: 0]
391
392
922
    uint32_t size = (uint32_t) parser->comment_list.size;
393
922
    if (size == 0) 
return523
;
  Branch (393:9): [True: 523, False: 399]
394
395
399
    pm_arena_t *arena = &parser->scratch_arena;
396
399
    pm_comments_walk_t walk = {
397
399
        .arena = arena,
398
399
        .source = parser->start,
399
399
        .comments = pm_comments_sort(parser, size),
400
399
        .size = size,
401
399
        .starts = (uint32_t *) pm_arena_alloc(arena, sizeof(uint32_t) * size, PRISM_ALIGNOF(uint32_t)),
402
399
        .attached = (const pm_node_t **) pm_arena_zalloc(arena, sizeof(pm_node_t *) * size, PRISM_ALIGNOF(pm_node_t *)),
403
399
        .unclaimed = (uint32_t *) pm_arena_alloc(arena, sizeof(uint32_t) * (size + 1), PRISM_ALIGNOF(uint32_t))
404
399
    };
405
406
1.36k
    for (uint32_t index = 0; index < size; 
index++967
)
walk.starts[index] = walk.comments[index]->location.start967
;
  Branch (406:30): [True: 967, False: 399]
407
1.76k
    for (uint32_t index = 0; index <= size; 
index++1.36k
)
walk.unclaimed[index] = index1.36k
;
  Branch (407:30): [True: 1.36k, False: 399]
408
409
399
    pm_comments_visit(&walk, root);
410
411
    /*
412
     * Attach comments that no node attached to during the walk to the
413
     * top-level statements, then group the comments by node with a counting
414
     * sort. Comments are placed in sorted order, so each node's comments stay
415
     * sorted by start offset.
416
     */
417
399
    const pm_node_t *statements = (const pm_node_t *) ((const pm_program_node_t *) root)->statements;
418
399
    uint32_t ranges_size = 0;
419
420
1.36k
    for (uint32_t index = 0; index < size; 
index++967
) {
  Branch (420:30): [True: 967, False: 399]
421
967
        if (walk.attached[index] == NULL) 
walk.attached[index] = statements56
;
  Branch (421:13): [True: 56, False: 911]
422
423
967
        uint32_t node_id = walk.attached[index]->node_id;
424
967
        PRISM_ASSERT(node_id <= parser->node_id);
  Branch (424:9): [True: 0, False: 967]
  Branch (424:9): [True: 967, False: 0]
425
967
        if (node_id >= ranges_size) 
ranges_size = node_id + 1708
;
  Branch (425:13): [True: 708, False: 259]
426
967
    }
427
428
    /* The ranges only extend to the largest node id that owns a comment, since
429
     * none of the nodes after it own any. */
430
399
    pm_comment_range_t *ranges = (pm_comment_range_t *) pm_arena_zalloc(&parser->metadata_arena, sizeof(pm_comment_range_t) * ranges_size, PRISM_ALIGNOF(pm_comment_range_t));
431
399
    uint32_t *attachments = (uint32_t *) pm_arena_alloc(&parser->metadata_arena, sizeof(uint32_t) * size, PRISM_ALIGNOF(uint32_t));
432
433
1.36k
    for (uint32_t index = 0; index < size; 
index++967
) {
  Branch (433:30): [True: 967, False: 399]
434
967
        ranges[walk.attached[index]->node_id].length++;
435
967
    }
436
437
399
    uint32_t offset = 0;
438
8.23k
    for (uint32_t index = 0; index < ranges_size; 
index++7.83k
) {
  Branch (438:30): [True: 7.83k, False: 399]
439
7.83k
        ranges[index].index = offset;
440
7.83k
        offset += ranges[index].length;
441
7.83k
        ranges[index].length = 0;
442
7.83k
    }
443
444
1.36k
    for (uint32_t index = 0; index < size; 
index++967
) {
  Branch (444:30): [True: 967, False: 399]
445
967
        pm_comment_range_t *range = &ranges[walk.attached[index]->node_id];
446
967
        attachments[range->index + range->length++] = index;
447
967
    }
448
449
399
    parser->comment_attachments = (pm_comment_attachments_t) {
450
399
        .comments = walk.comments,
451
399
        .attachments = attachments,
452
399
        .ranges = ranges,
453
399
        .ranges_size = ranges_size
454
399
    };
455
399
}