Skip to content

Commit

Permalink
Print names of enums that failed merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Nov 14, 2024
1 parent ac05c84 commit dd3dc5f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 61 deletions.
60 changes: 0 additions & 60 deletions src/transform/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,66 +113,6 @@ pub enum CheckLevel {
Descriptions,
}

pub(crate) fn check_mergeable_enums(a: &Enum, b: &Enum, level: CheckLevel) -> anyhow::Result<()> {
if let Err(e) = check_mergeable_enums_inner(a, b, level) {
bail!(
"Cannot merge enums.\nfirst: {:#?}\nsecond: {:#?}\ncause: {:?}",
a,
b,
e
)
}
Ok(())
}
pub(crate) fn check_mergeable_enums_inner(
a: &Enum,
b: &Enum,
level: CheckLevel,
) -> anyhow::Result<()> {
if a.bit_size != b.bit_size {
bail!("Different bit size: {} vs {}", a.bit_size, b.bit_size)
}

if level >= CheckLevel::Layout {
if a.variants.len() != b.variants.len() {
bail!("Different variant count")
}

let mut aok = [false; 1024];
let mut bok = [false; 1024];

for (ia, fa) in a.variants.iter().enumerate() {
if let Some((ib, _fb)) = b
.variants
.iter()
.enumerate()
.find(|(ib, fb)| !bok[*ib] && mergeable_variants(fa, fb, level))
{
aok[ia] = true;
bok[ib] = true;
} else {
bail!("Variant in first enum has no match: {:?}", fa);
}
}
}

Ok(())
}

pub(crate) fn mergeable_variants(a: &EnumVariant, b: &EnumVariant, level: CheckLevel) -> bool {
let mut res = true;
if level >= CheckLevel::Layout {
res &= a.value == b.value;
}
if level >= CheckLevel::Names {
res &= a.name == b.name;
}
if level >= CheckLevel::Descriptions {
res &= a.description == b.description;
}
res
}

impl Default for CheckLevel {
fn default() -> Self {
Self::Names
Expand Down
61 changes: 60 additions & 1 deletion src/transform/merge_enums.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::bail;
use log::*;
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
Expand Down Expand Up @@ -57,7 +58,7 @@ impl MergeEnums {

for id in &ids {
let e2 = ir.enums.get(id).unwrap();
if let Err(e) = check_mergeable_enums(&e, e2, self.check) {
if let Err(e) = check_mergeable_enums(&main_id, &e, id, e2, self.check) {
if self.skip_unmergeable {
info!("skipping: {:?}", to);
return Ok(());
Expand All @@ -76,3 +77,61 @@ impl MergeEnums {
Ok(())
}
}

fn check_mergeable_enums(
a_id: &str,
a: &Enum,
b_id: &str,
b: &Enum,
level: CheckLevel,
) -> anyhow::Result<()> {
if let Err(e) = check_mergeable_enums_inner(a, b, level) {
bail!("Cannot merge enums.\nfirst: {a_id}\n{a:#?}\nsecond: {b_id}\n{b:#?}\ncause: {e:?}",)
}
Ok(())
}

fn check_mergeable_enums_inner(a: &Enum, b: &Enum, level: CheckLevel) -> anyhow::Result<()> {
if a.bit_size != b.bit_size {
bail!("Different bit size: {} vs {}", a.bit_size, b.bit_size)
}

if level >= CheckLevel::Layout {
if a.variants.len() != b.variants.len() {
bail!("Different variant count")
}

let mut aok = [false; 1024];
let mut bok = [false; 1024];

for (ia, fa) in a.variants.iter().enumerate() {
if let Some((ib, _fb)) = b
.variants
.iter()
.enumerate()
.find(|(ib, fb)| !bok[*ib] && mergeable_variants(fa, fb, level))
{
aok[ia] = true;
bok[ib] = true;
} else {
bail!("Variant in first enum has no match: {:?}", fa);
}
}
}

Ok(())
}

fn mergeable_variants(a: &EnumVariant, b: &EnumVariant, level: CheckLevel) -> bool {
let mut res = true;
if level >= CheckLevel::Layout {
res &= a.value == b.value;
}
if level >= CheckLevel::Names {
res &= a.name == b.name;
}
if level >= CheckLevel::Descriptions {
res &= a.description == b.description;
}
res
}

0 comments on commit dd3dc5f

Please sign in to comment.