Skip to content

Commit 8aadb82

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

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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 the index of {0} in the 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 the index of {0} in the 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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,15 @@ impl ToTokens for AccessType {
6262
}
6363
}
6464
}
65-
Self::Ref(Accessor::Array(ArrayAccessor { doc, name, ty, .. })) => {
65+
Self::Ref(Accessor::Array(ArrayAccessor { doc, name, ty, note, .. })) => {
6666
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
67+
let note = note.as_ref().map(|note| quote! {
68+
#[doc = ""]
69+
#[doc = #note]
70+
});
6771
quote! {
6872
#[doc = #doc]
73+
#note
6974
#[inline(always)]
7075
pub const fn #name(&self, n: usize) -> &#ty {
7176
&self.#name[n]
@@ -85,14 +90,20 @@ impl ToTokens for AccessType {
8590
offset,
8691
dim,
8792
increment,
93+
note,
8894
})) => {
8995
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
9096
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
9197
let dim = unsuffixed(*dim);
9298
let increment = (*increment != 1).then(|| unsuffixed(*increment)).map(|i| quote!(#i *));
99+
let note = note.as_ref().map(|note| quote! {
100+
#[doc = ""]
101+
#[doc = #note]
102+
});
93103
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .add(#increment n).cast() } };
94104
quote! {
95105
#[doc = #doc]
106+
#note
96107
#[inline(always)]
97108
pub const fn #name(&self, n: usize) -> &#ty {
98109
#[allow(clippy::no_effect)]
@@ -145,6 +156,7 @@ pub struct ArrayAccessor {
145156
pub offset: u32,
146157
pub dim: u32,
147158
pub increment: u32,
159+
pub note: Option<String>,
148160
}
149161

150162
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)