From 8dbbbd2d64486d9e81265e49ce496ddb2a18e870 Mon Sep 17 00:00:00 2001 From: bbodi Date: Thu, 8 Oct 2026 14:30:13 +0200 Subject: [PATCH] Fix -debug when every initial file is excluded The debug compile unit was made from the first file of the initial package. When build tags exclude all of its files, as for the Windows-only tests/issues/test_issue_7547.odin on other targets, there is none, and `odin test -debug` crashed on an out-of-bounds index. Without `-debug` such a test builds and reports that there are no tests to run, which `tests/issues/run.sh` relies on. Fall back to the package's path for the compile unit's file. --- src/llvm_backend.cpp | 14 ++++++++++++-- tests/issues/run.sh | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index f22b1a73a..1a896c889 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -3706,7 +3706,10 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { gbString split_name = gb_string_make(temporary_allocator(), ""); LLVMBool is_optimized = build_context.optimization_level > 0; - AstFile *init_file = m->info->init_package->files[0]; + AstFile *init_file = nullptr; + if (m->info->init_package->files.count > 0) { + init_file = m->info->init_package->files[0]; + } if (Entity *entry_point = m->info->entry_point) { if (Ast *ident = entry_point->identifier.load()) { @@ -3716,11 +3719,18 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { } } + LLVMMetadataRef init_file_metadata = lb_get_file_metadata(m, init_file); + if (init_file_metadata == nullptr) { + // every file of the initial package was excluded by its build tags, as can be done for `odin test` + String path = m->info->init_package->fullpath; + init_file_metadata = LLVMDIBuilderCreateFile(m->debug_builder, cast(char const *)path.text, path.len, "", 0); + } + LLVMBool split_debug_inlining = build_context.build_mode == BuildMode_Assembly; LLVMBool debug_info_for_profiling = false; m->debug_compile_unit = LLVMDIBuilderCreateCompileUnit(m->debug_builder, LLVMDWARFSourceLanguageC99, - lb_get_file_metadata(m, init_file), + init_file_metadata, producer, gb_string_length(producer), is_optimized, "", 0, 1, split_name, gb_string_length(split_name), diff --git a/tests/issues/run.sh b/tests/issues/run.sh index 6661f8484..95216a0d0 100755 --- a/tests/issues/run.sh +++ b/tests/issues/run.sh @@ -268,6 +268,7 @@ fi $ODIN test ../test_lifetime_markers.odin $COMMON -o:size -lifetime-markers $ODIN test ../test_lifetime_markers.odin $COMMON -o:speed -lifetime-markers $ODIN test ../test_issue_7547.odin $COMMON +$ODIN test ../test_issue_7547.odin $COMMON -debug $ODIN test ../test_issue_7490.odin $COMMON $ODIN test ../test_issue_7779.odin $COMMON $ODIN run ../test_issue_7798.odin $COMMON