I’m studying the code of rupta, a pointer analysis framework for rust. I tried to add some codes to rupta, in order to learn where certain function is defined, given the DefId
of that function, as follows:
let cur_tcx: rustc_middle::ty::context::tyCtxt = /* snipped */;
let def_id_of_func: rustc_span::def_id::DefId = /* snipped */;
let cur_session: rustc_session::session::Session = /* snipped */;
let source_map = cur_session.source_map();
let span = cur_tcx.def_span(def_id_of_func);
let file = source_map.lookup_source_file(span.lo());
With the variable file
, I wrote these codes in addition, trying to figure out the exact path that the file locates in the file system:
let filename = file.name.clone();
let filename_cloned = filename.clone();
let path_of_file = match filename {
FileName::Real(real_file_name) => match real_file_name {
RealFileName::LocalPath(path_buf) => Ok(path_buf),
RealFileName::Remapped {
local_path: path_buf_optional,
virtual_name: virtual_path_buf,
} => {
if let Some(path_buf) = path_buf_optional {
Ok(path_buf)
} else {
Err(format!("Virtual: {}", virtual_path_buf.to_string_lossy()))
}
}
},
_ => Err(format!("Other: {:?}", filename_cloned)),
};
It works just fine in most cases, however something weird happened. For example, when analyzing config.rs
from the rustc_session
crate, the codes above indicate that the file is located in /home/<my-user-name>/.rustup/toolchains/nightly-2024-02-03-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/rustc_session/src/config.rs
. However the actual path is /home/<my-user-name>/.rustup/toolchains/nightly-2024-02-03-x86_64-unknown-linux-gnu/lib/rustlib/rustc-src/rust/compiler/rustc_session/config.rs
instead.
This phenomenon also happens when rupta analyzes rustc_driver_impl
, rustc_errors
and rustc_error_messages
, which are all from rustc-dev
.
In short, I want to figure out two questions:
- Why does
rustc_span::source_map::SourceMap::lookup_source_file
gives non-existent file paths? - Did I do wrong when fetching the file path that certain function is defined in? If so, is there any other way to do the same task?
Sadly I have little experience developing inside rustc
. Being not familiar with the rustc_*
libraries, I really don’t have any idea what I should try next.
All I am expecting is that I could know the real file path of the functions in the crates mentioned above. I am alse curious about the reason that rustc_span::source_map::SourceMap::lookup_source_file
gives false file paths.
Endericedragon F is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.