From 04b367e7a027f237199b20f243c62ac4339adc88 Mon Sep 17 00:00:00 2001 From: samsonium Date: Mon, 19 Oct 2026 18:39:40 +0300 Subject: [PATCH] fix: warnings fix (after -Wall -Wextra) --- CMakeLists.txt | 6 +++ include/compiler/utils/fzl_const_pool.h | 8 ++-- src/compiler/fzl_debug.c | 1 - src/compiler/fzl_parser.c | 28 ++++++-------- src/compiler/fzl_reader.c | 2 +- src/compiler/fzl_walker.c | 9 ++--- src/compiler/utils/fzl_const_pool.c | 50 ++++++++++++------------- src/compiler/utils/fzl_linked.c | 4 +- src/compiler/utils/fzl_options.c | 4 +- src/vm/fzl_vm.c | 11 +++++- src/vm/fzl_vm_env.c | 2 - src/vm/main.c | 2 +- 12 files changed, 66 insertions(+), 61 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db0aab3..f5a00e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,12 @@ project(fz-lang LANGUAGES C) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +add_compile_options(-Wall -Wextra -Werror=implicit-function-declaration) + +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + add_compile_options(-fsanitize=address,undefined -g) + add_link_options(-fsanitize=address,undefined) +endif() include_directories(include) diff --git a/include/compiler/utils/fzl_const_pool.h b/include/compiler/utils/fzl_const_pool.h index 0d1b128..f2f3b99 100644 --- a/include/compiler/utils/fzl_const_pool.h +++ b/include/compiler/utils/fzl_const_pool.h @@ -4,12 +4,12 @@ #include #include #include +#include // Constant types typedef enum { FZL_CONSTTYPE_STRING, - FZL_CONSTTYPE_NUMBER } fzl_consttype_t; // Constant descriptor @@ -34,7 +34,7 @@ typedef struct typedef struct { - size_t pool_index; + ssize_t pool_index; } fzl_const_hash_slot_t; // Constant pool descriptor @@ -53,13 +53,13 @@ typedef struct bool fzl_consts_equal(fzl_const_t a, fzl_const_t b); // Get hash of specified constant -static uint32_t fzl_hash_const(fzl_const_t c); +uint32_t fzl_hash_const(fzl_const_t c); // Allocate constants hash pool fzl_const_hash_pool_t *fzl_mkconstpool(); // Resize the hash table -static void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool); +void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool); // Push constant. Returns unique index size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c); diff --git a/src/compiler/fzl_debug.c b/src/compiler/fzl_debug.c index 2a78659..cbab185 100644 --- a/src/compiler/fzl_debug.c +++ b/src/compiler/fzl_debug.c @@ -27,7 +27,6 @@ void print_node(fzl_node_t *node, int level) if (!node) return; - size_t lvllen = print_level(levelbuf, level); strcpy(printbuf, levelbuf); switch (node->type) diff --git a/src/compiler/fzl_parser.c b/src/compiler/fzl_parser.c index b7c9b9f..5ce31bc 100644 --- a/src/compiler/fzl_parser.c +++ b/src/compiler/fzl_parser.c @@ -56,19 +56,6 @@ static fzl_token_t *peek(fzl_parser_t *parser) return parser->cursor->token; } -// Get token at (cursor + 1) -static fzl_token_t *next(fzl_parser_t *parser) -{ - if (!parser || !parser->cursor) - return NULL; - - fzl_reader_ll_t *next = parser->cursor->next; - if (!next) - return NULL; - - return next->token; -} - // Consume token at cursor and move to the next static fzl_token_t *consume(fzl_parser_t *parser) { @@ -204,8 +191,7 @@ fzl_node_t *parse_postfix(fzl_parser_t *parser) if (!parser) return NULL; - fzl_node_t *left = parse_primary(parser); - fzl_node_t *right = NULL; + fzl_node_t *left = parse_primary(parser); if (!left) { @@ -766,7 +752,15 @@ fzl_node_t *parse_function(fzl_parser_t *parser) if (!parser) return NULL; - fzl_token_t *fn = consume(parser); + fzl_token_t *fn = consume(parser); + if (fn->type != FZL_TOKTYPE_FUNCTION) + { + snprintf(plast_err, sizeof(plast_err), + "'%s': expected keyword 'fn' at %ld:%ld", last_known_tok->filename, + last_known_tok->ln, last_known_tok->cn); + return NULL; + } + fzl_token_t *name = consume(parser); if (!name || name->type != FZL_TOKTYPE_NAME) { @@ -1131,7 +1125,7 @@ void fzl_freeast(fzl_node_t *root) fzl_node_t *next = root->next; free(root); - root = root->next; + root = next; } } diff --git a/src/compiler/fzl_reader.c b/src/compiler/fzl_reader.c index 683fb3f..9f5cc70 100644 --- a/src/compiler/fzl_reader.c +++ b/src/compiler/fzl_reader.c @@ -313,7 +313,7 @@ int fzl_read(fzl_reader_t *reader) token = add_single(reader, FZL_TOKTYPE_PERCENT); else if (curr == '=') token = add_single(reader, FZL_TOKTYPE_EQ); - else if (curr >= 'A' && curr <= 'Z' || curr >= 'a' && curr <= 'z' || + else if ((curr >= 'A' && curr <= 'Z') || (curr >= 'a' && curr <= 'z') || curr == '_') token = parse_name(reader); else if (curr >= '0' && curr <= '9') diff --git a/src/compiler/fzl_walker.c b/src/compiler/fzl_walker.c index a99af45..0875399 100644 --- a/src/compiler/fzl_walker.c +++ b/src/compiler/fzl_walker.c @@ -64,7 +64,7 @@ fzl_walker_t *fzl_mkwalker(fzl_node_t *root) // --- Walking zone --- // Convert type token to `fzl_type_t` -const fzl_type_t tok_to_type(fzl_token_t *tok) +fzl_type_t tok_to_type(fzl_token_t *tok) { if (tok->type == FZL_TOKTYPE_NAME) return FZL_TYPE_OBJECT; @@ -403,7 +403,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node) } // NOTE: add sym_top limit check - size_t lvl = walker->sym_top; + ssize_t lvl = walker->sym_top; walker->syms[walker->sym_top++] = fzl_mksymt(); fzl_walk_ctx_t *newctx = nest_ctx(walker, FZL_WALK_CTX_TYPE_FN); @@ -939,7 +939,6 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node) } else if (node->token->type == FZL_TOKTYPE_NULL_LITERAL) { - fzl_token_t *tok = node->token; push_op(walker, FZL_OPCODE_PUSHNUM); push_word(walker, 0); push_op(walker, FZL_OPCODE_DEREFERENCE); @@ -1145,7 +1144,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node) if (obj_res.error) return obj_res; - fzl_ctable_t *ct; + fzl_ctable_t *ct = NULL; for (size_t i = 0; i < walker->classes_count; i++) { if (strcmp(obj_res.classname, walker->classes[i]->name) == 0) @@ -1164,7 +1163,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node) return (fzl_walk_res_t){.error = true, .error_msg = wlast_err}; } - fzl_ctable_field_t *ctf; + fzl_ctable_field_t *ctf = NULL; for (size_t i = 0; i < ct->fields_count; i++) { if (strcmp(field_node->memb.field->token->value, diff --git a/src/compiler/utils/fzl_const_pool.c b/src/compiler/utils/fzl_const_pool.c index 39c4c28..e2dbbe6 100644 --- a/src/compiler/utils/fzl_const_pool.c +++ b/src/compiler/utils/fzl_const_pool.c @@ -11,31 +11,31 @@ bool fzl_consts_equal(fzl_const_t a, fzl_const_t b) switch (a.type) { - case FZL_CONSTTYPE_STRING: - { - if (a.as.string.length != b.as.string.length) - return false; + case FZL_CONSTTYPE_STRING: + { + if (a.as.string.length != b.as.string.length) + return false; - return memcmp(a.as.string.chars, b.as.string.chars, a.as.string.length) == - 0; - } + return memcmp(a.as.string.chars, b.as.string.chars, a.as.string.length) == + 0; + } } return false; } -static uint32_t fzl_hash_const(fzl_const_t c) +uint32_t fzl_hash_const(fzl_const_t c) { - uint32_t hash = 2166136261u; + uint32_t hash = 2166136261u; const uint8_t *data = NULL; - size_t size = 0; + size_t size = 0; switch (c.type) { - case FZL_CONSTTYPE_STRING: - data = (const uint8_t *)c.as.string.chars; - size = c.as.string.length; - break; + case FZL_CONSTTYPE_STRING: + data = (const uint8_t *)c.as.string.chars; + size = c.as.string.length; + break; } for (size_t i = 0; i < size; i++) @@ -51,27 +51,27 @@ fzl_const_hash_pool_t *fzl_mkconstpool() { fzl_const_hash_pool_t *pool = malloc(sizeof(fzl_const_hash_pool_t)); - pool->size = 0; + pool->size = 0; pool->capacity = 8; - pool->array = malloc(pool->capacity * sizeof(fzl_const_t)); + pool->array = malloc(pool->capacity * sizeof(fzl_const_t)); - pool->slots_count = 0; + pool->slots_count = 0; pool->slots_capacity = 16; pool->slots = malloc(pool->slots_capacity * sizeof(fzl_const_hash_slot_t)); - for (int i = 0; i < pool->slots_capacity; i++) + for (size_t i = 0; i < pool->slots_capacity; i++) pool->slots[i].pool_index = -1; return pool; } -static void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool) +void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool) { - int old_capacity = pool->slots_capacity; + int old_capacity = pool->slots_capacity; fzl_const_hash_slot_t *old_slots = pool->slots; pool->slots_capacity = old_capacity * 2; pool->slots = malloc(pool->slots_capacity * sizeof(fzl_const_hash_slot_t)); - for (int i = 0; i < pool->slots_capacity; i++) + for (size_t i = 0; i < pool->slots_capacity; i++) pool->slots[i].pool_index = -1; pool->slots_count = 0; @@ -81,8 +81,8 @@ static void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool) continue; int index_in_pool = old_slots[i].pool_index; - fzl_const_t c = pool->array[index_in_pool]; - uint32_t hash = fzl_hash_const(c); + fzl_const_t c = pool->array[index_in_pool]; + uint32_t hash = fzl_hash_const(c); uint32_t bucket = hash & (pool->slots_capacity - 1); while (pool->slots[bucket].pool_index != -1) @@ -100,7 +100,7 @@ size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c) if (pool->slots_count * 4 >= pool->slots_capacity * 3) fzl_consts_pool_resize_hash(pool); - uint32_t hash = fzl_hash_const(c); + uint32_t hash = fzl_hash_const(c); uint32_t bucket = hash & (pool->slots_capacity - 1); while (pool->slots[bucket].pool_index != -1) @@ -119,7 +119,7 @@ size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c) pool->capacity * sizeof(fzl_const_t)); } - size_t new_index = pool->size; + size_t new_index = pool->size; pool->array[new_index] = c; pool->size++; diff --git a/src/compiler/utils/fzl_linked.c b/src/compiler/utils/fzl_linked.c index f7414eb..feb2887 100644 --- a/src/compiler/utils/fzl_linked.c +++ b/src/compiler/utils/fzl_linked.c @@ -72,9 +72,9 @@ bool fzl_linked_add_files(fzl_linked_t *l, char **files, size_t files_count) .end = current_start + file_sz - 1}; if (!l->source) - l->source = malloc(file_sz); + l->source = malloc(file_sz + 1); else - l->source = realloc(l->source, current_start + file_sz); + l->source = realloc(l->source, current_start + file_sz + 1); if (!l->source) { diff --git a/src/compiler/utils/fzl_options.c b/src/compiler/utils/fzl_options.c index 4060189..e91ab29 100644 --- a/src/compiler/utils/fzl_options.c +++ b/src/compiler/utils/fzl_options.c @@ -35,7 +35,7 @@ fzl_options_t *fzl_mkoptions(int argc, char **argv) return opts; } - size_t i; + int i; for (i = 1; i < argc; i++) { if (argv[i][0] != '-') @@ -108,7 +108,7 @@ fzl_options_t *fzl_mkoptions(int argc, char **argv) { printf("%sError%s: flags should go before source files list.\n", FZL_CERROR, FZL_CRST); - printf("Remain %zu arguments are not handled.\n", argc - 1 - i); + printf("Remain %d arguments are not handled.\n", argc - 1 - i); free(files); free(opts); diff --git a/src/vm/fzl_vm.c b/src/vm/fzl_vm.c index 3b13ee7..4dcce34 100644 --- a/src/vm/fzl_vm.c +++ b/src/vm/fzl_vm.c @@ -650,8 +650,17 @@ int fzl_vm_run(fzl_vm_t *vm) break; case FZL_OPCODE_JMP: - vm->ip = vm->ops[++vm->ip] - 1; + { + if (vm->ops_count >= vm->ip) + { + snprintf(last_err, sizeof(last_err), + "runtime error - expected JMP addr"); + return -1; + } + uint64_t jmp_ip = vm->ops[++vm->ip]; + vm->ip = jmp_ip - 1; break; + } case FZL_OPCODE_AND: if (vm->dtop < 2) diff --git a/src/vm/fzl_vm_env.c b/src/vm/fzl_vm_env.c index a7c98e6..533dc4b 100644 --- a/src/vm/fzl_vm_env.c +++ b/src/vm/fzl_vm_env.c @@ -7,8 +7,6 @@ #include #include -static const fzl_value_t null = {.type = FZL_TYPE_NULL}; - fzl_vm_env_t *fzl_mkenv() { fzl_vm_env_t *env = malloc(sizeof(fzl_vm_env_t)); diff --git a/src/vm/main.c b/src/vm/main.c index 55c85d4..cb357c0 100644 --- a/src/vm/main.c +++ b/src/vm/main.c @@ -33,7 +33,7 @@ run_args_t *parse_args(int argc, char **argv) args->help = false; args->debug = false; - for (size_t i = 0; i < argc; i++) + for (int i = 0; i < argc; i++) { if (argv[i][0] != '-') continue;