Extend span debugger

This commit is contained in:
Seo Sanghyeon 2014-12-27 21:00:48 +09:00
parent d4da758922
commit 98aeac2930
2 changed files with 51 additions and 5 deletions

View file

@ -138,8 +138,8 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
krate.encode(&mut json).unwrap();
}
if sess.opts.show_span.is_some() {
syntax::show_span::run(sess.diagnostic(), &krate);
if let Some(ref s) = sess.opts.show_span {
syntax::show_span::run(sess.diagnostic(), s.as_slice(), &krate);
}
krate

View file

@ -13,27 +13,73 @@
//! This module shows spans for all expressions in the crate
//! to help with compiler debugging.
use std::str::FromStr;
use ast;
use diagnostic;
use visit;
use visit::Visitor;
enum Mode {
Expression,
Pattern,
Type,
}
impl FromStr for Mode {
fn from_str(s: &str) -> Option<Mode> {
let mode = match s {
"expr" => Mode::Expression,
"pat" => Mode::Pattern,
"ty" => Mode::Type,
_ => return None
};
Some(mode)
}
}
struct ShowSpanVisitor<'a> {
span_diagnostic: &'a diagnostic::SpanHandler,
mode: Mode,
}
impl<'a, 'v> Visitor<'v> for ShowSpanVisitor<'a> {
fn visit_expr(&mut self, e: &ast::Expr) {
self.span_diagnostic.span_note(e.span, "expression");
if let Mode::Expression = self.mode {
self.span_diagnostic.span_note(e.span, "expression");
}
visit::walk_expr(self, e);
}
fn visit_pat(&mut self, p: &ast::Pat) {
if let Mode::Pattern = self.mode {
self.span_diagnostic.span_note(p.span, "pattern");
}
visit::walk_pat(self, p);
}
fn visit_ty(&mut self, t: &ast::Ty) {
if let Mode::Type = self.mode {
self.span_diagnostic.span_note(t.span, "type");
}
visit::walk_ty(self, t);
}
fn visit_mac(&mut self, macro: &ast::Mac) {
visit::walk_mac(self, macro);
}
}
pub fn run(span_diagnostic: &diagnostic::SpanHandler, krate: &ast::Crate) {
let mut v = ShowSpanVisitor { span_diagnostic: span_diagnostic };
pub fn run(span_diagnostic: &diagnostic::SpanHandler,
mode: &str,
krate: &ast::Crate) {
let mode = match mode.parse() {
Some(mode) => mode,
None => return
};
let mut v = ShowSpanVisitor {
span_diagnostic: span_diagnostic,
mode: mode,
};
visit::walk_crate(&mut v, krate);
}