1
Fork 0
rust/src/librustc_codegen_llvm/debuginfo/source_loc.rs

117 lines
3.8 KiB
Rust
Raw Normal View History

2015-04-29 18:14:37 +12:00
// Copyright 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.
use self::InternalDebugLocation::*;
use super::utils::{debug_context, span_start};
2016-12-16 13:25:18 -07:00
use super::metadata::UNKNOWN_COLUMN_NUMBER;
use super::FunctionDebugContext;
2015-04-29 18:14:37 +12:00
use llvm;
use llvm::debuginfo::DIScope;
use builder::Builder;
2015-04-29 18:14:37 +12:00
use libc::c_uint;
use std::ptr::NonNull;
2016-12-16 13:25:18 -07:00
use syntax_pos::{Span, Pos};
2015-04-29 18:14:37 +12:00
/// Sets the current debug location at the beginning of the span.
///
/// Maps to a call to llvm::LLVMSetCurrentDebugLocation(...).
pub fn set_source_location(
debug_context: &FunctionDebugContext<'ll>,
bx: &Builder<'_, 'll, '_>,
scope: Option<&'ll DIScope>,
span: Span,
) {
let function_debug_context = match *debug_context {
2015-04-29 18:14:37 +12:00
FunctionDebugContext::DebugInfoDisabled => return,
FunctionDebugContext::FunctionWithoutDebugInfo => {
2018-01-05 07:12:32 +02:00
set_debug_location(bx, UnknownLocation);
2015-04-29 18:14:37 +12:00
return;
}
FunctionDebugContext::RegularContext(ref data) => data
};
2015-04-29 18:14:37 +12:00
let dbg_loc = if function_debug_context.source_locations_enabled.get() {
2018-01-05 07:12:32 +02:00
debug!("set_source_location: {}", bx.sess().codemap().span_to_string(span));
let loc = span_start(bx.cx, span);
InternalDebugLocation::new(scope.unwrap(), loc.line, loc.col.to_usize())
} else {
UnknownLocation
};
2018-01-05 07:12:32 +02:00
set_debug_location(bx, dbg_loc);
2015-04-29 18:14:37 +12:00
}
/// Enables emitting source locations for the given functions.
///
/// Since we don't want source locations to be emitted for the function prelude,
2018-05-08 16:10:16 +03:00
/// they are disabled when beginning to codegen a new function. This functions
2015-04-29 18:14:37 +12:00
/// switches source location emitting on and must therefore be called before the
2018-05-08 16:10:16 +03:00
/// first real statement/expression of the function is codegened.
pub fn start_emitting_source_locations(dbg_context: &FunctionDebugContext<'ll>) {
match *dbg_context {
FunctionDebugContext::RegularContext(ref data) => {
2015-04-29 18:14:37 +12:00
data.source_locations_enabled.set(true)
},
_ => { /* safe to ignore */ }
}
}
#[derive(Copy, Clone, PartialEq)]
pub enum InternalDebugLocation<'ll> {
KnownLocation { scope: &'ll DIScope, line: usize, col: usize },
2015-04-29 18:14:37 +12:00
UnknownLocation
}
impl InternalDebugLocation<'ll> {
pub fn new(scope: &'ll DIScope, line: usize, col: usize) -> Self {
2015-04-29 18:14:37 +12:00
KnownLocation {
scope,
line,
col,
2015-04-29 18:14:37 +12:00
}
}
}
pub fn set_debug_location(bx: &Builder<'_, 'll, '_>, debug_location: InternalDebugLocation<'ll>) {
let metadata_node = match debug_location {
KnownLocation { scope, line, col } => {
// For MSVC, set the column number to zero.
// Otherwise, emit it. This mimics clang behaviour.
// See discussion in https://github.com/rust-lang/rust/issues/42921
let col_used = if bx.cx.sess().target.target.options.is_like_msvc {
UNKNOWN_COLUMN_NUMBER
} else {
col as c_uint
};
2015-04-29 18:14:37 +12:00
debug!("setting debug location to {} {}", line, col);
unsafe {
NonNull::new(llvm::LLVMRustDIBuilderCreateDebugLocation(
2018-01-05 07:12:32 +02:00
debug_context(bx.cx).llcontext,
2015-04-29 18:14:37 +12:00
line as c_uint,
col_used,
2015-04-29 18:14:37 +12:00
scope,
None))
2015-04-29 18:14:37 +12:00
}
}
UnknownLocation => {
debug!("clearing debug location ");
None
2015-04-29 18:14:37 +12:00
}
};
unsafe {
2018-01-05 07:12:32 +02:00
llvm::LLVMSetCurrentDebugLocation(bx.llbuilder, metadata_node);
}
2015-04-29 18:14:37 +12:00
}