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;
20 use syntax::parse::lexer::{self, Reader, StringReader};
21 use syntax::tokenstream::TokenTree;
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 // sub_span starts at span.lo, so we need to adjust the positions etc.
50 // If sub_span is None, we don't need to adjust.
51 pub fn make_sub_span(&self, span: Span, sub_span: Option<Span>) -> Option<Span> {
55 let FileMapAndBytePos {fm, pos} = self.sess.codemap().lookup_byte_offset(span.lo);
56 let base = pos + fm.start_pos;
58 lo: base + self.sess.codemap().lookup_byte_offset(sub.lo).pos,
59 hi: base + self.sess.codemap().lookup_byte_offset(sub.hi).pos,
60 expn_id: span.expn_id,
66 pub fn snippet(&self, span: Span) -> String {
67 match self.sess.codemap().span_to_snippet(span) {
69 Err(_) => String::new(),
73 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
74 // sadness - we don't have spans for sub-expressions nor access to the tokens
75 // so in order to get extents for the function name itself (which dxr expects)
76 // we need to re-tokenise the fn definition
78 // Note: this is a bit awful - it adds the contents of span to the end of
79 // the codemap as a new filemap. This is mostly OK, but means we should
80 // not iterate over the codemap. Also, any spans over the new filemap
81 // are incompatible with spans over other filemaps.
82 let filemap = self.sess
84 .new_filemap(String::from("<anon-dxr>"), None, self.snippet(span));
86 lexer::StringReader::new(s.diagnostic(), filemap)
89 fn span_to_tts(&self, span: Span) -> Vec<TokenTree> {
90 let srdr = self.retokenise_span(span);
91 let mut p = Parser::new(&self.sess.parse_sess, Box::new(srdr));
92 p.parse_all_token_trees().expect("Couldn't re-parse span")
95 // Re-parses a path and returns the span for the last identifier in the path
96 pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
97 let mut result = None;
99 let mut toks = self.retokenise_span(span);
100 let mut bracket_count = 0;
102 let ts = toks.real_token();
103 if ts.tok == token::Eof {
104 return self.make_sub_span(span, result)
106 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
107 result = Some(ts.sp);
110 bracket_count += match ts.tok {
113 token::BinOp(token::Shr) => -2,
119 // Return the span for the first identifier in the path.
120 pub fn span_for_first_ident(&self, span: Span) -> Option<Span> {
121 let mut toks = self.retokenise_span(span);
122 let mut bracket_count = 0;
124 let ts = toks.real_token();
125 if ts.tok == token::Eof {
128 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
129 return self.make_sub_span(span, Some(ts.sp));
132 bracket_count += match ts.tok {
135 token::BinOp(token::Shr) => -2,
141 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
142 // any brackets, or the last span.
143 pub fn sub_span_for_meth_name(&self, span: Span) -> Option<Span> {
144 let mut toks = self.retokenise_span(span);
145 let mut prev = toks.real_token();
146 let mut result = None;
147 let mut bracket_count = 0;
148 let mut prev_span = None;
149 while prev.tok != token::Eof {
151 let mut next = toks.real_token();
153 if (next.tok == token::OpenDelim(token::Paren) || next.tok == token::Lt) &&
154 bracket_count == 0 && prev.tok.is_ident() {
155 result = Some(prev.sp);
158 if bracket_count == 0 && next.tok == token::ModSep {
161 next = toks.real_token();
162 if next.tok == token::Lt && old.tok.is_ident() {
163 result = Some(old.sp);
167 bracket_count += match prev.tok {
168 token::OpenDelim(token::Paren) | token::Lt => 1,
169 token::CloseDelim(token::Paren) | token::Gt => -1,
170 token::BinOp(token::Shr) => -2,
174 if prev.tok.is_ident() && bracket_count == 0 {
175 prev_span = Some(prev.sp);
179 if result.is_none() && prev_span.is_some() {
180 return self.make_sub_span(span, prev_span);
182 return self.make_sub_span(span, result);
185 // Return the span for the last ident before a `<` and outside any
186 // angle brackets, or the last span.
187 pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
188 let mut toks = self.retokenise_span(span);
189 let mut prev = toks.real_token();
190 let mut result = None;
192 // We keep track of the following two counts - the depth of nesting of
193 // angle brackets, and the depth of nesting of square brackets. For the
194 // angle bracket count, we only count tokens which occur outside of any
195 // square brackets (i.e. bracket_count == 0). The intutition here is
196 // that we want to count angle brackets in the type, but not any which
197 // could be in expression context (because these could mean 'less than',
199 let mut angle_count = 0;
200 let mut bracket_count = 0;
202 let next = toks.real_token();
204 if (next.tok == token::Lt || next.tok == token::Colon) &&
206 bracket_count == 0 &&
207 prev.tok.is_ident() {
208 result = Some(prev.sp);
211 if bracket_count == 0 {
212 angle_count += match prev.tok {
215 token::BinOp(token::Shl) => 2,
216 token::BinOp(token::Shr) => -2,
221 bracket_count += match prev.tok {
222 token::OpenDelim(token::Bracket) => 1,
223 token::CloseDelim(token::Bracket) => -1,
227 if next.tok == token::Eof {
232 if angle_count != 0 || bracket_count != 0 {
233 let loc = self.sess.codemap().lookup_char_pos(span.lo);
235 "Mis-counted brackets when breaking path? Parsing '{}' \
241 if result.is_none() && prev.tok.is_ident() && angle_count == 0 {
242 return self.make_sub_span(span, Some(prev.sp));
244 self.make_sub_span(span, result)
247 // Reparse span and return an owned vector of sub spans of the first limit
248 // identifier tokens in the given nesting level.
249 // example with Foo<Bar<T,V>, Bar<T,V>>
250 // Nesting = 0: all idents outside of angle brackets: [Foo]
251 // Nesting = 1: idents within one level of angle brackets: [Bar, Bar]
252 pub fn spans_with_brackets(&self, span: Span, nesting: isize, limit: isize) -> Vec<Span> {
253 let mut result: Vec<Span> = vec![];
255 let mut toks = self.retokenise_span(span);
256 // We keep track of how many brackets we're nested in
257 let mut angle_count: isize = 0;
258 let mut bracket_count: isize = 0;
259 let mut found_ufcs_sep = false;
261 let ts = toks.real_token();
262 if ts.tok == token::Eof {
263 if angle_count != 0 || bracket_count != 0 {
264 if generated_code(span) {
267 let loc = self.sess.codemap().lookup_char_pos(span.lo);
269 "Mis-counted brackets when breaking path? \
270 Parsing '{}' in {}, line {}",
277 if (result.len() as isize) == limit {
280 bracket_count += match ts.tok {
281 token::OpenDelim(token::Bracket) => 1,
282 token::CloseDelim(token::Bracket) => -1,
285 if bracket_count > 0 {
288 angle_count += match ts.tok {
291 token::BinOp(token::Shl) => 2,
292 token::BinOp(token::Shr) => -2,
296 // Ignore the `>::` in `<Type as Trait>::AssocTy`.
298 // The root cause of this hack is that the AST representation of
299 // qpaths is horrible. It treats <A as B>::C as a path with two
300 // segments, B and C and notes that there is also a self type A at
301 // position 0. Because we don't have spans for individual idents,
302 // only the whole path, we have to iterate over the tokens in the
303 // path, trying to pull out the non-nested idents (e.g., avoiding 'a
304 // in `<A as B<'a>>::C`). So we end up with a span for `B>::C` from
305 // the start of the first ident to the end of the path.
306 if !found_ufcs_sep && angle_count == -1 {
307 found_ufcs_sep = true;
310 if ts.tok.is_ident() && angle_count == nesting {
311 result.push(self.make_sub_span(span, Some(ts.sp)).unwrap());
316 /// `span` must be the span for an item such as a function or struct. This
317 /// function returns the program text from the start of the span until the
318 /// end of the 'signature' part, that is up to, but not including an opening
319 /// brace or semicolon.
320 pub fn signature_string_for_span(&self, span: Span) -> Option<String> {
321 let mut toks = self.span_to_tts(span).into_iter();
322 let mut prev = toks.next().unwrap();
323 let first_span = prev.get_span();
324 let mut angle_count = 0;
326 if let TokenTree::Token(_, ref tok) = prev {
327 angle_count += match *tok {
328 token::Eof => { return None; }
331 token::BinOp(token::Shl) => 2,
332 token::BinOp(token::Shr) => -2,
340 if let TokenTree::Token(_, token::Semi) = tok {
341 return Some(self.snippet(mk_sp(first_span.lo, prev.get_span().hi)));
342 } else if let TokenTree::Delimited(_, ref d) = tok {
343 if d.delim == token::Brace {
344 return Some(self.snippet(mk_sp(first_span.lo, prev.get_span().hi)));
352 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
353 let mut toks = self.retokenise_span(span);
354 let mut prev = toks.real_token();
356 if prev.tok == token::Eof {
359 let next = toks.real_token();
361 return self.make_sub_span(span, Some(prev.sp));
367 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
368 let mut toks = self.retokenise_span(span);
370 let next = toks.real_token();
371 if next.tok == token::Eof {
375 return self.make_sub_span(span, Some(next.sp));
380 pub fn sub_span_after_keyword(&self, span: Span, keyword: keywords::Keyword) -> Option<Span> {
381 self.sub_span_after(span, |t| t.is_keyword(keyword))
384 pub fn sub_span_after_token(&self, span: Span, tok: Token) -> Option<Span> {
385 self.sub_span_after(span, |t| t == tok)
388 fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
389 let mut toks = self.retokenise_span(span);
391 let ts = toks.real_token();
392 if ts.tok == token::Eof {
396 let ts = toks.real_token();
397 if ts.tok == token::Eof {
400 return self.make_sub_span(span, Some(ts.sp));
407 // Returns a list of the spans of idents in a path.
408 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
409 pub fn spans_for_path_segments(&self, path: &ast::Path) -> Vec<Span> {
410 self.spans_with_brackets(path.span, 0, -1)
413 // Return an owned vector of the subspans of the param identifier
414 // tokens found in span.
415 pub fn spans_for_ty_params(&self, span: Span, number: isize) -> Vec<Span> {
416 // Type params are nested within one level of brackets:
417 // i.e. we want Vec<A, B> from Foo<A, B<T,U>>
418 self.spans_with_brackets(span, 1, number)
421 pub fn report_span_err(&self, kind: &str, span: Span) {
422 let loc = self.sess.codemap().lookup_char_pos(span.lo);
423 info!("({}) Could not find sub_span in `{}` in {}, line {}",
428 self.err_count.set(self.err_count.get() + 1);
429 if self.err_count.get() > 1000 {
430 bug!("span errors reached 1000, giving up");
434 // Return the name for a macro definition (identifier after first `!`)
435 pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
436 let mut toks = self.retokenise_span(span);
438 let ts = toks.real_token();
439 if ts.tok == token::Eof {
442 if ts.tok == token::Not {
443 let ts = toks.real_token();
444 if ts.tok.is_ident() {
445 return self.make_sub_span(span, Some(ts.sp));
453 // Return the name for a macro use (identifier before first `!`).
454 pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
455 let mut toks = self.retokenise_span(span);
456 let mut prev = toks.real_token();
458 if prev.tok == token::Eof {
461 let ts = toks.real_token();
462 if ts.tok == token::Not {
463 if prev.tok.is_ident() {
464 return self.make_sub_span(span, Some(prev.sp));
473 /// Return true if the span is generated code, and
474 /// it is not a subspan of the root callsite.
476 /// Used to filter out spans of minimal value,
477 /// such as references to macro internal variables.
478 pub fn filter_generated(&self, sub_span: Option<Span>, parent: Span) -> bool {
479 if !generated_code(parent) {
480 if sub_span.is_none() {
481 // Edge case - this occurs on generated code with incorrect expansion info.
486 // If sub_span is none, filter out generated code.
487 if sub_span.is_none() {
491 //If the span comes from a fake filemap, filter it.
492 if !self.sess.codemap().lookup_char_pos(parent.lo).file.is_real_file() {
496 // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root
497 // callsite. This filters out macro internal variables and most malformed spans.
498 let span = self.sess.codemap().source_callsite(parent);
499 !(span.contains(parent))
503 macro_rules! filter {
504 ($util: expr, $span: ident, $parent: expr, None) => {
505 if $util.filter_generated($span, $parent) {
509 ($util: expr, $span: ident, $parent: expr) => {
510 if $util.filter_generated($span, $parent) {