core: Restore debug location when generating allocas

Debug location is lost when moving the builder cursor.
This commit is contained in:
David Mak 2023-10-06 11:11:38 +08:00
parent c5629d4eb5
commit 1a54aaa1c0
2 changed files with 17 additions and 0 deletions

View File

@ -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,

View File

@ -27,12 +27,27 @@ pub fn gen_var<'ctx, 'a>(
ty: BasicTypeEnum<'ctx>,
name: Option<&str>,
) -> Result<PointerValue<'ctx>, 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)
}