1 use super::metadata::{UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER};
2 use super::utils::debug_context;
4 use crate::common::CodegenCx;
5 use crate::llvm::debuginfo::DIScope;
6 use crate::llvm::{self, Value};
7 use rustc_codegen_ssa::traits::*;
9 use rustc_data_structures::sync::Lrc;
10 use rustc_span::{BytePos, Pos, SourceFile, SourceFileAndLine, Span};
12 /// A source code location used to generate debug information.
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.
22 impl CodegenCx<'ll, '_> {
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);
29 // Use 1-based indexing.
30 let line = (line + 1) as u32;
31 let col = (pos - line_pos).to_u32() + 1;
33 (file, Some(line), Some(col))
35 Err(file) => (file, None, None),
38 // For MSVC, omit the column number.
39 // Otherwise, emit it. This mimics clang behaviour.
40 // See discussion in https://github.com/rust-lang/rust/issues/42921
41 if self.sess().target.target.options.is_like_msvc {
42 DebugLoc { file, line, col: None }
44 DebugLoc { file, line, col }
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());
52 llvm::LLVMRustDIBuilderCreateDebugLocation(
53 debug_context(self).llcontext,
54 line.unwrap_or(UNKNOWN_LINE_NUMBER),
55 col.unwrap_or(UNKNOWN_COLUMN_NUMBER),