diff --git a/src/build/build_options.c b/src/build/build_options.c index 4f53aab6e..27466beb2 100644 --- a/src/build/build_options.c +++ b/src/build/build_options.c @@ -1381,11 +1381,8 @@ ArchOsTarget arch_os_target_from_string(const char *target) static const char *check_dir(const char *path) { - static char *original_path = NULL; - if (!original_path) - { - original_path = getcwd(NULL, 0); - } + char original_path[PATH_MAX + 1]; + if (!getcwd(original_path, PATH_MAX)) error_exit("Failed to store path."); if (!dir_change(path)) error_exit("The path \"%s\" does not point to a valid directory.", path); if (!dir_change(original_path)) FAIL_WITH_ERR("Failed to change path to %s.", original_path); return path; diff --git a/src/build/project_creation.c b/src/build/project_creation.c index fc4f795f4..e0c633e8c 100644 --- a/src/build/project_creation.c +++ b/src/build/project_creation.c @@ -382,20 +382,23 @@ static bool check_name(const char *name) static char* get_cwd_project_name() { - char *full_path = getcwd(NULL, 0); + char full_path[PATH_MAX]; + if (!getcwd(full_path, PATH_MAX)) return NULL; size_t len = strlen(full_path); - for (size_t i = len; i > 0; i--) + for (size_t i = len - 1; i > 0; i--) { switch (full_path[i]) { - case '/': #if PLATFORM_WINDOWS case '\\': #endif - return &full_path[i + 1]; + case '/': + return str_copy(&full_path[i + 1], len - i - 1); + default: + break; } } - return full_path; + return str_copy(full_path, len); } static void chdir_or_fail(BuildOptions *build_options, const char *name) diff --git a/src/compiler/bigint.c b/src/compiler/bigint.c index 40613a77d..7fe9a636d 100644 --- a/src/compiler/bigint.c +++ b/src/compiler/bigint.c @@ -3,7 +3,6 @@ // a copy of which can be found in the LICENSE file. #include "compiler_internal.h" -#include #include UNUSED static inline uint32_t u32_min(uint32_t a, uint32_t b) @@ -116,11 +115,10 @@ Int128 i128_from_str(const char *str) UNUSED Int128 i128_from_strl(const char *str, const char *end) { - char c; Int128 x = { 0, 0 }; while (str != end) { - c = *(str++); + char c = *(str++); x = i128_add64(i128_mult64(x, 10), (uint64_t) (c - '0')); } return x; @@ -128,11 +126,10 @@ UNUSED Int128 i128_from_strl(const char *str, const char *end) UNUSED Int128 i128_from_hexstrl(const char *str, const char *end) { - char c; Int128 x = { 0, 0 }; while (str != end) { - c = *(str++); + char c = *(str++); x = i128_add64(i128_shl64(x, 4), (uint64_t)char_hex_to_nibble(c)); } return x; @@ -198,7 +195,7 @@ Int128 i128_xor(Int128 op1, Int128 op2) Int128 i128_neg(Int128 op1) { - if (!op1.low && !op1.low) return op1; + if (!op1.low && !op1.high) return op1; return i128_add64(i128_not(op1), 1); } @@ -323,7 +320,7 @@ Int128 i128_from_float_unsigned(Real d) UNUSED bool i128_get_bit(const Int128 *op, int bit) { ASSERT(bit < 128 && bit >= 0); - if (bit > 63) + if (bit > 63) // NOLINT { return (op->high >> (bit - 64)) & 1; } @@ -442,9 +439,9 @@ static uint32_t ctz64(uint64_t n) return c; } -uint32_t i128_ctz(const Int128 *n) +uint32_t i128_ctz(const Int128 *op) { - return !n->low ? ctz64(n->high) + 64 : ctz64(n->low); + return !op->low ? ctz64(op->high) + 64 : ctz64(op->low); } static uint32_t clz64(uint64_t n) @@ -886,13 +883,13 @@ Int int_shr64(Int op1, uint64_t op2) return (Int){ i128_extend(i128_ashr64(op1.i, op2), op1.type), op1.type }; } -Int int_shl64(Int op1, uint64_t op2) +Int int_shl64(Int op, uint64_t op2) { - if (type_kind_is_unsigned(op1.type)) + if (type_kind_is_unsigned(op.type)) { - return (Int){ i128_extend(i128_shl64(op1.i, op2), op1.type), op1.type }; + return (Int){ i128_extend(i128_shl64(op.i, op2), op.type), op.type }; } - return (Int){ i128_extend(i128_shl64(op1.i, op2), op1.type), op1.type }; + return (Int){ i128_extend(i128_shl64(op.i, op2), op.type), op.type }; } Real int_to_real(Int op) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index e44efd4a3..63978ff30 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -6,9 +6,6 @@ #include "../build/project.h" #include #include "../utils/whereami.h" -#if PLATFORM_POSIX -#include -#endif #if LLVM_AVAILABLE #include "c3_llvm.h" #endif @@ -347,6 +344,7 @@ void delete_object_files(const char **files, size_t count) { for (size_t i = 0; i < count; i++) { + assert(files); file_delete_file(files[i]); } } @@ -479,6 +477,7 @@ void compiler_compile(void) { case BACKEND_C: gen_contexts = c_gen(modules, module_count); + (void)gen_contexts; error_exit("Unfinished C backend!"); case BACKEND_LLVM: #if LLVM_AVAILABLE @@ -936,7 +935,6 @@ void print_syntax(BuildOptions *options) if (options->print_keywords) { - int index = 1; for (int i = 1; i < TOKEN_LAST; i++) { const char *name = token_type_to_string((TokenType)i); @@ -949,7 +947,6 @@ void print_syntax(BuildOptions *options) } if (options->print_operators) { - int index = 1; for (int i = 1; i < TOKEN_LAST; i++) { if (i == TOKEN_DOCS_START || i == TOKEN_DOCS_END) continue; @@ -991,7 +988,6 @@ void print_syntax(BuildOptions *options) } if (options->print_project_properties) { - int width; puts("Project properties"); puts("------------------"); for (int i = 0; i < project_default_keys_count; i++) @@ -1009,7 +1005,6 @@ void print_syntax(BuildOptions *options) } if (options->print_manifest_properties) { - int width; puts("Manifest properties"); puts("------------------"); for (int i = 0; i < manifest_default_keys_count; i++) @@ -1035,10 +1030,10 @@ void print_syntax(BuildOptions *options) puts(" 4. Mult | * / %"); puts(" 5. Shift | << >>"); puts(" 6. Bitwise | ^ | &"); - puts(" 7. Additive | + -"); + puts(" 7. Additive | + - +++"); puts(" 8. Relational | < > <= >= == !="); - puts(" 9. And | &&"); - puts("10. Or | ||"); + puts(" 9. And | && &&&"); + puts("10. Or | || |||"); puts("11. Ternary | ?: ??"); puts("12. Assign | = *= /= %= -= += |= &= ^= <<= >>="); } @@ -1111,13 +1106,11 @@ void execute_scripts(void) { error_exit("This target has 'exec' directives, to run it trust level must be set to '--trust=full'."); } - char *old_path = NULL; + char old_path[PATH_MAX + 1]; if (compiler.build.script_dir) { - old_path = getcwd(NULL, 0); - if (!dir_change(compiler.build.script_dir)) + if (getcwd(old_path, PATH_MAX) && !dir_change(compiler.build.script_dir)) { - free(old_path); error_exit("Failed to open script dir '%s'", compiler.build.script_dir); } } @@ -1146,7 +1139,6 @@ PRINT_SCRIPT:; } } dir_change(old_path); - free(old_path); } static void check_sanitizer_options(BuildTarget *target) diff --git a/src/compiler/sema_initializers.c b/src/compiler/sema_initializers.c index 69ae82f5e..f9702c556 100644 --- a/src/compiler/sema_initializers.c +++ b/src/compiler/sema_initializers.c @@ -824,13 +824,10 @@ bool sema_expr_analyse_initializer_list(SemaContext *context, Type *to, Expr *ex expr->type = to; return true; } - else - { - expr->resolve_status = RESOLVE_DONE; - expr_insert_addr(expr); - if (!sema_analyse_expr(context, expr)) return false; - return cast_explicit(context, expr, to); - } + expr->resolve_status = RESOLVE_DONE; + expr_insert_addr(expr); + if (!sema_analyse_expr(context, expr)) return false; + return cast_explicit(context, expr, to); } case TYPE_POINTER: case TYPE_FUNC_PTR: @@ -1049,12 +1046,12 @@ static inline void sema_update_const_initializer_with_designator_union(ConstInit } static inline ConstInitializer *sema_update_const_initializer_at_index(ConstInitializer *const_init, Type *element_type, - ArraySize array_count, ArrayIndex index, - ArrayIndex *insert_index_ref, Expr *value) + ArrayIndex index, + ArrayIndex *insert_index_ref) { ConstInitializer **array_elements = const_init->init_array.elements; ArrayIndex insert_index = *insert_index_ref; - ASSERT(insert_index >= array_count || array_elements); + ArrayIndex array_count = vec_size(array_elements); // Walk to the insert point or until we reached the end of the array. while (insert_index < array_count && array_elements[insert_index]->init_array_value.index < index) { @@ -1062,7 +1059,6 @@ static inline ConstInitializer *sema_update_const_initializer_at_index(ConstInit } // Pick up the initializer at the insert point. ConstInitializer *initializer = insert_index < array_count ? array_elements[insert_index] : NULL; - ConstInitializer *inner_value; // If we don't have an initializer, the location needs to be at the end. // Create and append: @@ -1111,14 +1107,8 @@ void const_init_rewrite_array_at(ConstInitializer *const_init, Expr *value, Arra Type *element_type = type_flatten(const_init->type->array.base); - // Get all the elements in the array - ConstInitializer **array_elements = const_init->init_array.elements; - - unsigned array_count = vec_size(array_elements); ArrayIndex insert_index = 0; - - ConstInitializer *inner_value = sema_update_const_initializer_at_index(const_init, element_type, - array_count, index, &insert_index, value); + ConstInitializer *inner_value = sema_update_const_initializer_at_index(const_init, element_type, index, &insert_index); const_init_rewrite_to_value(inner_value, value); } @@ -1149,14 +1139,11 @@ static inline void sema_update_const_initializer_with_designator_array(ConstInit // Get all the elements in the array - unsigned array_count = vec_size(const_init->init_array.elements); ArrayIndex insert_index = 0; for (ArrayIndex index = low_index; index <= high_index; index++) { - ConstInitializer *inner_value = sema_update_const_initializer_at_index(const_init, element_type, - array_count, index, &insert_index, - value); + ConstInitializer *inner_value = sema_update_const_initializer_at_index(const_init, element_type, index, &insert_index); // Update if (!is_last_path_element) diff --git a/src/compiler/sema_passes.c b/src/compiler/sema_passes.c index af8af47c8..2f0eee2ef 100644 --- a/src/compiler/sema_passes.c +++ b/src/compiler/sema_passes.c @@ -280,13 +280,13 @@ static Decl **sema_run_exec(CompilationUnit *unit, Decl *decl) } File *file; // TODO fix Win32 + char old_path_buffer[PATH_MAX]; char *old_path = NULL; if (compiler.build.script_dir) { - old_path = getcwd(NULL, 0); + old_path = getcwd(old_path, PATH_MAX); if (!dir_change(compiler.build.script_dir)) { - free(old_path); RETURN_PRINT_ERROR_AT(NULL, decl, "Failed to open script dir '%s'", compiler.build.script_dir); } } @@ -302,7 +302,6 @@ static Decl **sema_run_exec(CompilationUnit *unit, Decl *decl) if (old_path) { success = dir_change(old_path); - free(old_path); if (!success) { RETURN_PRINT_ERROR_AT(NULL, decl, "Failed to open run dir '%s'", compiler.build.script_dir); diff --git a/src/compiler/source_file.c b/src/compiler/source_file.c index 389d9ea0d..29d8d3110 100644 --- a/src/compiler/source_file.c +++ b/src/compiler/source_file.c @@ -3,13 +3,6 @@ // a copy of which can be found in the LICENSE file. #include -#ifdef _MSC_VER - -#define PATH_MAX 260 - -#else -#include -#endif #include "compiler_internal.h" static const size_t LEXER_FILES_START_CAPACITY = 128; diff --git a/src/utils/common.h b/src/utils/common.h index cae7fd506..0e9779b12 100644 --- a/src/utils/common.h +++ b/src/utils/common.h @@ -13,8 +13,11 @@ #include #include #include -#ifndef _MSC_VER +#ifdef _MSC_VER +#define PATH_MAX 260 +#else #include +#include #endif #define NO_ARENA 0 @@ -111,7 +114,7 @@ "https://github.com/c3lang/c3c/issues/new so that we can get it fixed.", _string, __func__, __FILE__, __LINE__, ##__VA_ARGS__); } while(0) -#define ASSERT(_condition) while (!(_condition)) { FATAL_ERROR("Violated assert: " #_condition); } +#define ASSERT(_condition) do { if (!(_condition)) { FATAL_ERROR("Violated assert: " #_condition); } } while (0) #define WARNING(_string, ...) do { eprintf("WARNING: "); eprintf(_string, ##__VA_ARGS__); eprintf("\n"); } while(0) #define UNREACHABLE FATAL_ERROR("Should be unreachable");