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;
17 use syntax::parse::lexer::{self, StringReader};
18 use syntax::parse::token::{self, Token};
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>,
29 impl<'a> SpanUtils<'a> {
30 pub fn new(sess: &'a Session) -> SpanUtils<'a> {
33 err_count: Cell::new(0),
37 pub fn make_filename_string(&self, file: &SourceFile) -> String {
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
46 self.sess.working_dir.0
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()
58 pub fn snippet(&self, span: Span) -> String {
59 match self.sess.source_map().span_to_snippet(span) {
61 Err(_) => String::new(),
65 pub fn retokenise_span(&self, span: Span) -> StringReader<'a> {
66 lexer::StringReader::retokenize(&self.sess.parse_sess, span)
69 pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
70 let mut toks = self.retokenise_span(span);
72 let next = toks.real_token();
73 if next.tok == token::Eof {
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);
86 // let ts = toks.real_token();
87 // if ts.tok == token::Eof {
90 // if ts.tok == token::Not {
91 // let ts = toks.real_token();
92 // if ts.tok.is_ident() {
93 // return Some(ts.sp);
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();
106 // if prev.tok == token::Eof {
109 // let ts = toks.real_token();
110 // if ts.tok == token::Not {
111 // if prev.tok.is_ident() {
112 // return Some(prev.sp);
121 /// Return true if the span is generated code, and
122 /// it is not a subspan of the root callsite.
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 {
131 if !generated_code(span) {
135 //If the span comes from a fake source_file, filter it.
138 .lookup_char_pos(span.lo())
144 macro_rules! filter {
145 ($util: expr, $parent: expr) => {
146 if $util.filter_generated($parent) {