diff --git a/Makefile b/Makefile index 670e02ac..59554e1d 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ endif OBJ := $(SRC:.c=.o) # define default flags, and override to append mandatory flags -CFLAGS ?= -O3 -Wall -Wextra -Werror +CFLAGS ?= -O3 -Wall -Wextra -Werror -Wshadow override CFLAGS += -std=gnu99 -fPIC -Ilib/src -Ilib/include # ABI versioning diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index 306891df..dc7704d5 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -776,7 +776,7 @@ uint32_t ts_query_start_byte_for_pattern(const TSQuery *, uint32_t); const TSQueryPredicateStep *ts_query_predicates_for_pattern( const TSQuery *self, uint32_t pattern_index, - uint32_t *length + uint32_t *step_count ); /* @@ -807,7 +807,7 @@ bool ts_query_is_pattern_guaranteed_at_step(const TSQuery *self, uint32_t byte_o */ const char *ts_query_capture_name_for_id( const TSQuery *, - uint32_t id, + uint32_t index, uint32_t *length ); @@ -817,13 +817,13 @@ const char *ts_query_capture_name_for_id( */ TSQuantifier ts_query_capture_quantifier_for_id( const TSQuery *, - uint32_t pattern_id, - uint32_t capture_id + uint32_t pattern_index, + uint32_t capture_index ); const char *ts_query_string_value_for_id( const TSQuery *, - uint32_t id, + uint32_t index, uint32_t *length ); @@ -907,7 +907,7 @@ void ts_query_cursor_set_point_range(TSQueryCursor *, TSPoint, TSPoint); * Otherwise, return `false`. */ bool ts_query_cursor_next_match(TSQueryCursor *, TSQueryMatch *match); -void ts_query_cursor_remove_match(TSQueryCursor *, uint32_t id); +void ts_query_cursor_remove_match(TSQueryCursor *, uint32_t match_id); /** * Advance to the next capture of the currently running query. diff --git a/lib/include/tree_sitter/parser.h b/lib/include/tree_sitter/parser.h index 2b14ac10..46994f46 100644 --- a/lib/include/tree_sitter/parser.h +++ b/lib/include/tree_sitter/parser.h @@ -166,7 +166,7 @@ struct TSLanguage { * Parse Table Macros */ -#define SMALL_STATE(id) id - LARGE_STATE_COUNT +#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT) #define STATE(id) id @@ -176,7 +176,7 @@ struct TSLanguage { {{ \ .shift = { \ .type = TSParseActionTypeShift, \ - .state = state_value \ + .state = (state_value) \ } \ }} @@ -184,7 +184,7 @@ struct TSLanguage { {{ \ .shift = { \ .type = TSParseActionTypeShift, \ - .state = state_value, \ + .state = (state_value), \ .repetition = true \ } \ }} diff --git a/lib/src/array.h b/lib/src/array.h index abec9410..e5cd361f 100644 --- a/lib/src/array.h +++ b/lib/src/array.h @@ -25,8 +25,8 @@ extern "C" { #define array_new() \ { NULL, 0, 0 } -#define array_get(self, index) \ - (assert((uint32_t)index < (self)->size), &(self)->contents[index]) +#define array_get(self, _index) \ + (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) #define array_front(self) array_get(self, 0) @@ -38,7 +38,7 @@ extern "C" { array__reserve((VoidArray *)(self), array__elem_size(self), new_capacity) // Free any memory allocated for this array. -#define array_delete(self) array__delete((VoidArray *)self) +#define array_delete(self) array__delete((VoidArray *)(self)) #define array_push(self, element) \ (array__grow((VoidArray *)(self), 1, array__elem_size(self)), \ @@ -65,19 +65,19 @@ extern "C" { // Remove `old_count` elements from the array starting at the given `index`. At // the same index, insert `new_count` new elements, reading their values from the // `new_contents` pointer. -#define array_splice(self, index, old_count, new_count, new_contents) \ +#define array_splice(self, _index, old_count, new_count, new_contents) \ array__splice( \ - (VoidArray *)(self), array__elem_size(self), index, \ + (VoidArray *)(self), array__elem_size(self), _index, \ old_count, new_count, new_contents \ ) // Insert one `element` into the array at the given `index`. -#define array_insert(self, index, element) \ - array__splice((VoidArray *)(self), array__elem_size(self), index, 0, 1, &element) +#define array_insert(self, _index, element) \ + array__splice((VoidArray *)(self), array__elem_size(self), _index, 0, 1, &(element)) // Remove one `element` from the array at the given `index`. -#define array_erase(self, index) \ - array__erase((VoidArray *)(self), array__elem_size(self), index) +#define array_erase(self, _index) \ + array__erase((VoidArray *)(self), array__elem_size(self), _index) #define array_pop(self) ((self)->contents[--(self)->size]) @@ -95,23 +95,23 @@ extern "C" { // out-parameter is set to true. Otherwise, `index` is set to an index where // `needle` should be inserted in order to preserve the sorting, and `exists` // is set to false. -#define array_search_sorted_with(self, compare, needle, index, exists) \ - array__search_sorted(self, 0, compare, , needle, index, exists) +#define array_search_sorted_with(self, compare, needle, _index, _exists) \ + array__search_sorted(self, 0, compare, , needle, _index, _exists) // Search a sorted array for a given `needle` value, using integer comparisons // of a given struct field (specified with a leading dot) to determine the order. // // See also `array_search_sorted_with`. -#define array_search_sorted_by(self, field, needle, index, exists) \ - array__search_sorted(self, 0, _compare_int, field, needle, index, exists) +#define array_search_sorted_by(self, field, needle, _index, _exists) \ + array__search_sorted(self, 0, compare_int, field, needle, _index, _exists) // Insert a given `value` into a sorted array, using the given `compare` // callback to determine the order. #define array_insert_sorted_with(self, compare, value) \ do { \ - unsigned index, exists; \ - array_search_sorted_with(self, compare, &(value), &index, &exists); \ - if (!exists) array_insert(self, index, value); \ + unsigned _index, _exists; \ + array_search_sorted_with(self, compare, &(value), &_index, &_exists); \ + if (!_exists) array_insert(self, _index, value); \ } while (0) // Insert a given `value` into a sorted array, using integer comparisons of @@ -120,9 +120,9 @@ extern "C" { // See also `array_search_sorted_by`. #define array_insert_sorted_by(self, field, value) \ do { \ - unsigned index, exists; \ - array_search_sorted_by(self, field, (value) field, &index, &exists); \ - if (!exists) array_insert(self, index, value); \ + unsigned _index, _exists; \ + array_search_sorted_by(self, field, (value) field, &_index, &_exists); \ + if (!_exists) array_insert(self, _index, value); \ } while (0) // Private @@ -217,28 +217,28 @@ static inline void array__splice(VoidArray *self, size_t element_size, } // A binary search routine, based on Rust's `std::slice::binary_search_by`. -#define array__search_sorted(self, start, compare, suffix, needle, index, exists) \ +#define array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \ do { \ - *(index) = start; \ - *(exists) = false; \ - uint32_t size = (self)->size - *(index); \ + *(_index) = start; \ + *(_exists) = false; \ + uint32_t size = (self)->size - *(_index); \ if (size == 0) break; \ int comparison; \ while (size > 1) { \ uint32_t half_size = size / 2; \ - uint32_t mid_index = *(index) + half_size; \ + uint32_t mid_index = *(_index) + half_size; \ comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ - if (comparison <= 0) *(index) = mid_index; \ + if (comparison <= 0) *(_index) = mid_index; \ size -= half_size; \ } \ - comparison = compare(&((self)->contents[*(index)] suffix), (needle)); \ - if (comparison == 0) *(exists) = true; \ - else if (comparison < 0) *(index) += 1; \ + comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \ + if (comparison == 0) *(_exists) = true; \ + else if (comparison < 0) *(_index) += 1; \ } while (0) // Helper macro for the `_sorted_by` routines below. This takes the left (existing) // parameter by reference in order to work with the generic sorting function above. -#define _compare_int(a, b) ((int)*(a) - (int)(b)) +#define compare_int(a, b) ((int)*(a) - (int)(b)) #ifdef __cplusplus } diff --git a/lib/src/atomic.h b/lib/src/atomic.h index 16573242..e8a2060a 100644 --- a/lib/src/atomic.h +++ b/lib/src/atomic.h @@ -46,11 +46,11 @@ static inline size_t atomic_load(const volatile size_t *p) { } static inline uint32_t atomic_inc(volatile uint32_t *p) { - return __sync_add_and_fetch(p, 1u); + return __sync_add_and_fetch(p, 1U); } static inline uint32_t atomic_dec(volatile uint32_t *p) { - return __sync_sub_and_fetch(p, 1u); + return __sync_sub_and_fetch(p, 1U); } #endif diff --git a/lib/src/get_changed_ranges.c b/lib/src/get_changed_ranges.c index 18a42417..bcf8da94 100644 --- a/lib/src/get_changed_ranges.c +++ b/lib/src/get_changed_ranges.c @@ -210,7 +210,7 @@ static void iterator_ascend(Iterator *self) { static bool iterator_descend(Iterator *self, uint32_t goal_position) { if (self->in_padding) return false; - bool did_descend; + bool did_descend = false; do { did_descend = false; TreeCursorEntry entry = *array_back(&self->cursor.stack); diff --git a/lib/src/language.h b/lib/src/language.h index 7234685e..db61b602 100644 --- a/lib/src/language.h +++ b/lib/src/language.h @@ -83,7 +83,7 @@ static inline uint16_t ts_language_lookup( for (unsigned i = 0; i < group_count; i++) { uint16_t section_value = *(data++); uint16_t symbol_count = *(data++); - for (unsigned i = 0; i < symbol_count; i++) { + for (unsigned j = 0; j < symbol_count; j++) { if (*(data++) == symbol) return section_value; } } @@ -269,17 +269,17 @@ static inline void ts_language_aliases_for_symbol( *start = &self->public_symbol_map[original_symbol]; *end = *start + 1; - unsigned i = 0; + unsigned idx = 0; for (;;) { - TSSymbol symbol = self->alias_map[i++]; + TSSymbol symbol = self->alias_map[idx++]; if (symbol == 0 || symbol > original_symbol) break; - uint16_t count = self->alias_map[i++]; + uint16_t count = self->alias_map[idx++]; if (symbol == original_symbol) { - *start = &self->alias_map[i]; - *end = &self->alias_map[i + count]; + *start = &self->alias_map[idx]; + *end = &self->alias_map[idx + count]; break; } - i += count; + idx += count; } } @@ -289,21 +289,21 @@ static inline void ts_language_write_symbol_as_dot_string( TSSymbol symbol ) { const char *name = ts_language_symbol_name(self, symbol); - for (const char *c = name; *c; c++) { - switch (*c) { + for (const char *chr = name; *chr; chr++) { + switch (*chr) { case '"': case '\\': fputc('\\', f); - fputc(*c, f); + fputc(*chr, f); break; case '\n': fputs("\\n", f); break; case '\t': - fputs("\\n", f); + fputs("\\t", f); break; default: - fputc(*c, f); + fputc(*chr, f); break; } } diff --git a/lib/src/lexer.c b/lib/src/lexer.c index 5940be5e..e32158b2 100644 --- a/lib/src/lexer.c +++ b/lib/src/lexer.c @@ -209,9 +209,9 @@ static void ts_lexer__advance(TSLexer *_self, bool skip) { if (!self->chunk) return; if (skip) { - LOG("skip", self->data.lookahead); + LOG("skip", self->data.lookahead) } else { - LOG("consume", self->data.lookahead); + LOG("consume", self->data.lookahead) } ts_lexer__do_advance(self, skip); diff --git a/lib/src/parser.c b/lib/src/parser.c index b6be9aa6..e6a4d3d5 100644 --- a/lib/src/parser.c +++ b/lib/src/parser.c @@ -132,10 +132,10 @@ typedef struct { static const char *ts_string_input_read( void *_self, uint32_t byte, - TSPoint pt, + TSPoint point, uint32_t *length ) { - (void)pt; + (void)point; TSStringInput *self = (TSStringInput *)_self; if (byte >= self->length) { *length = 0; @@ -159,9 +159,9 @@ static void ts_parser__log(TSParser *self) { if (self->dot_graph_file) { fprintf(self->dot_graph_file, "graph {\nlabel=\""); - for (char *c = &self->lexer.debug_buffer[0]; *c != 0; c++) { - if (*c == '"' || *c == '\\') fputc('\\', self->dot_graph_file); - fputc(*c, self->dot_graph_file); + for (char *chr = &self->lexer.debug_buffer[0]; *chr != 0; chr++) { + if (*chr == '"' || *chr == '\\') fputc('\\', self->dot_graph_file); + fputc(*chr, self->dot_graph_file); } fprintf(self->dot_graph_file, "\"\n}\n\n"); } @@ -871,19 +871,19 @@ static StackVersion ts_parser__reduce( if (next_slice.version != slice.version) break; i++; - SubtreeArray children = next_slice.subtrees; - ts_subtree_array_remove_trailing_extras(&children, &self->trailing_extras2); + SubtreeArray next_slice_children = next_slice.subtrees; + ts_subtree_array_remove_trailing_extras(&next_slice_children, &self->trailing_extras2); if (ts_parser__select_children( self, ts_subtree_from_mut(parent), - &children + &next_slice_children )) { ts_subtree_array_clear(&self->tree_pool, &self->trailing_extras); ts_subtree_release(&self->tree_pool, ts_subtree_from_mut(parent)); array_swap(&self->trailing_extras, &self->trailing_extras2); parent = ts_subtree_new_node( - symbol, &children, production_id, self->language + symbol, &next_slice_children, production_id, self->language ); } else { array_clear(&self->trailing_extras2); @@ -994,8 +994,8 @@ static bool ts_parser__do_all_potential_reductions( if (version >= version_count) break; bool merged = false; - for (StackVersion i = initial_version_count; i < version; i++) { - if (ts_stack_merge(self->stack, i, version)) { + for (StackVersion j = initial_version_count; j < version; j++) { + if (ts_stack_merge(self->stack, j, version)) { merged = true; break; } @@ -1018,8 +1018,8 @@ static bool ts_parser__do_all_potential_reductions( for (TSSymbol symbol = first_symbol; symbol < end_symbol; symbol++) { TableEntry entry; ts_language_table_entry(self->language, state, symbol, &entry); - for (uint32_t i = 0; i < entry.action_count; i++) { - TSParseAction action = entry.actions[i]; + for (uint32_t j = 0; j < entry.action_count; j++) { + TSParseAction action = entry.actions[j]; switch (action.type) { case TSParseActionTypeShift: case TSParseActionTypeRecover: @@ -1041,8 +1041,8 @@ static bool ts_parser__do_all_potential_reductions( } StackVersion reduction_version = STACK_VERSION_NONE; - for (uint32_t i = 0; i < self->reduce_actions.size; i++) { - ReduceAction action = self->reduce_actions.contents[i]; + for (uint32_t j = 0; j < self->reduce_actions.size; j++) { + ReduceAction action = self->reduce_actions.contents[j]; reduction_version = ts_parser__reduce( self, version, action.symbol, action.count, diff --git a/lib/src/query.c b/lib/src/query.c index 18cde3e5..ff4bb06c 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -1687,10 +1687,10 @@ static bool ts_query__analyze_patterns(TSQuery *self, unsigned *error_offset) { array_search_sorted_by(&subgraphs, .symbol, parent_symbol, &subgraph_index, &exists); if (!exists) { unsigned first_child_step_index = parent_step_index + 1; - uint32_t i, exists; - array_search_sorted_by(&self->step_offsets, .step_index, first_child_step_index, &i, &exists); - assert(exists); - *error_offset = self->step_offsets.contents[i].byte_offset; + uint32_t j, child_exists; + array_search_sorted_by(&self->step_offsets, .step_index, first_child_step_index, &j, &child_exists); + assert(child_exists); + *error_offset = self->step_offsets.contents[j].byte_offset; all_patterns_are_valid = false; break; } @@ -1750,10 +1750,10 @@ static bool ts_query__analyze_patterns(TSQuery *self, unsigned *error_offset) { if (analysis.finished_parent_symbols.size == 0) { assert(analysis.final_step_indices.size > 0); uint16_t impossible_step_index = *array_back(&analysis.final_step_indices); - uint32_t i, exists; - array_search_sorted_by(&self->step_offsets, .step_index, impossible_step_index, &i, &exists); - if (i >= self->step_offsets.size) i = self->step_offsets.size - 1; - *error_offset = self->step_offsets.contents[i].byte_offset; + uint32_t j, impossible_exists; + array_search_sorted_by(&self->step_offsets, .step_index, impossible_step_index, &j, &impossible_exists); + if (j >= self->step_offsets.size) j = self->step_offsets.size - 1; + *error_offset = self->step_offsets.contents[j].byte_offset; all_patterns_are_valid = false; break; } @@ -2103,13 +2103,13 @@ static TSQueryError ts_query__parse_predicate( if (!stream_is_ident_start(stream)) return TSQueryErrorSyntax; const char *capture_name = stream->input; stream_scan_identifier(stream); - uint32_t length = (uint32_t)(stream->input - capture_name); + uint32_t capture_length = (uint32_t)(stream->input - capture_name); // Add the capture id to the first step of the pattern int capture_id = symbol_table_id_for_name( &self->captures, capture_name, - length + capture_length ); if (capture_id == -1) { stream_reset(stream, capture_name); @@ -2126,14 +2126,14 @@ static TSQueryError ts_query__parse_predicate( else if (stream->next == '"') { TSQueryError e = ts_query__parse_string_literal(self, stream); if (e) return e; - uint16_t id = symbol_table_insert_name( + uint16_t query_id = symbol_table_insert_name( &self->predicate_values, self->string_buffer.contents, self->string_buffer.size ); array_push(&self->predicate_steps, ((TSQueryPredicateStep) { .type = TSQueryPredicateStepTypeString, - .value_id = id, + .value_id = query_id, })); } @@ -2141,15 +2141,15 @@ static TSQueryError ts_query__parse_predicate( else if (stream_is_ident_start(stream)) { const char *symbol_start = stream->input; stream_scan_identifier(stream); - uint32_t length = (uint32_t)(stream->input - symbol_start); - uint16_t id = symbol_table_insert_name( + uint32_t symbol_length = (uint32_t)(stream->input - symbol_start); + uint16_t query_id = symbol_table_insert_name( &self->predicate_values, symbol_start, - length + symbol_length ); array_push(&self->predicate_steps, ((TSQueryPredicateStep) { .type = TSQueryPredicateStepTypeString, - .value_id = id, + .value_id = query_id, })); } @@ -2762,9 +2762,9 @@ TSQuery *ts_query_new( uint32_t start_depth = step->depth; bool is_rooted = start_depth == 0; for (uint32_t step_index = start_step_index + 1; step_index < self->steps.size; step_index++) { - QueryStep *step = &self->steps.contents[step_index]; - if (step->is_dead_end) break; - if (step->depth == start_depth) { + QueryStep *child_step = &self->steps.contents[step_index]; + if (child_step->is_dead_end) break; + if (child_step->depth == start_depth) { is_rooted = false; break; } @@ -3642,8 +3642,8 @@ static inline bool ts_query_cursor__advance( } // Update all of the in-progress states with current node. - for (unsigned i = 0, copy_count = 0; i < self->states.size; i += 1 + copy_count) { - QueryState *state = &self->states.contents[i]; + for (unsigned j = 0, copy_count = 0; j < self->states.size; j += 1 + copy_count) { + QueryState *state = &self->states.contents[j]; QueryStep *step = &self->query->steps.contents[state->step_index]; state->has_in_progress_alternatives = false; copy_count = 0; @@ -3670,8 +3670,8 @@ static inline bool ts_query_cursor__advance( } if (step->supertype_symbol) { bool has_supertype = false; - for (unsigned j = 0; j < supertype_count; j++) { - if (supertypes[j] == step->supertype_symbol) { + for (unsigned k = 0; k < supertype_count; k++) { + if (supertypes[k] == step->supertype_symbol) { has_supertype = true; break; } @@ -3716,8 +3716,8 @@ static inline bool ts_query_cursor__advance( &self->capture_list_pool, state->capture_list_id ); - array_erase(&self->states, i); - i--; + array_erase(&self->states, j); + j--; } continue; } @@ -3777,8 +3777,8 @@ static inline bool ts_query_cursor__advance( } if (state->dead) { - array_erase(&self->states, i); - i--; + array_erase(&self->states, j); + j--; continue; } @@ -3797,29 +3797,29 @@ static inline bool ts_query_cursor__advance( // If this state's next step has an alternative step, then copy the state in order // to pursue both alternatives. The alternative step itself may have an alternative, // so this is an interactive process. - unsigned end_index = i + 1; - for (unsigned j = i; j < end_index; j++) { - QueryState *state = &self->states.contents[j]; - QueryStep *next_step = &self->query->steps.contents[state->step_index]; - if (next_step->alternative_index != NONE) { + unsigned end_index = j + 1; + for (unsigned k = j; k < end_index; k++) { + QueryState *child_state = &self->states.contents[k]; + QueryStep *child_step = &self->query->steps.contents[child_state->step_index]; + if (child_step->alternative_index != NONE) { // A "dead-end" step exists only to add a non-sequential jump into the step sequence, // via its alternative index. When a state reaches a dead-end step, it jumps straight // to the step's alternative. - if (next_step->is_dead_end) { - state->step_index = next_step->alternative_index; - j--; + if (child_step->is_dead_end) { + child_state->step_index = child_step->alternative_index; + k--; continue; } // A "pass-through" step exists only to add a branch into the step sequence, // via its alternative_index. When a state reaches a pass-through step, it splits // in order to process the alternative step, and then it advances to the next step. - if (next_step->is_pass_through) { - state->step_index++; - j--; + if (child_step->is_pass_through) { + child_state->step_index++; + k--; } - QueryState *copy = ts_query_cursor__copy_state(self, &state); + QueryState *copy = ts_query_cursor__copy_state(self, &child_state); if (copy) { LOG( " split state for branch. pattern:%u, from_step:%u, to_step:%u, immediate:%d, capture_count: %u\n", @@ -3831,8 +3831,8 @@ static inline bool ts_query_cursor__advance( ); end_index++; copy_count++; - copy->step_index = next_step->alternative_index; - if (next_step->alternative_is_immediate) { + copy->step_index = child_step->alternative_index; + if (child_step->alternative_is_immediate) { copy->seeking_immediate_match = true; } } @@ -3840,11 +3840,11 @@ static inline bool ts_query_cursor__advance( } } - for (unsigned i = 0; i < self->states.size; i++) { - QueryState *state = &self->states.contents[i]; + for (unsigned j = 0; j < self->states.size; j++) { + QueryState *state = &self->states.contents[j]; if (state->dead) { - array_erase(&self->states, i); - i--; + array_erase(&self->states, j); + j--; continue; } @@ -3852,8 +3852,8 @@ static inline bool ts_query_cursor__advance( // repeated nodes, this is necessary to avoid multiple redundant states, where // one state has a strict subset of another state's captures. bool did_remove = false; - for (unsigned j = i + 1; j < self->states.size; j++) { - QueryState *other_state = &self->states.contents[j]; + for (unsigned k = j + 1; k < self->states.size; k++) { + QueryState *other_state = &self->states.contents[k]; // Query states are kept in ascending order of start_depth and pattern_index. // Since the longest-match criteria is only used for deduping matches of the same @@ -3880,8 +3880,8 @@ static inline bool ts_query_cursor__advance( state->step_index ); capture_list_pool_release(&self->capture_list_pool, other_state->capture_list_id); - array_erase(&self->states, j); - j--; + array_erase(&self->states, k); + k--; continue; } other_state->has_in_progress_alternatives = true; @@ -3894,8 +3894,8 @@ static inline bool ts_query_cursor__advance( state->step_index ); capture_list_pool_release(&self->capture_list_pool, state->capture_list_id); - array_erase(&self->states, i); - i--; + array_erase(&self->states, j); + j--; did_remove = true; break; } @@ -3922,7 +3922,7 @@ static inline bool ts_query_cursor__advance( array_push(&self->finished_states, *state); array_erase(&self->states, (uint32_t)(state - self->states.contents)); did_match = true; - i--; + j--; } } } diff --git a/lib/src/stack.c b/lib/src/stack.c index 98e3a96f..44f989ae 100644 --- a/lib/src/stack.c +++ b/lib/src/stack.c @@ -316,7 +316,7 @@ inline StackSliceArray stack__iter( array_clear(&self->iterators); StackHead *head = array_get(&self->heads, version); - StackIterator iterator = { + StackIterator new_iterator = { .node = head->node, .subtrees = array_new(), .subtree_count = 0, @@ -326,10 +326,10 @@ inline StackSliceArray stack__iter( bool include_subtrees = false; if (goal_subtree_count >= 0) { include_subtrees = true; - array_reserve(&iterator.subtrees, (uint32_t)ts_subtree_alloc_size(goal_subtree_count) / sizeof(Subtree)); + array_reserve(&new_iterator.subtrees, (uint32_t)ts_subtree_alloc_size(goal_subtree_count) / sizeof(Subtree)); } - array_push(&self->iterators, iterator); + array_push(&self->iterators, new_iterator); while (self->iterators.size > 0) { for (uint32_t i = 0, size = self->iterators.size; i < size; i++) { @@ -505,7 +505,7 @@ inline StackAction pop_count_callback(void *payload, const StackIterator *iterat } StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, uint32_t count) { - return stack__iter(self, version, pop_count_callback, &count, count); + return stack__iter(self, version, pop_count_callback, &count, (int)count); } inline StackAction pop_pending_callback(void *payload, const StackIterator *iterator) { diff --git a/lib/src/subtree.c b/lib/src/subtree.c index 2bf25dc5..5f39bd82 100644 --- a/lib/src/subtree.c +++ b/lib/src/subtree.c @@ -56,10 +56,10 @@ const char *ts_external_scanner_state_data(const ExternalScannerState *self) { } } -bool ts_external_scanner_state_eq(const ExternalScannerState *a, const char *buffer, unsigned length) { +bool ts_external_scanner_state_eq(const ExternalScannerState *self, const char *buffer, unsigned length) { return - a->length == length && - memcmp(ts_external_scanner_state_data(a), buffer, length) == 0; + self->length == length && + memcmp(ts_external_scanner_state_data(self), buffer, length) == 0; } // SubtreeArray @@ -643,7 +643,7 @@ static inline void ts_subtree_set_has_changes(MutableSubtree *self) { } } -Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool) { +Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit, SubtreePool *pool) { typedef struct { Subtree *tree; Edit edit; @@ -653,9 +653,9 @@ Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool array_push(&stack, ((StackEntry) { .tree = &self, .edit = (Edit) { - .start = {edit->start_byte, edit->start_point}, - .old_end = {edit->old_end_byte, edit->old_end_point}, - .new_end = {edit->new_end_byte, edit->new_end_point}, + .start = {input_edit->start_byte, input_edit->start_point}, + .old_end = {input_edit->old_end_byte, input_edit->old_end_point}, + .new_end = {input_edit->new_end_byte, input_edit->new_end_point}, }, })); @@ -813,24 +813,24 @@ Subtree ts_subtree_last_external_token(Subtree tree) { return tree; } -static size_t ts_subtree__write_char_to_string(char *s, size_t n, int32_t c) { - if (c == -1) - return snprintf(s, n, "INVALID"); - else if (c == '\0') - return snprintf(s, n, "'\\0'"); - else if (c == '\n') - return snprintf(s, n, "'\\n'"); - else if (c == '\t') - return snprintf(s, n, "'\\t'"); - else if (c == '\r') - return snprintf(s, n, "'\\r'"); - else if (0 < c && c < 128 && isprint(c)) - return snprintf(s, n, "'%c'", c); +static size_t ts_subtree__write_char_to_string(char *str, size_t n, int32_t chr) { + if (chr == -1) + return snprintf(str, n, "INVALID"); + else if (chr == '\0') + return snprintf(str, n, "'\\0'"); + else if (chr == '\n') + return snprintf(str, n, "'\\n'"); + else if (chr == '\t') + return snprintf(str, n, "'\\t'"); + else if (chr == '\r') + return snprintf(str, n, "'\\r'"); + else if (0 < chr && chr < 128 && isprint(chr)) + return snprintf(str, n, "'%c'", chr); else - return snprintf(s, n, "%d", c); + return snprintf(str, n, "%d", chr); } -static const char *ROOT_FIELD = "__ROOT__"; +static const char *const ROOT_FIELD = "__ROOT__"; static size_t ts_subtree__write_to_string( Subtree self, char *string, size_t limit, @@ -902,17 +902,17 @@ static size_t ts_subtree__write_to_string( 0, false, NULL ); } else { - TSSymbol alias_symbol = alias_sequence + TSSymbol subtree_alias_symbol = alias_sequence ? alias_sequence[structural_child_index] : 0; - bool alias_is_named = alias_symbol - ? ts_language_symbol_metadata(language, alias_symbol).named + bool subtree_alias_is_named = subtree_alias_symbol + ? ts_language_symbol_metadata(language, subtree_alias_symbol).named : false; const char *child_field_name = is_visible ? NULL : field_name; - for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) { - if (!i->inherited && i->child_index == structural_child_index) { - child_field_name = language->field_names[i->field_id]; + for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) { + if (!map->inherited && map->child_index == structural_child_index) { + child_field_name = language->field_names[map->field_id]; break; } } @@ -920,7 +920,7 @@ static size_t ts_subtree__write_to_string( cursor += ts_subtree__write_to_string( child, *writer, limit, language, include_all, - alias_symbol, alias_is_named, child_field_name + subtree_alias_symbol, subtree_alias_is_named, child_field_name ); structural_child_index++; } @@ -996,12 +996,12 @@ void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset, ts_subtree_production_id(*self); for (uint32_t i = 0, n = ts_subtree_child_count(*self); i < n; i++) { const Subtree *child = &ts_subtree_children(*self)[i]; - TSSymbol alias_symbol = 0; + TSSymbol subtree_alias_symbol = 0; if (!ts_subtree_extra(*child) && child_info_offset) { - alias_symbol = language->alias_sequences[child_info_offset]; + subtree_alias_symbol = language->alias_sequences[child_info_offset]; child_info_offset++; } - ts_subtree__print_dot_graph(child, child_start_offset, language, alias_symbol, f); + ts_subtree__print_dot_graph(child, child_start_offset, language, subtree_alias_symbol, f); fprintf(f, "tree_%p -> tree_%p [tooltip=%u]\n", (void *)self, (void *)child, i); child_start_offset += ts_subtree_total_bytes(*child); } @@ -1028,12 +1028,12 @@ const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self) { } } -bool ts_subtree_external_scanner_state_eq(Subtree a, Subtree b) { - const ExternalScannerState *state_a = ts_subtree_external_scanner_state(a); - const ExternalScannerState *state_b = ts_subtree_external_scanner_state(b); +bool ts_subtree_external_scanner_state_eq(Subtree self, Subtree other) { + const ExternalScannerState *state_self = ts_subtree_external_scanner_state(self); + const ExternalScannerState *state_other = ts_subtree_external_scanner_state(other); return ts_external_scanner_state_eq( - state_a, - ts_external_scanner_state_data(state_b), - state_b->length + state_self, + ts_external_scanner_state_data(state_other), + state_other->length ); } diff --git a/lib/src/subtree.h b/lib/src/subtree.h index 7b4db2e6..c5eca079 100644 --- a/lib/src/subtree.h +++ b/lib/src/subtree.h @@ -175,7 +175,7 @@ typedef struct { void ts_external_scanner_state_init(ExternalScannerState *, const char *, unsigned); const char *ts_external_scanner_state_data(const ExternalScannerState *); -bool ts_external_scanner_state_eq(const ExternalScannerState *a, const char *, unsigned); +bool ts_external_scanner_state_eq(const ExternalScannerState *self, const char *, unsigned); void ts_external_scanner_state_delete(ExternalScannerState *self); void ts_subtree_array_copy(SubtreeArray, SubtreeArray *); @@ -212,7 +212,7 @@ Subtree ts_subtree_last_external_token(Subtree); const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self); bool ts_subtree_external_scanner_state_eq(Subtree, Subtree); -#define SUBTREE_GET(self, name) (self.data.is_inline ? self.data.name : self.ptr->name) +#define SUBTREE_GET(self, name) ((self).data.is_inline ? (self).data.name : (self).ptr->name) static inline TSSymbol ts_subtree_symbol(Subtree self) { return SUBTREE_GET(self, symbol); } static inline bool ts_subtree_visible(Subtree self) { return SUBTREE_GET(self, visible); } diff --git a/lib/src/tree.c b/lib/src/tree.c index 79e1d1ae..784c51fd 100644 --- a/lib/src/tree.c +++ b/lib/src/tree.c @@ -98,23 +98,23 @@ TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length) { return ranges; } -TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uint32_t *count) { +TSRange *ts_tree_get_changed_ranges(const TSTree *old_tree, const TSTree *new_tree, uint32_t *length) { TreeCursor cursor1 = {NULL, array_new()}; TreeCursor cursor2 = {NULL, array_new()}; - ts_tree_cursor_init(&cursor1, ts_tree_root_node(self)); - ts_tree_cursor_init(&cursor2, ts_tree_root_node(other)); + ts_tree_cursor_init(&cursor1, ts_tree_root_node(old_tree)); + ts_tree_cursor_init(&cursor2, ts_tree_root_node(new_tree)); TSRangeArray included_range_differences = array_new(); ts_range_array_get_changed_ranges( - self->included_ranges, self->included_range_count, - other->included_ranges, other->included_range_count, + old_tree->included_ranges, old_tree->included_range_count, + new_tree->included_ranges, new_tree->included_range_count, &included_range_differences ); TSRange *result; - *count = ts_subtree_get_changed_ranges( - &self->root, &other->root, &cursor1, &cursor2, - self->language, &included_range_differences, &result + *length = ts_subtree_get_changed_ranges( + &old_tree->root, &new_tree->root, &cursor1, &cursor2, + old_tree->language, &included_range_differences, &result ); array_delete(&included_range_differences); @@ -134,8 +134,8 @@ void ts_tree_print_dot_graph(const TSTree *self, int fd) { #include -void ts_tree_print_dot_graph(const TSTree *self, int fd) { - FILE *file = fdopen(dup(fd), "a"); +void ts_tree_print_dot_graph(const TSTree *self, int file_descriptor) { + FILE *file = fdopen(dup(file_descriptor), "a"); ts_subtree_print_dot_graph(self->root, self->language, file); fclose(file); } diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index 5383c094..97a53152 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -16,12 +16,12 @@ typedef struct { // CursorChildIterator -static inline bool ts_tree_cursor_is_entry_visible(const TreeCursor *self, uint32_t i) { - TreeCursorEntry *entry = &self->stack.contents[i]; - if (i == 0 || ts_subtree_visible(*entry->subtree)) { +static inline bool ts_tree_cursor_is_entry_visible(const TreeCursor *self, uint32_t index) { + TreeCursorEntry *entry = &self->stack.contents[index]; + if (index == 0 || ts_subtree_visible(*entry->subtree)) { return true; } else if (!ts_subtree_extra(*entry->subtree)) { - TreeCursorEntry *parent_entry = &self->stack.contents[i - 1]; + TreeCursorEntry *parent_entry = &self->stack.contents[index - 1]; return ts_language_alias_at( self->tree->language, parent_entry->subtree->ptr->production_id, @@ -444,9 +444,9 @@ void ts_tree_cursor_current_status( // Look for a field name associated with the current node. if (!*field_id) { - for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) { - if (!i->inherited && i->child_index == entry->structural_child_index) { - *field_id = i->field_id; + for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) { + if (!map->inherited && map->child_index == entry->structural_child_index) { + *field_id = map->field_id; break; } } @@ -454,10 +454,10 @@ void ts_tree_cursor_current_status( // Determine if the current node can have later siblings with the same field name. if (*field_id) { - for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) { + for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) { if ( - i->field_id == *field_id && - i->child_index > entry->structural_child_index + map->field_id == *field_id && + map->child_index > entry->structural_child_index ) { *can_have_later_siblings_with_this_field = true; break; @@ -528,9 +528,9 @@ TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *_self) { parent_entry->subtree->ptr->production_id, &field_map, &field_map_end ); - for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) { - if (!i->inherited && i->child_index == entry->structural_child_index) { - return i->field_id; + for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) { + if (!map->inherited && map->child_index == entry->structural_child_index) { + return map->field_id; } } }