]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/show_span.rs
Rollup merge of #37052 - srinivasreddy:hair_cx, r=pnkfelix
[rust.git] / src / libsyntax / show_span.rs
1 // Copyright 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 //! Span debugger
12 //!
13 //! This module shows spans for all expressions in the crate
14 //! to help with compiler debugging.
15
16 use std::str::FromStr;
17
18 use ast;
19 use errors;
20 use visit;
21 use visit::Visitor;
22
23 enum Mode {
24     Expression,
25     Pattern,
26     Type,
27 }
28
29 impl FromStr for Mode {
30     type Err = ();
31     fn from_str(s: &str) -> Result<Mode, ()> {
32         let mode = match s {
33             "expr" => Mode::Expression,
34             "pat" => Mode::Pattern,
35             "ty" => Mode::Type,
36             _ => return Err(())
37         };
38         Ok(mode)
39     }
40 }
41
42 struct ShowSpanVisitor<'a> {
43     span_diagnostic: &'a errors::Handler,
44     mode: Mode,
45 }
46
47 impl<'a> Visitor<'a> for ShowSpanVisitor<'a> {
48     fn visit_expr(&mut self, e: &'a ast::Expr) {
49         if let Mode::Expression = self.mode {
50             self.span_diagnostic.span_warn(e.span, "expression");
51         }
52         visit::walk_expr(self, e);
53     }
54
55     fn visit_pat(&mut self, p: &'a ast::Pat) {
56         if let Mode::Pattern = self.mode {
57             self.span_diagnostic.span_warn(p.span, "pattern");
58         }
59         visit::walk_pat(self, p);
60     }
61
62     fn visit_ty(&mut self, t: &'a ast::Ty) {
63         if let Mode::Type = self.mode {
64             self.span_diagnostic.span_warn(t.span, "type");
65         }
66         visit::walk_ty(self, t);
67     }
68
69     fn visit_mac(&mut self, mac: &'a ast::Mac) {
70         visit::walk_mac(self, mac);
71     }
72 }
73
74 pub fn run(span_diagnostic: &errors::Handler,
75            mode: &str,
76            krate: &ast::Crate) {
77     let mode = match mode.parse().ok() {
78         Some(mode) => mode,
79         None => return
80     };
81     let mut v = ShowSpanVisitor {
82         span_diagnostic: span_diagnostic,
83         mode: mode,
84     };
85     visit::walk_crate(&mut v, krate);
86 }