]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/debuginfo/source_loc.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / librustc_codegen_llvm / debuginfo / source_loc.rs
1 use super::metadata::{UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER};
2 use super::utils::debug_context;
3
4 use crate::common::CodegenCx;
5 use crate::llvm::debuginfo::DIScope;
6 use crate::llvm::{self, Value};
7 use rustc_codegen_ssa::traits::*;
8
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 }
21
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);
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         };
37
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 }
43         } else {
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());
50
51         unsafe {
52             llvm::LLVMRustDIBuilderCreateDebugLocation(
53                 debug_context(self).llcontext,
54                 line.unwrap_or(UNKNOWN_LINE_NUMBER),
55                 col.unwrap_or(UNKNOWN_COLUMN_NUMBER),
56                 scope,
57                 None,
58             )
59         }
60     }
61 }