Skip to content

Commit 0e8a640

Browse files
debuginfo: Improved handling of function preludes.
1 parent 8c09e2b commit 0e8a640

File tree

3 files changed

+92
-30
lines changed

3 files changed

+92
-30
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,7 @@ pub fn trans_closure(ccx: @mut CrateContext,
18861886
param_substs,
18871887
body.info(),
18881888
Some(body.span));
1889+
18891890
let raw_llargs = create_llargs_for_fn_args(fcx, self_arg, decl.inputs);
18901891

18911892
// Set the fixed stack segment flag if necessary.
@@ -1905,6 +1906,11 @@ pub fn trans_closure(ccx: @mut CrateContext,
19051906

19061907
maybe_load_env(fcx);
19071908

1909+
// Up until here, IR instructions for this function have explicitly not been annotated with
1910+
// source code location, so we don't step into call setup code. From here on, source location
1911+
// emitting should be enabled.
1912+
debuginfo::start_emitting_source_locations(fcx);
1913+
19081914
// This call to trans_block is the place where we bridge between
19091915
// translation calls that don't have a return value (trans_crate,
19101916
// trans_mod, trans_item, et cetera) and those that do

src/librustc/middle/trans/debuginfo.rs

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub struct DebugContext {
9696
priv crate_file: ~str,
9797
priv llcontext: ContextRef,
9898
priv builder: DIBuilderRef,
99-
priv curr_loc: (uint, uint),
99+
priv curr_loc: DebugLocation,
100100
priv created_files: HashMap<~str, DIFile>,
101101
priv created_types: HashMap<uint, DIType>,
102102
}
@@ -111,7 +111,7 @@ impl DebugContext {
111111
crate_file: crate,
112112
llcontext: llcontext,
113113
builder: builder,
114-
curr_loc: (0, 0),
114+
curr_loc: UnknownLocation,
115115
created_files: HashMap::new(),
116116
created_types: HashMap::new(),
117117
};
@@ -122,6 +122,7 @@ pub struct FunctionDebugContext {
122122
priv scope_map: HashMap<ast::NodeId, DIScope>,
123123
priv fn_metadata: DISubprogram,
124124
priv argument_counter: uint,
125+
priv source_locations_enabled: bool,
125126
}
126127

127128
/// Create any deferred debug metadata nodes
@@ -202,7 +203,7 @@ pub fn create_self_argument_metadata(bcx: @mut Block,
202203
}
203204
};
204205

205-
set_debug_location(cx, scope, loc.line, loc.col.to_uint());
206+
set_debug_location(cx, DebugLocation::new(scope, loc.line, *loc.col));
206207
unsafe {
207208
let instr = llvm::LLVMDIBuilderInsertDeclareAtEnd(
208209
DIB(cx),
@@ -212,6 +213,7 @@ pub fn create_self_argument_metadata(bcx: @mut Block,
212213

213214
llvm::LLVMSetInstDebugLocation(trans::build::B(bcx).llbuilder, instr);
214215
}
216+
set_debug_location(cx, UnknownLocation);
215217
}
216218

217219
/// Creates debug information for the given function argument.
@@ -274,7 +276,7 @@ pub fn create_argument_metadata(bcx: @mut Block,
274276
}
275277
};
276278

277-
set_debug_location(cx, scope, loc.line, loc.col.to_uint());
279+
set_debug_location(cx, DebugLocation::new(scope, loc.line, *loc.col));
278280
unsafe {
279281
let instr = llvm::LLVMDIBuilderInsertDeclareAtEnd(
280282
DIB(cx),
@@ -284,28 +286,39 @@ pub fn create_argument_metadata(bcx: @mut Block,
284286

285287
llvm::LLVMSetInstDebugLocation(trans::build::B(bcx).llbuilder, instr);
286288
}
289+
set_debug_location(cx, UnknownLocation);
287290
}
288291
}
289292

