1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use crate::generated_code;
use rustc_data_structures::sync::Lrc;
use rustc_lexer::{tokenize, TokenKind};
use rustc_session::Session;
use rustc_span::*;
#[derive(Clone)]
pub struct SpanUtils<'a> {
pub sess: &'a Session,
}
impl<'a> SpanUtils<'a> {
pub fn new(sess: &'a Session) -> SpanUtils<'a> {
SpanUtils { sess }
}
pub fn make_filename_string(&self, file: &SourceFile) -> String {
match &file.name {
FileName::Real(RealFileName::LocalPath(path)) => {
if path.is_absolute() {
self.sess
.source_map()
.path_mapping()
.map_prefix(path.into())
.0
.display()
.to_string()
} else {
self.sess
.opts
.working_dir
.remapped_path_if_available()
.join(&path)
.display()
.to_string()
}
}
filename => filename.prefer_remapped().to_string(),
}
}
pub fn snippet(&self, span: Span) -> String {
match self.sess.source_map().span_to_snippet(span) {
Ok(s) => s,
Err(_) => String::new(),
}
}
pub fn sub_span_of_star(&self, mut span: Span) -> Option<Span> {
let begin = self.sess.source_map().lookup_byte_offset(span.lo());
let end = self.sess.source_map().lookup_byte_offset(span.hi());
if begin.sf.start_pos != end.sf.start_pos {
span = span.shrink_to_lo();
}
let sf = Lrc::clone(&begin.sf);
self.sess.source_map().ensure_source_file_source_present(Lrc::clone(&sf));
let src =
sf.src.clone().or_else(|| sf.external_src.borrow().get_source().map(Lrc::clone))?;
let to_index = |pos: BytePos| -> usize { (pos - sf.start_pos).0 as usize };
let text = &src[to_index(span.lo())..to_index(span.hi())];
let start_pos = {
let mut pos = 0;
tokenize(text)
.map(|token| {
let start = pos;
pos += token.len;
(start, token)
})
.find(|(_pos, token)| token.kind == TokenKind::Star)?
.0
};
let lo = span.lo() + BytePos(start_pos as u32);
let hi = lo + BytePos(1);
Some(span.with_lo(lo).with_hi(hi))
}
pub fn filter_generated(&self, span: Span) -> bool {
if generated_code(span) {
return true;
}
!self.sess.source_map().lookup_char_pos(span.lo()).file.is_real_file()
}
}
macro_rules! filter {
($util: expr, $parent: expr) => {
if $util.filter_generated($parent) {
return None;
}
};
}