Skip to content

Commit abf860f

Browse files
authored
Merge pull request #236 from rust-embedded/check-width
check width in process_field_enum
2 parents d5c4749 + ab204b2 commit abf860f

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

CHANGELOG-rust.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This changelog tracks the Rust `svdtools` project. See
55

66
## [Unreleased]
77

8+
* Protect from using one `enumeratedValues` in fields with different width
9+
810
## [v0.3.17] 2024-07-05
911

1012
* Support "isDefault" enum value in `svdtools html`

src/patch/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl DeviceExt for Device {
172172
// Now process all peripherals
173173
for (periphspec, val) in device {
174174
let periphspec = periphspec.str()?;
175-
if !periphspec.starts_with('_') {
175+
if !Self::KEYWORDS.contains(&periphspec) {
176176
//val["_path"] = device["_path"]; // TODO: check
177177
self.process_peripheral(periphspec, val.hash()?, config)
178178
.with_context(|| format!("According to `{periphspec}`"))?;

src/patch/peripheral.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,12 @@ pub(crate) trait RegisterBlockExt: Name {
458458
rtag.name.drain(..len);
459459
}
460460
if let Some(dname) = rtag.display_name.as_mut() {
461-
if glob.is_match(&dname) {
461+
if glob.is_match(dname.as_str()) {
462462
dname.drain(..len);
463463
}
464464
}
465465
if let Some(name) = rtag.alternate_register.as_mut() {
466-
if glob.is_match(&name) {
466+
if glob.is_match(name.as_str()) {
467467
name.drain(..len);
468468
}
469469
}
@@ -483,13 +483,13 @@ pub(crate) trait RegisterBlockExt: Name {
483483
rtag.name.truncate(nlen - len);
484484
}
485485
if let Some(dname) = rtag.display_name.as_mut() {
486-
if glob.is_match(&dname) {
486+
if glob.is_match(dname.as_str()) {
487487
let nlen = dname.len();
488488
dname.truncate(nlen - len);
489489
}
490490
}
491491
if let Some(name) = rtag.alternate_register.as_mut() {
492-
if glob.is_match(&name) {
492+
if glob.is_match(name.as_str()) {
493493
let nlen = name.len();
494494
name.truncate(nlen - len);
495495
}

src/patch/register.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashSet;
2+
13
use anyhow::{anyhow, Context};
24
use itertools::Itertools;
35
use svd_parser::expand::{BlockPath, RegisterPath};
@@ -768,9 +770,11 @@ impl RegisterExt for Register {
768770
} else {
769771
let (fspec, ignore) = fspec.spec();
770772
let mut offsets: Vec<_> = Vec::new();
773+
let mut width_vals = HashSet::new();
771774
for (i, f) in self.fields().enumerate() {
772775
if matchname(&f.name, fspec) {
773-
offsets.push((f.bit_range.offset, f.name.to_string(), i));
776+
offsets.push((f.bit_offset(), f.name.to_string(), i));
777+
width_vals.insert(f.bit_width());
774778
}
775779
}
776780
if offsets.is_empty() {
@@ -779,7 +783,11 @@ impl RegisterExt for Register {
779783
}
780784
let present = self.present_fields();
781785
return Err(anyhow!(
782-
"Could not find field {rpath}:{fspec}. Present fields: {present}.`"
786+
"Could not find field {rpath}:{fspec}. Present fields: {present}."
787+
));
788+
} else if width_vals.len() > 1 {
789+
return Err(anyhow!(
790+
"{rpath}:{fspec}. Same enumeratedValues are used for different fields."
783791
));
784792
}
785793
let (min_offset, fname, min_offset_pos) =
@@ -794,11 +802,9 @@ impl RegisterExt for Register {
794802
let access = ftag.access.or(reg_access).unwrap_or_default();
795803
let checked_usage = check_usage(access, usage)
796804
.with_context(|| format!("In field {}", ftag.name))?;
797-
if config.enum_derive == EnumAutoDerive::None
798-
|| ftag.bit_range.offset == *min_offset
799-
{
805+
if config.enum_derive == EnumAutoDerive::None || ftag.bit_offset() == *min_offset {
800806
let mut evs = make_ev_array(fmod)?.usage(make_usage(access, checked_usage));
801-
if ftag.bit_range.offset == *min_offset {
807+
if ftag.bit_offset() == *min_offset {
802808
evs = evs.name(Some(name.clone()));
803809
}
804810
let evs = evs.build(VAL_LVL)?;

0 commit comments

Comments
 (0)