]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_span/src/caching_source_map_view.rs
Auto merge of #79073 - davidtwco:issue-78957-const-param-attrs, r=lcnr
[rust.git] / compiler / rustc_span / src / caching_source_map_view.rs
1 use crate::source_map::SourceMap;
2 use crate::{BytePos, SourceFile};
3 use rustc_data_structures::sync::Lrc;
4 use std::ops::Range;
5
6 #[derive(Clone)]
7 struct CacheEntry {
8     time_stamp: usize,
9     line_number: usize,
10     // The line's byte position range in the `SourceMap`. This range will fail to contain a valid
11     // position in certain edge cases. Spans often start/end one past something, and when that
12     // something is the last character of a file (this can happen when a file doesn't end in a
13     // newline, for example), we'd still like for the position to be considered within the last
14     // line. However, it isn't according to the exclusive upper bound of this range. We cannot
15     // change the upper bound to be inclusive, because for most lines, the upper bound is the same
16     // as the lower bound of the next line, so there would be an ambiguity.
17     //
18     // Since the containment aspect of this range is only used to see whether or not the cache
19     // entry contains a position, the only ramification of the above is that we will get cache
20     // misses for these rare positions. A line lookup for the position via `SourceMap::lookup_line`
21     // after a cache miss will produce the last line number, as desired.
22     line: Range<BytePos>,
23     file: Lrc<SourceFile>,
24     file_index: usize,
25 }
26
27 #[derive(Clone)]
28 pub struct CachingSourceMapView<'sm> {
29     source_map: &'sm SourceMap,
30     line_cache: [CacheEntry; 3],
31     time_stamp: usize,
32 }
33
34 impl<'sm> CachingSourceMapView<'sm> {
35     pub fn new(source_map: &'sm SourceMap) -> CachingSourceMapView<'sm> {
36         let files = source_map.files();
37         let first_file = files[0].clone();
38         let entry = CacheEntry {
39             time_stamp: 0,
40             line_number: 0,
41             line: BytePos(0)..BytePos(0),
42             file: first_file,
43             file_index: 0,
44         };
45
46         CachingSourceMapView {
47             source_map,
48             line_cache: [entry.clone(), entry.clone(), entry],
49             time_stamp: 0,
50         }
51     }
52
53     pub fn byte_pos_to_line_and_col(
54         &mut self,
55         pos: BytePos,
56     ) -> Option<(Lrc<SourceFile>, usize, BytePos)> {
57         self.time_stamp += 1;
58
59         // Check if the position is in one of the cached lines
60         for cache_entry in self.line_cache.iter_mut() {
61             if cache_entry.line.contains(&pos) {
62                 cache_entry.time_stamp = self.time_stamp;
63
64                 return Some((
65                     cache_entry.file.clone(),
66                     cache_entry.line_number,
67                     pos - cache_entry.line.start,
68                 ));
69             }
70         }
71
72         // No cache hit ...
73         let mut oldest = 0;
74         for index in 1..self.line_cache.len() {
75             if self.line_cache[index].time_stamp < self.line_cache[oldest].time_stamp {
76                 oldest = index;
77             }
78         }
79
80         let cache_entry = &mut self.line_cache[oldest];
81
82         // If the entry doesn't point to the correct file, fix it up
83         if !file_contains(&cache_entry.file, pos) {
84             let file_valid;
85             if self.source_map.files().len() > 0 {
86                 let file_index = self.source_map.lookup_source_file_idx(pos);
87                 let file = self.source_map.files()[file_index].clone();
88
89                 if file_contains(&file, pos) {
90                     cache_entry.file = file;
91                     cache_entry.file_index = file_index;
92                     file_valid = true;
93                 } else {
94                     file_valid = false;
95                 }
96             } else {
97                 file_valid = false;
98             }
99
100             if !file_valid {
101                 return None;
102             }
103         }
104
105         let line_index = cache_entry.file.lookup_line(pos).unwrap();
106         let line_bounds = cache_entry.file.line_bounds(line_index);
107
108         cache_entry.line_number = line_index + 1;
109         cache_entry.line = line_bounds;
110         cache_entry.time_stamp = self.time_stamp;
111
112         Some((cache_entry.file.clone(), cache_entry.line_number, pos - cache_entry.line.start))
113     }
114 }
115
116 #[inline]
117 fn file_contains(file: &SourceFile, pos: BytePos) -> bool {
118     // `SourceMap::lookup_source_file_idx` and `SourceFile::contains` both consider the position
119     // one past the end of a file to belong to it. Normally, that's what we want. But for the
120     // purposes of converting a byte position to a line and column number, we can't come up with a
121     // line and column number if the file is empty, because an empty file doesn't contain any
122     // lines. So for our purposes, we don't consider empty files to contain any byte position.
123     file.contains(pos) && !file.is_empty()
124 }