Skip to content

Commit

Permalink
More useless enum deletion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Nov 3, 2024
1 parent e6c61ca commit 2a63600
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
45 changes: 45 additions & 0 deletions src/transform/delete_enums_with_variants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use log::*;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

use super::delete_enums::remove_enum_ids;
use crate::ir::*;

#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteEnumsWithVariants {
variants: HashMap<u64, String>,
#[serde(default)]
pub soft: bool,
}

impl DeleteEnumsWithVariants {
pub fn run(&self, ir: &mut IR) -> anyhow::Result<()> {
let mut ids: HashSet<String> = HashSet::new();

'e: for (id, e) in &ir.enums {
if e.variants.len() != self.variants.len() {
continue;
}
for v in &e.variants {
let Some(name) = self.variants.get(&v.value) else {
continue 'e;
};
if name != &v.name {
continue 'e;
}
}
info!("deleting enum {}", id);
ids.insert(id.clone());
}

remove_enum_ids(ir, &ids);

if !self.soft {
for id in ids {
ir.enums.remove(&id);
}
}

Ok(())
}
}
13 changes: 9 additions & 4 deletions src/transform/delete_useless_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const USELESS_ZERO_NAMES: &[&str] = &[
"discon",
"disconnect",
"disconnected",
"not_detected",
"invalid",
];
const USELESS_ONE_NAMES: &[&str] = &[
"en",
Expand All @@ -58,9 +60,11 @@ const USELESS_ONE_NAMES: &[&str] = &[
"available",
"connect",
"connected",
"detected",
"valid",
];

const NOT_NAMES: &[&str] = &["not", "no"];
const NOT_NAMES: &[&str] = &["not", "no", "un", "de", "in"];

fn is_useless(e: &Enum) -> bool {
match e.bit_size {
Expand All @@ -76,9 +80,10 @@ fn is_useless(e: &Enum) -> bool {

let obvious = USELESS_ZERO_NAMES.iter().any(|s| s == &zero_name)
&& USELESS_ONE_NAMES.iter().any(|s| s == &one_name);
let not = NOT_NAMES
.iter()
.any(|not| zero_name == format!("{not}{one_name}"));
let not = NOT_NAMES.iter().any(|not| {
zero_name == format!("{not}{one_name}")
|| zero_name == format!("{not}_{one_name}")
});

obvious || not
}
Expand Down
7 changes: 4 additions & 3 deletions src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,10 @@ mod common;
pub mod delete;
pub mod delete_enums;
pub mod delete_enums_used_in;
pub mod delete_enums_with_variants;
pub mod delete_fieldsets;
pub mod delete_useless_enums;
//pub mod find_duplicate_enums;
//pub mod find_duplicate_fieldsets;
pub mod delete_registers;
pub mod delete_useless_enums;
pub mod expand_extends;
pub mod fix_register_bit_sizes;
pub mod make_block;
Expand All @@ -279,6 +278,7 @@ pub enum Transform {
Sort(sort::Sort),
Delete(delete::Delete),
DeleteEnums(delete_enums::DeleteEnums),
DeleteEnumsWithVariants(delete_enums_with_variants::DeleteEnumsWithVariants),
DeleteEnumsUsedIn(delete_enums_used_in::DeleteEnumsUsedIn),
DeleteUselessEnums(delete_useless_enums::DeleteUselessEnums),
DeleteFieldsets(delete_fieldsets::DeleteFieldsets),
Expand Down Expand Up @@ -307,6 +307,7 @@ impl Transform {
Self::Delete(t) => t.run(ir),
Self::DeleteEnums(t) => t.run(ir),
Self::DeleteEnumsUsedIn(t) => t.run(ir),
Self::DeleteEnumsWithVariants(t) => t.run(ir),
Self::DeleteUselessEnums(t) => t.run(ir),
Self::DeleteFieldsets(t) => t.run(ir),
Self::DeleteRegisters(t) => t.run(ir),
Expand Down

0 comments on commit 2a63600

Please sign in to comment.