diff options
Diffstat (limited to 'compiler/rustc_span/src/source_map.rs')
-rw-r--r-- | compiler/rustc_span/src/source_map.rs | 84 |
1 files changed, 33 insertions, 51 deletions
diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 0b575c13a..dcf346acb 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -9,21 +9,16 @@ //! within the `SourceMap`, which upon request can be converted to line and column //! information, source code snippets, etc. -pub use crate::hygiene::{ExpnData, ExpnKind}; -pub use crate::*; - +use crate::*; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::{Hash128, Hash64, StableHasher}; use rustc_data_structures::sync::{IntoDynSyncSend, Lrc, MappedReadGuard, ReadGuard, RwLock}; use std::cmp; +use std::fs; use std::hash::Hash; +use std::io::{self, BorrowedBuf, Read}; use std::path::{self, Path, PathBuf}; -use std::fs; -use std::io; -use std::io::BorrowedBuf; -use std::io::Read; - #[cfg(test)] mod tests; @@ -41,7 +36,7 @@ pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span { } } -pub mod monotonic { +mod monotonic { use std::ops::{Deref, DerefMut}; /// A `MonotonicVec` is a `Vec` which can only be grown. @@ -51,18 +46,14 @@ pub mod monotonic { // field is inaccessible pub struct MonotonicVec<T>(Vec<T>); impl<T> MonotonicVec<T> { - pub fn new(val: Vec<T>) -> MonotonicVec<T> { - MonotonicVec(val) - } - - pub fn push(&mut self, val: T) { + pub(super) fn push(&mut self, val: T) { self.0.push(val); } } impl<T> Default for MonotonicVec<T> { fn default() -> Self { - MonotonicVec::new(vec![]) + MonotonicVec(vec![]) } } @@ -207,7 +198,7 @@ impl StableSourceFileId { // #[derive(Default)] -pub(super) struct SourceMapFiles { +struct SourceMapFiles { source_files: monotonic::MonotonicVec<Lrc<SourceFile>>, stable_id_to_source_file: FxHashMap<StableSourceFileId, Lrc<SourceFile>>, } @@ -466,33 +457,6 @@ impl SourceMap { self.span_to_string(sp, FileNameDisplayPreference::Remapped) } - /// Format the span location suitable for pretty printing annotations with relative line numbers - pub fn span_to_relative_line_string(&self, sp: Span, relative_to: Span) -> String { - if self.files.borrow().source_files.is_empty() || sp.is_dummy() || relative_to.is_dummy() { - return "no-location".to_string(); - } - - let lo = self.lookup_char_pos(sp.lo()); - let hi = self.lookup_char_pos(sp.hi()); - let offset = self.lookup_char_pos(relative_to.lo()); - - if lo.file.name != offset.file.name || !relative_to.contains(sp) { - return self.span_to_embeddable_string(sp); - } - - let lo_line = lo.line.saturating_sub(offset.line); - let hi_line = hi.line.saturating_sub(offset.line); - - format!( - "{}:+{}:{}: +{}:{}", - lo.file.name.display(FileNameDisplayPreference::Remapped), - lo_line, - lo.col.to_usize() + 1, - hi_line, - hi.col.to_usize() + 1, - ) - } - /// Format the span location to be printed in diagnostics. Must not be emitted /// to build artifacts as this may leak local file paths. Use span_to_embeddable_string /// for string suitable for embedding. @@ -1124,16 +1088,13 @@ pub struct FilePathMapping { impl FilePathMapping { pub fn empty() -> FilePathMapping { - FilePathMapping::new(Vec::new()) + FilePathMapping::new(Vec::new(), FileNameDisplayPreference::Local) } - pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping { - let filename_display_for_diagnostics = if mapping.is_empty() { - FileNameDisplayPreference::Local - } else { - FileNameDisplayPreference::Remapped - }; - + pub fn new( + mapping: Vec<(PathBuf, PathBuf)>, + filename_display_for_diagnostics: FileNameDisplayPreference, + ) -> FilePathMapping { FilePathMapping { mapping, filename_display_for_diagnostics } } @@ -1287,6 +1248,27 @@ impl FilePathMapping { } } + /// Expand a relative path to an absolute path **without** remapping taken into account. + /// + /// The resulting `RealFileName` will have its `virtual_path` portion erased if + /// possible (i.e. if there's also a remapped path). + pub fn to_local_embeddable_absolute_path( + &self, + file_path: RealFileName, + working_directory: &RealFileName, + ) -> RealFileName { + let file_path = file_path.local_path_if_available(); + if file_path.is_absolute() { + // No remapping has applied to this path and it is absolute, + // so the working directory cannot influence it either, so + // we are done. + return RealFileName::LocalPath(file_path.to_path_buf()); + } + debug_assert!(file_path.is_relative()); + let working_directory = working_directory.local_path_if_available(); + RealFileName::LocalPath(Path::new(working_directory).join(file_path)) + } + /// Attempts to (heuristically) reverse a prefix mapping. /// /// Returns [`Some`] if there is exactly one mapping where the "to" part is |