1 use crate::generated_code
;
2 use rustc_data_structures
::sync
::Lrc
;
3 use rustc_lexer
::{tokenize, TokenKind}
;
4 use rustc_session
::Session
;
8 pub struct SpanUtils
<'a
> {
12 impl<'a
> SpanUtils
<'a
> {
13 pub fn new(sess
: &'a Session
) -> SpanUtils
<'a
> {
17 pub fn make_filename_string(&self, file
: &SourceFile
) -> String
{
19 FileName
::Real(RealFileName
::LocalPath(path
)) => {
20 if path
.is_absolute() {
21 self.sess
.source_map().path_mapping().map_prefix(path
).0.display().to_string()
26 .remapped_path_if_available()
32 filename
=> filename
.prefer_remapped().to_string(),
36 pub fn snippet(&self, span
: Span
) -> String
{
37 match self.sess
.source_map().span_to_snippet(span
) {
39 Err(_
) => String
::new(),
43 /// Finds the span of `*` token withing the larger `span`.
44 pub fn sub_span_of_star(&self, mut span
: Span
) -> Option
<Span
> {
45 let begin
= self.sess
.source_map().lookup_byte_offset(span
.lo());
46 let end
= self.sess
.source_map().lookup_byte_offset(span
.hi());
47 // Make the range zero-length if the span is invalid.
48 if begin
.sf
.start_pos
!= end
.sf
.start_pos
{
49 span
= span
.shrink_to_lo();
52 let sf
= Lrc
::clone(&begin
.sf
);
54 self.sess
.source_map().ensure_source_file_source_present(Lrc
::clone(&sf
));
56 sf
.src
.clone().or_else(|| sf
.external_src
.borrow().get_source().map(Lrc
::clone
))?
;
57 let to_index
= |pos
: BytePos
| -> usize { (pos - sf.start_pos).0 as usize }
;
58 let text
= &src
[to_index(span
.lo())..to_index(span
.hi())];
67 .find(|(_pos
, token
)| token
.kind
== TokenKind
::Star
)?
70 let lo
= span
.lo() + BytePos(start_pos
as u32);
71 let hi
= lo
+ BytePos(1);
72 Some(span
.with_lo(lo
).with_hi(hi
))
75 /// Return true if the span is generated code, and
76 /// it is not a subspan of the root callsite.
78 /// Used to filter out spans of minimal value,
79 /// such as references to macro internal variables.
80 pub fn filter_generated(&self, span
: Span
) -> bool
{
81 if generated_code(span
) {
85 //If the span comes from a fake source_file, filter it.
86 !self.sess
.source_map().lookup_char_pos(span
.lo()).file
.is_real_file()
91 ($util
: expr
, $parent
: expr
) => {
92 if $util
.filter_generated($parent
) {