Skip to content

Commit c7cb2cf

Browse files
committed
Pretty print json in ui tests
1 parent 65c899e commit c7cb2cf

File tree

8 files changed

+418
-21
lines changed

8 files changed

+418
-21
lines changed

src/librustc/session/config.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl OutputType {
155155
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
156156
pub enum ErrorOutputType {
157157
HumanReadable(ColorConfig),
158-
Json,
158+
Json(bool),
159159
Short(ColorConfig),
160160
}
161161

@@ -1104,6 +1104,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
11041104
"enable ThinLTO when possible"),
11051105
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
11061106
"control whether #[inline] functions are in all cgus"),
1107+
pretty_json_error_format: bool = (false, parse_bool, [UNTRACKED],
1108+
"allow `--error-format=pretty-json` (used for compiletest)"),
11071109
}
11081110

11091111
pub fn default_lib_output() -> CrateType {
@@ -1433,7 +1435,8 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
14331435
let error_format = if matches.opts_present(&["error-format".to_owned()]) {
14341436
match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
14351437
Some("human") => ErrorOutputType::HumanReadable(color),
1436-
Some("json") => ErrorOutputType::Json,
1438+
Some("json") => ErrorOutputType::Json(false),
1439+
Some("pretty-json") => ErrorOutputType::Json(true),
14371440
Some("short") => ErrorOutputType::Short(color),
14381441

14391442
None => ErrorOutputType::HumanReadable(color),
@@ -1474,6 +1477,11 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
14741477

14751478
let debugging_opts = build_debugging_options(matches, error_format);
14761479

1480+
if !debugging_opts.pretty_json_error_format && error_format == ErrorOutputType::Json(true) {
1481+
early_error(ErrorOutputType::Json(false), "--error-format=pretty-json is unstable \
1482+
(use -Zpretty-json-error-format)");
1483+
}
1484+
14771485
let mut output_types = BTreeMap::new();
14781486
if !debugging_opts.parse_only {
14791487
for list in matches.opt_strs("emit") {

src/librustc/session/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl Session {
372372
match self.opts.error_format {
373373
// when outputting JSON for tool consumption, the tool might want
374374
// the duplicates
375-
config::ErrorOutputType::Json => {
375+
config::ErrorOutputType::Json(_) => {
376376
do_method()
377377
},
378378
_ => {
@@ -736,11 +736,11 @@ pub fn build_session_with_codemap(sopts: config::Options,
736736
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => {
737737
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false))
738738
}
739-
(config::ErrorOutputType::Json, None) => {
740-
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone()))
739+
(config::ErrorOutputType::Json(pretty), None) => {
740+
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone(), pretty))
741741
}
742-
(config::ErrorOutputType::Json, Some(dst)) => {
743-
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone()))
742+
(config::ErrorOutputType::Json(pretty), Some(dst)) => {
743+
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone(), pretty))
744744
}
745745
(config::ErrorOutputType::Short(color_config), None) => {
746746
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true))
@@ -918,7 +918,7 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
918918
config::ErrorOutputType::HumanReadable(color_config) => {
919919
Box::new(EmitterWriter::stderr(color_config, None, false))
920920
}
921-
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
921+
config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
922922
config::ErrorOutputType::Short(color_config) => {
923923
Box::new(EmitterWriter::stderr(color_config, None, true))
924924
}
@@ -933,7 +933,7 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
933933
config::ErrorOutputType::HumanReadable(color_config) => {
934934
Box::new(EmitterWriter::stderr(color_config, None, false))
935935
}
936-
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()),
936+
config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
937937
config::ErrorOutputType::Short(color_config) => {
938938
Box::new(EmitterWriter::stderr(color_config, None, true))
939939
}

src/libsyntax/json.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,54 @@ use std::rc::Rc;
3030
use std::io::{self, Write};
3131
use std::vec;
3232

33-
use rustc_serialize::json::as_json;
33+
use rustc_serialize::json::{as_json, as_pretty_json};
3434

3535
pub struct JsonEmitter {
3636
dst: Box<Write + Send>,
3737
registry: Option<Registry>,
3838
cm: Rc<CodeMapper + 'static>,
39+
pretty: bool,
3940
}
4041

4142
impl JsonEmitter {
4243
pub fn stderr(registry: Option<Registry>,
43-
code_map: Rc<CodeMap>) -> JsonEmitter {
44+
code_map: Rc<CodeMap>,
45+
pretty: bool) -> JsonEmitter {
4446
JsonEmitter {
4547
dst: Box::new(io::stderr()),
4648
registry,
4749
cm: code_map,
50+
pretty,
4851
}
4952
}
5053

51-
pub fn basic() -> JsonEmitter {
54+
pub fn basic(pretty: bool) -> JsonEmitter {
5255
let file_path_mapping = FilePathMapping::empty();
53-
JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping)))
56+
JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping)), pretty)
5457
}
5558

