2019-11-23 13:17:37 +00:00
|
|
|
use crate::source_map::SourceMap;
|
|
|
|
use crate::{BytePos, SourceFile};
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2016-08-31 20:51:24 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct CacheEntry {
|
|
|
|
time_stamp: usize,
|
|
|
|
line_number: usize,
|
|
|
|
line_start: BytePos,
|
|
|
|
line_end: BytePos,
|
2018-08-18 10:13:52 +00:00
|
|
|
file: Lrc<SourceFile>,
|
2017-04-27 14:12:57 +00:00
|
|
|
file_index: usize,
|
2016-08-31 20:51:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 15:43:03 +00:00
|
|
|
#[derive(Clone)]
|
2020-02-22 14:07:05 +00:00
|
|
|
pub struct CachingSourceMapView<'sm> {
|
|
|
|
source_map: &'sm SourceMap,
|
2016-08-31 20:51:24 +00:00
|
|
|
line_cache: [CacheEntry; 3],
|
|
|
|
time_stamp: usize,
|
|
|
|
}
|
|
|
|
|
2020-02-22 14:07:05 +00:00
|
|
|
impl<'sm> CachingSourceMapView<'sm> {
|
|
|
|
pub fn new(source_map: &'sm SourceMap) -> CachingSourceMapView<'sm> {
|
2018-08-18 10:14:14 +00:00
|
|
|
let files = source_map.files();
|
2017-04-27 14:12:57 +00:00
|
|
|
let first_file = files[0].clone();
|
2016-08-31 20:51:24 +00:00
|
|
|
let entry = CacheEntry {
|
|
|
|
time_stamp: 0,
|
|
|
|
line_number: 0,
|
|
|
|
line_start: BytePos(0),
|
|
|
|
line_end: BytePos(0),
|
|
|
|
file: first_file,
|
2017-04-27 14:12:57 +00:00
|
|
|
file_index: 0,
|
2016-08-31 20:51:24 +00:00
|
|
|
};
|
|
|
|
|
2018-08-18 10:14:25 +00:00
|
|
|
CachingSourceMapView {
|
2018-08-18 10:14:14 +00:00
|
|
|
source_map,
|
2018-10-23 08:13:59 +00:00
|
|
|
line_cache: [entry.clone(), entry.clone(), entry],
|
2016-08-31 20:51:24 +00:00
|
|
|
time_stamp: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn byte_pos_to_line_and_col(
|
|
|
|
&mut self,
|
|
|
|
pos: BytePos,
|
|
|
|
) -> Option<(Lrc<SourceFile>, usize, BytePos)> {
|
2016-08-31 20:51:24 +00:00
|
|
|
self.time_stamp += 1;
|
|
|
|
|
|
|
|
// Check if the position is in one of the cached lines
|
|
|
|
for cache_entry in self.line_cache.iter_mut() {
|
2020-09-21 00:40:40 +00:00
|
|
|
if line_contains((cache_entry.line_start, cache_entry.line_end), pos) {
|
2016-08-31 20:51:24 +00:00
|
|
|
cache_entry.time_stamp = self.time_stamp;
|
2017-04-27 14:12:57 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
return Some((
|
|
|
|
cache_entry.file.clone(),
|
|
|
|
cache_entry.line_number,
|
|
|
|
pos - cache_entry.line_start,
|
|
|
|
));
|
2016-08-31 20:51:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No cache hit ...
|
|
|
|
let mut oldest = 0;
|
2019-12-22 22:42:04 +00:00
|
|
|
for index in 1..self.line_cache.len() {
|
2016-08-31 20:51:24 +00:00
|
|
|
if self.line_cache[index].time_stamp < self.line_cache[oldest].time_stamp {
|
|
|
|
oldest = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let cache_entry = &mut self.line_cache[oldest];
|
|
|
|
|
|
|
|
// If the entry doesn't point to the correct file, fix it up
|
2020-09-21 00:40:40 +00:00
|
|
|
if !file_contains(&cache_entry.file, pos) {
|
2016-09-06 16:14:43 +00:00
|
|
|
let file_valid;
|
2018-08-18 10:14:14 +00:00
|
|
|
if self.source_map.files().len() > 0 {
|
|
|
|
let file_index = self.source_map.lookup_source_file_idx(pos);
|
|
|
|
let file = self.source_map.files()[file_index].clone();
|
2016-09-06 16:14:43 +00:00
|
|
|
|
2020-09-21 00:40:40 +00:00
|
|
|
if file_contains(&file, pos) {
|
2016-09-06 16:14:43 +00:00
|
|
|
cache_entry.file = file;
|
2017-04-27 14:12:57 +00:00
|
|
|
cache_entry.file_index = file_index;
|
2016-09-06 16:14:43 +00:00
|
|
|
file_valid = true;
|
|
|
|
} else {
|
|
|
|
file_valid = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file_valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !file_valid {
|
|
|
|
return None;
|
|
|
|
}
|
2016-08-31 20:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let line_index = cache_entry.file.lookup_line(pos).unwrap();
|
|
|
|
let line_bounds = cache_entry.file.line_bounds(line_index);
|
|
|
|
|
|
|
|
cache_entry.line_number = line_index + 1;
|
|
|
|
cache_entry.line_start = line_bounds.0;
|
|
|
|
cache_entry.line_end = line_bounds.1;
|
|
|
|
cache_entry.time_stamp = self.time_stamp;
|
|
|
|
|
2020-03-20 14:03:11 +00:00
|
|
|
Some((cache_entry.file.clone(), cache_entry.line_number, pos - cache_entry.line_start))
|
2016-08-31 20:51:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 00:40:40 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn line_contains(line_bounds: (BytePos, BytePos), pos: BytePos) -> bool {
|
|
|
|
// This condition will be false in one case where we'd rather it wasn't. Spans often start/end
|
|
|
|
// one past something, and when that something is the last character of a file (this can happen
|
|
|
|
// when a file doesn't end in a newline, for example), we'd still like for the position to be
|
|
|
|
// considered within the last line. However, it isn't according to the exclusive upper bound
|
|
|
|
// below. We cannot change the upper bound to be inclusive, because for most lines, the upper
|
|
|
|
// bound is the same as the lower bound of the next line, so there would be an ambiguity.
|
|
|
|
//
|
|
|
|
// Supposing we only use this function to check whether or not the line cache entry contains
|
|
|
|
// a position, the only ramification of the above is that we will get cache misses for these
|
|
|
|
// rare positions. A line lookup for the position via `SourceMap::lookup_line` after a cache
|
|
|
|
// miss will produce the last line number, as desired.
|
|
|
|
line_bounds.0 <= pos && pos < line_bounds.1
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn file_contains(file: &SourceFile, pos: BytePos) -> bool {
|
|
|
|
// `SourceMap::lookup_source_file_idx` and `SourceFile::contains` both consider the position
|
|
|
|
// one past the end of a file to belong to it. Normally, that's what we want. But for the
|
|
|
|
// purposes of converting a byte position to a line and column number, we can't come up with a
|
|
|
|
// line and column number if the file is empty, because an empty file doesn't contain any
|
|
|
|
// lines. So for our purposes, we don't consider empty files to contain any byte position.
|
|
|
|
file.contains(pos) && !file.is_empty()
|
|
|
|
}
|