Skip to content

Commit fbffa94

Browse files
committed
rustc_driver: add --xpretty typed,unsuffixed_literals (integers and floats).
1 parent 95593a6 commit fbffa94

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

src/librustc_driver/pretty.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use syntax::ptr::P;
3434

3535
use graphviz as dot;
3636

37+
use std::borrow::{Cow, IntoCow};
3738
use std::old_io::{self, BufReader};
3839
use std::option;
3940
use std::str::FromStr;
@@ -44,6 +45,7 @@ pub enum PpSourceMode {
4445
PpmEveryBodyLoops,
4546
PpmExpanded,
4647
PpmTyped,
48+
PpmTypedUnsuffixedLiterals,
4749
PpmIdentified,
4850
PpmExpandedIdentified,
4951
PpmExpandedHygiene,
@@ -76,6 +78,7 @@ pub fn parse_pretty(sess: &Session,
7678
("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
7779
("expanded", _) => PpmSource(PpmExpanded),
7880
("typed", _) => PpmSource(PpmTyped),
81+
("typed,unsuffixed_literals", true) => PpmSource(PpmTypedUnsuffixedLiterals),
7982
("expanded,identified", _) => PpmSource(PpmExpandedIdentified),
8083
("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene),
8184
("identified", _) => PpmSource(PpmIdentified),
@@ -85,7 +88,8 @@ pub fn parse_pretty(sess: &Session,
8588
if extended {
8689
sess.fatal(&format!(
8790
"argument to `xpretty` must be one of `normal`, \
88-
`expanded`, `flowgraph[,unlabelled]=<nodeid>`, `typed`, `identified`, \
91+
`expanded`, `flowgraph[,unlabelled]=<nodeid>`, `typed`, \
92+
`typed,unsuffixed_literals`, `identified`, \
8993
`expanded,identified`, or `everybody_loops`; got {}", name));
9094
} else {
9195
sess.fatal(&format!(
@@ -163,6 +167,7 @@ impl pprust::PpAnn for HygieneAnnotation {
163167
}
164168
}
165169

170+
#[derive(Copy)]
166171
struct TypedAnnotation<'a, 'tcx: 'a> {
167172
tcx: &'a ty::ctxt<'tcx>,
168173
}
@@ -193,6 +198,44 @@ impl<'a, 'tcx> pprust::PpAnn for TypedAnnotation<'a, 'tcx> {
193198
}
194199
}
195200

201+
struct UnsuffixedLitAnnotation<A> {
202+
ann: A
203+
}
204+
205+
impl<A: pprust::PpAnn> pprust::PpAnn for UnsuffixedLitAnnotation<A> {
206+
fn pre(&self,
207+
s: &mut pprust::State,
208+
node: pprust::AnnNode) -> old_io::IoResult<()> {
209+
self.ann.pre(s, node)
210+
}
211+
fn post(&self,
212+
s: &mut pprust::State,
213+
node: pprust::AnnNode) -> old_io::IoResult<()> {
214+
self.ann.post(s, node)
215+
}
216+
fn literal_to_string<'a>(&self,
217+
s: &mut pprust::State,
218+
lit: &'a ast::Lit) -> Cow<'a, str> {
219+
match lit.node {
220+
ast::LitInt(i, t) => {
221+
match t {
222+
ast::UnsignedIntLit(_) |
223+
ast::SignedIntLit(_, ast::Plus) |
224+
ast::UnsuffixedIntLit(ast::Plus) => {
225+
format!("{}", i)
226+
}
227+
ast::SignedIntLit(_, ast::Minus) |
228+
ast::UnsuffixedIntLit(ast::Minus) => {
229+
format!("-{}", i)
230+
}
231+
}.into_cow()
232+
}
233+
ast::LitFloat(ref f, _) => f.into_cow(),
234+
_ => self.ann.literal_to_string(s, lit)
235+
}
236+
}
237+
}
238+
196239
fn gather_flowgraph_variants(sess: &Session) -> Vec<borrowck_dot::Variant> {
197240
let print_loans = sess.opts.debugging_opts.flowgraph_print_loans;
198241
let print_moves = sess.opts.debugging_opts.flowgraph_print_moves;
@@ -374,6 +417,7 @@ pub fn printing_phase<'a, 'b>(control: &'a mut driver::CompileController<'b>,
374417
}
375418

376419
PpmSource(PpmTyped) |
420+
PpmSource(PpmTypedUnsuffixedLiterals) |
377421
PpmFlowGraph(_) => {
378422
&mut control.after_analysis
379423
}
@@ -405,7 +449,10 @@ pub fn print_from_phase(state: driver::CompileState,
405449
PpmSource(mode) => {
406450
debug!("pretty printing source code {:?}", mode);
407451

408-
let typed_annotation = state.tcx.map(|tcx| TypedAnnotation { tcx: tcx });
452+
let typed_ann = state.tcx.map(|tcx| TypedAnnotation { tcx: tcx });
453+
let typed_unsuffixed_lit_ann = typed_ann.map(|ann| {
454+
UnsuffixedLitAnnotation { ann: ann }
455+
});
409456
let annotation: &pprust::PpAnn = match mode {
410457
PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
411458
NO_ANNOTATION
@@ -417,7 +464,11 @@ pub fn print_from_phase(state: driver::CompileState,
417464
HYGIENE_ANNOTATION
418465
}
419466
PpmTyped => {
420-
typed_annotation.as_ref().expect("--pretty=typed missing type context")
467+
typed_ann.as_ref().expect("--pretty=typed missing type context")
468+
}
469+
PpmTypedUnsuffixedLiterals => {
470+
typed_unsuffixed_lit_ann.as_ref().expect(
471+
"--xpretty=typed,unsuffixed_literals missing type context")
421472
}
422473
};
423474

0 commit comments

Comments
 (0)