1use std::fs::File;
13use std::io::{self, BorrowedBuf, Read};
14use std::{fs, path};
15
16use rustc_data_structures::sync::{IntoDynSyncSend, MappedReadGuard, ReadGuard, RwLock};
17use rustc_data_structures::unhash::UnhashMap;
18use rustc_macros::{Decodable, Encodable};
19use tracing::{debug, instrument, trace};
20
21use crate::*;
22
23#[cfg(test)]
24mod tests;
25
26pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span {
30 let ctxt = sp.ctxt();
31 if ctxt.is_root() {
32 return sp;
33 }
34
35 let enclosing_ctxt = enclosing_sp.ctxt();
36 let expn_data1 = ctxt.outer_expn_data();
37 if !enclosing_ctxt.is_root()
38 && expn_data1.call_site == enclosing_ctxt.outer_expn_data().call_site
39 {
40 sp
41 } else {
42 original_sp(expn_data1.call_site, enclosing_sp)
43 }
44}
45
46mod monotonic {
47 use std::ops::{Deref, DerefMut};
48
49 pub struct MonotonicVec<T>(Vec<T>);
55 impl<T> MonotonicVec<T> {
56 pub(super) fn push(&mut self, val: T) {
57 self.0.push(val);
58 }
59 }
60
61 impl<T> Default for MonotonicVec<T> {
62 fn default() -> Self {
63 MonotonicVec(vec![])
64 }
65 }
66
67 impl<T> Deref for MonotonicVec<T> {
68 type Target = Vec<T>;
69 fn deref(&self) -> &Self::Target {
70 &self.0
71 }
72 }
73
74 impl<T> !DerefMut for MonotonicVec<T> {}
75}
76
77#[derive(Clone, Encodable, Decodable, Debug, Copy, PartialEq, Hash, HashStable_Generic)]
78pub struct Spanned<T> {
79 pub node: T,
80 pub span: Span,
81}
82
83pub fn respan<T>(sp: Span, t: T) -> Spanned<T> {
84 Spanned { node: t, span: sp }
85}
86
87pub fn dummy_spanned<T>(t: T) -> Spanned<T> {
88 respan(DUMMY_SP, t)
89}
90
91pub trait FileLoader {
97 fn file_exists(&self, path: &Path) -> bool;
99
100 fn read_file(&self, path: &Path) -> io::Result<String>;
104
105 fn read_binary_file(&self, path: &Path) -> io::Result<Arc<[u8]>>;
108}
109
110pub struct RealFileLoader;
112
113impl FileLoader for RealFileLoader {
114 fn file_exists(&self, path: &Path) -> bool {
115 path.exists()
116 }
117
118 fn read_file(&self, path: &Path) -> io::Result<String> {
119 let mut file = File::open(path)?;
120 let size = file.metadata().map(|metadata| metadata.len()).ok().unwrap_or(0);
121
122 if size > SourceFile::MAX_FILE_SIZE.into() {
123 return Err(io::Error::other(format!(
124 "text files larger than {} bytes are unsupported",
125 SourceFile::MAX_FILE_SIZE
126 )));
127 }
128 let mut contents = String::new();
129 file.read_to_string(&mut contents)?;
130 Ok(contents)
131 }
132
133 fn read_binary_file(&self, path: &Path) -> io::Result<Arc<[u8]>> {
134 let mut file = fs::File::open(path)?;
135 let len = file.metadata()?.len();
136
137 let mut bytes = Arc::new_uninit_slice(len as usize);
138 let mut buf = BorrowedBuf::from(Arc::get_mut(&mut bytes).unwrap());
139 match file.read_buf_exact(buf.unfilled()) {
140 Ok(()) => {}
141 Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => {
142 drop(bytes);
143 return fs::read(path).map(Vec::into);
144 }
145 Err(e) => return Err(e),
146 }
147 let bytes = unsafe { bytes.assume_init() };
150
151 let mut probe = [0u8; 32];
161 let n = loop {
162 match file.read(&mut probe) {
163 Ok(0) => return Ok(bytes),
164 Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
165 Err(e) => return Err(e),
166 Ok(n) => break n,
167 }
168 };
169 let mut bytes: Vec<u8> = bytes.iter().copied().chain(probe[..n].iter().copied()).collect();
170 file.read_to_end(&mut bytes)?;
171 Ok(bytes.into())
172 }
173}
174
175#[derive(Default)]
180struct SourceMapFiles {
181 source_files: monotonic::MonotonicVec<Arc<SourceFile>>,
182 stable_id_to_source_file: UnhashMap<StableSourceFileId, Arc<SourceFile>>,
183}
184
185pub struct SourceMapInputs {
187 pub file_loader: Box<dyn FileLoader + Send + Sync>,
188 pub path_mapping: FilePathMapping,
189 pub hash_kind: SourceFileHashAlgorithm,
190 pub checksum_hash_kind: Option<SourceFileHashAlgorithm>,
191}
192
193pub struct SourceMap {
194 files: RwLock<SourceMapFiles>,
195 file_loader: IntoDynSyncSend<Box<dyn FileLoader + Sync + Send>>,
196
197 path_mapping: FilePathMapping,
200
201 hash_kind: SourceFileHashAlgorithm,
203
204 checksum_hash_kind: Option<SourceFileHashAlgorithm>,
209}
210
211impl SourceMap {
212 pub fn new(path_mapping: FilePathMapping) -> SourceMap {
213 Self::with_inputs(SourceMapInputs {
214 file_loader: Box::new(RealFileLoader),
215 path_mapping,
216 hash_kind: SourceFileHashAlgorithm::Md5,
217 checksum_hash_kind: None,
218 })
219 }
220
221 pub fn with_inputs(
222 SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind }: SourceMapInputs,
223 ) -> SourceMap {
224 SourceMap {
225 files: Default::default(),
226 file_loader: IntoDynSyncSend(file_loader),
227 path_mapping,
228 hash_kind,
229 checksum_hash_kind,
230 }
231 }
232
233 pub fn path_mapping(&self) -> &FilePathMapping {
234 &self.path_mapping
235 }
236
237 pub fn file_exists(&self, path: &Path) -> bool {
238 self.file_loader.file_exists(path)
239 }
240
241 pub fn load_file(&self, path: &Path) -> io::Result<Arc<SourceFile>> {
242 let src = self.file_loader.read_file(path)?;
243 let filename = path.to_owned().into();
244 Ok(self.new_source_file(filename, src))
245 }
246
247 pub fn load_binary_file(&self, path: &Path) -> io::Result<(Arc<[u8]>, Span)> {
252 let bytes = self.file_loader.read_binary_file(path)?;
253
254 let text = std::str::from_utf8(&bytes).unwrap_or("").to_string();
260 let file = self.new_source_file(path.to_owned().into(), text);
261 Ok((
262 bytes,
263 Span::new(
264 file.start_pos,
265 BytePos(file.start_pos.0 + file.normalized_source_len.0),
266 SyntaxContext::root(),
267 None,
268 ),
269 ))
270 }
271
272 pub fn files(&self) -> MappedReadGuard<'_, monotonic::MonotonicVec<Arc<SourceFile>>> {
275 ReadGuard::map(self.files.borrow(), |files| &files.source_files)
276 }
277
278 pub fn source_file_by_stable_id(
279 &self,
280 stable_id: StableSourceFileId,
281 ) -> Option<Arc<SourceFile>> {
282 self.files.borrow().stable_id_to_source_file.get(&stable_id).cloned()
283 }
284
285 fn register_source_file(
286 &self,
287 file_id: StableSourceFileId,
288 mut file: SourceFile,
289 ) -> Result<Arc<SourceFile>, OffsetOverflowError> {
290 let mut files = self.files.borrow_mut();
291
292 file.start_pos = BytePos(if let Some(last_file) = files.source_files.last() {
293 last_file.end_position().0.checked_add(1).ok_or(OffsetOverflowError)?
296 } else {
297 0
298 });
299
300 let file = Arc::new(file);
301 files.source_files.push(Arc::clone(&file));
302 files.stable_id_to_source_file.insert(file_id, Arc::clone(&file));
303
304 Ok(file)
305 }
306
307 pub fn new_source_file(&self, filename: FileName, src: String) -> Arc<SourceFile> {
311 self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| {
312 eprintln!(
313 "fatal error: rustc does not support text files larger than {} bytes",
314 SourceFile::MAX_FILE_SIZE
315 );
316 crate::fatal_error::FatalError.raise()
317 })
318 }
319
320 fn try_new_source_file(
321 &self,
322 filename: FileName,
323 src: String,
324 ) -> Result<Arc<SourceFile>, OffsetOverflowError> {
325 let (filename, _) = self.path_mapping.map_filename_prefix(&filename);
329
330 let stable_id = StableSourceFileId::from_filename_in_current_crate(&filename);
331 match self.source_file_by_stable_id(stable_id) {
332 Some(lrc_sf) => Ok(lrc_sf),
333 None => {
334 let source_file =
335 SourceFile::new(filename, src, self.hash_kind, self.checksum_hash_kind)?;
336
337 debug_assert_eq!(source_file.stable_id, stable_id);
340
341 self.register_source_file(stable_id, source_file)
342 }
343 }
344 }
345
346 pub fn new_imported_source_file(
351 &self,
352 filename: FileName,
353 src_hash: SourceFileHash,
354 checksum_hash: Option<SourceFileHash>,
355 stable_id: StableSourceFileId,
356 normalized_source_len: u32,
357 unnormalized_source_len: u32,
358 cnum: CrateNum,
359 file_local_lines: FreezeLock<SourceFileLines>,
360 multibyte_chars: Vec<MultiByteChar>,
361 normalized_pos: Vec<NormalizedPos>,
362 metadata_index: u32,
363 ) -> Arc<SourceFile> {
364 let normalized_source_len = RelativeBytePos::from_u32(normalized_source_len);
365
366 let source_file = SourceFile {
367 name: filename,
368 src: None,
369 src_hash,
370 checksum_hash,
371 external_src: FreezeLock::new(ExternalSource::Foreign {
372 kind: ExternalSourceKind::AbsentOk,
373 metadata_index,
374 }),
375 start_pos: BytePos(0),
376 normalized_source_len,
377 unnormalized_source_len,
378 lines: file_local_lines,
379 multibyte_chars,
380 normalized_pos,
381 stable_id,
382 cnum,
383 };
384
385 self.register_source_file(stable_id, source_file)
386 .expect("not enough address space for imported source file")
387 }
388
389 pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize {
391 match file {
392 FileName::DocTest(_, offset) => {
393 if *offset < 0 {
394 orig - (-(*offset)) as usize
395 } else {
396 orig + *offset as usize
397 }
398 }
399 _ => orig,
400 }
401 }
402
403 pub fn lookup_source_file(&self, pos: BytePos) -> Arc<SourceFile> {
405 let idx = self.lookup_source_file_idx(pos);
406 Arc::clone(&(*self.files.borrow().source_files)[idx])
407 }
408
409 pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
411 let sf = self.lookup_source_file(pos);
412 let (line, col, col_display) = sf.lookup_file_pos_with_col_display(pos);
413 Loc { file: sf, line, col, col_display }
414 }
415
416 pub fn lookup_line(&self, pos: BytePos) -> Result<SourceFileAndLine, Arc<SourceFile>> {
418 let f = self.lookup_source_file(pos);
419
420 let pos = f.relative_position(pos);
421 match f.lookup_line(pos) {
422 Some(line) => Ok(SourceFileAndLine { sf: f, line }),
423 None => Err(f),
424 }
425 }
426
427 pub fn span_to_string(
428 &self,
429 sp: Span,
430 filename_display_pref: FileNameDisplayPreference,
431 ) -> String {
432 let (source_file, lo_line, lo_col, hi_line, hi_col) = self.span_to_location_info(sp);
433
434 let file_name = match source_file {
435 Some(sf) => sf.name.display(filename_display_pref).to_string(),
436 None => return "no-location".to_string(),
437 };
438
439 format!(
440 "{file_name}:{lo_line}:{lo_col}{}",
441 if let FileNameDisplayPreference::Short = filename_display_pref {
442 String::new()
443 } else {
444 format!(": {hi_line}:{hi_col}")
445 }
446 )
447 }
448
449 pub fn span_to_location_info(
450 &self,
451 sp: Span,
452 ) -> (Option<Arc<SourceFile>>, usize, usize, usize, usize) {
453 if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
454 return (None, 0, 0, 0, 0);
455 }
456
457 let lo = self.lookup_char_pos(sp.lo());
458 let hi = self.lookup_char_pos(sp.hi());
459 (Some(lo.file), lo.line, lo.col.to_usize() + 1, hi.line, hi.col.to_usize() + 1)
460 }
461
462 pub fn span_to_embeddable_string(&self, sp: Span) -> String {
464 self.span_to_string(sp, FileNameDisplayPreference::Remapped)
465 }
466
467 pub fn span_to_diagnostic_string(&self, sp: Span) -> String {
471 self.span_to_string(sp, self.path_mapping.filename_display_for_diagnostics)
472 }
473
474 pub fn span_to_filename(&self, sp: Span) -> FileName {
475 self.lookup_char_pos(sp.lo()).file.name.clone()
476 }
477
478 pub fn filename_for_diagnostics<'a>(&self, filename: &'a FileName) -> FileNameDisplay<'a> {
479 filename.display(self.path_mapping.filename_display_for_diagnostics)
480 }
481
482 pub fn is_multiline(&self, sp: Span) -> bool {
483 let lo = self.lookup_source_file_idx(sp.lo());
484 let hi = self.lookup_source_file_idx(sp.hi());
485 if lo != hi {
486 return true;
487 }
488 let f = Arc::clone(&(*self.files.borrow().source_files)[lo]);
489 let lo = f.relative_position(sp.lo());
490 let hi = f.relative_position(sp.hi());
491 f.lookup_line(lo) != f.lookup_line(hi)
492 }
493
494 #[instrument(skip(self), level = "trace")]
495 pub fn is_valid_span(&self, sp: Span) -> Result<(Loc, Loc), SpanLinesError> {
496 let lo = self.lookup_char_pos(sp.lo());
497 trace!(?lo);
498 let hi = self.lookup_char_pos(sp.hi());
499 trace!(?hi);
500 if lo.file.start_pos != hi.file.start_pos {
501 return Err(SpanLinesError::DistinctSources(Box::new(DistinctSources {
502 begin: (lo.file.name.clone(), lo.file.start_pos),
503 end: (hi.file.name.clone(), hi.file.start_pos),
504 })));
505 }
506 Ok((lo, hi))
507 }
508
509 pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
510 match self.span_to_prev_source(sp) {
511 Ok(s) => s.rsplit_once('\n').unwrap_or(("", &s)).1.trim_start().is_empty(),
512 Err(_) => false,
513 }
514 }
515
516 pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
517 debug!("span_to_lines(sp={:?})", sp);
518 let (lo, hi) = self.is_valid_span(sp)?;
519 assert!(hi.line >= lo.line);
520
521 if sp.is_dummy() {
522 return Ok(FileLines { file: lo.file, lines: Vec::new() });
523 }
524
525 let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
526
527 let mut start_col = lo.col;
530
531 let hi_line = hi.line.saturating_sub(1);
539 for line_index in lo.line.saturating_sub(1)..hi_line {
540 let line_len = lo.file.get_line(line_index).map_or(0, |s| s.chars().count());
541 lines.push(LineInfo { line_index, start_col, end_col: CharPos::from_usize(line_len) });
542 start_col = CharPos::from_usize(0);
543 }
544
545 lines.push(LineInfo { line_index: hi_line, start_col, end_col: hi.col });
547
548 Ok(FileLines { file: lo.file, lines })
549 }
550
551 pub fn span_to_source<F, T>(&self, sp: Span, extract_source: F) -> Result<T, SpanSnippetError>
555 where
556 F: Fn(&str, usize, usize) -> Result<T, SpanSnippetError>,
557 {
558 let local_begin = self.lookup_byte_offset(sp.lo());
559 let local_end = self.lookup_byte_offset(sp.hi());
560
561 if local_begin.sf.start_pos != local_end.sf.start_pos {
562 Err(SpanSnippetError::DistinctSources(Box::new(DistinctSources {
563 begin: (local_begin.sf.name.clone(), local_begin.sf.start_pos),
564 end: (local_end.sf.name.clone(), local_end.sf.start_pos),
565 })))
566 } else {
567 self.ensure_source_file_source_present(&local_begin.sf);
568
569 let start_index = local_begin.pos.to_usize();
570 let end_index = local_end.pos.to_usize();
571 let source_len = local_begin.sf.normalized_source_len.to_usize();
572
573 if start_index > end_index || end_index > source_len {
574 return Err(SpanSnippetError::MalformedForSourcemap(MalformedSourceMapPositions {
575 name: local_begin.sf.name.clone(),
576 source_len,
577 begin_pos: local_begin.pos,
578 end_pos: local_end.pos,
579 }));
580 }
581
582 if let Some(ref src) = local_begin.sf.src {
583 extract_source(src, start_index, end_index)
584 } else if let Some(src) = local_begin.sf.external_src.read().get_source() {
585 extract_source(src, start_index, end_index)
586 } else {
587 Err(SpanSnippetError::SourceNotAvailable { filename: local_begin.sf.name.clone() })
588 }
589 }
590 }
591
592 pub fn is_span_accessible(&self, sp: Span) -> bool {
593 self.span_to_source(sp, |src, start_index, end_index| {
594 Ok(src.get(start_index..end_index).is_some())
595 })
596 .is_ok_and(|is_accessible| is_accessible)
597 }
598
599 pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
601 self.span_to_source(sp, |src, start_index, end_index| {
602 src.get(start_index..end_index)
603 .map(|s| s.to_string())
604 .ok_or(SpanSnippetError::IllFormedSpan(sp))
605 })
606 }
607
608 pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
609 Some(self.indentation_before(sp)?.len())
610 }
611
612 pub fn indentation_before(&self, sp: Span) -> Option<String> {
613 self.span_to_source(sp, |src, start_index, _| {
614 let before = &src[..start_index];
615 let last_line = before.rsplit_once('\n').map_or(before, |(_, last)| last);
616 Ok(last_line
617 .split_once(|c: char| !c.is_whitespace())
618 .map_or(last_line, |(indent, _)| indent)
619 .to_string())
620 })
621 .ok()
622 }
623
624 pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
626 self.span_to_source(sp, |src, start_index, _| {
627 src.get(..start_index).map(|s| s.to_string()).ok_or(SpanSnippetError::IllFormedSpan(sp))
628 })
629 }
630
631 pub fn span_extend_to_prev_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
634 if let Ok(prev_source) = self.span_to_prev_source(sp) {
635 let prev_source = prev_source.rsplit(c).next().unwrap_or("");
636 if !prev_source.is_empty() && (accept_newlines || !prev_source.contains('\n')) {
637 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
638 }
639 }
640
641 sp
642 }
643
644 pub fn span_extend_to_prev_char_before(
647 &self,
648 sp: Span,
649 c: char,
650 accept_newlines: bool,
651 ) -> Span {
652 if let Ok(prev_source) = self.span_to_prev_source(sp) {
653 let prev_source = prev_source.rsplit(c).next().unwrap_or("");
654 if accept_newlines || !prev_source.contains('\n') {
655 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32 - 1_u32));
656 }
657 }
658
659 sp
660 }
661
662 pub fn span_extend_to_prev_str(
666 &self,
667 sp: Span,
668 pat: &str,
669 accept_newlines: bool,
670 include_whitespace: bool,
671 ) -> Option<Span> {
672 let prev_source = self.span_to_prev_source(sp).ok()?;
677 for ws in &[" ", "\t", "\n"] {
678 let pat = pat.to_owned() + ws;
679 if let Some(pat_pos) = prev_source.rfind(&pat) {
680 let just_after_pat_pos = pat_pos + pat.len() - 1;
681 let just_after_pat_plus_ws = if include_whitespace {
682 just_after_pat_pos
683 + prev_source[just_after_pat_pos..]
684 .find(|c: char| !c.is_whitespace())
685 .unwrap_or(0)
686 } else {
687 just_after_pat_pos
688 };
689 let len = prev_source.len() - just_after_pat_plus_ws;
690 let prev_source = &prev_source[just_after_pat_plus_ws..];
691 if accept_newlines || !prev_source.trim_start().contains('\n') {
692 return Some(sp.with_lo(BytePos(sp.lo().0 - len as u32)));
693 }
694 }
695 }
696
697 None
698 }
699
700 pub fn span_to_next_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
702 self.span_to_source(sp, |src, _, end_index| {
703 src.get(end_index..).map(|s| s.to_string()).ok_or(SpanSnippetError::IllFormedSpan(sp))
704 })
705 }
706
707 pub fn span_extend_while(
709 &self,
710 span: Span,
711 f: impl Fn(char) -> bool,
712 ) -> Result<Span, SpanSnippetError> {
713 self.span_to_source(span, |s, _start, end| {
714 let n = s[end..].char_indices().find(|&(_, c)| !f(c)).map_or(s.len() - end, |(i, _)| i);
715 Ok(span.with_hi(span.hi() + BytePos(n as u32)))
716 })
717 }
718
719 pub fn span_extend_while_whitespace(&self, span: Span) -> Span {
722 self.span_extend_while(span, char::is_whitespace).unwrap_or(span)
723 }
724
725 pub fn span_extend_prev_while(
727 &self,
728 span: Span,
729 f: impl Fn(char) -> bool,
730 ) -> Result<Span, SpanSnippetError> {
731 self.span_to_source(span, |s, start, _end| {
732 let n = s[..start]
733 .char_indices()
734 .rfind(|&(_, c)| !f(c))
735 .map_or(start, |(i, _)| start - i - 1);
736 Ok(span.with_lo(span.lo() - BytePos(n as u32)))
737 })
738 }
739
740 pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
742 if let Ok(next_source) = self.span_to_next_source(sp) {
743 let next_source = next_source.split(c).next().unwrap_or("");
744 if !next_source.is_empty() && (accept_newlines || !next_source.contains('\n')) {
745 return sp.with_hi(BytePos(sp.hi().0 + next_source.len() as u32));
746 }
747 }
748
749 sp
750 }
751
752 pub fn span_extend_to_line(&self, sp: Span) -> Span {
754 self.span_extend_to_prev_char(self.span_extend_to_next_char(sp, '\n', true), '\n', true)
755 }
756
757 pub fn span_until_char(&self, sp: Span, c: char) -> Span {
760 match self.span_to_snippet(sp) {
761 Ok(snippet) => {
762 let snippet = snippet.split(c).next().unwrap_or("").trim_end();
763 if !snippet.is_empty() && !snippet.contains('\n') {
764 sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
765 } else {
766 sp
767 }
768 }
769 _ => sp,
770 }
771 }
772
773 pub fn span_wrapped_by_angle_or_parentheses(&self, span: Span) -> bool {
778 self.span_to_source(span, |src, start_index, end_index| {
779 if src.get(start_index..end_index).is_none() {
780 return Ok(false);
781 }
782 let end_src = &src[end_index..];
784 let mut i = 0;
785 let mut found_right_parentheses = false;
786 let mut found_right_angle = false;
787 while let Some(cc) = end_src.chars().nth(i) {
788 if cc == ' ' {
789 i = i + 1;
790 } else if cc == '>' {
791 found_right_angle = true;
793 break;
794 } else if cc == ')' {
795 found_right_parentheses = true;
796 break;
797 } else {
798 return Ok(false);
800 }
801 }
802 i = start_index;
804 let start_src = &src[0..start_index];
805 while let Some(cc) = start_src.chars().nth(i) {
806 if cc == ' ' {
807 if i == 0 {
808 return Ok(false);
809 }
810 i = i - 1;
811 } else if cc == '<' {
812 if !found_right_angle {
814 return Ok(false);
816 }
817 break;
818 } else if cc == '(' {
819 if !found_right_parentheses {
820 return Ok(false);
822 }
823 break;
824 } else {
825 return Ok(false);
827 }
828 }
829 Ok(true)
830 })
831 .is_ok_and(|is_accessible| is_accessible)
832 }
833
834 pub fn span_through_char(&self, sp: Span, c: char) -> Span {
837 if let Ok(snippet) = self.span_to_snippet(sp)
838 && let Some(offset) = snippet.find(c)
839 {
840 return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32));
841 }
842 sp
843 }
844
845 pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
850 let mut whitespace_found = false;
851
852 self.span_take_while(sp, |c| {
853 if !whitespace_found && c.is_whitespace() {
854 whitespace_found = true;
855 }
856
857 !whitespace_found || c.is_whitespace()
858 })
859 }
860
861 pub fn span_until_whitespace(&self, sp: Span) -> Span {
866 self.span_take_while(sp, |c| !c.is_whitespace())
867 }
868
869 pub fn span_take_while<P>(&self, sp: Span, predicate: P) -> Span
871 where
872 P: for<'r> FnMut(&'r char) -> bool,
873 {
874 if let Ok(snippet) = self.span_to_snippet(sp) {
875 let offset = snippet.chars().take_while(predicate).map(|c| c.len_utf8()).sum::<usize>();
876
877 sp.with_hi(BytePos(sp.lo().0 + (offset as u32)))
878 } else {
879 sp
880 }
881 }
882
883 pub fn guess_head_span(&self, sp: Span) -> Span {
889 self.span_until_char(sp, '{')
892 }
893
894 pub fn start_point(&self, sp: Span) -> Span {
896 let width = {
897 let sp = sp.data();
898 let local_begin = self.lookup_byte_offset(sp.lo);
899 let start_index = local_begin.pos.to_usize();
900 let src = local_begin.sf.external_src.read();
901
902 let snippet = if let Some(ref src) = local_begin.sf.src {
903 Some(&src[start_index..])
904 } else {
905 src.get_source().map(|src| &src[start_index..])
906 };
907
908 match snippet {
909 None => 1,
910 Some(snippet) => match snippet.chars().next() {
911 None => 1,
912 Some(c) => c.len_utf8(),
913 },
914 }
915 };
916
917 sp.with_hi(BytePos(sp.lo().0 + width as u32))
918 }
919
920 pub fn end_point(&self, sp: Span) -> Span {
922 let sp = sp.data();
923 let pos = sp.hi.0;
924
925 let width = self.find_width_of_character_at_span(sp, false);
926 let corrected_end_position = pos.checked_sub(width).unwrap_or(pos);
927
928 let end_point = BytePos(cmp::max(corrected_end_position, sp.lo.0));
929 sp.with_lo(end_point)
930 }
931
932 pub fn next_point(&self, sp: Span) -> Span {
939 if sp.is_dummy() {
940 return sp;
941 }
942
943 let sp = sp.data();
944 let start_of_next_point = sp.hi.0;
945 let width = self.find_width_of_character_at_span(sp, true);
946 let end_of_next_point =
950 start_of_next_point.checked_add(width).unwrap_or(start_of_next_point);
951
952 let end_of_next_point = BytePos(cmp::max(start_of_next_point + 1, end_of_next_point));
953 Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt, None)
954 }
955
956 pub fn span_look_ahead(&self, span: Span, expect: &str, limit: Option<usize>) -> Option<Span> {
958 let mut sp = span;
959 for _ in 0..limit.unwrap_or(100_usize) {
960 sp = self.next_point(sp);
961 if let Ok(ref snippet) = self.span_to_snippet(sp) {
962 if snippet == expect {
963 return Some(sp);
964 }
965 if snippet.chars().any(|c| !c.is_whitespace()) {
966 break;
967 }
968 }
969 }
970 None
971 }
972
973 #[instrument(skip(self, sp))]
976 fn find_width_of_character_at_span(&self, sp: SpanData, forwards: bool) -> u32 {
977 if sp.lo == sp.hi && !forwards {
978 debug!("early return empty span");
979 return 1;
980 }
981
982 let local_begin = self.lookup_byte_offset(sp.lo);
983 let local_end = self.lookup_byte_offset(sp.hi);
984 debug!("local_begin=`{:?}`, local_end=`{:?}`", local_begin, local_end);
985
986 if local_begin.sf.start_pos != local_end.sf.start_pos {
987 debug!("begin and end are in different files");
988 return 1;
989 }
990
991 let start_index = local_begin.pos.to_usize();
992 let end_index = local_end.pos.to_usize();
993 debug!("start_index=`{:?}`, end_index=`{:?}`", start_index, end_index);
994
995 if (!forwards && end_index == usize::MIN) || (forwards && start_index == usize::MAX) {
998 debug!("start or end of span, cannot be multibyte");
999 return 1;
1000 }
1001
1002 let source_len = local_begin.sf.normalized_source_len.to_usize();
1003 debug!("source_len=`{:?}`", source_len);
1004 if start_index > end_index || end_index > source_len - 1 {
1006 debug!("source indexes are malformed");
1007 return 1;
1008 }
1009
1010 let src = local_begin.sf.external_src.read();
1011
1012 let snippet = if let Some(src) = &local_begin.sf.src {
1013 src
1014 } else if let Some(src) = src.get_source() {
1015 src
1016 } else {
1017 return 1;
1018 };
1019
1020 if forwards {
1021 (snippet.ceil_char_boundary(end_index + 1) - end_index) as u32
1022 } else {
1023 (end_index - snippet.floor_char_boundary(end_index - 1)) as u32
1024 }
1025 }
1026
1027 pub fn get_source_file(&self, filename: &FileName) -> Option<Arc<SourceFile>> {
1028 let filename = self.path_mapping().map_filename_prefix(filename).0;
1030 for sf in self.files.borrow().source_files.iter() {
1031 if filename == sf.name {
1032 return Some(Arc::clone(&sf));
1033 }
1034 }
1035 None
1036 }
1037
1038 pub fn lookup_byte_offset(&self, bpos: BytePos) -> SourceFileAndBytePos {
1040 let idx = self.lookup_source_file_idx(bpos);
1041 let sf = Arc::clone(&(*self.files.borrow().source_files)[idx]);
1042 let offset = bpos - sf.start_pos;
1043 SourceFileAndBytePos { sf, pos: offset }
1044 }
1045
1046 pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
1050 self.files.borrow().source_files.partition_point(|x| x.start_pos <= pos) - 1
1051 }
1052
1053 pub fn count_lines(&self) -> usize {
1054 self.files().iter().fold(0, |a, f| a + f.count_lines())
1055 }
1056
1057 pub fn ensure_source_file_source_present(&self, source_file: &SourceFile) -> bool {
1058 source_file.add_external_src(|| {
1059 let FileName::Real(ref name) = source_file.name else {
1060 return None;
1061 };
1062
1063 let local_path: Cow<'_, Path> = match name {
1064 RealFileName::LocalPath(local_path) => local_path.into(),
1065 RealFileName::Remapped { local_path: Some(local_path), .. } => local_path.into(),
1066 RealFileName::Remapped { local_path: None, virtual_name } => {
1067 self.path_mapping.reverse_map_prefix_heuristically(virtual_name)?.into()
1073 }
1074 };
1075
1076 self.file_loader.read_file(&local_path).ok()
1077 })
1078 }
1079
1080 pub fn is_imported(&self, sp: Span) -> bool {
1081 let source_file_index = self.lookup_source_file_idx(sp.lo());
1082 let source_file = &self.files()[source_file_index];
1083 source_file.is_imported()
1084 }
1085
1086 pub fn stmt_span(&self, stmt_span: Span, block_span: Span) -> Span {
1090 if !stmt_span.from_expansion() {
1091 return stmt_span;
1092 }
1093 let mac_call = original_sp(stmt_span, block_span);
1094 self.mac_call_stmt_semi_span(mac_call).map_or(mac_call, |s| mac_call.with_hi(s.hi()))
1095 }
1096
1097 pub fn mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span> {
1105 let span = self.span_extend_while_whitespace(mac_call);
1106 let span = self.next_point(span);
1107 if self.span_to_snippet(span).as_deref() == Ok(";") { Some(span) } else { None }
1108 }
1109}
1110
1111pub fn get_source_map() -> Option<Arc<SourceMap>> {
1112 with_session_globals(|session_globals| session_globals.source_map.clone())
1113}
1114
1115#[derive(Clone)]
1116pub struct FilePathMapping {
1117 mapping: Vec<(PathBuf, PathBuf)>,
1118 filename_display_for_diagnostics: FileNameDisplayPreference,
1119 filename_embeddable_preference: FileNameEmbeddablePreference,
1120}
1121
1122impl FilePathMapping {
1123 pub fn empty() -> FilePathMapping {
1124 FilePathMapping::new(
1125 Vec::new(),
1126 FileNameDisplayPreference::Local,
1127 FileNameEmbeddablePreference::RemappedOnly,
1128 )
1129 }
1130
1131 pub fn new(
1132 mapping: Vec<(PathBuf, PathBuf)>,
1133 filename_display_for_diagnostics: FileNameDisplayPreference,
1134 filename_embeddable_preference: FileNameEmbeddablePreference,
1135 ) -> FilePathMapping {
1136 FilePathMapping {
1137 mapping,
1138 filename_display_for_diagnostics,
1139 filename_embeddable_preference,
1140 }
1141 }
1142
1143 pub fn map_prefix<'a>(&'a self, path: impl Into<Cow<'a, Path>>) -> (Cow<'a, Path>, bool) {
1147 let path = path.into();
1148 if path.as_os_str().is_empty() {
1149 return (path, false);
1152 }
1153
1154 return remap_path_prefix(&self.mapping, path);
1155
1156 #[instrument(level = "debug", skip(mapping), ret)]
1157 fn remap_path_prefix<'a>(
1158 mapping: &'a [(PathBuf, PathBuf)],
1159 path: Cow<'a, Path>,
1160 ) -> (Cow<'a, Path>, bool) {
1161 for (from, to) in mapping.iter().rev() {
1165 debug!("Trying to apply {from:?} => {to:?}");
1166
1167 if let Ok(rest) = path.strip_prefix(from) {
1168 let remapped = if rest.as_os_str().is_empty() {
1169 to.into()
1176 } else {
1177 to.join(rest).into()
1178 };
1179 debug!("Match - remapped");
1180
1181 return (remapped, true);
1182 } else {
1183 debug!("No match - prefix {from:?} does not match");
1184 }
1185 }
1186
1187 debug!("not remapped");
1188 (path, false)
1189 }
1190 }
1191
1192 fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
1193 match file {
1194 FileName::Real(realfile) if let RealFileName::LocalPath(local_path) = realfile => {
1195 let (mapped_path, mapped) = self.map_prefix(local_path);
1196 let realfile = if mapped {
1197 RealFileName::Remapped {
1198 local_path: Some(local_path.clone()),
1199 virtual_name: mapped_path.into_owned(),
1200 }
1201 } else {
1202 realfile.clone()
1203 };
1204 (FileName::Real(realfile), mapped)
1205 }
1206 FileName::Real(_) => unreachable!("attempted to remap an already remapped filename"),
1207 other => (other.clone(), false),
1208 }
1209 }
1210
1211 pub fn to_real_filename<'a>(&self, local_path: impl Into<Cow<'a, Path>>) -> RealFileName {
1215 let local_path = local_path.into();
1216 if let (remapped_path, true) = self.map_prefix(&*local_path) {
1217 RealFileName::Remapped {
1218 virtual_name: remapped_path.into_owned(),
1219 local_path: Some(local_path.into_owned()),
1220 }
1221 } else {
1222 RealFileName::LocalPath(local_path.into_owned())
1223 }
1224 }
1225
1226 pub fn to_embeddable_absolute_path(
1232 &self,
1233 file_path: RealFileName,
1234 working_directory: &RealFileName,
1235 ) -> RealFileName {
1236 match file_path {
1237 RealFileName::Remapped { local_path, virtual_name } => {
1240 RealFileName::Remapped {
1241 local_path: match self.filename_embeddable_preference {
1242 FileNameEmbeddablePreference::RemappedOnly => None,
1243 FileNameEmbeddablePreference::LocalAndRemapped => local_path,
1244 },
1245 virtual_name,
1249 }
1250 }
1251
1252 RealFileName::LocalPath(unmapped_file_path) => {
1253 let (new_path, was_remapped) = self.map_prefix(&unmapped_file_path);
1255 if was_remapped {
1256 return RealFileName::Remapped {
1258 virtual_name: new_path.into_owned(),
1259 local_path: match self.filename_embeddable_preference {
1261 FileNameEmbeddablePreference::RemappedOnly => None,
1262 FileNameEmbeddablePreference::LocalAndRemapped => {
1263 Some(unmapped_file_path)
1264 }
1265 },
1266 };
1267 }
1268
1269 if new_path.is_absolute() {
1270 return RealFileName::LocalPath(new_path.into_owned());
1274 }
1275
1276 debug_assert!(new_path.is_relative());
1277 let unmapped_file_path_rel = new_path;
1278
1279 match working_directory {
1280 RealFileName::LocalPath(unmapped_working_dir_abs) => {
1281 let unmapped_file_path_abs =
1282 unmapped_working_dir_abs.join(unmapped_file_path_rel);
1283
1284 let (file_path_abs, was_remapped) =
1288 self.map_prefix(&unmapped_file_path_abs);
1289 if was_remapped {
1290 RealFileName::Remapped {
1291 virtual_name: file_path_abs.into_owned(),
1292 local_path: match self.filename_embeddable_preference {
1293 FileNameEmbeddablePreference::RemappedOnly => None,
1294 FileNameEmbeddablePreference::LocalAndRemapped => {
1295 Some(unmapped_file_path_abs)
1296 }
1297 },
1298 }
1299 } else {
1300 RealFileName::LocalPath(file_path_abs.into_owned())
1303 }
1304 }
1305 RealFileName::Remapped {
1306 local_path,
1307 virtual_name: remapped_working_dir_abs,
1308 } => {
1309 RealFileName::Remapped {
1312 virtual_name: Path::new(remapped_working_dir_abs)
1313 .join(&unmapped_file_path_rel),
1314 local_path: match self.filename_embeddable_preference {
1315 FileNameEmbeddablePreference::RemappedOnly => None,
1316 FileNameEmbeddablePreference::LocalAndRemapped => local_path
1317 .as_ref()
1318 .map(|local_path| local_path.join(unmapped_file_path_rel)),
1319 },
1320 }
1321 }
1322 }
1323 }
1324 }
1325 }
1326
1327 #[instrument(level = "debug", skip(self), ret)]
1339 fn reverse_map_prefix_heuristically(&self, path: &Path) -> Option<PathBuf> {
1340 let mut found = None;
1341
1342 for (from, to) in self.mapping.iter() {
1343 let has_normal_component = to.components().any(|c| match c {
1344 path::Component::Normal(s) => !s.is_empty(),
1345 _ => false,
1346 });
1347
1348 if !has_normal_component {
1349 continue;
1350 }
1351
1352 let Ok(rest) = path.strip_prefix(to) else {
1353 continue;
1354 };
1355
1356 if found.is_some() {
1357 return None;
1358 }
1359
1360 found = Some(from.join(rest));
1361 }
1362
1363 found
1364 }
1365}