This commit is contained in:
nora 2022-12-17 21:34:04 +01:00
parent 002bad34ae
commit a9e488f3e3
11 changed files with 433 additions and 40 deletions

80
src/processor/files.rs Normal file
View file

@ -0,0 +1,80 @@
use anyhow::{Context, Result};
use std::{
fs,
path::{Path, PathBuf},
};
#[derive(Debug)]
pub struct SourceFile {
pub path: PathBuf,
}
#[derive(Default)]
pub struct Changes {
any_change: bool,
}
pub struct FileChange<'a, 'b> {
pub path: &'a Path,
content: String,
changes: &'b mut Changes,
has_written_change: bool,
}
impl FileChange<'_, '_> {
pub fn before_content(&self) -> &str {
&self.content
}
pub fn write(&mut self, new: &str) -> Result<()> {
self.has_written_change = true;
fs::write(&self.path, new).with_context(|| format!("writing file {}", self.path.display()))
}
pub fn rollback(mut self) -> Result<()> {
assert!(self.has_written_change);
self.has_written_change = false;
fs::write(self.path, &self.content)
.with_context(|| format!("writing file {}", self.path.display()))
}
pub fn commit(mut self) {
assert!(self.has_written_change);
self.has_written_change = false;
self.changes.any_change = true;
}
}
impl Drop for FileChange<'_, '_> {
fn drop(&mut self) {
if self.has_written_change {
fs::write(&self.path, self.before_content()).ok();
if !std::thread::panicking() {
panic!("File contains unsaved changes!");
}
}
}
}
impl SourceFile {
pub fn try_change<'file, 'change>(
&'file self,
changes: &'change mut Changes,
) -> Result<FileChange<'file, 'change>> {
let path = &self.path;
Ok(FileChange {
path,
changes,
has_written_change: false,
content: fs::read_to_string(path)
.with_context(|| format!("opening file {}", path.display()))?,
})
}
}
impl Changes {
pub fn had_changes(&self) -> bool {
self.any_change
}
}

115
src/processor/mod.rs Normal file
View file

@ -0,0 +1,115 @@
mod files;
mod reaper;
use std::{ffi::OsStr, path::Path};
use anyhow::{ensure, Context, Result};
use crate::{build::Build, processor::files::Changes, Options};
use self::files::SourceFile;
pub trait Processor {
fn process_file(&mut self, krate: &mut syn::File, checker: &mut ProcessChecker) -> bool;
fn name(&self) -> &'static str;
}
#[derive(Debug)]
pub struct Minimizer {
files: Vec<SourceFile>,
build: Build,
no_verify: bool,
}
impl Minimizer {
pub fn new_glob_dir(path: &Path, build: Build, options: &Options) -> Self {
let walk = walkdir::WalkDir::new(path);
let files = walk
.into_iter()
.filter_map(|entry| match entry {
Ok(entry) => Some(entry),
Err(err) => {
eprintln!("WARN: Error in walkdir: {err}");
None
}
})
.filter(|entry| entry.path().extension() == Some(OsStr::new("rs")))
.map(|entry| SourceFile {
path: entry.into_path(),
})
.collect();
Self {
files,
build,
no_verify: options.no_verify,
}
}
pub fn run_passes<'a>(
&mut self,
passes: impl IntoIterator<Item = Box<dyn Processor>>,
) -> Result<()> {
let inital_build = self.build.build()?;
println!("Initial build: {}", inital_build);
if !self.no_verify {
ensure!(
inital_build.reproduces_issue(),
"Initial build must reproduce issue"
);
}
for mut pass in passes {
'pass: loop {
println!("Starting a round of {}", pass.name());
let mut changes = Changes::default();
for file in &self.files {
let file_display = file.path.display();
let mut change = file.try_change(&mut changes)?;
let mut krate = syn::parse_file(change.before_content())
.with_context(|| format!("parsing file {file_display}"))?;
let has_made_change = pass.process_file(&mut krate, &mut ProcessChecker {});
if has_made_change {
let result = prettyplease::unparse(&krate);
change.write(&result)?;
let after = self.build.build()?;
println!("{file_display}: After {}: {after}", pass.name());
if after.reproduces_issue() {
change.commit();
} else {
change.rollback()?;
}
} else {
println!("{file_display}: After {}: no change", pass.name());
}
}
if !changes.had_changes() {
println!("Finished {}", pass.name());
break 'pass;
}
}
}
Ok(())
}
}
pub struct ProcessChecker {}
impl ProcessChecker {
pub fn can_process(&mut self, _: &[String]) -> bool {
true
}
}

229
src/processor/reaper.rs Normal file
View file

