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.
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.
11 use rustc::session::Session;
19 use syntax::parse::lexer::{self, StringReader};
20 use syntax::parse::token::{self, Token};
21 use syntax::symbol::keywords;
25 pub struct SpanUtils<'a> {
26 pub sess: &'a Session,
27 // FIXME given that we clone SpanUtils all over the place, this err_count is
28 // probably useless and any logic relying on it is bogus.
29 pub err_count: Cell<isize>,
32 impl<'a> SpanUtils<'a> {
33 pub fn new(sess: &'a Session) -> SpanUtils<'a> {
36 err_count: Cell::new(0),
40 pub fn make_path_string(file_name: &str) -> String {
41 let path = Path::new(file_name);
42 if path.is_absolute() {
43 path.clone().display().to_string()
45 env::current_dir().unwrap().join(&path).display().to_string()
49 pub fn snippet(&self, span: Span) -> String {
50 match self.sess.codemap().span_to_snippet(span) {
52 Err(_) => String::new(),
56 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
57 lexer::StringReader::retokenize(&self.sess.parse_sess, span)
60 // Re-parses a path and returns the span for the last identifier in the path
61 pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
62 let mut result = None;
64 let mut toks = self.retokenise_span(span);
65 let mut bracket_count = 0;
67 let ts = toks.real_token();
68 if ts.tok == token::Eof {
71 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
75 bracket_count += match ts.tok {
78 token::BinOp(token::Shr) => -2,
84 // Return the span for the first identifier in the path.
85 pub fn span_for_first_ident(&self, span: Span) -> Option<Span> {
86 let mut toks = self.retokenise_span(span);
87 let mut bracket_count = 0;
89 let ts = toks.real_token();
90 if ts.tok == token::Eof {
93 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
97 bracket_count += match ts.tok {
100 token::BinOp(token::Shr) => -2,
106 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
107 // any brackets, or the last span.
108 pub fn sub_span_for_meth_name(&self, span: Span) -> Option<Span> {
109 let mut toks = self.retokenise_span(span);
110 let mut prev = toks.real_token();
111 let mut result = None;
112 let mut bracket_count = 0;
113 let mut prev_span = None;
114 while prev.tok != token::Eof {
116 let mut next = toks.real_token();
118 if (next.tok == token::OpenDelim(token::Paren) || next.tok == token::Lt) &&
119 bracket_count == 0 && prev.tok.is_ident() {
120 result = Some(prev.sp);
123 if bracket_count == 0 && next.tok == token::ModSep {
126 next = toks.real_token();
127 if next.tok == token::Lt && old.tok.is_ident() {
128 result = Some(old.sp);
132 bracket_count += match prev.tok {
133 token::OpenDelim(token::Paren) | token::Lt => 1,
134 token::CloseDelim(token::Paren) | token::Gt => -1,
135 token::BinOp(token::Shr) => -2,
139 if prev.tok.is_ident() && bracket_count == 0 {
140 prev_span = Some(prev.sp);
147 // Return the span for the last ident before a `<` and outside any
148 // angle brackets, or the last span.
149 pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
150 let mut toks = self.retokenise_span(span);
151 let mut prev = toks.real_token();
152 let mut result = None;
154 // We keep track of the following two counts - the depth of nesting of
155 // angle brackets, and the depth of nesting of square brackets. For the
156 // angle bracket count, we only count tokens which occur outside of any
157 // square brackets (i.e. bracket_count == 0). The intutition here is
158 // that we want to count angle brackets in the type, but not any which
159 // could be in expression context (because these could mean 'less than',
161 let mut angle_count = 0;
162 let mut bracket_count = 0;
164 let next = toks.real_token();
166 if (next.tok == token::Lt || next.tok == token::Colon) &&
168 bracket_count == 0 &&
169 prev.tok.is_ident() {
170 result = Some(prev.sp);
173 if bracket_count == 0 {
174 angle_count += match prev.tok {
177 token::BinOp(token::Shl) => 2,
178 token::BinOp(token::Shr) => -2,
183 bracket_count += match prev.tok {
184 token::OpenDelim(token::Bracket) => 1,
185 token::CloseDelim(token::Bracket) => -1,
189 if next.tok == token::Eof {
194 if angle_count != 0 || bracket_count != 0 {
195 let loc = self.sess.codemap().lookup_char_pos(span.lo());
197 "Mis-counted brackets when breaking path? Parsing '{}' \
203 if result.is_none() && prev.tok.is_ident() && angle_count == 0 {
204 return Some(prev.sp);
209 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
210 let mut toks = self.retokenise_span(span);
211 let mut prev = toks.real_token();
213 if prev.tok == token::Eof {
216 let next = toks.real_token();
218 return Some(prev.sp);
224 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
225 let mut toks = self.retokenise_span(span);
227 let next = toks.real_token();
228 if next.tok == token::Eof {
232 return Some(next.sp);
237 pub fn sub_span_after_keyword(&self, span: Span, keyword: keywords::Keyword) -> Option<Span> {
238 self.sub_span_after(span, |t| t.is_keyword(keyword))
241 pub fn sub_span_after_token(&self, span: Span, tok: Token) -> Option<Span> {
242 self.sub_span_after(span, |t| t == tok)
245 fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
246 let mut toks = self.retokenise_span(span);
248 let ts = toks.real_token();
249 if ts.tok == token::Eof {
253 let ts = toks.real_token();
254 if ts.tok == token::Eof {
263 // // Return the name for a macro definition (identifier after first `!`)
264 // pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
265 // let mut toks = self.retokenise_span(span);
267 // let ts = toks.real_token();
268 // if ts.tok == token::Eof {
271 // if ts.tok == token::Not {
272 // let ts = toks.real_token();
273 // if ts.tok.is_ident() {
274 // return Some(ts.sp);
282 // // Return the name for a macro use (identifier before first `!`).
283 // pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
284 // let mut toks = self.retokenise_span(span);
285 // let mut prev = toks.real_token();
287 // if prev.tok == token::Eof {
290 // let ts = toks.real_token();
291 // if ts.tok == token::Not {
292 // if prev.tok.is_ident() {
293 // return Some(prev.sp);
302 /// Return true if the span is generated code, and
303 /// it is not a subspan of the root callsite.
305 /// Used to filter out spans of minimal value,
306 /// such as references to macro internal variables.
307 pub fn filter_generated(&self, sub_span: Option<Span>, parent: Span) -> bool {
308 if !generated_code(parent) {
309 if sub_span.is_none() {
310 // Edge case - this occurs on generated code with incorrect expansion info.
315 // If sub_span is none, filter out generated code.
316 let sub_span = match sub_span {
321 //If the span comes from a fake filemap, filter it.
322 if !self.sess.codemap().lookup_char_pos(parent.lo()).file.is_real_file() {
326 // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root
327 // callsite. This filters out macro internal variables and most malformed spans.
328 !parent.source_callsite().contains(sub_span)
332 macro_rules! filter {
333 ($util: expr, $span: ident, $parent: expr, None) => {
334 if $util.filter_generated($span, $parent) {
338 ($util: expr, $span: ident, $parent: expr) => {
339 if $util.filter_generated($span, $parent) {