Skip to content

Commit

Permalink
debuginfo: Add debuginfo::with_source_location_override() function...
Browse files Browse the repository at this point in the history
... and use it to fix a debug-location issue with constants.
  • Loading branch information
michaelwoerister committed Mar 5, 2015
1 parent fed1249 commit e316c66
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/consts.rs
Expand Up @@ -173,7 +173,7 @@ pub fn get_const_expr<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
&**expr
} else {
ccx.sess().span_bug(ref_expr.span,
&format!("get_const_val given non-constant item {}",
&format!("get_const_expr given non-constant item {}",
item.repr(ccx.tcx())));
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/librustc_trans/trans/debuginfo.rs
Expand Up @@ -695,6 +695,7 @@ struct FunctionDebugContextData {
fn_metadata: DISubprogram,
argument_counter: Cell<uint>,
source_locations_enabled: Cell<bool>,
source_location_override: Cell<bool>,
}

enum VariableAccess<'a> {
Expand Down Expand Up @@ -1174,6 +1175,12 @@ pub fn set_source_location(fcx: &FunctionContext,
return;
}
FunctionDebugContext::RegularContext(box ref function_debug_context) => {
if function_debug_context.source_location_override.get() {
// Just ignore any attempts to set a new debug location while
// the override is active.
return;
}

let cx = fcx.ccx;

debug!("set_source_location: {}", cx.sess().codemap().span_to_string(span));
Expand All @@ -1192,6 +1199,35 @@ pub fn set_source_location(fcx: &FunctionContext,
}
}

/// This function makes sure that all debug locations emitted while executing
/// `wrapped_function` are set to the given `debug_loc`.
pub fn with_source_location_override<F, R>(fcx: &FunctionContext,
debug_loc: DebugLoc,
wrapped_function: F) -> R
where F: FnOnce() -> R
{
match fcx.debug_context {
FunctionDebugContext::DebugInfoDisabled => {
wrapped_function()
}
FunctionDebugContext::FunctionWithoutDebugInfo => {
set_debug_location(fcx.ccx, UnknownLocation);
wrapped_function()
}
FunctionDebugContext::RegularContext(box ref function_debug_context) => {
if function_debug_context.source_location_override.get() {
wrapped_function()
} else {
debug_loc.apply(fcx);
function_debug_context.source_location_override.set(true);
let result = wrapped_function();
function_debug_context.source_location_override.set(false);
result
}
}
}
}

/// Clears the current debug location.
///
/// Instructions generated hereafter won't be assigned a source location.
Expand Down Expand Up @@ -1412,6 +1448,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
fn_metadata: fn_metadata,
argument_counter: Cell::new(1),
source_locations_enabled: Cell::new(false),
source_location_override: Cell::new(false),
};


Expand Down
10 changes: 8 additions & 2 deletions src/librustc_trans/trans/expr.rs
Expand Up @@ -147,15 +147,21 @@ pub fn trans_into<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
ast::ExprPath(..) => {
match bcx.def(expr.id) {
def::DefConst(did) => {
let expr = consts::get_const_expr(bcx.ccx(), did, expr);
let const_expr = consts::get_const_expr(bcx.ccx(), did, expr);
// Temporarily get cleanup scopes out of the way,
// as they require sub-expressions to be contained
// inside the current AST scope.
// These should record no cleanups anyways, `const`
// can't have destructors.
let scopes = mem::replace(&mut *bcx.fcx.scopes.borrow_mut(),
vec![]);
bcx = trans_into(bcx, expr, dest);
// Lock emitted debug locations to the location of
// the constant reference expression.
debuginfo::with_source_location_override(bcx.fcx,
expr.debug_loc(),
|| {
bcx = trans_into(bcx, const_expr, dest)
});
let scopes = mem::replace(&mut *bcx.fcx.scopes.borrow_mut(),
scopes);
assert!(scopes.is_empty());
Expand Down
67 changes: 67 additions & 0 deletions src/test/debuginfo/constant-debug-locs.rs
@@ -0,0 +1,67 @@
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-android: FIXME(#10381)
// min-lldb-version: 310

// compile-flags:-g

#![allow(unused_variables)]
#![allow(dead_code)]
#![omit_gdb_pretty_printer_section]

// This test makes sure that the compiler doesn't crash when trying to assign
// debug locations to const-expressions.

use std::sync::MUTEX_INIT;
use std::cell::UnsafeCell;

const CONSTANT: u64 = 3 + 4;

struct Struct {
a: isize,
b: usize,
}
const STRUCT: Struct = Struct { a: 1, b: 2 };

struct TupleStruct(u32);
const TUPLE_STRUCT: TupleStruct = TupleStruct(4);

enum Enum {
Variant1(char),
Variant2 { a: u8 },
Variant3
}

const VARIANT1: Enum = Enum::Variant1('v');
const VARIANT2: Enum = Enum::Variant2 { a: 2 };
const VARIANT3: Enum = Enum::Variant3;

const STRING: &'static str = "String";

const VEC: [u32; 8] = [0; 8];

const NESTED: (Struct, TupleStruct) = (STRUCT, TUPLE_STRUCT);

const UNSAFE_CELL: UnsafeCell<bool> = UnsafeCell { value: false };

fn main() {
let mut _constant = CONSTANT;
let mut _struct = STRUCT;
let mut _tuple_struct = TUPLE_STRUCT;
let mut _variant1 = VARIANT1;
let mut _variant2 = VARIANT2;
let mut _variant3 = VARIANT3;
let mut _string = STRING;
let mut _vec = VEC;
let mut _nested = NESTED;
let mut _extern = MUTEX_INIT;
let mut _unsafe_cell = UNSAFE_CELL;
}

0 comments on commit e316c66

Please sign in to comment.