Skip to content

Commit 0dd0742

Browse files
committed
rollup merge of #20258: sanxiyn/show-span-2
2 parents ba2b79c + 98aeac2 commit 0dd0742

File tree

5 files changed

+64
-12
lines changed

5 files changed

+64
-12
lines changed

src/librustc/session/config.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub struct Options {
105105
pub prints: Vec<PrintRequest>,
106106
pub cg: CodegenOptions,
107107
pub color: ColorConfig,
108+
pub show_span: Option<String>,
108109
pub externs: HashMap<String, Vec<String>>,
109110
pub crate_name: Option<String>,
110111
/// An optional name to use as the crate for std during std injection,
@@ -211,6 +212,7 @@ pub fn basic_options() -> Options {
211212
prints: Vec::new(),
212213
cg: basic_codegen_options(),
213214
color: Auto,
215+
show_span: None,
214216
externs: HashMap::new(),
215217
crate_name: None,
216218
alt_std_name: None,
@@ -259,7 +261,6 @@ debugging_opts! {
259261
BORROWCK_STATS,
260262
NO_LANDING_PADS,
261263
DEBUG_LLVM,
262-
SHOW_SPAN,
263264
COUNT_TYPE_SIZES,
264265
META_STATS,
265266
GC,
@@ -299,7 +300,6 @@ pub fn debugging_opts_map() -> Vec<(&'static str, &'static str, u64)> {
299300
("no-landing-pads", "omit landing pads for unwinding",
300301
NO_LANDING_PADS),
301302
("debug-llvm", "enable debug output from LLVM", DEBUG_LLVM),
302-
("show-span", "show spans for compiler debugging", SHOW_SPAN),
303303
("count-type-sizes", "count the sizes of aggregate types",
304304
COUNT_TYPE_SIZES),
305305
("meta-stats", "gather metadata statistics", META_STATS),
@@ -823,6 +823,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
823823
`flowgraph=<nodeid>` (graphviz formatted flowgraph for node), or
824824
`everybody_loops` (all function bodies replaced with `loop {}`).",
825825
"TYPE"),
826+
opt::opt_u("", "show-span", "Show spans for compiler debugging", "expr|pat|ty"),
826827
opt::flagopt("", "dep-info",
827828
"Output dependency info to <filename> after compiling, \
828829
in a format suitable for use by Makefiles", "FILENAME"),
@@ -1143,6 +1144,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
11431144
prints: prints,
11441145
cg: cg,
11451146
color: color,
1147+
show_span: None,
11461148
externs: externs,
11471149
crate_name: crate_name,
11481150
alt_std_name: None,

src/librustc/session/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ impl Session {
201201
pub fn no_landing_pads(&self) -> bool {
202202
self.debugging_opt(config::NO_LANDING_PADS)
203203
}
204-
pub fn show_span(&self) -> bool {
205-
self.debugging_opt(config::SHOW_SPAN)
204+
pub fn unstable_options(&self) -> bool {
205+
self.debugging_opt(config::UNSTABLE_OPTIONS)
206206
}
207207
pub fn print_enum_sizes(&self) -> bool {
208208
self.debugging_opt(config::PRINT_ENUM_SIZES)

src/librustc_driver/driver.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
146146
println!("{}", json::as_json(&krate));
147147
}
148148

149-
if sess.show_span() {
150-
syntax::show_span::run(sess.diagnostic(), &krate);
149+
if let Some(ref s) = sess.opts.show_span {
150+
syntax::show_span::run(sess.diagnostic(), s.as_slice(), &krate);
151151
}
152152

153153
krate
@@ -572,7 +572,7 @@ pub fn stop_after_phase_1(sess: &Session) -> bool {
572572
debug!("invoked with --parse-only, returning early from compile_input");
573573
return true;
574574
}
575-
if sess.show_span() {
575+
if sess.opts.show_span.is_some() {
576576
return true;
577577
}
578578
return sess.opts.debugging_opts & config::AST_JSON_NOEXPAND != 0;

src/librustc_driver/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn run_compiler(args: &[String]) {
134134
_ => early_error("multiple input filenames provided")
135135
};
136136

137-
let sess = build_session(sopts, input_file_path, descriptions);
137+
let mut sess = build_session(sopts, input_file_path, descriptions);
138138
let cfg = config::build_configuration(&sess);
139139
if print_crate_info(&sess, Some(&input), &odir, &ofile) {
140140
return
@@ -145,7 +145,7 @@ fn run_compiler(args: &[String]) {
145145
pretty::parse_pretty(&sess, a.as_slice(), false)
146146
});
147147
let pretty = if pretty.is_none() &&
148-
sess.debugging_opt(config::UNSTABLE_OPTIONS) {
148+
sess.unstable_options() {
149149
matches.opt_str("xpretty").map(|a| {
150150
// extended with unstable pretty-print variants
151151
pretty::parse_pretty(&sess, a.as_slice(), true)
@@ -162,6 +162,10 @@ fn run_compiler(args: &[String]) {
162162
None => {/* continue */ }
163163
}
164164

165+
if sess.unstable_options() {
166+
sess.opts.show_span = matches.opt_str("show-span");
167+
}
168+
165169
let r = matches.opt_strs("Z");
166170
if r.contains(&("ls".to_string())) {
167171
match input {

src/libsyntax/show_span.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,73 @@
1313
//! This module shows spans for all expressions in the crate
1414
//! to help with compiler debugging.
1515
16+
use std::str::FromStr;
17+
1618
use ast;
1719
use diagnostic;
1820
use visit;
1921
use visit::Visitor;
2022

23+
enum Mode {
24+
Expression,
25+
Pattern,
26+
Type,
27+
}
28+
29+
impl FromStr for Mode {
30+
fn from_str(s: &str) -> Option<Mode> {
31+
let mode = match s {
32+
"expr" => Mode::Expression,
33+
"pat" => Mode::Pattern,
34+
"ty" => Mode::Type,
35+
_ => return None
36+
};
37+
Some(mode)
38+
}
39+
}
40+
2141
struct ShowSpanVisitor<'a> {
2242
span_diagnostic: &'a diagnostic::SpanHandler,
43+
mode: Mode,
2344
}
2445

2546
impl<'a, 'v> Visitor<'v> for ShowSpanVisitor<'a> {
2647
fn visit_expr(&mut self, e: &ast::Expr) {
27-
self.span_diagnostic.span_note(e.span, "expression");
48+
if let Mode::Expression = self.mode {
49+
self.span_diagnostic.span_note(e.span, "expression");
50+
}
2851
visit::walk_expr(self, e);
2952
}
3053

54+
fn visit_pat(&mut self, p: &ast::Pat) {
55+
if let Mode::Pattern = self.mode {
56+
self.span_diagnostic.span_note(p.span, "pattern");
57+
}
58+
visit::walk_pat(self, p);
59+
}
60+
61+
fn visit_ty(&mut self, t: &ast::Ty) {
62+
if let Mode::Type = self.mode {
63+
self.span_diagnostic.span_note(t.span, "type");
64+
}
65+
visit::walk_ty(self, t);
66+
}
67+
3168
fn visit_mac(&mut self, macro: &ast::Mac) {
3269
visit::walk_mac(self, macro);
3370
}
3471
}
3572

36-
pub fn run(span_diagnostic: &diagnostic::SpanHandler, krate: &ast::Crate) {
37-
let mut v = ShowSpanVisitor { span_diagnostic: span_diagnostic };
73+
pub fn run(span_diagnostic: &diagnostic::SpanHandler,
74+
mode: &str,
75+
krate: &ast::Crate) {
76+
let mode = match mode.parse() {
77+
Some(mode) => mode,
78+
None => return
79+
};
80+
let mut v = ShowSpanVisitor {
81+
span_diagnostic: span_diagnostic,
82+
mode: mode,
83+
};
3884
visit::walk_crate(&mut v, krate);
3985
}

0 commit comments

Comments
 (0)