Skip to content
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

Add -Zerror-metrics=PATH to save diagnostic metadata to disk #119355

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use rustc_span::hygiene::{ExpnKind, MacroKind};
use std::borrow::Cow;
use std::cmp::{max, min, Reverse};
use std::error::Report;
use std::fs::File;
use std::io::prelude::*;
use std::io::{self, IsTerminal};
use std::iter;
Expand Down Expand Up @@ -538,7 +539,7 @@ impl Emitter for EmitterWriter {
&primary_span,
&children,
suggestions,
self.track_diagnostics.then_some(&diag.emitted_at),
&diag.emitted_at,
);
}

Expand Down Expand Up @@ -637,6 +638,7 @@ pub struct EmitterWriter {

macro_backtrace: bool,
track_diagnostics: bool,
metrics: Option<File>,
terminal_url: TerminalUrl,
}

Expand Down Expand Up @@ -666,6 +668,7 @@ impl EmitterWriter {
diagnostic_width: None,
macro_backtrace: false,
track_diagnostics: false,
metrics: None,
terminal_url: TerminalUrl::No,
}
}
Expand Down Expand Up @@ -2079,15 +2082,27 @@ impl EmitterWriter {
span: &MultiSpan,
children: &[SubDiagnostic],
suggestions: &[CodeSuggestion],
emitted_at: Option<&DiagnosticLocation>,
emitted_at_location: &DiagnosticLocation,
) {
let emitted_at = self.track_diagnostics.then_some(emitted_at_location);
let max_line_num_len = if self.ui_testing {
ANONYMIZED_LINE_NUM.len()
} else {
let n = self.get_max_line_num(span, children);
num_decimal_digits(n)
};

if let Some(ref mut file) = &mut self.metrics {
let _ = writeln!(
file,
"{:?},{:?},{},{}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using this custom format when we already have JSON diagnostic output?

Even if we shouldn't be emitting as much info as JSON-formatted diagnostics, we should perhaps make it easier for arbitrary programs to consume by formatting it like JSON.

code,
emitted_at_location,
children.len(),
suggestions.len()
);
}

match self.emit_messages_default_inner(
span,
messages,
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ fn default_track_diagnostic(d: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnosti
pub static TRACK_DIAGNOSTICS: AtomicRef<fn(&mut Diagnostic, &mut dyn FnMut(&mut Diagnostic))> =
AtomicRef::new(&(default_track_diagnostic as _));

#[derive(Copy, Clone, Default)]
#[derive(Clone, Default)]
pub struct DiagCtxtFlags {
/// If false, warning-level lints are suppressed.
/// (rustc: see `--allow warnings` and `--cap-lints`)
Expand All @@ -535,6 +535,8 @@ pub struct DiagCtxtFlags {
pub deduplicate_diagnostics: bool,
/// Track where errors are created. Enabled with `-Ztrack-diagnostics`.
pub track_diagnostics: bool,
/// Track whether error metrics are stored. Enabled with `-Zerror-metrics=path`.
pub metrics: Option<PathBuf>,
}

impl Drop for DiagCtxtInner {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ impl UnstableOptions {
macro_backtrace: self.macro_backtrace,
deduplicate_diagnostics: self.deduplicate_diagnostics,
track_diagnostics: self.track_diagnostics,
metrics: self.error_metrics.clone(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,8 @@ options! {
"emit a section containing stack size metadata (default: no)"),
emit_thin_lto: bool = (true, parse_bool, [TRACKED],
"emit the bc module with thin LTO info (default: yes)"),
error_metrics: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
"stores metrics about the errors being emitted by rustc to disk"),
export_executable_symbols: bool = (false, parse_bool, [TRACKED],
"export symbols from executables, as if they were dynamic libraries"),
extra_const_ub_checks: bool = (false, parse_bool, [TRACKED],
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use std::any::Any;
use std::cell::{self, RefCell};
use std::env;
use std::fmt;
use std::fs::File;
use std::ops::{Div, Mul};
use std::path::{Path, PathBuf};
use std::str::FromStr;
Expand Down Expand Up @@ -1009,6 +1010,12 @@ fn default_emitter(
);
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
} else {
let metrics = sopts.unstable_opts.error_metrics.as_ref().and_then(|path| {
let mut path = path.clone();
std::fs::create_dir_all(&path).ok()?;
path.push(format!("error_metrics_{}", std::process::id()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why PID? Can we use something more ordered such as YYYY-MM-DD-HH-SS-PID?

File::options().create(true).append(true).open(&path).ok()
});
let emitter = EmitterWriter::stderr(color_config, fallback_bundle)
.fluent_bundle(bundle)
.sm(Some(source_map))
Expand All @@ -1018,6 +1025,7 @@ fn default_emitter(
.macro_backtrace(macro_backtrace)
.track_diagnostics(track_diagnostics)
.terminal_url(terminal_url)
.metrics(metrics)
.ignored_directories_in_source_blocks(
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
);
Expand Down