Skip to content

Commit

Permalink
Add warning about indexing register arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 19, 2024
1 parent c13b87d commit a3aa726
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Add warning about indexing register arrays
- Skip generating `.add(0)` and `1 *` in accessors
- Bump MSRV of generated code to 1.76
- move `must_use` from methods to generic type
Expand Down
10 changes: 10 additions & 0 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,10 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
&description,
);
let mut accessors = Vec::with_capacity((array_info.dim + 1) as _);
let first_name = svd::array::names(info, array_info).next().unwrap();
let note = (array_info.indexes().next().unwrap() != "0").then(||
format!("<div class=\"warning\">`n` is number of {0} in array. `n == 0` corresponds to `{first_name}` {0}.</div>", "cluster")
);
accessors.push(
Accessor::Array(ArrayAccessor {
doc,
Expand All @@ -1060,6 +1064,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
offset: info.address_offset,
dim: array_info.dim,
increment: array_info.dim_increment,
note,
})
.raw_if(!array_convertible),
);
Expand Down Expand Up @@ -1234,6 +1239,10 @@ fn expand_register(
&description,
);
let mut accessors = Vec::with_capacity((array_info.dim + 1) as _);
let first_name = svd::array::names(info, array_info).next().unwrap();
let note = (array_info.indexes().next().unwrap() != "0").then(||
format!("<div class=\"warning\">`n` is number of {0} in array. `n == 0` corresponds to `{first_name}` {0}.</div>", "register")
);
accessors.push(
Accessor::Array(ArrayAccessor {
doc,
Expand All @@ -1242,6 +1251,7 @@ fn expand_register(
offset: info.address_offset,
dim: array_info.dim,
increment: array_info.dim_increment,
note,
})
.raw_if(!array_convertible),
);
Expand Down
7 changes: 7 additions & 0 deletions src/generate/peripheral/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,20 @@ impl ToTokens for AccessType {
offset,
dim,
increment,
note,
})) => {
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
let dim = unsuffixed(*dim);
let increment = (*increment != 1).then(|| unsuffixed(*increment)).map(|i| quote!(#i *));
let note = note.as_ref().map(|note| quote! {
#[doc = ""]
#[doc = #note]
});
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .add(#increment n).cast() } };
quote! {
#[doc = #doc]
#note
#[inline(always)]
pub const fn #name(&self, n: usize) -> &#ty {
#[allow(clippy::no_effect)]
Expand Down Expand Up @@ -145,6 +151,7 @@ pub struct ArrayAccessor {
pub offset: u32,
pub dim: u32,
pub increment: u32,
pub note: Option<String>,
}

#[derive(Clone, Debug)]
Expand Down

0 comments on commit a3aa726

Please sign in to comment.