]> git.proxmox.com Git - rustc.git/blame - src/librustc_ast_passes/show_span.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_ast_passes / show_span.rs
CommitLineData
1a4d82fc
JJ
1//! Span debugger
2//!
3//! This module shows spans for all expressions in the crate
4//! to help with compiler debugging.
5
6use std::str::FromStr;
7
74b04a01
XL
8use rustc_ast::ast;
9use rustc_ast::visit;
10use rustc_ast::visit::Visitor;
1a4d82fc
JJ
11
12enum Mode {
13 Expression,
14 Pattern,
15 Type,
16}
17
18impl FromStr for Mode {
85aaf69f
SL
19 type Err = ();
20 fn from_str(s: &str) -> Result<Mode, ()> {
1a4d82fc
JJ
21 let mode = match s {
22 "expr" => Mode::Expression,
23 "pat" => Mode::Pattern,
24 "ty" => Mode::Type,
dfeec247 25 _ => return Err(()),
1a4d82fc 26 };
85aaf69f 27 Ok(mode)
1a4d82fc
JJ
28 }
29}
30
31struct ShowSpanVisitor<'a> {
dfeec247 32 span_diagnostic: &'a rustc_errors::Handler,
1a4d82fc
JJ
33 mode: Mode,
34}
35
476ff2be
SL
36impl<'a> Visitor<'a> for ShowSpanVisitor<'a> {
37 fn visit_expr(&mut self, e: &'a ast::Expr) {
1a4d82fc 38 if let Mode::Expression = self.mode {
9cc50fc6 39 self.span_diagnostic.span_warn(e.span, "expression");
1a4d82fc
JJ
40 }
41 visit::walk_expr(self, e);
42 }
43
476ff2be 44 fn visit_pat(&mut self, p: &'a ast::Pat) {
1a4d82fc 45 if let Mode::Pattern = self.mode {
9cc50fc6 46 self.span_diagnostic.span_warn(p.span, "pattern");
1a4d82fc
JJ
47 }
48 visit::walk_pat(self, p);
49 }
50
476ff2be 51 fn visit_ty(&mut self, t: &'a ast::Ty) {
1a4d82fc 52 if let Mode::Type = self.mode {
9cc50fc6 53 self.span_diagnostic.span_warn(t.span, "type");
1a4d82fc
JJ
54 }
55 visit::walk_ty(self, t);
56 }
57
ba9703b0 58 fn visit_mac(&mut self, mac: &'a ast::MacCall) {
1a4d82fc
JJ
59 visit::walk_mac(self, mac);
60 }
61}
62
dfeec247 63pub fn run(span_diagnostic: &rustc_errors::Handler, mode: &str, krate: &ast::Crate) {
85aaf69f 64 let mode = match mode.parse().ok() {
1a4d82fc 65 Some(mode) => mode,
dfeec247 66 None => return,
1a4d82fc 67 };
dfeec247 68 let mut v = ShowSpanVisitor { span_diagnostic, mode };
1a4d82fc
JJ
69 visit::walk_crate(&mut v, krate);
70}