Skip to content

Commit

Permalink
Merge pull request #24 from eZioPan/add-err-msg
Browse files Browse the repository at this point in the history
add err msg about renaming collision
  • Loading branch information
Dirbaio committed Feb 19, 2024
2 parents 06d679e + b1a7f10 commit 2222feb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
15 changes: 8 additions & 7 deletions src/svd2ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ pub fn convert_peripheral(ir: &mut IR, p: &svd::Peripheral) -> anyhow::Result<()
for e in &f.enumerated_values {
let e = if let Some(derived_from) = &e.derived_from {
let Some(e) = enum_from_name.get(derived_from.as_str()) else {
warn!("unknown enum to derive from ({} -> {})", f.name, derived_from);
continue
warn!(
"unknown enum to derive from ({} -> {})",
f.name, derived_from
);
continue;
};
e
} else {
Expand Down Expand Up @@ -349,7 +352,7 @@ pub fn convert_svd(svd: &svd::Device) -> anyhow::Result<IR> {
}
irqs.sort_by_key(|i| &i.name);

for (_n, &i) in irqs.iter().enumerate() {
for &i in irqs.iter() {
let iname = i.name.to_ascii_uppercase();

if !device.interrupts.iter().any(|j| j.name == iname) {
Expand Down Expand Up @@ -407,14 +410,12 @@ fn enum_map(blocks: &[ProtoBlock]) -> HashMap<&'_ str, &'_ svd::EnumeratedValues
for block in blocks {
for r in &block.registers {
let svd::RegisterCluster::Register(r) = r else {
continue
continue;
};
if r.derived_from.is_some() {
continue;
}
let Some(fields) = &r.fields else {
continue
};
let Some(fields) = &r.fields else { continue };
for f in fields {
for e in &f.enumerated_values {
if let Some(name) = &e.name {
Expand Down
2 changes: 1 addition & 1 deletion src/transform/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub(crate) fn extract_variant_desc(
format!(
"{}: {}\n",
v.value,
v.description.clone().unwrap_or(String::new())
v.description.clone().unwrap_or_default()
)
.as_str(),
);
Expand Down
70 changes: 62 additions & 8 deletions src/transform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use crate::ir::*;
use crate::util::{ToSanitizedPascalCase, ToSanitizedSnakeCase, ToSanitizedUpperCase};
Expand All @@ -24,6 +24,7 @@ impl Sanitize {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NameKind {
Device,
DevicePeripheral,
Expand All @@ -36,14 +37,52 @@ pub enum NameKind {
EnumVariant,
}

#[derive(PartialEq, Eq, Hash)]
struct NameCollisionError {
kind: NameKind,
old: String,
new: String,
}

impl NameCollisionError {
fn new(kind: NameKind, old: String, new: String) -> Self {
Self { kind, old, new }
}
}

impl std::fmt::Debug for NameCollisionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Err: on rename {:?} \"{}\", new name \"{}\" already exist",
self.kind, self.old, self.new
)
}
}

struct NameCollisionErrors(HashSet<NameCollisionError>);

impl std::fmt::Debug for NameCollisionErrors {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if !self.0.is_empty() {
writeln!(f)?
}

for err in self.0.iter() {
writeln!(f, "{:?}", err)?
}
Ok(())
}
}

fn rename_opt(s: &mut Option<String>, f: impl Fn(&mut String)) {
if let Some(s) = s {
f(s)
}
}

pub fn map_block_names(ir: &mut IR, f: impl Fn(&mut String)) {
remap_names(&mut ir.blocks, &f);
remap_names(NameKind::Block, &mut ir.blocks, &f).unwrap();

for (_, d) in ir.devices.iter_mut() {
for p in &mut d.peripherals {
Expand All @@ -62,7 +101,7 @@ pub fn map_block_names(ir: &mut IR, f: impl Fn(&mut String)) {
}

pub fn map_fieldset_names(ir: &mut IR, f: impl Fn(&mut String)) {
remap_names(&mut ir.fieldsets, &f);
remap_names(NameKind::Fieldset, &mut ir.fieldsets, &f).unwrap();

for (_, b) in ir.blocks.iter_mut() {
for i in b.items.iter_mut() {
Expand All @@ -75,7 +114,7 @@ pub fn map_fieldset_names(ir: &mut IR, f: impl Fn(&mut String)) {
}

pub fn map_enum_names(ir: &mut IR, f: impl Fn(&mut String)) {
remap_names(&mut ir.enums, &f);
remap_names(NameKind::Enum, &mut ir.enums, &f).unwrap();

for (_, fs) in ir.fieldsets.iter_mut() {
for ff in fs.fields.iter_mut() {
Expand All @@ -85,7 +124,7 @@ pub fn map_enum_names(ir: &mut IR, f: impl Fn(&mut String)) {
}

pub fn map_device_names(ir: &mut IR, f: impl Fn(&mut String)) {
remap_names(&mut ir.devices, &f);
remap_names(NameKind::Device, &mut ir.devices, &f).unwrap();
}

pub fn map_device_interrupt_names(ir: &mut IR, f: impl Fn(&mut String)) {
Expand Down Expand Up @@ -169,13 +208,28 @@ pub fn map_descriptions(ir: &mut IR, mut ff: impl FnMut(&str) -> String) -> anyh
Ok(())
}

fn remap_names<T>(x: &mut HashMap<String, T>, f: impl Fn(&mut String)) {
fn remap_names<T>(
kind: NameKind,
x: &mut HashMap<String, T>,
f: impl Fn(&mut String),
) -> Result<(), NameCollisionErrors> {
let mut res = HashMap::new();
let mut errs = HashSet::new();

for (mut name, val) in x.drain() {
let orginal_name = name.clone();
f(&mut name);
assert!(res.insert(name, val).is_none())
if res.insert(name.clone(), val).is_some() {
errs.insert(NameCollisionError::new(kind, orginal_name, name));
}
}

if !errs.is_empty() {
return Err(NameCollisionErrors(errs));
}
*x = res

*x = res;
Ok(())
}

fn sanitize_path(p: &str) -> String {
Expand Down

0 comments on commit 2222feb

Please sign in to comment.