Skip to content

Commit

Permalink
Merge pull request #236 from rust-embedded/check-width
Browse files Browse the repository at this point in the history
check width in process_field_enum
  • Loading branch information
burrbull committed Aug 10, 2024
2 parents d5c4749 + ab204b2 commit abf860f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This changelog tracks the Rust `svdtools` project. See

## [Unreleased]

* Protect from using one `enumeratedValues` in fields with different width

## [v0.3.17] 2024-07-05

* Support "isDefault" enum value in `svdtools html`
Expand Down
2 changes: 1 addition & 1 deletion src/patch/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl DeviceExt for Device {
// Now process all peripherals
for (periphspec, val) in device {
let periphspec = periphspec.str()?;
if !periphspec.starts_with('_') {
if !Self::KEYWORDS.contains(&periphspec) {
//val["_path"] = device["_path"]; // TODO: check
self.process_peripheral(periphspec, val.hash()?, config)
.with_context(|| format!("According to `{periphspec}`"))?;
Expand Down
8 changes: 4 additions & 4 deletions src/patch/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,12 @@ pub(crate) trait RegisterBlockExt: Name {
rtag.name.drain(..len);
}
if let Some(dname) = rtag.display_name.as_mut() {
if glob.is_match(&dname) {
if glob.is_match(dname.as_str()) {
dname.drain(..len);
}
}
if let Some(name) = rtag.alternate_register.as_mut() {
if glob.is_match(&name) {
if glob.is_match(name.as_str()) {
name.drain(..len);
}
}
Expand All @@ -483,13 +483,13 @@ pub(crate) trait RegisterBlockExt: Name {
rtag.name.truncate(nlen - len);
}
if let Some(dname) = rtag.display_name.as_mut() {
if glob.is_match(&dname) {
if glob.is_match(dname.as_str()) {
let nlen = dname.len();
dname.truncate(nlen - len);
}
}
if let Some(name) = rtag.alternate_register.as_mut() {
if glob.is_match(&name) {
if glob.is_match(name.as_str()) {
let nlen = name.len();
name.truncate(nlen - len);
}
Expand Down
18 changes: 12 additions & 6 deletions src/patch/register.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use anyhow::{anyhow, Context};
use itertools::Itertools;
use svd_parser::expand::{BlockPath, RegisterPath};
Expand Down Expand Up @@ -768,9 +770,11 @@ impl RegisterExt for Register {
} else {
let (fspec, ignore) = fspec.spec();
let mut offsets: Vec<_> = Vec::new();
let mut width_vals = HashSet::new();
for (i, f) in self.fields().enumerate() {
if matchname(&f.name, fspec) {
offsets.push((f.bit_range.offset, f.name.to_string(), i));
offsets.push((f.bit_offset(), f.name.to_string(), i));
width_vals.insert(f.bit_width());
}
}
if offsets.is_empty() {
Expand All @@ -779,7 +783,11 @@ impl RegisterExt for Register {
}
let present = self.present_fields();
return Err(anyhow!(
"Could not find field {rpath}:{fspec}. Present fields: {present}.`"
"Could not find field {rpath}:{fspec}. Present fields: {present}."
));
} else if width_vals.len() > 1 {
return Err(anyhow!(
"{rpath}:{fspec}. Same enumeratedValues are used for different fields."
));
}
let (min_offset, fname, min_offset_pos) =
Expand All @@ -794,11 +802,9 @@ impl RegisterExt for Register {
let access = ftag.access.or(reg_access).unwrap_or_default();
let checked_usage = check_usage(access, usage)
.with_context(|| format!("In field {}", ftag.name))?;
if config.enum_derive == EnumAutoDerive::None
|| ftag.bit_range.offset == *min_offset
{
if config.enum_derive == EnumAutoDerive::None || ftag.bit_offset() == *min_offset {
let mut evs = make_ev_array(fmod)?.usage(make_usage(access, checked_usage));
if ftag.bit_range.offset == *min_offset {
if ftag.bit_offset() == *min_offset {
evs = evs.name(Some(name.clone()));
}
let evs = evs.build(VAL_LVL)?;
Expand Down

0 comments on commit abf860f

Please sign in to comment.