Major rework to the union support#437
Conversation
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/visitors/union.rb:
* ridlbe/c++11/writers/stubheader.rb:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/visitors/union.rb:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/templates/cli/inl/union_inl.erb:
* tests/union/test.idl:
* ridlbe/c++11/templates/cli/inl/union_inl.erb:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/templates/cli/inl/union_inl.erb:
* ridlbe/c++11/templates/cli/inl/union_inl.erb:
* tests/union/client.cpp:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/visitors/union.rb:
* tests/union/checks.h:
📝 WalkthroughWalkthroughReplace manual union storage/management with std::variant and index-based in-place initialization; switch special-member declarations to = default; rename value_initializer → default_value; add Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@tests/union/test.idl`:
- Around line 187-193: Add runtime test coverage for the DefaultDataTypeDef
union (and its typedef mylong) by mirroring the existing tests for DefaultData:
declare matching interface methods in the IDL (same signatures used for
DefaultData) and implement runtime checks in client.cpp (e.g., a new test
function similar to test_data_default_union()) that exercises the case 1, case 2
and default mylong variant, performs round-trip calls and asserts correct
discriminator/value handling to ensure the typedef chain is honored by codegen
and execution.
🧹 Nitpick comments (1)
ridlbe/c++11/visitors/union.rb (1)
194-209: Consider extracting shared logic withvalue_initializer.The
default_valuemethod closely mirrorsvalue_initializer(lines 177-192) — both traverse the typedef chain looking for@defaultannotations. The only difference is the output format (with vs. without braces).This duplication could be reduced by extracting the shared traversal logic into a private helper.
♻️ Optional refactor to reduce duplication
+ private + + def find_default_annotation_value + unless node.annotations[:default].first.nil? + return node.annotations[:default].first.fields[:value] + end + res_idl_type = _idltype + while res_idl_type.is_a?(IDL::Type::ScopedName) + unless res_idl_type.node.annotations[:default].first.nil? + return res_idl_type.node.annotations[:default].first.fields[:value] + end + res_idl_type = res_idl_type.node.idltype + end + nil + end + + public + def value_initializer - unless node.annotations[:default].first.nil? - "{#{node.annotations[:default].first.fields[:value]}}" - else - res_idl_type = _idltype - while res_idl_type.is_a?(IDL::Type::ScopedName) - unless res_idl_type.node.annotations[:default].first.nil? - return "{#{res_idl_type.node.annotations[:default].first.fields[:value]}}" - end - res_idl_type = res_idl_type.node.idltype - end - _resolved_idltype.value_initializer - end + val = find_default_annotation_value + val ? "{#{val}}" : _resolved_idltype.value_initializer end def default_value - unless node.annotations[:default].first.nil? - "#{node.annotations[:default].first.fields[:value]}" - else - res_idl_type = _idltype - while res_idl_type.is_a?(IDL::Type::ScopedName) - unless res_idl_type.node.annotations[:default].first.nil? - return "#{res_idl_type.node.annotations[:default].first.fields[:value]}" - end - res_idl_type = res_idl_type.node.idltype - end - _resolved_idltype.default_value - end + val = find_default_annotation_value + val ? val.to_s : _resolved_idltype.default_value end
* ridlbe/c++11/visitors/union.rb:
|
See RemedyIT/idl2cpp11#81 how this PR heavily reduces the generated code for unions |
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/templates/cli/inl/union_inl.erb:
* tests/union/checks.h:
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@ridlbe/c`++11/templates/cli/inl/union_inl.erb:
- Around line 150-156: The move setter unconditionally assigns this->disc_ =
_x11_disc even when _x11_disc isn’t a parameter (only present when
_m.has_multiple_discriminators?); fix by using the correct discriminator value:
if _m.has_multiple_discriminators? keep using _x11_disc, otherwise assign the
concrete label (e.g., the first nondefault label used previously) — compute that
label as _lbl in this scope or hoist its computation so the move setter can set
this->disc_ correctly before emplacing into this->u_.emplace<<%= _index
%>>(std::move(_x11_<%= _m.cxxname %>)).
🧹 Nitpick comments (2)
ridlbe/c++11/templates/cli/hdr/union_post.erb (2)
26-26: Consider adding[[nodiscard]]to the discriminator getter.The AI summary indicates
_d()was updated to use[[nodiscard]], but the code shows it without this attribute. Based on learnings, TAOX11 requires a C++17 capable compiler, so[[nodiscard]]is safe to use in generated code and would be beneficial here to warn callers who discard the discriminator value.,
Suggested change
- inline <%= switch_in_cxxtype %> _d () const { return this->disc_; } + [[nodiscard]] inline <%= switch_in_cxxtype %> _d () const { return this->disc_; }
44-49: Consider adding[[nodiscard]]to member accessors.Similarly, the AI summary mentions
[[nodiscard]]on member accessors, but these declarations lack the attribute. Since discarding the return value of these getters is almost certainly a logic error,[[nodiscard]]would help catch such mistakes at compile time.,
Suggested change
- inline <%= _m.cxx_in_type %> <%= _m.cxxname %> () const; + [[nodiscard]] inline <%= _m.cxx_in_type %> <%= _m.cxxname %> () const; /// Get the value of the union, if the discriminator doesn't match a /// BAD_PARAM exception is thrown - inline <%= _m.cxx_out_type %> <%= _m.cxxname %> (); + [[nodiscard]] inline <%= _m.cxx_out_type %> <%= _m.cxxname %> ();
* ridlbe/c++11/templates/cli/inl/union_inl.erb:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/templates/cli/inl/union_inl.erb:
* ridlbe/c++11/templates/cli/hdr/union_post.erb:
* ridlbe/c++11/templates/cli/inl/union_inl.erb:
Leverage
std::variantas type-safe union which reduces the generated code for IDL unions significantly.Summary by CodeRabbit
Improvements
Refactor
Tests