@willbooster/tree-sitter-c 43.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.
- package/README.md +18 -15
- package/grammar.js +57 -21
- package/package.json +2 -1
- package/src/conditionalMacro.h +2 -7
- package/src/directiveBuffer.h +212 -0
- package/src/directiveFacts.h +363 -0
- package/src/directiveScan.h +13 -11
- package/src/directiveUnits.h +273 -94
- package/src/emptyDirectives.h +522 -450
- package/src/functionMacro.h +151 -16
- package/src/macroProof.h +1 -1
- package/src/pragma.h +129 -7
- package/src/scanner.c +252 -63
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +2 -1
- package/types/treeSitterDsl.d.ts +6 -0
package/src/emptyDirectives.h
CHANGED
|
@@ -1,32 +1,20 @@
|
|
|
1
|
-
// code-gauge-allow-file fileNcss -- One source-fact pass shares directive lexing,
|
|
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 "
|
|
6
|
-
#include <stdlib.h>
|
|
5
|
+
#include "directiveFacts.h"
|
|
7
6
|
|
|
8
|
-
#define DIRECTIVE_FACT_PAGE_SHIFT 15
|
|
9
|
-
#define DIRECTIVE_FACT_PAGE_SIZE (1 << DIRECTIVE_FACT_PAGE_SHIFT)
|
|
10
|
-
|
|
11
|
-
// Each kind of fact is a directory of `pages` pointers, one per `DIRECTIVE_FACT_PAGE_SIZE` source bytes up to and
|
|
12
|
-
// possibly past the furthest fact, to bitset pages allocated only where a fact is set, so a missing directory or page
|
|
13
|
-
// holds no fact; source without facts, such as an excluded range, costs one pointer per page of each directory.
|
|
14
|
-
// `empty_directives_release` frees them.
|
|
15
7
|
typedef struct {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// whether the facts, which `built` says exist, are in use. `pragmas` is the fact that the input read for them holds
|
|
20
|
-
// `pragma_word`, without which no scan has to look for a `_Pragma` operator.
|
|
21
|
-
bool built, anchored, ready, pragmas;
|
|
22
|
-
} EmptyDirectives;
|
|
8
|
+
uint32_t start, end;
|
|
9
|
+
bool open;
|
|
10
|
+
} DirectiveOpenSpan;
|
|
23
11
|
|
|
24
12
|
typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_UNDEF, DIRECTIVE_LINE, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
|
|
25
13
|
|
|
26
|
-
// The reverse pass's state
|
|
14
|
+
// The reverse pass's state over a segment's condition line ends and trailing trivia run.
|
|
27
15
|
typedef struct {
|
|
28
|
-
bool prefix, line_end, tail
|
|
29
|
-
|
|
16
|
+
bool prefix, line_end, tail;
|
|
17
|
+
DirectiveOpenSpan line_end_span, tail_span;
|
|
30
18
|
} DirectiveCarry;
|
|
31
19
|
|
|
32
20
|
typedef struct {
|
|
@@ -37,69 +25,80 @@ typedef struct {
|
|
|
37
25
|
uint32_t start, comment_start, required_end;
|
|
38
26
|
} DirectiveLine;
|
|
39
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
|
+
|
|
40
44
|
static bool directive_scan_buffered(DirectiveScan *scan, DirectiveInput *input);
|
|
41
|
-
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);
|
|
42
55
|
static void directive_scan_resume(DirectiveScan *scan, DirectiveSpans *run, DirectiveSpans *tail);
|
|
43
56
|
static void directive_scan_handoff(DirectiveScan *scan, uint32_t hash, bool skipped);
|
|
44
57
|
static bool directive_segment(EmptyDirectives *state, DirectiveInput *input);
|
|
45
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);
|
|
46
61
|
static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit);
|
|
47
62
|
static bool directive_line_clean(const DirectiveLine *line);
|
|
48
|
-
static void directive_shift(DirectiveBuffer *buffer, uint32_t start);
|
|
49
63
|
static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail);
|
|
50
64
|
static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail);
|
|
51
|
-
static bool
|
|
52
|
-
static void directive_carry(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveCarry *carry, int32_t c, uint32_t byte);
|
|
53
|
-
static void
|
|
54
|
-
static void
|
|
55
|
-
static void
|
|
56
|
-
static uint32_t directive_skip_inert(const EmptyDirectives *state, const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte);
|
|
57
|
-
static
|
|
58
|
-
static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte);
|
|
59
|
-
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);
|
|
60
72
|
static void directive_barrier(EmptyDirectives *state, DirectiveInput *input);
|
|
61
73
|
static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
|
|
62
74
|
static void directive_complete_required(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
|
|
63
75
|
static bool directive_requires_operand(DirectiveKind kind);
|
|
64
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);
|
|
65
83
|
static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
|
|
66
84
|
static bool directive_pragma_part(DirectiveLine *line, int32_t c);
|
|
67
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);
|
|
68
89
|
static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
|
|
69
90
|
static void directive_neutral_units(DirectiveInput *input);
|
|
70
|
-
static void directive_advance_plain(DirectiveInput *input, uint32_t length);
|
|
71
91
|
static bool directive_header_ready(DirectiveLine *line, int32_t c);
|
|
72
92
|
static bool directive_header(DirectiveInput *input, int32_t close);
|
|
73
93
|
static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
|
|
74
|
-
static bool directive_skip_splice(DirectiveInput *input);
|
|
75
|
-
static uint32_t directive_splice_end(const DirectiveInput *input);
|
|
76
94
|
|
|
77
95
|
static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
|
|
78
|
-
static void empty_directives_release(EmptyDirectives *state);
|
|
79
|
-
static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte);
|
|
80
|
-
static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte);
|
|
81
|
-
static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
|
|
82
|
-
static int32_t directive_next(DirectiveInput *input);
|
|
83
|
-
static int32_t directive_physical_next(DirectiveInput *input);
|
|
84
|
-
static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c);
|
|
85
|
-
static int32_t directive_peek(const DirectiveInput *input);
|
|
86
|
-
static int32_t directive_plain(const DirectiveInput *input);
|
|
87
|
-
static void directive_skip_plain(DirectiveInput *input);
|
|
88
|
-
typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
|
|
89
|
-
static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
|
|
90
|
-
static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end);
|
|
91
|
-
static bool directive_run_continues(DirectiveRun run, uint16_t c);
|
|
92
96
|
static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input);
|
|
93
97
|
static bool directive_pragma_operator(const DirectiveInput *input);
|
|
94
98
|
static int32_t directive_pragma_token(DirectiveInput *input);
|
|
95
99
|
static bool directive_comment(DirectiveInput *input, bool *line);
|
|
96
100
|
static bool directive_quoted(DirectiveInput *input, int32_t quote);
|
|
97
101
|
static bool directive_raw(DirectiveInput *input);
|
|
98
|
-
typedef struct {
|
|
99
|
-
GnuRawStringPrefix raw;
|
|
100
|
-
char directive[13];
|
|
101
|
-
unsigned length;
|
|
102
|
-
} DirectiveWord;
|
|
103
102
|
|
|
104
103
|
static uint32_t directive_keyword_end(const DirectiveInput *input);
|
|
105
104
|
static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end);
|
|
@@ -108,7 +107,6 @@ static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool
|
|
|
108
107
|
static DirectiveKind directive_word_kind(const DirectiveWord *word);
|
|
109
108
|
static void directive_word_character(DirectiveWord *word, int32_t c);
|
|
110
109
|
static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
|
|
111
|
-
static uint32_t directive_byte(const DirectiveInput *input);
|
|
112
110
|
static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end);
|
|
113
111
|
|
|
114
112
|
// Scans the zero-width token `symbol` that starts the directive context: once at the start of the input, where it
|
|
@@ -126,87 +124,6 @@ static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer,
|
|
|
126
124
|
return state->ready || !anchored;
|
|
127
125
|
}
|
|
128
126
|
|
|
129
|
-
static bool empty_directives_after_keyword(const EmptyDirectives *state, TSLexer *lexer) {
|
|
130
|
-
if (!state || !state->directive_ends) return false;
|
|
131
|
-
TSLexerPosition position;
|
|
132
|
-
lexer->get_position(lexer, &position);
|
|
133
|
-
return directive_bit(state, state->directive_ends, position.bytes);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// False only when the facts show that no `_Pragma` operator can follow any position.
|
|
137
|
-
static bool empty_directives_pragma_possible(const EmptyDirectives *state) {
|
|
138
|
-
return !state || !state->built || state->pragmas;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Whether none of the `count` tokens, at least eight, is valid but those whose byte in `extras` is 0xff.
|
|
142
|
-
static inline bool empty_directives_only_valid(const bool *valid_symbols, const uint8_t *extras, unsigned count) {
|
|
143
|
-
_Static_assert(sizeof(bool) == 1, "valid_symbols is read as bytes");
|
|
144
|
-
uint64_t others = 0;
|
|
145
|
-
for (unsigned i = 0; i < count; i += 8) {
|
|
146
|
-
unsigned offset = i + 8 > count ? count - 8 : i;
|
|
147
|
-
uint64_t valid, extra;
|
|
148
|
-
memcpy(&valid, valid_symbols + offset, sizeof valid);
|
|
149
|
-
memcpy(&extra, extras + offset, sizeof extra);
|
|
150
|
-
others |= valid & ~extra;
|
|
151
|
-
}
|
|
152
|
-
return !others;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
static bool empty_directives_at_start(const EmptyDirectives *state, TSLexer *lexer) {
|
|
156
|
-
if (!state || !state->directive_starts) return false;
|
|
157
|
-
TSLexerPosition position;
|
|
158
|
-
lexer->get_position(lexer, &position);
|
|
159
|
-
return directive_bit(state, state->directive_starts, position.bytes);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
static bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *lexer) {
|
|
163
|
-
if (!state || !state->line_ends || !(directive_horizontal(lexer->lookahead) || directive_newline(lexer->lookahead))) return false;
|
|
164
|
-
TSLexerPosition position;
|
|
165
|
-
lexer->get_position(lexer, &position);
|
|
166
|
-
return empty_directives_line_end_at(state, position.bytes);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
static bool empty_directives_line_end_at(const EmptyDirectives *state, uint32_t byte) {
|
|
170
|
-
return directive_bit(state, state->line_ends, byte);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
|
|
174
|
-
|
|
175
|
-
static bool empty_directives_skip(TSLexer *lexer, void *payload) {
|
|
176
|
-
return empty_directives_consume(lexer, payload, false);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// A null directive, including its prefix, starts only at `#`, `\\` or a space, so other characters cannot match `starts`.
|
|
180
|
-
static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
|
|
181
|
-
int32_t c = lexer->lookahead;
|
|
182
|
-
if (!state || !state->starts || !(c == '#' || c == '\\' || pragma_space(c))) return false;
|
|
183
|
-
TSLexerPosition position;
|
|
184
|
-
lexer->get_position(lexer, &position);
|
|
185
|
-
if (!empty_directives_start_at(state, position.bytes)) return false;
|
|
186
|
-
empty_directives_consume_started(lexer, state, skip_prefix);
|
|
187
|
-
return true;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
static bool empty_directives_start_at(const EmptyDirectives *state, uint32_t byte) {
|
|
191
|
-
return directive_bit(state, state->starts, byte);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Consumes the rest of a null directive whose start was confirmed at a position the lexer has not left except over
|
|
195
|
-
// prefix characters: as a token (`skip_prefix`) up to its first recorded end, which `directive_record` places before
|
|
196
|
-
// the comments or after the line, and otherwise the whole logical line, since some scanner lookahead dispatches on
|
|
197
|
-
// the character after the skip without reading comments.
|
|
198
|
-
static void empty_directives_consume_started(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
|
|
199
|
-
TSLexerPosition position;
|
|
200
|
-
while (lexer->lookahead != '#' && !pragma_eof(lexer)) lexer->advance(lexer, skip_prefix);
|
|
201
|
-
for (bool whole = !skip_prefix;; whole = false) {
|
|
202
|
-
do {
|
|
203
|
-
lexer->advance(lexer, false);
|
|
204
|
-
lexer->get_position(lexer, &position);
|
|
205
|
-
} while (!pragma_eof(lexer) && !directive_bit(state, state->ends, position.bytes));
|
|
206
|
-
if (!whole || pragma_eof(lexer) || !directive_bit(state, state->comments, position.bytes)) return;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
127
|
// Ordinary code goes through `DirectiveScan` without being stored, and only directive segments, each from a directive's
|
|
211
128
|
// `#` to the end of the logical line where no directive state is left, are stored for the line-state pass and the
|
|
212
129
|
// reverse prefix pass. The facts are stored as `EmptyDirectives` describes.
|
|
@@ -235,11 +152,12 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
|
|
|
235
152
|
for (;;) {
|
|
236
153
|
if (!directive_scan_buffered(&scan, &input)) {
|
|
237
154
|
buffer.length = buffer.position_count = 0;
|
|
238
|
-
if (!directive_scan_lexer(&scan, &buffer)) break;
|
|
155
|
+
if (!directive_scan_lexer(state, &scan, &buffer)) break;
|
|
239
156
|
input = (DirectiveInput){.buffer = &buffer, .byte = buffer.fill_byte};
|
|
240
157
|
}
|
|
241
158
|
directive_shift(&buffer, input.index);
|
|
242
159
|
buffer.carries = false;
|
|
160
|
+
state->barriers.count = 0;
|
|
243
161
|
uint32_t start = input.byte;
|
|
244
162
|
input = (DirectiveInput){.buffer = &buffer, .byte = start, .previous_byte = start, .before_previous_byte = start};
|
|
245
163
|
bool more = directive_segment(state, &input);
|
|
@@ -289,13 +207,17 @@ static bool directive_scan_buffered(DirectiveScan *scan, DirectiveInput *input)
|
|
|
289
207
|
}
|
|
290
208
|
|
|
291
209
|
// Feeds the lexer's characters until a directive's `#`, which is left as the lookahead, or the end of the input.
|
|
292
|
-
static bool directive_scan_lexer(DirectiveScan *scan, DirectiveBuffer *buffer) {
|
|
210
|
+
static bool directive_scan_lexer(EmptyDirectives *state, DirectiveScan *scan, DirectiveBuffer *buffer) {
|
|
293
211
|
TSLexer *lexer = buffer->lexer;
|
|
294
212
|
TSLexerPosition position;
|
|
295
213
|
for (;;) {
|
|
296
214
|
directive_scan_plain_run(scan, lexer);
|
|
297
215
|
if (pragma_eof(lexer)) break;
|
|
298
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
|
+
}
|
|
299
221
|
if (directive_scan_feed(scan, c)) {
|
|
300
222
|
lexer->get_position(lexer, &position);
|
|
301
223
|
buffer->fill_byte = position.bytes;
|
|
@@ -310,6 +232,168 @@ static bool directive_scan_lexer(DirectiveScan *scan, DirectiveBuffer *buffer) {
|
|
|
310
232
|
return false;
|
|
311
233
|
}
|
|
312
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
|
+
|
|
313
397
|
// Runs the line-state pass from a directive's `#` until a logical line ends with no directive state left and
|
|
314
398
|
// `directive_segment_ends` lets `DirectiveScan` take over again. Returns false at the end of the input.
|
|
315
399
|
static bool directive_segment(EmptyDirectives *state, DirectiveInput *input) {
|
|
@@ -333,6 +417,7 @@ static bool directive_segment(EmptyDirectives *state, DirectiveInput *input) {
|
|
|
333
417
|
// which may run on for any length, goes back to `DirectiveScan`.
|
|
334
418
|
static bool directive_segment_ends(const DirectiveInput *input) {
|
|
335
419
|
if (input->index >= DIRECTIVE_SEGMENT_UNITS) return true;
|
|
420
|
+
if (input->index == input->buffer->length) return directive_lexer_segment_ends(input->buffer->lexer);
|
|
336
421
|
uint32_t index = input->index, limit = input->index + DIRECTIVE_FILL_UNITS;
|
|
337
422
|
while (index < limit && directive_has(input, index) && directive_horizontal(directive_character(input, index))) index++;
|
|
338
423
|
if (index >= limit || !directive_has(input, index)) return true;
|
|
@@ -340,6 +425,31 @@ static bool directive_segment_ends(const DirectiveInput *input) {
|
|
|
340
425
|
return c != '#' && !directive_newline(c) && !directive_short_line_comment(input, index, limit);
|
|
341
426
|
}
|
|
342
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
|
+
|
|
343
453
|
// A line splice would continue the comment, so the line holds no `\`.
|
|
344
454
|
static bool directive_short_line_comment(const DirectiveInput *input, uint32_t index, uint32_t limit) {
|
|
345
455
|
if (directive_character(input, index) != '/' || !directive_has(input, index + 1) ||
|
|
@@ -357,184 +467,133 @@ static bool directive_line_clean(const DirectiveLine *line) {
|
|
|
357
467
|
!line->pragma_name && !line->pragma_depth && !line->header && line->kind == DIRECTIVE_ORDINARY && !line->required_end;
|
|
358
468
|
}
|
|
359
469
|
|
|
360
|
-
//
|
|
361
|
-
static void directive_shift(DirectiveBuffer *buffer, uint32_t start) {
|
|
362
|
-
if (!start) return;
|
|
363
|
-
uint32_t length = buffer->length - start;
|
|
364
|
-
for (uint32_t i = 0; i < length; i++) {
|
|
365
|
-
uint32_t from = start + i;
|
|
366
|
-
buffer->characters[i >> DIRECTIVE_PAGE_SHIFT][i & (DIRECTIVE_PAGE_SIZE - 1)] =
|
|
367
|
-
buffer->characters[from >> DIRECTIVE_PAGE_SHIFT][from & (DIRECTIVE_PAGE_SIZE - 1)];
|
|
368
|
-
}
|
|
369
|
-
buffer->length = length;
|
|
370
|
-
uint32_t count = 0;
|
|
371
|
-
for (uint32_t i = 0; i < buffer->position_count; i++) {
|
|
372
|
-
DirectivePosition position = buffer->positions[i];
|
|
373
|
-
if (position.index < start) continue;
|
|
374
|
-
position.index -= start;
|
|
375
|
-
buffer->positions[count++] = position;
|
|
376
|
-
}
|
|
377
|
-
buffer->position_count = count;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// 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.
|
|
381
471
|
static void directive_finish(EmptyDirectives *state, DirectiveBuffer *buffer, DirectiveSpans *run, DirectiveSpans *tail) {
|
|
382
|
-
bool failed = buffer->failed || run->failed || tail->failed;
|
|
472
|
+
bool failed = buffer->failed || run->failed || tail->failed || state->line_ends.failed;
|
|
383
473
|
free(run->items);
|
|
384
474
|
free(tail->items);
|
|
475
|
+
free(state->barriers.items);
|
|
476
|
+
state->barriers = (DirectiveBarriers){0};
|
|
385
477
|
directive_release(buffer);
|
|
386
478
|
state->pragmas = buffer->pragma || failed;
|
|
479
|
+
state->failed = failed;
|
|
387
480
|
if (failed) empty_directives_release(state);
|
|
388
481
|
}
|
|
389
482
|
|
|
390
|
-
static void empty_directives_release(EmptyDirectives *state) {
|
|
391
|
-
uint8_t **directories[] = {state->starts, state->ends, state->comments, state->line_ends, state->directive_starts, state->directive_ends};
|
|
392
|
-
for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
|
|
393
|
-
if (!directories[i]) continue;
|
|
394
|
-
for (uint32_t page = 0; page < state->pages; page++) free(directories[i][page]);
|
|
395
|
-
free(directories[i]);
|
|
396
|
-
}
|
|
397
|
-
state->starts = state->ends = state->comments = state->line_ends = state->directive_starts = state->directive_ends = NULL;
|
|
398
|
-
state->pages = 0;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
483
|
// Marks the prefixes of the segment's null directives and the spaces before its directive line ends, reading the
|
|
402
484
|
// segment backwards from `input`, where its forward pass stopped. A prefix reaching the segment's `#` extends over the
|
|
403
485
|
// run before the segment, whose spans `prefix` holds. Stores in `tail`, in byte order, the spans of the segment's
|
|
404
|
-
// trailing run of spaces and backslashes after its last line end, over which a later
|
|
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.
|
|
405
488
|
static void directive_prefixes(EmptyDirectives *state, const DirectiveInput *input, const DirectiveSpans *prefix, DirectiveSpans *tail) {
|
|
406
489
|
DirectiveCarry carry = {.tail = true};
|
|
407
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;
|
|
408
493
|
bool complete = true;
|
|
409
494
|
tail->count = 0;
|
|
410
495
|
for (uint32_t i = input->index; i > 0;) {
|
|
411
|
-
if (!carry.prefix && !carry.line_end && !carry.tail)
|
|
496
|
+
if (!carry.prefix && !carry.line_end && !carry.tail) {
|
|
497
|
+
i = directive_skip_inert(state, input, i, &position, &byte);
|
|
498
|
+
}
|
|
412
499
|
if (!i) break;
|
|
413
500
|
int32_t c;
|
|
414
501
|
complete = directive_step_back(input, &i, &position, &byte, &c, &width);
|
|
415
502
|
if (!complete) break;
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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;
|
|
419
507
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
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);
|
|
423
514
|
}
|
|
424
515
|
|
|
425
|
-
static
|
|
426
|
-
|
|
427
|
-
DirectiveSpan span = spans->items[i];
|
|
428
|
-
spans->items[i] = spans->items[spans->count - 1 - i];
|
|
429
|
-
spans->items[spans->count - 1 - i] = span;
|
|
430
|
-
}
|
|
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);
|
|
431
518
|
}
|
|
432
519
|
|
|
433
|
-
static void
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
// Moves back to the character before `*index`, reading a surrogate pair as one character, and decodes it into `*c` and
|
|
440
|
-
// its byte width into `*width`; false when its width exceeds the bytes left.
|
|
441
|
-
static bool directive_step_back(const DirectiveInput *input, uint32_t *index, uint32_t *position, uint32_t *byte, int32_t *c, uint32_t *width) {
|
|
442
|
-
const DirectiveBuffer *buffer = input->buffer;
|
|
443
|
-
uint32_t i = --*index;
|
|
444
|
-
if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
|
|
445
|
-
directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) i = --*index;
|
|
446
|
-
*width = directive_expected_width(input, i, c);
|
|
447
|
-
if (*position && buffer->positions[*position - 1].index == i) {
|
|
448
|
-
DirectivePosition recorded = buffer->positions[--*position];
|
|
449
|
-
*byte = recorded.byte;
|
|
450
|
-
*width = recorded.width;
|
|
451
|
-
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;
|
|
452
526
|
}
|
|
453
|
-
if (*width > *byte) return false;
|
|
454
|
-
*byte -= *width;
|
|
455
|
-
return true;
|
|
456
527
|
}
|
|
457
528
|
|
|
458
529
|
// Carries null-directive prefixes back from their `#` and line ends back from their barriers over the character `c` at
|
|
459
530
|
// `byte`, and whether the trailing run continues.
|
|
460
|
-
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--;
|
|
461
535
|
bool hash = c == '#' && directive_bit(state, state->starts, byte);
|
|
462
|
-
bool condition_end = directive_bit(state, state->line_ends, byte);
|
|
463
536
|
bool space = pragma_space(c) || c == '\\';
|
|
464
537
|
carry->line_end = condition_end || (carry->line_end && directive_horizontal(c));
|
|
465
|
-
if (carry->line_end)
|
|
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);
|
|
466
540
|
carry->prefix = !condition_end && (hash || (carry->prefix && space));
|
|
467
541
|
if (carry->prefix) directive_set(state, buffer, &state->starts, byte);
|
|
468
542
|
carry->tail = carry->tail && !condition_end && space;
|
|
469
543
|
}
|
|
470
544
|
|
|
471
|
-
// Extends
|
|
472
|
-
static void
|
|
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) {
|
|
473
547
|
if (width > DIRECTIVE_CHARACTER_BYTES) width = DIRECTIVE_CHARACTER_BYTES;
|
|
474
|
-
if (
|
|
475
|
-
|
|
548
|
+
if (span->open && byte + width == span->start) {
|
|
549
|
+
span->start = byte;
|
|
476
550
|
return;
|
|
477
551
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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;
|
|
482
562
|
}
|
|
483
563
|
|
|
484
564
|
// Steps back from the character before `end` over characters that cannot change the reverse pass's state while no
|
|
485
565
|
// prefix or line end is carried: plain-width characters that are neither `#` nor barriers. Returns the new end.
|
|
486
|
-
|
|
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) {
|
|
487
568
|
const DirectiveBuffer *buffer = input->buffer;
|
|
488
|
-
|
|
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;
|
|
489
582
|
while (end > stop) {
|
|
490
|
-
uint32_t
|
|
491
|
-
|
|
492
|
-
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;
|
|
493
585
|
uint32_t previous = *byte - width;
|
|
494
|
-
if (
|
|
586
|
+
if (has_barrier && previous <= barrier_byte) break;
|
|
495
587
|
*byte = previous;
|
|
496
588
|
end--;
|
|
497
589
|
}
|
|
498
590
|
return end;
|
|
499
591
|
}
|
|
500
592
|
|
|
501
|
-
static
|
|
502
|
-
if (
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
buffer->failed = true;
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
if (!*facts && !(*facts = calloc(state->pages, sizeof **facts))) {
|
|
509
|
-
buffer->failed = true;
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
|
-
uint8_t **bits = &(*facts)[page];
|
|
513
|
-
if (!*bits && !(*bits = calloc(DIRECTIVE_FACT_PAGE_SIZE / 8, 1))) {
|
|
514
|
-
buffer->failed = true;
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
uint32_t offset = byte & (DIRECTIVE_FACT_PAGE_SIZE - 1);
|
|
518
|
-
(*bits)[offset / 8] |= 1u << (offset % 8);
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
static bool directive_bit(const EmptyDirectives *state, uint8_t *const *facts, uint32_t byte) {
|
|
522
|
-
uint32_t page = byte >> DIRECTIVE_FACT_PAGE_SHIFT, offset = byte & (DIRECTIVE_FACT_PAGE_SIZE - 1);
|
|
523
|
-
return facts && page < state->pages && facts[page] && (facts[page][offset / 8] & (1u << (offset % 8)));
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
static bool directive_grow_directories(EmptyDirectives *state, uint32_t pages) {
|
|
527
|
-
uint8_t ***directories[] = {&state->starts, &state->ends, &state->comments, &state->line_ends, &state->directive_starts, &state->directive_ends};
|
|
528
|
-
if ((size_t)pages > SIZE_MAX / sizeof(uint8_t *)) return false;
|
|
529
|
-
for (unsigned i = 0; i < sizeof directories / sizeof *directories; i++) {
|
|
530
|
-
if (!*directories[i]) continue;
|
|
531
|
-
uint8_t **directory = realloc(*directories[i], (size_t)pages * sizeof *directory);
|
|
532
|
-
if (!directory) return false;
|
|
533
|
-
memset(directory + state->pages, 0, (size_t)(pages - state->pages) * sizeof *directory);
|
|
534
|
-
*directories[i] = directory;
|
|
535
|
-
}
|
|
536
|
-
state->pages = pages;
|
|
537
|
-
return true;
|
|
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;
|
|
538
597
|
}
|
|
539
598
|
|
|
540
599
|
static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
|
|
@@ -552,19 +611,128 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
|
|
|
552
611
|
}
|
|
553
612
|
if (directive_horizontal(c) || (line->beginning && pragma_space(c))) return;
|
|
554
613
|
if (c == '#' && line->beginning) {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
line
|
|
560
|
-
|
|
561
|
-
|
|
614
|
+
directive_start_hash(state, input, line);
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
if (directive_neutral(line)) {
|
|
618
|
+
directive_neutral_code(state, input, line, c);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
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;
|
|
562
624
|
line->beginning = false;
|
|
563
|
-
line->pending = true;
|
|
564
625
|
return;
|
|
565
626
|
}
|
|
566
|
-
|
|
567
|
-
|
|
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;
|
|
641
|
+
}
|
|
642
|
+
|
|
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);
|
|
668
|
+
return true;
|
|
669
|
+
}
|
|
670
|
+
|
|
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);
|
|
683
|
+
}
|
|
684
|
+
|
|
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;
|
|
693
|
+
}
|
|
694
|
+
if (i >= end) return false;
|
|
695
|
+
*index_io = i + 1;
|
|
696
|
+
*has_operand = true;
|
|
697
|
+
return true;
|
|
698
|
+
}
|
|
699
|
+
|
|
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;
|
|
720
|
+
}
|
|
721
|
+
return false;
|
|
722
|
+
}
|
|
723
|
+
|
|
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;
|
|
732
|
+
}
|
|
733
|
+
if (c >= 0x80 || c == '\\') return false;
|
|
734
|
+
}
|
|
735
|
+
return false;
|
|
568
736
|
}
|
|
569
737
|
|
|
570
738
|
// code-gauge-allow functionCognitiveComplexity -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
|
|
@@ -592,17 +760,63 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
|
|
|
592
760
|
}
|
|
593
761
|
|
|
594
762
|
// Outside a directive name, a line with no `_Pragma` or header operand in progress keeps its state through every token
|
|
595
|
-
// when it is of no directive kind, or of a kind that only waits for an operand it already has
|
|
596
|
-
// classification.
|
|
763
|
+
// when it is of no directive kind, or of a kind that only waits for an operand it already has.
|
|
597
764
|
static bool directive_neutral(const DirectiveLine *line) {
|
|
598
|
-
|
|
599
|
-
return
|
|
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));
|
|
767
|
+
}
|
|
768
|
+
|
|
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;
|
|
600
813
|
}
|
|
601
814
|
|
|
602
815
|
// Also consumes the stored characters after the token that leave the line state as it is.
|
|
603
816
|
static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
|
|
604
817
|
line->operand = true;
|
|
605
818
|
line->beginning = false;
|
|
819
|
+
if (directive_skip_plain_line(input, c, false)) return;
|
|
606
820
|
if (c == '"' || c == '\'') {
|
|
607
821
|
if (directive_quoted(input, c)) {
|
|
608
822
|
directive_line_end(state, input, line);
|
|
@@ -717,7 +931,7 @@ static void directive_complete_required(EmptyDirectives *state, DirectiveInput *
|
|
|
717
931
|
line->pragma_name = false;
|
|
718
932
|
if (!line->required_end) return;
|
|
719
933
|
if (line->operand || line->pragma_pending || pragma_name) {
|
|
720
|
-
|
|
934
|
+
directive_offset_push(&state->directive_ends, input->buffer, line->required_end);
|
|
721
935
|
}
|
|
722
936
|
line->required_end = 0;
|
|
723
937
|
}
|
|
@@ -729,164 +943,13 @@ static bool directive_requires_operand(DirectiveKind kind) {
|
|
|
729
943
|
static void directive_barrier(EmptyDirectives *state, DirectiveInput *input) {
|
|
730
944
|
if (!input->index || !directive_newline(directive_character(input, input->index - 1))) return;
|
|
731
945
|
input->buffer->carries = true;
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
directive_set(state, input->buffer, &state->line_ends, input->before_previous_byte);
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
static int32_t directive_next(DirectiveInput *input) {
|
|
741
|
-
int32_t plain = directive_plain(input);
|
|
742
|
-
if (plain >= 0) {
|
|
743
|
-
directive_skip_plain(input);
|
|
744
|
-
return plain;
|
|
745
|
-
}
|
|
746
|
-
while (directive_has(input, input->index)) {
|
|
747
|
-
int32_t c = directive_physical_next(input);
|
|
748
|
-
if (c == '\\' && directive_skip_splice(input)) continue;
|
|
749
|
-
if (directive_newline(c)) {
|
|
750
|
-
if (directive_has(input, input->index) && directive_newline(directive_character(input, input->index)) &&
|
|
751
|
-
directive_character(input, input->index) != c) directive_physical_next(input);
|
|
752
|
-
return '\n';
|
|
753
|
-
}
|
|
754
|
-
return c;
|
|
755
|
-
}
|
|
756
|
-
return -1;
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
static int32_t directive_physical_next(DirectiveInput *input) {
|
|
760
|
-
const DirectiveBuffer *buffer = input->buffer;
|
|
761
|
-
uint32_t index = input->index;
|
|
762
|
-
int32_t c;
|
|
763
|
-
uint32_t width = directive_expected_width(input, index, &c);
|
|
764
|
-
input->index += c > 0xffff ? 2 : 1;
|
|
765
|
-
input->before_previous_byte = input->previous_byte;
|
|
766
|
-
input->previous_byte = input->byte;
|
|
767
|
-
if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index == index) {
|
|
768
|
-
DirectivePosition position = buffer->positions[input->position_index++];
|
|
769
|
-
input->previous_byte = position.byte;
|
|
770
|
-
input->byte = position.byte + position.width;
|
|
771
|
-
} else input->byte += width;
|
|
772
|
-
return c;
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
// Decodes the character at `index` into `*c` and returns its byte width when it has no recorded position.
|
|
776
|
-
static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c) {
|
|
777
|
-
uint32_t unit = directive_character(input, index);
|
|
778
|
-
if (unit >= 0xd800 && unit <= 0xdbff && directive_has(input, index + 1)) {
|
|
779
|
-
uint32_t low = directive_character(input, index + 1);
|
|
780
|
-
if (low >= 0xdc00 && low <= 0xdfff) {
|
|
781
|
-
*c = (int32_t)(0x10000 + ((unit - 0xd800) << 10) + (low - 0xdc00));
|
|
782
|
-
return 4;
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
*c = (int32_t)unit;
|
|
786
|
-
return input->buffer->utf16 ? 2 : directive_utf8_width(unit);
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
// Consumes the rest of a line splice after its `\\`; false when none follows.
|
|
790
|
-
static bool directive_skip_splice(DirectiveInput *input) {
|
|
791
|
-
uint32_t after = directive_splice_end(input);
|
|
792
|
-
if (after == input->index) return false;
|
|
793
|
-
if (directive_horizontal(directive_character(input, input->index))) input->spaced_splice = true;
|
|
794
|
-
while (input->index < after) directive_physical_next(input);
|
|
795
|
-
return true;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
static uint32_t directive_splice_end(const DirectiveInput *input) {
|
|
799
|
-
uint32_t after = input->index;
|
|
800
|
-
while (directive_has(input, after) && directive_horizontal(directive_character(input, after))) after++;
|
|
801
|
-
if (!directive_has(input, after) || !directive_newline(directive_character(input, after))) return input->index;
|
|
802
|
-
int32_t newline = directive_character(input, after++);
|
|
803
|
-
if (directive_has(input, after) && directive_newline(directive_character(input, after)) &&
|
|
804
|
-
directive_character(input, after) != newline) after++;
|
|
805
|
-
return after;
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
static int32_t directive_peek(const DirectiveInput *input) {
|
|
809
|
-
int32_t plain = directive_plain(input);
|
|
810
|
-
if (plain >= 0) return plain;
|
|
811
|
-
DirectiveInput copy = *input;
|
|
812
|
-
return directive_next(©);
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
// Consumes the longest run of plain characters that `run` accepts and returns its length.
|
|
816
|
-
static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run) {
|
|
817
|
-
const DirectiveBuffer *buffer = input->buffer;
|
|
818
|
-
uint32_t index = input->index;
|
|
819
|
-
// Scans the stored characters before the next recorded position and reads more only at their end, because reading
|
|
820
|
-
// may store a character with a recorded position, which ends the run.
|
|
821
|
-
while (directive_has(input, index)) {
|
|
822
|
-
uint32_t end = buffer->length;
|
|
823
|
-
if (input->position_index < buffer->position_count && buffer->positions[input->position_index].index < end) {
|
|
824
|
-
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);
|
|
825
950
|
}
|
|
826
|
-
uint32_t start = index;
|
|
827
|
-
index = directive_run_end(buffer, run, index, end);
|
|
828
|
-
if (index < end || index == start || end < buffer->length) break;
|
|
829
951
|
}
|
|
830
|
-
|
|
831
|
-
if (length) directive_advance_plain(input, length);
|
|
832
|
-
return length;
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
// Moves `input` over `length` plain characters, at least one.
|
|
836
|
-
static void directive_advance_plain(DirectiveInput *input, uint32_t length) {
|
|
837
|
-
uint32_t width = input->buffer->utf16 ? 2 : 1;
|
|
838
|
-
input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
|
|
839
|
-
input->previous_byte = input->byte + (length - 1) * width;
|
|
840
|
-
input->byte += length * width;
|
|
841
|
-
input->index += length;
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
// The index of the first stored character from `index` to `end` that ends the run, or `end`.
|
|
845
|
-
static uint32_t directive_run_end(const DirectiveBuffer *buffer, DirectiveRun run, uint32_t index, uint32_t end) {
|
|
846
|
-
while (index < end) {
|
|
847
|
-
const uint16_t *page = buffer->characters[index >> DIRECTIVE_PAGE_SHIFT];
|
|
848
|
-
uint32_t page_end = (index | (DIRECTIVE_PAGE_SIZE - 1)) + 1;
|
|
849
|
-
if (page_end > end) page_end = end;
|
|
850
|
-
for (; index < page_end; index++) {
|
|
851
|
-
uint16_t c = page[index & (DIRECTIVE_PAGE_SIZE - 1)];
|
|
852
|
-
if (c >= 0x80 || c == '\\' || directive_newline(c) || !directive_run_continues(run, c)) return index;
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
return end;
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
// `c` is ASCII.
|
|
859
|
-
static bool directive_run_continues(DirectiveRun run, uint16_t c) {
|
|
860
|
-
bool identifier = (preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1;
|
|
861
|
-
switch (run) {
|
|
862
|
-
case DIRECTIVE_RUN_WORD:
|
|
863
|
-
return identifier;
|
|
864
|
-
case DIRECTIVE_RUN_SPACE:
|
|
865
|
-
return directive_horizontal(c);
|
|
866
|
-
case DIRECTIVE_RUN_LINE_COMMENT:
|
|
867
|
-
return true;
|
|
868
|
-
case DIRECTIVE_RUN_BLOCK_COMMENT:
|
|
869
|
-
return c != '*';
|
|
870
|
-
}
|
|
871
|
-
return false;
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
static void directive_skip_plain(DirectiveInput *input) {
|
|
875
|
-
input->index++;
|
|
876
|
-
input->before_previous_byte = input->previous_byte;
|
|
877
|
-
input->previous_byte = input->byte;
|
|
878
|
-
input->byte += input->buffer->utf16 ? 2 : 1;
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
// The next character when it is ASCII, cannot start a line splice or a newline, and has its expected width; otherwise -1.
|
|
882
|
-
static int32_t directive_plain(const DirectiveInput *input) {
|
|
883
|
-
if (!directive_has(input, input->index)) return -1;
|
|
884
|
-
uint16_t c = directive_character(input, input->index);
|
|
885
|
-
if (c >= 0x80 || c == '\\' || directive_newline(c)) return -1;
|
|
886
|
-
const DirectiveBuffer *buffer = input->buffer;
|
|
887
|
-
bool positioned = input->position_index < buffer->position_count &&
|
|
888
|
-
buffer->positions[input->position_index].index == input->index;
|
|
889
|
-
return positioned ? -1 : c;
|
|
952
|
+
directive_barrier_push(&state->barriers, input->buffer, input->index - 1, input->previous_byte, input->byte);
|
|
890
953
|
}
|
|
891
954
|
|
|
892
955
|
// `_Pragma` is the macro name of `#define _Pragma(x)` or `#ifdef _Pragma`, but an operator before a string literal.
|
|
@@ -959,7 +1022,7 @@ static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *i
|
|
|
959
1022
|
DirectiveBuffer *buffer = input->buffer;
|
|
960
1023
|
// Directives in a group that may be skipped need no operand, and the parser cannot tell which groups are skipped.
|
|
961
1024
|
if (directive_requires_operand(kind) && !buffer->conditional_depth) line->required_end = end;
|
|
962
|
-
else
|
|
1025
|
+
else directive_offset_push(&state->directive_ends, buffer, end);
|
|
963
1026
|
if (word->length < sizeof word->directive) {
|
|
964
1027
|
if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "ifdef") == 0 || strcmp(word->directive, "ifndef") == 0) buffer->conditional_depth++;
|
|
965
1028
|
else if (strcmp(word->directive, "endif") == 0 && buffer->conditional_depth) buffer->conditional_depth--;
|
|
@@ -993,22 +1056,35 @@ static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool
|
|
|
993
1056
|
for (uint32_t i = 0; i < length && i < significant; i++) directive_word_character(word, directive_character(input, start + i));
|
|
994
1057
|
}
|
|
995
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.
|
|
996
1061
|
static DirectiveKind directive_word_kind(const DirectiveWord *word) {
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
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;
|
|
1010
1087
|
}
|
|
1011
|
-
return DIRECTIVE_ORDINARY;
|
|
1012
1088
|
}
|
|
1013
1089
|
|
|
1014
1090
|
static void directive_word_character(DirectiveWord *word, int32_t c) {
|
|
@@ -1045,10 +1121,6 @@ static bool directive_raw(DirectiveInput *input) {
|
|
|
1045
1121
|
return true;
|
|
1046
1122
|
}
|
|
1047
1123
|
|
|
1048
|
-
static uint32_t directive_byte(const DirectiveInput *input) {
|
|
1049
|
-
return input->byte;
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
1124
|
// A null directive with comments has two ends: before its first comment, marked in `comments` too, and after its line.
|
|
1053
1125
|
// It keeps its comments when a spaced splice follows their start, where the lexer would read them differently.
|
|
1054
1126
|
static void directive_record(EmptyDirectives *state, DirectiveInput *input, const DirectiveLine *line, uint32_t end) {
|