290293
/// Sets the current debug location at the beginning of the span.
291294
///
292295
/// Maps to a call to llvm::LLVMSetCurrentDebugLocation(...). The node_id parameter is used to
293296
/// reliably find the correct visibility scope for the code position.
294-
pub fn update_source_pos(fcx: &FunctionContext,
295-
node_id: ast::NodeId,
296-
span: span) {
297+
pub fn set_source_location(fcx: &FunctionContext,
298+
node_id: ast::NodeId,
299+
span: span) {
297300
let cx: &mut CrateContext = fcx.ccx;
298301

299302
if !cx.sess.opts.debuginfo || (*span.lo == 0 && *span.hi == 0) {
300303
return;
301304
}
302305

303-
debug!("update_source_pos: %s", cx.sess.codemap.span_to_str(span));
306+
debug!("set_source_location: %s", cx.sess.codemap.span_to_str(span));
304307

305-
let loc = span_start(cx, span);
306-
let scope = scope_metadata(fcx, node_id, span);
308+
if fcx.debug_context.get_ref().source_locations_enabled {
309+
let loc = span_start(cx, span);
310+
let scope = scope_metadata(fcx, node_id, span);
307311

308-
set_debug_location(cx, scope, loc.line, loc.col.to_uint());
312+
set_debug_location(cx, DebugLocation::new(scope, loc.line, *loc.col));
313+
} else {
314+
set_debug_location(cx, UnknownLocation);
315+
}
316+
}
317+
318+
pub fn start_emitting_source_locations(fcx: &mut FunctionContext) {
319+
for debug_context in fcx.debug_context.mut_iter() {
320+
debug_context.source_locations_enabled = true;
321+
}
309322
}
310323

311324
pub fn create_function_debug_context(cx: &mut CrateContext,
@@ -401,6 +414,8 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
401414
ptr::null()
402415
};
403416

417+
let scope_line = get_scope_line(cx, top_level_block, loc.line);
418+
404419
let fn_metadata = do function_name.to_c_str().with_ref |function_name| {
405420
unsafe {
406421
llvm::LLVMDIBuilderCreateFunction(
@@ -413,7 +428,7 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
413428
function_type_metadata,
414429
false,
415430
true,
416-
loc.line as c_uint,
431+
scope_line as c_uint,
417432
FlagPrototyped as c_uint,
418433
cx.sess.opts.optimize != session::No,
419434
llfn,
@@ -427,6 +442,7 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
427442
scope_map: HashMap::new(),
428443
fn_metadata: fn_metadata,
429444
argument_counter: 1,
445+
source_locations_enabled: false,
430446
};
431447

432448
let arg_pats = do fn_decl.inputs.map |arg_ref| { arg_ref.pat };
@@ -438,8 +454,6 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
438454
fn_ast_id: ast::NodeId,
439455
fn_decl: &ast::fn_decl,
440456
param_substs: Option<@param_substs>) -> DIArray {
441-
//let cx = fcx.ccx;
442-
443457
if !cx.sess.opts.extra_debuginfo {
444458
return create_DIArray(DIB(cx), []);
445459
}
@@ -575,9 +589,22 @@ pub fn create_function_debug_context(cx: &mut CrateContext,
575589

576590
return create_DIArray(DIB(cx), template_params);
577591
}
578-
}
579-
580592

593+
fn get_scope_line(cx: &CrateContext,
594+
top_level_block: Option<&ast::Block>,
595+
default: uint)
596+
-> uint {
597+
match top_level_block {
598+
Some(&ast::Block { stmts: ref statements, _ }) if statements.len() > 0 => {
599+
span_start(cx, statements[0].span).line
600+
}
601+
Some(&ast::Block { expr: Some(@ref expr), _ }) => {
602+
span_start(cx, expr.span).line
603+
}
604+
_ => default
605+
}
606+
}
607+
}
581608

582609
//=-------------------------------------------------------------------------------------------------
583610
// Module-Internal debug info creation functions
@@ -650,7 +677,7 @@ fn declare_local(bcx: @mut Block,
650677
}
651678
};
652679

