@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.
@@ -1,65 +1,20 @@
1
- // code-gauge-allow-file fileNcss -- One source-fact pass shares on-demand directive-segment storage, directive lexing, physical-position conversion, prefix spans and fact bitsets.
1
+ // code-gauge-allow-file fileNcss -- One source-fact pass shares directive lexing, line-state tracking, and reverse prefix carry.
2
2
  #ifndef TREE_SITTER_C_EMPTY_DIRECTIVES_H_
3
3
  #define TREE_SITTER_C_EMPTY_DIRECTIVES_H_
4
4
 
5
- #include "directiveScan.h"
6
- #include <stdlib.h>
5
+ #include "directiveFacts.h"
7
6
 
8
- #define DIRECTIVE_PAGE_SHIFT 12
9
- #define DIRECTIVE_PAGE_SIZE (1 << DIRECTIVE_PAGE_SHIFT)
10
- #define DIRECTIVE_SEGMENT_UNITS (DIRECTIVE_PAGE_SIZE * 16)
11
- #define DIRECTIVE_FILL_UNITS 256
12
-
13
- typedef struct {
14
- uint32_t index, byte, width;
15
- } DirectivePosition;
16
-
17
- // The characters of one directive segment, read from the lexer as the line-state pass needs them.
18
- typedef struct {
19
- uint16_t **characters;
20
- DirectivePosition *positions;
21
- TSLexer *lexer;
22
- uint32_t length, page_count, page_capacity, position_count, position_capacity;
23
- // The lexer's byte position, which follows the last stored character.
24
- uint32_t fill_byte;
25
- uint32_t conditional_depth;
26
- // The characters stored last, as `DirectiveScan` keeps those it reads.
27
- uint64_t recent;
28
- // Whether the current segment recorded a null directive or a line-end barrier, which the reverse pass carries, and
29
- // whether the characters stored or scanned hold `pragma_word`.
30
- bool utf16, encoding_known, failed, carries, pragma;
31
- } DirectiveBuffer;
32
-
33
- // A position in a `DirectiveBuffer`; copies share the buffer, so a lookahead copy reads what the original will.
34
- typedef struct {
35
- DirectiveBuffer *buffer;
36
- uint32_t index, byte, position_index, previous_byte, before_previous_byte;
37
- // Whether a line splice with spaces before its newline was read, which the lexer's tokens do not take as a splice.
38
- bool spaced_splice;
39
- } DirectiveInput;
40
-
41
- #define DIRECTIVE_FACT_PAGE_SHIFT 15
42
- #define DIRECTIVE_FACT_PAGE_SIZE (1 << DIRECTIVE_FACT_PAGE_SHIFT)
43
-
44
- // Each kind of fact is a directory of `pages` pointers, one per `DIRECTIVE_FACT_PAGE_SIZE` source bytes up to and
45
- // possibly past the furthest fact, to bitset pages allocated only where a fact is set, so a missing directory or page
46
- // holds no fact; source without facts, such as an excluded range, costs one pointer per page of each directory.
47
- // `empty_directives_release` frees them.
48
7
  typedef struct {
49
- uint8_t **starts, **ends, **comments, **line_ends, **directive_starts, **directive_ends;
50
- uint32_t pages;
51
- // `anchored` and `ready` belong to the scanner state: whether the token at the start of the input is out, and
52
- // whether the facts, which `built` says exist, are in use. `pragmas` is the fact that the input read for them holds
53
- // `pragma_word`, without which no scan has to look for a `_Pragma` operator.
54
- bool built, anchored, ready, pragmas;
55
- } EmptyDirectives;
8
+ uint32_t start, end;
9
+ bool open;
10
+ } DirectiveOpenSpan;
56
11
 
57
12
  typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_UNDEF, DIRECTIVE_LINE, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
58
13
 
59
- // The reverse pass's state; the trailing run's open span covers the bytes from `tail_start` to before `tail_end`.
14
+ // The reverse pass's state over a segment's condition line ends and trailing trivia run.
60
15
  typedef struct {
61
- bool prefix, line_end, tail, tail_open;
62
- uint32_t tail_start, tail_end;
16
+ bool prefix, line_end, tail;
17
+ DirectiveOpenSpan line_end_span, tail_span;
63
18
  } DirectiveCarry;
64
19
 
65
20
  typedef struct {
@@ -70,79 +25,80 @@ typedef struct {
70
25
  uint32_t start, comment_start, required_end;
71
26
  } DirectiveLine;
72
27
 
28
+ typedef struct {
29
+ GnuRawStringPrefix raw;
30
+ char directive[13];
31
+ unsigned length;
32
+ } DirectiveWord;
33
+
34
+ typedef struct {
35
+ const char *units;
36
+ uint32_t count, size, base_byte;
37
+ } DirectiveUnitsSlice;
38
+
39
+ typedef struct {
40
+ uint32_t hash, kw_end, nl;
41
+ bool barrier;
42
+ } DirectivePlainOutcome;
43
+
73
44
  static bool directive_scan_buffered(DirectiveScan *scan, DirectiveInput *input);
74
- static bool directive_scan_lexer(DirectiveScan *scan, DirectiveBuffer *buffer);
45
+ static bool directive_scan_lexer(EmptyDirectives *state, DirectiveScan *scan, DirectiveBuffer *buffer);
46
+ static bool directive_scan_lexer_plain_lines(EmptyDirectives *state, DirectiveScan *scan, DirectiveBuffer *buffer);
47
+ static uint32_t directive_units_skip_blank_lines(DirectiveUnitsSlice slice, uint32_t *index_io);
48
+ static void directive_scan_finish_plain_lines(DirectiveScan *scan, TSLexer *lexer, DirectiveUnitsSlice slice, uint32_t consumed, uint32_t line_feeds, DirectivePlainOutcome last);
49
+ static bool directive_units_plain_line(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveUnitsSlice slice, uint32_t *index_io, DirectivePlainOutcome *out);
50
+ static bool directive_units_plain_keyword(DirectiveUnitsSlice slice, uint32_t *index_io, DirectiveWord *word);
51
+ static bool directive_units_record_plain(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveUnitsSlice slice, const DirectiveWord *word, DirectiveKind kind, uint32_t hash, uint32_t kw_end, uint32_t nl, bool has_operand);
52
+ static bool directive_units_plain_header(const char *units, uint32_t size, uint32_t *index_io, uint32_t count, bool *has_operand);
53
+ static bool directive_units_plain_tail(const char *units, uint32_t size, uint32_t i, uint32_t count, DirectiveKind kind, uint32_t *nl_out, bool *has_operand);
54
+ static bool directive_units_plain_comment(const char *units, uint32_t size, uint32_t i, uint32_t count, uint32_t *nl_out);
75
55
  static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, DirectiveSpans *tail);
76
56
  static void directive_scan_handoff(DirectiveScan *scan, uint32_t hash, bool skipped);
77
57
  static bool directive_segment(EmptyDirectives *state, DirectiveInput *input);
78
58
  static bool directive_segment_ends(const DirectiveInput *input);
59
+ static bool directive_lexer_segment_ends(const TSLexer *lexer);
60
+ static bool directive_units_short_comment(const char *units, uint32_t size, uint32_t i, uint32_t limit);
79
61
  static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit);
80
62
  static bool directive_line_clean(const DirectiveLine *line);
81
- static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
82
63
  static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail);
83
64
  static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail);
84
- static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width);
85
- static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte);
86
- static void directive_carry_tail(DirectiveCarry *carry, DirectiveSpans *tail, uint32_t byte, uint32_t width);
87
- static void directive_spans_reverse(DirectiveSpans *spans);
88
- static void directive_mark_spans(EmptyDirectives *state, DirectiveBuffer *buffer, const DirectiveSpans *spans);
89
- static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte);
90
- static inline bool directive_has(const DirectiveInput *input, uint32_t index);
91
- static bool directive_fill(DirectiveBuffer *buffer, uint32_t index);
92
- static bool directive_append(DirectiveBuffer *buffer);
93
- static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c);
94
- static inline void directive_note(DirectiveBuffer *buffer, uint32_t c);
95
- static bool directive_reserve(DirectiveBuffer *buffer, unsigned width);
96
- static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width);
97
- static void directive_encode(DirectiveBuffer *buffer, uint32_t c);
98
- static uint16_t directive_character(const DirectiveInput *input, uint32_t index);
99
- static void directive_store(DirectiveBuffer *buffer, uint16_t c);
100
- static void directive_release(DirectiveBuffer *buffer);
101
- static unsigned directive_utf8_width(uint32_t c);
102
- static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte);
103
- static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte);
104
- static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages);
65
+ static bool directive_prefixes_done(const EmptyDirectives *state, const DirectiveBuffer *buffer, const DirectiveCarry *carry);
66
+ static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte, uint32_t span_width);
67
+ static void directive_open_span_extend(DirectiveOpenSpan *span, DirectiveSpans *spans, uint32_t byte, uint32_t width);
68
+ static void directive_open_span_flush(DirectiveOpenSpan *span, DirectiveSpans *spans);
69
+ static void directive_spans_reverse_from(DirectiveSpans *spans, uint32_t from);
70
+ static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t *position, uint32_t *byte);
71
+ static uint32_t directive_inert_width(uint32_t c, bool utf16);
105
72
  static void directive_barrier(EmptyDirectives *state, DirectiveInput *input);
106
73
  static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
