]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_save_analysis/src/span_utils.rs
Revamp RealFileName public methods
[rust.git] / compiler / rustc_save_analysis / src / span_utils.rs
1 use crate::generated_code;
2 use rustc_data_structures::sync::Lrc;
3 use rustc_lexer::{tokenize, TokenKind};
4 use rustc_session::Session;
5 use rustc_span::*;
6
7 #[derive(Clone)]
8 pub struct SpanUtils<'a> {
9     pub sess: &'a Session,
10 }
11
12 impl<'a> SpanUtils<'a> {
13     pub fn new(sess: &'a Session) -> SpanUtils<'a> {
14         SpanUtils { sess }
15     }
16
17     pub fn make_filename_string(&self, file: &SourceFile) -> String {
18         match &file.name {
19             FileName::Real(RealFileName::LocalPath(path)) => {
20                 if path.is_absolute() {
21                     self.sess
22                         .source_map()
23                         .path_mapping()
24                         .map_prefix(path.into())
25                         .0
26                         .display()
27                         .to_string()
28                 } else {
29                     self.sess
30                         .working_dir
31                         .remapped_path_if_available()
32                         .join(&path)
33                         .display()
34                         .to_string()
35                 }
36             }
37             // If the file name was remapped, we assume the user
38             // configured it the way they wanted to, so use that directly
39             FileName::Real(RealFileName::Remapped { local_path: _, virtual_name }) => {
40                 virtual_name.display().to_string()
41             }
42             filename => filename.to_string(),
43         }
44     }
45
46     pub fn snippet(&self, span: Span) -> String {
47         match self.sess.source_map().span_to_snippet(span) {
48             Ok(s) => s,
49             Err(_) => String::new(),
50         }
51     }
52
53     /// Finds the span of `*` token withing the larger `span`.
54     pub fn sub_span_of_star(&self, mut span: Span) -> Option<Span> {
55         let begin = self.sess.source_map().lookup_byte_offset(span.lo());
56         let end = self.sess.source_map().lookup_byte_offset(span.hi());
57         // Make the range zero-length if the span is invalid.
58         if begin.sf.start_pos != end.sf.start_pos {
59             span = span.shrink_to_lo();
60         }
61
62         let sf = Lrc::clone(&begin.sf);
63
64         self.sess.source_map().ensure_source_file_source_present(Lrc::clone(&sf));
65         let src =
66             sf.src.clone().or_else(|| sf.external_src.borrow().get_source().map(Lrc::clone))?;
67         let to_index = |pos: BytePos| -> usize { (pos - sf.start_pos).0 as usize };
68         let text = &src[to_index(span.lo())..to_index(span.hi())];
69         let start_pos = {
70             let mut pos = 0;
71             tokenize(text)
72                 .map(|token| {
73                     let start = pos;
74                     pos += token.len;
75                     (start, token)
76                 })
77                 .find(|(_pos, token)| token.kind == TokenKind::Star)?
78                 .0
79         };
80         let lo = span.lo() + BytePos(start_pos as u32);
81         let hi = lo + BytePos(1);
82         Some(span.with_lo(lo).with_hi(hi))
83     }
84
85     /// Return true if the span is generated code, and
86     /// it is not a subspan of the root callsite.
87     ///
88     /// Used to filter out spans of minimal value,
89     /// such as references to macro internal variables.
90     pub fn filter_generated(&self, span: Span) -> bool {
91         if generated_code(span) {
92             return true;
93         }
94
95         //If the span comes from a fake source_file, filter it.
96         !self.sess.source_map().lookup_char_pos(span.lo()).file.is_real_file()
97     }
98 }
99
100 macro_rules! filter {
101     ($util: expr, $parent: expr) => {
102         if $util.filter_generated($parent) {
103             return None;
104         }
105     };
106 }