@willbooster/tree-sitter-c 43.0.0 → 45.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,436 @@
1
+ #ifndef TREE_SITTER_C_DIRECTIVE_FACTS_H_
2
+ #define TREE_SITTER_C_DIRECTIVE_FACTS_H_
3
+
4
+ #include "directiveBuffer.h"
5
+ #include <stdlib.h>
6
+
7
+ #define DIRECTIVE_FACT_PAGE_SHIFT 15
8
+ #define DIRECTIVE_FACT_PAGE_SIZE (1 << DIRECTIVE_FACT_PAGE_SHIFT)
9
+
10
+ typedef struct {
11
+ uint32_t *items;
12
+ uint32_t count, capacity;
13
+ } DirectiveOffsets;
14
+
15
+ typedef struct {
16
+ uint32_t index, byte, end_byte;
17
+ } DirectiveBarrier;
18
+
19
+ typedef struct {
20
+ DirectiveBarrier *items;
21
+ uint32_t count, capacity;
22
+ } DirectiveBarriers;
23
+
24
+ // Null-directive positions (`starts`, `ends`, `comments`) are directories of `pages` pointers, one per
25
+ // `DIRECTIVE_FACT_PAGE_SIZE` source bytes, to bitset pages allocated only when a null directive exists; directive
26
+ // boundaries (`directive_starts`, `directive_ends`) and condition line-end runs (`line_ends`) are sorted byte arrays.
27
+ // `empty_directives_release` frees them.
28
+ typedef struct {
29
+ uint8_t **starts, **ends, **comments;
30
+ uint32_t pages;
31
+ DirectiveOffsets directive_starts, directive_ends;
32
+ DirectiveBarriers barriers;
33
+ DirectiveSpans line_ends;
34
+ // `anchored` and `ready` belong to the scanner state: whether the token at the start of the input is out, and
35
+ // whether the facts, which `built` says exist, are in use. `pragmas` is the fact that the input read for them holds
36
+ // `pragma_word`, without which no scan has to look for a `_Pragma` operator.
37
+ bool built, anchored, ready, pragmas, failed;
38
+ } EmptyDirectives;
39
+
40
+ typedef struct {
41
+ TSSymbol include, define, preproc_if, ifdef, ifndef, preproc_else, elif, elifdef, elifndef, endif, pragma, directive;
42
+ } PreprocDirectiveSymbols;
43
+
44
+ static bool empty_directives_skip_prefix(TSLexer *lexer);
45
+ static bool empty_directives_scan_word_step(TSLexer *lexer, char *word, unsigned capacity, unsigned *length, bool *trailing_splice, TSLexerPosition *position);
46
+ static bool empty_directives_ucn_follows(TSLexer *lexer, bool continuation);
47
+ static inline bool empty_directives_name_character(int32_t c, bool continuation);
48
+ static bool directive_word_continues(DirectiveInput *input, bool number, bool strict_identifier, bool *in_identifier, int32_t *previous);
49
+ static bool directive_consume_ucn(DirectiveInput *input);
50
+ static bool directive_ucn(DirectiveInput *input, bool continuation);
51
+ static TSSymbol empty_directives_keyword_symbol(const char *word, const PreprocDirectiveSymbols *symbols);
52
+ static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte);
53
+ static bool directive_offset_has(const DirectiveOffsets *offsets, uint32_t byte);
54
+ static void directive_offset_push(DirectiveOffsets *offsets, DirectiveBuffer *buffer, uint32_t byte);
55
+ static void directive_barrier_push(DirectiveBarriers *barriers, DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t end_byte);
56
+ static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
57
+ static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte);
58
+ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
59
+ static void directive_mark_spans(EmptyDirectives *state, DirectiveBuffer *buffer, const DirectiveSpans *spans, uint8_t ***facts);
60
+ static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte);
61
+ static void directive_set_range(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint64_t start, uint64_t end);
62
+ static void directive_set_page_bits(uint8_t *bits, uint32_t b0, uint32_t b1);
63
+ static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte);
64
+ static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages);
65
+
66
+ static inline bool empty_directives_has_directives(const EmptyDirectives *state) {
67
+ return !state || state->failed || state->directive_starts.count > 0;
68
+ }
69
+
70
+ static bool empty_directives_has_keyword_end(const EmptyDirectives *state, uint32_t keyword_end) {
71
+ if (!state) return false;
72
+ return state->failed || directive_offset_has(&state->directive_ends, keyword_end);
73
+ }
74
+
75
+ // Scans `#`, any directive prefix spacing or block comments, and the ASCII directive name into `word`, marking the
76
+ // token end at the name's last character and storing the post-keyword cursor byte in `*keyword_end`.
77
+ static bool empty_directives_scan_word(TSLexer *lexer, char *word, unsigned capacity, uint32_t *keyword_end) {
78
+ lexer->advance(lexer, false);
79
+ if (!empty_directives_skip_prefix(lexer) || !empty_directives_name_character(lexer->lookahead, false)) return false;
80
+ unsigned length = 0;
81
+ bool trailing_splice = false;
82
+ TSLexerPosition position = {0};
83
+ while (empty_directives_scan_word_step(lexer, word, capacity, &length, &trailing_splice, &position)) {}
84
+ if (!trailing_splice) lexer->get_position(lexer, &position);
85
+ word[length < capacity ? length : 0] = '\0';
86
+ *keyword_end = position.bytes;
87
+ return true;
88
+ }
89
+
90
+ static bool empty_directives_scan_word_step(TSLexer *lexer, char *word, unsigned capacity, unsigned *length, bool *trailing_splice, TSLexerPosition *position) {
91
+ int32_t c = lexer->lookahead;
92
+ if (empty_directives_name_character(c, true)) {
93
+ if (*length < capacity) word[(*length)++] = (char)c;
94
+ lexer->advance(lexer, false);
95
+ lexer->mark_end(lexer);
96
+ *trailing_splice = false;
97
+ return true;
98
+ }
99
+ if (c != '\\') {
100
+ if (preproc_identifier_character(c, true)) *length = capacity;
101
+ return false;
102
+ }
103
+ if (!*trailing_splice) lexer->get_position(lexer, position);
104
+ *trailing_splice = true;
105
+ lexer->advance(lexer, false);
106
+ if (scan_preproc_newline(lexer, false)) return true;
107
+ if (empty_directives_ucn_follows(lexer, true)) *length = capacity;
108
+ return false;
109
+ }
110
+
111
+ static bool empty_directives_ucn_follows(TSLexer *lexer, bool continuation) {
112
+ if (!scan_preproc_splices(lexer)) return false;
113
+ int32_t prefix = lexer->lookahead;
114
+ if (prefix != 'u' && prefix != 'U' && prefix != 'N') return false;
115
+ lexer->advance(lexer, false);
116
+ if (!scan_preproc_splices(lexer)) return false;
117
+ bool braced = prefix != 'U' && lexer->lookahead == '{';
118
+ DirectiveUcn ucn;
119
+ if (!directive_ucn_start(&ucn, prefix, braced)) return false;
120
+ if (braced) lexer->advance(lexer, false);
121
+ for (bool done = false;;) {
122
+ if (!scan_preproc_splices(lexer)) return false;
123
+ if (!directive_ucn_feed(&ucn, lexer->lookahead, continuation, &done)) return false;
124
+ lexer->advance(lexer, false);
125
+ if (done) return true;
126
+ }
127
+ }
128
+
129
+ static inline bool empty_directives_name_character(int32_t c, bool continuation) {
130
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (continuation && c == '_');
131
+ }
132
+
133
+ static bool empty_directives_skip_prefix(TSLexer *lexer) {
134
+ for (;;) {
135
+ int32_t c = lexer->lookahead;
136
+ if (directive_horizontal(c)) {
137
+ lexer->advance(lexer, false);
138
+ } else if (c == '\\') {
139
+ lexer->advance(lexer, false);
140
+ if (!scan_preproc_newline(lexer, false)) return false;
141
+ } else if (c == '/') {
142
+ lexer->advance(lexer, false);
143
+ if (!scan_preproc_splices(lexer) || lexer->lookahead != '*' || !scan_preproc_block_comment(lexer)) return false;
144
+ } else {
145
+ return true;
146
+ }
147
+ }
148
+ }
149
+
150
+ static bool empty_directives_select_symbol(const char *word, const PreprocDirectiveSymbols *symbols, const bool *valid_symbols, TSSymbol *result) {
151
+ TSSymbol specific = empty_directives_keyword_symbol(word, symbols);
152
+ if (valid_symbols[specific]) {
153
+ *result = specific;
154
+ return true;
155
+ }
156
+ bool can_fallback = specific == symbols->directive || specific == symbols->define ||
157
+ specific == symbols->include || specific == symbols->pragma;
158
+ if (can_fallback && valid_symbols[symbols->directive]) {
159
+ *result = symbols->directive;
160
+ return true;
161
+ }
162
+ return false;
163
+ }
164
+
165
+ // code-gauge-allow functionCognitiveComplexity -- Maps each fixed directive keyword spelling to its external token slot.
166
+ static TSSymbol empty_directives_keyword_symbol(const char *word, const PreprocDirectiveSymbols *symbols) {
167
+ switch (word[0]) {
168
+ case 'd':
169
+ return strcmp(word, "define") == 0 ? symbols->define : symbols->directive;
170
+ case 'e':
171
+ if (strcmp(word, "else") == 0) return symbols->preproc_else;
172
+ if (strcmp(word, "elif") == 0) return symbols->elif;
173
+ if (strcmp(word, "elifdef") == 0) return symbols->elifdef;
174
+ if (strcmp(word, "elifndef") == 0) return symbols->elifndef;
175
+ if (strcmp(word, "endif") == 0) return symbols->endif;
176
+ return symbols->directive;
177
+ case 'i':
178
+ if (strcmp(word, "if") == 0) return symbols->preproc_if;
179
+ if (strcmp(word, "ifdef") == 0) return symbols->ifdef;
180
+ if (strcmp(word, "ifndef") == 0) return symbols->ifndef;
181
+ if (strcmp(word, "include") == 0) return symbols->include;
182
+ return symbols->directive;
183
+ case 'p':
184
+ return strcmp(word, "pragma") == 0 ? symbols->pragma : symbols->directive;
185
+ default:
186
+ return symbols->directive;
187
+ }
188
+ }
189
+
190
+ // False only when the facts show that no `_Pragma` operator can follow any position.
191
+ static inline bool empty_directives_pragma_possible(const EmptyDirectives *state) {
192
+ return !state || !state->built || state->pragmas;
193
+ }
194
+
195
+ // Whether none of the `count` tokens, at least eight, is valid but those whose byte in `extras` is 0xff.
196
+ static inline bool empty_directives_only_valid(const bool *valid_symbols, const uint8_t *extras, unsigned count) {
197
+ _Static_assert(sizeof(bool) == 1, "valid_symbols is read as bytes");
198
+ uint64_t others = 0;
199
+ for (unsigned i = 0; i < count; i += 8) {
200
+ unsigned offset = i + 8 > count ? count - 8 : i;
201
+ uint64_t valid, extra;
202
+ memcpy(&valid, valid_symbols + offset, sizeof valid);
203
+ memcpy(&extra, extras + offset, sizeof extra);
204
+ others |= valid & ~extra;
205
+ }
206
+ return !others;
207
+ }
208
+
209
+ static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lexer) {
210
+ if (!state) return false;
211
+ if (state->failed) return true;
212
+ TSLexerPosition position;
213
+ lexer->get_position(lexer, &position);
214
+ return directive_offset_has(&state->directive_starts, position.bytes);
215
+ }
216
+
217
+ static inline bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *lexer) {
218
+ if (!state || !state->line_ends.count || !(directive_horizontal(lexer->lookahead) || directive_newline(lexer->lookahead))) return false;
219
+ TSLexerPosition position;
220
+ lexer->get_position(lexer, &position);
221
+ return empty_directives_line_end_at(state, position.bytes);
222
+ }
223
+
224
+ static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte) {
225
+ const DirectiveSpans *spans = &state->line_ends;
226
+ if (!spans->count || byte < spans->items[0].start || byte >= spans->items[spans->count - 1].end) return false;
227
+ uint32_t low = 0, high = spans->count;
228
+ while (low < high) {
229
+ uint32_t mid = low + (high - low) / 2;
230
+ DirectiveSpan span = spans->items[mid];
231
+ if (byte < span.start) high = mid;
232
+ else if (byte >= span.end) low = mid + 1;
233
+ else return true;
234
+ }
235
+ return false;
236
+ }
237
+
238
+ static bool directive_offset_has(const DirectiveOffsets *offsets, uint32_t byte) {
239
+ if (!offsets->count || byte < offsets->items[0] || byte > offsets->items[offsets->count - 1]) return false;
240
+ uint32_t low = 0, high = offsets->count;
241
+ while (low < high) {
242
+ uint32_t mid = low + (high - low) / 2;
243
+ uint32_t item = offsets->items[mid];
244
+ if (item == byte) return true;
245
+ if (item < byte) low = mid + 1;
246
+ else high = mid;
247
+ }
248
+ return false;
249
+ }
250
+
251
+ static void directive_offset_push(DirectiveOffsets *offsets, DirectiveBuffer *buffer, uint32_t byte) {
252
+ if (buffer->failed || (offsets->count && offsets->items[offsets->count - 1] == byte)) return;
253
+ if (!directive_grow_items((void **)&offsets->items, offsets->count, &offsets->capacity, sizeof(*offsets->items))) {
254
+ buffer->failed = true;
255
+ return;
256
+ }
257
+ offsets->items[offsets->count++] = byte;
258
+ }
259
+
260
+ static void directive_barrier_push(DirectiveBarriers *barriers, DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t end_byte) {
261
+ if (buffer->failed || (barriers->count && barriers->items[barriers->count - 1].byte == byte)) return;
262
+ if (!directive_grow_items((void **)&barriers->items, barriers->count, &barriers->capacity, sizeof(*barriers->items))) {
263
+ buffer->failed = true;
264
+ return;
265
+ }
266
+ barriers->items[barriers->count++] = (DirectiveBarrier){index, byte, end_byte};
267
+ }
268
+
269
+ static bool empty_directives_skip(TSLexer *lexer, void *payload) {
270
+ return empty_directives_consume(lexer, payload, false);
271
+ }
272
+
273
+ // A null directive, including its prefix, starts only at `#`, `\\` or a space, so other characters cannot match `starts`.
274
+ static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
275
+ int32_t c = lexer->lookahead;
276
+ if (!state || !state->starts || !(c == '#' || c == '\\' || pragma_space(c))) return false;
277
+ TSLexerPosition position;
278
+ lexer->get_position(lexer, &position);
279
+ if (!empty_directives_start_at(state, position.bytes)) return false;
280
+ empty_directives_consume_started(lexer, state, skip_prefix);
281
+ return true;
282
+ }
283
+
284
+ static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte) {
285
+ return directive_bit(state, state->starts, byte);
286
+ }
287
+
288
+ // Consumes the rest of a null directive whose start was confirmed at a position the lexer has not left except over
289
+ // prefix characters: as a token (`skip_prefix`) up to its first recorded end, which `directive_record` places before
290
+ // the comments or after the line, and otherwise the whole logical line, since some scanner lookahead dispatches on
291
+ // the character after the skip without reading comments.
292
+ static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
293
+ TSLexerPosition position;
294
+ while (lexer->lookahead != '#' && !pragma_eof(lexer)) lexer->advance(lexer, skip_prefix);
295
+ for (bool whole = !skip_prefix;; whole = false) {
296
+ do {
297
+ lexer->advance(lexer, false);
298
+ lexer->get_position(lexer, &position);
299
+ } while (!pragma_eof(lexer) && !directive_bit(state, state->ends, position.bytes));
300
+ if (!whole || pragma_eof(lexer) || !directive_bit(state, state->comments, position.bytes)) return;
301
+ }
302
+ }
303
+
304
+ static void empty_directives_release(EmptyDirectives *state) {
305
+ uint8_t **directories[] = {state->starts, state->ends, state->comments};
306
+ for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
307
+ if (!directories[i]) continue;
308
+ for (uint32_t page = 0; page < state->pages; page++) free(directories[i][page]);
309
+ free(directories[i]);
310
+ }
311
+ state->starts = state->ends = state->comments = NULL;
312
+ state->pages = 0;
313
+ free(state->directive_starts.items);
314
+ free(state->directive_ends.items);
315
+ free(state->barriers.items);
316
+ free(state->line_ends.items);
317
+ state->directive_starts = state->directive_ends = (DirectiveOffsets){0};
318
+ state->barriers = (DirectiveBarriers){0};
319
+ state->line_ends = (DirectiveSpans){0};
320
+ }
321
+
322
+ static void directive_mark_spans(EmptyDirectives *state, DirectiveBuffer *buffer, const DirectiveSpans *spans, uint8_t ***facts) {
323
+ for (uint32_t i = 0; i < spans->count; i++) {
324
+ directive_set_range(state, buffer, facts, spans->items[i].start, spans->items[i].end);
325
+ }
326
+ }
327
+
328
+ static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte) {
329
+ directive_set_range(state, buffer, facts, byte, (uint64_t)byte + 1);
330
+ }
331
+
332
+ static void directive_set_range(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint64_t start, uint64_t end) {
333
+ if (end > (uint64_t)UINT32_MAX + 1) end = (uint64_t)UINT32_MAX + 1;
334
+ if (buffer->failed || start >= end) return;
335
+ uint32_t last_page = (uint32_t)((end - 1) >> DIRECTIVE_FACT_PAGE_SHIFT);
336
+ if (last_page >= state->pages &&
337
+ !directive_grow_directories(state, state->pages * 2 > last_page ? state->pages * 2 : last_page + 1)) {
338
+ buffer->failed = true;
339
+ return;
340
+ }
341
+ if (!*facts && !(*facts = calloc(state->pages, sizeof **facts))) {
342
+ buffer->failed = true;
343
+ return;
344
+ }
345
+ for (uint64_t byte = start; byte < end;) {
346
+ uint32_t page = (uint32_t)(byte >> DIRECTIVE_FACT_PAGE_SHIFT);
347
+ uint8_t **bits = &(*facts)[page];
348
+ if (!*bits && !(*bits = calloc(DIRECTIVE_FACT_PAGE_SIZE / 8, 1))) {
349
+ buffer->failed = true;
350
+ return;
351
+ }
352
+ uint64_t page_end = ((uint64_t)page + 1) << DIRECTIVE_FACT_PAGE_SHIFT;
353
+ if (page_end > end) page_end = end;
354
+ directive_set_page_bits(*bits, (uint32_t)byte & (DIRECTIVE_FACT_PAGE_SIZE - 1),
355
+ (uint32_t)(page_end - ((uint64_t)page << DIRECTIVE_FACT_PAGE_SHIFT)));
356
+ byte = page_end;
357
+ }
358
+ }
359
+
360
+ static void directive_set_page_bits(uint8_t *bits, uint32_t b0, uint32_t b1) {
361
+ uint32_t w0 = b0 >> 3, w1 = (b1 - 1) >> 3;
362
+ uint8_t start_mask = (uint8_t)(0xffu << (b0 & 7)), end_mask = (uint8_t)(0xffu >> (7 - ((b1 - 1) & 7)));
363
+ if (w0 == w1) {
364
+ bits[w0] |= start_mask & end_mask;
365
+ return;
366
+ }
367
+ bits[w0] |= start_mask;
368
+ if (w1 > w0 + 1) memset(bits + w0 + 1, 0xff, w1 - w0 - 1);
369
+ bits[w1] |= end_mask;
370
+ }
371
+
372
+ static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte) {
373
+ uint32_t page = byte >> DIRECTIVE_FACT_PAGE_SHIFT, offset = byte & (DIRECTIVE_FACT_PAGE_SIZE - 1);
374
+ return facts && page < state->pages && facts[page] && (facts[page][offset / 8] & (1u << (offset % 8)));
375
+ }
376
+
377
+ static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages) {
378
+ uint8_t ***directories[] = {&state->starts, &state->ends, &state->comments};
379
+ if ((size_t)pages > SIZE_MAX / sizeof(uint8_t *)) return false;
380
+ for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
381
+ if (!*directories[i]) continue;
382
+ uint8_t **directory = realloc(*directories[i], (size_t)pages * sizeof *directory);
383
+ if (!directory) return false;
384
+ memset(directory + state->pages, 0, (size_t)(pages - state->pages) * sizeof *directory);
385
+ *directories[i] = directory;
386
+ }
387
+ state->pages = pages;
388
+ return true;
389
+ }
390
+
391
+ static bool directive_word_continues(DirectiveInput *input, bool number, bool strict_identifier, bool *in_identifier, int32_t *previous) {
392
+ if (strict_identifier && *in_identifier && directive_consume_ucn(input)) {
393
+ *previous = 0x80;
394
+ return true;
395
+ }
396
+ int32_t c = directive_peek(input);
397
+ bool exponent_sign = number && (c == '+' || c == '-') &&
398
+ (*previous == 'e' || *previous == 'E' || *previous == 'p' || *previous == 'P');
399
+ if (!preproc_word(c, true) && !(number && (c == '.' || c == '\'' || exponent_sign))) return false;
400
+ if (strict_identifier && !(c != '\\' && preproc_identifier_character(c, true))) *in_identifier = false;
401
+ *previous = directive_next(input);
402
+ return true;
403
+ }
404
+
405
+ static bool directive_consume_ucn(DirectiveInput *input) {
406
+ if (directive_peek(input) != '\\') return false;
407
+ bool had_spaced = input->spaced_splice;
408
+ DirectiveInput copy = *input;
409
+ copy.spaced_splice = false;
410
+ directive_next(&copy);
411
+ if (copy.spaced_splice || !directive_ucn(&copy, true)) return false;
412
+ copy.spaced_splice = had_spaced;
413
+ *input = copy;
414
+ return true;
415
+ }
416
+
417
+ static bool directive_ucn(DirectiveInput *input, bool continuation) {
418
+ bool had_spaced = input->spaced_splice;
419
+ DirectiveInput copy = *input;
420
+ copy.spaced_splice = false;
421
+ int32_t prefix = directive_next(&copy);
422
+ bool braced = prefix != 'U' && (prefix == 'u' || prefix == 'N') && directive_peek(&copy) == '{';
423
+ DirectiveUcn ucn;
424
+ if (!directive_ucn_start(&ucn, prefix, braced)) return false;
425
+ if (braced) directive_next(&copy);
426
+ for (bool done = false;;) {
427
+ if (!directive_ucn_feed(&ucn, directive_next(&copy), continuation, &done)) return false;
428
+ if (done) break;
429
+ }
430
+ if (copy.spaced_splice) return false;
431
+ copy.spaced_splice = had_spaced;
432
+ *input = copy;
433
+ return true;
434
+ }
435
+
436
+ #endif
@@ -500,18 +500,20 @@ static bool directive_newline(int32_t c) {
500
500
  return c == '\n' || c == '\r';
501
501
  }