107
74
  static void directive_complete_required(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
108
75
  static bool directive_requires_operand(DirectiveKind kind);
109
76
  static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
77
+ static void directive_start_hash(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
78
+ static bool directive_try_plain_directive(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
79
+ static void directive_record_plain_directive(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, DirectiveKind kind, uint32_t kw_end_byte, uint32_t nl, bool has_operand);
80
+ static bool directive_scan_plain_header(const uint16_t *page, uint32_t *index_io, uint32_t end, bool *has_operand);
81
+ static bool directive_scan_plain_tail(const uint16_t *page, uint32_t i, uint32_t end, DirectiveKind kind, uint32_t *nl_out, bool *has_operand);
82
+ static bool directive_scan_plain_comment(const uint16_t *page, uint32_t i, uint32_t end, uint32_t *nl_out);
110
83
  static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
111
84
  static bool directive_pragma_part(DirectiveLine *line, int32_t c);
112
85
  static bool directive_neutral(const DirectiveLine *line);
86
+ static bool directive_skip_plain_line(DirectiveInput *input, int32_t first, bool condition);
87
+ static uint32_t directive_plain_line_end(const DirectiveInput *input);
88
+ static bool directive_plain_condition_step(const uint16_t *page, uint32_t i, uint32_t end, bool *after_lparen, uint16_t *prev);
113
89
  static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
90
+ static void directive_neutral_units(DirectiveInput *input);
114
91
  static bool directive_header_ready(DirectiveLine *line, int32_t c);
115
92
  static bool directive_header(DirectiveInput *input, int32_t close);
116
93
  static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
117
- static bool directive_skip_splice(DirectiveInput *input);
118
- static uint32_t directive_splice_end(const DirectiveInput *input);
119
94
 
120
95
  static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
121
- static void empty_directives_release(EmptyDirectives *state);
122
- static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte);
123
- static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte);
124
- static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
125
- static int32_t directive_next(DirectiveInput *input);
126
- static int32_t directive_physical_next(DirectiveInput *input);
127
- static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c);
128
- static int32_t directive_peek(const DirectiveInput *input);
129
- static int32_t directive_plain(const DirectiveInput *input);
130
- static void directive_skip_plain(DirectiveInput *input);
131
- typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_CODE, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
132
- static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
133
- static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end);
134
- static bool directive_run_continues(DirectiveRun run, uint16_t c);
135
96
  static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input);
136
97
  static bool directive_pragma_operator(const DirectiveInput *input);
137
98
  static int32_t directive_pragma_token(DirectiveInput *input);
138
99
  static bool directive_comment(DirectiveInput *input, bool *line);
139
100
  static bool directive_quoted(DirectiveInput *input, int32_t quote);
140
101
  static bool directive_raw(DirectiveInput *input);
141
- typedef struct {
142
- GnuRawStringPrefix raw;
143
- char directive[13];
144
- unsigned length;
145
- } DirectiveWord;
146
102
 
147
103
  static uint32_t directive_keyword_end(const DirectiveInput *input);
148
104
  static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end);
@@ -151,7 +107,6 @@ static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool
151
107
  static DirectiveKind directive_word_kind(const DirectiveWord *word);
152
108
  static void directive_word_character(DirectiveWord *word, int32_t c);
153
109
  static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
154
- static uint32_t directive_byte(const DirectiveInput *input);
155
110
  static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end);
156
111
 
157
112
  // Scans the zero-width token `symbol` that starts the directive context: once at the start of the input, where it
@@ -169,87 +124,6 @@ static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer,
169
124
  return state->ready || !anchored;
170
125
  }
171
126
 
172
- static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer *lexer) {
173
- if (!state || !state->directive_ends) return false;
174
- TSLexerPosition position;
175
- lexer->get_position(lexer, &position);
176
- return directive_bit(state, state->directive_ends, position.bytes);
177
- }
178
-
179
- // False only when the facts show that no `_Pragma` operator can follow any position.
180
- static bool empty_directives_pragma_possible(const EmptyDirectives *state) {
181
- return !state || !state->built || state->pragmas;
182
- }
183
-
184
- // Whether none of the `count` tokens, at least eight, is valid but those whose byte in `extras` is 0xff.
185
- static inline bool empty_directives_only_valid(const bool *valid_symbols, const uint8_t *extras, unsigned count) {
186
- _Static_assert(sizeof(bool) == 1, "valid_symbols is read as bytes");
187
- uint64_t others = 0;
188
- for (unsigned i = 0; i < count; i += 8) {
189
- unsigned offset = i + 8 > count ? count - 8 : i;
190
- uint64_t valid, extra;
191
- memcpy(&valid, valid_symbols + offset, sizeof valid);
192
- memcpy(&extra, extras + offset, sizeof extra);
193
- others |= valid & ~extra;
194
- }
195
- return !others;
196
- }
197
-
198
- static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lexer) {
199
- if (!state || !state->directive_starts) return false;
200
- TSLexerPosition position;
201
- lexer->get_position(lexer, &position);
202
- return directive_bit(state, state->directive_starts, position.bytes);
203
- }
204
-
205
- static bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *lexer) {
206
- if (!state || !state->line_ends || !(directive_horizontal(lexer->lookahead) || directive_newline(lexer->lookahead))) return false;
207
- TSLexerPosition position;
208
- lexer->get_position(lexer, &position);
209
- return empty_directives_line_end_at(state, position.bytes);
210
- }
211
-
212
- static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte) {
213
- return directive_bit(state, state->line_ends, byte);
214
- }
215
-
216
- static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
217
-
218
- static bool empty_directives_skip(TSLexer *lexer, void *payload) {
219
- return empty_directives_consume(lexer, payload, false);
220
- }
221
-
222
- // A null directive, including its prefix, starts only at `#`, `\\` or a space, so other characters cannot match `starts`.
223
- static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
224
- int32_t c = lexer->lookahead;
225
- if (!state || !state->starts || !(c == '#' || c == '\\' || pragma_space(c))) return false;
226
- TSLexerPosition position;
227
- lexer->get_position(lexer, &position);
228
- if (!empty_directives_start_at(state, position.bytes)) return false;
229
- empty_directives_consume_started(lexer, state, skip_prefix);
230
- return true;
231
- }
232
-
233
- static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte) {
234
- return directive_bit(state, state->starts, byte);
235
- }
236
-
237
- // Consumes the rest of a null directive whose start was confirmed at a position the lexer has not left except over
238
- // prefix characters: as a token (`skip_prefix`) up to its first recorded end, which `directive_record` places before
239
- // the comments or after the line, and otherwise the whole logical line, since some scanner lookahead dispatches on
240
- // the character after the skip without reading comments.
241
- static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
242
- TSLexerPosition position;
243
- while (lexer->lookahead != '#' && !pragma_eof(lexer)) lexer->advance(lexer, skip_prefix);
244
- for (bool whole = !skip_prefix;; whole = false) {
245
- do {
246
- lexer->advance(lexer, false);
247
- lexer->get_position(lexer, &position);
248
- } while (!pragma_eof(lexer) && !directive_bit(state, state->ends, position.bytes));
249
- if (!whole || pragma_eof(lexer) || !directive_bit(state, state->comments, position.bytes)) return;
250
- }
251
- }
252
-
253
127
  // Ordinary code goes through `DirectiveScan` without being stored, and only directive segments, each from a directive's
254
128
  // `#` to the end of the logical line where no directive state is left, are stored for the line-state pass and the
255
129
  // reverse prefix pass. The facts are stored as `EmptyDirectives` describes.
@@ -269,19 +143,21 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
269
143
  DirectiveInput input = {.buffer = &buffer, .byte = position.bytes};
270
144
  // `run` holds the run before a segment's `#`, and `tail` the segment's trailing run.
271
145
  DirectiveSpans run = {0}, tail = {0};
272
- DirectiveScan scan = {.beginning = true, .run = &run, .pragma = &buffer.pragma};
146
+ DirectiveScan scan = {.beginning = true, .run = &run, .classes = buffer.classes, .pragma = &buffer.pragma};
273
147
  if (directive_scan_leading_comment(&scan, lexer)) {
274
148
  free(run.items);
275
149
  return false;
276
150
  }
151
+ directive_unit_classes(buffer.classes);
277
152
  for (;;) {
278
153
  if (!directive_scan_buffered(&scan, &input)) {
279
154
  buffer.length = buffer.position_count = 0;
280
- if (!directive_scan_lexer(&scan, &buffer)) break;
155
+ if (!directive_scan_lexer(state, &scan, &buffer)) break;
281
156
  input = (DirectiveInput){.buffer = &buffer, .byte = buffer.fill_byte};
282
157
  }
283
158
  directive_shift(&buffer, input.index);
284
159
  buffer.carries = false;
160
+ state->barriers.count = 0;
285
161
  uint32_t start = input.byte;
286
162
  input = (DirectiveInput){.buffer = &buffer, .byte = start, .previous_byte = start, .before_previous_byte = start};
287
163
  bool more = directive_segment(state, &input);
@@ -299,7 +175,7 @@ static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, Dire
299
175
  DirectiveSpans before = *run;
300
176
  *run = *tail;
301
177
  *tail = before;
302
- *scan = (DirectiveScan){.beginning = true, .run = run, .pragma = scan->pragma};
178
+ *scan = (DirectiveScan){.beginning = true, .run = run, .classes = scan->classes, .pragma = scan->pragma};
303
179
  if (!run->count) return;
304
180
  DirectiveSpan last = run->items[--run->count];
305
181
  scan->run_spans = run->count;
@@ -331,13 +207,17 @@ static bool directive_scan_buffered(DirectiveScan *scan, DirectiveInput *input)
331
207
  }
332
208
 
333
209
  // Feeds the lexer's characters until a directive's `#`, which is left as the lookahead, or the end of the input.
