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.
10 pub use crate::hygiene
::{ExpnData, ExpnKind}
;
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}
;
17 use std
::convert
::TryFrom
;
19 use std
::path
::{Path, PathBuf}
;
20 use std
::sync
::atomic
::Ordering
;
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
40 original_sp(expn_data1
.call_site
, enclosing_sp
)
44 #[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, HashStable_Generic)]
45 pub struct Spanned
<T
> {
50 pub fn respan
<T
>(sp
: Span
, t
: T
) -> Spanned
<T
> {
51 Spanned { node: t, span: sp }
54 pub fn dummy_spanned
<T
>(t
: T
) -> Spanned
<T
> {
58 // _____________________________________________________________________________
59 // SourceFile, MultiByteChar, FileName, FileLines
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
;
67 /// Returns an absolute path to a file, if possible.
68 fn abs_path(&self, path
: &Path
) -> Option
<PathBuf
>;
70 /// Read the contents of an UTF-8 file into memory.
71 fn read_file(&self, path
: &Path
) -> io
::Result
<String
>;
74 /// A FileLoader that uses std::fs to load real files.
75 pub struct RealFileLoader
;
77 impl FileLoader
for RealFileLoader
{
78 fn file_exists(&self, path
: &Path
) -> bool
{
79 fs
::metadata(path
).is_ok()
82 fn abs_path(&self, path
: &Path
) -> Option
<PathBuf
> {
83 if path
.is_absolute() {
84 Some(path
.to_path_buf())
86 env
::current_dir().ok().map(|cwd
| cwd
.join(path
))
90 fn read_file(&self, path
: &Path
) -> io
::Result
<String
> {
91 fs
::read_to_string(path
)
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
);
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(
107 source_file
.name_was_remapped
,
108 source_file
.unmapped_path
.as_ref(),
114 name_was_remapped
: bool
,
115 unmapped_path
: Option
<&FileName
>,
116 ) -> StableSourceFileId
{
117 let mut hasher
= StableHasher
::new();
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
)
125 name
.hash(&mut hasher
);
127 name_was_remapped
.hash(&mut hasher
);
128 unmapped_path
.hash(&mut hasher
);
130 StableSourceFileId(hasher
.finish())
134 // _____________________________________________________________________________
139 pub(super) struct SourceMapFiles
{
140 source_files
: Vec
<Lrc
<SourceFile
>>,
141 stable_id_to_source_file
: FxHashMap
<StableSourceFileId
, Lrc
<SourceFile
>>,
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
,
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
,
154 /// The algorithm used for hashing the contents of each source file.
155 hash_kind
: SourceFileHashAlgorithm
,
159 pub fn new(path_mapping
: FilePathMapping
) -> SourceMap
{
160 Self::with_file_loader_and_hash_kind(
161 Box
::new(RealFileLoader
),
163 SourceFileHashAlgorithm
::Md5
,
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
,
173 used_address_space
: AtomicU32
::new(0),
174 files
: Default
::default(),
181 pub fn path_mapping(&self) -> &FilePathMapping
{
185 pub fn file_exists(&self, path
: &Path
) -> bool
{
186 self.file_loader
.file_exists(path
)
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
))
195 /// Loads source file as a binary blob.
197 /// Unlike `load_file`, guarantees that no normalization like BOM-removal
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
)?
;
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
209 let text
= std
::str::from_utf8(&bytes
).unwrap_or("").to_string();
210 self.new_source_file(path
.to_owned().into(), text
);
214 pub fn files(&self) -> MappedLockGuard
<'_
, Vec
<Lrc
<SourceFile
>>> {
215 LockGuard
::map(self.files
.borrow(), |files
| &mut files
.source_files
)
218 pub fn source_file_by_stable_id(
220 stable_id
: StableSourceFileId
,
221 ) -> Option
<Lrc
<SourceFile
>> {
222 self.files
.borrow().stable_id_to_source_file
.get(&stable_id
).cloned()
225 fn allocate_address_space(&self, size
: usize) -> Result
<usize, OffsetOverflowError
> {
226 let size
= u32::try_from(size
).map_err(|_
| OffsetOverflowError
)?
;
229 let current
= self.used_address_space
.load(Ordering
::Relaxed
);
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
)?
;
239 .compare_exchange(current
, next
, Ordering
::Relaxed
, Ordering
::Relaxed
)
242 return Ok(usize::try_from(current
).unwrap());
247 /// Creates a new `SourceFile`.
248 /// If a file already exists in the `SourceMap` with the same ID, that file is returned
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()
257 fn try_new_source_file(
259 mut filename
: FileName
,
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();
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
,
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;
283 was_remapped
= false;
287 StableSourceFileId
::new_from_pieces(&filename
, was_remapped
, Some(&unmapped_path
));
289 let lrc_sf
= match self.source_file_by_stable_id(file_id
) {
290 Some(lrc_sf
) => lrc_sf
,
292 let start_pos
= self.allocate_address_space(src
.len())?
;
294 let source_file
= Lrc
::new(SourceFile
::new(
299 Pos
::from_usize(start_pos
),
303 let mut files
= self.files
.borrow_mut();
305 files
.source_files
.push(source_file
.clone());
306 files
.stable_id_to_source_file
.insert(file_id
, source_file
.clone());
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(
321 name_was_remapped
: bool
,
322 src_hash
: SourceFileHash
,
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
> {
334 .allocate_address_space(source_len
)
335 .expect("not enough address space for imported source file");
337 let end_pos
= Pos
::from_usize(start_pos
+ source_len
);
338 let start_pos
= Pos
::from_usize(start_pos
);
340 for pos
in &mut file_local_lines
{
341 *pos
= *pos
+ start_pos
;
344 for mbc
in &mut file_local_multibyte_chars
{
345 mbc
.pos
= mbc
.pos
+ start_pos
;
348 for swc
in &mut file_local_non_narrow_chars
{
349 *swc
= *swc
+ start_pos
;
352 for nc
in &mut file_local_normalized_pos
{
353 nc
.pos
= nc
.pos
+ start_pos
;
356 let source_file
= Lrc
::new(SourceFile
{
362 external_src
: Lock
::new(ExternalSource
::Foreign
{
363 kind
: ExternalSourceKind
::AbsentOk
,
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
,
377 let mut files
= self.files
.borrow_mut();
379 files
.source_files
.push(source_file
.clone());
381 .stable_id_to_source_file
382 .insert(StableSourceFileId
::new(&source_file
), source_file
.clone());
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)
392 // If there is a doctest offset, applies it to the line.
393 pub fn doctest_offset_line(&self, file
: &FileName
, orig
: usize) -> usize {
395 FileName
::DocTest(_
, offset
) => {
397 orig
- (-(*offset
)) as usize
399 orig
+ *offset
as usize
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
;
417 let start_width_idx
= f
419 .binary_search_by_key(&linebpos
, |x
| x
.pos())
420 .unwrap_or_else(|x
| x
);
421 let end_width_idx
= f
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
]
430 col
.0 - special_chars
+ non_narrow
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 }
440 let end_width_idx
= f
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
448 Loc { file: f, line: 0, col: chpos, col_display }
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
);
457 let f
= (*self.files
.borrow().source_files
)[idx
].clone();
459 match f
.lookup_line(pos
) {
460 Some(line
) => Ok(SourceFileAndLine { sf: f, line }
),
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,
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() {
478 let lhs_end
= match self.lookup_line(sp_lhs
.hi()) {
480 Err(_
) => return None
,
482 let rhs_begin
= match self.lookup_line(sp_rhs
.lo()) {
484 Err(_
) => return None
,
487 // If we must cross lines to merge, don't merge.
488 if lhs_end
.line
!= rhs_begin
.line
{
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
))
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();
505 let lo
= self.lookup_char_pos(sp
.lo());
506 let hi
= self.lookup_char_pos(sp
.hi());
511 lo
.col
.to_usize() + 1,
513 hi
.col
.to_usize() + 1,
517 pub fn span_to_filename(&self, sp
: Span
) -> FileName
{
518 self.lookup_char_pos(sp
.lo()).file
.name
.clone()
521 pub fn span_to_unmapped_path(&self, sp
: Span
) -> FileName
{
522 self.lookup_char_pos(sp
.lo())
526 .expect("`SourceMap::span_to_unmapped_path` called for imported `SourceFile`?")
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());
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
),
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),
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
);
562 return Ok(FileLines { file: lo.file, lines: Vec::new() }
);
565 let mut lines
= Vec
::with_capacity(hi
.line
- lo
.line
+ 1);
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
;
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
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);
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 }
);
588 Ok(FileLines { file: lo.file, lines }
)
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
>
596 F
: Fn(&str, usize, usize) -> Result
<String
, SpanSnippetError
>,
598 let local_begin
= self.lookup_byte_offset(sp
.lo());
599 let local_end
= self.lookup_byte_offset(sp
.hi());
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
),
607 self.ensure_source_file_source_present(local_begin
.sf
.clone());
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();
613 if start_index
> end_index
|| end_index
> source_len
{
614 return Err(SpanSnippetError
::MalformedForSourcemap(MalformedSourceMapPositions
{
615 name
: local_begin
.sf
.name
.clone(),
617 begin_pos
: local_begin
.pos
,
618 end_pos
: local_end
.pos
,
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
)
627 Err(SpanSnippetError
::SourceNotAvailable { filename: local_begin.sf.name.clone() }
)
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
))
641 pub fn span_to_margin(&self, sp
: Span
) -> Option
<usize> {
642 match self.span_to_prev_source(sp
) {
647 .map(|last_line
| last_line
.len() - last_line
.trim_start().len()),
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
))
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));
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
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));
694 /// Given a `Span`, tries to get a shorter span ending before the first occurrence of `char`
696 pub fn span_until_char(&self, sp
: Span
, c
: char) -> Span
{
697 match self.span_to_snippet(sp
) {
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))
710 /// Given a `Span`, tries to get a shorter span ending just after the first occurrence of `char`
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));
721 /// Given a `Span`, gets a new `Span` covering the first token and all its trailing whitespace
722 /// or the original `Span`.
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;
728 self.span_take_while(sp
, |c
| {
729 if !whitespace_found
&& c
.is_whitespace() {
730 whitespace_found
= true;
733 !whitespace_found
|| c
.is_whitespace()
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.
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())
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
748 P
: for<'r
> FnMut(&'r
char) -> bool
,
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>();
753 sp
.with_hi(BytePos(sp
.lo().0 + (offset
as u32)))
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.
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
, '
{'
)
770 /// Returns a new span representing just the start point of this span.
771 pub fn start_point(&self, sp
: Span
) -> Span
{
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
)
779 /// Returns a new span representing just the end point of this span.
780 pub fn end_point(&self, sp
: Span
) -> Span
{
783 let width
= self.find_width_of_character_at_span(sp
, false);
784 let corrected_end_position
= pos
.checked_sub(width
).unwrap_or(pos
);
786 let end_point
= BytePos(cmp
::max(corrected_end_position
, sp
.lo().0));
787 sp
.with_lo(end_point
)
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;
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
);
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())
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 {
809 debug
!("find_width_of_character_at_span: early return empty span");
813 let local_begin
= self.lookup_byte_offset(sp
.lo
);
814 let local_end
= self.lookup_byte_offset(sp
.hi
);
816 "find_width_of_character_at_span: local_begin=`{:?}`, local_end=`{:?}`",
817 local_begin
, local_end
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");
825 let start_index
= local_begin
.pos
.to_usize();
826 let end_index
= local_end
.pos
.to_usize();
828 "find_width_of_character_at_span: start_index=`{:?}`, end_index=`{:?}`",
829 start_index
, end_index
832 // Disregard indexes that are at the start or end of their spans, they can't fit bigger
834 if (!forwards
&& end_index
== usize::min_value())
835 || (forwards
&& start_index
== usize::max_value())
837 debug
!("find_width_of_character_at_span: start or end of span, cannot be multibyte");
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");
849 let src
= local_begin
.sf
.external_src
.borrow();
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
{
855 &src
[start_index
..len
]
856 } else if let Some(src
) = src
.get_source() {
858 &src
[start_index
..len
]
862 debug
!("find_width_of_character_at_span: snippet=`{:?}`", snippet
);
864 let mut target
= if forwards { end_index + 1 }
else { end_index - 1 }
;
865 debug
!("find_width_of_character_at_span: initial target=`{:?}`", target
);
867 while !snippet
.is_char_boundary(target
- start_index
) && target
< source_len
{
868 target
= if forwards
{
871 match target
.checked_sub(1) {
872 Some(target
) => target
,
878 debug
!("find_width_of_character_at_span: target=`{:?}`", target
);
880 debug
!("find_width_of_character_at_span: final target=`{:?}`", target
);
882 if forwards { (target - end_index) as u32 }
else { (end_index - target) as u32 }
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());
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 }
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
];
907 // The number of extra bytes due to multibyte chars in the `SourceFile`.
908 let mut total_extra_bytes
= 0;
910 for mbc
in map
.multibyte_chars
.iter() {
911 debug
!("{}-byte char at {:?}", mbc
.bytes
, mbc
.pos
);
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
918 assert
!(bpos
.to_u32() >= mbc
.pos
.to_u32() + mbc
.bytes
as u32);
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)
928 // Returns the index of the `SourceFile` (in `self.files`) that contains `pos`.
929 pub fn lookup_source_file_idx(&self, pos
: BytePos
) -> usize {
933 .binary_search_by_key(&pos
, |key
| key
.start_pos
)
934 .unwrap_or_else(|p
| p
- 1)
937 pub fn count_lines(&self) -> usize {
938 self.files().iter().fold(0, |a
, f
| a
+ f
.count_lines())
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
)
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))
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.
958 /// ```rust,ignore (pseudo-Rust)
960 /// fn my_function(param: T)
961 /// // ^ Original span
964 /// fn my_function(param: T)
965 /// // ^^^^^^^^^^^ Generated span with snippet `my_function<T>`
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
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");
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() {
987 '
<'
=> bracket_counter
+= 1,
988 '
>'
=> bracket_counter
-= 1,
990 if bracket_counter
== 0 {
996 offset
+= c
.len_utf8();
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));
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())])
1008 format
!("{}<", &snippet
[..offset
])
1011 .push_str(&self.span_to_snippet(span
).unwrap_or_else(|_
| "T".to_string()));
1012 new_snippet
.push('
>'
);
1014 return Some((sugg_span
, new_snippet
));
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(),
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()
1035 pub struct FilePathMapping
{
1036 mapping
: Vec
<(PathBuf
, PathBuf
)>,
1039 impl FilePathMapping
{
1040 pub fn empty() -> FilePathMapping
{
1041 FilePathMapping { mapping: vec![] }
1044 pub fn new(mapping
: Vec
<(PathBuf
, PathBuf
)>) -> FilePathMapping
{
1045 FilePathMapping { mapping }
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
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);