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