653-
set_debug_location(cx, scope, loc.line, loc.col.to_uint());
680+
set_debug_location(cx, DebugLocation::new(scope, loc.line, *loc.col));
654681
unsafe {
655682
let instr = llvm::LLVMDIBuilderInsertDeclareAtEnd(
656683
DIB(cx),
@@ -1409,22 +1436,51 @@ fn type_metadata(cx: &mut CrateContext,
14091436
return type_metadata;
14101437
}
14111438

1412-
fn set_debug_location(cx: &mut CrateContext, scope: DIScope, line: uint, col: uint) {
1413-
if dbg_cx(cx).curr_loc == (line, col) {
1439+
#[deriving(Eq)]
1440+
enum DebugLocation {
1441+
KnownLocation { scope: DIScope, line: uint, col: uint },
1442+
UnknownLocation
1443+
}
1444+
1445+
impl DebugLocation {
1446+
fn new(scope: DIScope, line: uint, col: uint) -> DebugLocation {
1447+
KnownLocation {
1448+
scope: scope,
1449+
line: line,
1450+
col: col,
1451+
}
1452+
}
1453+
}
1454+
1455+
fn set_debug_location(cx: &mut CrateContext, debug_location: DebugLocation) {
1456+
if debug_location == dbg_cx(cx).curr_loc {
14141457
return;
14151458
}
1416-
debug!("setting debug location to %u %u", line, col);
1417-
dbg_cx(cx).curr_loc = (line, col);
14181459

1419-
let elems = ~[C_i32(line as i32), C_i32(col as i32), scope, ptr::null()];
1420-
unsafe {
1421-
let dbg_loc = llvm::LLVMMDNodeInContext(
1422-
dbg_cx(cx).llcontext,
1423-
vec::raw::to_ptr(elems),
1424-
elems.len() as c_uint);
14251460

1426-
llvm::LLVMSetCurrentDebugLocation(cx.builder.B, dbg_loc);
1461+
let metadata_node;
1462+
1463+
match debug_location {
1464+
KnownLocation { scope, line, col } => {
1465+
debug!("setting debug location to %u %u", line, col);
1466+
let elements = [C_i32(line as i32), C_i32(col as i32), scope, ptr::null()];
1467+
unsafe {
1468+
metadata_node = llvm::LLVMMDNodeInContext(dbg_cx(cx).llcontext,
1469+
vec::raw::to_ptr(elements),
1470+
elements.len() as c_uint);
1471+
}
1472+
}
1473+
UnknownLocation => {
1474+
debug!("clearing debug location ");
1475+
metadata_node = ptr::null();
1476+
}
1477+
};
1478+
1479+
unsafe {
1480+
llvm::LLVMSetCurrentDebugLocation(cx.builder.B, metadata_node);
14271481
}
1482+
1483+
dbg_cx(cx).curr_loc = debug_location;
14281484
}
14291485

14301486
//=-------------------------------------------------------------------------------------------------

src/librustc/middle/trans/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ pub fn trans_into(bcx: @mut Block, expr: @ast::expr, dest: Dest) -> @mut Block {
413413
dest.to_str(bcx.ccx()));
414414
let _indenter = indenter();
415415

416-
debuginfo::update_source_pos(bcx.fcx, expr.id, expr.span);
416+
debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
417417

418418
let dest = {
419419
if ty::type_is_voidish(ty) {
@@ -485,7 +485,7 @@ fn trans_to_datum_unadjusted(bcx: @mut Block, expr: @ast::expr) -> DatumBlock {
485485
debug!("trans_to_datum_unadjusted(expr=%s)", bcx.expr_to_str(expr));
486486
let _indenter = indenter();
487487

488-
debuginfo::update_source_pos(bcx.fcx, expr.id, expr.span);
488+
debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
489489

490490
match ty::expr_kind(bcx.tcx(), bcx.ccx().maps.method_map, expr) {
491491
ty::LvalueExpr => {

0 commit comments

Comments
 (0)