]> git.lizzy.rs Git - rust.git/blob - src/librustc_save_analysis/span_utils.rs
save-analysis: add `Signature` info to structs
[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::tokenstream::TokenTree;
22 use syntax_pos::*;
23
24 #[derive(Clone)]
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>,
30 }
31
32 impl<'a> SpanUtils<'a> {
33     pub fn new(sess: &'a Session) -> SpanUtils<'a> {
34         SpanUtils {
35             sess: sess,
36             err_count: Cell::new(0),
37         }
38     }
39
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()
44         } else {
45             env::current_dir().unwrap().join(&path).display().to_string()
46         }
47     }
48
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> {
52         match sub_span {
53             None => None,
54             Some(sub) => {
55                 let FileMapAndBytePos {fm, pos} = self.sess.codemap().lookup_byte_offset(span.lo);
56                 let base = pos + fm.start_pos;
57                 Some(Span {
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,
61                 })
62             }
63         }
64     }
65
66     pub fn snippet(&self, span: Span) -> String {
67         match self.sess.codemap().span_to_snippet(span) {
68             Ok(s) => s,
69             Err(_) => String::new(),
70         }
71     }
72
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
77
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
83                           .codemap()
84                           .new_filemap(String::from("<anon-dxr>"), None, self.snippet(span));
85         let s = self.sess;
86         lexer::StringReader::new(s.diagnostic(), filemap)
87     }
88
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")
93     }
94
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;
98
99         let mut toks = self.retokenise_span(span);
100         let mut bracket_count = 0;
101         loop {
102             let ts = toks.real_token();
103             if ts.tok == token::Eof {
104                 return self.make_sub_span(span, result)
105             }
106             if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
107                 result = Some(ts.sp);
108             }
109
110             bracket_count += match ts.tok {
111                 token::Lt => 1,
112                 token::Gt => -1,
113                 token::BinOp(token::Shr) => -2,
114                 _ => 0,
115             }
116         }
117     }
118
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;
123         loop {
124             let ts = toks.real_token();
125             if ts.tok == token::Eof {
126                 return None;
127             }
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));
130             }
131
132             bracket_count += match ts.tok {
133                 token::Lt => 1,
134                 token::Gt => -1,
135                 token::BinOp(token::Shr) => -2,
136                 _ => 0,
137             }
138         }
139     }
140
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 {
150             prev_span = None;
151             let mut next = toks.real_token();
152
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);
156             }
157
158             if bracket_count == 0 && next.tok == token::ModSep {
159                 let old = prev;
160                 prev = next;
161                 next = toks.real_token();
162                 if next.tok == token::Lt && old.tok.is_ident() {
163                     result = Some(old.sp);
164                 }
165             }
166
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,
171                 _ => 0,
172             };
173
174             if prev.tok.is_ident() && bracket_count == 0 {
175                 prev_span = Some(prev.sp);
176             }
177             prev = next;
178         }
179         if result.is_none() && prev_span.is_some() {
180             return self.make_sub_span(span, prev_span);
181         }
182         return self.make_sub_span(span, result);
183     }
184
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;
191
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',
198         // etc.).
199         let mut angle_count = 0;
200         let mut bracket_count = 0;
201         loop {
202             let next = toks.real_token();
203
204             if (next.tok == token::Lt || next.tok == token::Colon) &&
205                angle_count == 0 &&
206                bracket_count == 0 &&
207                prev.tok.is_ident() {
208                 result = Some(prev.sp);
209             }
210
211             if bracket_count == 0 {
212                 angle_count += match prev.tok {
213                     token::Lt => 1,
214                     token::Gt => -1,
215                     token::BinOp(token::Shl) => 2,
216                     token::BinOp(token::Shr) => -2,
217                     _ => 0,
218                 };
219             }
220
221             bracket_count += match prev.tok {
222                 token::OpenDelim(token::Bracket) => 1,
223                 token::CloseDelim(token::Bracket) => -1,
224                 _ => 0,
225             };
226
227             if next.tok == token::Eof {
228                 break;
229             }
230             prev = next;
231         }
232         if angle_count != 0 || bracket_count != 0 {
233             let loc = self.sess.codemap().lookup_char_pos(span.lo);
234             span_bug!(span,
235                       "Mis-counted brackets when breaking path? Parsing '{}' \
236                        in {}, line {}",
237                       self.snippet(span),
238                       loc.file.name,
239                       loc.line);
240         }
241         if result.is_none() && prev.tok.is_ident() && angle_count == 0 {
242             return self.make_sub_span(span, Some(prev.sp));
243         }
244         self.make_sub_span(span, result)
245     }
246
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![];
254
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;
260         loop {
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) {
265                         return vec![];
266                     }
267                     let loc = self.sess.codemap().lookup_char_pos(span.lo);
268                     span_bug!(span,
269                               "Mis-counted brackets when breaking path? \
270                                Parsing '{}' in {}, line {}",
271                               self.snippet(span),
272                               loc.file.name,
273                               loc.line);
274                 }
275                 return result
276             }
277             if (result.len() as isize) == limit {
278                 return result;
279             }
280             bracket_count += match ts.tok {
281                 token::OpenDelim(token::Bracket) => 1,
282                 token::CloseDelim(token::Bracket) => -1,
283                 _ => 0,
284             };
285             if bracket_count > 0 {
286                 continue;
287             }
288             angle_count += match ts.tok {
289                 token::Lt => 1,
290                 token::Gt => -1,
291                 token::BinOp(token::Shl) => 2,
292                 token::BinOp(token::Shr) => -2,
293                 _ => 0,
294             };
295
296             // Ignore the `>::` in `<Type as Trait>::AssocTy`.
297
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;
308                 angle_count += 1;
309             }
310             if ts.tok.is_ident() && angle_count == nesting {
311                 result.push(self.make_sub_span(span, Some(ts.sp)).unwrap());
312             }
313         }
314     }
315
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;
325         for tok in toks {
326             if let TokenTree::Token(_, ref tok) = prev {
327                 angle_count += match *tok {
328                     token::Eof => { return None; }
329                     token::Lt => 1,
330                     token::Gt => -1,
331                     token::BinOp(token::Shl) => 2,
332                     token::BinOp(token::Shr) => -2,
333                     _ => 0,
334                 };
335             }
336             if angle_count > 0 {
337                 prev = tok;
338                 continue;
339             }
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)));
345                 }
346             }
347             prev = tok;
348         }
349         None
350     }
351
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();
355         loop {
356             if prev.tok == token::Eof {
357                 return None;
358             }
359             let next = toks.real_token();
360             if next.tok == tok {
361                 return self.make_sub_span(span, Some(prev.sp));
362             }
363             prev = next;
364         }
365     }
366
367     pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
368         let mut toks = self.retokenise_span(span);
369         loop {
370             let next = toks.real_token();
371             if next.tok == token::Eof {
372                 return None;
373             }
374             if next.tok == tok {
375                 return self.make_sub_span(span, Some(next.sp));
376             }
377         }
378     }
379
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))
382     }
383
384     pub fn sub_span_after_token(&self, span: Span, tok: Token) -> Option<Span> {
385         self.sub_span_after(span, |t| t == tok)
386     }
387
388     fn sub_span_after<F: Fn(Token) -> bool>(&self, span: Span, f: F) -> Option<Span> {
389         let mut toks = self.retokenise_span(span);
390         loop {
391             let ts = toks.real_token();
392             if ts.tok == token::Eof {
393                 return None;
394             }
395             if f(ts.tok) {
396                 let ts = toks.real_token();
397                 if ts.tok == token::Eof {
398                     return None
399                 } else {
400                     return self.make_sub_span(span, Some(ts.sp));
401                 }
402             }
403         }
404     }
405
406
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)
411     }
412
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)
419     }
420
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 {}",
424               kind,
425               self.snippet(span),
426               loc.file.name,
427               loc.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");
431         }
432     }
433
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);
437         loop {
438             let ts = toks.real_token();
439             if ts.tok == token::Eof {
440                 return None;
441             }
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));
446                 } else {
447                     return None;
448                 }
449             }
450         }
451     }
452
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();
457         loop {
458             if prev.tok == token::Eof {
459                 return None;
460             }
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));
465                 } else {
466                     return None;
467                 }
468             }
469             prev = ts;
470         }
471     }
472
473     /// Return true if the span is generated code, and
474     /// it is not a subspan of the root callsite.
475     ///
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.
482                 return true;
483             }
484             return false;
485         }
486         // If sub_span is none, filter out generated code.
487         if sub_span.is_none() {
488             return true;
489         }
490
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() {
493             return true;
494         }
495
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))
500     }
501 }
502
503 macro_rules! filter {
504     ($util: expr, $span: ident, $parent: expr, None) => {
505         if $util.filter_generated($span, $parent) {
506             return None;
507         }
508     };
509     ($util: expr, $span: ident, $parent: expr) => {
510         if $util.filter_generated($span, $parent) {
511             return;
512         }
513     };
514 }