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::parse::token::{self, Token};
22 use syntax::symbol::keywords;
26 pub struct SpanUtils<'a> {
27 pub sess: &'a Session,
28 // FIXME given that we clone SpanUtils all over the place, this err_count is
29 // probably useless and any logic relying on it is bogus.
30 pub err_count: Cell<isize>,
33 impl<'a> SpanUtils<'a> {
34 pub fn new(sess: &'a Session) -> SpanUtils<'a> {
37 err_count: Cell::new(0),
41 pub fn make_path_string(file_name: &str) -> String {
42 let path = Path::new(file_name);
43 if path.is_absolute() {
44 path.clone().display().to_string()
46 env::current_dir().unwrap().join(&path).display().to_string()
50 // sub_span starts at span.lo, so we need to adjust the positions etc.
51 // If sub_span is None, we don't need to adjust.
52 pub fn make_sub_span(&self, span: Span, sub_span: Option<Span>) -> Option<Span> {
56 let FileMapAndBytePos {fm, pos} = self.sess.codemap().lookup_byte_offset(span.lo);
57 let base = pos + fm.start_pos;
59 lo: base + self.sess.codemap().lookup_byte_offset(sub.lo).pos,
60 hi: base + self.sess.codemap().lookup_byte_offset(sub.hi).pos,
61 expn_id: span.expn_id,
67 pub fn snippet(&self, span: Span) -> String {
68 match self.sess.codemap().span_to_snippet(span) {
70 Err(_) => String::new(),
74 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
75 // sadness - we don't have spans for sub-expressions nor access to the tokens
76 // so in order to get extents for the function name itself (which dxr expects)
77 // we need to re-tokenise the fn definition
79 // Note: this is a bit awful - it adds the contents of span to the end of
80 // the codemap as a new filemap. This is mostly OK, but means we should
81 // not iterate over the codemap. Also, any spans over the new filemap
82 // are incompatible with spans over other filemaps.
83 let filemap = self.sess
85 .new_filemap(String::from("<anon-dxr>"), None, self.snippet(span));
87 lexer::StringReader::new(s.diagnostic(), filemap)
90 // Re-parses a path and returns the span for the last identifier in the path
91 pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
92 let mut result = None;
94 let mut toks = self.retokenise_span(span);
95 let mut bracket_count = 0;
97 let ts = toks.real_token();
98 if ts.tok == token::Eof {
99 return self.make_sub_span(span, result)
101 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
102 result = Some(ts.sp);
105 bracket_count += match ts.tok {
108 token::BinOp(token::Shr) => -2,
114 // Return the span for the first identifier in the path.
115 pub fn span_for_first_ident(&self, span: Span) -> Option<Span> {
116 let mut toks = self.retokenise_span(span);
117 let mut bracket_count = 0;
119 let ts = toks.real_token();
120 if ts.tok == token::Eof {
123 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
124 return self.make_sub_span(span, Some(ts.sp));
127 bracket_count += match ts.tok {
130 token::BinOp(token::Shr) => -2,
136 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
137 // any brackets, or the last span.
138 pub fn sub_span_for_meth_name(&self, span: Span) -> Option<Span> {
139 let mut toks = self.retokenise_span(span);
140 let mut prev = toks.real_token();
141 let mut result = None;
142 let mut bracket_count = 0;
143 let mut prev_span = None;
144 while prev.tok != token::Eof {
146 let mut next = toks.real_token();
148 if (next.tok == token::OpenDelim(token::Paren) || next.tok == token::Lt) &&
149 bracket_count == 0 && prev.tok.is_ident() {
150 result = Some(prev.sp);
153 if bracket_count == 0 && next.tok == token::ModSep {
156 next = toks.real_token();
157 if next.tok == token::Lt && old.tok.is_ident() {
158 result = Some(old.sp);
162 bracket_count += match prev.tok {
163 token::OpenDelim(token::Paren) | token::Lt => 1,
164 token::CloseDelim(token::Paren) | token::Gt => -1,
165 token::BinOp(token::Shr) => -2,
169 if prev.tok.is_ident() && bracket_count == 0 {
170 prev_span = Some(prev.sp);
174 if result.is_none() && prev_span.is_some() {
175 return self.make_sub_span(span, prev_span);
177 return self.make_sub_span(span, result);
180 // Return the span for the last ident before a `<` and outside any
181 // angle brackets, or the last span.
182 pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
183 let mut toks = self.retokenise_span(span);
184 let mut prev = toks.real_token();
185 let mut result = None;
187 // We keep track of the following two counts - the depth of nesting of
188 // angle brackets, and the depth of nesting of square brackets. For the
189 // angle bracket count, we only count tokens which occur outside of any
190 // square brackets (i.e. bracket_count == 0). The intutition here is
191 // that we want to count angle brackets in the type, but not any which
192 // could be in expression context (because these could mean 'less than',
194 let mut angle_count = 0;
195 let mut bracket_count = 0;
197 let next = toks.real_token();
199 if (next.tok == token::Lt || next.tok == token::Colon) &&
201 bracket_count == 0 &&
202 prev.tok.is_ident() {
203 result = Some(prev.sp);
206 if bracket_count == 0 {
207 angle_count += match prev.tok {
210 token::BinOp(token::Shl) => 2,
211 token::BinOp(token::Shr) => -2,
216 bracket_count += match prev.tok {
217 token::OpenDelim(token::Bracket) => 1,
218 token::CloseDelim(token::Bracket) => -1,
222 if next.tok == token::Eof {
227 if angle_count != 0 || bracket_count != 0 {
228 let loc = self.sess.codemap().lookup_char_pos(span.lo);
230 "Mis-counted brackets when breaking path? Parsing '{}' \
236 if result.is_none() && prev.tok.is_ident() && angle_count == 0 {
237 return self.make_sub_span(span, Some(prev.sp));
239 self.make_sub_span(span, result)
242 // Reparse span and return an owned vector of sub spans of the first limit
243 // identifier tokens in the given nesting level.
244 // example with Foo<Bar<T,V>, Bar<T,V>>
245 // Nesting = 0: all idents outside of angle brackets: [Foo]
246 // Nesting = 1: idents within one level of angle brackets: [Bar, Bar]
247 pub fn spans_with_brackets(&self, span: Span, nesting: isize, limit: isize) -> Vec<Span> {
248 let mut result: Vec<Span> = vec![];
250 let mut toks = self.retokenise_span(span);
251 // We keep track of how many brackets we're nested in
252 let mut angle_count: isize = 0;
253 let mut bracket_count: isize = 0;
254 let mut found_ufcs_sep = false;
256 let ts = toks.real_token();
257 if ts.tok == token::Eof {
258 if angle_count != 0 || bracket_count != 0 {
259 if generated_code(span) {
262 let loc = self.sess.codemap().lookup_char_pos(span.lo);
264 "Mis-counted brackets when breaking path? \
265 Parsing '{}' in {}, line {}",
272 if (result.len() as isize) == limit {
275 bracket_count += match ts.tok {
276 token::OpenDelim(token::Bracket) => 1,
277 token::CloseDelim(token::Bracket) => -1,
280 if bracket_count > 0 {
283 angle_count += match ts.tok {
286 token::BinOp(token::Shl) => 2,
287 token::BinOp(token::Shr) => -2,
291 // Ignore the `>::` in `<Type as Trait>::AssocTy`.
293 // The root cause of this hack is that the AST representation of
294 // qpaths is horrible. It treats <A as B>::C as a path with two
295 // segments, B and C and notes that there is also a self type A at
296 // position 0. Because we don't have spans for individual idents,
297 // only the whole path, we have to iterate over the tokens in the
298 // path, trying to pull out the non-nested idents (e.g., avoiding 'a
299 // in `<A as B<'a>>::C`). So we end up with a span for `B>::C` from
300 // the start of the first ident to the end of the path.
301 if !found_ufcs_sep && angle_count == -1 {
302 found_ufcs_sep = true;
305 if ts.tok.is_ident() && angle_count == nesting {
306 result.push(self.make_sub_span(span, Some(ts.sp)).unwrap());
311 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
312 let mut toks = self.retokenise_span(span);
313 let mut prev = toks.real_token();
315 if prev.tok == token::Eof {
318 let next = toks.real_token();
320 return self.make_sub_span(span, Some(prev.sp));
326 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
327 let mut toks = self.retokenise_span(span);
329 let next = toks.real_token();
330 if next.tok == token::Eof {
334 return self.make_sub_span(span, Some(next.sp));
339 pub fn sub_span_after_keyword(&self, span: Span, keyword: keywords::Keyword) -> Option<Span> {
340 self.sub_span_after(span, |t| t.is_keyword(keyword))
343 pub fn sub_span_after_token(&self, span: Span, tok: Token) -> Option<Span> {
344 self.sub_span_after(span, |t| t == tok)
347 fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
348 let mut toks = self.retokenise_span(span);
350 let ts = toks.real_token();
351 if ts.tok == token::Eof {
355 let ts = toks.real_token();
356 if ts.tok == token::Eof {
359 return self.make_sub_span(span, Some(ts.sp));
366 // Returns a list of the spans of idents in a path.
367 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
368 pub fn spans_for_path_segments(&self, path: &ast::Path) -> Vec<Span> {
369 self.spans_with_brackets(path.span, 0, -1)
372 // Return an owned vector of the subspans of the param identifier
373 // tokens found in span.
374 pub fn spans_for_ty_params(&self, span: Span, number: isize) -> Vec<Span> {
375 // Type params are nested within one level of brackets:
376 // i.e. we want Vec<A, B> from Foo<A, B<T,U>>
377 self.spans_with_brackets(span, 1, number)
380 pub fn report_span_err(&self, kind: &str, span: Span) {
381 let loc = self.sess.codemap().lookup_char_pos(span.lo);
382 info!("({}) Could not find sub_span in `{}` in {}, line {}",
387 self.err_count.set(self.err_count.get() + 1);
388 if self.err_count.get() > 1000 {
389 bug!("span errors reached 1000, giving up");
393 // Return the name for a macro definition (identifier after first `!`)
394 pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
395 let mut toks = self.retokenise_span(span);
397 let ts = toks.real_token();
398 if ts.tok == token::Eof {
401 if ts.tok == token::Not {
402 let ts = toks.real_token();
403 if ts.tok.is_ident() {
404 return self.make_sub_span(span, Some(ts.sp));
412 // Return the name for a macro use (identifier before first `!`).
413 pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
414 let mut toks = self.retokenise_span(span);
415 let mut prev = toks.real_token();
417 if prev.tok == token::Eof {
420 let ts = toks.real_token();
421 if ts.tok == token::Not {
422 if prev.tok.is_ident() {
423 return self.make_sub_span(span, Some(prev.sp));
432 /// Return true if the span is generated code, and
433 /// it is not a subspan of the root callsite.
435 /// Used to filter out spans of minimal value,
436 /// such as references to macro internal variables.
437 pub fn filter_generated(&self, sub_span: Option<Span>, parent: Span) -> bool {
438 if !generated_code(parent) {
439 if sub_span.is_none() {
440 // Edge case - this occurs on generated code with incorrect expansion info.
445 // If sub_span is none, filter out generated code.
446 if sub_span.is_none() {
450 //If the span comes from a fake filemap, filter it.
451 if !self.sess.codemap().lookup_char_pos(parent.lo).file.is_real_file() {
455 // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root
456 // callsite. This filters out macro internal variables and most malformed spans.
457 let span = self.sess.codemap().source_callsite(parent);
458 !(span.contains(parent))
462 macro_rules! filter {
463 ($util: expr, $span: ident, $parent: expr, None) => {
464 if $util.filter_generated($span, $parent) {
468 ($util: expr, $span: ident, $parent: expr) => {
469 if $util.filter_generated($span, $parent) {