Fixed leaky imports, modified log stuff

This commit is contained in:
Krzosa Karol
2022-10-11 10:20:18 +02:00
parent 977a62d5ae
commit 4004b8b8d3
8 changed files with 67 additions and 41 deletions

1
.gitignore vendored
View File

@@ -8,6 +8,7 @@
*.out *.out
*.c *.c
tests/
backup* backup*
start.bat start.bat
__pycache__ __pycache__

View File

@@ -561,7 +561,7 @@ arena_init(Arena *a, String debug_name){
a->debug_string = debug_name; a->debug_string = debug_name;
} }
enum Log_Kind{Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace}; enum Log_Kind{Log_Kind_Normal_No_NewLine, Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace};
typedef void Log_Proc(Log_Kind kind, String string, char *file, int line); typedef void Log_Proc(Log_Kind kind, String string, char *file, int line);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Thread Context // Thread Context
@@ -753,6 +753,7 @@ array_make(Arena *a, S64 size = 16){
// Logging // Logging
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#define log_info(...) handle_log_message(Log_Kind_Normal, __LINE__, __FILE__,##__VA_ARGS__) #define log_info(...) handle_log_message(Log_Kind_Normal, __LINE__, __FILE__,##__VA_ARGS__)
#define log_info_no_nl(...) handle_log_message(Log_Kind_Normal_No_NewLine, __LINE__, __FILE__,##__VA_ARGS__)
#define log_trace(...) handle_log_message(Log_Kind_Trace, __LINE__, __FILE__,##__VA_ARGS__) #define log_trace(...) handle_log_message(Log_Kind_Trace, __LINE__, __FILE__,##__VA_ARGS__)
#define log_error(...) handle_log_message(Log_Kind_Error, __LINE__, __FILE__,##__VA_ARGS__) #define log_error(...) handle_log_message(Log_Kind_Error, __LINE__, __FILE__,##__VA_ARGS__)
function void function void
@@ -764,6 +765,9 @@ handle_log_message(Log_Kind kind, int line, const char *file, const char *str, .
if(thread_ctx.log_proc) thread_ctx.log_proc(kind, message, (char *)file, line); if(thread_ctx.log_proc) thread_ctx.log_proc(kind, message, (char *)file, line);
else{ else{
printf("%s", message.str); printf("%s", message.str);
if(kind != Log_Kind_Normal_No_NewLine){
printf("\n");
}
} }
} }

View File

@@ -3,4 +3,5 @@
pushd %~dp0 pushd %~dp0
rem cl main.cpp -I.. user32.lib rem cl main.cpp -I.. user32.lib
clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o main.exe -Wl,user32.lib
ubuntu run clang core_main.cpp -O0 -Wall -Wno-unused-function -fno-exceptions -fdiagnostics-absolute-paths -g -o core.out
popd popd

View File

