Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {

fn make_file_info(source_file: &SourceFile, embed_source: bool) -> Option<FileInfo> {
let has_md5 = source_file.src_hash.kind == SourceFileHashAlgorithm::Md5;
let has_source = embed_source && source_file.src.is_some();
let has_source = embed_source
&& (source_file.src.is_some() || source_file.external_src.read().get_source().is_some());

if !has_md5 && !has_source {
return None;
Expand All @@ -62,6 +63,8 @@ fn make_file_info(source_file: &SourceFile, embed_source: bool) -> Option<FileIn
if embed_source {
if let Some(src) = &source_file.src {
info.source = Some(LineString::String(src.as_bytes().to_vec()));
} else if let Some(src) = source_file.external_src.read().get_source() {
info.source = Some(LineString::String(src.as_bytes().to_vec()));
}
}

Expand All @@ -79,19 +82,22 @@ impl DebugContext {
let span = hygiene::walk_chain_collapsed(span, function_span);
match tcx.sess.source_map().lookup_line(span.lo()) {
Ok(SourceFileAndLine { sf: file, line }) => {
let file_id = self.add_source_file(&file);
let file_id = self.add_source_file(tcx, &file);
let line_pos = file.lines()[line];
let col = file.relative_position(span.lo()) - line_pos;

(file_id, u64::try_from(line).unwrap() + 1, u64::from(col.to_u32()) + 1)
}
Err(file) => (self.add_source_file(&file), 0, 0),
Err(file) => (self.add_source_file(tcx, &file), 0, 0),
}
}

pub(crate) fn add_source_file(&mut self, source_file: &SourceFile) -> FileId {
pub(crate) fn add_source_file(&mut self, tcx: TyCtxt<'_>, source_file: &SourceFile) -> FileId {
let cache_key = (source_file.stable_id, source_file.src_hash);
*self.created_files.entry(cache_key).or_insert_with(|| {
if self.embed_source && source_file.src.is_none() {
tcx.sess.source_map().ensure_source_file_source_present(source_file);
}
let line_program: &mut LineProgram = &mut self.dwarf.unit.line_program;
let line_strings: &mut LineStringTable = &mut self.dwarf.line_strings;

Expand Down
15 changes: 11 additions & 4 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::borrow::Cow;
use std::fmt::{self, Write};
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use std::sync::Arc;
use std::{assert_matches, iter, ptr};

use libc::{c_longlong, c_uint};
Expand Down Expand Up @@ -607,8 +606,16 @@ pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFi
};
let hash_value = hex_encode(source_file.src_hash.hash_bytes());

let source =
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
let mut source = None;
let external_src;
if cx.sess().opts.unstable_opts.embed_source {
source = source_file.src.as_deref().map(String::as_str);
if source.is_none() {
cx.tcx.sess.source_map().ensure_source_file_source_present(source_file);
external_src = source_file.external_src.read();
source = external_src.get_source();
}
}

create_file(DIB(cx), &file_name, &directory, &hash_value, hash_kind, source)
}
Expand All @@ -626,7 +633,7 @@ fn create_file<'ll>(
directory: &str,
hash_value: &str,
hash_kind: llvm::ChecksumKind,
source: Option<&Arc<String>>,
source: Option<&str>,
) -> &'ll DIFile {
unsafe {
llvm::LLVMRustDIBuilderCreateFile(
Expand Down
Loading