Skip to content
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
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# unreleased

* Wrap conditional fields in `#if` blocks in generated C++ `operator==` and
`operator!=`, so headers still compile when a feature is disabled (#937).

* Emit `&str`, `&CStr`, and arrays thereof as C string literals, and byte-string
(`b"..."`) constants as `uint8_t[]` arrays, instead of dropping them.

Expand Down
43 changes: 30 additions & 13 deletions src/bindgen/language_backend/clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,36 @@ impl<'a> CLikeLanguageBackend<'a> {
other
);
out.open_brace();
out.write("return ");
let vec: Vec<_> = s
.fields
.iter()
.skip(skip_fields)
.map(|field| format!("{} {} {}.{}", field.name, $op, other, field.name))
.collect();
out.write_vertical_source_list(
self,
&vec[..],
ListType::Join(&format!(" {}", $conjuc)),
|_, out, s| write!(out, "{}", s),
);
out.write("return");
let fields = &s.fields[skip_fields..];
if matches!($op, "==" | "!=") && fields.iter().any(|field| field.cfg.is_some()) {
out.new_line();
for field in fields {
let condition = field.cfg.to_condition(self.config);
condition.write_before(self.config, out);
write!(
out,
"{} {} {}.{} {}",
field.name, $op, other, field.name, $conjuc
);
condition.write_after(self.config, out);
out.new_line();
}
// A neutral operand keeps the expression valid even if all fields are absent.
write!(out, "{}", $op == "==");
} else {
out.write(" ");
let vec: Vec<_> = fields
.iter()
.map(|field| format!("{} {} {}.{}", field.name, $op, other, field.name))
.collect();
out.write_vertical_source_list(
self,
&vec[..],
ListType::Join(&format!(" {}", $conjuc)),
|_, out, s| write!(out, "{}", s),
);
}
out.write(";");
out.close_brace(false);
}};
Expand Down
15 changes: 15 additions & 0 deletions tests/expectations/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ struct ConditionalField {
int32_t field
#endif
;

bool operator==(const ConditionalField& other) const {
return
#if defined(X11)
field == other.field &&
#endif
true;
}
bool operator!=(const ConditionalField& other) const {
return
#if defined(X11)
field != other.field ||
#endif
false;
}
};
constexpr static const ConditionalField ConditionalField_ZERO = ConditionalField{
#if defined(X11)
Expand Down
3 changes: 0 additions & 3 deletions tests/rust/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ struct BarHandle {
y: f32,
}

// FIXME(#634): Support deriving methods for structs with conditional fields.
/// cbindgen:derive-eq=false
/// cbindgen:derive-neq=false
#[repr(C)]
struct ConditionalField {
#[cfg(x11)]
Expand Down