@@ -302,7 +302,7 @@ const U32 COMPILE_TESTING = 0x8;
function void function void
compile_file(String filename, U32 compile_flags = COMPILE_NULL){ compile_file(String filename, U32 compile_flags = COMPILE_NULL){
if(is_flag_set(compile_flags, COMPILE_AND_RUN)){ if(is_flag_set(compile_flags, COMPILE_AND_RUN)){
log_info("\n%Q - ", filename); log_info_no_nl("%Q - ", filename);
} }
String result = compile_file_to_string(filename); String result = compile_file_to_string(filename);
@@ -322,20 +322,20 @@ compile_file(String filename, U32 compile_flags = COMPILE_NULL){
F64 end = os_time(); F64 end = os_time();
if(is_flag_set(compile_flags, COMPILE_PRINT_STATS)){ if(is_flag_set(compile_flags, COMPILE_PRINT_STATS)){
log_info("\ntotal = %f", os_time() - total_time); log_info("total = %f", os_time() - total_time);
log_info("\nclang = %f", end - begin); log_info("clang = %f", end - begin);
log_info("\nparsing = %f", parsing_time_end - parsing_time_begin); log_info("parsing = %f", parsing_time_end - parsing_time_begin);
log_info("\nresolving = %f", resolving_time_end - resolving_time_begin); log_info("resolving = %f", resolving_time_end - resolving_time_begin);
log_info("\ngeneratin = %f", generating_time_end - generating_time_begin); log_info("generatin = %f", generating_time_end - generating_time_begin);
} }
if(is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)){ if(is_flag_set(compile_flags, COMPILE_PRINT_ALLOCATOR_STATS_BEFORE_DESTROY)){
Arena *p = (Arena *)pctx->perm; Arena *p = (Arena *)pctx->perm;
Arena *stage = (Arena *)&pctx->stage_arena; Arena *stage = (Arena *)&pctx->stage_arena;
log_info("\nPernament arena len: %llu commit: %llu", p->len, p->memory.commit); log_info("Pernament arena len: %llu commit: %llu", p->len, p->memory.commit);
log_info("\nStage arena len: %llu commit: %llu", stage->len, stage->memory.commit); log_info("Stage arena len: %llu commit: %llu", stage->len, stage->memory.commit);
log_info("\nScratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit); log_info("Scratch1 len: %llu commit: %llu", thread_ctx.scratch[0].len, thread_ctx.scratch[0].memory.commit);
log_info("\nScratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit); log_info("Scratch2 len: %llu commit: %llu", thread_ctx.scratch[1].len, thread_ctx.scratch[1].memory.commit);
} }
if(is_flag_set(compile_flags, COMPILE_AND_RUN)){ if(is_flag_set(compile_flags, COMPILE_AND_RUN)){
@@ -348,11 +348,12 @@ compile_file(String filename, U32 compile_flags = COMPILE_NULL){
int result = system((char *)sys.str); int result = system((char *)sys.str);
assert(result != -1); assert(result != -1);
if(result == 0){ if(result == 0){
log_info(PRINTF_GREEN "OK!" PRINTF_RESET); log_info_no_nl(PRINTF_GREEN "OK!" PRINTF_RESET);
} }
if(result != 0){ if(result != 0){
log_info(PRINTF_RED "ERROR!" PRINTF_RESET); log_info_no_nl(PRINTF_RED "ERROR!" PRINTF_RESET);
} }
log_info("");
} }
destroy_compiler(); destroy_compiler();

View File

@@ -2,10 +2,9 @@
Current: Current:
- [ ] DANGEROUS BUG! &(int64_t){32} creates a new value, we shouldn't do this with variables!!!
- [ ] String declaration in Language.core - [ ] String declaration in Language.core
- [ ] Way to import and force evaluate #import_lazy #import ? - [ ] Way to import and force evaluate #import_lazy #import ?
- [ ] Typeof operator
- [ ] Imports are leaking names ! Multimedia leaks windows stuff - [ ] Imports are leaking names ! Multimedia leaks windows stuff
- [ ] Test and bulletproof any, slices - [ ] Test and bulletproof any, slices
@@ -251,6 +250,17 @@ For modules it's a bit different cause they should be distributed as valid.
int main(int argument_count, char **arguments){ int main(int argument_count, char **arguments){
Scratch scratch;
Array<String> args = {scratch};
for(int i = 1; i < argument_count; i+=1){
String arg = string_from_cstring(arguments[i]);
args.add(arg);
}
if(!args.len){
log_info("Please specify a file to compile!");
}
#if OS_WINDOWS #if OS_WINDOWS
// Set output mode to handle virtual terminal sequences // Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -267,11 +277,9 @@ int main(int argument_count, char **arguments){
if (!SetConsoleMode(hOut, dwMode)) { if (!SetConsoleMode(hOut, dwMode)) {
return GetLastError(); return GetLastError();
} }
#endif
#if OS_WINDOWS
test_os_memory(); test_os_memory();
#endif #endif
thread_ctx_init(); thread_ctx_init();
test_unicode(); test_unicode();
@@ -281,19 +289,28 @@ int main(int argument_count, char **arguments){
// emit_line_directives = false; // emit_line_directives = false;
// emit_type_info = false; // emit_type_info = false;
if(argument_count > 1){ For(args){
String program_name = string_from_cstring(arguments[1]);
compile_file(program_name, COMPILE_PRINT_STATS);
}
else { if(it == "-testing"_s){ // @copy_paste
Scratch scratch; Scratch scratch;
Array<OS_File_Info> examples = os_list_dir(scratch, "examples"_s); Array<OS_File_Info> examples = os_list_dir(scratch, "examples"_s);
Array<OS_File_Info> tests = os_list_dir(scratch, "tests"_s);
For(examples){ For(examples){
if(it.is_directory) continue; if(it.is_directory) continue;
compile_file(it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING); compile_file(it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING);
} }
For(tests){
if(it.is_directory) continue;
compile_file(it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING);
} }
log_info("End of program\n"); }
else {
String program_name = string_from_cstring(arguments[1]);
compile_file(program_name, COMPILE_PRINT_STATS);
}
}
log_info("End of program");
return 0; return 0;
} }

View File

@@ -746,7 +746,7 @@ register_ast_file(Token *pos, String absolute_file_path, Ast_Module *module, B32
file->absolute_file_path = absolute_file_path; file->absolute_file_path = absolute_file_path;
file->absolute_base_folder = string_copy(pctx->perm, string_chop_last_slash(file->absolute_file_path)); file->absolute_base_folder = string_copy(pctx->perm, string_chop_last_slash(file->absolute_file_path));
file->module = module; file->module = module;
file->parent_scope = 0; file->parent_scope = module;
file->file = file; // @warning: self referential! file->file = file; // @warning: self referential!
file->pos = pos; file->pos = pos;
file->debug_name = string_skip_to_last_slash(absolute_file_path); file->debug_name = string_skip_to_last_slash(absolute_file_path);

View File

@@ -455,15 +455,17 @@ inside_scope_search(Scope_Search *search, Ast_Scope *scope, int level){
} }
// Search for declarations in imported implicitly scopes // Search for declarations in imported implicitly scopes
if(level < 2 || scope->kind != AST_FILE){ // We dont want to descent into foreign implicit imports
// so we cap it with level and then we need to make sure that we
// descent into module 1 level away and it's files but no more then that
Iter(&scope->implicit_imports){ Iter(&scope->implicit_imports){
if(level > 0 && it.item[0]->kind == AST_MODULE) continue;
inside_scope_search(search, it.item[0], level + 1); inside_scope_search(search, it.item[0], level + 1);
if(search->exit_on_find && search->results.len){ if(search->exit_on_find && search->results.len){
return; return;
} }
} }
}
} }
function void function void

View File

@@ -21,14 +21,14 @@ os_write_file(String filename, String filecontent){
} }
function String function String
os_read_file(Allocator *a, String name){ os_read_file(Arena *a, String name){
String result = {0}; String result = {0};
FILE *f = fopen((char *)name.str, "rb"); FILE *f = fopen((char *)name.str, "rb");
if(f){ if(f){
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
result.len = ftell(f); result.len = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
result.str = (U8 *)exp_alloc(a, result.len + 1); result.str = (U8 *)arena_push_size(a, result.len + 1);
fread(result.str, result.len, 1, f); fread(result.str, result.len, 1, f);
fclose(f); fclose(f);
result.str[result.len] = 0; result.str[result.len] = 0;
@@ -38,7 +38,7 @@ os_read_file(Allocator *a, String name){
} }
function String function String
os_get_exe_dir(Allocator *a){ os_get_exe_dir(Arena *a){
char buffer[PATH_MAX] = {}; char buffer[PATH_MAX] = {};
if (readlink("/proc/self/exe", buffer, PATH_MAX) == -1) { if (readlink("/proc/self/exe", buffer, PATH_MAX) == -1) {
log_info("Failed to retrieve the path of the executable, the method used is fetching /proc/self/exe, very likely you are using an OS that doesn't follow this convention and as such it is currently not supported"); log_info("Failed to retrieve the path of the executable, the method used is fetching /proc/self/exe, very likely you are using an OS that doesn't follow this convention and as such it is currently not supported");
@@ -52,7 +52,7 @@ os_get_exe_dir(Allocator *a){
} }
function String function String
os_get_absolute_path(Allocator *a, String path){ os_get_absolute_path(Arena *a, String path){
assert(path.str[path.len] == 0); assert(path.str[path.len] == 0);
char buffer[PATH_MAX] = {}; char buffer[PATH_MAX] = {};
realpath((char *)path.str, buffer); realpath((char *)path.str, buffer);
@@ -73,14 +73,14 @@ os_does_file_exist(String path){
} }
function String function String
os_get_working_dir(Allocator *allocator){ os_get_working_dir(Arena *allocator){
char *buffer = exp_alloc_array(allocator, char, PATH_MAX); char *buffer = arena_push_array(allocator, char, PATH_MAX);
char *result = getcwd(buffer, PATH_MAX); char *result = getcwd(buffer, PATH_MAX);
return string_from_cstring(result); return string_from_cstring(result);
} }
function Array<OS_File_Info> function Array<OS_File_Info>
os_list_dir(Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){ os_list_dir(Arena *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){
Scratch scratch(a); Scratch scratch(a);
Array<String> dirs_to_read = {scratch}; Array<String> dirs_to_read = {scratch};
dirs_to_read.add(dir); dirs_to_read.add(dir);
@@ -120,7 +120,7 @@ os_list_dir(Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){
closedir(d); closedir(d);
} }
else{ else{
log_info("ERROR Failed to read dir: %Q, errno: %s\n", *it, strerror(errno)); log_info("ERROR Failed to read dir: %Q, errno: %s", *it, strerror(errno));
} }
} }