Skip to content

process_wrapper: Convert non-JSON diagnostics from LLVM to JSON. #3309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 185 additions & 7 deletions util/process_wrapper/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ mod output;
mod rustc;
mod util;

use std::collections::HashMap;
use std::fmt;
use std::fs::{copy, OpenOptions};
use std::io;
use std::process::{exit, Command, ExitStatus, Stdio};

use tinyjson::JsonValue;

use crate::options::options;
use crate::output::{process_output, LineOutput};
use crate::rustc::ErrorFormat;

#[cfg(windows)]
fn status_code(status: ExitStatus, was_killed: bool) -> i32 {
Expand Down Expand Up @@ -69,6 +73,48 @@ macro_rules! log {
};
}

fn json_warning(line: &str) -> JsonValue {
JsonValue::Object(HashMap::from([
(
"$message_type".to_string(),
JsonValue::String("diagnostic".to_string()),
),
("message".to_string(), JsonValue::String(line.to_string())),
("code".to_string(), JsonValue::Null),
(
"level".to_string(),
JsonValue::String("warning".to_string()),
),
("spans".to_string(), JsonValue::Array(Vec::new())),
("children".to_string(), JsonValue::Array(Vec::new())),
("rendered".to_string(), JsonValue::String(line.to_string())),
]))
}

fn process_line(
mut line: String,
quit_on_rmeta: bool,
format: ErrorFormat,
metadata_emitted: &mut bool,
) -> Result<LineOutput, String> {
// LLVM can emit lines that look like the following, and these will be interspersed
// with the regular JSON output. Arguably, rustc should be fixed not to emit lines
// like these (or to convert them to JSON), but for now we convert them to JSON
// ourselves.
if line.contains("is not a recognized feature for this target (ignoring feature)") {
if let Ok(json_str) = json_warning(&line).stringify() {
line = json_str;
} else {
return Ok(LineOutput::Skip);
}
}
if quit_on_rmeta {
rustc::stop_on_rmeta_completion(line, format, metadata_emitted)
} else {
rustc::process_json(line, format)
}
}

fn main() -> Result<(), ProcessWrapperError> {
let opts = options().map_err(|e| ProcessWrapperError(e.to_string()))?;

Expand Down Expand Up @@ -135,13 +181,7 @@ fn main() -> Result<(), ProcessWrapperError> {
&mut child_stderr,
stderr.as_mut(),
output_file.as_mut(),
move |line| {
if quit_on_rmeta {
rustc::stop_on_rmeta_completion(line, format, metadata_emitted)
} else {
rustc::process_json(line, format)
}
},
move |line| process_line(line, quit_on_rmeta, format, metadata_emitted),
);
if me {
// If recv returns Ok(), a signal was sent in this channel so we should terminate the child process.
Expand Down Expand Up @@ -188,3 +228,141 @@ fn main() -> Result<(), ProcessWrapperError> {

exit(code)
}

#[cfg(test)]
mod test {
use super::*;

fn parse_json(json_str: &str) -> Result<JsonValue, String> {
json_str.parse::<JsonValue>().map_err(|e| e.to_string())
}

#[test]
fn test_process_line_diagnostic_json() -> Result<(), String> {
let mut metadata_emitted = false;
let LineOutput::Message(msg) = process_line(
r#"
{
"$message_type": "diagnostic",
"rendered": "Diagnostic message"
}
"#
.to_string(),
false,
ErrorFormat::Json,
&mut metadata_emitted,
)?
else {
return Err("Expected a LineOutput::Message".to_string());
};
assert_eq!(
parse_json(&msg)?,
parse_json(
r#"
{
"$message_type": "diagnostic",
"rendered": "Diagnostic message"
}
"#
)?
);
Ok(())
}

#[test]
fn test_process_line_diagnostic_rendered() -> Result<(), String> {
let mut metadata_emitted = false;
let LineOutput::Message(msg) = process_line(
r#"
{
"$message_type": "diagnostic",
"rendered": "Diagnostic message"
}
"#
.to_string(),
/*quit_on_rmeta=*/ false,
ErrorFormat::Rendered,
&mut metadata_emitted,
)?
else {
return Err("Expected a LineOutput::Message".to_string());
};
assert_eq!(msg, "Diagnostic message");
Ok(())
}

#[test]
fn test_process_line_llvm_feature_warning() -> Result<(), String> {
let mut metadata_emitted = false;
let LineOutput::Message(msg) = process_line(
"'+zaamo' is not a recognized feature for this target (ignoring feature)".to_string(),
/*quit_on_rmeta=*/ false,
ErrorFormat::Json,
&mut metadata_emitted,
)?
else {
return Err("Expected a LineOutput::Message".to_string());
};
assert_eq!(
parse_json(&msg)?,
parse_json(
r#"
{
"$message_type": "diagnostic",
"message": "'+zaamo' is not a recognized feature for this target (ignoring feature)",
"code": null,
"level": "warning",
"spans": [],
"children": [],
"rendered": "'+zaamo' is not a recognized feature for this target (ignoring feature)"
}
"#
)?
);
Ok(())
}

#[test]
fn test_process_line_emit_link() -> Result<(), String> {
let mut metadata_emitted = false;
assert!(matches!(
process_line(
r#"
{
"$message_type": "artifact",
"emit": "link"
}
"#
.to_string(),
/*quit_on_rmeta=*/ true,
ErrorFormat::Rendered,
&mut metadata_emitted,
)?,
LineOutput::Skip
));
assert!(!metadata_emitted);
Ok(())
}

#[test]
fn test_process_line_emit_metadata() -> Result<(), String> {
let mut metadata_emitted = false;
assert!(matches!(
process_line(
r#"
{
"$message_type": "artifact",
"emit": "metadata"
}
"#
.to_string(),
/*quit_on_rmeta=*/ true,
ErrorFormat::Rendered,
&mut metadata_emitted,
)?,
LineOutput::Terminate
));
assert!(metadata_emitted);
Ok(())
}
}