Skip to content

Commit 6af4515

Browse files
committed
Auto merge of #45721 - nikomatsakis:hir-tree, r=arielb1
add -Zunpretty=hir-tree This uses the debug impls to dump the raw HIR. Particularly useful when learning how the compiler works. cc @qmx
2 parents 45594d5 + 61f31fd commit 6af4515

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/librustc_driver/pretty.rs

+42
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub enum PpFlowGraphMode {
7777
pub enum PpMode {
7878
PpmSource(PpSourceMode),
7979
PpmHir(PpSourceMode),
80+
PpmHirTree(PpSourceMode),
8081
PpmFlowGraph(PpFlowGraphMode),
8182
PpmMir,
8283
PpmMirCFG,
@@ -93,6 +94,7 @@ impl PpMode {
9394
PpmSource(PpmExpandedIdentified) |
9495
PpmSource(PpmExpandedHygiene) |
9596
PpmHir(_) |
97+
PpmHirTree(_) |
9698
PpmMir |
9799
PpmMirCFG |
98100
PpmFlowGraph(_) => true,
@@ -125,6 +127,7 @@ pub fn parse_pretty(sess: &Session,
125127
("hir", true) => PpmHir(PpmNormal),
126128
("hir,identified", true) => PpmHir(PpmIdentified),
127129
("hir,typed", true) => PpmHir(PpmTyped),
130+
("hir-tree", true) => PpmHirTree(PpmNormal),
128131
("mir", true) => PpmMir,
129132
("mir-cfg", true) => PpmMirCFG,
130133
("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
@@ -971,6 +974,23 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
971974
})
972975
}
973976

977+
(PpmHirTree(s), None) => {
978+
let out: &mut Write = &mut out;
979+
s.call_with_pp_support_hir(sess,
980+
cstore,
981+
hir_map,
982+
analysis,
983+
resolutions,
984+
arena,
985+
arenas,
986+
output_filenames,
987+
crate_name,
988+
move |_annotation, krate| {
989+
debug!("pretty printing source code {:?}", s);
990+
write!(out, "{:#?}", krate)
991+
})
992+
}
993+
974994
(PpmHir(s), Some(uii)) => {
975995
let out: &mut Write = &mut out;
976996
s.call_with_pp_support_hir(sess,
@@ -1005,6 +1025,28 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
10051025
pp_state.s.eof()
10061026
})
10071027
}
1028+
1029+
(PpmHirTree(s), Some(uii)) => {
1030+
let out: &mut Write = &mut out;
1031+
s.call_with_pp_support_hir(sess,
1032+
cstore,
1033+
hir_map,
1034+
analysis,
1035+
resolutions,
1036+
arena,
1037+
arenas,
1038+
output_filenames,
1039+
crate_name,
1040+
move |_annotation, _krate| {
1041+
debug!("pretty printing source code {:?}", s);
1042+
for node_id in uii.all_matching_node_ids(hir_map) {
1043+
let node = hir_map.get(node_id);
1044+
write!(out, "{:#?}", node)?;
1045+
}
1046+
Ok(())
1047+
})
1048+
}
1049+
10081050
_ => unreachable!(),
10091051
}
10101052
.unwrap();

src/test/run-make/hir-tree/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-include ../tools.mk
2+
3+
# Test that hir-tree output doens't crash and includes
4+
# the string constant we would expect to see.
5+
6+
all:
7+
$(RUSTC) -o $(TMPDIR)/input.hir -Z unstable-options \
8+
--unpretty=hir-tree input.rs
9+
grep '"Hello, Rustaceans!\\n"' $(TMPDIR)/input.hir

src/test/run-make/hir-tree/input.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
fn main() {
12+
println!("Hello, Rustaceans!");
13+
}

0 commit comments

Comments
 (0)