Skip to content

Commit

Permalink
format variant with optional brackets around the sum (bytecodeallianc…
Browse files Browse the repository at this point in the history
…e#2048)

wit-bindgen generated Rust code warns because of unnecessary brackets
generated by the format() function
  • Loading branch information
cpetig authored Feb 11, 2025
1 parent eabdf0e commit beac7de
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions crates/wit-parser/src/sizealign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,38 @@ impl ArchitectureSize {

// create a suitable expression in bytes from a pointer size argument
pub fn format(&self, ptrsize_expr: &str) -> String {
self.format_term(ptrsize_expr, false)
}

// create a suitable expression in bytes from a pointer size argument,
// extended API with optional brackets around the sum
pub fn format_term(&self, ptrsize_expr: &str, suppress_brackets: bool) -> String {
if self.pointers != 0 {
if self.bytes > 0 {
// both
format!(
"({}+{}*{ptrsize_expr})",
self.constant_bytes(),
self.pointers_to_add()
)
if suppress_brackets {
format!(
"{}+{}*{ptrsize_expr}",
self.constant_bytes(),
self.pointers_to_add()
)
} else {
format!(
"({}+{}*{ptrsize_expr})",
self.constant_bytes(),
self.pointers_to_add()
)
}
} else if self.pointers == 1 {
// one pointer
ptrsize_expr.into()
} else {
// only pointer
format!("({}*{ptrsize_expr})", self.pointers_to_add())
if suppress_brackets {
format!("{}*{ptrsize_expr}", self.pointers_to_add())
} else {
format!("({}*{ptrsize_expr})", self.pointers_to_add())
}
}
} else {
// only bytes
Expand Down

0 comments on commit beac7de

Please sign in to comment.