]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/span_utils.rs
e06aefd865f1ba6486c1dadf5ab4bb10081f0f84
[rust.git] / src / librustc_save_analysis / span_utils.rs
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.
4 //
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.
10
11 use rustc::session::Session;
12
13 use generated_code;
14
15 use std::cell::Cell;
16 use std::env;
17 use std::path::Path;
18
19 use syntax::ast;
20 use syntax::parse::lexer::{self, Reader, StringReader};
21 use syntax::parse::token::{self, Token};
22 use syntax::symbol::keywords;
23 use syntax_pos::*;
24
25 #[derive(Clone)]
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>,
31 }
32
33 impl<'a> SpanUtils<'a> {
34     pub fn new(sess: &'a Session) -> SpanUtils<'a> {
35         SpanUtils {
36             sess: sess,
37             err_count: Cell::new(0),
38         }
39     }
40
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()
45         } else {
46             env::current_dir().unwrap().join(&path).display().to_string()
47         }
48     }
49
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> {
53         match sub_span {
54             None => None,
55             Some(sub) => {
56                 let FileMapAndBytePos {fm, pos} = self.sess.codemap().lookup_byte_offset(span.lo);
57                 let base = pos + fm.start_pos;
58                 Some(Span {
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,
62                 })
63             }
64         }
65     }
66
67     pub fn snippet(&self, span: Span) -> String {
68         match self.sess.codemap().span_to_snippet(span) {
69             Ok(s) => s,
70             Err(_) => String::new(),
71         }
72     }
73
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
78
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
84                           .codemap()
85                           .new_filemap(String::from("<anon-dxr>"), None, self.snippet(span));
86         let s = self.sess;
87         lexer::StringReader::new(s.diagnostic(), filemap)
88     }
89
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;
93
94         let mut toks = self.retokenise_span(span);
95         let mut bracket_count = 0;
96         loop {
97             let ts = toks.real_token();
98             if ts.tok == token::Eof {
99                 return self.make_sub_span(span, result)
100             }
101             if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
102                 result = Some(ts.sp);
103             }
104
105             bracket_count += match ts.tok {
106                 token::Lt => 1,
107                 token::Gt => -1,
108                 token::BinOp(token::Shr) => -2,
109                 _ => 0,
110             }
111         }
112     }
113
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;
118         loop {
119             let ts = toks.real_token();
120             if ts.tok == token::Eof {
121                 return None;
122             }
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));
125             }
126
127             bracket_count += match ts.tok {
128                 token::Lt => 1,
129                 token::Gt => -1,
130                 token::BinOp(token::Shr) => -2,
131                 _ => 0,
132             }
133         }
134     }
135
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 {
145             prev_span = None;
146             let mut next = toks.real_token();
147
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);
151             }
152
153             if bracket_count == 0 && next.tok == token::ModSep {
154                 let old = prev;
155                 prev = next;
156                 next = toks.real_token();
157                 if next.tok == token::Lt && old.tok.is_ident() {
158                     result = Some(old.sp);
159                 }
160             }
161
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,
166                 _ => 0,
167             };
168
169             if prev.tok.is_ident() && bracket_count == 0 {
170                 prev_span = Some(prev.sp);
171             }
172             prev = next;
173         }
174         if result.is_none() && prev_span.is_some() {
175             return self.make_sub_span(span, prev_span);
176         }
177         return self.make_sub_span(span, result);
178     }
179
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;
186
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',
193         // etc.).
194         let mut angle_count = 0;
195         let mut bracket_count = 0;
196         loop {
197             let next = toks.real_token();
198
199             if (next.tok == token::Lt || next.tok == token::Colon) &&
200                angle_count == 0 &&
201                bracket_count == 0 &&
202                prev.tok.is_ident() {
203                 result = Some(prev.sp);
204             }
205
206             if bracket_count == 0 {
207                 angle_count += match prev.tok {
208                     token::Lt => 1,
209                     token::Gt => -1,
210                     token::BinOp(token::Shl) => 2,
211                     token::BinOp(token::Shr) => -2,
212                     _ => 0,
213                 };
214             }
215
216             bracket_count += match prev.tok {
217                 token::OpenDelim(token::Bracket) => 1,
218                 token::CloseDelim(token::Bracket) => -1,
219                 _ => 0,
220             };
221
222             if next.tok == token::Eof {
223                 break;
224             }
225             prev = next;
226         }
227         if angle_count != 0 || bracket_count != 0 {
228             let loc = self.sess.codemap().lookup_char_pos(span.lo);
229             span_bug!(span,
230                       "Mis-counted brackets when breaking path? Parsing '{}' \
231                        in {}, line {}",
232                       self.snippet(span),
233                       loc.file.name,
234                       loc.line);
235         }
236         if result.is_none() && prev.tok.is_ident() && angle_count == 0 {
237             return self.make_sub_span(span, Some(prev.sp));
238         }
239         self.make_sub_span(span, result)
240     }
241
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![];
249
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;
255         loop {
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) {
260                         return vec![];
261                     }
262                     let loc = self.sess.codemap().lookup_char_pos(span.lo);
263                     span_bug!(span,
264                               "Mis-counted brackets when breaking path? \
265                                Parsing '{}' in {}, line {}",
266                               self.snippet(span),
267                               loc.file.name,
268                               loc.line);
269                 }
270                 return result
271             }
272             if (result.len() as isize) == limit {
273                 return result;
274             }
275             bracket_count += match ts.tok {
276                 token::OpenDelim(token::Bracket) => 1,
277                 token::CloseDelim(token::Bracket) => -1,
278                 _ => 0,
279             };
280             if bracket_count > 0 {
281                 continue;
282             }
283             angle_count += match ts.tok {
284                 token::Lt => 1,
285                 token::Gt => -1,
286                 token::BinOp(token::Shl) => 2,
287                 token::BinOp(token::Shr) => -2,
288                 _ => 0,
289             };
290
291             // Ignore the `>::` in `<Type as Trait>::AssocTy`.
292
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;
303                 angle_count += 1;
304             }
305             if ts.tok.is_ident() && angle_count == nesting {
306                 result.push(self.make_sub_span(span, Some(ts.sp)).unwrap());
307             }
308         }
309     }
310
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();
314         loop {
315             if prev.tok == token::Eof {
316                 return None;
317             }
318             let next = toks.real_token();
319             if next.tok == tok {
320                 return self.make_sub_span(span, Some(prev.sp));
321             }
322             prev = next;
323         }
324     }
325
326     pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
327         let mut toks = self.retokenise_span(span);
328         loop {
329             let next = toks.real_token();
330             if next.tok == token::Eof {
331                 return None;
332             }
333             if next.tok == tok {
334                 return self.make_sub_span(span, Some(next.sp));
335             }
336         }
337     }
338
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))
341     }
342
343     pub fn sub_span_after_token(&self, span: Span, tok: Token) -> Option<Span> {
344         self.sub_span_after(span, |t| t == tok)
345     }
346
347     fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
348         let mut toks = self.retokenise_span(span);
349         loop {
350             let ts = toks.real_token();
351             if ts.tok == token::Eof {
352                 return None;
353             }
354             if f(ts.tok) {
355                 let ts = toks.real_token();
356                 if ts.tok == token::Eof {
357                     return None
358                 } else {
359                     return self.make_sub_span(span, Some(ts.sp));
360                 }
361             }
362         }
363     }
364
365
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)
370     }
371
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)
378     }
379
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 {}",
383               kind,
384               self.snippet(span),
385               loc.file.name,
386               loc.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");
390         }
391     }
392
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);
396         loop {
397             let ts = toks.real_token();
398             if ts.tok == token::Eof {
399                 return None;
400             }
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));
405                 } else {
406                     return None;
407                 }
408             }
409         }
410     }
411
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();
416         loop {
417             if prev.tok == token::Eof {
418                 return None;
419             }
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));
424                 } else {
425                     return None;
426                 }
427             }
428             prev = ts;
429         }
430     }
431
432     /// Return true if the span is generated code, and
433     /// it is not a subspan of the root callsite.
434     ///
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.
441                 return true;
442             }
443             return false;
444         }
445         // If sub_span is none, filter out generated code.
446         if sub_span.is_none() {
447             return true;
448         }
449
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() {
452             return true;
453         }
454
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))
459     }
460 }
461
462 macro_rules! filter {
463     ($util: expr, $span: ident, $parent: expr, None) => {
464         if $util.filter_generated($span, $parent) {
465             return None;
466         }
467     };
468     ($util: expr, $span: ident, $parent: expr) => {
469         if $util.filter_generated($span, $parent) {
470             return;
471         }
472     };
473 }