From 54a1098f903134d247599275997e62c2f93ce9e7 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 11 Aug 2024 00:20:17 +0900 Subject: [PATCH] replace is_first_field to enumerate --- src/bindgen/ir/constant.rs | 2 +- src/bindgen/language_backend/clike.rs | 31 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index 2f1d3bff..ca6499d8 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -184,7 +184,7 @@ impl Literal { } } - fn can_be_constexpr(&self) -> bool { + pub(crate) fn can_be_constexpr(&self) -> bool { !self.has_pointer_casts() } diff --git a/src/bindgen/language_backend/clike.rs b/src/bindgen/language_backend/clike.rs index bf5a39fb..638e01d6 100644 --- a/src/bindgen/language_backend/clike.rs +++ b/src/bindgen/language_backend/clike.rs @@ -893,6 +893,9 @@ impl LanguageBackend for CLikeLanguageBackend<'_> { fields, path, } => { + let allow_constexpr = self.config.constant.allow_constexpr && l.can_be_constexpr(); + let is_constexpr = self.config.language == Language::Cxx + && (self.config.constant.allow_static_const || allow_constexpr); if self.config.language == Language::C { write!(out, "({})", export_name); } else { @@ -900,23 +903,33 @@ impl LanguageBackend for CLikeLanguageBackend<'_> { } write!(out, "{{ "); - let mut is_first_field = true; // In C++, same order as defined is required. let ordered_fields = out.bindings().struct_field_names(path); - for ordered_key in ordered_fields.iter() { + for (i, ordered_key) in ordered_fields.iter().enumerate() { if let Some(lit) = fields.get(ordered_key) { - if !is_first_field { - write!(out, ", "); - } - is_first_field = false; - if self.config.language == Language::Cxx { + if is_constexpr { + if i > 0 { + write!(out, ", "); + } + // TODO: Some C++ versions (c++20?) now support designated // initializers, consider generating them. write!(out, "/* .{} = */ ", ordered_key); + self.write_literal(out, lit); } else { - write!(out, ".{} = ", ordered_key); + if i > 0 { + write!(out, ", "); + } + + if self.config.language == Language::Cxx { + // TODO: Some C++ versions (c++20?) now support designated + // initializers, consider generating them. + write!(out, "/* .{} = */ ", ordered_key); + } else { + write!(out, ".{} = ", ordered_key); + } + self.write_literal(out, lit); } - self.write_literal(out, lit); } } write!(out, " }}");