Skip to content

Commit c2e7743

Browse files
committed
rustc_codegen_ssa: move debuginfo scopes into FunctionDebugContext.
1 parent 06869b8 commit c2e7743

File tree

6 files changed

+88
-154
lines changed

6 files changed

+88
-154
lines changed

src/librustc_codegen_llvm/debuginfo/create_scope_map.rs

+21-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_codegen_ssa::mir::debuginfo::{FunctionDebugContext, FunctionDebugContextData, DebugScope};
1+
use rustc_codegen_ssa::mir::debuginfo::{FunctionDebugContext, DebugScope};
22
use super::metadata::file_metadata;
33
use super::utils::{DIB, span_start};
44

@@ -12,34 +12,20 @@ use libc::c_uint;
1212
use syntax_pos::Pos;
1313

1414
use rustc_index::bit_set::BitSet;
15-
use rustc_index::vec::{Idx, IndexVec};
16-
17-
use syntax_pos::BytePos;
15+
use rustc_index::vec::Idx;
1816

1917
/// Produces DIScope DIEs for each MIR Scope which has variables defined in it.
20-
/// If debuginfo is disabled, the returned vector is empty.
21-
pub fn create_mir_scopes(
18+
pub fn compute_mir_scopes(
2219
cx: &CodegenCx<'ll, '_>,
2320
mir: &Body<'_>,
24-
debug_context: &FunctionDebugContext<&'ll DISubprogram>,
25-
) -> IndexVec<SourceScope, DebugScope<&'ll DIScope>> {
26-
let null_scope = DebugScope {
27-
scope_metadata: None,
28-
file_start_pos: BytePos(0),
29-
file_end_pos: BytePos(0)
30-
};
31-
let mut scopes = IndexVec::from_elem(null_scope, &mir.source_scopes);
32-
33-
let debug_context = match *debug_context {
34-
FunctionDebugContext::RegularContext(ref data) => data,
35-
FunctionDebugContext::DebugInfoDisabled |
36-
FunctionDebugContext::FunctionWithoutDebugInfo => {
37-
return scopes;
38-
}
39-
};
40-
21+
fn_metadata: &'ll DISubprogram,
22+
debug_context: &mut FunctionDebugContext<&'ll DIScope>,
23+
) {
4124
// Find all the scopes with variables defined in them.
4225
let mut has_variables = BitSet::new_empty(mir.source_scopes.len());
26+
// FIXME(eddyb) base this on `decl.name`, or even better, on debuginfo.
27+
// FIXME(eddyb) take into account that arguments always have debuginfo,
28+
// irrespective of their name (assuming full debuginfo is enabled).
4329
for var in mir.vars_iter() {
4430
let decl = &mir.local_decls[var];
4531
has_variables.insert(decl.visibility_scope);
@@ -48,31 +34,29 @@ pub fn create_mir_scopes(
4834
// Instantiate all scopes.
4935
for idx in 0..mir.source_scopes.len() {
5036
let scope = SourceScope::new(idx);
51-
make_mir_scope(cx, &mir, &has_variables, debug_context, scope, &mut scopes);
37+
make_mir_scope(cx, &mir, fn_metadata, &has_variables, debug_context, scope);
5238
}
53-
54-
scopes
5539
}
5640

5741
fn make_mir_scope(cx: &CodegenCx<'ll, '_>,
5842
mir: &Body<'_>,
43+
fn_metadata: &'ll DISubprogram,
5944
has_variables: &BitSet<SourceScope>,
60-
debug_context: &FunctionDebugContextData<&'ll DISubprogram>,
61-
scope: SourceScope,
62-
scopes: &mut IndexVec<SourceScope, DebugScope<&'ll DIScope>>) {
63-
if scopes[scope].is_valid() {
45+
debug_context: &mut FunctionDebugContext<&'ll DISubprogram>,
46+
scope: SourceScope) {
47+
if debug_context.scopes[scope].is_valid() {
6448
return;
6549
}
6650

6751
let scope_data = &mir.source_scopes[scope];
6852
let parent_scope = if let Some(parent) = scope_data.parent_scope {
69-
make_mir_scope(cx, mir, has_variables, debug_context, parent, scopes);
70-
scopes[parent]
53+
make_mir_scope(cx, mir, fn_metadata, has_variables, debug_context, parent);
54+
debug_context.scopes[parent]
7155
} else {
7256
// The root is the function itself.
7357
let loc = span_start(cx, mir.span);
74-
scopes[scope] = DebugScope {
75-
scope_metadata: Some(debug_context.fn_metadata),
58+
debug_context.scopes[scope] = DebugScope {
59+
scope_metadata: Some(fn_metadata),
7660
file_start_pos: loc.file.start_pos,
7761
file_end_pos: loc.file.end_pos,
7862
};
@@ -86,8 +70,8 @@ fn make_mir_scope(cx: &CodegenCx<'ll, '_>,
8670
// However, we don't skip creating a nested scope if
8771
// our parent is the root, because we might want to
8872
// put arguments in the root and not have shadowing.
89-
if parent_scope.scope_metadata.unwrap() != debug_context.fn_metadata {
90-
scopes[scope] = parent_scope;
73+
if parent_scope.scope_metadata.unwrap() != fn_metadata {
74+
debug_context.scopes[scope] = parent_scope;
9175
return;
9276
}
9377
}
@@ -105,7 +89,7 @@ fn make_mir_scope(cx: &CodegenCx<'ll, '_>,
10589
loc.line as c_uint,
10690
loc.col.to_usize() as c_uint))
10791
};
108-
scopes[scope] = DebugScope {
92+
debug_context.scopes[scope] = DebugScope {
10993
scope_metadata,
11094
file_start_pos: loc.file.start_pos,
11195
file_end_pos: loc.file.end_pos,

src/librustc_codegen_llvm/debuginfo/mod.rs

+26-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use self::metadata::{type_metadata, file_metadata, TypeMap};
1111
use self::source_loc::InternalDebugLocation::{self, UnknownLocation};
1212

1313
use crate::llvm;
14-
use crate::llvm::debuginfo::{DIFile, DIType, DIScope, DIBuilder, DISubprogram, DIArray, DIFlags,
14+
use crate::llvm::debuginfo::{DIFile, DIType, DIScope, DIBuilder, DIArray, DIFlags,
1515
DISPFlags, DILexicalBlock};
1616
use rustc::hir::CodegenFnAttrFlags;
1717
use rustc::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
@@ -29,13 +29,13 @@ use rustc_data_structures::small_c_str::SmallCStr;
2929
use rustc_index::vec::IndexVec;
3030
use rustc_codegen_ssa::debuginfo::type_names;
3131
use rustc_codegen_ssa::mir::debuginfo::{FunctionDebugContext, DebugScope, VariableAccess,
32-
VariableKind, FunctionDebugContextData};
32+
VariableKind};
3333

3434
use libc::c_uint;
3535
use std::cell::RefCell;
3636
use std::ffi::{CStr, CString};
3737

38-
use syntax_pos::{self, Span, Pos};
38+
use syntax_pos::{self, BytePos, Span, Pos};
3939
use syntax::ast;
4040
use syntax::symbol::Symbol;
4141
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt};
@@ -48,7 +48,7 @@ pub mod metadata;
4848
mod create_scope_map;
4949
mod source_loc;
5050

51-
pub use self::create_scope_map::{create_mir_scopes};
51+
pub use self::create_scope_map::compute_mir_scopes;
5252
pub use self::metadata::create_global_var_metadata;
5353
pub use self::metadata::extend_scope_to_file;
5454
pub use self::source_loc::set_source_location;
@@ -149,21 +149,21 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
149149
impl DebugInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
150150
fn declare_local(
151151
&mut self,
152-
dbg_context: &FunctionDebugContext<&'ll DISubprogram>,
152+
dbg_context: &FunctionDebugContext<&'ll DIScope>,
153153
variable_name: ast::Name,
154154
variable_type: Ty<'tcx>,
155155
scope_metadata: &'ll DIScope,
156156
variable_access: VariableAccess<'_, &'ll Value>,
157157
variable_kind: VariableKind,
158158
span: Span,
159159
) {
160-
assert!(!dbg_context.get_ref(span).source_locations_enabled);
160+
assert!(!dbg_context.source_locations_enabled);
161161
let cx = self.cx();
162162

163163
let file = span_start(cx, span).file;
164164
let file_metadata = file_metadata(cx,
165165
&file.name,
166-
dbg_context.get_ref(span).defining_crate);
166+
dbg_context.defining_crate);
167167

168168
let loc = span_start(cx, span);
169169
let type_metadata = type_metadata(cx, variable_type, span);
@@ -215,8 +215,8 @@ impl DebugInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
215215

216216
fn set_source_location(
217217
&mut self,
218-
debug_context: &mut FunctionDebugContext<&'ll DISubprogram>,
219-
scope: Option<&'ll DIScope>,
218+
debug_context: &mut FunctionDebugContext<&'ll DIScope>,
219+
scope: &'ll DIScope,
220220
span: Span,
221221
) {
222222
set_source_location(debug_context, &self, scope, span)
@@ -269,14 +269,14 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
269269
sig: ty::FnSig<'tcx>,
270270
llfn: &'ll Value,
271271
mir: &mir::Body<'_>,
272-
) -> FunctionDebugContext<&'ll DISubprogram> {
272+
) -> Option<FunctionDebugContext<&'ll DIScope>> {
273273
if self.sess().opts.debuginfo == DebugInfo::None {
274-
return FunctionDebugContext::DebugInfoDisabled;
274+
return None;
275275
}
276276

277277
if let InstanceDef::Item(def_id) = instance.def {
278278
if self.tcx().codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NO_DEBUG) {
279-
return FunctionDebugContext::FunctionWithoutDebugInfo;
279+
return None;
280280
}
281281
}
282282

@@ -285,7 +285,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
285285
// This can be the case for functions inlined from another crate
286286
if span.is_dummy() {
287287
// FIXME(simulacrum): Probably can't happen; remove.
288-
return FunctionDebugContext::FunctionWithoutDebugInfo;
288+
return None;
289289
}
290290

291291
let def_id = instance.def_id();
@@ -358,14 +358,23 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
358358
None)
359359
};
360360

361-
// Initialize fn debug context (including scope map and namespace map)
362-
let fn_debug_context = FunctionDebugContextData {
363-
fn_metadata,
361+
// Initialize fn debug context (including scopes).
362+
// FIXME(eddyb) figure out a way to not need `Option` for `scope_metadata`.
363+
let null_scope = DebugScope {
364+
scope_metadata: None,
365+
file_start_pos: BytePos(0),
366+
file_end_pos: BytePos(0)
367+
};
368+
let mut fn_debug_context = FunctionDebugContext {
369+
scopes: IndexVec::from_elem(null_scope, &mir.source_scopes),
364370
source_locations_enabled: false,
365371
defining_crate: def_id.krate,
366372
};
367373

368-
return FunctionDebugContext::RegularContext(fn_debug_context);
374+
// Fill in all the scopes, with the information from the MIR body.
375+
compute_mir_scopes(self, mir, fn_metadata, &mut fn_debug_context);
376+
377+
return Some(fn_debug_context);
369378

370379
fn get_function_signature<'ll, 'tcx>(
371380
cx: &CodegenCx<'ll, 'tcx>,
@@ -550,14 +559,6 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
550559
metadata::create_vtable_metadata(self, ty, vtable)
551560
}
552561

553-
fn create_mir_scopes(
554-
&self,
555-
mir: &mir::Body<'_>,
556-
debug_context: &mut FunctionDebugContext<&'ll DISubprogram>,
557-
) -> IndexVec<mir::SourceScope, DebugScope<&'ll DIScope>> {
558-
create_scope_map::create_mir_scopes(self, mir, debug_context)
559-
}
560-
561562
fn extend_scope_to_file(
562563
&self,
563564
scope_metadata: &'ll DIScope,

src/librustc_codegen_llvm/debuginfo/source_loc.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,13 @@ use syntax_pos::{Span, Pos};
1818
pub fn set_source_location<D>(
1919
debug_context: &FunctionDebugContext<D>,
2020
bx: &Builder<'_, 'll, '_>,
21-
scope: Option<&'ll DIScope>,
21+
scope: &'ll DIScope,
2222
span: Span,
2323
) {
24-
let function_debug_context = match *debug_context {
25-
FunctionDebugContext::DebugInfoDisabled => return,
26-
FunctionDebugContext::FunctionWithoutDebugInfo => {
27-
set_debug_location(bx, UnknownLocation);
28-
return;
29-
}
30-
FunctionDebugContext::RegularContext(ref data) => data
31-
};
32-
33-
let dbg_loc = if function_debug_context.source_locations_enabled {
24+
let dbg_loc = if debug_context.source_locations_enabled {
3425
debug!("set_source_location: {}", bx.sess().source_map().span_to_string(span));
3526
let loc = span_start(bx.cx(), span);
36-
InternalDebugLocation::new(scope.unwrap(), loc.line, loc.col.to_usize())
27+
InternalDebugLocation::new(scope, loc.line, loc.col.to_usize())
3728
} else {
3829
UnknownLocation
3930
};

0 commit comments

Comments
 (0)