Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable respace #879

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
- Added `#![cfg_attr(docsrs, feature(doc_auto_cfg))]` to the generated library code. This
adds a display of the feature gates in the documentation of the generated library
- Split on the start of attribute instead of the end
- Disable whitespace merging in `respace`

## [v0.35.0] - 2024-11-12

Expand Down
1 change: 1 addition & 0 deletions ci/svd2rust-regress/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,4 @@
- arch: cortex-m
mfgr: RaspberryPi
chip: rp2350
svd_url: https://raw.githubusercontent.com/rp-rs/rp235x-pac/refs/heads/main/svd/RP2350.svd.patched
4 changes: 2 additions & 2 deletions src/generate/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ pub fn render(
.0
.description
.as_deref()
.map(util::respace)
.as_deref()
.map(util::escape_special_chars)
.as_deref()
.map(util::respace)
.unwrap_or_else(|| interrupt.0.name.as_str().into())
);

Expand Down
51 changes: 30 additions & 21 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
let p_ty = ident(&name, config, "peripheral", span);
let name_str = p_ty.to_string();
let address = util::hex(p.base_address + config.base_address_shift);
let doc = util::respace(p.description.as_ref().unwrap_or(&name));
let doc = util::escape_special_chars(&doc);
let doc = util::escape_special_chars(p.description.as_ref().unwrap_or(&name));
let doc = util::respace(&doc);
let doc = quote! { #[doc = #doc] };

let mod_ty = ident(&name, config, "peripheral_mod", span);
let (derive_regs, base, path) = if let Some(path) = path {
Expand All @@ -65,17 +66,17 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result

let phtml = config.settings.html_url.as_ref().map(|url| {
let doc = format!("See peripheral [structure]({url}#{})", &path.peripheral);
quote!(#[doc = ""] #[doc = #doc])
quote!(#[doc = ""] #doc)
});

let per_to_tokens = |out: &mut TokenStream,
feature_attribute: &TokenStream,
doc: &str,
doc: &TokenStream,
p_ty: &Ident,
doc_alias: Option<TokenStream>,
address: LitInt| {
out.extend(quote! {
#[doc = #doc]
#doc
#phtml
#doc_alias
#feature_attribute
Expand All @@ -95,8 +96,9 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
let mut feature_names = Vec::with_capacity(dim.dim as _);
for pi in svd::peripheral::expand(p, dim) {
let name = &pi.name;
let doc = util::respace(pi.description.as_ref().unwrap_or(&pi.name));
let doc = util::escape_special_chars(&doc);
let doc = util::escape_special_chars(pi.description.as_ref().unwrap_or(&pi.name));
let doc = util::respace(&doc);
let doc = quote! { #[doc = #doc] };
let p_ty = ident(name, config, "peripheral", span);
let name_str = p_ty.to_string();
let doc_alias = (&name_str != name).then(|| quote!(#[doc(alias = #name)]));
Expand Down Expand Up @@ -125,7 +127,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
if derive_regs {
// re-export the base module to allow deriveFrom this one
out.extend(quote! {
#[doc = #doc]
#doc
#feature_any_attribute
pub use self::#base as #mod_ty;
});
Expand All @@ -145,7 +147,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
if derive_regs {
// re-export the base module to allow deriveFrom this one
out.extend(quote! {
#[doc = #doc]
#doc
#feature_attribute
pub use self::#base as #mod_ty;
});
Expand Down Expand Up @@ -188,11 +190,17 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
"Pushing {} register or cluster blocks into output",
ercs.len()
);
let reg_block =
register_or_cluster_block(&ercs, &derive_infos, None, "Register block", None, config)?;
let reg_block = register_or_cluster_block(
&ercs,
&derive_infos,
None,
&quote! { #[doc = "Register block"] },
None,
config,
)?;

out.extend(quote! {
#[doc = #doc]
#doc
#feature_attribute
pub mod #mod_ty
});
Expand Down Expand Up @@ -457,8 +465,8 @@ impl FieldRegions {
}

fn make_comment(size: u32, offset: u32, description: &str) -> String {
let desc = util::respace(description);
let desc = util::escape_special_chars(&desc);
let desc = util::escape_special_chars(description);
let desc = util::respace(&desc);
if size > 32 {
let end = offset + size / 8;
format!("0x{offset:02x}..0x{end:02x} - {desc}")
Expand All @@ -471,7 +479,7 @@ fn register_or_cluster_block(
ercs: &[RegisterCluster],
derive_infos: &[DeriveInfo],
name: Option<&str>,
doc: &str,
doc: &TokenStream,
size: Option<u32>,
config: &Config,
) -> Result<TokenStream> {
Expand Down Expand Up @@ -598,7 +606,7 @@ fn register_or_cluster_block(
Ok(quote! {
#[repr(C)]
#derive_debug
#[doc = #doc]
#doc
#doc_alias
pub struct #block_ty {
#rbfs
Expand Down Expand Up @@ -1328,8 +1336,9 @@ fn cluster_block(
config: &Config,
) -> Result<TokenStream> {
let doc = c.description.as_ref().unwrap_or(&c.name);
let doc = util::respace(doc);
let doc = util::escape_special_chars(&doc);
let doc = util::escape_special_chars(doc);
let doc = util::respace(&doc);
let doc = quote! { #[doc = #doc] };
let mod_name = c.name.remove_dim().to_string();

// name_snake_case needs to take into account array type.
Expand All @@ -1356,7 +1365,7 @@ fn cluster_block(
.push(path_segment(ident(&dname, config, "cluster_mod", span)));

Ok(quote! {
#[doc = #doc]
#doc
pub use #derived as #block_ty;
pub use #mod_derived as #mod_ty;
})
Expand Down Expand Up @@ -1388,11 +1397,11 @@ fn cluster_block(
};

Ok(quote! {
#[doc = #doc]
#doc
pub use self::#mod_ty::#block_ty;

///Cluster
#[doc = #doc]
#doc
pub mod #mod_ty {
#mod_items
}
Expand Down
29 changes: 16 additions & 13 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ pub fn render(
let reg_ty = ident(&name, config, "register", span);
let doc_alias = (reg_ty.to_string().as_str() != name).then(|| quote!(#[doc(alias = #name)]));
let mod_ty = ident(&name, config, "register_mod", span);
let description = util::respace(register.description.as_deref().unwrap_or_else(|| {
warn!("Missing description for register {}", register.name);
""
}));
let description = util::escape_special_chars(&description);
let description =
util::escape_special_chars(register.description.as_deref().unwrap_or_else(|| {
warn!("Missing description for register {}", register.name);
""
}));
let description = util::respace(&description);

if let Some(dpath) = dpath.as_ref() {
let mut derived = if &dpath.block == path {
Expand Down Expand Up @@ -669,7 +670,8 @@ pub fn fields(
span,
);
let description_raw = f.description.as_deref().unwrap_or(""); // raw description, if absent using empty string
let description = util::respace(&util::escape_special_chars(description_raw));
let description = util::escape_special_chars(description_raw);
let description = util::respace(&description);

let can_read = can_read
&& (f.access != Some(Access::WriteOnly))
Expand Down Expand Up @@ -901,8 +903,8 @@ pub fn fields(
let pc = &v.pc;
let is_variant = &v.is_sc;

let doc = util::respace(&v.doc);
let doc = util::escape_special_chars(&doc);
let doc = util::escape_special_chars(&v.doc);
let doc = util::respace(&doc);
enum_items.extend(quote! {
#[doc = #doc]
#inline
Expand All @@ -915,8 +917,8 @@ pub fn fields(
let pc = &v.pc;
let is_variant = &v.is_sc;

let doc = util::respace(&v.doc);
let doc = util::escape_special_chars(&doc);
let doc = util::escape_special_chars(&v.doc);
let doc = util::respace(&doc);
enum_items.extend(quote! {
#[doc = #doc]
#inline
Expand Down Expand Up @@ -1179,8 +1181,8 @@ pub fn fields(
for v in &variants {
let pc = &v.pc;
let sc = &v.sc;
let doc = util::respace(&v.doc);
let doc = util::escape_special_chars(&doc);
let doc = util::escape_special_chars(&v.doc);
let doc = util::respace(&doc);
proxy_items.extend(quote! {
#[doc = #doc]
#inline
Expand Down Expand Up @@ -1542,8 +1544,9 @@ fn add_from_variants<'a>(

let mut vars = TokenStream::new();
for v in variants.map(|v| {
let desc = util::respace(&format!("{}: {}", v.value, v.doc));
let desc = format!("{}: {}", v.value, v.doc);
let desc = util::escape_special_chars(&desc);
let desc = util::respace(&desc);
let pcv = &v.pc;
let pcval = &unsuffixed(v.value);
quote! {
Expand Down
4 changes: 2 additions & 2 deletions src/generate/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ pub fn render(
"{} - {}",
i.value,
i.description
.as_ref()
.map(|s| util::respace(s))
.as_ref()
.map(|s| util::escape_special_chars(s))
.as_ref()
.map(|s| util::respace(s))
.unwrap_or_else(|| i.name.as_str().into())
);

Expand Down
12 changes: 7 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ pub fn sanitize_keyword(sc: Cow<str>) -> Cow<str> {
}
}

pub fn respace(s: &str) -> String {
s.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
.replace(r"\n", "\n")
pub fn respace(s: &str) -> Cow<'_, str> {
if s.contains("\n\n ") {
let ss: Vec<_> = s.split("\n\n").map(|s| s.trim_start()).collect();
ss.join("\n\n").into()
} else {
s.into()
}
}

pub fn escape_brackets(s: &str) -> String {
Expand Down