Skip to content

Commit

Permalink
Output stats also when analyzing
Browse files Browse the repository at this point in the history
  • Loading branch information
JustusAdam committed Dec 18, 2024
1 parent 15888c1 commit 4a91946
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target/

/rust-dfpp-patch
flow-graph.json
flow-graph.stat.json

# Local cargo configuration
.cargo/config.toml
Expand Down
89 changes: 63 additions & 26 deletions crates/paralegal-flow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,25 @@ struct Callbacks<'a> {
stats: Stats,
rustc_second_timer: Option<Instant>,
stat_ref: &'a mut Option<AnalyzerStats>,
output_location: &'a mut Option<PathBuf>,
}

struct NoopCallbacks;

impl rustc_driver::Callbacks for NoopCallbacks {}

impl<'a> Callbacks<'a> {
pub fn new(opts: &'static Args, stat_ref: &'a mut Option<AnalyzerStats>) -> Self {
pub fn new(
opts: &'static Args,
stat_ref: &'a mut Option<AnalyzerStats>,
output_location: &'a mut Option<PathBuf>,
) -> Self {
Self {
opts,
stats: Default::default(),
rustc_second_timer: None,
stat_ref,
output_location,
}
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -298,14 +309,21 @@ fn add_to_rustflags(new: impl IntoIterator<Item = String>) -> Result<(), std::en
Ok(())
}

#[derive(strum::AsRefStr, PartialEq, Eq)]
enum CrateHandling {
JustCompile,
CompileAndDump,
Analyze,
}

struct CrateInfo {
name: Option<String>,
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<String>) -> CrateHandling {
fn how_to_handle_this_crate(plugin_args: &Args, compiler_args: &mut Vec<String>) -> CrateInfo {
let crate_name = compiler_args
.iter()
.enumerate()
Expand All @@ -325,8 +343,12 @@ fn how_to_handle_this_crate(plugin_args: &Args, compiler_args: &mut Vec<String>)
);
}

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
Expand All @@ -341,6 +363,12 @@ fn how_to_handle_this_crate(plugin_args: &Args, compiler_args: &mut Vec<String>)
CrateHandling::CompileAndDump
}
_ => CrateHandling::JustCompile,
};

CrateInfo {
name: crate_name,
handling,
is_build_script,
}
}

Expand Down Expand Up @@ -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<dyn rustc_driver::Callbacks + Send>
}
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions crates/paralegal-policy/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ impl<T: NodeExt + Copy> NodeExt for &'_ T {
(*self).predecessors(ctx)
}

#[allow(deprecated)]
fn shortest_path(
self,
to: GlobalNode,
Expand Down
2 changes: 1 addition & 1 deletion crates/paralegal-spdg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4a91946

Please sign in to comment.