Skip to content

Commit 5059a3c

Browse files
committed
rustc_codegen_ssa: move debuginfo-related things to a new mir::debuginfo module.
1 parent 92df638 commit 5059a3c

File tree

7 files changed

+165
-156
lines changed

7 files changed

+165
-156
lines changed

src/librustc_codegen_llvm/debuginfo/create_scope_map.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_codegen_ssa::debuginfo::{FunctionDebugContext, FunctionDebugContextData, MirDebugScope};
1+
use rustc_codegen_ssa::mir::debuginfo::{FunctionDebugContext, FunctionDebugContextData, DebugScope};
22
use super::metadata::file_metadata;
33
use super::utils::{DIB, span_start};
44

@@ -22,8 +22,8 @@ pub fn create_mir_scopes(
2222
cx: &CodegenCx<'ll, '_>,
2323
mir: &Body<'_>,
2424
debug_context: &FunctionDebugContext<&'ll DISubprogram>,
25-
) -> IndexVec<SourceScope, MirDebugScope<&'ll DIScope>> {
26-
let null_scope = MirDebugScope {
25+
) -> IndexVec<SourceScope, DebugScope<&'ll DIScope>> {
26+
let null_scope = DebugScope {
2727
scope_metadata: None,
2828
file_start_pos: BytePos(0),
2929
file_end_pos: BytePos(0)
@@ -59,7 +59,7 @@ fn make_mir_scope(cx: &CodegenCx<'ll, '_>,
5959
has_variables: &BitSet<SourceScope>,
6060
debug_context: &FunctionDebugContextData<&'ll DISubprogram>,
6161
scope: SourceScope,
62-
scopes: &mut IndexVec<SourceScope, MirDebugScope<&'ll DIScope>>) {
62+
scopes: &mut IndexVec<SourceScope, DebugScope<&'ll DIScope>>) {
6363
if scopes[scope].is_valid() {
6464
return;
6565
}
@@ -71,7 +71,7 @@ fn make_mir_scope(cx: &CodegenCx<'ll, '_>,
7171
} else {
7272
// The root is the function itself.
7373
let loc = span_start(cx, mir.span);
74-
scopes[scope] = MirDebugScope {
74+
scopes[scope] = DebugScope {
7575
scope_metadata: Some(debug_context.fn_metadata),
7676
file_start_pos: loc.file.start_pos,
7777
file_end_pos: loc.file.end_pos,
@@ -105,7 +105,7 @@ fn make_mir_scope(cx: &CodegenCx<'ll, '_>,
105105
loc.line as c_uint,
106106
loc.col.to_usize() as c_uint))
107107
};
108-
scopes[scope] = MirDebugScope {
108+
scopes[scope] = DebugScope {
109109
scope_metadata,
110110
file_start_pos: loc.file.start_pos,
111111
file_end_pos: loc.file.end_pos,

src/librustc_codegen_llvm/debuginfo/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// See doc.rs for documentation.
22
mod doc;
33

4-
use rustc_codegen_ssa::debuginfo::VariableAccess::*;
5-
use rustc_codegen_ssa::debuginfo::VariableKind::*;
4+
use rustc_codegen_ssa::mir::debuginfo::VariableAccess::*;
5+
use rustc_codegen_ssa::mir::debuginfo::VariableKind::*;
66

77
use self::utils::{DIB, span_start, create_DIArray, is_node_local_to_unit};
88
use self::namespace::mangled_name_of_instance;
@@ -27,8 +27,9 @@ use rustc::session::config::{self, DebugInfo};
2727
use rustc::util::nodemap::{DefIdMap, FxHashMap, FxHashSet};
2828
use rustc_data_structures::small_c_str::SmallCStr;
2929
use rustc_index::vec::IndexVec;
30-
use rustc_codegen_ssa::debuginfo::{FunctionDebugContext, MirDebugScope, VariableAccess,
31-
VariableKind, FunctionDebugContextData, type_names};
30+
use rustc_codegen_ssa::debuginfo::type_names;
31+
use rustc_codegen_ssa::mir::debuginfo::{FunctionDebugContext, DebugScope, VariableAccess,
32+
VariableKind, FunctionDebugContextData};
3233

3334
use libc::c_uint;
3435
use std::cell::RefCell;
@@ -553,7 +554,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
553554
&self,
554555
mir: &mir::Body<'_>,
555556
debug_context: &mut FunctionDebugContext<&'ll DISubprogram>,
556-
) -> IndexVec<mir::SourceScope, MirDebugScope<&'ll DIScope>> {
557+
) -> IndexVec<mir::SourceScope, DebugScope<&'ll DIScope>> {
557558
create_scope_map::create_mir_scopes(self, mir, debug_context)
558559
}
559560

src/librustc_codegen_llvm/debuginfo/source_loc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use self::InternalDebugLocation::*;
22

33
use super::utils::{debug_context, span_start};
44
use super::metadata::UNKNOWN_COLUMN_NUMBER;
5-
use rustc_codegen_ssa::debuginfo::FunctionDebugContext;
5+
use rustc_codegen_ssa::mir::debuginfo::FunctionDebugContext;
66

77
use crate::llvm;
88
use crate::llvm::debuginfo::DIScope;
Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,2 @@
1-
use syntax_pos::{BytePos, Span};
2-
use rustc::hir::def_id::CrateNum;
3-
1+
// FIXME(eddyb) find a place for this (or a way to replace it).
42
pub mod type_names;
5-
6-
pub enum FunctionDebugContext<D> {
7-
RegularContext(FunctionDebugContextData<D>),
8-
DebugInfoDisabled,
9-
FunctionWithoutDebugInfo,
10-
}
11-
12-
impl<D> FunctionDebugContext<D> {
13-
pub fn get_ref(&self, span: Span) -> &FunctionDebugContextData<D> {
14-
match *self {
15-
FunctionDebugContext::RegularContext(ref data) => data,
16-
FunctionDebugContext::DebugInfoDisabled => {
17-
span_bug!(
18-
span,
19-
"debuginfo: Error trying to access FunctionDebugContext \
20-
although debug info is disabled!",
21-
);
22-
}
23-
FunctionDebugContext::FunctionWithoutDebugInfo => {
24-
span_bug!(
25-
span,
26-
"debuginfo: Error trying to access FunctionDebugContext \
27-
for function that should be ignored by debug info!",
28-
);
29-
}
30-
}
31-
}
32-
}
33-
34-
/// Enables emitting source locations for the given functions.
35-
///
36-
/// Since we don't want source locations to be emitted for the function prelude,
37-
/// they are disabled when beginning to codegen a new function. This functions
38-
/// switches source location emitting on and must therefore be called before the
39-
/// first real statement/expression of the function is codegened.
40-
pub fn start_emitting_source_locations<D>(dbg_context: &mut FunctionDebugContext<D>) {
41-
match *dbg_context {
42-
FunctionDebugContext::RegularContext(ref mut data) => {
43-
data.source_locations_enabled = true;
44-
},
45-
_ => { /* safe to ignore */ }
46-
}
47-
}
48-
49-
pub struct FunctionDebugContextData<D> {
50-
pub fn_metadata: D,
51-
pub source_locations_enabled: bool,
52-
pub defining_crate: CrateNum,
53-
}
54-
55-
pub enum VariableAccess<'a, V> {
56-
// The llptr given is an alloca containing the variable's value
57-
DirectVariable { alloca: V },
58-
// The llptr given is an alloca containing the start of some pointer chain
59-
// leading to the variable's content.
60-
IndirectVariable { alloca: V, address_operations: &'a [i64] }
61-
}
62-
63-
pub enum VariableKind {
64-
ArgumentVariable(usize /*index*/),
65-
LocalVariable,
66-
}
67-
68-
69-
#[derive(Clone, Copy, Debug)]
70-
pub struct MirDebugScope<D> {
71-
pub scope_metadata: Option<D>,
72-
// Start and end offsets of the file to which this DIScope belongs.
73-
// These are used to quickly determine whether some span refers to the same file.
74-
pub file_start_pos: BytePos,
75-
pub file_end_pos: BytePos,
76-
}
77-
78-
impl<D> MirDebugScope<D> {
79-
pub fn is_valid(&self) -> bool {
80-
!self.scope_metadata.is_none()
81-
}
82-
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
use rustc::hir::def_id::CrateNum;
2+
use rustc::mir;
3+
use crate::traits::*;
4+
5+
use syntax_pos::{DUMMY_SP, BytePos, Span};
6+
7+
use super::FunctionCx;
8+
9+
pub enum FunctionDebugContext<D> {
10+
RegularContext(FunctionDebugContextData<D>),
11+
DebugInfoDisabled,
12+
FunctionWithoutDebugInfo,
13+
}
14+
15+
impl<D> FunctionDebugContext<D> {
16+
pub fn get_ref(&self, span: Span) -> &FunctionDebugContextData<D> {
17+
match *self {
18+
FunctionDebugContext::RegularContext(ref data) => data,
19+
FunctionDebugContext::DebugInfoDisabled => {
20+
span_bug!(
21+
span,
22+
"debuginfo: Error trying to access FunctionDebugContext \
23+
although debug info is disabled!",
24+
);
25+
}
26+
FunctionDebugContext::FunctionWithoutDebugInfo => {
27+
span_bug!(
28+
span,
29+
"debuginfo: Error trying to access FunctionDebugContext \
30+
for function that should be ignored by debug info!",
31+
);
32+
}
33+
}
34+
}
35+
}
36+
37+
/// Enables emitting source locations for the given functions.
38+
///
39+
/// Since we don't want source locations to be emitted for the function prelude,
40+
/// they are disabled when beginning to codegen a new function. This functions
41+
/// switches source location emitting on and must therefore be called before the
42+
/// first real statement/expression of the function is codegened.
43+
pub fn start_emitting_source_locations<D>(dbg_context: &mut FunctionDebugContext<D>) {
44+
match *dbg_context {
45+
FunctionDebugContext::RegularContext(ref mut data) => {
46+
data.source_locations_enabled = true;
47+
},
48+
_ => { /* safe to ignore */ }
49+
}
50+
}
51+
52+
pub struct FunctionDebugContextData<D> {
53+
pub fn_metadata: D,
54+
pub source_locations_enabled: bool,
55+
pub defining_crate: CrateNum,
56+
}
57+
58+
pub enum VariableAccess<'a, V> {
59+
// The llptr given is an alloca containing the variable's value
60+
DirectVariable { alloca: V },
61+
// The llptr given is an alloca containing the start of some pointer chain
62+
// leading to the variable's content.
63+
IndirectVariable { alloca: V, address_operations: &'a [i64] }
64+
}
65+
66+
pub enum VariableKind {
67+
ArgumentVariable(usize /*index*/),
68+
LocalVariable,
69+
}
70+
71+
72+
#[derive(Clone, Copy, Debug)]
73+
pub struct DebugScope<D> {
74+
pub scope_metadata: Option<D>,
75+
// Start and end offsets of the file to which this DIScope belongs.
76+
// These are used to quickly determine whether some span refers to the same file.
77+
pub file_start_pos: BytePos,
78+
pub file_end_pos: BytePos,
79+
}
80+
81+
impl<D> DebugScope<D> {
82+
pub fn is_valid(&self) -> bool {
83+
!self.scope_metadata.is_none()
84+
}
85+
}
86+
87+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
88+
pub fn set_debug_loc(
89+
&mut self,
90+
bx: &mut Bx,
91+
source_info: mir::SourceInfo
92+
) {
93+
let (scope, span) = self.debug_loc(source_info);
94+
bx.set_source_location(&mut self.debug_context, scope, span);
95+
}
96+
97+
pub fn debug_loc(&self, source_info: mir::SourceInfo) -> (Option<Bx::DIScope>, Span) {
98+
// Bail out if debug info emission is not enabled.
99+
match self.debug_context {
100+
FunctionDebugContext::DebugInfoDisabled |
101+
FunctionDebugContext::FunctionWithoutDebugInfo => {
102+
return (self.scopes[source_info.scope].scope_metadata, source_info.span);
103+
}
104+
FunctionDebugContext::RegularContext(_) =>{}
105+
}
106+
107+
// In order to have a good line stepping behavior in debugger, we overwrite debug
108+
// locations of macro expansions with that of the outermost expansion site
109+
// (unless the crate is being compiled with `-Z debug-macros`).
110+
if !source_info.span.from_expansion() ||
111+
self.cx.sess().opts.debugging_opts.debug_macros {
112+
let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo());
113+
(scope, source_info.span)
114+
} else {
115+
// Walk up the macro expansion chain until we reach a non-expanded span.
116+
// We also stop at the function body level because no line stepping can occur
117+
// at the level above that.
118+
let span = syntax_pos::hygiene::walk_chain(source_info.span, self.mir.span.ctxt());
119+
let scope = self.scope_metadata_for_loc(source_info.scope, span.lo());
120+
// Use span of the outermost expansion site, while keeping the original lexical scope.
121+
(scope, span)
122+
}
123+
}
124+
125+
// DILocations inherit source file name from the parent DIScope. Due to macro expansions
126+
// it may so happen that the current span belongs to a different file than the DIScope
127+
// corresponding to span's containing source scope. If so, we need to create a DIScope
128+
// "extension" into that file.
129+
fn scope_metadata_for_loc(&self, scope_id: mir::SourceScope, pos: BytePos)
130+
-> Option<Bx::DIScope> {
131+
let scope_metadata = self.scopes[scope_id].scope_metadata;
132+
if pos < self.scopes[scope_id].file_start_pos ||
133+
pos >= self.scopes[scope_id].file_end_pos {
134+
let sm = self.cx.sess().source_map();
135+
let defining_crate = self.debug_context.get_ref(DUMMY_SP).defining_crate;
136+
Some(self.cx.extend_scope_to_file(
137+
scope_metadata.unwrap(),
138+
&sm.lookup_char_pos(pos).file,
139+
defining_crate
140+
))
141+
} else {
142+
scope_metadata
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)