diff --git a/cmake/compiler_utils.cmake b/cmake/compiler_utils.cmake index 263584e..17df7d3 100644 --- a/cmake/compiler_utils.cmake +++ b/cmake/compiler_utils.cmake @@ -99,8 +99,11 @@ function(compiler_get_warnings_as_errors_setup VARNAME) elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") if (ZSERIO_ENABLE_WERROR) set(WARNINGS_SETUP "-Werror") - set(WARNINGS_SETUP # used by zserio @deprecated feature (DeprecatedAttribute.h) - "${WARNINGS_SETUP} -Wno-deprecated-declarations") + set(WARNINGS_SETUP_LIST + "-Wno-deprecated-declarations" # used by zserio @deprecated feature (DeprecatedAttribute.h) + "-Wno-unreachable-code" # used by expressions tests + ) + string(REPLACE ";" " " WARNINGS_SETUP "${WARNINGS_SETUP} ${WARNINGS_SETUP_LIST}") endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "8.0.0") set(WARNINGS_SETUP_LIST diff --git a/extension/src/zserio/extension/cpp17/CppExpressionFormattingPolicy.java b/extension/src/zserio/extension/cpp17/CppExpressionFormattingPolicy.java index f203877..0cdd447 100644 --- a/extension/src/zserio/extension/cpp17/CppExpressionFormattingPolicy.java +++ b/extension/src/zserio/extension/cpp17/CppExpressionFormattingPolicy.java @@ -198,7 +198,7 @@ else if (expr.op1().getExprZserioType() instanceof BitmaskType) final BitmaskType bitmaskType = (BitmaskType)expr.op1().getExprZserioType(); final CppNativeType bitmaskNativeType = cppNativeMapper.getCppType(bitmaskType); return new UnaryExpressionFormatting( - "static_cast<" + bitmaskNativeType.getFullName() + "::ZserioType::ValueType>(", ")"); + "::zserio::bitmaskToValue<" + bitmaskNativeType.getFullName() + ">(", ")"); } else { diff --git a/runtime/src/zserio/Bitmasks.h b/runtime/src/zserio/Bitmasks.h index fa9b491..b891ab2 100644 --- a/runtime/src/zserio/Bitmasks.h +++ b/runtime/src/zserio/Bitmasks.h @@ -11,6 +11,19 @@ namespace zserio { +template +constexpr std::enable_if_t, typename T::ZserioType> bitmaskToValue(T value) +{ + return value.getValue(); +} + +template +constexpr std::enable_if_t && std::is_enum_v, typename T::ZserioType> +bitmaskToValue(typename T::Values rawValue) +{ + return static_cast(static_cast(rawValue)); +} + namespace detail { diff --git a/runtime/src/zserio/Enums.h b/runtime/src/zserio/Enums.h index 58e85f2..4bf55f1 100644 --- a/runtime/src/zserio/Enums.h +++ b/runtime/src/zserio/Enums.h @@ -16,7 +16,7 @@ namespace zserio /** * Enum traits provides various information for Zserio enums. * - * This information is provided via specializations of the EnumTraits strucure. + * This information is provided via specializations of the EnumTraits structure. */ template struct EnumTraits diff --git a/test/language/expressions/CMakeLists.txt b/test/language/expressions/CMakeLists.txt new file mode 100644 index 0000000..e0e172c --- /dev/null +++ b/test/language/expressions/CMakeLists.txt @@ -0,0 +1,24 @@ +add_library(expressions_zs STATIC ${TEST_ZS_ROOT}/expressions.zs) +zserio_generate_cpp( + TARGET expressions_zs + SRC_DIR ${TEST_ZS_ROOT} + GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen + EXTRA_ARGS ${ZSERIO_EXTRA_ARGS} + GENERATED_SOURCES_VAR GENERATED_SOURCES + OUTPUT_VAR ZSERIO_LOG + ERROR_VAR ZSERIO_LOG +) +target_link_libraries(expressions_zs PUBLIC ZserioCpp17Runtime) +if (ZSERIO_LOG) + file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/zserio_log.txt "${ZSERIO_LOG}") + check_zserio_warnings("${ZSERIO_LOG}" 0) +endif () + +add_custom_test(expressions + DEPENDS + expressions_zs + SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/cpp/BitmaskTypeTest.cpp + GENERATED_SOURCES + ${GENERATED_SOURCES} +) diff --git a/test/language/expressions/ClangTidySuppressions.txt b/test/language/expressions/ClangTidySuppressions.txt new file mode 100644 index 0000000..052f963 --- /dev/null +++ b/test/language/expressions/ClangTidySuppressions.txt @@ -0,0 +1,21 @@ +bugprone-exception-escape:gen/expressions/isset_operator/IsSetOperator.cpp +bugprone-exception-escape:gen/expressions/isset_operator/Parameterized.h +bugprone-exception-escape:gen/expressions/isset_operator/Parameterized.cpp + +google-explicit-constructor:gen/expressions/bitmask_type/Colors.h +google-explicit-constructor:gen/expressions/isset_operator/TestBitmask.h + +performance-move-const-arg:gen/expressions/field_type/FieldTypeExpression.cpp +performance-move-const-arg:gen/expressions/field_type_with_clash/FieldTypeExpression.cpp + +readability-simplify-boolean-expr:gen/expressions/array_type/ArrayTypeExpression.cpp +readability-simplify-boolean-expr:gen/expressions/index_operator/Element.cpp +readability-simplify-boolean-expr:gen/expressions/parameterized_array_type/ParameterizedArrayElement.cpp +readability-simplify-boolean-expr:gen/expressions/question_mark/QuestionMarkExpression.cpp +readability-simplify-boolean-expr:gen/expressions/uint64_type/UInt64TypeExpression.cpp + +readability-uppercase-literal-suffix:gen/expressions/float_type/FloatTypeExpression.cpp + +readability-implicit-bool-conversion:gen/expressions/index_operator/Element.cpp +readability-implicit-bool-conversion:gen/expressions/parameterized_array_type/ParameterizedArrayElement.cpp +readability-implicit-bool-conversion:gen/expressions/parameterized_array_type/ParameterizedArrayHolder.cpp diff --git a/test/language/expressions/cpp/BitmaskTypeTest.cpp b/test/language/expressions/cpp/BitmaskTypeTest.cpp new file mode 100644 index 0000000..2042bb6 --- /dev/null +++ b/test/language/expressions/cpp/BitmaskTypeTest.cpp @@ -0,0 +1,75 @@ +#include "expressions/bitmask_type/BitmaskTypeExpression.h" +#include "gtest/gtest.h" + +namespace expressions +{ +namespace bitmask_type +{ + +TEST(BitmaskTypeTest, bitSizeOfNoColor) +{ + BitmaskTypeExpression data; + data.colors = Colors(); + data.hasNotColorRed = true; + + ASSERT_EQ(9, zserio::detail::bitSizeOf(zserio::View(data))); +} + +TEST(BitmaskTypeTest, bitSizeOfRed) +{ + BitmaskTypeExpression data; + data.colors = Colors::Values::RED; + data.hasColorRed = true; + + ASSERT_EQ(9, zserio::detail::bitSizeOf(zserio::View(data))); +} + +TEST(BitmaskTypeTest, bitSizeOfGreen) +{ + BitmaskTypeExpression data; + data.colors = Colors::Values::GREEN; + data.hasColorGreen = true; + data.hasNotColorRed = true; + data.hasOtherColorThanRed = true; + + ASSERT_EQ(11, zserio::detail::bitSizeOf(zserio::View(data))); +} + +TEST(BitmaskTypeTest, bitSizeOfBlue) +{ + BitmaskTypeExpression data; + data.colors = Colors::Values::BLUE; + data.hasColorBlue = true; + data.hasNotColorRed = true; + data.hasOtherColorThanRed = true; + + ASSERT_EQ(11, zserio::detail::bitSizeOf(zserio::View(data))); +} + +TEST(BitmaskTypeTest, bitSizeOfBlueGreen) +{ + BitmaskTypeExpression data; + data.colors = Colors::Values::BLUE | Colors::Values::GREEN; + data.hasColorGreen = true; + data.hasColorBlue = true; + data.hasNotColorRed = true; + data.hasOtherColorThanRed = true; + + ASSERT_EQ(12, zserio::detail::bitSizeOf(zserio::View(data))); +} + +TEST(BitmaskTypeTest, bitSizeOfAllColors) +{ + BitmaskTypeExpression data; + data.colors = Colors::Values::RED | Colors::Values::GREEN | Colors::Values::BLUE; + data.hasColorRed = true; + data.hasColorGreen = true; + data.hasColorBlue = true; + data.hasAllColors = true; + data.hasOtherColorThanRed = true; + + ASSERT_EQ(13, zserio::detail::bitSizeOf(zserio::View(data))); +} + +} // namespace bitmask_type +} // namespace expressions