From 1a54aaa1c0fa7db604ff5d47b493a5ec7e266a7d Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 6 Oct 2023 11:11:38 +0800 Subject: [PATCH] core: Restore debug location when generating allocas Debug location is lost when moving the builder cursor. --- nac3core/src/codegen/mod.rs | 2 ++ nac3core/src/codegen/stmt.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 9006a59c..28a4d60f 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -129,6 +129,8 @@ impl CodeGenTargetMachineOptions { pub struct CodeGenContext<'ctx, 'a> { pub ctx: &'ctx Context, pub builder: Builder<'ctx>, + /// The [DebugInfoBuilder], [compilation unit information][DICompileUnit], and + /// [scope information][DIScope] of this context. pub debug_info: (DebugInfoBuilder<'ctx>, DICompileUnit<'ctx>, DIScope<'ctx>), pub module: Module<'ctx>, pub top_level: &'a TopLevelContext, diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index 450df820..f737c877 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -27,12 +27,27 @@ pub fn gen_var<'ctx, 'a>( ty: BasicTypeEnum<'ctx>, name: Option<&str>, ) -> Result, String> { + // Restore debug location + let di_loc = ctx.debug_info.0.create_debug_location( + ctx.ctx, + ctx.current_loc.row as u32, + ctx.current_loc.column as u32, + ctx.debug_info.2, + None, + ); + // put the alloca in init block let current = ctx.builder.get_insert_block().unwrap(); + // position before the last branching instruction... ctx.builder.position_before(&ctx.init_bb.get_last_instruction().unwrap()); + ctx.builder.set_current_debug_location(di_loc); + let ptr = ctx.builder.build_alloca(ty, name.unwrap_or("")); + ctx.builder.position_at_end(current); + ctx.builder.set_current_debug_location(di_loc); + Ok(ptr) }