diff --git a/compiler/extensions/cpp/freemarker/CompoundField.inc.ftl b/compiler/extensions/cpp/freemarker/CompoundField.inc.ftl index 94638f076..3d409e442 100644 --- a/compiler/extensions/cpp/freemarker/CompoundField.inc.ftl +++ b/compiler/extensions/cpp/freemarker/CompoundField.inc.ftl @@ -1108,7 +1108,7 @@ ${I}context.${field.getterName}().init<<@array_traits_type_name field/>>(<#rt> <#if hasChoiceTag || uses_packing_context(fieldList)> public: <#if hasChoiceTag> - ::zserio::DeltaContext& getChoiceTag() + ::zserio::DeltaContext& choiceTag() { return m_choiceTag; } diff --git a/compiler/extensions/cpp/freemarker/Union.cpp.ftl b/compiler/extensions/cpp/freemarker/Union.cpp.ftl index 53ed55308..582661749 100644 --- a/compiler/extensions/cpp/freemarker/Union.cpp.ftl +++ b/compiler/extensions/cpp/freemarker/Union.cpp.ftl @@ -466,7 +466,7 @@ ${name}::ChoiceTag ${name}::choiceTag() const void ${name}::initPackingContext(${name}::ZserioPackingContext& context) const { - context.getChoiceTag().init<${choiceTagArrayTraits}>(static_cast(m_choiceTag)); + context.choiceTag().init<${choiceTagArrayTraits}>(static_cast(m_choiceTag)); switch (m_choiceTag) { @@ -510,7 +510,7 @@ size_t ${name}::bitSizeOf(${name}::ZserioPackingContext& context, size_t bitPosi { size_t endBitPosition = bitPosition; - endBitPosition += context.getChoiceTag().bitSizeOf<${choiceTagArrayTraits}>(static_cast(m_choiceTag)); + endBitPosition += context.choiceTag().bitSizeOf<${choiceTagArrayTraits}>(static_cast(m_choiceTag)); switch (m_choiceTag) { @@ -557,7 +557,7 @@ size_t ${name}::initializeOffsets(${name}::ZserioPackingContext& context, size_t { size_t endBitPosition = bitPosition; - endBitPosition += context.getChoiceTag().bitSizeOf<${choiceTagArrayTraits}>(static_cast(m_choiceTag)); + endBitPosition += context.choiceTag().bitSizeOf<${choiceTagArrayTraits}>(static_cast(m_choiceTag)); switch (m_choiceTag) { @@ -701,7 +701,7 @@ void ${name}::write(::zserio::BitStreamWriter&<#if fieldList?has_content> out(out, static_cast(m_choiceTag)); + context.choiceTag().write<${choiceTagArrayTraits}>(out, static_cast(m_choiceTag)); switch (m_choiceTag) { @@ -734,7 +734,7 @@ ${name}::ChoiceTag ${name}::readChoiceTag(::zserio::BitStreamReader& in) ${name}::ChoiceTag ${name}::readChoiceTag(${name}::ZserioPackingContext& context, ::zserio::BitStreamReader& in) { - return static_cast<${name}::ChoiceTag>(static_cast(context.getChoiceTag().read<${choiceTagArrayTraits}>(in))); + return static_cast<${name}::ChoiceTag>(static_cast(context.choiceTag().read<${choiceTagArrayTraits}>(in))); } diff --git a/compiler/extensions/java/freemarker/CompoundField.inc.ftl b/compiler/extensions/java/freemarker/CompoundField.inc.ftl index d2abffcab..691520c84 100644 --- a/compiler/extensions/java/freemarker/CompoundField.inc.ftl +++ b/compiler/extensions/java/freemarker/CompoundField.inc.ftl @@ -620,7 +620,7 @@ ${I}<@compound_get_field field/>.initPackingContext(zserioContext.${field.getter } <#if hasChoiceTag> - public zserio.runtime.array.DeltaContext getChoiceTag() + public zserio.runtime.array.DeltaContext choiceTag() { return choiceTag; } diff --git a/compiler/extensions/java/freemarker/Union.java.ftl b/compiler/extensions/java/freemarker/Union.java.ftl index f7a3c708d..f22e86f35 100644 --- a/compiler/extensions/java/freemarker/Union.java.ftl +++ b/compiler/extensions/java/freemarker/Union.java.ftl @@ -135,7 +135,7 @@ public class ${name} implements <#rt> public void initPackingContext(zserio.runtime.array.PackingContext context) { final ZserioPackingContext zserioContext = context.cast(); - zserioContext.getChoiceTag().init( + zserioContext.choiceTag().init( new ${choiceTagArrayTraits}(), new ${choiceTagArrayElement}(choiceTag)); @@ -190,7 +190,7 @@ public class ${name} implements <#rt> final ZserioPackingContext zserioContext = context.cast(); long endBitPosition = bitPosition; - endBitPosition += zserioContext.getChoiceTag().bitSizeOf( + endBitPosition += zserioContext.choiceTag().bitSizeOf( new ${choiceTagArrayTraits}(), new ${choiceTagArrayElement}(choiceTag)); @@ -370,7 +370,7 @@ public class ${name} implements <#rt> { final ZserioPackingContext zserioContext = context.cast(); choiceTag = ((${choiceTagArrayElement}) - zserioContext.getChoiceTag().read( + zserioContext.choiceTag().read( new ${choiceTagArrayTraits}(), in)).get(); switch (choiceTag) @@ -426,7 +426,7 @@ public class ${name} implements <#rt> final ZserioPackingContext zserioContext = context.cast(); long endBitPosition = bitPosition; - endBitPosition += zserioContext.getChoiceTag().bitSizeOf( + endBitPosition += zserioContext.choiceTag().bitSizeOf( new ${choiceTagArrayTraits}(), new ${choiceTagArrayElement}(choiceTag)); @@ -471,7 +471,7 @@ public class ${name} implements <#rt> throws java.io.IOException { final ZserioPackingContext zserioContext = context.cast(); - zserioContext.getChoiceTag().write( + zserioContext.choiceTag().write( new ${choiceTagArrayTraits}(), out, new ${choiceTagArrayElement}(choiceTag)); diff --git a/test/data b/test/data index fc851cef4..6f9fd1814 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit fc851cef493687c3e653d442a73e22ea53691b55 +Subproject commit 6f9fd1814a1c60ff4dfb37757e7803f7cfe92445 diff --git a/test/extensions/language/array_types/CMakeLists.txt b/test/extensions/language/array_types/CMakeLists.txt index 9221dff80..8adafa2a5 100644 --- a/test/extensions/language/array_types/CMakeLists.txt +++ b/test/extensions/language/array_types/CMakeLists.txt @@ -14,15 +14,34 @@ if (ZSERIO_LOG) check_zserio_warnings("${ZSERIO_LOG}" 0) endif () +add_library(choice_tag_clash_zs STATIC ${TEST_ZS_ROOT}/choice_tag_clash.zs) +zserio_generate_cpp( + TARGET choice_tag_clash_zs + SRC_DIR ${TEST_ZS_ROOT} + GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen_choice_tag_clash + EXTRA_ARGS -withoutSourcesAmalgamation -withoutCrossExtensionCheck ${ZSERIO_EXTRA_ARGS} + GENERATED_SOURCES_VAR GENERATED_SOURCES_CHOICE_TAG_CLASH + OUTPUT_VAR ZSERIO_LOG_CHOICE_TAG_CLASH + ERROR_VAR ZSERIO_LOG_CHOICE_TAG_CLASH +) +target_link_libraries(choice_tag_clash_zs PUBLIC ZserioCppRuntime) +if (ZSERIO_LOG_CHOICE_TAG_CLASH) + file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/zserio_log_choice_tag_clash.txt + "${ZSERIO_LOG_CHOICE_TAG_CLASH}") + check_zserio_warnings("${ZSERIO_LOG_CHOICE_TAG_CLASH}" 0) +endif () + add_custom_test(array_types DEPENDS array_types_zs + choice_tag_clash_zs SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ArraysMappingTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/AutoArrayBitfieldParamTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/AutoArrayStructRecursionTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/AutoArraySubtypedUInt8Test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/AutoArrayUInt8Test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ChoiceTagClashTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/FixedArrayUInt8Test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/PackedArraysMappingTest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/PackedAutoArrayBitfieldParamTest.cpp @@ -44,4 +63,5 @@ add_custom_test(array_types ${CMAKE_CURRENT_SOURCE_DIR}/cpp/VariableArrayTernaryOperatorTest.cpp GENERATED_SOURCES ${GENERATED_SOURCES} + ${GENERATED_SOURCES_CHOICE_TAG_CLASH} ) diff --git a/test/extensions/language/array_types/ClangTidySuppressions.txt b/test/extensions/language/array_types/ClangTidySuppressions.txt index 3325df81b..614b119f1 100644 --- a/test/extensions/language/array_types/ClangTidySuppressions.txt +++ b/test/extensions/language/array_types/ClangTidySuppressions.txt @@ -53,6 +53,7 @@ cppcoreguidelines-pro-type-member-init:gen/array_types/packing_interface_optimiz cppcoreguidelines-pro-type-member-init:gen/array_types/packing_interface_optimization/PackedColorChoice.cpp cppcoreguidelines-pro-type-member-init:gen/array_types/packing_interface_optimization/UnpackedColorChoice.cpp cppcoreguidelines-pro-type-member-init:gen/array_types/variable_array_ternary_operator/VariableArrayElement.cpp +cppcoreguidelines-pro-type-member-init:gen_choice_tag_clash/choice_tag_clash/TestChoice.cpp google-explicit-constructor:gen/array_types/arrays_mapping/TestBitmask.h google-explicit-constructor:gen/array_types/packed_arrays_mapping/TestBitmask.h @@ -99,9 +100,11 @@ readability-make-member-function-const:gen/array_types/packing_interface_optimiz readability-make-member-function-const:gen/array_types/packing_interface_optimization/UnpackedColorChoice.cpp readability-make-member-function-const:gen/array_types/packing_interface_optimization/UnpackedColorStruct.cpp readability-make-member-function-const:gen/array_types/variable_array_ternary_operator/VariableArrayElement.cpp +readability-make-member-function-const:gen_choice_tag_clash/choice_tag_clash/TestChoice.cpp readability-simplify-boolean-expr:gen/array_types/packed_auto_array_empty_compounds/EmptyChoice.cpp readability-simplify-boolean-expr:gen/array_types/packed_auto_array_empty_compounds/EmptyUnion.cpp readability-simplify-boolean-expr:gen/array_types/packing_interface_optimization/MixedColorChoice.cpp readability-simplify-boolean-expr:gen/array_types/packing_interface_optimization/PackedColorChoice.cpp readability-simplify-boolean-expr:gen/array_types/packing_interface_optimization/UnpackedColorChoice.cpp +readability-simplify-boolean-expr:gen_choice_tag_clash/choice_tag_clash/TestChoice.cpp diff --git a/test/extensions/language/array_types/build.xml b/test/extensions/language/array_types/build.xml index 1bb39c266..ab8138d79 100644 --- a/test/extensions/language/array_types/build.xml +++ b/test/extensions/language/array_types/build.xml @@ -12,6 +12,9 @@ + + + diff --git a/test/extensions/language/array_types/cpp/ChoiceTagClashTest.cpp b/test/extensions/language/array_types/cpp/ChoiceTagClashTest.cpp new file mode 100644 index 000000000..1ac6418db --- /dev/null +++ b/test/extensions/language/array_types/cpp/ChoiceTagClashTest.cpp @@ -0,0 +1,74 @@ +#include "choice_tag_clash/ChoiceTagClash.h" +#include "gtest/gtest.h" +#include "zserio/SerializeUtil.h" + +namespace choice_tag_clash +{ + +using allocator_type = ChoiceTagClash::allocator_type; +template +using vector_type = zserio::vector; +using BitBuffer = zserio::BasicBitBuffer>; + +class ChoiceTagClashTest : public ::testing::Test +{ +protected: + ChoiceTagClash createChoiceTagClash() + { + ChoiceTagClash choiceTagClash; + fillChoices(choiceTagClash.getChoices()); + fillUnions(choiceTagClash.getUnions()); + return choiceTagClash; + } + + static void fillChoices(vector_type& choices) + { + for (size_t i = 0; i < NUM_CHOICES; ++i) + { + choices.emplace_back(); + if (i % 2 == 0) + { + choices.back().setChoiceTag(static_cast(i)); + } + else + { + choices.back().setStringField("text " + zserio::toString(i)); + } + } + } + + static void fillUnions(vector_type& unions) + { + for (size_t i = 0; i < NUM_UNIONS; ++i) + { + unions.emplace_back(); + if (i % 2 == 0) + { + unions.back().setChoiceTag(static_cast(i)); + } + else + { + unions.back().setStringField("text " + zserio::toString(i)); + } + } + } + +private: + static constexpr size_t NUM_CHOICES = 10; + static constexpr size_t NUM_UNIONS = 13; +}; + +constexpr size_t ChoiceTagClashTest::NUM_CHOICES; +constexpr size_t ChoiceTagClashTest::NUM_UNIONS; + +TEST_F(ChoiceTagClashTest, writeRead) +{ + ChoiceTagClash choiceTagClash = createChoiceTagClash(); + + BitBuffer bitBuffer = zserio::serialize(choiceTagClash); + ChoiceTagClash readChoiceTagClash = zserio::deserialize(bitBuffer); + + ASSERT_EQ(choiceTagClash, readChoiceTagClash); +} + +} // namespace choice_tag_clash diff --git a/test/extensions/language/array_types/doc_options.txt b/test/extensions/language/array_types/doc_options.txt new file mode 100644 index 000000000..4816a680b --- /dev/null +++ b/test/extensions/language/array_types/doc_options.txt @@ -0,0 +1 @@ +ZSERIO_ARGS=-withoutCrossExtensionCheck diff --git a/test/extensions/language/array_types/java/array_types/ChoiceTagClashTest.java b/test/extensions/language/array_types/java/array_types/ChoiceTagClashTest.java new file mode 100644 index 000000000..6a183a30b --- /dev/null +++ b/test/extensions/language/array_types/java/array_types/ChoiceTagClashTest.java @@ -0,0 +1,61 @@ +package choice_tag_clash; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +import zserio.runtime.io.BitBuffer; +import zserio.runtime.io.SerializeUtil; + +public class ChoiceTagClashTest +{ + @Test + public void writeRead() + { + final ChoiceTagClash choiceTagClash = new ChoiceTagClash(createChoices(), createUnions()); + + final BitBuffer bitBuffer = SerializeUtil.serialize(choiceTagClash); + final ChoiceTagClash readChoiceTagClash = SerializeUtil.deserialize(ChoiceTagClash.class, bitBuffer); + + assertEquals(choiceTagClash, readChoiceTagClash); + } + + private TestChoice[] createChoices() + { + final TestChoice[] choices = new TestChoice[NUM_CHOICES]; + for (int i = 0; i < NUM_CHOICES; ++i) + { + choices[i] = new TestChoice(i % 2 == 0); + if (i % 2 == 0) + { + choices[i].setChoiceTag(i); + } + else + { + choices[i].setStringField("text " + i); + } + } + return choices; + } + + private TestUnion[] createUnions() + { + final TestUnion[] unions = new TestUnion[NUM_UNIONS]; + for (int i = 0; i < NUM_UNIONS; ++i) + { + unions[i] = new TestUnion(); + if (i % 2 == 0) + { + unions[i].setChoiceTag(i); + } + else + { + unions[i].setStringField("text " + i); + } + } + return unions; + } + + private static final int NUM_CHOICES = 10; + private static final int NUM_UNIONS = 13; +} diff --git a/test/extensions/language/array_types/xml_options.txt b/test/extensions/language/array_types/xml_options.txt new file mode 100644 index 000000000..4816a680b --- /dev/null +++ b/test/extensions/language/array_types/xml_options.txt @@ -0,0 +1 @@ +ZSERIO_ARGS=-withoutCrossExtensionCheck