]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/span_utils.rs
Rollup merge of #55343 - Keruspe:remap-debuginfo-release, r=alexcrichton
[rust.git] / src / librustc_save_analysis / span_utils.rs
1 // Copyright 2012-2014 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.
4 //
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.
10
11 use rustc::session::Session;
12
13 use generated_code;
14
15 use std::cell::Cell;
16
17 use syntax::parse::lexer::{self, StringReader};
18 use syntax::parse::token::{self, Token};
19 use syntax_pos::*;
20
21 #[derive(Clone)]
22 pub struct SpanUtils<'a> {
23     pub sess: &'a Session,
24     // FIXME given that we clone SpanUtils all over the place, this err_count is
25     // probably useless and any logic relying on it is bogus.
26     pub err_count: Cell<isize>,
27 }
28
29 impl<'a> SpanUtils<'a> {
30     pub fn new(sess: &'a Session) -> SpanUtils<'a> {
31         SpanUtils {
32             sess,
33             err_count: Cell::new(0),
34         }
35     }
36
37     pub fn make_filename_string(&self, file: &SourceFile) -> String {
38         match &file.name {
39             FileName::Real(path) if !file.name_was_remapped => {
40                 if path.is_absolute() {
41                     self.sess.source_map().path_mapping()
42                         .map_prefix(path.clone()).0
43                         .display()
44                         .to_string()
45                 } else {
46                     self.sess.working_dir.0
47                         .join(&path)
48                         .display()
49                         .to_string()
50                 }
51             },
52             // If the file name is already remapped, we assume the user
53             // configured it the way they wanted to, so use that directly
54             filename => filename.to_string()
55         }
56     }
57
58     pub fn snippet(&self, span: Span) -> String {
59         match self.sess.source_map().span_to_snippet(span) {
60             Ok(s) => s,
61             Err(_) => String::new(),
62         }
63     }
64
65     pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
66         lexer::StringReader::retokenize(&self.sess.parse_sess, span)
67     }
68
69     pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
70         let mut toks = self.retokenise_span(span);
71         loop {
72             let next = toks.real_token();
73             if next.tok == token::Eof {
74                 return None;
75             }
76             if next.tok == tok {
77                 return Some(next.sp);
78             }
79         }
80     }
81
82     // // Return the name for a macro definition (identifier after first `!`)
83     // pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
84     //     let mut toks = self.retokenise_span(span);
85     //     loop {
86     //         let ts = toks.real_token();
87     //         if ts.tok == token::Eof {
88     //             return None;
89     //         }
90     //         if ts.tok == token::Not {
91     //             let ts = toks.real_token();
92     //             if ts.tok.is_ident() {
93     //                 return Some(ts.sp);
94     //             } else {
95     //                 return None;
96     //             }
97     //         }
98     //     }
99     // }
100
101     // // Return the name for a macro use (identifier before first `!`).
102     // pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
103     //     let mut toks = self.retokenise_span(span);
104     //     let mut prev = toks.real_token();
105     //     loop {
106     //         if prev.tok == token::Eof {
107     //             return None;
108     //         }
109     //         let ts = toks.real_token();
110     //         if ts.tok == token::Not {
111     //             if prev.tok.is_ident() {
112     //                 return Some(prev.sp);
113     //             } else {
114     //                 return None;
115     //             }
116     //         }
117     //         prev = ts;
118     //     }
119     // }
120
121     /// Return true if the span is generated code, and
122     /// it is not a subspan of the root callsite.
123     ///
124     /// Used to filter out spans of minimal value,
125     /// such as references to macro internal variables.
126     pub fn filter_generated(&self, span: Span) -> bool {
127         if span.is_dummy() {
128             return true;
129         }
130
131         if !generated_code(span) {
132             return false;
133         }
134
135         //If the span comes from a fake source_file, filter it.
136         !self.sess
137             .source_map()
138             .lookup_char_pos(span.lo())
139             .file
140             .is_real_file()
141     }
142 }
143
144 macro_rules! filter {
145     ($util: expr, $parent: expr) => {
146         if $util.filter_generated($parent) {
147             return None;
148         }
149     };
150 }