From cb66cd423646e684f7cd01b0b313241788a91dca Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Fri, 18 Oct 2024 21:52:16 +0300 Subject: [PATCH 1/4] clippy --- src/generate/register.rs | 4 ++-- src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/generate/register.rs b/src/generate/register.rs index 84ea9e07..b4600dc7 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -3,7 +3,6 @@ use crate::svd::{ ModifiedWriteValues, ReadAction, Register, RegisterProperties, Usage, WriteConstraint, WriteConstraintRange, }; -use core::u64; use log::warn; use proc_macro2::{Delimiter, Group, Ident, Span, TokenStream}; use quote::quote; @@ -117,7 +116,7 @@ pub fn render( register.properties.reset_value.is_some(), &mod_ty, false, - ®ister, + register, &rpath, config, )?, @@ -162,6 +161,7 @@ fn read_action_docs(can_read: bool, read_action: Option) -> String { doc } +#[allow(clippy::too_many_arguments)] fn api_docs( can_read: bool, can_write: bool, diff --git a/src/lib.rs b/src/lib.rs index 6a0b754f..ec574c98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ //! //! - `build.rs`, build script that places `device.x` somewhere the linker can find. //! - `device.x`, linker script that weakly aliases all the interrupt handlers to the default -//! exception handler (`DefaultHandler`). +//! exception handler (`DefaultHandler`). //! - `lib.rs`, the generated code. //! //! All these files must be included in the same device crate. The `lib.rs` file contains several @@ -95,7 +95,7 @@ //! //! - `build.rs`, build script that places `device.x` somewhere the linker can find. //! - `device.x`, linker script that weakly aliases all the interrupt handlers to the default -//! exception handler (`DefaultHandler`). +//! exception handler (`DefaultHandler`). //! - `lib.rs`, the generated code. //! //! All these files must be included in the same device crate. The `lib.rs` file contains several From dbd8734071d577b77fa08cb77a06808cdb83c9ae Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 19 Oct 2024 10:13:28 +0300 Subject: [PATCH 2/4] clean accessors --- CHANGELOG.md | 1 + src/generate/peripheral.rs | 22 ++++++++++------------ src/generate/peripheral/accessor.rs | 21 ++++++++++++++------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cfe7b61..cbe26557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +- Skip generating `.add(0)` and `1 *` in accessors - Bump MSRV of generated code to 1.76 - move `must_use` from methods to generic type diff --git a/src/generate/peripheral.rs b/src/generate/peripheral.rs index e58a61ec..65ca041a 100644 --- a/src/generate/peripheral.rs +++ b/src/generate/peripheral.rs @@ -998,7 +998,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result Result Result Result { + let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o))); quote! { #[doc = #doc] #[inline(always)] pub const fn #name(&self) -> &#ty { - unsafe { &*core::ptr::from_ref(self).cast::().add(#offset).cast() } + unsafe { &*core::ptr::from_ref(self).cast::() #offset .cast() } } } } @@ -84,7 +87,10 @@ impl ToTokens for AccessType { increment, })) => { let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site()); - let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::().add(#offset).add(#increment * n).cast() } }; + 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 cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::() #offset .add(#increment n).cast() } }; quote! { #[doc = #doc] #[inline(always)] @@ -109,6 +115,7 @@ impl ToTokens for AccessType { basename, i, } = elem; + let i = unsuffixed(*i as u64); quote! { #[doc = #doc] #[inline(always)] @@ -127,7 +134,7 @@ pub struct RegAccessor { pub doc: String, pub name: Ident, pub ty: syn::Type, - pub offset: syn::LitInt, + pub offset: u32, } #[derive(Clone, Debug)] @@ -135,9 +142,9 @@ pub struct ArrayAccessor { pub doc: String, pub name: Ident, pub ty: syn::Type, - pub offset: syn::LitInt, - pub dim: syn::LitInt, - pub increment: syn::LitInt, + pub offset: u32, + pub dim: u32, + pub increment: u32, } #[derive(Clone, Debug)] @@ -146,5 +153,5 @@ pub struct ArrayElemAccessor { pub name: Ident, pub ty: syn::Type, pub basename: Ident, - pub i: syn::LitInt, + pub i: usize, } From c13b87d2cbc20c878096aa7ae5780ddfd8e55b26 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 19 Oct 2024 13:06:09 +0300 Subject: [PATCH 3/4] strings --- src/generate/interrupt.rs | 8 ++++---- src/generate/peripheral.rs | 10 +++++----- src/generate/register.rs | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/generate/interrupt.rs b/src/generate/interrupt.rs index 10d9bdde..7417c382 100644 --- a/src/generate/interrupt.rs +++ b/src/generate/interrupt.rs @@ -62,10 +62,10 @@ pub fn render( interrupt .0 .description - .as_ref() - .map(|s| util::respace(s)) - .as_ref() - .map(|s| util::escape_special_chars(s)) + .as_deref() + .map(util::respace) + .as_deref() + .map(util::escape_special_chars) .unwrap_or_else(|| interrupt.0.name.clone()) ); diff --git a/src/generate/peripheral.rs b/src/generate/peripheral.rs index 65ca041a..6a183557 100644 --- a/src/generate/peripheral.rs +++ b/src/generate/peripheral.rs @@ -305,7 +305,7 @@ impl Region { let mut idents: Vec<_> = self .rbfs .iter() - .filter_map(|f| f.syn_field.ident.as_ref().map(|ident| ident.to_string())) + .filter_map(|f| f.syn_field.ident.as_ref().map(ToString::to_string)) .collect(); if idents.is_empty() { return None; @@ -343,7 +343,7 @@ impl Region { let idents: Vec<_> = self .rbfs .iter() - .filter_map(|f| f.syn_field.ident.as_ref().map(|ident| ident.to_string())) + .filter_map(|f| f.syn_field.ident.as_ref().map(ToString::to_string)) .collect(); if idents.is_empty() { @@ -726,7 +726,7 @@ fn check_erc_derive_infos( }; match register { Register::Single(_) => { - let ty_name = info_name.to_string(); + let ty_name = info_name.clone(); *derive_info = match explicit_rpath { None => { match compare_this_against_prev( @@ -758,7 +758,7 @@ fn check_erc_derive_infos( let re = Regex::new(format!("^{re_string}$").as_str()).map_err(|_| { anyhow!("Error creating regex for register {}", register.name) })?; - let ty_name = info_name.to_string(); // keep suffix for regex matching + let ty_name = info_name.clone(); // keep suffix for regex matching *derive_info = match explicit_rpath { None => { match compare_this_against_prev( @@ -787,7 +787,7 @@ fn check_erc_derive_infos( } RegisterCluster::Cluster(cluster) => { *derive_info = DeriveInfo::Cluster; - ercs_type_info.push((cluster.name.to_string(), None, erc, derive_info)); + ercs_type_info.push((cluster.name.clone(), None, erc, derive_info)); } }; } diff --git a/src/generate/register.rs b/src/generate/register.rs index b4600dc7..0747b9a4 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -222,7 +222,7 @@ fn api_docs( let idx = format!("[{idx}]"); rpath.name.replace("[%s]", &idx).replace("%s", &idx) } else { - rpath.name.to_string() + rpath.name.clone() }; // TODO: support html_urls for registers in cluster if rpath.block.path.is_empty() { From 8aadb82fe81a5db9481ea91ccca6d77315417345 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 19 Oct 2024 13:25:56 +0300 Subject: [PATCH 4/4] Add warning about indexing register arrays --- CHANGELOG.md | 1 + src/generate/peripheral.rs | 10 ++++++++++ src/generate/peripheral/accessor.rs | 14 +++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbe26557..6eb4b26b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/generate/peripheral.rs b/src/generate/peripheral.rs index 6a183557..9e3101ec 100644 --- a/src/generate/peripheral.rs +++ b/src/generate/peripheral.rs @@ -1052,6 +1052,10 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result`n` is the index of {0} in the array. `n == 0` corresponds to `{first_name}` {0}.", "cluster") + ); accessors.push( Accessor::Array(ArrayAccessor { doc, @@ -1060,6 +1064,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result`n` is the index of {0} in the array. `n == 0` corresponds to `{first_name}` {0}.", "register") + ); accessors.push( Accessor::Array(ArrayAccessor { doc, @@ -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), ); diff --git a/src/generate/peripheral/accessor.rs b/src/generate/peripheral/accessor.rs index 85f663d4..5bb91718 100644 --- a/src/generate/peripheral/accessor.rs +++ b/src/generate/peripheral/accessor.rs @@ -62,10 +62,15 @@ impl ToTokens for AccessType { } } } - Self::Ref(Accessor::Array(ArrayAccessor { doc, name, ty, .. })) => { + Self::Ref(Accessor::Array(ArrayAccessor { doc, name, ty, note, .. })) => { let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site()); + let note = note.as_ref().map(|note| quote! { + #[doc = ""] + #[doc = #note] + }); quote! { #[doc = #doc] + #note #[inline(always)] pub const fn #name(&self, n: usize) -> &#ty { &self.#name[n] @@ -85,14 +90,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::() #offset .add(#increment n).cast() } }; quote! { #[doc = #doc] + #note #[inline(always)] pub const fn #name(&self, n: usize) -> &#ty { #[allow(clippy::no_effect)] @@ -145,6 +156,7 @@ pub struct ArrayAccessor { pub offset: u32, pub dim: u32, pub increment: u32, + pub note: Option, } #[derive(Clone, Debug)]