5659
pub fn new(dst: Box<Write + Send>,
5760
registry: Option<Registry>,
58-
code_map: Rc<CodeMap>) -> JsonEmitter {
61+
code_map: Rc<CodeMap>,
62+
pretty: bool) -> JsonEmitter {
5963
JsonEmitter {
6064
dst,
6165
registry,
6266
cm: code_map,
67+
pretty,
6368
}
6469
}
6570
}
6671

6772
impl Emitter for JsonEmitter {
6873
fn emit(&mut self, db: &DiagnosticBuilder) {
6974
let data = Diagnostic::from_diagnostic_builder(db, self);
70-
if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
75+
let result = if self.pretty {
76+
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
77+
} else {
78+
writeln!(&mut self.dst, "{}", as_json(&data))
79+
};
80+
if let Err(e) = result {
7181
panic!("failed to print diagnostics: {:?}", e);
7282
}
7383
}

src/test/ui/lint/unused_parens_json_suggestion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: --error-format json
11+
// compile-flags: --error-format pretty-json -Zpretty_json_error_format
1212

1313
// The output for humans should just highlight the whole span without showing
1414
// the suggested replacement, but we also want to test that suggested
Original file line numberDiff line numberDiff line change
@@ -1 +1,91 @@
1-
{"message":"unnecessary parentheses around assigned value","code":{"code":"unused_parens","explanation":null},"level":"warning","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"lint level defined here","code":null,"level":"note","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":847,"byte_end":860,"line_start":19,"line_end":19,"column_start":9,"column_end":22,"is_primary":true,"text":[{"text":"#![warn(unused_parens)]","highlight_start":9,"highlight_end":22}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[],"rendered":null},{"message":"remove these parentheses","code":null,"level":"help","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":"1 / (2 + 3)","expansion":null}],"children":[],"rendered":null}],"rendered":null}
1+
{
2+
"message": "unnecessary parentheses around assigned value",
3+
"code": {
4+
"code": "unused_parens",
5+
"explanation": null
6+
},
7+
"level": "warning",
8+
"spans": [
9+
{
10+
"file_name": "$DIR/unused_parens_json_suggestion.rs",
11+
"byte_start": 1035,
12+
"byte_end": 1048,
13+
"line_start": 24,
14+
"line_end": 24,
15+
"column_start": 14,
16+
"column_end": 27,
17+
"is_primary": true,
18+
"text": [
19+
{
20+
"text": " let _a = (1 / (2 + 3));",
21+
"highlight_start": 14,
22+
"highlight_end": 27
23+
}
24+
],
25+
"label": null,
26+
"suggested_replacement": null,
27+
"expansion": null
28+
}
29+
],
30+
"children": [
31+
{
32+
"message": "lint level defined here",
33+
"code": null,
34+
"level": "note",
35+
"spans": [
36+
{
37+
"file_name": "$DIR/unused_parens_json_suggestion.rs",
38+
"byte_start": 881,
39+
"byte_end": 894,
40+
"line_start": 19,
41+
"line_end": 19,
42+
"column_start": 9,
43+
"column_end": 22,
44+
"is_primary": true,
45+
"text": [
46+
{
47+
"text": "#![warn(unused_parens)]",
48+
"highlight_start": 9,
49+
"highlight_end": 22
50+
}
51+
],
52+
"label": null,
53+
"suggested_replacement": null,
54+
"expansion": null
55+
}
56+
],
57+
"children": [],
58+
"rendered": null
59+
},
60+
{
61+
"message": "remove these parentheses",
62+
"code": null,
63+
"level": "help",
64+
"spans": [
65+
{
66+
"file_name": "$DIR/unused_parens_json_suggestion.rs",
67+
"byte_start": 1035,
68+
"byte_end": 1048,
69+
"line_start": 24,
70+
"line_end": 24,
71+
"column_start": 14,
72+
"column_end": 27,
73+
"is_primary": true,
74+
"text": [
75+
{
76+
"text": " let _a = (1 / (2 + 3));",
77+
"highlight_start": 14,
78+
"highlight_end": 27
79+
}
80+
],
81+
"label": null,
82+
"suggested_replacement": "1 / (2 + 3)",
83+
"expansion": null
84+
}
85+
],
86+
"children": [],
87+
"rendered": null
88+
}
89+
],
90+
"rendered": null
91+
}

src/test/ui/lint/use_suggestion_json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: --error-format json
11+
// compile-flags: --error-format pretty-json -Zpretty_json_error_format
1212

1313
// The output for humans should just highlight the whole span without showing
1414
// the suggested replacement, but we also want to test that suggested

0 commit comments

Comments
 (0)