334
- static bool directive_scan_lexer(DirectiveScan *scan, DirectiveBuffer *buffer) {
210
+ static bool directive_scan_lexer(EmptyDirectives *state, DirectiveScan *scan, DirectiveBuffer *buffer) {
335
211
  TSLexer *lexer = buffer->lexer;
336
212
  TSLexerPosition position;
337
213
  for (;;) {
338
214
  directive_scan_plain_run(scan, lexer);
339
215
  if (pragma_eof(lexer)) break;
340
216
  int32_t c = lexer->lookahead;
217
+ if (c == '#' && scan->beginning && !scan->splice && !scan->pair &&
218
+ directive_scan_lexer_plain_lines(state, scan, buffer)) {
219
+ continue;
220
+ }
341
221
  if (directive_scan_feed(scan, c)) {
342
222
  lexer->get_position(lexer, &position);
343
223
  buffer->fill_byte = position.bytes;
@@ -352,6 +232,168 @@ static bool directive_scan_lexer(DirectiveScan *scan, DirectiveBuffer *buffer) {
352
232
  return false;
353
233
  }
354
234
 
235
+ // Scans consecutive plain ASCII directive lines directly from `peek_units` without buffering or running the reverse pass.
236
+ static bool directive_scan_lexer_plain_lines(EmptyDirectives *state, DirectiveScan *scan, DirectiveBuffer *buffer) {
237
+ TSLexer *lexer = buffer->lexer;
238
+ if (lexer->is_at_included_range_start(lexer) || directive_pragma_pending(scan->recent)) return false;
239
+ DirectiveUnitsSlice slice = {0};
240
+ slice.units = lexer->peek_units(lexer, &slice.count, &slice.size);
241
+ if (slice.count < 4) return false;
242
+ TSLexerPosition position;
243
+ lexer->get_position(lexer, &position);
244
+ slice.base_byte = position.bytes;
245
+ uint32_t i = 0, consumed = 0, line_feeds = 0;
246
+ DirectivePlainOutcome last = {0};
247
+ for (;;) {
248
+ uint32_t line_start = i;
249
+ uint32_t extra_lf = consumed ? directive_units_skip_blank_lines(slice, &i) : 0;
250
+ if (i >= slice.count || directive_unit(slice.units, slice.size, i) != '#' ||
251
+ !directive_units_plain_line(state, buffer, slice, &i, &last)) {
252
+ i = line_start;
253
+ break;
254
+ }
255
+ line_feeds += extra_lf + 1;
256
+ consumed = i;
257
+ }
258
+ if (!consumed) return false;
259
+ directive_scan_finish_plain_lines(scan, lexer, slice, consumed, line_feeds, last);
260
+ return true;
261
+ }
262
+
263
+ static uint32_t directive_units_skip_blank_lines(DirectiveUnitsSlice slice, uint32_t *index_io) {
264
+ uint32_t i = *index_io, extra_lf = 0;
265
+ for (; i < slice.count; i++) {
266
+ uint32_t ch = directive_unit(slice.units, slice.size, i);
267
+ if (directive_horizontal((int32_t)ch)) continue;
268
+ if (ch == '\n' && i + 1 < slice.count && directive_unit(slice.units, slice.size, i + 1) != '\r') {
269
+ extra_lf++;
270
+ continue;
271
+ }
272
+ break;
273
+ }
274
+ *index_io = i;
275
+ return extra_lf;
276
+ }
277
+
278
+ static void directive_scan_finish_plain_lines(DirectiveScan *scan, TSLexer *lexer, DirectiveUnitsSlice slice, uint32_t consumed, uint32_t line_feeds, DirectivePlainOutcome last) {
279
+ scan->mode = SCAN_CODE;
280
+ scan->beginning = true;
281
+ scan->recent = 0;
282
+ scan->run_spans = 0;
283
+ if (last.barrier) {
284
+ scan->in_run = false;
285
+ } else {
286
+ uint32_t run = last.nl;
287
+ while (run > last.kw_end && directive_horizontal((int32_t)directive_unit(slice.units, slice.size, run - 1))) run--;
288
+ scan->in_run = true;
289
+ scan->run_start = slice.base_byte + run * slice.size;
290
+ scan->run_end = slice.base_byte + (consumed - 1) * slice.size + 1;
291
+ }
292
+ lexer->advance_units(lexer, consumed, line_feeds, 0);
293
+ }
294
+
295
+ static bool directive_units_plain_line(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveUnitsSlice slice, uint32_t *index_io, DirectivePlainOutcome *out) {
296
+ uint32_t hash = *index_io, i = hash + 1;
297
+ DirectiveWord word = {0};
298
+ if (!directive_units_plain_keyword(slice, &i, &word)) return false;
299
+ uint32_t kw_end = i, nl = 0;
300
+ DirectiveKind kind = directive_word_kind(&word);
301
+ bool has_operand = false;
302
+ if (kind == DIRECTIVE_HEADER && !directive_units_plain_header(slice.units, slice.size, &i, slice.count, &has_operand)) return false;
303
+ if (!directive_units_plain_tail(slice.units, slice.size, i, slice.count, kind, &nl, &has_operand)) return false;
304
+ bool barrier = directive_units_record_plain(state, buffer, slice, &word, kind, hash, kw_end, nl, has_operand);
305
+ *index_io = nl + 1;
306
+ *out = (DirectivePlainOutcome){.hash = hash, .kw_end = kw_end, .nl = nl, .barrier = barrier};
307
+ return true;
308
+ }
309
+
310
+ static bool directive_units_plain_keyword(DirectiveUnitsSlice slice, uint32_t *index_io, DirectiveWord *word) {
311
+ uint32_t i = *index_io;
312
+ while (i < slice.count && directive_horizontal((int32_t)directive_unit(slice.units, slice.size, i))) i++;
313
+ if (i >= slice.count || !empty_directives_name_character((int32_t)directive_unit(slice.units, slice.size, i), false)) return false;
314
+ while (i < slice.count && empty_directives_name_character((int32_t)directive_unit(slice.units, slice.size, i), true)) {
315
+ directive_word_character(word, (int32_t)directive_unit(slice.units, slice.size, i++));
316
+ }
317
+ if (i >= slice.count || word->length >= sizeof word->directive) return false;
318
+ uint32_t sep = directive_unit(slice.units, slice.size, i);
319
+ if (sep >= 0x80 || sep == '\\' || preproc_word((int32_t)sep, true)) return false;
320
+ *index_io = i;
321
+ return true;
322
+ }
323
+
324
+ // code-gauge-allow functionParameterCount -- Commits starts, conditional depth, ends, and line-end spans for one unbuffered directive.
325
+ static bool directive_units_record_plain(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveUnitsSlice slice, const DirectiveWord *word, DirectiveKind kind, uint32_t hash, uint32_t kw_end, uint32_t nl, bool has_operand) {
326
+ directive_offset_push(&state->directive_starts, buffer, slice.base_byte + hash * slice.size);
327
+ bool ignored = buffer->conditional_depth > 0;
328
+ if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "ifdef") == 0 || strcmp(word->directive, "ifndef") == 0) buffer->conditional_depth++;
329
+ else if (strcmp(word->directive, "endif") == 0 && buffer->conditional_depth) buffer->conditional_depth--;
330
+ if (!directive_requires_operand(kind) || ignored || has_operand) {
331
+ directive_offset_push(&state->directive_ends, buffer, slice.base_byte + kw_end * slice.size);
332
+ }
333
+ bool barrier = kind == DIRECTIVE_CONDITION || (directive_requires_operand(kind) && !has_operand);
334
+ if (barrier) {
335
+ uint32_t sp = nl;
336
+ while (sp > kw_end && directive_horizontal((int32_t)directive_unit(slice.units, slice.size, sp - 1))) sp--;
337
+ directive_spans_push(&state->line_ends, slice.base_byte + sp * slice.size, slice.base_byte + (nl + 1) * slice.size);
338
+ }
339
+ return barrier;
340
+ }
341
+
342
+ static bool directive_units_plain_header(const char *units, uint32_t size, uint32_t *index_io, uint32_t count, bool *has_operand) {
343
+ uint32_t i = *index_io;
344
+ while (i < count && directive_horizontal((int32_t)directive_unit(units, size, i))) i++;
345
+ uint32_t open = i < count ? directive_unit(units, size, i) : 0;
346
+ if (open != '<' && open != '"') return false;
347
+ uint32_t close = open == '<' ? '>' : '"';
348
+ for (i++; i < count && directive_unit(units, size, i) != close; i++) {
349
+ uint32_t c = directive_unit(units, size, i);
350
+ if (c >= 0x80 || c == '\\' || directive_newline((int32_t)c)) return false;
351
+ }
352
+ if (i >= count) return false;
353
+ *index_io = i + 1;
354
+ *has_operand = true;
355
+ return true;
356
+ }
357
+
358
+ // code-gauge-allow functionCognitiveComplexity -- Validates plain-tail exclusions up to an unpaired `\n` in one unit pass.
359
+ // code-gauge-allow functionParameterCount -- Scans a unit slice from `i` to `count` and outputs newline index and operand presence.
360
+ static bool directive_units_plain_tail(const char *units, uint32_t size, uint32_t i, uint32_t count, DirectiveKind kind, uint32_t *nl_out, bool *has_operand) {
361
+ bool after_lparen = false;
362
+ uint32_t prev = 0;
363
+ for (; i < count; i++) {
364
+ uint32_t c = directive_unit(units, size, i);
365
+ if (c == '\n') {
366
+ if (i + 1 >= count || directive_unit(units, size, i + 1) == '\r') return false;
367
+ *nl_out = i;
368
+ return true;
369
+ }
370
+ if (c == '/') return directive_units_plain_comment(units, size, i, count, nl_out);
371
+ if (c >= 0x80 || c == '\r' || c == '"' || c == '\'' || c == '\\') return false;
372
+ if (prev == '_' && c == 'P' && (i + 1 >= count || directive_unit(units, size, i + 1) == 'r')) return false;
373
+ if (kind == DIRECTIVE_CONDITION && c == '<' && after_lparen) return false;
374
+ if (!directive_horizontal((int32_t)c)) {
375
+ *has_operand = true;
376
+ after_lparen = c == '(';
377
+ }
378
+ prev = c;
379
+ }
380
+ return false;
381
+ }
382
+
383
+ static bool directive_units_plain_comment(const char *units, uint32_t size, uint32_t i, uint32_t count, uint32_t *nl_out) {
384
+ if (i + 1 >= count || directive_unit(units, size, i + 1) != '/') return false;
385
+ for (i += 2; i < count; i++) {
386
+ uint32_t c = directive_unit(units, size, i);
387
+ if (c == '\n') {
388
+ if (i + 1 >= count || directive_unit(units, size, i + 1) == '\r') return false;
389
+ *nl_out = i;
390
+ return true;
391
+ }
392
+ if (c >= 0x80 || c == '\r' || c == '\\') return false;
393
+ }
394
+ return false;
395
+ }
396
+
355
397
  // Runs the line-state pass from a directive's `#` until a logical line ends with no directive state left and
356
398
  // `directive_segment_ends` lets `DirectiveScan` take over again. Returns false at the end of the input.
357
399
  static bool directive_segment(EmptyDirectives *state, DirectiveInput *input) {
@@ -375,6 +417,7 @@ static bool directive_segment(EmptyDirectives *state, DirectiveInput *input) {
375
417
  // which may run on for any length, goes back to `DirectiveScan`.
376
418
  static bool directive_segment_ends(const DirectiveInput *input) {
377
419
  if (input->index >= DIRECTIVE_SEGMENT_UNITS) return true;
420
+ if (input->index == input->buffer->length) return directive_lexer_segment_ends(input->buffer->lexer);
378
421
  uint32_t index = input->index, limit = input->index + DIRECTIVE_FILL_UNITS;
379
422
  while (index < limit && directive_has(input, index) && directive_horizontal(directive_character(input, index))) index++;
380
423
  if (index >= limit || !directive_has(input, index)) return true;
@@ -382,6 +425,31 @@ static bool directive_segment_ends(const DirectiveInput *input) {
382
425
  return c != '#' && !directive_newline(c) && !directive_short_line_comment(input, index, limit);
383
426
  }
384
427
 
428
+ static bool directive_lexer_segment_ends(const TSLexer *lexer) {
429
+ uint32_t count, size;
430
+ const char *units = lexer->peek_units(lexer, &count, &size);
431
+ if (!count) {
432
+ int32_t c = lexer->lookahead;
433
+ return c != '#' && !directive_newline(c);
434
+ }
435
+ uint32_t limit = count < DIRECTIVE_FILL_UNITS ? count : DIRECTIVE_FILL_UNITS;
436
+ uint32_t i = 0;
437
+ while (i < limit && directive_horizontal((int32_t)directive_unit(units, size, i))) i++;
438
+ if (i >= limit) return true;
439
+ uint32_t c = directive_unit(units, size, i);
440
+ return c != '#' && !directive_newline((int32_t)c) && !directive_units_short_comment(units, size, i, limit);
441
+ }
442
+
443
+ static bool directive_units_short_comment(const char *units, uint32_t size, uint32_t i, uint32_t limit) {
444
+ if (directive_unit(units, size, i) != '/' || i + 1 >= limit || directive_unit(units, size, i + 1) != '/') return false;
445
+ for (i += 2; i < limit; i++) {
446
+ uint32_t c = directive_unit(units, size, i);
447
+ if (c >= 0x80 || c == '\\') return false;
448
+ if (directive_newline((int32_t)c)) return true;
449
+ }
450
+ return false;
451
+ }
452
+
385
453
  // A line splice would continue the comment, so the line holds no `\`.
386
454
  static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit) {
387
455
  if (directive_character(input, index) != '/' || !directive_has(input, index + 1) ||
@@ -399,343 +467,272 @@ static bool directive_line_clean(const DirectiveLine *line) {
399
467
  !line->pragma_name && !line->pragma_depth && !line->header && line->kind == DIRECTIVE_ORDINARY && !line->required_end;
400
468
  }
401
469
 
402
- // Drops the characters before `start`, which `DirectiveScan` has consumed.
403
- static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
404
- if (!start) return;
405
- uint32_t length = buffer->length - start;
406
- for (uint32_t i = 0; i < length; i++) {
407
- uint32_t from = start + i;
408
- buffer->characters[i >> DIRECTIVE_PAGE_SHIFT][i & (DIRECTIVE_PAGE_SIZE - 1)] =
409
- buffer->characters[from >> DIRECTIVE_PAGE_SHIFT][from & (DIRECTIVE_PAGE_SIZE - 1)];
410
- }
411
- buffer->length = length;
412
- uint32_t count = 0;
413
- for (uint32_t i = 0; i < buffer->position_count; i++) {
414
- DirectivePosition position = buffer->positions[i];
415
- if (position.index < start) continue;
416
- position.index -= start;
417
- buffer->positions[count++] = position;
418
- }
419
- buffer->position_count = count;
420
- }
421
-
422
- // Without all the facts, none is kept, and a `_Pragma` operator may be anywhere.
470
+ // Without all the facts, none is kept, and a `_Pragma` operator or `#` directive may be anywhere.
423
471
  static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail) {
424
- bool failed = buffer->failed || run->failed || tail->failed;
472
+ bool failed = buffer->failed || run->failed || tail->failed || state->line_ends.failed;
425
473
  free(run->items);
426
474
  free(tail->items);
475
+ free(state->barriers.items);
476
+ state->barriers = (DirectiveBarriers){0};
427
477
  directive_release(buffer);
428
478
  state->pragmas = buffer->pragma || failed;
479
+ state->failed = failed;
429
480
  if (failed) empty_directives_release(state);
430
481
  }
431
482
 
432
- static void empty_directives_release(EmptyDirectives *state) {
433
- uint8_t **directories[] = {state->starts, state->ends, state->comments, state->line_ends, state->directive_starts, state->directive_ends};
434
- for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
435
- if (!directories[i]) continue;
436
- for (uint32_t page = 0; page < state->pages; page++) free(directories[i][page]);
437
- free(directories[i]);
438
- }
439
- state->starts = state->ends = state->comments = state->line_ends = state->directive_starts = state->directive_ends = NULL;
440
- state->pages = 0;
441
- }
442
-
443
483
  // Marks the prefixes of the segment's null directives and the spaces before its directive line ends, reading the
444
484
  // segment backwards from `input`, where its forward pass stopped. A prefix reaching the segment's `#` extends over the
445
485
  // run before the segment, whose spans `prefix` holds. Stores in `tail`, in byte order, the spans of the segment's
446
- // trailing run of spaces and backslashes after its last line end, over which a later null directive's prefix may extend.
486
+ // trailing run of spaces and backslashes after its last line end, over which a later directive's prefix may extend.
487
+ // code-gauge-allow functionCognitiveComplexity -- Carries null-directive prefixes, condition line ends, and trailing trivia runs in one backward pass.
447
488
  static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail) {
448
489
  DirectiveCarry carry = {.tail = true};
449
490
  uint32_t byte = input->byte, position = input->position_index, width;
491
+ uint32_t line_ends_base = state->line_ends.count;
492
+ uint32_t unit_bytes = input->buffer->encoding_known && input->buffer->utf16 ? 2u : 1u;
450
493
  bool complete = true;
451
494
  tail->count = 0;
452
495
  for (uint32_t i = input->index; i > 0;) {
453
- if (!carry.prefix && !carry.line_end && !carry.tail) i = directive_skip_inert(state, input, i, position, &byte);
496
+ if (!carry.prefix && !carry.line_end && !carry.tail) {
497
+ i = directive_skip_inert(state, input, i, &position, &byte);
498
+ }
454
499
  if (!i) break;
455
500
  int32_t c;
456
501
  complete = directive_step_back(input, &i, &position, &byte, &c, &width);
457
502
  if (!complete) break;
458
- directive_carry(state, input->buffer, &carry, c, byte);
459
- if (carry.tail) directive_carry_tail(&carry, tail, byte, width);
460
- else if (!input->buffer->carries) break;
461
- }
462
- if (carry.tail_open) directive_spans_push(tail, carry.tail_start, carry.tail_end);
463
- directive_spans_reverse(tail);
464
- if (complete && carry.prefix) directive_mark_spans(state, input->buffer, prefix);
465
- }
466
-
467
- static void directive_spans_reverse(DirectiveSpans *spans) {
468
- for (uint32_t i = 0; i < spans->count / 2; i++) {
469
- DirectiveSpan span = spans->items[i];
470
- spans->items[i] = spans->items[spans->count - 1 - i];
471
- spans->items[spans->count - 1 - i] = span;
503
+ uint32_t span_width = width < unit_bytes ? width : unit_bytes;
504
+ directive_carry(state, input->buffer, &carry, c, byte, span_width ? span_width : 1u);
505
+ if (carry.tail) directive_open_span_extend(&carry.tail_span, tail, byte, width);
506
+ else if (directive_prefixes_done(state, input->buffer, &carry)) break;
472
507
  }
508
+ directive_open_span_flush(&carry.line_end_span, &state->line_ends);
509
+ directive_spans_reverse_from(&state->line_ends, line_ends_base);
510
+ directive_open_span_flush(&carry.tail_span, tail);
511
+ directive_spans_reverse_from(tail, 0);
512
+ state->barriers.count = 0;
513
+ if (complete && carry.prefix) directive_mark_spans(state, input->buffer, prefix, &state->starts);
473
514
  }
474
515
 
475
- static void directive_mark_spans(EmptyDirectives *state, DirectiveBuffer *buffer, const DirectiveSpans *spans) {
476
- for (uint32_t i = 0; i < spans->count; i++) {
477
- for (uint32_t b = spans->items[i].start; b < spans->items[i].end; b++) directive_set(state, buffer, &state->starts, b);
478
- }
516
+ static bool directive_prefixes_done(const EmptyDirectives *state, const DirectiveBuffer *buffer, const DirectiveCarry *carry) {
517
+ return !buffer->carries || (!carry->prefix && !carry->line_end && !state->barriers.count && !state->starts);
479
518
  }
480
519
 
481
- // Moves back to the character before `*index`, reading a surrogate pair as one character, and decodes it into `*c` and
482
- // its byte width into `*width`; false when its width exceeds the bytes left.
483
- static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width) {
484
- const DirectiveBuffer *buffer = input->buffer;
485
- uint32_t i = --*index;
486
- if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
487
- directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) i = --*index;
488
- *width = directive_expected_width(input, i, c);
489
- if (*position && buffer->positions[*position - 1].index == i) {
490
- DirectivePosition recorded = buffer->positions[--*position];
491
- *byte = recorded.byte;
492
- *width = recorded.width;
493
- return true;
520
+ static void directive_spans_reverse_from(DirectiveSpans *spans, uint32_t from) {
521
+ uint32_t length = spans->count - from;
522
+ for (uint32_t i = 0; i < length / 2; i++) {
523
+ DirectiveSpan span = spans->items[from + i];
524
+ spans->items[from + i] = spans->items[spans->count - 1 - i];
525
+ spans->items[spans->count - 1 - i] = span;
494
526
  }
495
- if (*width > *byte) return false;
496
- *byte -= *width;
497
- return true;
498
527
  }
499
528
 
500
529
  // Carries null-directive prefixes back from their `#` and line ends back from their barriers over the character `c` at
501
530
  // `byte`, and whether the trailing run continues.
502
- static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte) {
531
+ static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte, uint32_t span_width) {
532
+ while (state->barriers.count && state->barriers.items[state->barriers.count - 1].byte > byte) state->barriers.count--;
533
+ bool condition_end = state->barriers.count && state->barriers.items[state->barriers.count - 1].byte == byte;
534
+ if (condition_end) state->barriers.count--;
503
535
  bool hash = c == '#' && directive_bit(state, state->starts, byte);
504
- bool condition_end = directive_bit(state, state->line_ends, byte);
505
536
  bool space = pragma_space(c) || c == '\\';
506
537
  carry->line_end = condition_end || (carry->line_end && directive_horizontal(c));
507
- if (carry->line_end) directive_set(state, buffer, &state->line_ends, byte);
538
+ if (carry->line_end) directive_open_span_extend(&carry->line_end_span, &state->line_ends, byte, span_width);
539
+ else directive_open_span_flush(&carry->line_end_span, &state->line_ends);
508
540
  carry->prefix = !condition_end && (hash || (carry->prefix && space));
509
541
  if (carry->prefix) directive_set(state, buffer, &state->starts, byte);
510
542
  carry->tail = carry->tail && !condition_end && space;
511
543
  }
512
544
 
513
- // Extends the trailing run back over the character at `byte`, starting a span where an included range left bytes out.
514
- static void directive_carry_tail(DirectiveCarry *carry, DirectiveSpans *tail, uint32_t byte, uint32_t width) {
545
+ // Extends `span` back over the character at `byte`, starting a new span in `spans` where bytes were left out.
546
+ static void directive_open_span_extend(DirectiveOpenSpan *span, DirectiveSpans *spans, uint32_t byte, uint32_t width) {
515
547
  if (width > DIRECTIVE_CHARACTER_BYTES) width = DIRECTIVE_CHARACTER_BYTES;
516
- if (carry->tail_open && byte + width == carry->tail_start) {
517
- carry->tail_start = byte;
548
+ if (span->open && byte + width == span->start) {
549
+ span->start = byte;
518
550
  return;
519
551
  }
520
- if (carry->tail_open) directive_spans_push(tail, carry->tail_start, carry->tail_end);
521
- carry->tail_start = byte;
522
- carry->tail_end = byte + width;
523
- carry->tail_open = true;
552
+ directive_open_span_flush(span, spans);
553
+ span->start = byte;
554
+ span->end = byte + width;
555
+ span->open = true;
556
+ }
557
+
558
+ static void directive_open_span_flush(DirectiveOpenSpan *span, DirectiveSpans *spans) {
559
+ if (!span->open) return;
560
+ directive_spans_push(spans, span->start, span->end);
561
+ span->open = false;
524
562
  }
525
563
 
526
564
  // Steps back from the character before `end` over characters that cannot change the reverse pass's state while no
527
565
  // prefix or line end is carried: plain-width characters that are neither `#` nor barriers. Returns the new end.
528
- static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte) {
566
+ // code-gauge-allow functionCognitiveComplexity -- Jumps directly to the next barrier when no null directives exist and steps over inert units otherwise.
567
+ static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t *position, uint32_t *byte) {
529
568
  const DirectiveBuffer *buffer = input->buffer;
530
- uint32_t stop = position ? buffer->positions[position - 1].index + 1 : 0, ascii_width = buffer->utf16 ? 2 : 1;
569
+ if (!state->starts) {
570
+ if (!state->barriers.count) return 0;
571
+ DirectiveBarrier target = state->barriers.items[state->barriers.count - 1];
572
+ if (target.index + 1 < end) {
573
+ *byte = target.end_byte;
574
+ while (*position && buffer->positions[*position - 1].index > target.index) (*position)--;
575
+ return target.index + 1;
576
+ }
577
+ return end;
578
+ }
579
+ uint32_t stop = *position ? buffer->positions[*position - 1].index + 1 : 0;
580
+ bool has_barrier = state->barriers.count > 0;
581
+ uint32_t barrier_byte = has_barrier ? state->barriers.items[state->barriers.count - 1].byte : 0;
531
582
  while (end > stop) {
532
- uint32_t c = directive_character(input, end - 1);
533
- uint32_t width = c < 0x80 ? ascii_width : buffer->utf16 && (c < 0xd800 || c > 0xdfff) ? 2 : 0;
534
- if (!width || c == '#' || width > *byte) break;
583
+ uint32_t width = directive_inert_width(directive_character(input, end - 1), buffer->utf16);
584
+ if (!width || width > *byte) break;
535
585
  uint32_t previous = *byte - width;
536
- if (directive_bit(state, state->line_ends, previous)) break;
586
+ if (has_barrier && previous <= barrier_byte) break;
537
587
  *byte = previous;
538
588
  end--;
539
589
  }
540
590
  return end;
541
591
  }
542
592
 
543
- // Reads characters from the lexer until `index` is stored; false at the end of the input.
544
- static inline bool directive_has(const DirectiveInput *input, uint32_t index) {
545
- return index < input->buffer->length || directive_fill(input->buffer, index);
593
+ static uint32_t directive_inert_width(uint32_t c, bool utf16) {
594
+ if (c == '#') return 0;
595
+ if (c < 0x80) return utf16 ? 2 : 1;
596
+ return utf16 && (c < 0xd800 || c > 0xdfff) ? 2 : 0;
546
597
  }
547
598
 
548
- // Reads ahead to the end of the line or `DIRECTIVE_FILL_UNITS` characters past `index`, since the pass reads each
549
- // stored character several times and the characters it leaves behind go back to `DirectiveScan`.
550
- static bool directive_fill(DirectiveBuffer *buffer, uint32_t index) {
551
- uint32_t limit = index + DIRECTIVE_FILL_UNITS;
552
- while (buffer->length < limit && !buffer->failed && !pragma_eof(buffer->lexer)) {
553
- int32_t c = buffer->lexer->lookahead;
554
- if (!directive_append(buffer)) buffer->failed = true;
555
- else if (directive_newline(c) && buffer->length > index) break;
599
+ static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
600
+ if (c == '\n') {
601
+ directive_line_end(state, input, line);
602
+ return;
556
603
  }
557
- return index < buffer->length;
558
- }
559
-
560
- static bool directive_append(DirectiveBuffer *buffer) {
561
- TSLexer *lexer = buffer->lexer;
562
- uint32_t c = lexer->lookahead, index = buffer->length, start = buffer->fill_byte;
563
- if (c < 0x80 && buffer->encoding_known && (index >> DIRECTIVE_PAGE_SHIFT) < buffer->page_count) {
564
- return directive_append_ascii(buffer, (uint16_t)c);
604
+ if (c == '/' && (directive_peek(input) == '/' || directive_peek(input) == '*')) {
605
+ if (line->pending && !line->comment_start) {
606
+ line->comment_start = input->previous_byte;
607
+ input->spaced_splice = false;
608
+ }
609
+ directive_line_comment(state, input, line);
610
+ return;
565
611
  }
566
- unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
567
- if (!directive_reserve(buffer, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
568
- directive_encode(buffer, c);
569
- directive_note(buffer, c);
570
- TSLexerPosition end;
571
- lexer->advance(lexer, false);
572
- lexer->get_position(lexer, &end);
573
- buffer->fill_byte = end.bytes;
574
- uint32_t measured = end.bytes - start;
575
- // A character stored before the encoding is known keeps its measured width, which a later choice cannot change.
576
- bool known = buffer->encoding_known;
577
- if (!known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
578
- buffer->utf16 = measured == utf16_width;
579
- buffer->encoding_known = true;
612
+ if (directive_horizontal(c) || (line->beginning && pragma_space(c))) return;
613
+ if (c == '#' && line->beginning) {
614
+ directive_start_hash(state, input, line);
615
+ return;
580
616
  }
581
- if (!known || c > 0x10ffff || measured != (buffer->utf16 ? utf16_width : width)) {
582
- return directive_position(buffer, index, start, measured);
617
+ if (directive_neutral(line)) {
618
+ directive_neutral_code(state, input, line, c);
619
+ return;
583
620
  }
584
- return true;
585
- }
586
-
587
- static bool directive_position(DirectiveBuffer *buffer, uint32_t index, uint32_t byte, uint32_t width) {
588
- if (buffer->position_count == buffer->position_capacity) {
589
- uint32_t capacity = buffer->position_capacity ? buffer->position_capacity * 2 : 16;
590
- if (capacity < buffer->position_capacity || (size_t)capacity > SIZE_MAX / sizeof(*buffer->positions)) return false;
591
- void *positions = realloc(buffer->positions, (size_t)capacity * sizeof(*buffer->positions));
592
- if (!positions) return false;
593
- buffer->positions = positions;
594
- buffer->position_capacity = capacity;
621
+ if (!line->pending && !line->pragma_depth && !line->pragma_pending && !line->header &&
622
+ line->kind == DIRECTIVE_CONDITION && directive_skip_plain_line(input, c, true)) {
623
+ line->operand = true;
624
+ line->beginning = false;
625
+ return;
595
626
  }
596
- buffer->positions[buffer->position_count++] = (DirectivePosition){index, byte, width};
597
- return true;
627
+ directive_code(state, input, line, c);
628
+ }
629
+
630
+ static void directive_start_hash(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line) {
631
+ line->pragma_pending = line->pragma_newline = false;
632
+ line->pragma_depth = 0;
633
+ line->kind = DIRECTIVE_ORDINARY;
634
+ line->operand = false;
635
+ line->start = input->previous_byte;
636
+ line->comment_start = 0;
637
+ directive_offset_push(&state->directive_starts, input->buffer, line->start);
638
+ if (directive_try_plain_directive(state, input, line)) return;
639
+ line->beginning = false;
640
+ line->pending = true;
598
641
  }
599
642
 
600
- // An ASCII character on a stored page, once the encoding is known, takes the expected width unless the lexer skipped to
601
- // another included range after it.
602
- static bool directive_append_ascii(DirectiveBuffer *buffer, uint16_t c) {
603
- TSLexer *lexer = buffer->lexer;
604
- uint32_t index = buffer->length, start = buffer->fill_byte;
605
- buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)] = c;
606
- buffer->length = index + 1;
607
- directive_note(buffer, c);
608
- TSLexerPosition end;
609
- lexer->advance(lexer, false);
610
- lexer->get_position(lexer, &end);
611
- buffer->fill_byte = end.bytes;
612
- return end.bytes - start == (buffer->utf16 ? 2u : 1u) || directive_position(buffer, index, start, end.bytes - start);
613
- }
614
-
615
- static inline void directive_note(DirectiveBuffer *buffer, uint32_t c) {
616
- buffer->recent = buffer->recent << 8 | (c < 0x80 ? c : 0);
617
- if (directive_pragma_word(buffer->recent)) buffer->pragma = true;
618
- }
619
-
620
- static bool directive_reserve(DirectiveBuffer *buffer, unsigned width) {
621
- if (buffer->length > UINT32_MAX - width) return false;
622
- uint32_t last = (buffer->length + width - 1) >> DIRECTIVE_PAGE_SHIFT;
623
- if (last >= buffer->page_capacity) {
624
- uint32_t next = buffer->page_capacity ? buffer->page_capacity * 2 : 16;
625
- if (next < buffer->page_capacity || (size_t)next > SIZE_MAX / sizeof(*buffer->characters)) return false;
626
- void *characters = realloc(buffer->characters, (size_t)next * sizeof(*buffer->characters));
627
- if (!characters) return false;
628
- buffer->characters = characters;
629
- buffer->page_capacity = next;
630
- }
631
- while (buffer->page_count <= last) {
632
- uint16_t *page = malloc((size_t)DIRECTIVE_PAGE_SIZE * sizeof(*page));
633
- if (!page) return false;
634
- buffer->characters[buffer->page_count++] = page;
635
- }
643
+ // Fast-paths an entire single-line ASCII directive (`#define`, `#include`, `#if`, `#ifdef`, `#endif`, etc.) that
644
+ // contains no block comments, non-header quotes, splices, or `_Pragma` tokens.
645
+ // code-gauge-allow functionCognitiveComplexity -- Validates keyword, optional header path, and plain tail in one pass.
646
+ // code-gauge-allow functionNcss -- Keeps the single-page fast path self-contained.
647
+ static bool directive_try_plain_directive(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line) {
648
+ DirectiveBuffer *buffer = input->buffer;
649
+ if (!buffer->encoding_known) return false;
650
+ uint32_t index = input->index, end = directive_plain_line_end(input);
651
+ if (index >= end) return false;
652
+ const uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT];
653
+ uint32_t mask = DIRECTIVE_PAGE_SIZE - 1, i = index;
654
+ while (i < end && directive_horizontal(page[i & mask])) i++;
655
+ if (i >= end || !empty_directives_name_character(page[i & mask], false)) return false;
656
+ DirectiveWord word = {0};
657
+ while (i < end && empty_directives_name_character(page[i & mask], true)) directive_word_character(&word, page[i++ & mask]);
658
+ if (i >= end || word.length >= sizeof word.directive) return false;
659
+ uint16_t sep = page[i & mask];
660
+ if (sep >= 0x80 || sep == '\\' || preproc_word(sep, true)) return false;
661
+ DirectiveKind kind = directive_word_kind(&word);
662
+ uint32_t kw_end_byte = input->byte + (i - index) * (buffer->utf16 ? 2u : 1u);
663
+ bool has_operand = false;
664
+ if (kind == DIRECTIVE_HEADER && !directive_scan_plain_header(page, &i, end, &has_operand)) return false;
665
+ uint32_t nl = 0;
666
+ if (!directive_scan_plain_tail(page, i, end, kind, &nl, &has_operand)) return false;
667
+ directive_record_plain_directive(state, input, line, &word, kind, kw_end_byte, nl, has_operand);
636
668
  return true;
637
669
  }
638
670
 
639
- // Only the two units of a character above U+FFFF form a surrogate pair. A decoding error is stored as U+FFFD with the
640
- // recorded position `directive_append` gives it, and a lone surrogate as U+E000, which `preproc_word` and `pragma_space`
641
- // classify alike, so that neither joins a neighbouring lone surrogate into a character the lexer did not read.
642
- static void directive_encode(DirectiveBuffer *buffer, uint32_t c) {
643
- if (c > 0x10ffff) directive_store(buffer, 0xfffd);
644
- else if (c >= 0xd800 && c <= 0xdfff) directive_store(buffer, 0xe000);
645
- else if (c <= 0xffff) directive_store(buffer, c);
646
- else {
647
- c -= 0x10000;
648
- directive_store(buffer, 0xd800 | (c >> 10));
649
- directive_store(buffer, 0xdc00 | (c & 0x3ff));
650
- }
651
- }
652
-
653
- static uint16_t directive_character(const DirectiveInput *input, uint32_t index) {
654
- return input->buffer->characters[index >> DIRECTIVE_PAGE_SHIFT][index & (DIRECTIVE_PAGE_SIZE - 1)];
655
- }
656
-
657
- static void directive_store(DirectiveBuffer *buffer, uint16_t c) {
658
- buffer->characters[buffer->length >> DIRECTIVE_PAGE_SHIFT][buffer->length & (DIRECTIVE_PAGE_SIZE - 1)] = c;
659
- buffer->length++;
660
- }
661
-
662
- static void directive_release(DirectiveBuffer *buffer) {
663
- for (uint32_t i = 0; i < buffer->page_count; i++) free(buffer->characters[i]);
664
- free(buffer->characters);
665
- free(buffer->positions);
666
- }
667
-
668
- static unsigned directive_utf8_width(uint32_t c) {
669
- return c < 0x80 || c > 0x10ffff ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
671
+ // code-gauge-allow functionParameterCount -- Commits the parsed keyword, depth, advance, and barrier of one plain directive line.
672
+ static void directive_record_plain_directive(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, DirectiveKind kind, uint32_t kw_end_byte, uint32_t nl, bool has_operand) {
673
+ DirectiveBuffer *buffer = input->buffer;
674
+ bool ignored = buffer->conditional_depth > 0;
675
+ if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "ifdef") == 0 || strcmp(word->directive, "ifndef") == 0) buffer->conditional_depth++;
676
+ else if (strcmp(word->directive, "endif") == 0 && buffer->conditional_depth) buffer->conditional_depth--;
677
+ if (!directive_requires_operand(kind) || ignored || has_operand) directive_offset_push(&state->directive_ends, buffer, kw_end_byte);
678
+ directive_advance_plain(input, nl - input->index);
679
+ directive_next(input);
680
+ line->beginning = true;
681
+ line->pending = false;
682
+ if (kind == DIRECTIVE_CONDITION || (directive_requires_operand(kind) && !has_operand)) directive_barrier(state, input);
670
683
  }
671
684
 
672
- static void directive_set(EmptyDirectives *state, DirectiveBuffer *buffer, uint8_t ***facts, uint32_t byte) {
673
- if (buffer->failed) return;
674
- uint32_t page = byte >> DIRECTIVE_FACT_PAGE_SHIFT;
675
- if (page >= state->pages && !directive_grow_directories(state, state->pages * 2 > page ? state->pages * 2 : page + 1)) {
676
- buffer->failed = true;
677
- return;
685
+ static bool directive_scan_plain_header(const uint16_t *page, uint32_t *index_io, uint32_t end, bool *has_operand) {
686
+ uint32_t mask = DIRECTIVE_PAGE_SIZE - 1, i = *index_io;
687
+ while (i < end && directive_horizontal(page[i & mask])) i++;
688
+ if (i >= end || (page[i & mask] != '<' && page[i & mask] != '"')) return false;
689
+ uint16_t close = page[i & mask] == '<' ? '>' : '"';
690
+ for (i++; i < end && page[i & mask] != close; i++) {
691
+ uint16_t c = page[i & mask];
692
+ if (c >= 0x80 || c == '\\' || directive_newline(c)) return false;
678
693
  }
679
- if (!*facts && !(*facts = calloc(state->pages, sizeof **facts))) {
680
- buffer->failed = true;
681
- return;
682
- }
683
- uint8_t **bits = &(*facts)[page];
684
- if (!*bits && !(*bits = calloc(DIRECTIVE_FACT_PAGE_SIZE / 8, 1))) {
685
- buffer->failed = true;
686
- return;
687
- }
688
- uint32_t offset = byte & (DIRECTIVE_FACT_PAGE_SIZE - 1);
689
- (*bits)[offset / 8] |= 1u << (offset % 8);
690
- }
691
-
692
- static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte) {
693
- uint32_t page = byte >> DIRECTIVE_FACT_PAGE_SHIFT, offset = byte & (DIRECTIVE_FACT_PAGE_SIZE - 1);
694
- return facts && page < state->pages && facts[page] && (facts[page][offset / 8] & (1u << (offset % 8)));
694
+ if (i >= end) return false;
695
+ *index_io = i + 1;
696
+ *has_operand = true;
697
+ return true;
695
698
  }
696
699
 
697
- static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages) {
698
- uint8_t ***directories[] = {&state->starts, &state->ends, &state->comments, &state->line_ends, &state->directive_starts, &state->directive_ends};
699
- if ((size_t)pages > SIZE_MAX / sizeof(uint8_t *)) return false;
700
- for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
701
- if (!*directories[i]) continue;
702
- uint8_t **directory = realloc(*directories[i], (size_t)pages * sizeof *directory);
703
- if (!directory) return false;
704
- memset(directory + state->pages, 0, (size_t)(pages - state->pages) * sizeof *directory);
705
- *directories[i] = directory;
700
+ // code-gauge-allow functionCognitiveComplexity -- Validates plain-tail exclusions and trailing `//` comment in one pass.
701
+ static bool directive_scan_plain_tail(const uint16_t *page, uint32_t i, uint32_t end, DirectiveKind kind, uint32_t *nl_out, bool *has_operand) {
702
+ uint32_t mask = DIRECTIVE_PAGE_SIZE - 1;
703
+ bool after_lparen = false;
704
+ uint16_t prev = 0;
705
+ for (; i < end; i++) {
706
+ uint16_t c = page[i & mask];
707
+ if (directive_newline(c)) {
708
+ *nl_out = i;
709
+ return true;
710
+ }
711
+ if (c == '/') return directive_scan_plain_comment(page, i, end, nl_out);
712
+ if (c >= 0x80 || c == '"' || c == '\'' || c == '\\') return false;
713
+ if (prev == '_' && c == 'P' && (i + 1 >= end || page[(i + 1) & mask] == 'r')) return false;
714
+ if (kind == DIRECTIVE_CONDITION && c == '<' && after_lparen) return false;
715
+ if (!directive_horizontal(c)) {
716
+ *has_operand = true;
717
+ after_lparen = c == '(';
718
+ }
719
+ prev = c;
706
720
  }
707
- state->pages = pages;
708
- return true;
721
+ return false;
709
722
  }
710
723
 
711
- static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
712
- if (c == '\n') {
713
- directive_line_end(state, input, line);
714
- return;
715
- }
716
- if (c == '/' && (directive_peek(input) == '/' || directive_peek(input) == '*')) {
717
- if (line->pending && !line->comment_start) {
718
- line->comment_start = input->previous_byte;
719
- input->spaced_splice = false;
724
+ static bool directive_scan_plain_comment(const uint16_t *page, uint32_t i, uint32_t end, uint32_t *nl_out) {
725
+ uint32_t mask = DIRECTIVE_PAGE_SIZE - 1;
726
+ if (i + 1 >= end || page[(i + 1) & mask] != '/') return false;
727
+ for (i += 2; i < end; i++) {
728
+ uint16_t c = page[i & mask];
729
+ if (directive_newline(c)) {
730
+ *nl_out = i;
731
+ return true;
720
732
  }
721
- directive_line_comment(state, input, line);
722
- return;
733
+ if (c >= 0x80 || c == '\\') return false;
723
734
  }
724
- if (directive_horizontal(c) || (line->beginning && pragma_space(c))) return;
725
- if (c == '#' && line->beginning) {
726
- line->pragma_pending = line->pragma_newline = false;
727
- line->pragma_depth = 0;
728
- line->kind = DIRECTIVE_ORDINARY;
729
- line->operand = false;
730
- line->start = input->previous_byte;
731
- line->comment_start = 0;
732
- directive_set(state, input->buffer, &state->directive_starts, line->start);
733
- line->beginning = false;
734
- line->pending = true;
735
- return;
736
- }
737
- if (directive_neutral(line)) directive_neutral_code(state, input, line, c);
738
- else directive_code(state, input, line, c);
735
+ return false;
739
736
  }
740
737
 
741
738
  // code-gauge-allow functionCognitiveComplexity -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
@@ -763,30 +760,92 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
763
760
  }
764
761
 
765
762
  // Outside a directive name, a line with no `_Pragma` or header operand in progress keeps its state through every token
766
- // when it is of no directive kind, or of a kind that only waits for an operand it already has: its words need no
767
- // classification.
763
+ // when it is of no directive kind, or of a kind that only waits for an operand it already has.
768
764
  static bool directive_neutral(const DirectiveLine *line) {
769
- bool settled = line->kind == DIRECTIVE_ORDINARY || (line->operand && directive_requires_operand(line->kind));
770
- return settled && !line->pending && !line->pragma_depth && !line->pragma_pending && !line->header;
765
+ if (line->pending || line->pragma_depth || line->pragma_pending || line->header) return false;
766
+ return line->kind == DIRECTIVE_ORDINARY || (line->operand && directive_requires_operand(line->kind));
771
767
  }
772
768
 
773
- // Also consumes the following characters that leave the line state as it is, which is every plain character but `/`.
769
+ // Advances to the buffered newline when the rest of the line is plain ASCII with no quote, splice, block comment, or
770
+ // condition-changing `<` or `_Pragma`.
771
+ // code-gauge-allow functionCognitiveComplexity -- Checks all plain-line exclusions in one pass to the buffered newline.
772
+ static bool directive_skip_plain_line(DirectiveInput *input, int32_t first, bool condition) {
773
+ if ((uint32_t)first >= 0x80u || first == '"' || first == '\'' || first == '\\' || (condition && first == '<')) return false;
774
+ uint32_t index = input->index, end = directive_plain_line_end(input);
775
+ if (index >= end) return false;
776
+ const DirectiveBuffer *buffer = input->buffer;
777
+ const uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT];
778
+ if (first == '/' && page[index & (DIRECTIVE_PAGE_SIZE - 1)] == '*') return false;
779
+ bool after_lparen = first == '(';
780
+ uint16_t prev = (uint16_t)first;
781
+ for (uint32_t i = index; i < end; i++) {
782
+ uint16_t c = page[i & (DIRECTIVE_PAGE_SIZE - 1)];
783
+ if (directive_newline(c)) {
784
+ if (i > index) directive_advance_plain(input, i - index);
785
+ return true;
786
+ }
787
+ if (c >= 0x80 || c == '"' || c == '\'' || c == '\\') return false;
788
+ if (c == '/' && (i + 1 >= end || page[(i + 1) & (DIRECTIVE_PAGE_SIZE - 1)] == '*')) return false;
789
+ if (condition && !directive_plain_condition_step(page, i, end, &after_lparen, &prev)) return false;
790
+ }
791
+ return false;
792
+ }
793
+
794
+ static uint32_t directive_plain_line_end(const DirectiveInput *input) {
795
+ if (!directive_has(input, input->index)) return input->index;
796
+ const DirectiveBuffer *buffer = input->buffer;
797
+ uint32_t index = input->index, end = buffer->length;
798
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index < end) {
799
+ end = buffer->positions[input->position_index].index;
800
+ }
801
+ uint32_t page_end = (index | (DIRECTIVE_PAGE_SIZE - 1)) + 1;
802
+ return page_end < end ? page_end : end;
803
+ }
804
+
805
+ static bool directive_plain_condition_step(const uint16_t *page, uint32_t i, uint32_t end, bool *after_lparen, uint16_t *prev) {
806
+ uint32_t mask = DIRECTIVE_PAGE_SIZE - 1;
807
+ uint16_t c = page[i & mask];
808
+ if (c == '<' && *after_lparen) return false;
809
+ if (*prev == '_' && c == 'P' && (i + 1 >= end || page[(i + 1) & mask] == 'r')) return false;
810
+ if (!directive_horizontal(c)) *after_lparen = c == '(';
811
+ *prev = c;
812
+ return true;
813
+ }
814
+
815
+ // Also consumes the stored characters after the token that leave the line state as it is.
774
816
  static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
775
817
  line->operand = true;
776
818
  line->beginning = false;
819
+ if (directive_skip_plain_line(input, c, false)) return;
820
+ if (c == '"' || c == '\'') {
821
+ if (directive_quoted(input, c)) {
822
+ directive_line_end(state, input, line);
823
+ return;
824
+ }
825
+ } else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
826
+ directive_word(input, c, true, false);
827
+ }
828
+ directive_neutral_units(input);
829
+ }
830
+
831
+ // Consumes the tokens stored after `input` up to the line feed or to the first one that needs more than its units, as
832
+ // `directive_line_character` would on a neutral line: `directive_scan_units` reads a line with no directive state, and
833
+ // a neutral line keeps its state through every token.
834
+ static void directive_neutral_units(DirectiveInput *input) {
835
+ const DirectiveBuffer *buffer = input->buffer;
777
836
  for (;;) {
778
- if (c == '"' || c == '\'') {
779
- if (directive_quoted(input, c)) {
780
- directive_line_end(state, input, line);
781
- return;
782
- }
783
- } else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
784
- directive_word(input, c, true, false);
837
+ uint32_t index = input->index, end = buffer->length;
838
+ if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index < end) {
839
+ end = buffer->positions[input->position_index].index;
785
840
  }
786
- directive_plain_run(input, DIRECTIVE_RUN_CODE);
787
- c = directive_plain(input);
788
- if (c < 0 || c == '/') return;
789
- directive_skip_plain(input);
841
+ uint32_t page_end = (index | (DIRECTIVE_PAGE_SIZE - 1)) + 1;
842
+ if (page_end < end) end = page_end;
843
+ if (index >= end) return;
844
+ DirectiveScan scan = {.classes = buffer->classes};
845
+ const uint16_t *units = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT] + (index & (DIRECTIVE_PAGE_SIZE - 1));
846
+ uint32_t length = directive_scan_units(&scan, (const char *)units, end - index, 2, 0, false).consumed;
847
+ if (length) directive_advance_plain(input, length);
848
+ if (length < end - index) return;
790
849
  }
791
850
  }
792
851
 
@@ -872,7 +931,7 @@ static void directive_complete_required(EmptyDirectives *state, DirectiveInput *
872
931
  line->pragma_name = false;
873
932
  if (!line->required_end) return;
874
933
  if (line->operand || line->pragma_pending || pragma_name) {
875
- directive_set(state, input->buffer, &state->directive_ends, line->required_end);
934
+ directive_offset_push(&state->directive_ends, input->buffer, line->required_end);
876
935
  }
877
936
  line->required_end = 0;
878
937
  }
@@ -884,162 +943,13 @@ static bool directive_requires_operand(DirectiveKind kind) {
884
943
  static void directive_barrier(EmptyDirectives *state, DirectiveInput *input) {
885
944
  if (!input->index || !directive_newline(directive_character(input, input->index - 1))) return;
886
945
  input->buffer->carries = true;
887
- directive_set(state, input->buffer, &state->line_ends, input->previous_byte);
888
- if (input->index < 2) return;
889
- int32_t previous = directive_character(input, input->index - 2);
890
- if (directive_newline(previous) && previous != directive_character(input, input->index - 1)) {
891
- directive_set(state, input->buffer, &state->line_ends, input->before_previous_byte);
892
- }
893
- }
894
-
895
- static int32_t directive_next(DirectiveInput *input) {
896
- int32_t plain = directive_plain(input);
897
- if (plain >= 0) {
898
- directive_skip_plain(input);
899
- return plain;
900
- }
901
- while (directive_has(input, input->index)) {
902
- int32_t c = directive_physical_next(input);
903
- if (c == '\\' && directive_skip_splice(input)) continue;
904
- if (directive_newline(c)) {
905
- if (directive_has(input, input->index) && directive_newline(directive_character(input, input->index)) &&
906
- directive_character(input, input->index) != c) directive_physical_next(input);
907
- return '\n';
908
- }
909
- return c;
910
- }
911
- return -1;
912
- }
913
-
914
- static int32_t directive_physical_next(DirectiveInput *input) {
915
- const DirectiveBuffer *buffer = input->buffer;
916
- uint32_t index = input->index;
917
- int32_t c;
918
- uint32_t width = directive_expected_width(input, index, &c);
919
- input->index += c > 0xffff ? 2 : 1;
920
- input->before_previous_byte = input->previous_byte;
921
- input->previous_byte = input->byte;
922
- if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index == index) {
923
- DirectivePosition position = buffer->positions[input->position_index++];
924
- input->previous_byte = position.byte;
925
- input->byte = position.byte + position.width;
926
- } else input->byte += width;
927
- return c;
928
- }
929
-
930
- // Decodes the character at `index` into `*c` and returns its byte width when it has no recorded position.
931
- static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c) {
932
- uint32_t unit = directive_character(input, index);
933
- if (unit >= 0xd800 && unit <= 0xdbff && directive_has(input, index + 1)) {
934
- uint32_t low = directive_character(input, index + 1);
935
- if (low >= 0xdc00 && low <= 0xdfff) {
936
- *c = (int32_t)(0x10000 + ((unit - 0xd800) << 10) + (low - 0xdc00));
937
- return 4;
938
- }
939
- }
940
- *c = (int32_t)unit;
941
- return input->buffer->utf16 ? 2 : directive_utf8_width(unit);
942
- }
943
-
944
- // Consumes the rest of a line splice after its `\\`; false when none follows.
945
- static bool directive_skip_splice(DirectiveInput *input) {
946
- uint32_t after = directive_splice_end(input);
947
- if (after == input->index) return false;
948
- if (directive_horizontal(directive_character(input, input->index))) input->spaced_splice = true;
949
- while (input->index < after) directive_physical_next(input);
950
- return true;
951
- }
952
-
953
- static uint32_t directive_splice_end(const DirectiveInput *input) {
954
- uint32_t after = input->index;
955
- while (directive_has(input, after) && directive_horizontal(directive_character(input, after))) after++;
956
- if (!directive_has(input, after) || !directive_newline(directive_character(input, after))) return input->index;
957
- int32_t newline = directive_character(input, after++);
958
- if (directive_has(input, after) && directive_newline(directive_character(input, after)) &&
959
- directive_character(input, after) != newline) after++;
960
- return after;
961
- }
962
-
963
- static int32_t directive_peek(const DirectiveInput *input) {
964
- int32_t plain = directive_plain(input);
965
- if (plain >= 0) return plain;
966
- DirectiveInput copy = *input;
967
- return directive_next(&copy);
968
- }
969
-
970
- // Consumes the longest run of plain characters that `run` accepts and returns its length.
971
- static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run) {
972
- const DirectiveBuffer *buffer = input->buffer;
973
- uint32_t index = input->index;
974
- // Scans the stored characters before the next recorded position and reads more only at their end, because reading
975
- // may store a character with a recorded position, which ends the run.
976
- while (directive_has(input, index)) {
977
- uint32_t end = buffer->length;
978
- if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index < end) {
979
- end = buffer->positions[input->position_index].index;
946
+ if (input->index >= 2) {
947
+ int32_t previous = directive_character(input, input->index - 2);
948
+ if (directive_newline(previous) && previous != directive_character(input, input->index - 1)) {
949
+ directive_barrier_push(&state->barriers, input->buffer, input->index - 2, input->before_previous_byte, input->previous_byte);
980
950
  }
981
- uint32_t start = index;
982
- index = directive_run_end(buffer, run, index, end);
983
- if (index < end || index == start || end < buffer->length) break;
984
951
  }
985
- uint32_t length = index - input->index, width = buffer->utf16 ? 2 : 1;
986
- if (!length) return 0;
987
- input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
988
- input->previous_byte = input->byte + (length - 1) * width;
989
- input->byte += length * width;
990
- input->index = index;
991
- return length;
992
- }
993
-
994
- // The index of the first stored character from `index` to `end` that ends the run, or `end`.
995
- static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end) {
996
- while (index < end) {
997
- const uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT];
998
- uint32_t page_end = (index | (DIRECTIVE_PAGE_SIZE - 1)) + 1;
999
- if (page_end > end) page_end = end;
1000
- for (; index < page_end; index++) {
1001
- uint16_t c = page[index & (DIRECTIVE_PAGE_SIZE - 1)];
1002
- if (c >= 0x80 || c == '\\' || directive_newline(c) || !directive_run_continues(run, c)) return index;
1003
- }
1004
- }
1005
- return end;
1006
- }
1007
-
1008
- // `c` is ASCII.
1009
- static bool directive_run_continues(DirectiveRun run, uint16_t c) {
1010
- bool identifier = (preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1;
1011
- switch (run) {
1012
- case DIRECTIVE_RUN_WORD:
1013
- return identifier;
1014
- case DIRECTIVE_RUN_CODE:
1015
- // The rest of a neutral line: characters that start no token.
1016
- return !identifier && c != '"' && c != '\'' && c != '/';
1017
- case DIRECTIVE_RUN_SPACE:
1018
- return directive_horizontal(c);
1019
- case DIRECTIVE_RUN_LINE_COMMENT:
1020
- return true;
1021
- case DIRECTIVE_RUN_BLOCK_COMMENT:
1022
- return c != '*';
1023
- }
1024
- return false;
1025
- }
1026
-
1027
- static void directive_skip_plain(DirectiveInput *input) {
1028
- input->index++;
1029
- input->before_previous_byte = input->previous_byte;
1030
- input->previous_byte = input->byte;
1031
- input->byte += input->buffer->utf16 ? 2 : 1;
1032
- }
1033
-
1034
- // The next character when it is ASCII, cannot start a line splice or a newline, and has its expected width; otherwise -1.
1035
- static int32_t directive_plain(const DirectiveInput *input) {
1036
- if (!directive_has(input, input->index)) return -1;
1037
- uint16_t c = directive_character(input, input->index);
1038
- if (c >= 0x80 || c == '\\' || directive_newline(c)) return -1;
1039
- const DirectiveBuffer *buffer = input->buffer;
1040
- bool positioned = input->position_index < buffer->position_count &&
1041
- buffer->positions[input->position_index].index == input->index;
1042
- return positioned ? -1 : c;
952
+ directive_barrier_push(&state->barriers, input->buffer, input->index - 1, input->previous_byte, input->byte);
1043
953
  }
1044
954
 
1045
955
  // `_Pragma` is the macro name of `#define _Pragma(x)` or `#ifdef _Pragma`, but an operator before a string literal.
@@ -1112,7 +1022,7 @@ static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *i
1112
1022
  DirectiveBuffer *buffer = input->buffer;
1113
1023
  // Directives in a group that may be skipped need no operand, and the parser cannot tell which groups are skipped.
1114
1024
  if (directive_requires_operand(kind) && !buffer->conditional_depth) line->required_end = end;
1115
- else directive_set(state, buffer, &state->directive_ends, end);
1025
+ else directive_offset_push(&state->directive_ends, buffer, end);
1116
1026
  if (word->length < sizeof word->directive) {
1117
1027
  if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "ifdef") == 0 || strcmp(word->directive, "ifndef") == 0) buffer->conditional_depth++;
1118
1028
  else if (strcmp(word->directive, "endif") == 0 && buffer->conditional_depth) buffer->conditional_depth--;
@@ -1146,22 +1056,35 @@ static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool
1146
1056
  for (uint32_t i = 0; i < length && i < significant; i++) directive_word_character(word, directive_character(input, start + i));
1147
1057
  }
