|
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; |
3 | 3 |
|
4 | 4 | use crate::common::CodegenCx;
|
5 | 5 | use crate::llvm::debuginfo::DIScope;
|
6 | 6 | use crate::llvm::{self, Value};
|
7 | 7 | use rustc_codegen_ssa::traits::*;
|
8 | 8 |
|
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 | +} |
11 | 21 |
|
12 | 22 | 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 | + }; |
15 | 37 |
|
16 |
| - // For MSVC, set the column number to zero. |
| 38 | + // For MSVC, omit the column number. |
17 | 39 | // Otherwise, emit it. This mimics clang behaviour.
|
18 | 40 | // 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 } |
21 | 43 | } else {
|
22 |
| - loc.col.to_usize() as c_uint |
23 |
| - }; |
| 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()); |
24 | 50 |
|
25 | 51 | unsafe {
|
26 | 52 | llvm::LLVMRustDIBuilderCreateDebugLocation(
|
27 | 53 | debug_context(self).llcontext,
|
28 |
| - loc.line as c_uint, |
29 |
| - col_used, |
| 54 | + line.unwrap_or(UNKNOWN_LINE_NUMBER), |
| 55 | + col.unwrap_or(UNKNOWN_COLUMN_NUMBER), |
30 | 56 | scope,
|
31 | 57 | None,
|
32 | 58 | )
|
|
0 commit comments