diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-06-15 14:57:21 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | abe41cc2d5413faa8adeb16831f654435b6d0ef0 (patch) | |
tree | 3e08fe7d1857296ab9e41b5184b79005225361a5 | |
parent | 829e0174acac0adee10595cc04dd32cb64753107 (diff) |
Add compiler option for number of threads to use (instead of env)
-rw-r--r-- | include/compiler_options.h | 2 | ||||
-rw-r--r-- | src/compiler.c | 20 | ||||
-rw-r--r-- | src/tokenizer.c | 57 | ||||
-rw-r--r-- | tests/main.c | 32 |
4 files changed, 38 insertions, 73 deletions
diff --git a/include/compiler_options.h b/include/compiler_options.h index 1854372..e9ac6e2 100644 --- a/include/compiler_options.h +++ b/include/compiler_options.h @@ -6,6 +6,8 @@ typedef void(*amal_compiler_error_callback)(const char *err_msg, int err_msg_len typedef struct { amal_compiler_error_callback error_callback; void *error_callback_userdata; + /* Number of threads to use. If 0, use the number of threads available on the system */ + unsigned int num_threads; } amal_compiler_options; #endif diff --git a/src/compiler.c b/src/compiler.c index bdcb1c8..2b59a93 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -12,15 +12,6 @@ #include <limits.h> #include <assert.h> -static CHECK_RESULT int get_thread_count_env_var(int *thread_count) { - char *threads; - threads = getenv("THREADS"); - if(!threads) - return -1; - *thread_count = atoi(threads); - return 0; -} - static usize strnlen(const char *str, usize max_length) { usize len; len = 0; @@ -70,23 +61,20 @@ static CHECK_RESULT int init_default_types(amal_compiler *compiler) { void amal_compiler_options_init(amal_compiler_options *self) { self->error_callback = NULL; self->error_callback_userdata = NULL; + self->num_threads = 0; } int amal_compiler_init(amal_compiler *self, const amal_compiler_options *options) { int i; - int result; - result = get_thread_count_env_var(&self->usable_thread_count); - if(result != 0) { + self->usable_thread_count = options->num_threads; + if(self->usable_thread_count == 0) { self->usable_thread_count = amal_get_usable_thread_count(); if(self->usable_thread_count == 0) { amal_log_warning("Unable to get the number of threads available on the system, using 1 thread."); - amal_log_warning("You can override the number of threads using by setting the environment variable THREADS"); + amal_log_warning("You can override the number of threads using by setting compiler option for number of thread to use."); self->usable_thread_count = 1; } - } else if(self->usable_thread_count <= 0) { - amal_log_error("Environment variable THREADS contains invalid number for threads. THREADS has to be at least 1."); - return AMAL_COMPILER_ERR; } am_memset(&self->allocator, 0, sizeof(self->allocator)); diff --git a/src/tokenizer.c b/src/tokenizer.c index 03a72a1..bd57af7 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -709,63 +709,6 @@ void tokenizer_print_error_args(Tokenizer *self, int index, const char *fmt, va_ } void tokenizer_print_error(Tokenizer *self, int index, const char *fmt, ...) { -#if 0 - va_list args; - int line; - int line_start; - int line_end; - /*int code_start;*/ - int prev_column; - int i; - - line = tokenizer_get_line_by_index(self, index); - line_start = tokenizer_get_start_of_line_from_index(self, index); - line_end = tokenizer_get_end_of_line_from_index(self, index); - /*code_start = find_non_whitespace(&self->code.data[line_start], line_end - line_start); - if(code_start != -1) - line_start += code_start;*/ - prev_column = index - line_start; - - if(self->compiler_options->error_callback) { - char buffer[2048]; - int bytes_copied; - - bytes_copied = 0; - bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "%.*s:%d:%d: error: ", (int)self->code_name.size, self->code_name.data, line, 1 + prev_column)); - - if(sizeof(buffer) - bytes_copied > 0) { - va_start(args, fmt); - bytes_copied += max(0, vsnprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, fmt, args)); - va_end(args); - } - - if(sizeof(buffer) - bytes_copied > 0) - bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "\n%.*s\n", line_end - line_start, self->code.data + line_start)); - - if(sizeof(buffer) - bytes_copied > 0) { - for(i = 0; i < prev_column; ++i) - bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, " ")); - } - - if(sizeof(buffer) - bytes_copied > 0) - bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "^\n")); - - self->compiler_options->error_callback(buffer, bytes_copied, self->compiler_options->error_callback_userdata); - } else { - amal_mutex *mutex; - mutex = amal_log_get_mutex(); - ignore_result_int(amal_mutex_lock(mutex, "tokenizer_print_error")); - va_start(args, fmt); - fprintf(stderr, "\x1b[1;37m%.*s:%d:%d:\x1b[0m \x1b[1;31merror:\x1b[0m ", (int)self->code_name.size, self->code_name.data, line, 1 + prev_column); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n%.*s\n", line_end - line_start, self->code.data + line_start); - for(i = 0; i < prev_column; ++i) - fprintf(stderr, " "); - fprintf(stderr, "\x1b[1;32m^\x1b[0m\n"); - va_end(args); - ignore_result_int(amal_mutex_unlock(mutex)); - } -#endif va_list args; va_start(args, fmt); tokenizer_print_error_args(self, index, fmt, args); diff --git a/tests/main.c b/tests/main.c index 3080807..c4c567d 100644 --- a/tests/main.c +++ b/tests/main.c @@ -120,13 +120,33 @@ static char* join_str(const char *str1, const char *str2, char delimiter) { return buf; } +static CHECK_RESULT int get_thread_count_env_var(int *thread_count) { + char *threads; + threads = getenv("THREADS"); + if(!threads) + return -1; + *thread_count = atoi(threads); + return 0; +} + static void test_load(const char *filepath) { amal_compiler compiler; amal_compiler_options options; char *full_path; int result; + int num_threads; + + result = get_thread_count_env_var(&num_threads); + if(result != 0) + num_threads = 0; + + if(num_threads < 0) { + amal_log_error("Environment variable THREADS contains invalid number for threads. THREADS has to be at least 0 (0 = use the number of available threads on the system)"); + exit(1); + } amal_compiler_options_init(&options); + options.num_threads = num_threads; ++num_tests_run; full_path = get_full_path(filepath); /* @@ -162,8 +182,19 @@ static void test_load_error(const char *filepath, const char *expected_error) { amal_compiler compiler; amal_compiler_options options; int result; + int num_threads; + + result = get_thread_count_env_var(&num_threads); + if(result != 0) + num_threads = 0; + + if(num_threads < 0) { + amal_log_error("Environment variable THREADS contains invalid number for threads. THREADS has to be at least 0 (0 = use the number of available threads on the system)"); + exit(1); + } amal_compiler_options_init(&options); + options.num_threads = num_threads; ++num_tests_run; options.error_callback = error_callback_assert; ErrorExpectedData expected_data; @@ -200,6 +231,7 @@ static void test_load_error(const char *filepath, const char *expected_error) { free(expected_data.expected_error); } + /* TODO: Restrict variables in global scope to const */ int main(int argc, char **argv) { return_if_error(test_hash_map()); |