1148
1058
 
1059
+ // code-gauge-allow functionCognitiveComplexity -- Dispatches fixed directive keywords by length instead of scanning every table entry.
1060
+ // code-gauge-allow functionNcss -- Length-bucketed dispatch keeps all fixed directive spellings in one switch.
1149
1061
  static DirectiveKind directive_word_kind(const DirectiveWord *word) {
1150
- if (word->length >= sizeof word->directive) return DIRECTIVE_ORDINARY;
1151
- if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "elif") == 0) return DIRECTIVE_CONDITION;
1152
- if (strcmp(word->directive, pragma_word) == 0) return DIRECTIVE_PRAGMA;
1153
- if (strcmp(word->directive, "include") == 0 || strcmp(word->directive, "include_next") == 0 ||
1154
- strcmp(word->directive, "embed") == 0) return DIRECTIVE_HEADER;
1155
- if (strcmp(word->directive, "warning") == 0 || strcmp(word->directive, "error") == 0) return DIRECTIVE_DIAGNOSTIC;
1156
- if (strcmp(word->directive, "undef") == 0) return DIRECTIVE_UNDEF;
1157
- if (strcmp(word->directive, "line") == 0) return DIRECTIVE_LINE;
1158
- static const char *const required[] = {
1159
- "define", "ifdef", "ifndef", "elifdef", "elifndef",
1160
- };
1161
- for (unsigned i = 0; i < sizeof required / sizeof *required; i++) {
1162
- if (strcmp(word->directive, required[i]) == 0) return DIRECTIVE_REQUIRED;
1062
+ switch (word->length) {
1063
+ case 2:
1064
+ return memcmp(word->directive, "if", 2) == 0 ? DIRECTIVE_CONDITION : DIRECTIVE_ORDINARY;
1065
+ case 4:
1066
+ if (memcmp(word->directive, "elif", 4) == 0) return DIRECTIVE_CONDITION;
1067
+ return memcmp(word->directive, "line", 4) == 0 ? DIRECTIVE_LINE : DIRECTIVE_ORDINARY;
1068
+ case 5:
1069
+ if (memcmp(word->directive, "ifdef", 5) == 0) return DIRECTIVE_REQUIRED;
1070
+ if (memcmp(word->directive, "undef", 5) == 0) return DIRECTIVE_UNDEF;
1071
+ if (memcmp(word->directive, "embed", 5) == 0) return DIRECTIVE_HEADER;
1072
+ return memcmp(word->directive, "error", 5) == 0 ? DIRECTIVE_DIAGNOSTIC : DIRECTIVE_ORDINARY;
1073
+ case 6:
1074
+ return memcmp(word->directive, "define", 6) == 0 || memcmp(word->directive, "ifndef", 6) == 0
1075
+ ? DIRECTIVE_REQUIRED : DIRECTIVE_ORDINARY;
1076
+ case 7:
1077
+ if (memcmp(word->directive, pragma_word, 7) == 0) return DIRECTIVE_PRAGMA;
1078
+ if (memcmp(word->directive, "include", 7) == 0) return DIRECTIVE_HEADER;
1079
+ if (memcmp(word->directive, "elifdef", 7) == 0) return DIRECTIVE_REQUIRED;
1080
+ return memcmp(word->directive, "warning", 7) == 0 ? DIRECTIVE_DIAGNOSTIC : DIRECTIVE_ORDINARY;
1081
+ case 8:
1082
+ return memcmp(word->directive, "elifndef", 8) == 0 ? DIRECTIVE_REQUIRED : DIRECTIVE_ORDINARY;
1083
+ case 12:
1084
+ return memcmp(word->directive, "include_next", 12) == 0 ? DIRECTIVE_HEADER : DIRECTIVE_ORDINARY;
1085
+ default:
1086
+ return DIRECTIVE_ORDINARY;
1163
1087
  }
1164
- return DIRECTIVE_ORDINARY;
1165
1088
  }
1166
1089
 
1167
1090
  static void directive_word_character(DirectiveWord *word, int32_t c) {
@@ -1198,10 +1121,6 @@ static bool directive_raw(DirectiveInput *input) {
1198
1121
  return true;
1199
1122
  }
1200
1123
 
1201
- static uint32_t directive_byte(const DirectiveInput *input) {
1202
- return input->byte;
1203
- }
1204
-
1205
1124
  // A null directive with comments has two ends: before its first comment, marked in `comments` too, and after its line.
1206
1125
  // It keeps its comments when a spaced splice follows their start, where the lexer would read them differently.
1207
1126
  static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end) {