Skip to content

Commit 0c51f2f

Browse files
committed
Use byte offsets when emitting debuginfo columns
1 parent e1a5472 commit 0c51f2f

File tree

6 files changed

+68
-48
lines changed

6 files changed

+68
-48
lines changed

src/librustc_codegen_llvm/debuginfo/create_scope_map.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
use super::metadata::file_metadata;
2-
use super::utils::{span_start, DIB};
1+
use super::metadata::{file_metadata, UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER};
2+
use super::utils::DIB;
33
use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext};
44

55
use crate::common::CodegenCx;
66
use crate::llvm;
77
use crate::llvm::debuginfo::{DIScope, DISubprogram};
88
use rustc::mir::{Body, SourceScope};
99

10-
use libc::c_uint;
11-
12-
use rustc_span::Pos;
13-
1410
use rustc_index::bit_set::BitSet;
1511
use rustc_index::vec::Idx;
1612

@@ -54,7 +50,7 @@ fn make_mir_scope(
5450
debug_context.scopes[parent]
5551
} else {
5652
// The root is the function itself.
57-
let loc = span_start(cx, mir.span);
53+
let loc = cx.lookup_debug_loc(mir.span.lo());
5854
debug_context.scopes[scope] = DebugScope {
5955
scope_metadata: Some(fn_metadata),
6056
file_start_pos: loc.file.start_pos,
@@ -70,17 +66,16 @@ fn make_mir_scope(
7066
return;
7167
}
7268

73-
let loc = span_start(cx, scope_data.span);
69+
let loc = cx.lookup_debug_loc(scope_data.span.lo());
7470
let file_metadata = file_metadata(cx, &loc.file.name, debug_context.defining_crate);
7571

7672
let scope_metadata = unsafe {
7773
Some(llvm::LLVMRustDIBuilderCreateLexicalBlock(
7874
DIB(cx),
7975
parent_scope.scope_metadata.unwrap(),
8076
file_metadata,
81-
loc.line as c_uint,
82-
// Loc column is 0-based while debug one is 1-based.
83-
loc.col.to_usize() as c_uint + 1,
77+
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
78+
loc.col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
8479
))
8580
};
8681
debug_context.scopes[scope] = DebugScope {

src/librustc_codegen_llvm/debuginfo/metadata.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use self::RecursiveTypeDescription::*;
55
use super::namespace::mangled_name_of_instance;
66
use super::type_names::compute_debuginfo_type_name;
77
use super::utils::{
8-
create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit, span_start, DIB,
8+
create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit, DIB,
99
};
1010
use super::CrateDebugContext;
1111

@@ -2280,10 +2280,10 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global
22802280
let span = tcx.def_span(def_id);
22812281

22822282
let (file_metadata, line_number) = if !span.is_dummy() {
2283-
let loc = span_start(cx, span);
2284-
(file_metadata(cx, &loc.file.name, LOCAL_CRATE), loc.line as c_uint)
2283+
let loc = cx.lookup_debug_loc(span.lo());
2284+
(file_metadata(cx, &loc.file.name, LOCAL_CRATE), loc.line)
22852285
} else {
2286-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
2286+
(unknown_file_metadata(cx), None)
22872287
};
22882288

22892289
let is_local_to_unit = is_node_local_to_unit(cx, def_id);
@@ -2308,7 +2308,7 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global
23082308
// which is what we want for no_mangle statics
23092309
linkage_name.as_ref().map_or(ptr::null(), |name| name.as_ptr()),
23102310
file_metadata,
2311-
line_number,
2311+
line_number.unwrap_or(UNKNOWN_LINE_NUMBER),
23122312
type_metadata,
23132313
is_local_to_unit,
23142314
global,

src/librustc_codegen_llvm/debuginfo/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ mod doc;
33

44
use rustc_codegen_ssa::mir::debuginfo::VariableKind::*;
55

6-
use self::metadata::{file_metadata, type_metadata, TypeMap};
6+
use self::metadata::{file_metadata, type_metadata, TypeMap, UNKNOWN_LINE_NUMBER};
77
use self::namespace::mangled_name_of_instance;
88
use self::type_names::compute_debuginfo_type_name;
9-
use self::utils::{create_DIArray, is_node_local_to_unit, span_start, DIB};
9+
use self::utils::{create_DIArray, is_node_local_to_unit, DIB};
1010

1111
use crate::llvm;
1212
use crate::llvm::debuginfo::{
@@ -257,7 +257,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
257257

258258
let def_id = instance.def_id();
259259
let containing_scope = get_containing_scope(self, instance);
260-
let loc = span_start(self, span);
260+
let loc = self.lookup_debug_loc(span.lo());
261261
let file_metadata = file_metadata(self, &loc.file.name, def_id.krate);
262262

263263
let function_type_metadata = unsafe {
@@ -313,9 +313,9 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
313313
function_name.as_ptr(),
314314
linkage_name.as_ptr(),
315315
file_metadata,
316-
loc.line as c_uint,
316+
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
317317
function_type_metadata,
318-
scope_line as c_uint,
318+
scope_line.unwrap_or(UNKNOWN_LINE_NUMBER),
319319
flags,
320320
spflags,
321321
llfn,
@@ -538,7 +538,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
538538
variable_kind: VariableKind,
539539
span: Span,
540540
) -> &'ll DIVariable {
541-
let loc = span_start(self, span);
541+
let loc = self.lookup_debug_loc(span.lo());
542542
let file_metadata = file_metadata(self, &loc.file.name, dbg_context.defining_crate);
543543

544544
let type_metadata = type_metadata(self, variable_type, span);
@@ -557,7 +557,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
557557
scope_metadata,
558558
name.as_ptr(),
559559
file_metadata,
560-
loc.line as c_uint,
560+
loc.line.unwrap_or(UNKNOWN_LINE_NUMBER),
561561
type_metadata,
562562
true,
563563
DIFlags::FlagZero,

src/librustc_codegen_llvm/debuginfo/source_loc.rs

+39-14
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
1-
use super::metadata::UNKNOWN_COLUMN_NUMBER;
2-
use super::utils::{debug_context, span_start};
1+
use super::metadata::{UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER};
2+
use super::utils::debug_context;
33

44
use crate::common::CodegenCx;
55
use crate::llvm::debuginfo::DIScope;
66
use crate::llvm::{self, Value};
77
use rustc_codegen_ssa::traits::*;
88

9-
use libc::c_uint;
10-
use rustc_span::{Pos, Span};
9+
use rustc_data_structures::sync::Lrc;
10+
use rustc_span::{BytePos, Pos, SourceFile, SourceFileAndLine, Span};
11+
12+
/// A source code location used to generate debug information.
13+
pub struct DebugLoc {
14+
/// Information about the original source file.
15+
pub file: Lrc<SourceFile>,
16+
/// The (1-based) line number.
17+
pub line: Option<u32>,
18+
/// The (1-based) column number.
19+
pub col: Option<u32>,
20+
}
1121

1222
impl CodegenCx<'ll, '_> {
13-
pub fn create_debug_loc(&self, scope: &'ll DIScope, span: Span) -> &'ll Value {
14-
let loc = span_start(self, span);
23+
/// Looks up debug source information about a `BytePos`.
24+
pub fn lookup_debug_loc(&self, pos: BytePos) -> DebugLoc {
25+
let (file, line, col) = match self.sess().source_map().lookup_line(pos) {
26+
Ok(SourceFileAndLine { sf: file, line }) => {
27+
let line_pos = file.line_begin_pos(pos);
28+
29+
// Use 1-based indexing.
30+
let line = (line + 1) as u32;
31+
let col = (pos - line_pos).to_u32() + 1;
32+
33+
(file, Some(line), Some(col))
34+
}
35+
Err(file) => (file, None, None),
36+
};
1537

16-
// For MSVC, set the column number to zero.
38+
// For MSVC, omit the column number.
1739
// Otherwise, emit it. This mimics clang behaviour.
1840
// See discussion in https://github.com/rust-lang/rust/issues/42921
19-
let col_used = if self.sess().target.target.options.is_like_msvc {
20-
UNKNOWN_COLUMN_NUMBER
41+
if self.sess().target.target.options.is_like_msvc {
42+
DebugLoc { file, line, col: None }
2143
} else {
22-
// Loc column is 0-based while debug one is 1-based.
23-
loc.col.to_usize() as c_uint + 1
24-
};
44+
DebugLoc { file, line, col }
45+
}
46+
}
47+
48+
pub fn create_debug_loc(&self, scope: &'ll DIScope, span: Span) -> &'ll Value {
49+
let DebugLoc { line, col, .. } = self.lookup_debug_loc(span.lo());
2550

2651
unsafe {
2752
llvm::LLVMRustDIBuilderCreateDebugLocation(
2853
debug_context(self).llcontext,
29-
loc.line as c_uint,
30-
col_used,
54+
line.unwrap_or(UNKNOWN_LINE_NUMBER),
55+
col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
3156
scope,
3257
None,
3358
)

src/librustc_codegen_llvm/debuginfo/utils.rs

-8
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ use rustc_hir::def_id::DefId;
99
use crate::common::CodegenCx;
1010
use crate::llvm;
1111
use crate::llvm::debuginfo::{DIArray, DIBuilder, DIDescriptor, DIScope};
12-
use rustc_codegen_ssa::traits::*;
13-
14-
use rustc_span::Span;
1512

1613
pub fn is_node_local_to_unit(cx: &CodegenCx<'_, '_>, def_id: DefId) -> bool {
1714
// The is_local_to_unit flag indicates whether a function is local to the
@@ -32,11 +29,6 @@ pub fn create_DIArray(builder: &DIBuilder<'ll>, arr: &[Option<&'ll DIDescriptor>
3229
};
3330
}
3431

35-
/// Returns rustc_span::Loc corresponding to the beginning of the span
36-
pub fn span_start(cx: &CodegenCx<'_, '_>, span: Span) -> rustc_span::Loc {
37-
cx.sess().source_map().lookup_char_pos(span.lo())
38-
}
39-
4032
#[inline]
4133
pub fn debug_context(cx: &'a CodegenCx<'ll, 'tcx>) -> &'a CrateDebugContext<'ll, 'tcx> {
4234
cx.dbg_cx.as_ref().unwrap()

src/test/codegen/debug-column.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
// Verify that emitted debuginfo column nubmers are 1-based. Regression test for issue #65437.
1+
// Verify that debuginfo column nubmers are 1-based byte offsets.
22
//
33
// ignore-windows
44
// compile-flags: -C debuginfo=2
55

66
fn main() {
77
unsafe {
8-
// CHECK: call void @giraffe(), !dbg [[DBG:!.*]]
9-
// CHECK: [[DBG]] = !DILocation(line: 10, column: 9
8+
// Column numbers are 1-based. Regression test for #65437.
9+
// CHECK: call void @giraffe(), !dbg [[A:!.*]]
1010
giraffe();
11+
12+
// Column numbers use byte offests. Regression test for #67360
13+
// CHECK: call void @turtle(), !dbg [[B:!.*]]
14+
/* ż */ turtle();
15+
16+
// CHECK: [[A]] = !DILocation(line: 10, column: 9,
17+
// CHECK: [[B]] = !DILocation(line: 14, column: 10,
1118
}
1219
}
1320

1421
extern {
1522
fn giraffe();
23+
fn turtle();
1624
}

0 commit comments

Comments
 (0)