fix(lib): silence warnings with -Wpedantic

This commit is contained in:
Amaan Qureshi 2024-09-27 12:19:42 -04:00
parent 0c43988a5e
commit 28972f916a
9 changed files with 62 additions and 52 deletions

View file

@ -6,8 +6,8 @@ extern "C" {
#endif
#include "./alloc.h"
#include "./ts_assert.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
@ -37,7 +37,7 @@ extern "C" {
/// Get a pointer to the element at a given `index` in the array.
#define array_get(self, _index) \
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
(ts_assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
/// Get a pointer to the first element in the array.
#define array_front(self) array_get(self, 0)
@ -171,7 +171,7 @@ static inline void _array__delete(Array *self) {
/// This is not what you're looking for, see `array_erase`.
static inline void _array__erase(Array *self, size_t element_size,
uint32_t index) {
assert(index < self->size);
ts_assert(index < self->size);
char *contents = (char *)self->contents;
memmove(contents + index * element_size, contents + (index + 1) * element_size,
(self->size - index - 1) * element_size);
@ -222,7 +222,7 @@ static inline void _array__splice(Array *self, size_t element_size,
uint32_t new_size = self->size + new_count - old_count;
uint32_t old_end = index + old_count;
uint32_t new_end = index + new_count;
assert(old_end <= self->size);
ts_assert(old_end <= self->size);
_array__reserve(self, element_size, new_size);

View file

@ -3,7 +3,7 @@
#include "./language.h"
#include "./error_costs.h"
#include "./tree_cursor.h"
#include <assert.h>
#include "./ts_assert.h"
// #define DEBUG_GET_CHANGED_RANGES

View file

@ -43,7 +43,7 @@ void ts_language_table_entry(
result->is_reusable = false;
result->actions = NULL;
} else {
assert(symbol < self->token_count);
ts_assert(symbol < self->token_count);
uint32_t action_index = ts_language_lookup(self, state, symbol);
const TSParseActionEntry *entry = &self->parse_actions[action_index];
result->action_count = entry->entry.count;

View file

@ -1,7 +1,6 @@
#define _POSIX_C_SOURCE 200112L
#include <time.h>
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
@ -21,6 +20,7 @@
#include "./stack.h"
#include "./subtree.h"
#include "./tree.h"
#include "./ts_assert.h"
#include "./wasm_store.h"
#define LOG(...) \
@ -405,7 +405,7 @@ static unsigned ts_parser__external_scanner_serialize(
self->external_scanner_payload,
self->lexer.debug_buffer
);
assert(length <= TREE_SITTER_SERIALIZATION_BUFFER_SIZE);
ts_assert(length <= TREE_SITTER_SERIALIZATION_BUFFER_SIZE);
return length;
}
}
@ -1041,7 +1041,7 @@ static void ts_parser__accept(
StackVersion version,
Subtree lookahead
) {
assert(ts_subtree_is_eof(lookahead));
ts_assert(ts_subtree_is_eof(lookahead));
ts_stack_push(self->stack, version, lookahead, false, 1);
StackSliceArray pop = ts_stack_pop_all(self->stack, version);
@ -1052,7 +1052,7 @@ static void ts_parser__accept(
for (uint32_t j = trees.size - 1; j + 1 > 0; j--) {
Subtree tree = trees.contents[j];
if (!ts_subtree_extra(tree)) {
assert(!tree.data.is_inline);
ts_assert(!tree.data.is_inline);
uint32_t child_count = ts_subtree_child_count(tree);
const Subtree *children = ts_subtree_children(tree);
for (uint32_t k = 0; k < child_count; k++) {
@ -1070,7 +1070,7 @@ static void ts_parser__accept(
}
}
assert(root.ptr);
ts_assert(root.ptr);
self->accept_count++;
if (self->finished_tree.ptr) {
@ -1206,7 +1206,7 @@ static bool ts_parser__recover_to_state(
SubtreeArray error_trees = ts_stack_pop_error(self->stack, slice.version);
if (error_trees.size > 0) {
assert(error_trees.size == 1);
ts_assert(error_trees.size == 1);
Subtree error_tree = error_trees.contents[0];
uint32_t error_child_count = ts_subtree_child_count(error_tree);
if (error_child_count > 0) {
@ -1494,8 +1494,7 @@ static void ts_parser__handle_error(
for (unsigned i = previous_version_count; i < version_count; i++) {
bool did_merge = ts_stack_merge(self->stack, version, previous_version_count);
assert(did_merge);
(void)did_merge; // fix warning/error with clang -Os
ts_assert(did_merge);
}
ts_stack_record_summary(self->stack, version, MAX_SUMMARY_DEPTH);
@ -2101,7 +2100,7 @@ TSTree *ts_parser_parse(
}
} while (version_count != 0);
assert(self->finished_tree.ptr);
ts_assert(self->finished_tree.ptr);
ts_subtree_balance(self->finished_tree, &self->tree_pool, self->language);
LOG("done");
LOG_TREE(self->finished_tree);

View file

@ -442,7 +442,7 @@ static const CaptureList *capture_list_pool_get(const CaptureListPool *self, uin
}
static CaptureList *capture_list_pool_get_mut(CaptureListPool *self, uint16_t id) {
assert(id < self->list.size);
ts_assert(id < self->list.size);
return &self->list.contents[id];
}
@ -1695,7 +1695,7 @@ static bool ts_query__analyze_patterns(TSQuery *self, unsigned *error_offset) {
unsigned first_child_step_index = parent_step_index + 1;
uint32_t j, child_exists;
array_search_sorted_by(&self->step_offsets, .step_index, first_child_step_index, &j, &child_exists);
assert(child_exists);
ts_assert(child_exists);
*error_offset = self->step_offsets.contents[j].byte_offset;
all_patterns_are_valid = false;
break;
@ -1754,7 +1754,7 @@ static bool ts_query__analyze_patterns(TSQuery *self, unsigned *error_offset) {
// If this pattern cannot match, store the pattern index so that it can be
// returned to the caller.
if (analysis.finished_parent_symbols.size == 0) {
assert(analysis.final_step_indices.size > 0);
ts_assert(analysis.final_step_indices.size > 0);
uint16_t impossible_step_index = *array_back(&analysis.final_step_indices);
uint32_t j, impossible_exists;
array_search_sorted_by(&self->step_offsets, .step_index, impossible_step_index, &j, &impossible_exists);
@ -2932,7 +2932,7 @@ bool ts_query__step_is_fallible(
const TSQuery *self,
uint16_t step_index
) {
assert((uint32_t)step_index + 1 < self->steps.size);
ts_assert((uint32_t)step_index + 1 < self->steps.size);
QueryStep *step = &self->steps.contents[step_index];
QueryStep *next_step = &self->steps.contents[step_index + 1];
return (

View file

@ -82,9 +82,9 @@ typedef StackAction (*StackCallback)(void *, const StackIterator *);
static void stack_node_retain(StackNode *self) {
if (!self)
return;
assert(self->ref_count > 0);
ts_assert(self->ref_count > 0);
self->ref_count++;
assert(self->ref_count != 0);
ts_assert(self->ref_count != 0);
}
static void stack_node_release(
@ -93,7 +93,7 @@ static void stack_node_release(
SubtreePool *subtree_pool
) {
recur:
assert(self->ref_count != 0);
ts_assert(self->ref_count != 0);
self->ref_count--;
if (self->ref_count > 0) return;
@ -567,7 +567,7 @@ SubtreeArray ts_stack_pop_error(Stack *self, StackVersion version) {
bool found_error = false;
StackSliceArray pop = stack__iter(self, version, pop_error_callback, &found_error, 1);
if (pop.size > 0) {
assert(pop.size == 1);
ts_assert(pop.size == 1);
ts_stack_renumber_version(self, pop.contents[0].version, version);
return pop.contents[0].subtrees;
}
@ -663,8 +663,8 @@ void ts_stack_remove_version(Stack *self, StackVersion version) {
void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2) {
if (v1 == v2) return;
assert(v2 < v1);
assert((uint32_t)v1 < self->heads.size);
ts_assert(v2 < v1);
ts_assert((uint32_t)v1 < self->heads.size);
StackHead *source_head = &self->heads.contents[v1];
StackHead *target_head = &self->heads.contents[v2];
if (target_head->summary && !source_head->summary) {
@ -683,7 +683,7 @@ void ts_stack_swap_versions(Stack *self, StackVersion v1, StackVersion v2) {
}
StackVersion ts_stack_copy_version(Stack *self, StackVersion version) {
assert(version < self->heads.size);
ts_assert(version < self->heads.size);
array_push(&self->heads, self->heads.contents[version]);
StackHead *head = array_back(&self->heads);
stack_node_retain(head->node);
@ -743,7 +743,7 @@ bool ts_stack_is_paused(const Stack *self, StackVersion version) {
Subtree ts_stack_resume(Stack *self, StackVersion version) {
StackHead *head = array_get(&self->heads, version);
assert(head->status == StackStatusPaused);
ts_assert(head->status == StackStatusPaused);
Subtree result = head->lookahead_when_paused;
head->status = StackStatusActive;
head->lookahead_when_paused = NULL_SUBTREE;

View file

@ -1,4 +1,3 @@
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <stdbool.h>
@ -11,6 +10,7 @@
#include "./length.h"
#include "./language.h"
#include "./error_costs.h"
#include "./ts_assert.h"
#include <stddef.h>
typedef struct {
@ -229,7 +229,7 @@ void ts_subtree_set_symbol(
) {
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol);
if (self->data.is_inline) {
assert(symbol < UINT8_MAX);
ts_assert(symbol < UINT8_MAX);
self->data.symbol = symbol;
self->data.named = metadata.named;
self->data.visible = metadata.visible;
@ -371,7 +371,7 @@ void ts_subtree_summarize_children(
MutableSubtree self,
const TSLanguage *language
) {
assert(!self.data.is_inline);
ts_assert(!self.data.is_inline);
self.ptr->named_child_count = 0;
self.ptr->visible_child_count = 0;
@ -583,16 +583,16 @@ Subtree ts_subtree_new_missing_leaf(
void ts_subtree_retain(Subtree self) {
if (self.data.is_inline) return;
assert(self.ptr->ref_count > 0);
ts_assert(self.ptr->ref_count > 0);
atomic_inc((volatile uint32_t *)&self.ptr->ref_count);
assert(self.ptr->ref_count != 0);
ts_assert(self.ptr->ref_count != 0);
}
void ts_subtree_release(SubtreePool *pool, Subtree self) {
if (self.data.is_inline) return;
array_clear(&pool->tree_stack);
assert(self.ptr->ref_count > 0);
ts_assert(self.ptr->ref_count > 0);
if (atomic_dec((volatile uint32_t *)&self.ptr->ref_count) == 0) {
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self));
}
@ -604,7 +604,7 @@ void ts_subtree_release(SubtreePool *pool, Subtree self) {
for (uint32_t i = 0; i < tree.ptr->child_count; i++) {
Subtree child = children[i];
if (child.data.is_inline) continue;
assert(child.ptr->ref_count > 0);
ts_assert(child.ptr->ref_count > 0);
if (atomic_dec((volatile uint32_t *)&child.ptr->ref_count) == 0) {
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(child));
}

11
lib/src/ts_assert.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef TREE_SITTER_ASSERT_H_
#define TREE_SITTER_ASSERT_H_
#ifdef NDEBUG
#define ts_assert(e) ((void)(e))
#else
#include <assert.h>
#define ts_assert(e) assert(e)
#endif
#endif // TREE_SITTER_ASSERT_H_

View file

@ -258,7 +258,7 @@ static wasm_trap_t *callback__debug_message(
) {
wasmtime_context_t *context = wasmtime_caller_context(caller);
TSWasmStore *store = env;
assert(args_and_results_len == 2);
ts_assert(args_and_results_len == 2);
uint32_t string_address = args_and_results[0].i32;
uint32_t value = args_and_results[1].i32;
uint8_t *memory = wasmtime_memory_data(context, &store->memory);
@ -282,7 +282,7 @@ static wasm_trap_t *callback__lexer_advance(
size_t args_and_results_len
) {
wasmtime_context_t *context = wasmtime_caller_context(caller);
assert(args_and_results_len == 2);
ts_assert(args_and_results_len == 2);
TSWasmStore *store = env;
TSLexer *lexer = store->current_lexer;
@ -432,7 +432,7 @@ static inline wasm_functype_t* wasm_functype_new_4_0(
snprintf(*output, message_length + 1, __VA_ARGS__); \
} while (0)
WasmLanguageId *language_id_new() {
WasmLanguageId *language_id_new(void) {
WasmLanguageId *self = ts_malloc(sizeof(WasmLanguageId));
self->is_language_deleted = false;
self->ref_count = 1;
@ -476,13 +476,13 @@ static bool ts_wasm_store__provide_builtin_import(
wasmtime_val_t value = WASM_I32_VAL(self->current_memory_offset);
wasmtime_global_t global;
error = wasmtime_global_new(context, self->const_i32_type, &value, &global);
assert(!error);
ts_assert(!error);
*import = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_GLOBAL, .of.global = global};
} else if (name_eq(import_name, "__table_base")) {
wasmtime_val_t value = WASM_I32_VAL(self->current_function_table_offset);
wasmtime_global_t global;
error = wasmtime_global_new(context, self->const_i32_type, &value, &global);
assert(!error);
ts_assert(!error);
*import = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_GLOBAL, .of.global = global};
} else if (name_eq(import_name, "__stack_pointer")) {
*import = (wasmtime_extern_t) {.kind = WASMTIME_EXTERN_GLOBAL, .of.global = self->stack_pointer_global};
@ -530,7 +530,7 @@ static bool ts_wasm_store__call_module_initializer(
wasmtime_context_t *context = wasmtime_store_context(self->store);
wasmtime_func_t initialization_func = export->of.func;
wasmtime_error_t *error = wasmtime_func_call(context, &initialization_func, NULL, 0, NULL, 0, trap);
assert(!error);
ts_assert(!error);
return true;
} else {
return false;
@ -729,7 +729,7 @@ TSWasmStore *ts_wasm_store_new(TSWasmEngine *engine, TSWasmError *wasm_error) {
wasmtime_val_t stack_pointer_value = WASM_I32_VAL(0);
wasmtime_global_t stack_pointer_global;
error = wasmtime_global_new(context, var_i32_type, &stack_pointer_value, &stack_pointer_global);
assert(!error);
ts_assert(!error);
*self = (TSWasmStore) {
.engine = wasmtime_engine_clone(engine),
@ -801,7 +801,7 @@ TSWasmStore *ts_wasm_store_new(TSWasmEngine *engine, TSWasmError *wasm_error) {
size_t name_len;
wasmtime_extern_t export = {.kind = WASM_EXTERN_GLOBAL};
bool exists = wasmtime_instance_export_nth(context, &instance, i, &export_name, &name_len, &export);
assert(exists);
ts_assert(exists);
if (export.kind == WASMTIME_EXTERN_GLOBAL) {
if (name_eq(name, "__stack_pointer")) {
@ -881,7 +881,7 @@ TSWasmStore *ts_wasm_store_new(TSWasmEngine *engine, TSWasmError *wasm_error) {
wasmtime_func_t func = {function_table.store_id, *definition->storage_location};
wasmtime_val_t func_val = {.kind = WASMTIME_FUNCREF, .of.funcref = func};
error = wasmtime_table_set(context, &function_table, table_index, &func_val);
assert(!error);
ts_assert(!error);
*(int32_t *)(definition->storage_location) = table_index;
table_index++;
}
@ -1069,7 +1069,7 @@ static bool ts_wasm_store__instantiate(
char *export_name;
wasmtime_extern_t export = {.kind = WASM_EXTERN_GLOBAL};
bool exists = wasmtime_instance_export_nth(context, &instance, i, &export_name, &name_len, &export);
assert(exists);
ts_assert(exists);
// If the module exports an initialization or data-relocation function, call it.
if (ts_wasm_store__call_module_initializer(self, name, &export, &trap)) {
@ -1105,7 +1105,7 @@ static bool ts_wasm_store__instantiate(
wasmtime_func_t language_func = language_extern.of.func;
wasmtime_val_t language_address_val;
error = wasmtime_func_call(context, &language_func, NULL, 0, &language_address_val, 1, &trap);
assert(!error);
ts_assert(!error);
if (trap) {
wasm_trap_message(trap, &message);
format(
@ -1378,7 +1378,7 @@ const TSLanguage *ts_wasm_store_load_language(
// to mark this language as WASM-based and to store the language's
// WASM-specific data.
language->lex_fn = ts_wasm_store__sentinel_lex_fn;
language->keyword_lex_fn = (void *)language_module;
language->keyword_lex_fn = (bool (*)(TSLexer *, TSStateId))language_module;
// Clear out any instances of languages that have been deleted.
for (unsigned i = 0; i < self->language_instances.size; i++) {
@ -1486,8 +1486,8 @@ void ts_wasm_store_reset_heap(TSWasmStore *self) {
};
wasmtime_error_t *error = wasmtime_func_call(context, &func, args, 1, NULL, 0, &trap);
assert(!error);
assert(!trap);
ts_assert(!error);
ts_assert(!trap);
}
bool ts_wasm_store_start(TSWasmStore *self, TSLexer *lexer, const TSLanguage *language) {
@ -1516,8 +1516,8 @@ static void ts_wasm_store__call(
wasmtime_context_t *context = wasmtime_store_context(self->store);
wasmtime_val_t value;
bool succeeded = wasmtime_table_get(context, &self->function_table, function_index, &value);
assert(succeeded);
assert(value.kind == WASMTIME_FUNCREF);
ts_assert(succeeded);
ts_assert(value.kind == WASMTIME_FUNCREF);
wasmtime_func_t func = value.of.funcref;
wasm_trap_t *trap = NULL;
@ -1705,13 +1705,13 @@ static inline LanguageWasmModule *ts_language__wasm_module(const TSLanguage *sel
void ts_wasm_language_retain(const TSLanguage *self) {
LanguageWasmModule *module = ts_language__wasm_module(self);
assert(module->ref_count > 0);
ts_assert(module->ref_count > 0);
atomic_inc(&module->ref_count);
}
void ts_wasm_language_release(const TSLanguage *self) {
LanguageWasmModule *module = ts_language__wasm_module(self);
assert(module->ref_count > 0);
ts_assert(module->ref_count > 0);
if (atomic_dec(&module->ref_count) == 0) {
// Update the language id to reflect that the language is deleted. This allows any wasm stores
// that hold wasm instances for this language to delete those instances.