1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! The SourceMap tracks all the source code used within a single crate, mapping
12 //! from integer byte positions to the original source code location. Each bit
13 //! of source parsed during crate parsing (typically files, in-memory strings,
14 //! or various bits of macro expansion) cover a continuous range of bytes in the
15 //! SourceMap and are represented by SourceFiles. Byte positions are stored in
16 //! `spans` and used pervasively in the compiler. They are absolute positions
17 //! within the SourceMap, which upon request can be converted to line and column
18 //! information, source code snippets, etc.
21 pub use syntax_pos::*;
22 pub use syntax_pos::hygiene::{ExpnFormat, ExpnInfo};
23 pub use self::ExpnFormat::*;
25 use rustc_data_structures::fx::FxHashMap;
26 use rustc_data_structures::stable_hasher::StableHasher;
27 use rustc_data_structures::sync::{Lrc, Lock, LockGuard, MappedLockGuard};
30 use std::path::{Path, PathBuf};
34 use std::io::{self, Read};
35 use errors::SourceMapper;
37 /// Return the span itself if it doesn't come from a macro expansion,
38 /// otherwise return the call site span up to the `enclosing_sp` by
39 /// following the `expn_info` chain.
40 pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span {
41 let call_site1 = sp.ctxt().outer().expn_info().map(|ei| ei.call_site);
42 let call_site2 = enclosing_sp.ctxt().outer().expn_info().map(|ei| ei.call_site);
43 match (call_site1, call_site2) {
45 (Some(call_site1), Some(call_site2)) if call_site1 == call_site2 => sp,
46 (Some(call_site1), _) => original_sp(call_site1, enclosing_sp),
50 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
51 pub struct Spanned<T> {
56 pub fn respan<T>(sp: Span, t: T) -> Spanned<T> {
57 Spanned {node: t, span: sp}
60 pub fn dummy_spanned<T>(t: T) -> Spanned<T> {
64 // _____________________________________________________________________________
65 // SourceFile, MultiByteChar, FileName, FileLines
68 /// An abstraction over the fs operations used by the Parser.
69 pub trait FileLoader {
70 /// Query the existence of a file.
71 fn file_exists(&self, path: &Path) -> bool;
73 /// Return an absolute path to a file, if possible.
74 fn abs_path(&self, path: &Path) -> Option<PathBuf>;
76 /// Read the contents of an UTF-8 file into memory.
77 fn read_file(&self, path: &Path) -> io::Result<String>;
80 /// A FileLoader that uses std::fs to load real files.
81 pub struct RealFileLoader;
83 impl FileLoader for RealFileLoader {
84 fn file_exists(&self, path: &Path) -> bool {
85 fs::metadata(path).is_ok()
88 fn abs_path(&self, path: &Path) -> Option<PathBuf> {
89 if path.is_absolute() {
90 Some(path.to_path_buf())
94 .map(|cwd| cwd.join(path))
98 fn read_file(&self, path: &Path) -> io::Result<String> {
99 let mut src = String::new();
100 fs::File::open(path)?.read_to_string(&mut src)?;
105 // This is a SourceFile identifier that is used to correlate SourceFiles between
106 // subsequent compilation sessions (which is something we need to do during
107 // incremental compilation).
108 #[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
109 pub struct StableFilemapId(u128);
111 impl StableFilemapId {
112 pub fn new(source_file: &SourceFile) -> StableFilemapId {
113 let mut hasher = StableHasher::new();
115 source_file.name.hash(&mut hasher);
116 source_file.name_was_remapped.hash(&mut hasher);
117 source_file.unmapped_path.hash(&mut hasher);
119 StableFilemapId(hasher.finish())
123 // _____________________________________________________________________________
128 pub(super) struct SourceMapFiles {
129 pub(super) file_maps: Vec<Lrc<SourceFile>>,
130 stable_id_to_source_file: FxHashMap<StableFilemapId, Lrc<SourceFile>>
133 pub struct SourceMap {
134 pub(super) files: Lock<SourceMapFiles>,
135 file_loader: Box<dyn FileLoader + Sync + Send>,
136 // This is used to apply the file path remapping as specified via
137 // --remap-path-prefix to all SourceFiles allocated within this SourceMap.
138 path_mapping: FilePathMapping,
139 /// In case we are in a doctest, replace all file names with the PathBuf,
140 /// and add the given offsets to the line info
141 doctest_offset: Option<(FileName, isize)>,
145 pub fn new(path_mapping: FilePathMapping) -> SourceMap {
147 files: Default::default(),
148 file_loader: Box::new(RealFileLoader),
150 doctest_offset: None,
154 pub fn new_doctest(path_mapping: FilePathMapping,
155 file: FileName, line: isize) -> SourceMap {
157 doctest_offset: Some((file, line)),
158 ..SourceMap::new(path_mapping)
163 pub fn with_file_loader(file_loader: Box<dyn FileLoader + Sync + Send>,
164 path_mapping: FilePathMapping)
167 files: Default::default(),
168 file_loader: file_loader,
170 doctest_offset: None,
174 pub fn path_mapping(&self) -> &FilePathMapping {
178 pub fn file_exists(&self, path: &Path) -> bool {
179 self.file_loader.file_exists(path)
182 pub fn load_file(&self, path: &Path) -> io::Result<Lrc<SourceFile>> {
183 let src = self.file_loader.read_file(path)?;
184 let filename = if let Some((ref name, _)) = self.doctest_offset {
187 path.to_owned().into()
189 Ok(self.new_source_file(filename, src))
192 pub fn files(&self) -> MappedLockGuard<Vec<Lrc<SourceFile>>> {
193 LockGuard::map(self.files.borrow(), |files| &mut files.file_maps)
196 pub fn source_file_by_stable_id(&self, stable_id: StableFilemapId) -> Option<Lrc<SourceFile>> {
197 self.files.borrow().stable_id_to_source_file.get(&stable_id).map(|fm| fm.clone())
200 fn next_start_pos(&self) -> usize {
201 match self.files.borrow().file_maps.last() {
203 // Add one so there is some space between files. This lets us distinguish
204 // positions in the source_map, even in the presence of zero-length files.
205 Some(last) => last.end_pos.to_usize() + 1,
209 /// Creates a new source_file.
210 /// This does not ensure that only one SourceFile exists per file name.
211 pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
212 let start_pos = self.next_start_pos();
214 // The path is used to determine the directory for loading submodules and
215 // include files, so it must be before remapping.
216 // Note that filename may not be a valid path, eg it may be `<anon>` etc,
217 // but this is okay because the directory determined by `path.pop()` will
218 // be empty, so the working directory will be used.
219 let unmapped_path = filename.clone();
221 let (filename, was_remapped) = match filename {
222 FileName::Real(filename) => {
223 let (filename, was_remapped) = self.path_mapping.map_prefix(filename);
224 (FileName::Real(filename), was_remapped)
226 other => (other, false),
228 let source_file = Lrc::new(SourceFile::new(
233 Pos::from_usize(start_pos),
236 let mut files = self.files.borrow_mut();
238 files.file_maps.push(source_file.clone());
239 files.stable_id_to_source_file.insert(StableFilemapId::new(&source_file),
240 source_file.clone());
245 /// Allocates a new SourceFile representing a source file from an external
246 /// crate. The source code of such an "imported source_file" is not available,
247 /// but we still know enough to generate accurate debuginfo location
248 /// information for things inlined from other crates.
249 pub fn new_imported_source_file(
252 name_was_remapped: bool,
253 crate_of_origin: u32,
257 mut file_local_lines: Vec<BytePos>,
258 mut file_local_multibyte_chars: Vec<MultiByteChar>,
259 mut file_local_non_narrow_chars: Vec<NonNarrowChar>,
260 ) -> Lrc<SourceFile> {
261 let start_pos = self.next_start_pos();
263 let end_pos = Pos::from_usize(start_pos + source_len);
264 let start_pos = Pos::from_usize(start_pos);
266 for pos in &mut file_local_lines {
267 *pos = *pos + start_pos;
270 for mbc in &mut file_local_multibyte_chars {
271 mbc.pos = mbc.pos + start_pos;
274 for swc in &mut file_local_non_narrow_chars {
275 *swc = *swc + start_pos;
278 let source_file = Lrc::new(SourceFile {
285 external_src: Lock::new(ExternalSource::AbsentOk),
288 lines: file_local_lines,
289 multibyte_chars: file_local_multibyte_chars,
290 non_narrow_chars: file_local_non_narrow_chars,
294 let mut files = self.files.borrow_mut();
296 files.file_maps.push(source_file.clone());
297 files.stable_id_to_source_file.insert(StableFilemapId::new(&source_file),
298 source_file.clone());
303 pub fn mk_substr_filename(&self, sp: Span) -> String {
304 let pos = self.lookup_char_pos(sp.lo());
305 format!("<{}:{}:{}>",
308 pos.col.to_usize() + 1)
311 // If there is a doctest_offset, apply it to the line
312 pub fn doctest_offset_line(&self, mut orig: usize) -> usize {
313 if let Some((_, line)) = self.doctest_offset {
315 orig = orig + line as usize;
317 orig = orig - (-line) as usize;
323 /// Lookup source information about a BytePos
324 pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
325 let chpos = self.bytepos_to_file_charpos(pos);
326 match self.lookup_line(pos) {
327 Ok(SourceFileAndLine { fm: f, line: a }) => {
328 let line = a + 1; // Line numbers start at 1
329 let linebpos = f.lines[a];
330 let linechpos = self.bytepos_to_file_charpos(linebpos);
331 let col = chpos - linechpos;
334 let start_width_idx = f
336 .binary_search_by_key(&linebpos, |x| x.pos())
337 .unwrap_or_else(|x| x);
338 let end_width_idx = f
340 .binary_search_by_key(&pos, |x| x.pos())
341 .unwrap_or_else(|x| x);
342 let special_chars = end_width_idx - start_width_idx;
343 let non_narrow: usize = f
344 .non_narrow_chars[start_width_idx..end_width_idx]
348 col.0 - special_chars + non_narrow
350 debug!("byte pos {:?} is on the line at byte pos {:?}",
352 debug!("char pos {:?} is on the line at char pos {:?}",
354 debug!("byte is on line: {}", line);
355 assert!(chpos >= linechpos);
365 let end_width_idx = f
367 .binary_search_by_key(&pos, |x| x.pos())
368 .unwrap_or_else(|x| x);
369 let non_narrow: usize = f
370 .non_narrow_chars[0..end_width_idx]
374 chpos.0 - end_width_idx + non_narrow
386 // If the relevant source_file is empty, we don't return a line number.
387 pub fn lookup_line(&self, pos: BytePos) -> Result<SourceFileAndLine, Lrc<SourceFile>> {
388 let idx = self.lookup_source_file_idx(pos);
390 let f = (*self.files.borrow().file_maps)[idx].clone();
392 match f.lookup_line(pos) {
393 Some(line) => Ok(SourceFileAndLine { fm: f, line: line }),
398 pub fn lookup_char_pos_adj(&self, pos: BytePos) -> LocWithOpt {
399 let loc = self.lookup_char_pos(pos);
401 filename: loc.file.name.clone(),
408 /// Returns `Some(span)`, a union of the lhs and rhs span. The lhs must precede the rhs. If
409 /// there are gaps between lhs and rhs, the resulting union will cross these gaps.
410 /// For this to work, the spans have to be:
412 /// * the ctxt of both spans much match
413 /// * the lhs span needs to end on the same line the rhs span begins
414 /// * the lhs span must start at or before the rhs span
415 pub fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span> {
416 // make sure we're at the same expansion id
417 if sp_lhs.ctxt() != sp_rhs.ctxt() {
421 let lhs_end = match self.lookup_line(sp_lhs.hi()) {
423 Err(_) => return None
425 let rhs_begin = match self.lookup_line(sp_rhs.lo()) {
427 Err(_) => return None
430 // if we must cross lines to merge, don't merge
431 if lhs_end.line != rhs_begin.line {
435 // ensure these follow the expected order and we don't overlap
436 if (sp_lhs.lo() <= sp_rhs.lo()) && (sp_lhs.hi() <= sp_rhs.lo()) {
437 Some(sp_lhs.to(sp_rhs))
443 pub fn span_to_string(&self, sp: Span) -> String {
444 if self.files.borrow().file_maps.is_empty() && sp.is_dummy() {
445 return "no-location".to_string();
448 let lo = self.lookup_char_pos_adj(sp.lo());
449 let hi = self.lookup_char_pos_adj(sp.hi());
450 format!("{}:{}:{}: {}:{}",
453 lo.col.to_usize() + 1,
455 hi.col.to_usize() + 1)
458 pub fn span_to_filename(&self, sp: Span) -> FileName {
459 self.lookup_char_pos(sp.lo()).file.name.clone()
462 pub fn span_to_unmapped_path(&self, sp: Span) -> FileName {
463 self.lookup_char_pos(sp.lo()).file.unmapped_path.clone()
464 .expect("SourceMap::span_to_unmapped_path called for imported SourceFile?")
467 pub fn is_multiline(&self, sp: Span) -> bool {
468 let lo = self.lookup_char_pos(sp.lo());
469 let hi = self.lookup_char_pos(sp.hi());
473 pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
474 debug!("span_to_lines(sp={:?})", sp);
476 if sp.lo() > sp.hi() {
477 return Err(SpanLinesError::IllFormedSpan(sp));
480 let lo = self.lookup_char_pos(sp.lo());
481 debug!("span_to_lines: lo={:?}", lo);
482 let hi = self.lookup_char_pos(sp.hi());
483 debug!("span_to_lines: hi={:?}", hi);
485 if lo.file.start_pos != hi.file.start_pos {
486 return Err(SpanLinesError::DistinctSources(DistinctSources {
487 begin: (lo.file.name.clone(), lo.file.start_pos),
488 end: (hi.file.name.clone(), hi.file.start_pos),
491 assert!(hi.line >= lo.line);
493 let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
495 // The span starts partway through the first line,
496 // but after that it starts from offset 0.
497 let mut start_col = lo.col;
499 // For every line but the last, it extends from `start_col`
500 // and to the end of the line. Be careful because the line
501 // numbers in Loc are 1-based, so we subtract 1 to get 0-based
503 for line_index in lo.line-1 .. hi.line-1 {
504 let line_len = lo.file.get_line(line_index)
505 .map(|s| s.chars().count())
507 lines.push(LineInfo { line_index,
509 end_col: CharPos::from_usize(line_len) });
510 start_col = CharPos::from_usize(0);
513 // For the last line, it extends from `start_col` to `hi.col`:
514 lines.push(LineInfo { line_index: hi.line - 1,
518 Ok(FileLines {file: lo.file, lines: lines})
521 /// Extract the source surrounding the given `Span` using the `extract_source` function. The
522 /// extract function takes three arguments: a string slice containing the source, an index in
523 /// the slice for the beginning of the span and an index in the slice for the end of the span.
524 fn span_to_source<F>(&self, sp: Span, extract_source: F) -> Result<String, SpanSnippetError>
525 where F: Fn(&str, usize, usize) -> String
527 if sp.lo() > sp.hi() {
528 return Err(SpanSnippetError::IllFormedSpan(sp));
531 let local_begin = self.lookup_byte_offset(sp.lo());
532 let local_end = self.lookup_byte_offset(sp.hi());
534 if local_begin.fm.start_pos != local_end.fm.start_pos {
535 return Err(SpanSnippetError::DistinctSources(DistinctSources {
536 begin: (local_begin.fm.name.clone(),
537 local_begin.fm.start_pos),
538 end: (local_end.fm.name.clone(),
539 local_end.fm.start_pos)
542 self.ensure_source_file_source_present(local_begin.fm.clone());
544 let start_index = local_begin.pos.to_usize();
545 let end_index = local_end.pos.to_usize();
546 let source_len = (local_begin.fm.end_pos -
547 local_begin.fm.start_pos).to_usize();
549 if start_index > end_index || end_index > source_len {
550 return Err(SpanSnippetError::MalformedForCodemap(
551 MalformedCodemapPositions {
552 name: local_begin.fm.name.clone(),
554 begin_pos: local_begin.pos,
555 end_pos: local_end.pos,
559 if let Some(ref src) = local_begin.fm.src {
560 return Ok(extract_source(src, start_index, end_index));
561 } else if let Some(src) = local_begin.fm.external_src.borrow().get_source() {
562 return Ok(extract_source(src, start_index, end_index));
564 return Err(SpanSnippetError::SourceNotAvailable {
565 filename: local_begin.fm.name.clone()
571 /// Return the source snippet as `String` corresponding to the given `Span`
572 pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
573 self.span_to_source(sp, |src, start_index, end_index| src[start_index..end_index]
577 pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
578 match self.span_to_prev_source(sp) {
580 Ok(source) => source.split('\n').last().map(|last_line| {
581 last_line.len() - last_line.trim_left().len()
586 /// Return the source snippet as `String` before the given `Span`
587 pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
588 self.span_to_source(sp, |src, start_index, _| src[..start_index].to_string())
591 /// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
592 /// if no character could be found or if an error occurred while retrieving the code snippet.
593 pub fn span_extend_to_prev_char(&self, sp: Span, c: char) -> Span {
594 if let Ok(prev_source) = self.span_to_prev_source(sp) {
595 let prev_source = prev_source.rsplit(c).nth(0).unwrap_or("").trim_left();
596 if !prev_source.is_empty() && !prev_source.contains('\n') {
597 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
604 /// Extend the given `Span` to just after the previous occurrence of `pat` when surrounded by
605 /// whitespace. Return the same span if no character could be found or if an error occurred
606 /// while retrieving the code snippet.
607 pub fn span_extend_to_prev_str(&self, sp: Span, pat: &str, accept_newlines: bool) -> Span {
608 // assure that the pattern is delimited, to avoid the following
610 // ^^^^ returned span without the check
611 // ---------- correct span
612 for ws in &[" ", "\t", "\n"] {
613 let pat = pat.to_owned() + ws;
614 if let Ok(prev_source) = self.span_to_prev_source(sp) {
615 let prev_source = prev_source.rsplit(&pat).nth(0).unwrap_or("").trim_left();
616 if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) {
617 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
625 /// Given a `Span`, try to get a shorter span ending before the first occurrence of `c` `char`
626 pub fn span_until_char(&self, sp: Span, c: char) -> Span {
627 match self.span_to_snippet(sp) {
629 let snippet = snippet.split(c).nth(0).unwrap_or("").trim_right();
630 if !snippet.is_empty() && !snippet.contains('\n') {
631 sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
640 /// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char`
642 pub fn span_through_char(&self, sp: Span, c: char) -> Span {
643 if let Ok(snippet) = self.span_to_snippet(sp) {
644 if let Some(offset) = snippet.find(c) {
645 return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32));
651 /// Given a `Span`, get a new `Span` covering the first token and all its trailing whitespace or
652 /// the original `Span`.
654 /// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
655 pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
656 let mut whitespace_found = false;
658 self.span_take_while(sp, |c| {
659 if !whitespace_found && c.is_whitespace() {
660 whitespace_found = true;
663 if whitespace_found && !c.is_whitespace() {
671 /// Given a `Span`, get a new `Span` covering the first token without its trailing whitespace or
672 /// the original `Span` in case of error.
674 /// If `sp` points to `"let mut x"`, then a span pointing at `"let"` will be returned.
675 pub fn span_until_whitespace(&self, sp: Span) -> Span {
676 self.span_take_while(sp, |c| !c.is_whitespace())
679 /// Given a `Span`, get a shorter one until `predicate` yields false.
680 pub fn span_take_while<P>(&self, sp: Span, predicate: P) -> Span
681 where P: for <'r> FnMut(&'r char) -> bool
683 if let Ok(snippet) = self.span_to_snippet(sp) {
684 let offset = snippet.chars()
685 .take_while(predicate)
686 .map(|c| c.len_utf8())
689 sp.with_hi(BytePos(sp.lo().0 + (offset as u32)))
695 pub fn def_span(&self, sp: Span) -> Span {
696 self.span_until_char(sp, '{')
699 /// Returns a new span representing just the start-point of this span
700 pub fn start_point(&self, sp: Span) -> Span {
702 let width = self.find_width_of_character_at_span(sp, false);
703 let corrected_start_position = pos.checked_add(width).unwrap_or(pos);
704 let end_point = BytePos(cmp::max(corrected_start_position, sp.lo().0));
705 sp.with_hi(end_point)
708 /// Returns a new span representing just the end-point of this span
709 pub fn end_point(&self, sp: Span) -> Span {
712 let width = self.find_width_of_character_at_span(sp, false);
713 let corrected_end_position = pos.checked_sub(width).unwrap_or(pos);
715 let end_point = BytePos(cmp::max(corrected_end_position, sp.lo().0));
716 sp.with_lo(end_point)
719 /// Returns a new span representing the next character after the end-point of this span
720 pub fn next_point(&self, sp: Span) -> Span {
721 let start_of_next_point = sp.hi().0;
723 let width = self.find_width_of_character_at_span(sp, true);
724 // If the width is 1, then the next span should point to the same `lo` and `hi`. However,
725 // in the case of a multibyte character, where the width != 1, the next span should
726 // span multiple bytes to include the whole character.
727 let end_of_next_point = start_of_next_point.checked_add(
728 width - 1).unwrap_or(start_of_next_point);
730 let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point));
731 Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt())
734 /// Finds the width of a character, either before or after the provided span.
735 fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
736 // Disregard malformed spans and assume a one-byte wide character.
737 if sp.lo() >= sp.hi() {
738 debug!("find_width_of_character_at_span: early return malformed span");
742 let local_begin = self.lookup_byte_offset(sp.lo());
743 let local_end = self.lookup_byte_offset(sp.hi());
744 debug!("find_width_of_character_at_span: local_begin=`{:?}`, local_end=`{:?}`",
745 local_begin, local_end);
747 let start_index = local_begin.pos.to_usize();
748 let end_index = local_end.pos.to_usize();
749 debug!("find_width_of_character_at_span: start_index=`{:?}`, end_index=`{:?}`",
750 start_index, end_index);
752 // Disregard indexes that are at the start or end of their spans, they can't fit bigger
754 if (!forwards && end_index == usize::min_value()) ||
755 (forwards && start_index == usize::max_value()) {
756 debug!("find_width_of_character_at_span: start or end of span, cannot be multibyte");
760 let source_len = (local_begin.fm.end_pos - local_begin.fm.start_pos).to_usize();
761 debug!("find_width_of_character_at_span: source_len=`{:?}`", source_len);
762 // Ensure indexes are also not malformed.
763 if start_index > end_index || end_index > source_len {
764 debug!("find_width_of_character_at_span: source indexes are malformed");
768 let src = local_begin.fm.external_src.borrow();
770 // We need to extend the snippet to the end of the src rather than to end_index so when
771 // searching forwards for boundaries we've got somewhere to search.
772 let snippet = if let Some(ref src) = local_begin.fm.src {
774 (&src[start_index..len])
775 } else if let Some(src) = src.get_source() {
777 (&src[start_index..len])
781 debug!("find_width_of_character_at_span: snippet=`{:?}`", snippet);
783 let mut target = if forwards { end_index + 1 } else { end_index - 1 };
784 debug!("find_width_of_character_at_span: initial target=`{:?}`", target);
786 while !snippet.is_char_boundary(target - start_index) && target < source_len {
787 target = if forwards {
790 match target.checked_sub(1) {
791 Some(target) => target,
797 debug!("find_width_of_character_at_span: target=`{:?}`", target);
799 debug!("find_width_of_character_at_span: final target=`{:?}`", target);
802 (target - end_index) as u32
804 (end_index - target) as u32
808 pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
809 for fm in self.files.borrow().file_maps.iter() {
810 if *filename == fm.name {
811 return Some(fm.clone());
817 /// For a global BytePos compute the local offset within the containing SourceFile
818 pub fn lookup_byte_offset(&self, bpos: BytePos) -> SourceFileAndBytePos {
819 let idx = self.lookup_source_file_idx(bpos);
820 let fm = (*self.files.borrow().file_maps)[idx].clone();
821 let offset = bpos - fm.start_pos;
822 SourceFileAndBytePos {fm: fm, pos: offset}
825 /// Converts an absolute BytePos to a CharPos relative to the source_file.
826 pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos {
827 let idx = self.lookup_source_file_idx(bpos);
828 let map = &(*self.files.borrow().file_maps)[idx];
830 // The number of extra bytes due to multibyte chars in the SourceFile
831 let mut total_extra_bytes = 0;
833 for mbc in map.multibyte_chars.iter() {
834 debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos);
836 // every character is at least one byte, so we only
837 // count the actual extra bytes.
838 total_extra_bytes += mbc.bytes as u32 - 1;
839 // We should never see a byte position in the middle of a
841 assert!(bpos.to_u32() >= mbc.pos.to_u32() + mbc.bytes as u32);
847 assert!(map.start_pos.to_u32() + total_extra_bytes <= bpos.to_u32());
848 CharPos(bpos.to_usize() - map.start_pos.to_usize() - total_extra_bytes as usize)
851 // Return the index of the source_file (in self.files) which contains pos.
852 pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
853 let files = self.files.borrow();
854 let files = &files.file_maps;
855 let count = files.len();
857 // Binary search for the source_file.
862 if files[m].start_pos > pos {
869 assert!(a < count, "position {} does not resolve to a source location", pos.to_usize());
874 pub fn count_lines(&self) -> usize {
875 self.files().iter().fold(0, |a, f| a + f.count_lines())
879 pub fn generate_fn_name_span(&self, span: Span) -> Option<Span> {
880 let prev_span = self.span_extend_to_prev_str(span, "fn", true);
881 self.span_to_snippet(prev_span).map(|snippet| {
882 let len = snippet.find(|c: char| !c.is_alphanumeric() && c != '_')
883 .expect("no label after fn");
884 prev_span.with_hi(BytePos(prev_span.lo().0 + len as u32))
888 /// Take the span of a type parameter in a function signature and try to generate a span for the
889 /// function name (with generics) and a new snippet for this span with the pointed type
890 /// parameter as a new local type parameter.
893 /// ```rust,ignore (pseudo-Rust)
895 /// fn my_function(param: T)
896 /// // ^ Original span
899 /// fn my_function(param: T)
900 /// // ^^^^^^^^^^^ Generated span with snippet `my_function<T>`
903 /// Attention: The method used is very fragile since it essentially duplicates the work of the
904 /// parser. If you need to use this function or something similar, please consider updating the
905 /// source_map functions and this function to something more robust.
906 pub fn generate_local_type_param_snippet(&self, span: Span) -> Option<(Span, String)> {
907 // Try to extend the span to the previous "fn" keyword to retrieve the function
909 let sugg_span = self.span_extend_to_prev_str(span, "fn", false);
910 if sugg_span != span {
911 if let Ok(snippet) = self.span_to_snippet(sugg_span) {
912 // Consume the function name
913 let mut offset = snippet.find(|c: char| !c.is_alphanumeric() && c != '_')
914 .expect("no label after fn");
916 // Consume the generics part of the function signature
917 let mut bracket_counter = 0;
918 let mut last_char = None;
919 for c in snippet[offset..].chars() {
921 '<' => bracket_counter += 1,
922 '>' => bracket_counter -= 1,
923 '(' => if bracket_counter == 0 { break; }
926 offset += c.len_utf8();
930 // Adjust the suggestion span to encompass the function name with its generics
931 let sugg_span = sugg_span.with_hi(BytePos(sugg_span.lo().0 + offset as u32));
933 // Prepare the new suggested snippet to append the type parameter that triggered
934 // the error in the generics of the function signature
935 let mut new_snippet = if last_char == Some('>') {
936 format!("{}, ", &snippet[..(offset - '>'.len_utf8())])
938 format!("{}<", &snippet[..offset])
940 new_snippet.push_str(
941 &self.span_to_snippet(span).unwrap_or_else(|_| "T".to_string()));
942 new_snippet.push('>');
944 return Some((sugg_span, new_snippet));
952 impl SourceMapper for SourceMap {
953 fn lookup_char_pos(&self, pos: BytePos) -> Loc {
954 self.lookup_char_pos(pos)
956 fn span_to_lines(&self, sp: Span) -> FileLinesResult {
957 self.span_to_lines(sp)
959 fn span_to_string(&self, sp: Span) -> String {
960 self.span_to_string(sp)
962 fn span_to_filename(&self, sp: Span) -> FileName {
963 self.span_to_filename(sp)
965 fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span> {
966 self.merge_spans(sp_lhs, sp_rhs)
968 fn call_span_if_macro(&self, sp: Span) -> Span {
969 if self.span_to_filename(sp.clone()).is_macros() {
970 let v = sp.macro_backtrace();
971 if let Some(use_site) = v.last() {
972 return use_site.call_site;
977 fn ensure_source_file_source_present(&self, file_map: Lrc<SourceFile>) -> bool {
978 file_map.add_external_src(
979 || match file_map.name {
980 FileName::Real(ref name) => self.file_loader.read_file(name).ok(),
985 fn doctest_offset_line(&self, line: usize) -> usize {
986 self.doctest_offset_line(line)
991 pub struct FilePathMapping {
992 mapping: Vec<(PathBuf, PathBuf)>,
995 impl FilePathMapping {
996 pub fn empty() -> FilePathMapping {
1002 pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping {
1008 /// Applies any path prefix substitution as defined by the mapping.
1009 /// The return value is the remapped path and a boolean indicating whether
1010 /// the path was affected by the mapping.
1011 pub fn map_prefix(&self, path: PathBuf) -> (PathBuf, bool) {
1012 // NOTE: We are iterating over the mapping entries from last to first
1013 // because entries specified later on the command line should
1015 for &(ref from, ref to) in self.mapping.iter().rev() {
1016 if let Ok(rest) = path.strip_prefix(from) {
1017 return (to.join(rest), true);
1025 // _____________________________________________________________________________
1032 use rustc_data_structures::sync::Lrc;
1034 fn init_code_map() -> SourceMap {
1035 let cm = SourceMap::new(FilePathMapping::empty());
1036 cm.new_source_file(PathBuf::from("blork.rs").into(),
1037 "first line.\nsecond line".to_string());
1038 cm.new_source_file(PathBuf::from("empty.rs").into(),
1040 cm.new_source_file(PathBuf::from("blork2.rs").into(),
1041 "first line.\nsecond line".to_string());
1047 // Test lookup_byte_offset
1048 let cm = init_code_map();
1050 let fmabp1 = cm.lookup_byte_offset(BytePos(23));
1051 assert_eq!(fmabp1.fm.name, PathBuf::from("blork.rs").into());
1052 assert_eq!(fmabp1.pos, BytePos(23));
1054 let fmabp1 = cm.lookup_byte_offset(BytePos(24));
1055 assert_eq!(fmabp1.fm.name, PathBuf::from("empty.rs").into());
1056 assert_eq!(fmabp1.pos, BytePos(0));
1058 let fmabp2 = cm.lookup_byte_offset(BytePos(25));
1059 assert_eq!(fmabp2.fm.name, PathBuf::from("blork2.rs").into());
1060 assert_eq!(fmabp2.pos, BytePos(0));
1065 // Test bytepos_to_file_charpos
1066 let cm = init_code_map();
1068 let cp1 = cm.bytepos_to_file_charpos(BytePos(22));
1069 assert_eq!(cp1, CharPos(22));
1071 let cp2 = cm.bytepos_to_file_charpos(BytePos(25));
1072 assert_eq!(cp2, CharPos(0));
1077 // Test zero-length source_files.
1078 let cm = init_code_map();
1080 let loc1 = cm.lookup_char_pos(BytePos(22));
1081 assert_eq!(loc1.file.name, PathBuf::from("blork.rs").into());
1082 assert_eq!(loc1.line, 2);
1083 assert_eq!(loc1.col, CharPos(10));
1085 let loc2 = cm.lookup_char_pos(BytePos(25));
1086 assert_eq!(loc2.file.name, PathBuf::from("blork2.rs").into());
1087 assert_eq!(loc2.line, 1);
1088 assert_eq!(loc2.col, CharPos(0));
1091 fn init_code_map_mbc() -> SourceMap {
1092 let cm = SourceMap::new(FilePathMapping::empty());
1093 // € is a three byte utf8 char.
1094 cm.new_source_file(PathBuf::from("blork.rs").into(),
1095 "fir€st €€€€ line.\nsecond line".to_string());
1096 cm.new_source_file(PathBuf::from("blork2.rs").into(),
1097 "first line€€.\n€ second line".to_string());
1103 // Test bytepos_to_file_charpos in the presence of multi-byte chars
1104 let cm = init_code_map_mbc();
1106 let cp1 = cm.bytepos_to_file_charpos(BytePos(3));
1107 assert_eq!(cp1, CharPos(3));
1109 let cp2 = cm.bytepos_to_file_charpos(BytePos(6));
1110 assert_eq!(cp2, CharPos(4));
1112 let cp3 = cm.bytepos_to_file_charpos(BytePos(56));
1113 assert_eq!(cp3, CharPos(12));
1115 let cp4 = cm.bytepos_to_file_charpos(BytePos(61));
1116 assert_eq!(cp4, CharPos(15));
1121 // Test span_to_lines for a span ending at the end of source_file
1122 let cm = init_code_map();
1123 let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION);
1124 let file_lines = cm.span_to_lines(span).unwrap();
1126 assert_eq!(file_lines.file.name, PathBuf::from("blork.rs").into());
1127 assert_eq!(file_lines.lines.len(), 1);
1128 assert_eq!(file_lines.lines[0].line_index, 1);
1131 /// Given a string like " ~~~~~~~~~~~~ ", produces a span
1132 /// converting that range. The idea is that the string has the same
1133 /// length as the input, and we uncover the byte positions. Note
1134 /// that this can span lines and so on.
1135 fn span_from_selection(input: &str, selection: &str) -> Span {
1136 assert_eq!(input.len(), selection.len());
1137 let left_index = selection.find('~').unwrap() as u32;
1138 let right_index = selection.rfind('~').map(|x|x as u32).unwrap_or(left_index);
1139 Span::new(BytePos(left_index), BytePos(right_index + 1), NO_EXPANSION)
1142 /// Test span_to_snippet and span_to_lines for a span converting 3
1143 /// lines in the middle of a file.
1145 fn span_to_snippet_and_lines_spanning_multiple_lines() {
1146 let cm = SourceMap::new(FilePathMapping::empty());
1147 let inputtext = "aaaaa\nbbbbBB\nCCC\nDDDDDddddd\neee\n";
1148 let selection = " \n ~~\n~~~\n~~~~~ \n \n";
1149 cm.new_source_file(Path::new("blork.rs").to_owned().into(), inputtext.to_string());
1150 let span = span_from_selection(inputtext, selection);
1152 // check that we are extracting the text we thought we were extracting
1153 assert_eq!(&cm.span_to_snippet(span).unwrap(), "BB\nCCC\nDDDDD");
1155 // check that span_to_lines gives us the complete result with the lines/cols we expected
1156 let lines = cm.span_to_lines(span).unwrap();
1157 let expected = vec![
1158 LineInfo { line_index: 1, start_col: CharPos(4), end_col: CharPos(6) },
1159 LineInfo { line_index: 2, start_col: CharPos(0), end_col: CharPos(3) },
1160 LineInfo { line_index: 3, start_col: CharPos(0), end_col: CharPos(5) }
1162 assert_eq!(lines.lines, expected);
1167 // Test span_to_snippet for a span ending at the end of source_file
1168 let cm = init_code_map();
1169 let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION);
1170 let snippet = cm.span_to_snippet(span);
1172 assert_eq!(snippet, Ok("second line".to_string()));
1177 // Test span_to_str for a span ending at the end of source_file
1178 let cm = init_code_map();
1179 let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION);
1180 let sstr = cm.span_to_string(span);
1182 assert_eq!(sstr, "blork.rs:2:1: 2:12");
1185 /// Test failing to merge two spans on different lines
1187 fn span_merging_fail() {
1188 let cm = SourceMap::new(FilePathMapping::empty());
1189 let inputtext = "bbbb BB\ncc CCC\n";
1190 let selection1 = " ~~\n \n";
1191 let selection2 = " \n ~~~\n";
1192 cm.new_source_file(Path::new("blork.rs").to_owned().into(), inputtext.to_owned());
1193 let span1 = span_from_selection(inputtext, selection1);
1194 let span2 = span_from_selection(inputtext, selection2);
1196 assert!(cm.merge_spans(span1, span2).is_none());
1199 /// Returns the span corresponding to the `n`th occurrence of
1200 /// `substring` in `source_text`.
1201 trait SourceMapExtension {
1202 fn span_substr(&self,
1203 file: &Lrc<SourceFile>,
1210 impl SourceMapExtension for SourceMap {
1211 fn span_substr(&self,
1212 file: &Lrc<SourceFile>,
1218 println!("span_substr(file={:?}/{:?}, substring={:?}, n={})",
1219 file.name, file.start_pos, substring, n);
1223 let offset = source_text[hi..].find(substring).unwrap_or_else(|| {
1224 panic!("source_text `{}` does not have {} occurrences of `{}`, only {}",
1225 source_text, n, substring, i);
1227 let lo = hi + offset;
1228 hi = lo + substring.len();
1230 let span = Span::new(
1231 BytePos(lo as u32 + file.start_pos.0),
1232 BytePos(hi as u32 + file.start_pos.0),
1235 assert_eq!(&self.span_to_snippet(span).unwrap()[..],