Skip to content

Commit b2efcc4

Browse files
committed
ir: Add an option to generate an empty, default constructor for tagged enums.
This allows to clean up a pattern that has been showing up lately, see occurrences of: https://searchfox.org/mozilla-central/rev/9775cca0a10a9b5c5f4e15c8f7b3eff5bf91bbd0/servo/ports/geckolib/cbindgen.toml#329
1 parent 25e51f6 commit b2efcc4

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

docs.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ The rest are just local overrides for the same options found in the cbindgen.tom
270270
* derive-tagged-enum-destructor
271271
* derive-tagged-enum-copy-constructor
272272
* prefix-with-name
273+
* private-default-tagged-enum-constructor
273274

274275

275276

@@ -703,6 +704,14 @@ derive_tagged_enum_destructor = false
703704
# default: false
704705
derive_tagged_enum_copy_constructor = false
705706

707+
# Whether enums with fields should generate an empty, private destructor.
708+
# This allows the auto-generated constructor functions to compile, if there are
709+
# non-trivially constructible members. This falls in the same family of
710+
# dangerousness as `derive_tagged_enum_copy_constructor` and co.
711+
#
712+
# default: false
713+
private_default_tagged_enum_constructor = false
714+
706715

707716

708717

src/bindgen/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ pub struct EnumConfig {
400400
pub derive_tagged_enum_destructor: bool,
401401
/// Whether to generate copy-constructors of tagged enums.
402402
pub derive_tagged_enum_copy_constructor: bool,
403+
/// Whether to generate empty, private default-constructors for tagged
404+
/// enums.
405+
pub private_default_tagged_enum_constructor: bool,
403406
}
404407

405408
impl EnumConfig {
@@ -439,6 +442,15 @@ impl EnumConfig {
439442
}
440443
self.derive_tagged_enum_copy_constructor
441444
}
445+
pub(crate) fn private_default_tagged_enum_constructor(
446+
&self,
447+
annotations: &AnnotationSet,
448+
) -> bool {
449+
if let Some(x) = annotations.bool("private-default-tagged-enum-constructor") {
450+
return x;
451+
}
452+
self.private_default_tagged_enum_constructor
453+
}
442454
}
443455

444456
/// Settings to apply to generated constants.

src/bindgen/ir/enumeration.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,22 @@ impl Source for Enum {
890890
}
891891
}
892892

893+
if config.language == Language::Cxx
894+
&& config
895+
.enumeration
896+
.private_default_tagged_enum_constructor(&self.annotations)
897+
{
898+
out.new_line();
899+
out.new_line();
900+
write!(out, "private:");
901+
out.new_line();
902+
write!(out, "{}()", self.export_name);
903+
out.open_brace();
904+
out.close_brace(false);
905+
write!(out, "public:");
906+
out.new_line();
907+
}
908+
893909
if config.language == Language::Cxx
894910
&& config
895911
.enumeration

template.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ derive_mut_casts = false
102102
# cast_assert_name = "ASSERT"
103103
derive_tagged_enum_destructor = false
104104
derive_tagged_enum_copy_constructor = false
105+
private_default_tagged_enum_constructor = false
105106

106107

107108

0 commit comments

Comments
 (0)