From 4f0206ce08593628bf9458b623f61c2989558f69 Mon Sep 17 00:00:00 2001 From: flysand7 Date: Fri, 17 Jan 2025 01:12:23 +0300 Subject: [PATCH] Added compile-time checks for thread locals with -no-crt Now using any thread-local variables with -no-crt enabled will cause a compiler error, unless -no-thread-local is given. Also fixed a minor typo in a comment. --- src/build_settings.cpp | 16 +++++++++++++++- src/check_expr.cpp | 15 ++++++++++++++- src/main.cpp | 4 ++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 93168cf77..b3321637f 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -2133,7 +2133,21 @@ gb_internal bool init_build_paths(String init_filename) { case TargetOs_openbsd: case TargetOs_netbsd: case TargetOs_haiku: - gb_printf_err("-no-crt on unix systems requires either -default-to-nil-allocator or -default-to-panic-allocator to also be present because the default allocator requires crt\n"); + gb_printf_err("-no-crt on unix systems requires either -default-to-nil-allocator or -default-to-panic-allocator to also be present, because the default allocator requires crt\n"); + return false; + } + } + + if (build_context.no_crt && !build_context.no_thread_local && !build_context.ODIN_DEFAULT_TO_NIL_ALLOCATOR) { + switch (build_context.metrics.os) { + case TargetOs_linux: + case TargetOs_darwin: + case TargetOs_essence: + case TargetOs_freebsd: + case TargetOs_openbsd: + case TargetOs_netbsd: + case TargetOs_haiku: + gb_printf_err("-no-crt on unix systems requires either -default-to-nil-allocator or -no-thread-local to also be present, because the temporary allocator is a thread local, which are inaccessible without CRT initializing TLS\n"); return false; } } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 231ece2f4..7574c20a7 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1044,7 +1044,7 @@ gb_internal AstPackage *get_package_of_type(Type *type) { } -// NOTE(bill): 'content_name' is for debugging and error messages +// NOTE(bill): 'context_name' is for debugging and error messages gb_internal void check_assignment(CheckerContext *c, Operand *operand, Type *type, String context_name) { check_not_tuple(c, operand); if (operand->mode == Addressing_Invalid) { @@ -1822,6 +1822,19 @@ gb_internal Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *nam break; case Entity_Variable: + if (e->kind == Entity_Variable && build_context.no_crt && !build_context.no_thread_local && e->Variable.thread_local_model != "") { + switch (build_context.metrics.os) { + case TargetOs_linux: + case TargetOs_darwin: + case TargetOs_essence: + case TargetOs_freebsd: + case TargetOs_openbsd: + case TargetOs_netbsd: + case TargetOs_haiku: + Token token = ast_token(n); + error(token, "Illegal usage of thread locals: '%.*s'", LIT(e->token.string)); + } + } e->flags |= EntityFlag_Used; if (type == t_invalid) { o->type = t_invalid; diff --git a/src/main.cpp b/src/main.cpp index 1de5d987b..24e33850e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2334,6 +2334,10 @@ gb_internal void print_show_help(String const arg0, String command, String optio print_usage_line(2, "Sets the default allocator to be the nil_allocator, an allocator which does nothing."); } + if (print_flag("-default-to-panic-allocator")) { + print_usage_line(2, "Sets the default allocator to be the panic_allocator, an allocator which calls panic() on any allocation attempt."); + } + if (print_flag("-define:=")) { print_usage_line(2, "Defines a scalar boolean, integer or string as global constant."); print_usage_line(2, "Example: -define:SPAM=123");