502
502
 
503
+ static inline bool directive_grow_items(void **items, uint32_t count, uint32_t *capacity, size_t item_size) {
504
+ if (count < *capacity) return true;
505
+ uint32_t next = *capacity ? *capacity * 2 : 16;
506
+ void *grown = next > *capacity && (size_t)next <= SIZE_MAX / item_size ? realloc(*items, (size_t)next * item_size) : NULL;
507
+ if (!grown) return false;
508
+ *items = grown;
509
+ *capacity = next;
510
+ return true;
511
+ }
512
+
503
513
  static void directive_spans_push(DirectiveSpans *spans, uint32_t start, uint32_t end) {
504
- if (spans->count == spans->capacity) {
505
- uint32_t capacity = spans->capacity ? spans->capacity * 2 : 8;
506
- DirectiveSpan *items = capacity > spans->capacity && (size_t)capacity <= SIZE_MAX / sizeof(*items)
507
- ? realloc(spans->items, (size_t)capacity * sizeof(*items))
508
- : NULL;
509
- if (!items) {
510
- spans->failed = true;
511
- return;
512
- }
513
- spans->items = items;
514
- spans->capacity = capacity;
514
+ if (!directive_grow_items((void **)&spans->items, spans->count, &spans->capacity, sizeof(*spans->items))) {
515
+ spans->failed = true;
516
+ return;
515
517
  }
516
518
  spans->items[spans->count++] = (DirectiveSpan){start, end};
517
519
  }