Skip to content

rustc_span: add span_data_to_lines_and_cols to caching source map view #79012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rustc_span: refactor byte_pos_to_line_and_col
  • Loading branch information
tgnottingham committed Dec 4, 2020
commit 0987b841987fb7ffc09848c185d671ff3bea7d35
59 changes: 31 additions & 28 deletions compiler/rustc_span/src/caching_source_map_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,38 +70,16 @@ impl<'sm> CachingSourceMapView<'sm> {
}

// No cache hit ...
let mut oldest = 0;
for index in 1..self.line_cache.len() {
if self.line_cache[index].time_stamp < self.line_cache[oldest].time_stamp {
oldest = index;
}
}

let cache_entry = &mut self.line_cache[oldest];
let oldest = self.oldest_cache_entry_index();

// If the entry doesn't point to the correct file, fix it up
if !file_contains(&cache_entry.file, pos) {
let file_valid;
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];

if file_contains(&file, pos) {
cache_entry.file = file.clone();
cache_entry.file_index = file_index;
file_valid = true;
} else {
file_valid = false;
}
} else {
file_valid = false;
}

if !file_valid {
return None;
}
if !file_contains(&self.line_cache[oldest].file, pos) {
let (file, file_index) = self.file_for_position(pos)?;
self.line_cache[oldest].file = file;
self.line_cache[oldest].file_index = file_index;
}

let cache_entry = &mut self.line_cache[oldest];
let line_index = cache_entry.file.lookup_line(pos).unwrap();
let line_bounds = cache_entry.file.line_bounds(line_index);

Expand All @@ -111,6 +89,31 @@ impl<'sm> CachingSourceMapView<'sm> {

Some((cache_entry.file.clone(), cache_entry.line_number, pos - cache_entry.line.start))
}

fn oldest_cache_entry_index(&self) -> usize {
let mut oldest = 0;

for idx in 1..self.line_cache.len() {
if self.line_cache[idx].time_stamp < self.line_cache[oldest].time_stamp {
oldest = idx;
}
}

oldest
}

fn file_for_position(&self, pos: BytePos) -> Option<(Lrc<SourceFile>, usize)> {
if !self.source_map.files().is_empty() {
let file_idx = self.source_map.lookup_source_file_idx(pos);
let file = &self.source_map.files()[file_idx];

if file_contains(file, pos) {
return Some((file.clone(), file_idx));
}
}

None
}
}

#[inline]
Expand Down