Skip to content

Commit a3aa726

Browse files
committed
Add warning about indexing register arrays
1 parent c13b87d commit a3aa726

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Add warning about indexing register arrays
1011
- Skip generating `.add(0)` and `1 *` in accessors
1112
- Bump MSRV of generated code to 1.76
1213
- move `must_use` from methods to generic type

src/generate/peripheral.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,10 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10521052
&description,
10531053
);
10541054
let mut accessors = Vec::with_capacity((array_info.dim + 1) as _);
1055+
let first_name = svd::array::names(info, array_info).next().unwrap();
1056+
let note = (array_info.indexes().next().unwrap() != "0").then(||
1057+
format!("<div class=\"warning\">`n` is number of {0} in array. `n == 0` corresponds to `{first_name}` {0}.</div>", "cluster")
1058+
);
10551059
accessors.push(
10561060
Accessor::Array(ArrayAccessor {
10571061
doc,
@@ -1060,6 +1064,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10601064
offset: info.address_offset,
10611065
dim: array_info.dim,
10621066
increment: array_info.dim_increment,
1067+
note,
10631068
})
10641069
.raw_if(!array_convertible),
10651070
);
@@ -1234,6 +1239,10 @@ fn expand_register(
12341239
&description,
12351240
);
12361241
let mut accessors = Vec::with_capacity((array_info.dim + 1) as _);
1242+
let first_name = svd::array::names(info, array_info).next().unwrap();
1243+
let note = (array_info.indexes().next().unwrap() != "0").then(||
1244+
format!("<div class=\"warning\">`n` is number of {0} in array. `n == 0` corresponds to `{first_name}` {0}.</div>", "register")
1245+
);
12371246
accessors.push(
12381247
Accessor::Array(ArrayAccessor {
12391248
doc,
@@ -1242,6 +1251,7 @@ fn expand_register(
12421251
offset: info.address_offset,
12431252
dim: array_info.dim,
12441253
increment: array_info.dim_increment,
1254+
note,
12451255
})
12461256
.raw_if(!array_convertible),
12471257
);

src/generate/peripheral/accessor.rs

+7
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,20 @@ impl ToTokens for AccessType {
8585
offset,
8686
dim,
8787
increment,
88+
note,
8889
})) => {
8990
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
9091
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
9192
let dim = unsuffixed(*dim);
9293
let increment = (*increment != 1).then(|| unsuffixed(*increment)).map(|i| quote!(#i *));
94+
let note = note.as_ref().map(|note| quote! {
95+
#[doc = ""]
96+
#[doc = #note]
97+
});
9398
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .add(#increment n).cast() } };
9499
quote! {
95100
#[doc = #doc]
101+
#note
96102
#[inline(always)]
97103
pub const fn #name(&self, n: usize) -> &#ty {
98104
#[allow(clippy::no_effect)]
@@ -145,6 +151,7 @@ pub struct ArrayAccessor {
145151
pub offset: u32,
146152
pub dim: u32,
147153
pub increment: u32,
154+
pub note: Option<String>,
148155
}
149156

150157
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)