@willbooster/tree-sitter-c 42.0.0 → 44.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,544 @@
1
+ // code-gauge-allow-file fileNcss -- Unit-level code, comment, quote, and SIMD scanners share `DirectiveUnitPass`.
2
+ // The line-state pass of `directiveScan.h` over code units read from memory instead of characters fed one at a time.
3
+ // Included by `directiveScan.h`, whose `DirectiveScan` it reads and leaves as the character path would.
4
+ #ifndef TREE_SITTER_C_DIRECTIVE_UNITS_H_
5
+ #define TREE_SITTER_C_DIRECTIVE_UNITS_H_
6
+
7
+ #if defined(__ARM_NEON) && defined(__aarch64__)
8
+ #include <arm_neon.h>
9
+ #define DIRECTIVE_USE_NEON 1
10
+ #endif
11
+
12
+ // The classes of an ASCII character among the units of the input: whether it is part of a word, horizontal space, or
13
+ // plain code (word or plain punctuation), whether it is `_` or `P`, without both of which no text holds `pragma_word`,
14
+ // and whether it is plain text of a line comment, of a block comment and of a literal.
15
+ enum {
16
+ UNIT_WORD = 1,
17
+ UNIT_SPACE = 2,
18
+ UNIT_CODE = 4,
19
+ UNIT_UNDERSCORE = 8,
20
+ UNIT_P = 16,
21
+ UNIT_LINE = 32,
22
+ UNIT_BLOCK = 64,
23
+ UNIT_QUOTE = 128,
24
+ };
25
+ #define UNIT_PRAGMA (UNIT_UNDERSCORE | UNIT_P)
26
+ #define UNIT_CLASS_COUNT 128
27
+
28
+ typedef struct {
29
+ uint32_t consumed, line_feeds, tail;
30
+ } DirectiveUnits;
31
+
32
+ // The pass's state over `count` units of which `i` are read. `floor` is where the current stretch of plain code began,
33
+ // and `pragma_from` where `seen` started accumulating character classes.
34
+ typedef struct {
35
+ const char *units;
36
+ const uint8_t *classes;
37
+ uint32_t count, i, start, floor, pragma_from, line_feeds, line, quote;
38
+ int32_t previous;
39
+ DirectiveScanMode mode;
40
+ bool lines, beginning, star, number, pragma, fresh;
41
+ uint8_t seen;
42
+ #ifdef DIRECTIVE_USE_NEON
43
+ uint8x16x4_t table_lo, table_hi;
44
+ uint8x16_t seen_vec;
45
+ #endif
46
+ } DirectiveUnitPass;
47
+
48
+ #if defined(__GNUC__) || defined(__clang__)
49
+ #define DIRECTIVE_INLINE __attribute__((always_inline)) static inline
50
+ #else
51
+ #define DIRECTIVE_INLINE static inline
52
+ #endif
53
+
54
+ DIRECTIVE_INLINE bool directive_units_code(DirectiveUnitPass *pass, unsigned size);
55
+ DIRECTIVE_INLINE bool directive_units_code_beginning(DirectiveUnitPass *pass, unsigned size);
56
+ DIRECTIVE_INLINE bool directive_units_code_token(DirectiveUnitPass *pass, unsigned size, uint32_t c);
57
+ DIRECTIVE_INLINE bool directive_units_slash(DirectiveUnitPass *pass, unsigned size);
58
+ DIRECTIVE_INLINE bool directive_units_word_quote(DirectiveUnitPass *pass, unsigned size, uint32_t c);
59
+ DIRECTIVE_INLINE void directive_units_finish_code(DirectiveUnitPass *pass, unsigned size);
60
+ static uint32_t directive_units_word_start(const DirectiveUnitPass *pass, unsigned size, uint32_t end);
61
+ DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned size);
62
+ DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsigned size);
63
+ DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, unsigned size);
64
+ DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned size);
65
+ DIRECTIVE_INLINE uint32_t directive_units_advance_mask(DirectiveUnitPass *pass, unsigned size, uint8_t mask);
66
+ #ifdef DIRECTIVE_USE_NEON
67
+ DIRECTIVE_INLINE void directive_units_neon_advance(DirectiveUnitPass *pass, unsigned size, uint8_t mask);
68
+ DIRECTIVE_INLINE bool directive_units_neon_load(const char *units, unsigned size, uint32_t i, uint8x16_t *chars);
69
+ #endif
70
+ DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, unsigned size, DirectiveScanMode mode, uint32_t length);
71
+ DIRECTIVE_INLINE bool directive_units_close(DirectiveUnitPass *pass, unsigned size);
72
+ DIRECTIVE_INLINE bool directive_units_line_feed(DirectiveUnitPass *pass, unsigned size);
73
+ DIRECTIVE_INLINE bool directive_units_escape(const DirectiveUnitPass *pass, unsigned size);
74
+ DIRECTIVE_INLINE void directive_units_flush_pragma(DirectiveUnitPass *pass, unsigned size);
75
+ DIRECTIVE_INLINE uint32_t directive_unit(const char *units, unsigned size, uint32_t index);
76
+ static void directive_scan_units_end(DirectiveScan *scan, const char *units, unsigned size, uint32_t from, uint32_t end, uint32_t byte);
77
+ static void directive_scan_units_word(DirectiveScan *scan, const char *units, unsigned size, uint32_t first, uint32_t end);
78
+ static bool directive_units_pragma(const char *units, unsigned size, uint32_t start, uint32_t end);
79
+
80
+ // Runs the line-state pass over `count` units of `size` bytes, the first of which is at `byte` of the input, as
81
+ // `directive_scan_feed` does one character at a time. It stops before the first unit that needs the physical layer (a
82
+ // backslash that may start a line splice, a carriage return, a character that is not ASCII), that starts a directive or
83
+ // may open a raw string, or that the units read do not tell apart. Without `lines`, it also stops before each line
84
+ // feed, leaves `scan` as it is, and consumes no part of a word, comment or literal that it does not finish.
85
+ // code-gauge-allow functionNcss -- One pass copies the scan state in, dispatches on its mode, and copies it back.
86
+ DIRECTIVE_INLINE DirectiveUnits directive_scan_units(DirectiveScan *scan, const char *units, uint32_t count, unsigned size, uint32_t byte, bool lines) {
87
+ DirectiveUnitPass pass = {
88
+ .units = units, .classes = scan->classes, .count = count, .quote = (uint32_t)scan->quote, .previous = scan->previous,
89
+ .mode = scan->mode, .lines = lines, .beginning = scan->beginning, .star = scan->star, .number = scan->number,
90
+ #ifdef DIRECTIVE_USE_NEON
91
+ .table_lo = vld1q_u8_x4(scan->classes), .table_hi = vld1q_u8_x4(scan->classes + 64), .seen_vec = vdupq_n_u8(0),
92
+ #endif
93
+ };
94
+ for (bool more = true; more;) {
95
+ switch (pass.mode) {
96
+ case SCAN_WORD:
97
+ more = directive_units_word(&pass, size);
98
+ break;
99
+ case SCAN_LINE_COMMENT:
100
+ more = directive_units_line_comment(&pass, size);
101
+ break;
102
+ case SCAN_BLOCK_COMMENT:
103
+ more = directive_units_block_comment(&pass, size);
104
+ break;
105
+ case SCAN_QUOTE:
106
+ more = directive_units_quoted(&pass, size);
107
+ break;
108
+ default:
109
+ more = directive_units_code(&pass, size);
110
+ }
111
+ }
112
+ directive_units_finish_code(&pass, size);
113
+ if (!lines) return (DirectiveUnits){pass.mode == SCAN_CODE ? pass.i : pass.start, 0, 0};
114
+ if (!pass.i) return (DirectiveUnits){0};
115
+ directive_units_flush_pragma(&pass, size);
116
+ if (pass.pragma) *scan->pragma = true;
117
+ scan->mode = pass.mode;
118
+ scan->beginning = pass.beginning;
119
+ scan->star = pass.star;
120
+ scan->number = pass.number;
121
+ scan->quote = (int32_t)pass.quote;
122
+ scan->escape = false;
123
+ directive_scan_units_end(scan, units, size, pass.fresh ? pass.start : UINT32_MAX, pass.i, byte);
124
+ return (DirectiveUnits){pass.i, pass.line_feeds, pass.i - pass.line};
125
+ }
126
+
127
+ // Reads plain code, words and line breaks in one mode, opening another mode only at comments, quotes, raw-string
128
+ // candidates and digit separators.
129
+ DIRECTIVE_INLINE bool directive_units_code(DirectiveUnitPass *pass, unsigned size) {
130
+ for (;;) {
131
+ if (pass->beginning && !directive_units_code_beginning(pass, size)) {
132
+ return pass->mode != SCAN_CODE;
133
+ }
134
+ uint32_t c = directive_units_advance_mask(pass, size, UNIT_CODE | UNIT_SPACE);
135
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
136
+ if (c == '\n' && directive_units_line_feed(pass, size)) {
137
+ pass->beginning = true;
138
+ continue;
139
+ }
140
+ if (c == '#') {
141
+ pass->i++;
142
+ continue;
143
+ }
144
+ return directive_units_code_token(pass, size, c);
145
+ }
146
+ }
147
+
148
+ // Advances from the beginning of a line to its first non-space unit; returns true once the line holds plain code.
149
+ DIRECTIVE_INLINE bool directive_units_code_beginning(DirectiveUnitPass *pass, unsigned size) {
150
+ for (;;) {
151
+ uint32_t c = 0;
152
+ while (pass->i < pass->count && (c = directive_unit(pass->units, size, pass->i)) < UNIT_CLASS_COUNT &&
153
+ (pass->classes[c] & UNIT_SPACE)) {
154
+ pass->i++;
155
+ }
156
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
157
+ uint8_t class = pass->classes[c];
158
+ if (class & UNIT_CODE) {
159
+ pass->beginning = false;
160
+ pass->seen |= class;
161
+ pass->i++;
162
+ return true;
163
+ }
164
+ if (c != '\n' || !directive_units_line_feed(pass, size)) {
165
+ return directive_units_code_token(pass, size, c) && pass->mode == SCAN_CODE;
166
+ }
167
+ }
168
+ }
169
+
170
+ // Reads `c`, which is neither plain code nor a space.
171
+ DIRECTIVE_INLINE bool directive_units_code_token(DirectiveUnitPass *pass, unsigned size, uint32_t c) {
172
+ if (c == '"' || c == '\'') {
173
+ if (!directive_units_word_quote(pass, size, c)) {
174
+ pass->quote = c;
175
+ directive_units_open(pass, size, SCAN_QUOTE, 1);
176
+ }
177
+ pass->beginning = false;
178
+ return true;
179
+ }
180
+ if (c == '/') return directive_units_slash(pass, size);
181
+ if (c != '#' || pass->beginning) return false;
182
+ pass->i++;
183
+ pass->floor = pass->i;
184
+ return true;
185
+ }
186
+
187
+ DIRECTIVE_INLINE bool directive_units_slash(DirectiveUnitPass *pass, unsigned size) {
188
+ if (pass->i + 1 >= pass->count) return false;
189
+ uint32_t next = directive_unit(pass->units, size, pass->i + 1);
190
+ if (next == '\\') return false;
191
+ if (next == '/' || next == '*') {
192
+ pass->star = false;
193
+ directive_units_open(pass, size, next == '/' ? SCAN_LINE_COMMENT : SCAN_BLOCK_COMMENT, 2);
194
+ return true;
195
+ }
196
+ pass->beginning = false;
197
+ pass->i++;
198
+ pass->floor = pass->i;
199
+ return true;
200
+ }
201
+
202
+ // Reopens the word before `"` when it may be a raw-string prefix, or the number before `'` so `'` is read as a
203
+ // separator instead of a character literal.
204
+ DIRECTIVE_INLINE bool directive_units_word_quote(DirectiveUnitPass *pass, unsigned size, uint32_t c) {
205
+ if (pass->i <= pass->floor) return false;
206
+ uint32_t prev = directive_unit(pass->units, size, pass->i - 1);
207
+ if (c == '"' && prev != 'R') return false;
208
+ if (c == '\'' && !(pass->classes[prev] & UNIT_WORD) && prev != '.') return false;
209
+ uint32_t start = directive_units_word_start(pass, size, pass->i);
210
+ if (start >= pass->i) return false;
211
+ uint32_t first = directive_unit(pass->units, size, start);
212
+ bool number = first >= '0' && first <= '9';
213
+ if (c == '\'' && !number) return false;
214
+ if (c == '"' && pass->i - start >= sizeof ((GnuRawStringPrefix *)0)->value) return false;
215
+ pass->i = start;
216
+ pass->number = number;
217
+ directive_units_open(pass, size, SCAN_WORD, 1);
218
+ return true;
219
+ }
220
+
221
+ // Reopens an unfinished word at the stop of `SCAN_CODE` when the next character may continue it.
222
+ DIRECTIVE_INLINE void directive_units_finish_code(DirectiveUnitPass *pass, unsigned size) {
223
+ if (pass->mode != SCAN_CODE || pass->i <= pass->floor) return;
224
+ if (pass->i < pass->count) {
225
+ uint32_t next = directive_unit(pass->units, size, pass->i);
226
+ if (next < UNIT_CLASS_COUNT && next != '\\') return;
227
+ }
228
+ uint32_t prev = directive_unit(pass->units, size, pass->i - 1);
229
+ if (!(pass->classes[prev] & UNIT_WORD) && prev != '.' && prev != '+' && prev != '-') return;
230
+ uint32_t start = directive_units_word_start(pass, size, pass->i);
231
+ if (start >= pass->i) return;
232
+ uint32_t first = directive_unit(pass->units, size, start);
233
+ pass->mode = SCAN_WORD;
234
+ pass->start = start;
235
+ pass->fresh = true;
236
+ pass->number = first >= '0' && first <= '9';
237
+ }
238
+
239
+ // Finds the start of the unfinished word or preprocessing number in `[pass->floor .. end]`, or `end` when none is open.
240
+ // code-gauge-allow functionCognitiveComplexity -- Backward candidate bound and forward pp-number replay share one helper.
241
+ static uint32_t directive_units_word_start(const DirectiveUnitPass *pass, unsigned size, uint32_t end) {
242
+ uint32_t k = end;
243
+ while (k > pass->floor) {
244
+ uint32_t u = directive_unit(pass->units, size, k - 1);
245
+ if ((pass->classes[u] & UNIT_WORD) || u == '.') {
246
+ k--;
247
+ continue;
248
+ }
249
+ if ((u == '+' || u == '-') && k - 1 > pass->floor) {
250
+ uint32_t p = directive_unit(pass->units, size, k - 2);
251
+ if (p == 'e' || p == 'E' || p == 'p' || p == 'P') {
252
+ k -= 2;
253
+ continue;
254
+ }
255
+ }
256
+ break;
257
+ }
258
+ uint32_t word_start = end, prev = 0;
259
+ bool in_word = false, number = false;
260
+ for (uint32_t j = k; j < end; j++) {
261
+ uint32_t u = directive_unit(pass->units, size, j);
262
+ bool word = (pass->classes[u] & UNIT_WORD) != 0;
263
+ bool exponent = number && (u == '+' || u == '-') && (prev == 'e' || prev == 'E' || prev == 'p' || prev == 'P');
264
+ if (!in_word) {
265
+ if (word) {
266
+ in_word = true;
267
+ number = u >= '0' && u <= '9';
268
+ word_start = j;
269
+ }
270
+ } else if (!word && !(number && (u == '.' || exponent))) {
271
+ in_word = false;
272
+ }
273
+ prev = u;
274
+ }
275
+ return in_word ? word_start : end;
276
+ }
277
+
278
+ // code-gauge-allow functionCognitiveComplexity -- A number continues over signs and separators that end any other word.
279
+ DIRECTIVE_INLINE bool directive_units_word(DirectiveUnitPass *pass, unsigned size) {
280
+ uint32_t c = 0;
281
+ for (;;) {
282
+ c = directive_units_advance_mask(pass, size, UNIT_WORD);
283
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT || c == '\\') return false;
284
+ if (!pass->number) break;
285
+ int32_t previous = pass->i > (pass->fresh ? pass->start : 0)
286
+ ? (int32_t)directive_unit(pass->units, size, pass->i - 1)
287
+ : pass->previous;
288
+ bool exponent_sign = (c == '+' || c == '-') &&
289
+ (previous == 'e' || previous == 'E' || previous == 'p' || previous == 'P');
290
+ if (!(c == '.' || c == '\'' || exponent_sign)) break;
291
+ pass->i++;
292
+ }
293
+ if (c == '"' && (!pass->fresh || pass->i - pass->start < sizeof ((GnuRawStringPrefix *)0)->value)) return false;
294
+ return directive_units_close(pass, size);
295
+ }
296
+
297
+ DIRECTIVE_INLINE bool directive_units_line_comment(DirectiveUnitPass *pass, unsigned size) {
298
+ for (;;) {
299
+ uint32_t c = directive_units_advance_mask(pass, size, UNIT_LINE);
300
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
301
+ if (c == '\\' && directive_units_escape(pass, size)) {
302
+ pass->i++;
303
+ } else if (c == '\n' && !pass->lines) {
304
+ pass->mode = SCAN_CODE;
305
+ pass->floor = pass->i;
306
+ return false;
307
+ } else if (c == '\n' && directive_units_line_feed(pass, size)) {
308
+ pass->beginning = true;
309
+ return directive_units_close(pass, size);
310
+ } else {
311
+ return false;
312
+ }
313
+ }
314
+ }
315
+
316
+ // code-gauge-allow functionCognitiveComplexity -- The comment's end depends on the character before each `/`.
317
+ DIRECTIVE_INLINE bool directive_units_block_comment(DirectiveUnitPass *pass, unsigned size) {
318
+ for (;;) {
319
+ uint32_t before = pass->i;
320
+ uint32_t c = directive_units_advance_mask(pass, size, UNIT_BLOCK);
321
+ if (pass->i > before) pass->star = false;
322
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
323
+ bool star = pass->star;
324
+ pass->star = c == '*';
325
+ if (c == '*') {
326
+ pass->i++;
327
+ } else if (c == '/') {
328
+ pass->i++;
329
+ if (star) return directive_units_close(pass, size);
330
+ } else if (c == '\\' && directive_units_escape(pass, size)) {
331
+ pass->i++;
332
+ } else if (c != '\n' || !directive_units_line_feed(pass, size)) {
333
+ pass->star = star;
334
+ return false;
335
+ }
336
+ }
337
+ }
338
+
339
+ DIRECTIVE_INLINE bool directive_units_quoted(DirectiveUnitPass *pass, unsigned size) {
340
+ for (;;) {
341
+ uint32_t c = directive_units_advance_mask(pass, size, UNIT_QUOTE);
342
+ if (pass->i >= pass->count || c >= UNIT_CLASS_COUNT) return false;
343
+ if (c == pass->quote) {
344
+ pass->i++;
345
+ return directive_units_close(pass, size);
346
+ }
347
+ if (c == '"' || c == '\'') {
348
+ pass->i++;
349
+ } else if (c == '\\' && directive_units_escape(pass, size)) {
350
+ pass->seen |= pass->classes[directive_unit(pass->units, size, pass->i + 1)];
351
+ pass->i += 2;
352
+ } else {
353
+ return false;
354
+ }
355
+ }
356
+ }
357
+
358
+ #ifdef DIRECTIVE_USE_NEON
359
+ DIRECTIVE_INLINE void directive_units_neon_advance(DirectiveUnitPass *pass, unsigned size, uint8_t mask) {
360
+ uint8x16_t mask_vec = vdupq_n_u8(mask);
361
+ uint8x16_t hi_xor = vdupq_n_u8(0x40);
362
+ uint8x16_t nl_vec = vdupq_n_u8('\n');
363
+ bool cross_lines = pass->lines && (mask == (UNIT_CODE | UNIT_SPACE) || mask == UNIT_BLOCK);
364
+ uint8_t tail_req = mask == (UNIT_CODE | UNIT_SPACE) ? UNIT_CODE : UNIT_BLOCK;
365
+ uint8x16_t chars;
366
+ while (pass->count - pass->i >= 16 && directive_units_neon_load(pass->units, size, pass->i, &chars)) {
367
+ uint8x16_t cls = vorrq_u8(vqtbl4q_u8(pass->table_lo, chars), vqtbl4q_u8(pass->table_hi, veorq_u8(chars, hi_xor)));
368
+ if (vminvq_u8(vandq_u8(cls, mask_vec)) == 0) {
369
+ if (!cross_lines || !(vgetq_lane_u8(cls, 15) & tail_req)) break;
370
+ uint8x16_t lf = vceqq_u8(chars, nl_vec);
371
+ if (vminvq_u8(vorrq_u8(vandq_u8(cls, mask_vec), lf)) == 0) break;
372
+ pass->line_feeds += vaddvq_u8(vshrq_n_u8(lf, 7));
373
+ uint64_t nibbles = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(lf), 4)), 0);
374
+ pass->line = pass->i + 16u - ((uint32_t)__builtin_clzll(nibbles) >> 2);
375
+ pass->floor = pass->line;
376
+ }
377
+ pass->seen_vec = vorrq_u8(pass->seen_vec, cls);
378
+ pass->i += 16;
379
+ }
380
+ }
381
+
382
+ DIRECTIVE_INLINE bool directive_units_neon_load(const char *units, unsigned size, uint32_t i, uint8x16_t *chars) {
383
+ if (size == 1) {
384
+ *chars = vld1q_u8((const uint8_t *)units + i);
385
+ return vmaxvq_u8(*chars) < 0x80;
386
+ }
387
+ const uint8_t *ptr = (const uint8_t *)units + 2 * (size_t)i;
388
+ uint16x8_t lo = vreinterpretq_u16_u8(vld1q_u8(ptr));
389
+ uint16x8_t hi = vreinterpretq_u16_u8(vld1q_u8(ptr + 16));
390
+ if (vmaxvq_u16(vorrq_u16(lo, hi)) >= 0x80) return false;
391
+ *chars = vcombine_u8(vmovn_u16(lo), vmovn_u16(hi));
392
+ return true;
393
+ }
394
+ #endif
395
+
396
+ DIRECTIVE_INLINE uint32_t directive_units_advance_mask(DirectiveUnitPass *pass, unsigned size, uint8_t mask) {
397
+ #ifdef DIRECTIVE_USE_NEON
398
+ if (pass->count - pass->i >= 16) directive_units_neon_advance(pass, size, mask);
399
+ #endif
400
+ uint32_t c = 0;
401
+ uint8_t cls = 0;
402
+ while (pass->i < pass->count && (c = directive_unit(pass->units, size, pass->i)) < UNIT_CLASS_COUNT &&
403
+ ((cls = pass->classes[c]) & mask)) {
404
+ pass->seen |= cls;
405
+ pass->i++;
406
+ }
407
+ return c;
408
+ }
409
+
410
+ // Starts a word, comment or literal whose first `length` units are read with this.
411
+ DIRECTIVE_INLINE void directive_units_open(DirectiveUnitPass *pass, unsigned size, DirectiveScanMode mode, uint32_t length) {
412
+ directive_units_flush_pragma(pass, size);
413
+ pass->mode = mode;
414
+ pass->start = pass->i;
415
+ pass->fresh = true;
416
+ pass->i += length;
417
+ }
418
+
419
+ // Ends the word, comment or literal being read; code follows it.
420
+ DIRECTIVE_INLINE bool directive_units_close(DirectiveUnitPass *pass, unsigned size) {
421
+ directive_units_flush_pragma(pass, size);
422
+ pass->mode = SCAN_CODE;
423
+ pass->floor = pass->i;
424
+ return true;
425
+ }
426
+
427
+ // Reads the line feed at `pass->i` unless the pass stops at line feeds, or a carriage return may belong to it.
428
+ DIRECTIVE_INLINE bool directive_units_line_feed(DirectiveUnitPass *pass, unsigned size) {
429
+ if (!pass->lines || pass->i + 1 >= pass->count || directive_unit(pass->units, size, pass->i + 1) == '\r') return false;
430
+ directive_units_flush_pragma(pass, size);
431
+ pass->line_feeds++;
432
+ pass->line = ++pass->i;
433
+ pass->floor = pass->i;
434
+ pass->pragma_from = pass->i;
435
+ return true;
436
+ }
437
+
438
+ // Whether the backslash at `pass->i` starts no line splice and the unit after it is a plain character.
439
+ DIRECTIVE_INLINE bool directive_units_escape(const DirectiveUnitPass *pass, unsigned size) {
440
+ if (pass->i + 1 >= pass->count) return false;
441
+ uint32_t next = directive_unit(pass->units, size, pass->i + 1);
442
+ return next < UNIT_CLASS_COUNT && (pass->classes[next] & (UNIT_LINE | UNIT_SPACE)) == UNIT_LINE;
443
+ }
444
+
445
+ DIRECTIVE_INLINE void directive_units_flush_pragma(DirectiveUnitPass *pass, unsigned size) {
446
+ #ifdef DIRECTIVE_USE_NEON
447
+ if (vmaxvq_u8(vandq_u8(pass->seen_vec, vdupq_n_u8(UNIT_UNDERSCORE)))) pass->seen |= UNIT_UNDERSCORE;
448
+ if (vmaxvq_u8(vandq_u8(pass->seen_vec, vdupq_n_u8(UNIT_P)))) pass->seen |= UNIT_P;
449
+ pass->seen_vec = vdupq_n_u8(0);
450
+ #endif
451
+ if ((pass->seen & UNIT_PRAGMA) == UNIT_PRAGMA &&
452
+ directive_units_pragma(pass->units, size, pass->pragma_from, pass->i)) {
453
+ pass->pragma = true;
454
+ }
455
+ pass->seen = 0;
456
+ pass->pragma_from = pass->i;
457
+ }
458
+
459
+ // The unit at `index` of units of `size` bytes in the host's byte order.
460
+ DIRECTIVE_INLINE uint32_t directive_unit(const char *units, unsigned size, uint32_t index) {
461
+ if (size == 1) return (uint8_t)units[index];
462
+ uint16_t unit;
463
+ memcpy(&unit, units + 2 * (size_t)index, sizeof unit);
464
+ return unit;
465
+ }
466
+
467
+ // Leaves in `scan` what the character path keeps of the `end` units read: the last characters of the unfinished word,
468
+ // comment or literal, which starts at `from` or, when that is `UINT32_MAX`, before the units, and the run of spaces
469
+ // and backslashes that the units end with.
470
+ // code-gauge-allow functionCognitiveComplexity -- The characters kept and the run of spaces each depend on where their text starts among the units.
471
+ static void directive_scan_units_end(DirectiveScan *scan, const char *units, unsigned size, uint32_t from, uint32_t end, uint32_t byte) {
472
+ if (scan->mode == SCAN_CODE) {
473
+ scan->recent = 0;
474
+ } else {
475
+ uint64_t recent = from == UINT32_MAX ? scan->recent : 0;
476
+ uint32_t first = from == UINT32_MAX ? 0 : from;
477
+ for (uint32_t k = end - first > 8 ? end - 8 : first; k < end; k++) recent = recent << 8 | directive_unit(units, size, k);
478
+ scan->recent = recent;
479
+ if (from != UINT32_MAX && scan->mode == SCAN_WORD) scan->raw = (GnuRawStringPrefix){0};
480
+ if (scan->mode == SCAN_WORD && end > first) directive_scan_units_word(scan, units, size, first, end);
481
+ }
482
+ uint32_t run = end;
483
+ while (run > 0) {
484
+ uint32_t c = directive_unit(units, size, run - 1);
485
+ if (!directive_horizontal((int32_t)c) && c != '\n' && c != '\\') break;
486
+ run--;
487
+ }
488
+ bool continued = run == 0 && scan->in_run;
489
+ scan->in_run = run < end;
490
+ if (!scan->in_run) return;
491
+ if (!continued) {
492
+ scan->run_spans = 0;
493
+ scan->run_start = byte + run * size;
494
+ }
495
+ scan->run_end = byte + (end - 1) * size + 1;
496
+ }
497
+
498
+ // Adds the word's characters from `first` to before `end`, as `directive_scan_word_characters` does.
499
+ static void directive_scan_units_word(DirectiveScan *scan, const char *units, unsigned size, uint32_t first, uint32_t end) {
500
+ scan->previous = (int32_t)directive_unit(units, size, end - 1);
501
+ if (scan->raw.length >= sizeof scan->raw.value) return;
502
+ if (end - first > sizeof scan->raw.value - 1 - scan->raw.length) {
503
+ gnu_raw_string_prefix_invalidate(&scan->raw);
504
+ return;
505
+ }
506
+ for (uint32_t k = first; k < end; k++) gnu_raw_string_prefix_character(&scan->raw, (int32_t)directive_unit(units, size, k));
507
+ }
508
+
509
+ // Whether the ASCII units from `start` to before `end` hold `pragma_word`.
510
+ static bool directive_units_pragma(const char *units, unsigned size, uint32_t start, uint32_t end) {
511
+ uint32_t length = sizeof pragma_word - 1;
512
+ for (uint32_t i = start; i + length <= end; i++) {
513
+ if (directive_unit(units, size, i) != '_' || directive_unit(units, size, i + 1) != 'P') continue;
514
+ uint32_t matched = 2;
515
+ while (matched < length && directive_unit(units, size, i + matched) == (uint8_t)pragma_word[matched]) matched++;
516
+ if (matched == length) return true;
517
+ }
518
+ return false;
519
+ }
520
+
521
+ // Whether the characters in `recent` end with a start of `pragma_word` that the next ones may complete.
522
+ static bool directive_pragma_pending(uint64_t recent) {
523
+ uint64_t word = 0;
524
+ for (unsigned length = 1; length < sizeof pragma_word - 1; length++) {
525
+ word = word << 8 | (uint8_t)pragma_word[length - 1];
526
+ if ((recent & ((1ull << 8 * length) - 1)) == word) return true;
527
+ }
528
+ return false;
529
+ }
530
+
531
+ // code-gauge-allow functionCognitiveComplexity -- Each class is one condition on the character.
532
+ static void directive_unit_classes(uint8_t *classes) {
533
+ for (int32_t c = 0; c < UNIT_CLASS_COUNT; c++) {
534
+ bool text = c != 0 && !directive_newline(c) && c != '\\';
535
+ bool word = directive_scan_ascii_word(c);
536
+ bool space = directive_horizontal(c);
537
+ uint8_t code = (word ? UNIT_WORD | UNIT_CODE : 0) | (space ? UNIT_SPACE : directive_scan_ascii_plain(c) ? UNIT_CODE : 0);
538
+ uint8_t letter = c == '_' ? UNIT_UNDERSCORE : c == 'P' ? UNIT_P : 0;
539
+ uint8_t block = text && c != '*' && c != '/' ? UNIT_BLOCK : 0, quote = text && c != '"' && c != '\'' ? UNIT_QUOTE : 0;
540
+ classes[c] = (uint8_t)(code | letter | (text ? UNIT_LINE : 0) | block | quote);
541
+ }
542
+ }
543
+
544
+ #endif