Skip to content

Commit

Permalink
Add raltool config for strict enum descriptions
Browse files Browse the repository at this point in the history
Similar to strict enum names, this configuration instructs the combiner
to compare enum variant descriptions, the human-readable strings
associated with items. If they're not equal, then the enums cannot be
combined.

When applied to IOMUXC peripherals, this ensures that the documentation
associated with each enum variant is correct. The next commit checks in
the updates.
  • Loading branch information
mciantyre committed Apr 6, 2024
1 parent 34a15ac commit 835fbfb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions raltool-cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ combines:
- iomuxc
- iomuxc_gpr
- iomuxc_snvs
- StrictEnumDescs:
- iomuxc
- iomuxc_gpr
- iomuxc_snvs
# IR paths that should never be combined.
#
# These regexes match IR paths. If there's a match, that element is *never* considered for
Expand Down
23 changes: 19 additions & 4 deletions raltool/src/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ fn equivalent_options<E>(

#[derive(Clone, Copy)]
struct EquivalentEnums<'a> {
peripherals: &'a HashSet<String>,
names: &'a HashSet<String>,
descs: &'a HashSet<String>,
}

impl Equivalence<ir::Enum> for EquivalentEnums<'_> {
Expand All @@ -159,10 +160,13 @@ impl Equivalence<ir::Enum> for EquivalentEnums<'_> {
CompareIr { elem: b, .. }: CompareIr<ir::Enum>,
path: IrPath,
) -> bool {
let assert_name_equivalence = self.peripherals.contains(peripheral_part(path));
let assert_name_equivalence = self.names.contains(peripheral_part(path));
let assert_desc_equivalence = self.descs.contains(peripheral_part(path));
a.bit_size == b.bit_size
&& equivalent_slices(&a.variants, &b.variants, |q, r| {
q.value == r.value && (!assert_name_equivalence || q.name == r.name)
q.value == r.value
&& (!assert_name_equivalence || q.name == r.name)
&& (!assert_desc_equivalence || q.description == r.description)
})
}
}
Expand Down Expand Up @@ -350,7 +354,8 @@ impl<'ir> IrVersions<'ir> {
let exclusions = &exclusions;

let enums = EquivalentEnums {
peripherals: &config.strict_enum_names,
names: &config.strict_enum_names,
descs: &config.strict_enum_descs,
};
let fieldsets = EquivalentFieldSets { enums };
let blocks = EquivalentBlocks { fieldsets };
Expand Down Expand Up @@ -426,6 +431,7 @@ type RefMap<'a, K, V> = HashMap<RefHash<'a, K>, V>;
#[derive(Default)]
pub struct Config {
strict_enum_names: HashSet<String>,
strict_enum_descs: HashSet<String>,
never_combine: HashSet<String>,
}

Expand Down Expand Up @@ -592,6 +598,12 @@ pub enum Combine {
/// always safe to add to this list; however, it means there may be more
/// code generated.
StrictEnumNames(Vec<String>),
/// The list of peripheral names that require strict enum description
/// checks.
///
/// This is even stricter than [`StrictEnumNames`], since it asserts
/// equal descriptions (human-readable descriptions) for each enum variant.
StrictEnumDescs(Vec<String>),
/// The list of patterns (regex string) to never combine.
///
/// You should design patterns to the IR path names. Note that, unlike
Expand All @@ -612,6 +624,9 @@ where
Combine::StrictEnumNames(peripherals) => {
config.strict_enum_names.extend(peripherals);
}
Combine::StrictEnumDescs(peripherals) => {
config.strict_enum_descs.extend(peripherals);
}
Combine::NeverCombine(paths) => {
config.never_combine.extend(paths);
}
Expand Down

0 comments on commit 835fbfb

Please sign in to comment.