]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_save_analysis/src/span_utils.rs
Rollup merge of #80805 - camelid:iter-by_ref-example, r=steveklabnik
[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(name) if !file.name_was_remapped => {
20                 let path = name.local_path();
21                 if path.is_absolute() {
22                     self.sess
23                         .source_map()
24                         .path_mapping()
25                         .map_prefix(path.into())
26                         .0
27                         .display()
28                         .to_string()
29                 } else {
30                     self.sess.working_dir.0.join(&path).display().to_string()
31                 }
32             }
33             // If the file name is already remapped, we assume the user
34             // configured it the way they wanted to, so use that directly
35             filename => filename.to_string(),
36         }
37     }
38
39     pub fn snippet(&self, span: Span) -> String {
40         match self.sess.source_map().span_to_snippet(span) {
41             Ok(s) => s,
42             Err(_) => String::new(),
43         }
44     }
45
46     /// Finds the span of `*` token withing the larger `span`.
47     pub fn sub_span_of_star(&self, mut span: Span) -> Option<Span> {
48         let begin = self.sess.source_map().lookup_byte_offset(span.lo());
49         let end = self.sess.source_map().lookup_byte_offset(span.hi());
50         // Make the range zero-length if the span is invalid.
51         if begin.sf.start_pos != end.sf.start_pos {
52             span = span.shrink_to_lo();
53         }
54
55         let sf = Lrc::clone(&begin.sf);
56
57         self.sess.source_map().ensure_source_file_source_present(Lrc::clone(&sf));
58         let src =
59             sf.src.clone().or_else(|| sf.external_src.borrow().get_source().map(Lrc::clone))?;
60         let to_index = |pos: BytePos| -> usize { (pos - sf.start_pos).0 as usize };
61         let text = &src[to_index(span.lo())..to_index(span.hi())];
62         let start_pos = {
63             let mut pos = 0;
64             tokenize(text)
65                 .map(|token| {
66                     let start = pos;
67                     pos += token.len;
68                     (start, token)
69                 })
70                 .find(|(_pos, token)| token.kind == TokenKind::Star)?
71                 .0
72         };
73         let lo = span.lo() + BytePos(start_pos as u32);
74         let hi = lo + BytePos(1);
75         Some(span.with_lo(lo).with_hi(hi))
76     }
77
78     /// Return true if the span is generated code, and
79     /// it is not a subspan of the root callsite.
80     ///
81     /// Used to filter out spans of minimal value,
82     /// such as references to macro internal variables.
83     pub fn filter_generated(&self, span: Span) -> bool {
84         if generated_code(span) {
85             return true;
86         }
87
88         //If the span comes from a fake source_file, filter it.
89         !self.sess.source_map().lookup_char_pos(span.lo()).file.is_real_file()
90     }
91 }
92
93 macro_rules! filter {
94     ($util: expr, $parent: expr) => {
95         if $util.filter_generated($parent) {
96             return None;
97         }
98     };
99 }