Skip to content

Commit

Permalink
feature format fix
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 3, 2024
1 parent 0dd0e3f commit 1fd0fbe
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 36 deletions.
19 changes: 4 additions & 15 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
config,
)?);

let feature_format = config.ident_formats.get("peripheral_feature").unwrap();
for p in &d.peripherals {
if config.target == Target::CortexM
&& core_peripherals.contains(&p.name.to_uppercase().as_ref())
Expand Down Expand Up @@ -226,23 +227,15 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
}
let mut feature_attribute = TokenStream::new();
if config.feature_group && p.group_name.is_some() {
let feature_name = config
.ident_formats
.get("peripheral_feature")
.unwrap()
.apply(p.group_name.as_deref().unwrap());
let feature_name = feature_format.apply(p.group_name.as_deref().unwrap());
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] })
};

let span = Span::call_site();
match p {
Peripheral::Single(_p) => {
let p_name = util::name_of(p, config.ignore_groups);
let p_feature = config
.ident_formats
.get("peripheral_feature")
.unwrap()
.apply(&p_name);
let p_feature = feature_format.apply(&p_name);
let p_ty = ident(&p_name, &config, "peripheral", span);
let p_singleton = ident(&p_name, &config, "peripheral_sigleton", span);
if config.feature_peripheral {
Expand All @@ -259,11 +252,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
}
Peripheral::Array(p, dim_element) => {
for p_name in names(p, dim_element) {
let p_feature = config
.ident_formats
.get("peripheral_feature")
.unwrap()
.apply(&p_name);
let p_feature = feature_format.apply(&p_name);
let p_ty = ident(&p_name, &config, "peripheral", span);
let p_singleton = ident(&p_name, &config, "peripheral_sigleton", span);
if config.feature_peripheral {
Expand Down
7 changes: 4 additions & 3 deletions src/generate/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::svd::Peripheral;
use proc_macro2::{Span, TokenStream};
use quote::quote;

use crate::util::{self, ident, ToSanitizedCase};
use crate::util::{self, ident};
use crate::{Config, Target};
use anyhow::Result;

Expand Down Expand Up @@ -47,6 +47,7 @@ pub fn render(
let mut pos = 0;
let mut mod_items = TokenStream::new();
let span = Span::call_site();
let feature_format = config.ident_formats.get("peripheral_feature").unwrap();
for interrupt in &interrupts {
while pos < interrupt.0.value {
elements.extend(quote!(Vector { _reserved: 0 },));
Expand Down Expand Up @@ -74,13 +75,13 @@ pub fn render(
let mut feature_attribute = TokenStream::new();
let mut not_feature_attribute = TokenStream::new();
if config.feature_group && interrupt.1.is_some() {
let feature_name = interrupt.1.as_ref().unwrap().to_sanitized_snake_case();
let feature_name = feature_format.apply(interrupt.1.as_ref().unwrap());
feature_attribute_flag = true;
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] });
not_feature_attribute.extend(quote! { feature = #feature_name, });
}
if config.feature_peripheral {
let feature_name = interrupt.2.to_sanitized_snake_case();
let feature_name = feature_format.apply(&interrupt.2);
feature_attribute_flag = true;
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] });
not_feature_attribute.extend(quote! { feature = #feature_name, });
Expand Down
15 changes: 4 additions & 11 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
};

let mut feature_attribute = TokenStream::new();
let feature_format = config.ident_formats.get("peripheral_feature").unwrap();
if config.feature_group && p.group_name.is_some() {
let feature_name = p.group_name.as_ref().unwrap().to_sanitized_snake_case();
let feature_name = feature_format.apply(p.group_name.as_ref().unwrap());
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] });
};

Expand Down Expand Up @@ -85,11 +86,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
let name_str = p_ty.to_string();
let doc_alias = (&name_str != name).then(|| quote!(#[doc(alias = #name)]));
let address = util::hex(pi.base_address + config.base_address_shift);
let p_feature = config
.ident_formats
.get("peripheral_feature")
.unwrap()
.apply(name);
let p_feature = feature_format.apply(name);
feature_names.push(p_feature.to_string());
let mut feature_attribute_n = feature_attribute.clone();
if config.feature_peripheral {
Expand Down Expand Up @@ -153,11 +150,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
}
}
Peripheral::Single(_) => {
let p_feature = config
.ident_formats
.get("peripheral_feature")
.unwrap()
.apply(&name);
let p_feature = feature_format.apply(&name);
if config.feature_peripheral {
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
};
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,11 @@ Ignore this option if you are not building your own FPGA based soft-cores."),
}
if config.feature_peripheral {
features.extend(
util::peripheral_names(&device)
util::peripheral_names(&device, &config)
.iter()
.map(|s| format!("{s} = []\n")),
);
let add_peripherals: Vec<_> = util::peripheral_names(&device)
let add_peripherals: Vec<_> = util::peripheral_names(&device, &config)
.iter()
.map(|s| format!("\"{s}\""))
.collect();
Expand Down
9 changes: 4 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,17 +501,16 @@ pub fn group_names(d: &Device) -> Vec<Cow<str>> {
v
}

pub fn peripheral_names(d: &Device) -> Vec<String> {
pub fn peripheral_names(d: &Device, config: &Config) -> Vec<String> {
let mut v = Vec::new();
let feature_format = config.ident_formats.get("peripheral_feature").unwrap();
for p in &d.peripherals {
match p {
Peripheral::Single(info) => {
v.push(replace_suffix(&info.name.to_sanitized_snake_case(), ""));
v.push(replace_suffix(&feature_format.apply(&info.name), ""));
}
Peripheral::Array(info, dim) => {
v.extend(
svd_rs::array::names(info, dim).map(|n| n.to_sanitized_snake_case().into()),
);
v.extend(svd_rs::array::names(info, dim).map(|n| feature_format.apply(&n).into()));
}
}
}
Expand Down

0 comments on commit 1fd0fbe

Please sign in to comment.