From 4a919466e3fa56f0f453ff637db7f218b39c1688 Mon Sep 17 00:00:00 2001 From: Justus Adam Date: Wed, 18 Dec 2024 18:04:49 -0500 Subject: [PATCH] Output stats also when analyzing --- .gitignore | 1 + crates/paralegal-flow/src/lib.rs | 89 ++++++++++++++++++-------- crates/paralegal-policy/src/context.rs | 1 + crates/paralegal-spdg/src/lib.rs | 2 +- 4 files changed, 66 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index b0609954a5..9d2daec0dc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ target/ /rust-dfpp-patch flow-graph.json +flow-graph.stat.json # Local cargo configuration .cargo/config.toml diff --git a/crates/paralegal-flow/src/lib.rs b/crates/paralegal-flow/src/lib.rs index ef407247b7..819223fb20 100644 --- a/crates/paralegal-flow/src/lib.rs +++ b/crates/paralegal-flow/src/lib.rs @@ -129,6 +129,7 @@ struct Callbacks<'a> { stats: Stats, rustc_second_timer: Option, stat_ref: &'a mut Option, + output_location: &'a mut Option, } struct NoopCallbacks; @@ -136,12 +137,17 @@ struct NoopCallbacks; impl rustc_driver::Callbacks for NoopCallbacks {} impl<'a> Callbacks<'a> { - pub fn new(opts: &'static Args, stat_ref: &'a mut Option) -> Self { + pub fn new( + opts: &'static Args, + stat_ref: &'a mut Option, + output_location: &'a mut Option, + ) -> Self { Self { opts, stats: Default::default(), rustc_second_timer: None, stat_ref, + output_location, } } } @@ -259,6 +265,11 @@ impl<'a> Callbacks<'a> { println!("Analysis finished with timing: {}", self.stats); assert!(self.stat_ref.replace(stats).is_none()); + + assert!(self + .output_location + .replace(intermediate_out_dir(tcx, INTERMEDIATE_STAT_EXT)) + .is_none()); anyhow::Ok(self.opts.abort_after_analysis()) }) .unwrap(); @@ -298,14 +309,21 @@ fn add_to_rustflags(new: impl IntoIterator) -> Result<(), std::en Ok(()) } +#[derive(strum::AsRefStr, PartialEq, Eq)] enum CrateHandling { JustCompile, CompileAndDump, Analyze, } +struct CrateInfo { + name: Option, + handling: CrateHandling, + is_build_script: bool, +} + /// Also adds and additional features required by the Paralegal build config -fn how_to_handle_this_crate(plugin_args: &Args, compiler_args: &mut Vec) -> CrateHandling { +fn how_to_handle_this_crate(plugin_args: &Args, compiler_args: &mut Vec) -> CrateInfo { let crate_name = compiler_args .iter() .enumerate() @@ -325,8 +343,12 @@ fn how_to_handle_this_crate(plugin_args: &Args, compiler_args: &mut Vec) ); } - match &crate_name { - Some(krate) if krate == "build_script_build" => CrateHandling::JustCompile, + let is_build_script = matches!( + &crate_name, + Some(krate) if krate == "build_script_build" + ); + let handling = match &crate_name { + _ if is_build_script => CrateHandling::JustCompile, Some(krate) if matches!( plugin_args @@ -341,6 +363,12 @@ fn how_to_handle_this_crate(plugin_args: &Args, compiler_args: &mut Vec) CrateHandling::CompileAndDump } _ => CrateHandling::JustCompile, + }; + + CrateInfo { + name: crate_name, + handling, + is_build_script, } } @@ -451,9 +479,14 @@ impl rustc_plugin::RustcPlugin for DfppPlugin { let mut stat_ref = None; let out_path = plugin_args.result_path().to_owned(); - let handling = how_to_handle_this_crate(&plugin_args, &mut compiler_args); + let info = how_to_handle_this_crate(&plugin_args, &mut compiler_args); + println!( + "Handling crate {} as {}", + info.name.as_ref().map_or("unnamed", String::as_str), + info.handling.as_ref() + ); let result = { - let mut callbacks = match handling { + let mut callbacks = match info.handling { CrateHandling::JustCompile => { Box::new(NoopCallbacks) as Box } @@ -493,31 +526,35 @@ impl rustc_plugin::RustcPlugin for DfppPlugin { "Arguments: {}", Print(|f| write_sep(f, " ", &compiler_args, Display::fmt)) ); - Box::new(Callbacks::new(opts, &mut stat_ref)) + Box::new(Callbacks::new( + opts, + &mut stat_ref, + &mut output_path_location, + )) } }; rustc_driver::RunCompiler::new(&compiler_args, callbacks.as_mut()).run() }; - match handling { - CrateHandling::CompileAndDump => { - let filepath = output_path_location - .take() - .expect("Output path should be set"); - dump_stats.total_time = start.elapsed(); - let out = BufWriter::new(File::create(filepath).unwrap()); - serde_json::to_writer(out, &dump_stats).unwrap(); - } - CrateHandling::Analyze => { - let out = - BufWriter::new(File::create(out_path.with_extension(STAT_FILE_EXT)).unwrap()); - let mut stat = stat_ref.take().expect("stats must have been set"); - let self_time = start.elapsed(); - // See ana/mod.rs for explanations as to these adjustments. - stat.self_time = self_time; - serde_json::to_writer(out, &stat).unwrap(); - } - _ => (), + if info.handling != CrateHandling::JustCompile { + let filepath = output_path_location + .take() + .expect("Output path should be set"); + dump_stats.total_time = start.elapsed(); + println!( + "Writing statistics for dependency to {}", + filepath.display() + ); + let out = BufWriter::new(File::create(filepath).unwrap()); + serde_json::to_writer(out, &dump_stats).unwrap(); + } + if info.handling == CrateHandling::Analyze { + let out = BufWriter::new(File::create(out_path.with_extension(STAT_FILE_EXT)).unwrap()); + let mut stat = stat_ref.take().expect("stats must have been set"); + let self_time = start.elapsed(); + // See ana/mod.rs for explanations as to these adjustments. + stat.self_time = self_time; + serde_json::to_writer(out, &stat).unwrap(); } result } diff --git a/crates/paralegal-policy/src/context.rs b/crates/paralegal-policy/src/context.rs index a26881edcf..8f6a36e31f 100644 --- a/crates/paralegal-policy/src/context.rs +++ b/crates/paralegal-policy/src/context.rs @@ -1001,6 +1001,7 @@ impl NodeExt for &'_ T { (*self).predecessors(ctx) } + #[allow(deprecated)] fn shortest_path( self, to: GlobalNode, diff --git a/crates/paralegal-spdg/src/lib.rs b/crates/paralegal-spdg/src/lib.rs index 1d82970d9b..201e6e344a 100644 --- a/crates/paralegal-spdg/src/lib.rs +++ b/crates/paralegal-spdg/src/lib.rs @@ -385,7 +385,7 @@ pub struct AnalyzerStats { /// The number of functions we produced a PDG for pub pdg_functions: u32, /// The lines of code corresponding to the functions from - /// [`Self::dedup_functions`]. + /// [`Self::pdg_functions`]. pub pdg_locs: u32, /// The number of functions we produced PDGs for or we inspected to check /// for markers.