Skip to content

Commit

Permalink
Add formatting to the capi identifier as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter-Reactor committed Feb 6, 2025
1 parent f383467 commit a8e411b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 32 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ heck = "0.4"
displaydoc = "0.2"
askama = "0.12"
once_cell = "1.20.2"
itertools = "0.14.0"

[dev-dependencies]
insta = { version = "1.7.1", features = ["yaml"] }
Expand Down
24 changes: 24 additions & 0 deletions tool/src/c/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,30 @@ impl<'tcx> CFormatter<'tcx> {
)
}

pub(crate) fn fmt_identifier<'a>(&self, name: Cow<'a, str>) -> Cow<'a, str> {
// TODO(#60): handle other keywords
// TODO: Replace with LazyLock when MSRV is bumped to >= 1.80.0
static C_KEYWORDS: once_cell::sync::Lazy<std::collections::HashSet<&str>> =
once_cell::sync::Lazy::new(|| [].into());

static CPP_KEYWORDS: once_cell::sync::Lazy<std::collections::HashSet<&str>> =
once_cell::sync::Lazy::new(|| ["new", "default", "delete"].into());

let lang_keywords = {
if self.is_for_cpp {
&CPP_KEYWORDS
} else {
&C_KEYWORDS
}
};

if lang_keywords.contains(name.as_ref()) {
format!("{name}_").into()
} else {
name
}
}

fn diplomat_namespace(&self, ty: Cow<'tcx, str>) -> Cow<'tcx, str> {
if self.is_for_cpp {
format!("diplomat::{CAPI_NAMESPACE}::{ty}").into()
Expand Down
26 changes: 11 additions & 15 deletions tool/src/c/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,17 @@ impl<'tcx> TyGenContext<'_, 'tcx> {
_ => unreachable!("unknown AST/HIR variant"),
};

let mut params = String::new();
let mut first = true;
for (decl_ty, decl_name) in param_decls {
let comma = if first {
first = false;
""
} else {
", "
};
write!(&mut params, "{comma}{decl_ty} {decl_name}").unwrap();
}

if params.is_empty() {
params.push_str("void");
}
use itertools::Itertools;
let params = if !param_decls.is_empty() {
param_decls
.into_iter()
.map(|(ty, name)| {
format!("{ty} {name}", name = self.formatter.fmt_identifier(name))
})
.join(", ")
} else {
"void".to_owned()
};

(
MethodTemplate {
Expand Down
11 changes: 1 addition & 10 deletions tool/src/cpp/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,7 @@ impl<'tcx> Cpp2Formatter<'tcx> {

/// Replace any keywords used
pub fn fmt_identifier<'a>(&self, name: Cow<'a, str>) -> Cow<'a, str> {
// TODO(#60): handle other keywords
// TODO: Replace with LazyLock when MSRV is bumped to >= 1.80.0
static KEYWORDS: once_cell::sync::Lazy<HashSet<&str>> =
once_cell::sync::Lazy::new(|| ["new", "default", "delete"].into());

if KEYWORDS.contains(name.as_ref()) {
format!("{name}_").into()
} else {
name
}
self.c.fmt_identifier(name)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tool/src/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ mod test {
struct MyStruct(u64);

impl MyStruct {
pub fn keywordparam(&self, default: u8) {
pub fn new(&self, default: u8) {
self.0 = default;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace diplomat {
namespace capi {
extern "C" {

void MyStruct_keywordparam(const diplomat::capi::MyStruct* self, uint8_t default);
void MyStruct_new(const diplomat::capi::MyStruct* self, uint8_t default_);


void MyStruct_destroy(MyStruct* self);
Expand All @@ -15,8 +15,8 @@ namespace capi {
} // namespace capi
} // namespace

inline void MyStruct::keywordparam(uint8_t default_) const {
diplomat::capi::MyStruct_keywordparam(this->AsFFI(),
inline void MyStruct::new_(uint8_t default_) const {
diplomat::capi::MyStruct_new(this->AsFFI(),
default_);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace capi {
class MyStruct {
public:

inline void keywordparam(uint8_t default_) const;
inline void new_(uint8_t default_) const;

inline const diplomat::capi::MyStruct* AsFFI() const;
inline diplomat::capi::MyStruct* AsFFI();
Expand Down

0 comments on commit a8e411b

Please sign in to comment.