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;
13 use save::generated_code;
20 use syntax::codemap::*;
21 use syntax::parse::lexer;
22 use syntax::parse::lexer::{Reader, StringReader};
23 use syntax::parse::token;
24 use syntax::parse::token::{keywords, Token};
27 pub struct SpanUtils<'a> {
28 pub sess: &'a Session,
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 // Standard string for extents/location.
51 pub fn extent_str(&self, span: Span) -> String {
52 let lo_loc = self.sess.codemap().lookup_char_pos(span.lo);
53 let hi_loc = self.sess.codemap().lookup_char_pos(span.hi);
54 let lo_pos = self.sess.codemap().bytepos_to_file_charpos(span.lo);
55 let hi_pos = self.sess.codemap().bytepos_to_file_charpos(span.hi);
56 let lo_pos_byte = self.sess.codemap().lookup_byte_offset(span.lo).pos;
57 let hi_pos_byte = self.sess.codemap().lookup_byte_offset(span.hi).pos;
59 format!("file_name,\"{}\",file_line,{},file_col,{},extent_start,{},extent_start_bytes,{},\
60 file_line_end,{},file_col_end,{},extent_end,{},extent_end_bytes,{}",
61 SpanUtils::make_path_string(&lo_loc.file.name),
62 lo_loc.line, lo_loc.col.to_usize(), lo_pos.to_usize(), lo_pos_byte.to_usize(),
63 hi_loc.line, hi_loc.col.to_usize(), hi_pos.to_usize(), hi_pos_byte.to_usize())
66 // sub_span starts at span.lo, so we need to adjust the positions etc.
67 // If sub_span is None, we don't need to adjust.
68 pub fn make_sub_span(&self, span: Span, sub_span: Option<Span>) -> Option<Span> {
72 let FileMapAndBytePos {fm, pos} = self.sess.codemap().lookup_byte_offset(span.lo);
73 let base = pos + fm.start_pos;
75 lo: base + self.sess.codemap().lookup_byte_offset(sub.lo).pos,
76 hi: base + self.sess.codemap().lookup_byte_offset(sub.hi).pos,
77 expn_id: span.expn_id,
83 pub fn snippet(&self, span: Span) -> String {
84 match self.sess.codemap().span_to_snippet(span) {
86 Err(_) => String::new(),
90 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
91 // sadness - we don't have spans for sub-expressions nor access to the tokens
92 // so in order to get extents for the function name itself (which dxr expects)
93 // we need to re-tokenise the fn definition
95 // Note: this is a bit awful - it adds the contents of span to the end of
96 // the codemap as a new filemap. This is mostly OK, but means we should
97 // not iterate over the codemap. Also, any spans over the new filemap
98 // are incompatible with spans over other filemaps.
99 let filemap = self.sess
101 .new_filemap(String::from("<anon-dxr>"), self.snippet(span));
103 lexer::StringReader::new(s.diagnostic(), filemap)
106 // Re-parses a path and returns the span for the last identifier in the path
107 pub fn span_for_last_ident(&self, span: Span) -> Option<Span> {
108 let mut result = None;
110 let mut toks = self.retokenise_span(span);
111 let mut bracket_count = 0;
113 let ts = toks.real_token();
114 if ts.tok == token::Eof {
115 return self.make_sub_span(span, result)
117 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
118 result = Some(ts.sp);
121 bracket_count += match ts.tok {
124 token::BinOp(token::Shr) => -2,
130 // Return the span for the first identifier in the path.
131 pub fn span_for_first_ident(&self, span: Span) -> Option<Span> {
132 let mut toks = self.retokenise_span(span);
133 let mut bracket_count = 0;
135 let ts = toks.real_token();
136 if ts.tok == token::Eof {
139 if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
140 return self.make_sub_span(span, Some(ts.sp));
143 bracket_count += match ts.tok {
146 token::BinOp(token::Shr) => -2,
152 // Return the span for the last ident before a `(` or `<` or '::<' and outside any
153 // any brackets, or the last span.
154 pub fn sub_span_for_meth_name(&self, span: Span) -> Option<Span> {
155 let mut toks = self.retokenise_span(span);
156 let mut prev = toks.real_token();
157 let mut result = None;
158 let mut bracket_count = 0;
159 let mut last_span = None;
160 while prev.tok != token::Eof {
162 let mut next = toks.real_token();
164 if (next.tok == token::OpenDelim(token::Paren) || next.tok == token::Lt) &&
165 bracket_count == 0 && prev.tok.is_ident() {
166 result = Some(prev.sp);
169 if bracket_count == 0 && next.tok == token::ModSep {
172 next = toks.real_token();
173 if next.tok == token::Lt && old.tok.is_ident() {
174 result = Some(old.sp);
178 bracket_count += match prev.tok {
179 token::OpenDelim(token::Paren) | token::Lt => 1,
180 token::CloseDelim(token::Paren) | token::Gt => -1,
181 token::BinOp(token::Shr) => -2,
185 if prev.tok.is_ident() && bracket_count == 0 {
186 last_span = Some(prev.sp);
190 if result.is_none() && last_span.is_some() {
191 return self.make_sub_span(span, last_span);
193 return self.make_sub_span(span, result);
196 // Return the span for the last ident before a `<` and outside any
197 // brackets, or the last span.
198 pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
199 let mut toks = self.retokenise_span(span);
200 let mut prev = toks.real_token();
201 let mut result = None;
202 let mut bracket_count = 0;
204 let next = toks.real_token();
206 if (next.tok == token::Lt || next.tok == token::Colon) && bracket_count == 0 &&
207 prev.tok.is_ident() {
208 result = Some(prev.sp);
211 bracket_count += match prev.tok {
214 token::BinOp(token::Shl) => 2,
215 token::BinOp(token::Shr) => -2,
219 if next.tok == token::Eof {
224 if bracket_count != 0 {
225 let loc = self.sess.codemap().lookup_char_pos(span.lo);
226 self.sess.span_bug(span,
227 &format!("Mis-counted brackets when breaking path? Parsing '{}' \
233 if result.is_none() && prev.tok.is_ident() && bracket_count == 0 {
234 return self.make_sub_span(span, Some(prev.sp));
236 self.make_sub_span(span, result)
239 // Reparse span and return an owned vector of sub spans of the first limit
240 // identifier tokens in the given nesting level.
241 // example with Foo<Bar<T,V>, Bar<T,V>>
242 // Nesting = 0: all idents outside of brackets: [Foo]
243 // Nesting = 1: idents within one level of brackets: [Bar, Bar]
244 pub fn spans_with_brackets(&self, span: Span, nesting: isize, limit: isize) -> Vec<Span> {
245 let mut result: Vec<Span> = vec!();
247 let mut toks = self.retokenise_span(span);
248 // We keep track of how many brackets we're nested in
249 let mut bracket_count: isize = 0;
250 let mut found_ufcs_sep = false;
252 let ts = toks.real_token();
253 if ts.tok == token::Eof {
254 if bracket_count != 0 {
255 if generated_code(span) {
258 let loc = self.sess.codemap().lookup_char_pos(span.lo);
259 self.sess.span_bug(span,
260 &format!("Mis-counted brackets when breaking path? \
261 Parsing '{}' in {}, line {}",
268 if (result.len() as isize) == limit {
271 bracket_count += match ts.tok {
274 token::BinOp(token::Shl) => 2,
275 token::BinOp(token::Shr) => -2,
279 // Ignore the `>::` in `<Type as Trait>::AssocTy`.
281 // The root cause of this hack is that the AST representation of
282 // qpaths is horrible. It treats <A as B>::C as a path with two
283 // segments, B and C and notes that there is also a self type A at
284 // position 0. Because we don't have spans for individual idents,
285 // only the whole path, we have to iterate over the tokens in the
286 // path, trying to pull out the non-nested idents (e.g., avoiding 'a
287 // in `<A as B<'a>>::C`). So we end up with a span for `B>::C` from
288 // the start of the first ident to the end of the path.
289 if !found_ufcs_sep && bracket_count == -1 {
290 found_ufcs_sep = true;
293 if ts.tok.is_ident() && bracket_count == nesting {
294 result.push(self.make_sub_span(span, Some(ts.sp)).unwrap());
299 pub fn sub_span_before_token(&self, span: Span, tok: Token) -> Option<Span> {
300 let mut toks = self.retokenise_span(span);
301 let mut prev = toks.real_token();
303 if prev.tok == token::Eof {
306 let next = toks.real_token();
308 return self.make_sub_span(span, Some(prev.sp));
314 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
315 let mut toks = self.retokenise_span(span);
317 let next = toks.real_token();
318 if next.tok == token::Eof {
322 return self.make_sub_span(span, Some(next.sp));
327 pub fn sub_span_after_keyword(&self, span: Span, keyword: keywords::Keyword) -> Option<Span> {
328 self.sub_span_after(span, |t| t.is_keyword(keyword))
331 pub fn sub_span_after_token(&self, span: Span, tok: Token) -> Option<Span> {
332 self.sub_span_after(span, |t| t == tok)
335 fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
336 let mut toks = self.retokenise_span(span);
338 let ts = toks.real_token();
339 if ts.tok == token::Eof {
343 let ts = toks.real_token();
344 if ts.tok == token::Eof {
347 return self.make_sub_span(span, Some(ts.sp));
354 // Returns a list of the spans of idents in a path.
355 // E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
356 pub fn spans_for_path_segments(&self, path: &ast::Path) -> Vec<Span> {
357 self.spans_with_brackets(path.span, 0, -1)
360 // Return an owned vector of the subspans of the param identifier
361 // tokens found in span.
362 pub fn spans_for_ty_params(&self, span: Span, number: isize) -> Vec<Span> {
363 // Type params are nested within one level of brackets:
364 // i.e. we want Vec<A, B> from Foo<A, B<T,U>>
365 self.spans_with_brackets(span, 1, number)
368 pub fn report_span_err(&self, kind: &str, span: Span) {
369 let loc = self.sess.codemap().lookup_char_pos(span.lo);
370 info!("({}) Could not find sub_span in `{}` in {}, line {}",
375 self.err_count.set(self.err_count.get() + 1);
376 if self.err_count.get() > 1000 {
377 self.sess.bug("span errors reached 1000, giving up");
381 // Return the name for a macro definition (identifier after first `!`)
382 pub fn span_for_macro_def_name(&self, span: Span) -> Option<Span> {
383 let mut toks = self.retokenise_span(span);
385 let ts = toks.real_token();
386 if ts.tok == token::Eof {
389 if ts.tok == token::Not {
390 let ts = toks.real_token();
391 if ts.tok.is_ident() {
392 return self.make_sub_span(span, Some(ts.sp));
400 // Return the name for a macro use (identifier before first `!`).
401 pub fn span_for_macro_use_name(&self, span:Span) -> Option<Span> {
402 let mut toks = self.retokenise_span(span);
403 let mut prev = toks.real_token();
405 if prev.tok == token::Eof {
408 let ts = toks.real_token();
409 if ts.tok == token::Not {
410 if prev.tok.is_ident() {
411 return self.make_sub_span(span, Some(prev.sp));
420 /// Return true if the span is generated code, and
421 /// it is not a subspan of the root callsite.
423 /// Used to filter out spans of minimal value,
424 /// such as references to macro internal variables.
425 pub fn filter_generated(&self, sub_span: Option<Span>, parent: Span) -> bool {
426 if !generated_code(parent) {
427 if sub_span.is_none() {
428 // Edge case - this occurs on generated code with incorrect expansion info.
433 // If sub_span is none, filter out generated code.
434 if sub_span.is_none() {
438 //If the span comes from a fake filemap, filter it.
439 if !self.sess.codemap().lookup_char_pos(parent.lo).file.is_real_file() {
443 // Otherwise, a generated span is deemed invalid if it is not a sub-span of the root
444 // callsite. This filters out macro internal variables and most malformed spans.
445 let span = self.sess.codemap().source_callsite(parent);
446 !(span.contains(parent))
450 macro_rules! filter {
451 ($util: expr, $span: ident, $parent: expr, None) => {
452 if $util.filter_generated($span, $parent) {
456 ($util: expr, $span: ident, $parent: expr) => {
457 if $util.filter_generated($span, $parent) {