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.
|
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
use super::FunctionDebugContext;
|
2015-04-29 18:14:37 +12:00
|
|
|
use super::metadata::file_metadata;
|
2016-04-07 22:35:11 +03:00
|
|
|
use super::utils::{DIB, span_start};
|
2015-04-29 18:14:37 +12:00
|
|
|
|
|
|
|
use llvm;
|
|
|
|
use llvm::debuginfo::{DIScope, DISubprogram};
|
2017-01-01 00:42:09 -07:00
|
|
|
use common::CrateContext;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::{Mir, VisibilityScope};
|
2015-04-29 18:14:37 +12:00
|
|
|
|
|
|
|
use libc::c_uint;
|
2016-04-07 22:35:11 +03:00
|
|
|
use std::ptr;
|
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
use syntax_pos::Pos;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
use rustc_data_structures::bitvec::BitVector;
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
|
2015-04-29 18:14:37 +12:00
|
|
|
|
2016-08-24 19:34:31 -07:00
|
|
|
use syntax_pos::BytePos;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct MirDebugScope {
|
|
|
|
pub scope_metadata: DIScope,
|
|
|
|
// Start and end offsets of the file to which this DIScope belongs.
|
|
|
|
// These are used to quickly determine whether some span refers to the same file.
|
|
|
|
pub file_start_pos: BytePos,
|
|
|
|
pub file_end_pos: BytePos,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MirDebugScope {
|
|
|
|
pub fn is_valid(&self) -> bool {
|
|
|
|
!self.scope_metadata.is_null()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 22:35:11 +03:00
|
|
|
/// Produce DIScope DIEs for each MIR Scope which has variables defined in it.
|
|
|
|
/// If debuginfo is disabled, the returned vector is empty.
|
2017-01-01 00:42:09 -07:00
|
|
|
pub fn create_mir_scopes(ccx: &CrateContext, mir: &Mir, debug_context: &FunctionDebugContext)
|
2016-12-18 10:18:47 -07:00
|
|
|
-> IndexVec<VisibilityScope, MirDebugScope> {
|
2016-08-24 19:34:31 -07:00
|
|
|
let null_scope = MirDebugScope {
|
|
|
|
scope_metadata: ptr::null_mut(),
|
|
|
|
file_start_pos: BytePos(0),
|
|
|
|
file_end_pos: BytePos(0)
|
|
|
|
};
|
|
|
|
let mut scopes = IndexVec::from_elem(null_scope, &mir.visibility_scopes);
|
2016-04-07 22:35:11 +03:00
|
|
|
|
2016-12-18 11:08:57 -07:00
|
|
|
let fn_metadata = match *debug_context {
|
2016-12-15 15:08:18 -07:00
|
|
|
FunctionDebugContext::RegularContext(ref data) => data.fn_metadata,
|
2016-04-07 22:35:11 +03:00
|
|
|
FunctionDebugContext::DebugInfoDisabled |
|
|
|
|
FunctionDebugContext::FunctionWithoutDebugInfo => {
|
|
|
|
return scopes;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find all the scopes with variables defined in them.
|
2016-05-31 20:27:36 +03:00
|
|
|
let mut has_variables = BitVector::new(mir.visibility_scopes.len());
|
2016-09-26 22:50:03 +02:00
|
|
|
for var in mir.vars_iter() {
|
2016-09-25 01:38:27 +02:00
|
|
|
let decl = &mir.local_decls[var];
|
2017-04-11 23:52:51 +03:00
|
|
|
has_variables.insert(decl.source_info.scope.index());
|
2016-04-07 22:35:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate all scopes.
|
2016-05-31 20:27:36 +03:00
|
|
|
for idx in 0..mir.visibility_scopes.len() {
|
|
|
|
let scope = VisibilityScope::new(idx);
|
2017-01-01 00:42:09 -07:00
|
|
|
make_mir_scope(ccx, &mir, &has_variables, fn_metadata, scope, &mut scopes);
|
2016-04-07 22:35:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
scopes
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_mir_scope(ccx: &CrateContext,
|
|
|
|
mir: &Mir,
|
|
|
|
has_variables: &BitVector,
|
|
|
|
fn_metadata: DISubprogram,
|
2016-05-31 20:27:36 +03:00
|
|
|
scope: VisibilityScope,
|
2016-08-24 19:34:31 -07:00
|
|
|
scopes: &mut IndexVec<VisibilityScope, MirDebugScope>) {
|
|
|
|
if scopes[scope].is_valid() {
|
2016-04-07 22:35:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 20:27:36 +03:00
|
|
|
let scope_data = &mir.visibility_scopes[scope];
|
2016-04-07 22:35:11 +03:00
|
|
|
let parent_scope = if let Some(parent) = scope_data.parent_scope {
|
|
|
|
make_mir_scope(ccx, mir, has_variables, fn_metadata, parent, scopes);
|
2016-06-07 17:28:36 +03:00
|
|
|
scopes[parent]
|
2016-04-07 22:35:11 +03:00
|
|
|
} else {
|
|
|
|
// The root is the function itself.
|
2016-08-24 19:34:31 -07:00
|
|
|
let loc = span_start(ccx, mir.span);
|
|
|
|
scopes[scope] = MirDebugScope {
|
|
|
|
scope_metadata: fn_metadata,
|
|
|
|
file_start_pos: loc.file.start_pos,
|
|
|
|
file_end_pos: loc.file.end_pos,
|
|
|
|
};
|
2016-04-07 22:35:11 +03:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
if !has_variables.contains(scope.index()) {
|
2016-04-07 22:35:11 +03:00
|
|
|
// Do not create a DIScope if there are no variables
|
|
|
|
// defined in this MIR Scope, to avoid debuginfo bloat.
|
2016-04-16 21:51:26 +03:00
|
|
|
|
|
|
|
// However, we don't skip creating a nested scope if
|
|
|
|
// our parent is the root, because we might want to
|
|
|
|
// put arguments in the root and not have shadowing.
|
2016-08-24 19:34:31 -07:00
|
|
|
if parent_scope.scope_metadata != fn_metadata {
|
2016-06-07 17:28:36 +03:00
|
|
|
scopes[scope] = parent_scope;
|
2016-04-16 21:51:26 +03:00
|
|
|
return;
|
2016-04-07 22:35:11 +03:00
|
|
|
}
|
2016-04-16 21:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let loc = span_start(ccx, scope_data.span);
|
2016-06-09 16:36:20 -04:00
|
|
|
let file_metadata = file_metadata(ccx, &loc.file.name, &loc.file.abs_path);
|
2016-08-24 19:34:31 -07:00
|
|
|
let scope_metadata = unsafe {
|
2016-08-02 02:35:09 +03:00
|
|
|
llvm::LLVMRustDIBuilderCreateLexicalBlock(
|
2016-04-16 21:51:26 +03:00
|
|
|
DIB(ccx),
|
2016-08-24 19:34:31 -07:00
|
|
|
parent_scope.scope_metadata,
|
2016-04-16 21:51:26 +03:00
|
|
|
file_metadata,
|
|
|
|
loc.line as c_uint,
|
|
|
|
loc.col.to_usize() as c_uint)
|
2016-04-07 22:35:11 +03:00
|
|
|
};
|
2016-08-24 19:34:31 -07:00
|
|
|
scopes[scope] = MirDebugScope {
|
|
|
|
scope_metadata: scope_metadata,
|
|
|
|
file_start_pos: loc.file.start_pos,
|
|
|
|
file_end_pos: loc.file.end_pos,
|
|
|
|
};
|
2016-04-07 22:35:11 +03:00
|
|
|
}
|