@ -0,0 +1,229 @@
//! Deletes dead code.
use super::{files::Changes, Minimizer, Processor};
use anyhow::{ensure, Context, Result};
use proc_macro2::Span;
use rustfix::{diagnostics::Diagnostic, Suggestion};
use std::{collections::HashMap, ops::Range, path::Path};
use syn::{visit_mut::VisitMut, ImplItem, Item};
fn file_for_suggestion(suggestion: &Suggestion) -> &str {
&suggestion.solutions[0].replacements[0].snippet.file_name
}
impl Minimizer {
pub fn delete_dead_code(&mut self) -> Result<()> {
let inital_build = self.build.build()?;
println!("Before reaper: {}", inital_build);
if !self.no_verify {
ensure!(
inital_build.reproduces_issue(),
"Initial build must reproduce issue"
);
}
let (diags, suggestions) = self
.build
.get_suggestions()
.context("getting suggestions from rustc")?;
let mut suggestions_for_file = HashMap::<_, Vec<_>>::new();
for suggestion in &suggestions {
suggestions_for_file
.entry(file_for_suggestion(suggestion))
.or_default()
.push(suggestion);
}
// Always unconditionally apply unused imports.
self.apply_unused_imports(&suggestions_for_file)?;
self.run_passes([Box::new(DeleteUnusedFunctions(diags)) as Box<dyn Processor>])
.context("deleting unused functions")?;
Ok(())
}
fn apply_unused_imports<'a>(
&mut self,
suggestions: &HashMap<&str, Vec<&Suggestion>>,
) -> Result<()> {
for (file, suggestions) in suggestions {
let file = self
.files
.iter()
.find(|source| source.path == Path::new(file))
.expect("unknown file");
let mut changes = &mut Changes::default();
let mut change = file.try_change(&mut changes)?;
let desired_suggestions = suggestions
.iter()
.filter(|sugg| sugg.message.contains("unused import"))
.cloned()
.cloned()
.collect::<Vec<_>>();
let result = rustfix::apply_suggestions(change.before_content(), &desired_suggestions)?;
change.write(&result)?;
let after = self.build.build()?;
println!("{}: After reaper: {after}", file.path.display());
if after.reproduces_issue() {
change.commit();
} else {
change.rollback()?;
}
}
Ok(())
}
}
struct DeleteUnusedFunctions(Vec<Diagnostic>);
impl Processor for DeleteUnusedFunctions {
fn process_file(&mut self, krate: &mut syn::File, _: &mut super::ProcessChecker) -> bool {
let mut visitor = FindUnusedFunction::new(self.0.iter());
visitor.visit_file_mut(krate);
visitor.has_change
}
fn name(&self) -> &'static str {
"delete-unused-functions"
}
}
#[derive(Debug)]
struct Unused {
name: String,
line: usize,
column: Range<usize>,
}
impl Unused {
fn span_matches(&self, ident_span: Span) -> bool {
let (start, end) = (ident_span.start(), ident_span.end());
assert_eq!(start.line, end.line);
let line_matches = self.line == start.line;
let column_matches = self.column.start <= start.column && self.column.end >= end.column;
line_matches && column_matches
}
}
struct FindUnusedFunction {
unused_functions: Vec<Unused>,
has_change: bool,
}
impl FindUnusedFunction {
fn new<'a>(diags: impl Iterator<Item = &'a Diagnostic>) -> Self {
let unused_functions = diags
.filter_map(|diag| {
// FIXME: use `code` correctly
if diag
.code
.as_ref()
.map_or(false, |code| code.code != "dead_code")
{
return None;
}
if !diag.message.contains("function") {
return None;
}
let name = diag.message.split("`").nth(1)?.to_owned();
let span = &diag.spans[0];
assert_eq!(
span.line_start, span.line_end,
"encountered multiline span in dead_code"
);
Some(Unused {
name,
line: span.line_start,
column: (span.column_start - 1)..(span.column_end - 1),
})
})
.collect();
Self {
unused_functions,
has_change: false,
}
}
fn should_retain_item(&mut self, span: Span) -> bool {
let span_matches = self
.unused_functions
.iter()
.map(|a| a.span_matches(span))
.filter(|&matches| matches)
.count();
assert!(
span_matches < 2,
"multiple dead_code spans matched identifier: {span_matches}"
);
if span_matches == 1 {
self.has_change = true;
}
span_matches == 0
}
}
impl VisitMut for FindUnusedFunction {
fn visit_item_impl_mut(&mut self, item_impl: &mut syn::ItemImpl) {
item_impl.items.retain(|item| match item {
ImplItem::Method(method) => {
let span = method.sig.ident.span();
self.should_retain_item(span)
}
_ => true,
});
syn::visit_mut::visit_item_impl_mut(self, item_impl);
}
fn visit_file_mut(&mut self, krate: &mut syn::File) {
krate.items.retain(|item| match item {
Item::Fn(func) => {
let span = func.sig.ident.span();
self.should_retain_item(span)
}
_ => true,
});
syn::visit_mut::visit_file_mut(self, krate);
}
fn visit_item_mod_mut(&mut self, module: &mut syn::ItemMod) {
if let Some((_, content)) = &mut module.content {
content.retain(|item| match item {
Item::Fn(func) => {
let span = func.sig.ident.span();
self.should_retain_item(span)
}
_ => true,
})
}
syn::visit_mut::visit_item_mod_mut(self, module);
}
}