diff --git a/src/analyze.cpp b/src/analyze.cpp index 0ec07c8ac2..d908c7fcbe 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1012,6 +1012,7 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t BlockContext *context = new_block_context(fn_table_entry->fn_def_node, containing_context); fn_table_entry->fn_def_node->data.fn_def.block_context = context; context->di_scope = LLVMZigSubprogramToScope(subprogram); + ZigLLVMFnSetSubprogram(fn_table_entry->fn_value, subprogram); } } diff --git a/src/codegen.cpp b/src/codegen.cpp index c86ce0ed10..588e831af6 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -744,6 +744,7 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) { case CastOpBoolToInt: assert(wanted_type->id == TypeTableEntryIdInt); assert(actual_type->id == TypeTableEntryIdBool); + add_debug_source_node(g, node); return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, ""); } @@ -1965,6 +1966,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV endif_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "EndIf"); } + add_debug_source_node(g, source_node); LLVMBuildCondBr(g->builder, cond_value, then_block, else_block); LLVMPositionBuilderAtEnd(g->builder, then_block); @@ -3836,6 +3838,8 @@ static void init(CodeGen *g, Buf *source_path) { LLVMSetTarget(g->module, buf_ptr(&g->triple_str)); + ZigLLVMAddModuleDebugInfoFlag(g->module); + LLVMTargetRef target_ref; char *err_msg = nullptr; if (LLVMGetTargetFromTriple(buf_ptr(&g->triple_str), &target_ref, &err_msg)) { diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 74615d5ea6..16bdf1d548 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -150,6 +150,12 @@ void LLVMZigAddNonNullAttr(LLVMValueRef fn, unsigned i) unwrapped_function->addAttribute(i, Attribute::NonNull); } +void ZigLLVMFnSetSubprogram(LLVMValueRef fn, LLVMZigDISubprogram *subprogram) { + assert( isa(unwrap(fn)) ); + Function *unwrapped_function = reinterpret_cast(unwrap(fn)); + unwrapped_function->setSubprogram(reinterpret_cast(subprogram)); +} + LLVMZigDIType *LLVMZigCreateDebugPointerType(LLVMZigDIBuilder *dibuilder, LLVMZigDIType *pointee_type, uint64_t size_in_bits, uint64_t align_in_bits, const char *name) @@ -612,6 +618,10 @@ const char *ZigLLVMGetSubArchTypeName(ZigLLVM_SubArchType sub_arch) { abort(); } +void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module) { + unwrap(module)->addModuleFlag(Module::Warning, "Debug Info Version", DEBUG_METADATA_VERSION); +} + //------------------------------------ #include "buffer.hpp" @@ -627,7 +637,8 @@ void ZigLLVMGetTargetTriple(Buf *out_buf, ZigLLVM_ArchType arch_type, ZigLLVM_Su triple.setVendor((Triple::VendorType)vendor_type); triple.setOS((Triple::OSType)os_type); triple.setEnvironment((Triple::EnvironmentType)environ_type); - triple.setObjectFormat((Triple::ObjectFormatType)oformat); + // I guess it's a "triple" because we don't set the object format? + //triple.setObjectFormat((Triple::ObjectFormatType)oformat); const std::string &str = triple.str(); buf_init_from_mem(out_buf, str.c_str(), str.size()); diff --git a/src/zig_llvm.hpp b/src/zig_llvm.hpp index feb0e74125..d3e29ef097 100644 --- a/src/zig_llvm.hpp +++ b/src/zig_llvm.hpp @@ -100,6 +100,7 @@ unsigned LLVMZigTag_DW_variable(void); unsigned LLVMZigTag_DW_structure_type(void); LLVMZigDIBuilder *LLVMZigCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved); +void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module); void LLVMZigSetCurrentDebugLocation(LLVMBuilderRef builder, int line, int column, LLVMZigDIScope *scope); @@ -132,6 +133,9 @@ LLVMZigDISubprogram *LLVMZigCreateFunction(LLVMZigDIBuilder *dibuilder, LLVMZigD LLVMZigDIType *fn_di_type, bool is_local_to_unit, bool is_definition, unsigned scope_line, unsigned flags, bool is_optimized, LLVMZigDISubprogram *decl_subprogram); + +void ZigLLVMFnSetSubprogram(LLVMValueRef fn, LLVMZigDISubprogram *subprogram); + void LLVMZigDIBuilderFinalize(LLVMZigDIBuilder *dibuilder); LLVMZigInsertionPoint *LLVMZigSaveInsertPoint(LLVMBuilderRef builder); diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 7b7c829d94..34ca53e162 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -55,14 +55,8 @@ fn test_loc_vars(b: i32) { #attribute("test") fn bool_literals() { - should_be_true(true); - should_be_false(false); -} -fn should_be_true(b: bool) { - if (!b) unreachable{}; -} -fn should_be_false(b: bool) { - if (b) unreachable{}; + assert(true); + assert(!false); }