]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_span/src/source_map.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_span / src / source_map.rs
1 //! The `SourceMap` tracks all the source code used within a single crate, mapping
2 //! from integer byte positions to the original source code location. Each bit
3 //! of source parsed during crate parsing (typically files, in-memory strings,
4 //! or various bits of macro expansion) cover a continuous range of bytes in the
5 //! `SourceMap` and are represented by `SourceFile`s. Byte positions are stored in
6 //! `Span` and used pervasively in the compiler. They are absolute positions
7 //! within the `SourceMap`, which upon request can be converted to line and column
8 //! information, source code snippets, etc.
9
10 pub use crate::hygiene::{ExpnData, ExpnKind};
11 pub use crate::*;
12
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_data_structures::stable_hasher::StableHasher;
15 use rustc_data_structures::sync::{AtomicU32, Lock, LockGuard, Lrc, MappedLockGuard};
16 use std::cmp;
17 use std::convert::TryFrom;
18 use std::hash::Hash;
19 use std::path::{Path, PathBuf};
20 use std::sync::atomic::Ordering;
21
22 use std::fs;
23 use std::io;
24 use tracing::debug;
25
26 #[cfg(test)]
27 mod tests;
28
29 /// Returns the span itself if it doesn't come from a macro expansion,
30 /// otherwise return the call site span up to the `enclosing_sp` by
31 /// following the `expn_data` chain.
32 pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span {
33 let expn_data1 = sp.ctxt().outer_expn_data();
34 let expn_data2 = enclosing_sp.ctxt().outer_expn_data();
35 if expn_data1.is_root() || !expn_data2.is_root() && expn_data1.call_site == expn_data2.call_site
36 {
37 sp
38 } else {
39 original_sp(expn_data1.call_site, enclosing_sp)
40 }
41 }
42
43 pub mod monotonic {
44 use std::ops::{Deref, DerefMut};
45
46 /// A `MonotonicVec` is a `Vec` which can only be grown.
47 /// Once inserted, an element can never be removed or swapped,
48 /// guaranteeing that any indices into a `MonotonicVec` are stable
49 // This is declared in its own module to ensure that the private
50 // field is inaccessible
51 pub struct MonotonicVec<T>(Vec<T>);
52 impl<T> MonotonicVec<T> {
53 pub fn new(val: Vec<T>) -> MonotonicVec<T> {
54 MonotonicVec(val)
55 }
56
57 pub fn push(&mut self, val: T) {
58 self.0.push(val);
59 }
60 }
61
62 impl<T> Default for MonotonicVec<T> {
63 fn default() -> Self {
64 MonotonicVec::new(vec![])
65 }
66 }
67
68 impl<T> Deref for MonotonicVec<T> {
69 type Target = Vec<T>;
70 fn deref(&self) -> &Self::Target {
71 &self.0
72 }
73 }
74
75 impl<T> !DerefMut for MonotonicVec<T> {}
76 }
77
78 #[derive(Clone, Encodable, Decodable, Debug, Copy, HashStable_Generic)]
79 pub struct Spanned<T> {
80 pub node: T,
81 pub span: Span,
82 }
83
84 pub fn respan<T>(sp: Span, t: T) -> Spanned<T> {
85 Spanned { node: t, span: sp }
86 }
87
88 pub fn dummy_spanned<T>(t: T) -> Spanned<T> {
89 respan(DUMMY_SP, t)
90 }
91
92 // _____________________________________________________________________________
93 // SourceFile, MultiByteChar, FileName, FileLines
94 //
95
96 /// An abstraction over the fs operations used by the Parser.
97 pub trait FileLoader {
98 /// Query the existence of a file.
99 fn file_exists(&self, path: &Path) -> bool;
100
101 /// Read the contents of an UTF-8 file into memory.
102 fn read_file(&self, path: &Path) -> io::Result<String>;
103 }
104
105 /// A FileLoader that uses std::fs to load real files.
106 pub struct RealFileLoader;
107
108 impl FileLoader for RealFileLoader {
109 fn file_exists(&self, path: &Path) -> bool {
110 fs::metadata(path).is_ok()
111 }
112
113 fn read_file(&self, path: &Path) -> io::Result<String> {
114 fs::read_to_string(path)
115 }
116 }
117
118 // This is a `SourceFile` identifier that is used to correlate `SourceFile`s between
119 // subsequent compilation sessions (which is something we need to do during
120 // incremental compilation).
121 #[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
122 pub struct StableSourceFileId(u128);
123
124 // FIXME: we need a more globally consistent approach to the problem solved by
125 // StableSourceFileId, perhaps built atop source_file.name_hash.
126 impl StableSourceFileId {
127 pub fn new(source_file: &SourceFile) -> StableSourceFileId {
128 StableSourceFileId::new_from_pieces(
129 &source_file.name,
130 source_file.name_was_remapped,
131 source_file.unmapped_path.as_ref(),
132 )
133 }
134
135 fn new_from_pieces(
136 name: &FileName,
137 name_was_remapped: bool,
138 unmapped_path: Option<&FileName>,
139 ) -> StableSourceFileId {
140 let mut hasher = StableHasher::new();
141
142 if let FileName::Real(real_name) = name {
143 // rust-lang/rust#70924: Use the stable (virtualized) name when
144 // available. (We do not want artifacts from transient file system
145 // paths for libstd to leak into our build artifacts.)
146 real_name.stable_name().hash(&mut hasher)
147 } else {
148 name.hash(&mut hasher);
149 }
150 name_was_remapped.hash(&mut hasher);
151 unmapped_path.hash(&mut hasher);
152
153 StableSourceFileId(hasher.finish())
154 }
155 }
156
157 // _____________________________________________________________________________
158 // SourceMap
159 //
160
161 #[derive(Default)]
162 pub(super) struct SourceMapFiles {
163 source_files: monotonic::MonotonicVec<Lrc<SourceFile>>,
164 stable_id_to_source_file: FxHashMap<StableSourceFileId, Lrc<SourceFile>>,
165 }
166
167 pub struct SourceMap {
168 /// The address space below this value is currently used by the files in the source map.
169 used_address_space: AtomicU32,
170
171 files: Lock<SourceMapFiles>,
172 file_loader: Box<dyn FileLoader + Sync + Send>,
173 // This is used to apply the file path remapping as specified via
174 // `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`.
175 path_mapping: FilePathMapping,
176
177 /// The algorithm used for hashing the contents of each source file.
178 hash_kind: SourceFileHashAlgorithm,
179 }
180
181 impl SourceMap {
182 pub fn new(path_mapping: FilePathMapping) -> SourceMap {
183 Self::with_file_loader_and_hash_kind(
184 Box::new(RealFileLoader),
185 path_mapping,
186 SourceFileHashAlgorithm::Md5,
187 )
188 }
189
190 pub fn with_file_loader_and_hash_kind(
191 file_loader: Box<dyn FileLoader + Sync + Send>,
192 path_mapping: FilePathMapping,
193 hash_kind: SourceFileHashAlgorithm,
194 ) -> SourceMap {
195 SourceMap {
196 used_address_space: AtomicU32::new(0),
197 files: Default::default(),
198 file_loader,
199 path_mapping,
200 hash_kind,
201 }
202 }
203
204 pub fn path_mapping(&self) -> &FilePathMapping {
205 &self.path_mapping
206 }
207
208 pub fn file_exists(&self, path: &Path) -> bool {
209 self.file_loader.file_exists(path)
210 }
211
212 pub fn load_file(&self, path: &Path) -> io::Result<Lrc<SourceFile>> {
213 let src = self.file_loader.read_file(path)?;
214 let filename = path.to_owned().into();
215 Ok(self.new_source_file(filename, src))
216 }
217
218 /// Loads source file as a binary blob.
219 ///
220 /// Unlike `load_file`, guarantees that no normalization like BOM-removal
221 /// takes place.
222 pub fn load_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
223 // Ideally, this should use `self.file_loader`, but it can't
224 // deal with binary files yet.
225 let bytes = fs::read(path)?;
226
227 // We need to add file to the `SourceMap`, so that it is present
228 // in dep-info. There's also an edge case that file might be both
229 // loaded as a binary via `include_bytes!` and as proper `SourceFile`
230 // via `mod`, so we try to use real file contents and not just an
231 // empty string.
232 let text = std::str::from_utf8(&bytes).unwrap_or("").to_string();
233 self.new_source_file(path.to_owned().into(), text);
234 Ok(bytes)
235 }
236
237 // By returning a `MonotonicVec`, we ensure that consumers cannot invalidate
238 // any existing indices pointing into `files`.
239 pub fn files(&self) -> MappedLockGuard<'_, monotonic::MonotonicVec<Lrc<SourceFile>>> {
240 LockGuard::map(self.files.borrow(), |files| &mut files.source_files)
241 }
242
243 pub fn source_file_by_stable_id(
244 &self,
245 stable_id: StableSourceFileId,
246 ) -> Option<Lrc<SourceFile>> {
247 self.files.borrow().stable_id_to_source_file.get(&stable_id).cloned()
248 }
249
250 fn allocate_address_space(&self, size: usize) -> Result<usize, OffsetOverflowError> {
251 let size = u32::try_from(size).map_err(|_| OffsetOverflowError)?;
252
253 loop {
254 let current = self.used_address_space.load(Ordering::Relaxed);
255 let next = current
256 .checked_add(size)
257 // Add one so there is some space between files. This lets us distinguish
258 // positions in the `SourceMap`, even in the presence of zero-length files.
259 .and_then(|next| next.checked_add(1))
260 .ok_or(OffsetOverflowError)?;
261
262 if self
263 .used_address_space
264 .compare_exchange(current, next, Ordering::Relaxed, Ordering::Relaxed)
265 .is_ok()
266 {
267 return Ok(usize::try_from(current).unwrap());
268 }
269 }
270 }
271
272 /// Creates a new `SourceFile`.
273 /// If a file already exists in the `SourceMap` with the same ID, that file is returned
274 /// unmodified.
275 pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
276 self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| {
277 eprintln!("fatal error: rustc does not support files larger than 4GB");
278 crate::fatal_error::FatalError.raise()
279 })
280 }
281
282 fn try_new_source_file(
283 &self,
284 mut filename: FileName,
285 src: String,
286 ) -> Result<Lrc<SourceFile>, OffsetOverflowError> {
287 // The path is used to determine the directory for loading submodules and
288 // include files, so it must be before remapping.
289 // Note that filename may not be a valid path, eg it may be `<anon>` etc,
290 // but this is okay because the directory determined by `path.pop()` will
291 // be empty, so the working directory will be used.
292 let unmapped_path = filename.clone();
293
294 let was_remapped;
295 if let FileName::Real(real_filename) = &mut filename {
296 match real_filename {
297 RealFileName::Named(path_to_be_remapped)
298 | RealFileName::Devirtualized {
299 local_path: path_to_be_remapped,
300 virtual_name: _,
301 } => {
302 let mapped = self.path_mapping.map_prefix(path_to_be_remapped.clone());
303 was_remapped = mapped.1;
304 *path_to_be_remapped = mapped.0;
305 }
306 }
307 } else {
308 was_remapped = false;
309 }
310
311 let file_id =
312 StableSourceFileId::new_from_pieces(&filename, was_remapped, Some(&unmapped_path));
313
314 let lrc_sf = match self.source_file_by_stable_id(file_id) {
315 Some(lrc_sf) => lrc_sf,
316 None => {
317 let start_pos = self.allocate_address_space(src.len())?;
318
319 let source_file = Lrc::new(SourceFile::new(
320 filename,
321 was_remapped,
322 unmapped_path,
323 src,
324 Pos::from_usize(start_pos),
325 self.hash_kind,
326 ));
327
328 let mut files = self.files.borrow_mut();
329
330 files.source_files.push(source_file.clone());
331 files.stable_id_to_source_file.insert(file_id, source_file.clone());
332
333 source_file
334 }
335 };
336 Ok(lrc_sf)
337 }
338
339 /// Allocates a new `SourceFile` representing a source file from an external
340 /// crate. The source code of such an "imported `SourceFile`" is not available,
341 /// but we still know enough to generate accurate debuginfo location
342 /// information for things inlined from other crates.
343 pub fn new_imported_source_file(
344 &self,
345 filename: FileName,
346 name_was_remapped: bool,
347 src_hash: SourceFileHash,
348 name_hash: u128,
349 source_len: usize,
350 cnum: CrateNum,
351 mut file_local_lines: Vec<BytePos>,
352 mut file_local_multibyte_chars: Vec<MultiByteChar>,
353 mut file_local_non_narrow_chars: Vec<NonNarrowChar>,
354 mut file_local_normalized_pos: Vec<NormalizedPos>,
355 original_start_pos: BytePos,
356 original_end_pos: BytePos,
357 ) -> Lrc<SourceFile> {
358 let start_pos = self
359 .allocate_address_space(source_len)
360 .expect("not enough address space for imported source file");
361
362 let end_pos = Pos::from_usize(start_pos + source_len);
363 let start_pos = Pos::from_usize(start_pos);
364
365 for pos in &mut file_local_lines {
366 *pos = *pos + start_pos;
367 }
368
369 for mbc in &mut file_local_multibyte_chars {
370 mbc.pos = mbc.pos + start_pos;
371 }
372
373 for swc in &mut file_local_non_narrow_chars {
374 *swc = *swc + start_pos;
375 }
376
377 for nc in &mut file_local_normalized_pos {
378 nc.pos = nc.pos + start_pos;
379 }
380
381 let source_file = Lrc::new(SourceFile {
382 name: filename,
383 name_was_remapped,
384 unmapped_path: None,
385 src: None,
386 src_hash,
387 external_src: Lock::new(ExternalSource::Foreign {
388 kind: ExternalSourceKind::AbsentOk,
389 original_start_pos,
390 original_end_pos,
391 }),
392 start_pos,
393 end_pos,
394 lines: file_local_lines,
395 multibyte_chars: file_local_multibyte_chars,
396 non_narrow_chars: file_local_non_narrow_chars,
397 normalized_pos: file_local_normalized_pos,
398 name_hash,
399 cnum,
400 });
401
402 let mut files = self.files.borrow_mut();
403
404 files.source_files.push(source_file.clone());
405 files
406 .stable_id_to_source_file
407 .insert(StableSourceFileId::new(&source_file), source_file.clone());
408
409 source_file
410 }
411
412 pub fn mk_substr_filename(&self, sp: Span) -> String {
413 let pos = self.lookup_char_pos(sp.lo());
414 format!("<{}:{}:{}>", pos.file.name, pos.line, pos.col.to_usize() + 1)
415 }
416
417 // If there is a doctest offset, applies it to the line.
418 pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize {
419 match file {
420 FileName::DocTest(_, offset) => {
421 if *offset < 0 {
422 orig - (-(*offset)) as usize
423 } else {
424 orig + *offset as usize
425 }
426 }
427 _ => orig,
428 }
429 }
430
431 /// Looks up source information about a `BytePos`.
432 pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
433 let chpos = self.bytepos_to_file_charpos(pos);
434 match self.lookup_line(pos) {
435 Ok(SourceFileAndLine { sf: f, line: a }) => {
436 let line = a + 1; // Line numbers start at 1
437 let linebpos = f.lines[a];
438 let linechpos = self.bytepos_to_file_charpos(linebpos);
439 let col = chpos - linechpos;
440
441 let col_display = {
442 let start_width_idx = f
443 .non_narrow_chars
444 .binary_search_by_key(&linebpos, |x| x.pos())
445 .unwrap_or_else(|x| x);
446 let end_width_idx = f
447 .non_narrow_chars
448 .binary_search_by_key(&pos, |x| x.pos())
449 .unwrap_or_else(|x| x);
450 let special_chars = end_width_idx - start_width_idx;
451 let non_narrow: usize = f.non_narrow_chars[start_width_idx..end_width_idx]
452 .iter()
453 .map(|x| x.width())
454 .sum();
455 col.0 - special_chars + non_narrow
456 };
457 debug!("byte pos {:?} is on the line at byte pos {:?}", pos, linebpos);
458 debug!("char pos {:?} is on the line at char pos {:?}", chpos, linechpos);
459 debug!("byte is on line: {}", line);
460 assert!(chpos >= linechpos);
461 Loc { file: f, line, col, col_display }
462 }
463 Err(f) => {
464 let col_display = {
465 let end_width_idx = f
466 .non_narrow_chars
467 .binary_search_by_key(&pos, |x| x.pos())
468 .unwrap_or_else(|x| x);
469 let non_narrow: usize =
470 f.non_narrow_chars[0..end_width_idx].iter().map(|x| x.width()).sum();
471 chpos.0 - end_width_idx + non_narrow
472 };
473 Loc { file: f, line: 0, col: chpos, col_display }
474 }
475 }
476 }
477
478 // If the corresponding `SourceFile` is empty, does not return a line number.
479 pub fn lookup_line(&self, pos: BytePos) -> Result<SourceFileAndLine, Lrc<SourceFile>> {
480 let idx = self.lookup_source_file_idx(pos);
481
482 let f = (*self.files.borrow().source_files)[idx].clone();
483
484 match f.lookup_line(pos) {
485 Some(line) => Ok(SourceFileAndLine { sf: f, line }),
486 None => Err(f),
487 }
488 }
489
490 /// Returns a new `Span` covering the start and end `BytePos`s of the file containing the given
491 /// `pos`. This can be used to quickly determine if another `BytePos` or `Span` is from the same
492 /// file.
493 pub fn lookup_file_span(&self, pos: BytePos) -> Span {
494 let idx = self.lookup_source_file_idx(pos);
495 let SourceFile { start_pos, end_pos, .. } = *(*self.files.borrow().source_files)[idx];
496 Span::with_root_ctxt(start_pos, end_pos)
497 }
498
499 /// Returns `Some(span)`, a union of the LHS and RHS span. The LHS must precede the RHS. If
500 /// there are gaps between LHS and RHS, the resulting union will cross these gaps.
501 /// For this to work,
502 ///
503 /// * the syntax contexts of both spans much match,
504 /// * the LHS span needs to end on the same line the RHS span begins,
505 /// * the LHS span must start at or before the RHS span.
506 pub fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span> {
507 // Ensure we're at the same expansion ID.
508 if sp_lhs.ctxt() != sp_rhs.ctxt() {
509 return None;
510 }
511
512 let lhs_end = match self.lookup_line(sp_lhs.hi()) {
513 Ok(x) => x,
514 Err(_) => return None,
515 };
516 let rhs_begin = match self.lookup_line(sp_rhs.lo()) {
517 Ok(x) => x,
518 Err(_) => return None,
519 };
520
521 // If we must cross lines to merge, don't merge.
522 if lhs_end.line != rhs_begin.line {
523 return None;
524 }
525
526 // Ensure these follow the expected order and that we don't overlap.
527 if (sp_lhs.lo() <= sp_rhs.lo()) && (sp_lhs.hi() <= sp_rhs.lo()) {
528 Some(sp_lhs.to(sp_rhs))
529 } else {
530 None
531 }
532 }
533
534 pub fn span_to_string(&self, sp: Span) -> String {
535 if self.files.borrow().source_files.is_empty() && sp.is_dummy() {
536 return "no-location".to_string();
537 }
538
539 let lo = self.lookup_char_pos(sp.lo());
540 let hi = self.lookup_char_pos(sp.hi());
541 format!(
542 "{}:{}:{}: {}:{}",
543 lo.file.name,
544 lo.line,
545 lo.col.to_usize() + 1,
546 hi.line,
547 hi.col.to_usize() + 1,
548 )
549 }
550
551 pub fn span_to_filename(&self, sp: Span) -> FileName {
552 self.lookup_char_pos(sp.lo()).file.name.clone()
553 }
554
555 pub fn span_to_unmapped_path(&self, sp: Span) -> FileName {
556 self.lookup_char_pos(sp.lo())
557 .file
558 .unmapped_path
559 .clone()
560 .expect("`SourceMap::span_to_unmapped_path` called for imported `SourceFile`?")
561 }
562
563 pub fn is_multiline(&self, sp: Span) -> bool {
564 let lo = self.lookup_char_pos(sp.lo());
565 let hi = self.lookup_char_pos(sp.hi());
566 lo.line != hi.line
567 }
568
569 pub fn is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError> {
570 let lo = self.lookup_char_pos(sp.lo());
571 debug!("span_to_lines: lo={:?}", lo);
572 let hi = self.lookup_char_pos(sp.hi());
573 debug!("span_to_lines: hi={:?}", hi);
574 if lo.file.start_pos != hi.file.start_pos {
575 return Err(SpanLinesError::DistinctSources(DistinctSources {
576 begin: (lo.file.name.clone(), lo.file.start_pos),
577 end: (hi.file.name.clone(), hi.file.start_pos),
578 }));
579 }
580 Ok((lo, hi))
581 }
582
583 pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
584 match self.span_to_prev_source(sp) {
585 Ok(s) => s.split('\n').last().map(|l| l.trim_start().is_empty()).unwrap_or(false),
586 Err(_) => false,
587 }
588 }
589
590 pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
591 debug!("span_to_lines(sp={:?})", sp);
592 let (lo, hi) = self.is_valid_span(sp)?;
593 assert!(hi.line >= lo.line);
594
595 if sp.is_dummy() {
596 return Ok(FileLines { file: lo.file, lines: Vec::new() });
597 }
598
599 let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
600
601 // The span starts partway through the first line,
602 // but after that it starts from offset 0.
603 let mut start_col = lo.col;
604
605 // For every line but the last, it extends from `start_col`
606 // and to the end of the line. Be careful because the line
607 // numbers in Loc are 1-based, so we subtract 1 to get 0-based
608 // lines.
609 //
610 // FIXME: now that we handle DUMMY_SP up above, we should consider
611 // asserting that the line numbers here are all indeed 1-based.
612 let hi_line = hi.line.saturating_sub(1);
613 for line_index in lo.line.saturating_sub(1)..hi_line {
614 let line_len = lo.file.get_line(line_index).map(|s| s.chars().count()).unwrap_or(0);
615 lines.push(LineInfo { line_index, start_col, end_col: CharPos::from_usize(line_len) });
616 start_col = CharPos::from_usize(0);
617 }
618
619 // For the last line, it extends from `start_col` to `hi.col`:
620 lines.push(LineInfo { line_index: hi_line, start_col, end_col: hi.col });
621
622 Ok(FileLines { file: lo.file, lines })
623 }
624
625 /// Extracts the source surrounding the given `Span` using the `extract_source` function. The
626 /// extract function takes three arguments: a string slice containing the source, an index in
627 /// the slice for the beginning of the span and an index in the slice for the end of the span.
628 fn span_to_source<F>(&self, sp: Span, extract_source: F) -> Result<String, SpanSnippetError>
629 where
630 F: Fn(&str, usize, usize) -> Result<String, SpanSnippetError>,
631 {
632 let local_begin = self.lookup_byte_offset(sp.lo());
633 let local_end = self.lookup_byte_offset(sp.hi());
634
635 if local_begin.sf.start_pos != local_end.sf.start_pos {
636 Err(SpanSnippetError::DistinctSources(DistinctSources {
637 begin: (local_begin.sf.name.clone(), local_begin.sf.start_pos),
638 end: (local_end.sf.name.clone(), local_end.sf.start_pos),
639 }))
640 } else {
641 self.ensure_source_file_source_present(local_begin.sf.clone());
642
643 let start_index = local_begin.pos.to_usize();
644 let end_index = local_end.pos.to_usize();
645 let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
646
647 if start_index > end_index || end_index > source_len {
648 return Err(SpanSnippetError::MalformedForSourcemap(MalformedSourceMapPositions {
649 name: local_begin.sf.name.clone(),
650 source_len,
651 begin_pos: local_begin.pos,
652 end_pos: local_end.pos,
653 }));
654 }
655
656 if let Some(ref src) = local_begin.sf.src {
657 extract_source(src, start_index, end_index)
658 } else if let Some(src) = local_begin.sf.external_src.borrow().get_source() {
659 extract_source(src, start_index, end_index)
660 } else {
661 Err(SpanSnippetError::SourceNotAvailable { filename: local_begin.sf.name.clone() })
662 }
663 }
664 }
665
666 /// Returns the source snippet as `String` corresponding to the given `Span`.
667 pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
668 self.span_to_source(sp, |src, start_index, end_index| {
669 src.get(start_index..end_index)
670 .map(|s| s.to_string())
671 .ok_or_else(|| SpanSnippetError::IllFormedSpan(sp))
672 })
673 }
674
675 pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
676 match self.span_to_prev_source(sp) {
677 Err(_) => None,
678 Ok(source) => source
679 .split('\n')
680 .last()
681 .map(|last_line| last_line.len() - last_line.trim_start().len()),
682 }
683 }
684
685 /// Returns the source snippet as `String` before the given `Span`.
686 pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
687 self.span_to_source(sp, |src, start_index, _| {
688 src.get(..start_index)
689 .map(|s| s.to_string())
690 .ok_or_else(|| SpanSnippetError::IllFormedSpan(sp))
691 })
692 }
693
694 /// Extends the given `Span` to just after the previous occurrence of `c`. Return the same span
695 /// if no character could be found or if an error occurred while retrieving the code snippet.
696 pub fn span_extend_to_prev_char(&self, sp: Span, c: char) -> Span {
697 if let Ok(prev_source) = self.span_to_prev_source(sp) {
698 let prev_source = prev_source.rsplit(c).next().unwrap_or("").trim_start();
699 if !prev_source.is_empty() && !prev_source.contains('\n') {
700 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
701 }
702 }
703
704 sp
705 }
706
707 /// Extends the given `Span` to just after the previous occurrence of `pat` when surrounded by
708 /// whitespace. Returns the same span if no character could be found or if an error occurred
709 /// while retrieving the code snippet.
710 pub fn span_extend_to_prev_str(&self, sp: Span, pat: &str, accept_newlines: bool) -> Span {
711 // assure that the pattern is delimited, to avoid the following
712 // fn my_fn()
713 // ^^^^ returned span without the check
714 // ---------- correct span
715 for ws in &[" ", "\t", "\n"] {
716 let pat = pat.to_owned() + ws;
717 if let Ok(prev_source) = self.span_to_prev_source(sp) {
718 let prev_source = prev_source.rsplit(&pat).next().unwrap_or("").trim_start();
719 if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) {
720 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
721 }
722 }
723 }
724
725 sp
726 }
727
728 /// Given a `Span`, tries to get a shorter span ending before the first occurrence of `char`
729 /// `c`.
730 pub fn span_until_char(&self, sp: Span, c: char) -> Span {
731 match self.span_to_snippet(sp) {
732 Ok(snippet) => {
733 let snippet = snippet.split(c).next().unwrap_or("").trim_end();
734 if !snippet.is_empty() && !snippet.contains('\n') {
735 sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
736 } else {
737 sp
738 }
739 }
740 _ => sp,
741 }
742 }
743
744 /// Given a `Span`, tries to get a shorter span ending just after the first occurrence of `char`
745 /// `c`.
746 pub fn span_through_char(&self, sp: Span, c: char) -> Span {
747 if let Ok(snippet) = self.span_to_snippet(sp) {
748 if let Some(offset) = snippet.find(c) {
749 return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32));
750 }
751 }
752 sp
753 }
754
755 /// Given a `Span`, gets a new `Span` covering the first token and all its trailing whitespace
756 /// or the original `Span`.
757 ///
758 /// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
759 pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
760 let mut whitespace_found = false;
761
762 self.span_take_while(sp, |c| {
763 if !whitespace_found && c.is_whitespace() {
764 whitespace_found = true;
765 }
766
767 !whitespace_found || c.is_whitespace()
768 })
769 }
770
771 /// Given a `Span`, gets a new `Span` covering the first token without its trailing whitespace
772 /// or the original `Span` in case of error.
773 ///
774 /// If `sp` points to `"let mut x"`, then a span pointing at `"let"` will be returned.
775 pub fn span_until_whitespace(&self, sp: Span) -> Span {
776 self.span_take_while(sp, |c| !c.is_whitespace())
777 }
778
779 /// Given a `Span`, gets a shorter one until `predicate` yields `false`.
780 pub fn span_take_while<P>(&self, sp: Span, predicate: P) -> Span
781 where
782 P: for<'r> FnMut(&'r char) -> bool,
783 {
784 if let Ok(snippet) = self.span_to_snippet(sp) {
785 let offset = snippet.chars().take_while(predicate).map(|c| c.len_utf8()).sum::<usize>();
786
787 sp.with_hi(BytePos(sp.lo().0 + (offset as u32)))
788 } else {
789 sp
790 }
791 }
792
793 /// Given a `Span`, return a span ending in the closest `{`. This is useful when you have a
794 /// `Span` enclosing a whole item but we need to point at only the head (usually the first
795 /// line) of that item.
796 ///
797 /// *Only suitable for diagnostics.*
798 pub fn guess_head_span(&self, sp: Span) -> Span {
799 // FIXME: extend the AST items to have a head span, or replace callers with pointing at
800 // the item's ident when appropriate.
801 self.span_until_char(sp, '{')
802 }
803
804 /// Returns a new span representing just the start point of this span.
805 pub fn start_point(&self, sp: Span) -> Span {
806 let pos = sp.lo().0;
807 let width = self.find_width_of_character_at_span(sp, false);
808 let corrected_start_position = pos.checked_add(width).unwrap_or(pos);
809 let end_point = BytePos(cmp::max(corrected_start_position, sp.lo().0));
810 sp.with_hi(end_point)
811 }
812
813 /// Returns a new span representing just the end point of this span.
814 pub fn end_point(&self, sp: Span) -> Span {
815 let pos = sp.hi().0;
816
817 let width = self.find_width_of_character_at_span(sp, false);
818 let corrected_end_position = pos.checked_sub(width).unwrap_or(pos);
819
820 let end_point = BytePos(cmp::max(corrected_end_position, sp.lo().0));
821 sp.with_lo(end_point)
822 }
823
824 /// Returns a new span representing the next character after the end-point of this span.
825 pub fn next_point(&self, sp: Span) -> Span {
826 let start_of_next_point = sp.hi().0;
827
828 let width = self.find_width_of_character_at_span(sp.shrink_to_hi(), true);
829 // If the width is 1, then the next span should point to the same `lo` and `hi`. However,
830 // in the case of a multibyte character, where the width != 1, the next span should
831 // span multiple bytes to include the whole character.
832 let end_of_next_point =
833 start_of_next_point.checked_add(width - 1).unwrap_or(start_of_next_point);
834
835 let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point));
836 Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt())
837 }
838
839 /// Finds the width of a character, either before or after the provided span.
840 fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
841 let sp = sp.data();
842 if sp.lo == sp.hi {
843 debug!("find_width_of_character_at_span: early return empty span");
844 return 1;
845 }
846
847 let local_begin = self.lookup_byte_offset(sp.lo);
848 let local_end = self.lookup_byte_offset(sp.hi);
849 debug!(
850 "find_width_of_character_at_span: local_begin=`{:?}`, local_end=`{:?}`",
851 local_begin, local_end
852 );
853
854 if local_begin.sf.start_pos != local_end.sf.start_pos {
855 debug!("find_width_of_character_at_span: begin and end are in different files");
856 return 1;
857 }
858
859 let start_index = local_begin.pos.to_usize();
860 let end_index = local_end.pos.to_usize();
861 debug!(
862 "find_width_of_character_at_span: start_index=`{:?}`, end_index=`{:?}`",
863 start_index, end_index
864 );
865
866 // Disregard indexes that are at the start or end of their spans, they can't fit bigger
867 // characters.
868 if (!forwards && end_index == usize::MIN) || (forwards && start_index == usize::MAX) {
869 debug!("find_width_of_character_at_span: start or end of span, cannot be multibyte");
870 return 1;
871 }
872
873 let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
874 debug!("find_width_of_character_at_span: source_len=`{:?}`", source_len);
875 // Ensure indexes are also not malformed.
876 if start_index > end_index || end_index > source_len {
877 debug!("find_width_of_character_at_span: source indexes are malformed");
878 return 1;
879 }
880
881 let src = local_begin.sf.external_src.borrow();
882
883 // We need to extend the snippet to the end of the src rather than to end_index so when
884 // searching forwards for boundaries we've got somewhere to search.
885 let snippet = if let Some(ref src) = local_begin.sf.src {
886 let len = src.len();
887 &src[start_index..len]
888 } else if let Some(src) = src.get_source() {
889 let len = src.len();
890 &src[start_index..len]
891 } else {
892 return 1;
893 };
894 debug!("find_width_of_character_at_span: snippet=`{:?}`", snippet);
895
896 let mut target = if forwards { end_index + 1 } else { end_index - 1 };
897 debug!("find_width_of_character_at_span: initial target=`{:?}`", target);
898
899 while !snippet.is_char_boundary(target - start_index) && target < source_len {
900 target = if forwards {
901 target + 1
902 } else {
903 match target.checked_sub(1) {
904 Some(target) => target,
905 None => {
906 break;
907 }
908 }
909 };
910 debug!("find_width_of_character_at_span: target=`{:?}`", target);
911 }
912 debug!("find_width_of_character_at_span: final target=`{:?}`", target);
913
914 if forwards { (target - end_index) as u32 } else { (end_index - target) as u32 }
915 }
916
917 pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
918 for sf in self.files.borrow().source_files.iter() {
919 if *filename == sf.name {
920 return Some(sf.clone());
921 }
922 }
923 None
924 }
925
926 /// For a global `BytePos`, computes the local offset within the containing `SourceFile`.
927 pub fn lookup_byte_offset(&self, bpos: BytePos) -> SourceFileAndBytePos {
928 let idx = self.lookup_source_file_idx(bpos);
929 let sf = (*self.files.borrow().source_files)[idx].clone();
930 let offset = bpos - sf.start_pos;
931 SourceFileAndBytePos { sf, pos: offset }
932 }
933
934 /// Converts an absolute `BytePos` to a `CharPos` relative to the `SourceFile`.
935 pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos {
936 let idx = self.lookup_source_file_idx(bpos);
937 let map = &(*self.files.borrow().source_files)[idx];
938
939 // The number of extra bytes due to multibyte chars in the `SourceFile`.
940 let mut total_extra_bytes = 0;
941
942 for mbc in map.multibyte_chars.iter() {
943 debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos);
944 if mbc.pos < bpos {
945 // Every character is at least one byte, so we only
946 // count the actual extra bytes.
947 total_extra_bytes += mbc.bytes as u32 - 1;
948 // We should never see a byte position in the middle of a
949 // character.
950 assert!(bpos.to_u32() >= mbc.pos.to_u32() + mbc.bytes as u32);
951 } else {
952 break;
953 }
954 }
955
956 assert!(map.start_pos.to_u32() + total_extra_bytes <= bpos.to_u32());
957 CharPos(bpos.to_usize() - map.start_pos.to_usize() - total_extra_bytes as usize)
958 }
959
960 // Returns the index of the `SourceFile` (in `self.files`) that contains `pos`.
961 // This index is guaranteed to be valid for the lifetime of this `SourceMap`,
962 // since `source_files` is a `MonotonicVec`
963 pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
964 self.files
965 .borrow()
966 .source_files
967 .binary_search_by_key(&pos, |key| key.start_pos)
968 .unwrap_or_else(|p| p - 1)
969 }
970
971 pub fn count_lines(&self) -> usize {
972 self.files().iter().fold(0, |a, f| a + f.count_lines())
973 }
974
975 pub fn generate_fn_name_span(&self, span: Span) -> Option<Span> {
976 let prev_span = self.span_extend_to_prev_str(span, "fn", true);
977 if let Ok(snippet) = self.span_to_snippet(prev_span) {
978 debug!(
979 "generate_fn_name_span: span={:?}, prev_span={:?}, snippet={:?}",
980 span, prev_span, snippet
981 );
982
983 if snippet.is_empty() {
984 return None;
985 };
986
987 let len = snippet
988 .find(|c: char| !c.is_alphanumeric() && c != '_')
989 .expect("no label after fn");
990 Some(prev_span.with_hi(BytePos(prev_span.lo().0 + len as u32)))
991 } else {
992 None
993 }
994 }
995
996 /// Takes the span of a type parameter in a function signature and try to generate a span for
997 /// the function name (with generics) and a new snippet for this span with the pointed type
998 /// parameter as a new local type parameter.
999 ///
1000 /// For instance:
1001 /// ```rust,ignore (pseudo-Rust)
1002 /// // Given span
1003 /// fn my_function(param: T)
1004 /// // ^ Original span
1005 ///
1006 /// // Result
1007 /// fn my_function(param: T)
1008 /// // ^^^^^^^^^^^ Generated span with snippet `my_function<T>`
1009 /// ```
1010 ///
1011 /// Attention: The method used is very fragile since it essentially duplicates the work of the
1012 /// parser. If you need to use this function or something similar, please consider updating the
1013 /// `SourceMap` functions and this function to something more robust.
1014 pub fn generate_local_type_param_snippet(&self, span: Span) -> Option<(Span, String)> {
1015 // Try to extend the span to the previous "fn" keyword to retrieve the function
1016 // signature.
1017 let sugg_span = self.span_extend_to_prev_str(span, "fn", false);
1018 if sugg_span != span {
1019 if let Ok(snippet) = self.span_to_snippet(sugg_span) {
1020 // Consume the function name.
1021 let mut offset = snippet
1022 .find(|c: char| !c.is_alphanumeric() && c != '_')
1023 .expect("no label after fn");
1024
1025 // Consume the generics part of the function signature.
1026 let mut bracket_counter = 0;
1027 let mut last_char = None;
1028 for c in snippet[offset..].chars() {
1029 match c {
1030 '<' => bracket_counter += 1,
1031 '>' => bracket_counter -= 1,
1032 '(' => {
1033 if bracket_counter == 0 {
1034 break;
1035 }
1036 }
1037 _ => {}
1038 }
1039 offset += c.len_utf8();
1040 last_char = Some(c);
1041 }
1042
1043 // Adjust the suggestion span to encompass the function name with its generics.
1044 let sugg_span = sugg_span.with_hi(BytePos(sugg_span.lo().0 + offset as u32));
1045
1046 // Prepare the new suggested snippet to append the type parameter that triggered
1047 // the error in the generics of the function signature.
1048 let mut new_snippet = if last_char == Some('>') {
1049 format!("{}, ", &snippet[..(offset - '>'.len_utf8())])
1050 } else {
1051 format!("{}<", &snippet[..offset])
1052 };
1053 new_snippet
1054 .push_str(&self.span_to_snippet(span).unwrap_or_else(|_| "T".to_string()));
1055 new_snippet.push('>');
1056
1057 return Some((sugg_span, new_snippet));
1058 }
1059 }
1060
1061 None
1062 }
1063 pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
1064 source_file.add_external_src(|| match source_file.name {
1065 FileName::Real(ref name) => self.file_loader.read_file(name.local_path()).ok(),
1066 _ => None,
1067 })
1068 }
1069
1070 pub fn is_imported(&self, sp: Span) -> bool {
1071 let source_file_index = self.lookup_source_file_idx(sp.lo());
1072 let source_file = &self.files()[source_file_index];
1073 source_file.is_imported()
1074 }
1075 }
1076
1077 #[derive(Clone)]
1078 pub struct FilePathMapping {
1079 mapping: Vec<(PathBuf, PathBuf)>,
1080 }
1081
1082 impl FilePathMapping {
1083 pub fn empty() -> FilePathMapping {
1084 FilePathMapping { mapping: vec![] }
1085 }
1086
1087 pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping {
1088 FilePathMapping { mapping }
1089 }
1090
1091 /// Applies any path prefix substitution as defined by the mapping.
1092 /// The return value is the remapped path and a boolean indicating whether
1093 /// the path was affected by the mapping.
1094 pub fn map_prefix(&self, path: PathBuf) -> (PathBuf, bool) {
1095 // NOTE: We are iterating over the mapping entries from last to first
1096 // because entries specified later on the command line should
1097 // take precedence.
1098 for &(ref from, ref to) in self.mapping.iter().rev() {
1099 if let Ok(rest) = path.strip_prefix(from) {
1100 return (to.join(rest), true);
1101 }
1102 }
1103
1104 (path, false)
1105 }
1106 }