Skip to content

Commit

Permalink
Merge pull request #2089 from eclipse-iceoryx/iox-2047-fix-gcc-13-war…
Browse files Browse the repository at this point in the history
…nings

iox-#2047 Fix gcc 13 warnings
  • Loading branch information
elBoberido authored Nov 13, 2023
2 parents 7995b3c + 11b454d commit 90a9b03
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 94 deletions.
4 changes: 0 additions & 4 deletions .cirrus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ arch_linux_x64_build_task:
folder: iox-tests-bin
reupload_on_changes: true
fingerprint_key: $CIRRUS_OS_archlinux_x64_test_binaries_cache
env:
# use clang on Arch Linux since GCC triggers some compiler warnings which abort the job
CC: clang
CXX: clang++
build_script:
<<: *IOX_POSIX_CLEAN_BUILD_WITH_ADDITIONAL_USER
populate_test_binary_folder_script:
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/include/iceoryx_hoofs/cxx/variant_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class VariantQueue
uint64_t capacity() const noexcept;

private:
VariantQueueTypes m_type;
const VariantQueueTypes m_type;
fifo_t m_fifo;
};
} // namespace cxx
Expand Down
132 changes: 73 additions & 59 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/variant_queue.inl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2020 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2023 by Mathias Kraus <[email protected]>. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,34 +57,38 @@ optional<ValueType> VariantQueue<ValueType, Capacity>::push(const ValueType& val
{
case VariantQueueTypes::FiFo_SingleProducerSingleConsumer:
{
auto hadSpace =
m_fifo.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>()
->push(value);
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>();
auto hadSpace = queue->push(value);

return (hadSpace) ? nullopt : make_optional<ValueType>(value);
}
case VariantQueueTypes::SoFi_SingleProducerSingleConsumer:
{
ValueType overriddenValue;
auto hadSpace =
m_fifo.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>()
->push(value, overriddenValue);
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>();
auto hadSpace = queue->push(value, overriddenValue);

return (hadSpace) ? nullopt : make_optional<ValueType>(overriddenValue);
}
case VariantQueueTypes::FiFo_MultiProducerSingleConsumer:
{
auto hadSpace =
m_fifo.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>()
->tryPush(value);
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>();
auto hadSpace = queue->tryPush(value);

return (hadSpace) ? nullopt : make_optional<ValueType>(value);
}
case VariantQueueTypes::SoFi_MultiProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>()
->push(value);
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>();
return queue->push(value);
}
}

Expand All @@ -97,25 +102,28 @@ inline optional<ValueType> VariantQueue<ValueType, Capacity>::pop() noexcept
{
case VariantQueueTypes::FiFo_SingleProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>()
->pop();
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>();
return queue->pop();
}
case VariantQueueTypes::SoFi_SingleProducerSingleConsumer:
{
ValueType returnType;
auto hasReturnType =
m_fifo.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>()
->pop(returnType);
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>();
auto hasReturnType = queue->pop(returnType);

return (hasReturnType) ? make_optional<ValueType>(returnType) : nullopt;
}
case VariantQueueTypes::FiFo_MultiProducerSingleConsumer:
case VariantQueueTypes::SoFi_MultiProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>()
->pop();
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>();
return queue->pop();
}
}

Expand All @@ -129,22 +137,25 @@ inline bool VariantQueue<ValueType, Capacity>::empty() const noexcept
{
case VariantQueueTypes::FiFo_SingleProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>()
->empty();
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>();
return queue->empty();
}
case VariantQueueTypes::SoFi_SingleProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>()
->empty();
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>();
return queue->empty();
}
case VariantQueueTypes::FiFo_MultiProducerSingleConsumer:
case VariantQueueTypes::SoFi_MultiProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>()
->empty();
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>();
return queue->empty();
}
}

Expand All @@ -158,25 +169,25 @@ inline uint64_t VariantQueue<ValueType, Capacity>::size() noexcept
{
case VariantQueueTypes::FiFo_SingleProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>()
->size();
break;
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>();
return queue->size();
}
case VariantQueueTypes::SoFi_SingleProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>()
->size();
break;
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>();
return queue->size();
}
case VariantQueueTypes::FiFo_MultiProducerSingleConsumer:
case VariantQueueTypes::SoFi_MultiProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>()
->size();
break;
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>();
return queue->size();
}
}

Expand All @@ -197,17 +208,20 @@ inline bool VariantQueue<ValueType, Capacity>::setCapacity(const uint64_t newCap
}
case VariantQueueTypes::SoFi_SingleProducerSingleConsumer:
{
m_fifo.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>()
->setCapacity(newCapacity);
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>();
queue->setCapacity(newCapacity);
return true;
}
case VariantQueueTypes::FiFo_MultiProducerSingleConsumer:
case VariantQueueTypes::SoFi_MultiProducerSingleConsumer:
{
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>();
// we may discard elements in the queue if the size is reduced and the fifo contains too many elements
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>()
->setCapacity(newCapacity);
return queue->setCapacity(newCapacity);
}
}
return false;
Expand All @@ -220,25 +234,25 @@ inline uint64_t VariantQueue<ValueType, Capacity>::capacity() const noexcept
{
case VariantQueueTypes::FiFo_SingleProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>()
->capacity();
break;
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_SingleProducerSingleConsumer)>();
return queue->capacity();
}
case VariantQueueTypes::SoFi_SingleProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>()
->capacity();
break;
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::SoFi_SingleProducerSingleConsumer)>();
return queue->capacity();
}
case VariantQueueTypes::FiFo_MultiProducerSingleConsumer:
case VariantQueueTypes::SoFi_MultiProducerSingleConsumer:
{
return m_fifo
.template get_at_index<static_cast<uint64_t>(VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>()
->capacity();
break;
// SAFETY: 'm_type' ist 'const' and does not change after construction
auto* queue = m_fifo.template unsafe_get_at_index_unchecked<static_cast<uint64_t>(
VariantQueueTypes::FiFo_MultiProducerSingleConsumer)>();
return queue->capacity();
}
}

Expand Down
7 changes: 7 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_vocabulary_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ TYPED_TEST(stringTyped_test, SelfMoveAssignmentExcluded)
{
::testing::Test::RecordProperty("TEST_ID", "0ad45975-b68b-465a-b8c5-83dd8d8290d5");
this->testSubject = "M";
#if (defined(__GNUC__) && __GNUC__ == 13 && !defined(__clang__))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wself-move"
#endif
this->testSubject = std::move(this->testSubject);
#if (defined(__GNUC__) && __GNUC__ == 13 && !defined(__clang__))
#pragma GCC diagnostic pop
#endif
EXPECT_THAT(this->testSubject.size(), Eq(1U));
EXPECT_THAT(this->testSubject.c_str(), StrEq("M"));
}
Expand Down
35 changes: 27 additions & 8 deletions iceoryx_hoofs/vocabulary/include/iox/detail/string.inl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2023 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2023 by Mathias Kraus <[email protected]>. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -164,17 +165,35 @@ inline string<Capacity>& string<Capacity>::operator=(const char (&rhs)[N]) noexc
return *this;
}

std::memcpy(m_rawstring, rhs, N);
const auto sourceRawStringSize = static_cast<uint64_t>(strnlen(&rhs[0], N));
if (sourceRawStringSize <= Capacity)
{
m_rawstringSize = sourceRawStringSize;
}
else
{
m_rawstringSize = Capacity;

IOX_LOG(
WARN,
"iox::string: Assignment of array which is not zero-terminated! Last value of array overwritten with 0!");
}

// AXIVION DISABLE STYLE AutosarC++19_03-A16.0.1: pre-processor is required for setting gcc diagnostics, since gcc 8 incorrectly warns here about out of bounds array access
// AXIVION DISABLE STYLE AutosarC++19_03-A16.7.1: see rule 'A16.0.1' above
#if (defined(__GNUC__) && (__GNUC__ == 8)) && (__GNUC_MINOR__ >= 3)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#endif
std::memcpy(m_rawstring, rhs, m_rawstringSize);
#if (defined(__GNUC__) && (__GNUC__ == 8)) && (__GNUC_MINOR__ >= 3)
#pragma GCC diagnostic pop
#endif
// AXIVION ENABLE STYLE AutosarC++19_03-A16.7.1
// AXIVION ENABLE STYLE AutosarC++19_03-A16.0.1

m_rawstringSize = std::min(Capacity, static_cast<uint64_t>(strnlen(&rhs[0], N)));
m_rawstring[m_rawstringSize] = '\0';

if (rhs[m_rawstringSize] != '\0')
{
IOX_LOG(WARN,
"iox::string: Assignment of array which is not zero-terminated! Last value of array "
"overwritten with 0!");
}
return *this;
}

Expand Down
20 changes: 20 additions & 0 deletions iceoryx_hoofs/vocabulary/include/iox/detail/variant.inl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2021 by Perforce All rights reserved.
// Copyright (c) 2023 by Mathias Kraus <[email protected]>. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -204,6 +205,14 @@ inline typename internal::get_type_at_index<0, TypeIndex, Types...>::type* varia
return nullptr;
}

return unsafe_get_at_index_unchecked<TypeIndex>();
}

template <typename... Types>
template <uint64_t TypeIndex>
inline typename internal::get_type_at_index<0, TypeIndex, Types...>::type*
variant<Types...>::unsafe_get_at_index_unchecked() noexcept
{
using T = typename internal::get_type_at_index<0, TypeIndex, Types...>::type;

// AXIVION Next Construct AutosarC++19_03-M5.2.8 : conversion to typed pointer is intentional, it is correctly aligned and points to sufficient memory for a T by design
Expand All @@ -221,6 +230,17 @@ variant<Types...>::get_at_index() const noexcept
return const_cast<const T*>(const_cast<variant*>(this)->template get_at_index<TypeIndex>());
}

template <typename... Types>
template <uint64_t TypeIndex>
inline const typename internal::get_type_at_index<0, TypeIndex, Types...>::type*
variant<Types...>::unsafe_get_at_index_unchecked() const noexcept
{
using T = typename internal::get_type_at_index<0, TypeIndex, Types...>::type;
// AXIVION Next Construct AutosarC++19_03-A5.2.3 : avoid code duplication
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
return const_cast<const T*>(const_cast<variant*>(this)->template unsafe_get_at_index_unchecked<TypeIndex>());
}

template <typename... Types>
template <typename T>
inline const T* variant<Types...>::get() const noexcept
Expand Down
20 changes: 20 additions & 0 deletions iceoryx_hoofs/vocabulary/include/iox/variant.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2023 by Mathias Kraus <[email protected]>. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -212,6 +213,15 @@ class variant final
template <uint64_t TypeIndex>
typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() noexcept;

/// @brief returns a pointer to the type stored at index TypeIndex. (not stl compliant)
/// @tparam[in] TypeIndex index of the stored type
/// @return a pointer to the type at index
/// @attention this function is unsafe and does not check if the type at the index is the current active one; only
/// use this function when it is ensured that the type at index is indeed the current active one; it is undefined
/// behavior to call this on an index which is not the active type
template <uint64_t TypeIndex>
typename internal::get_type_at_index<0, TypeIndex, Types...>::type* unsafe_get_at_index_unchecked() noexcept;

/// @brief returns a pointer to the type stored at index TypeIndex. (not stl compliant)
/// @tparam[in] TypeIndex index of the stored type
/// @return if the variant does contain the type at index TypeIndex it returns a valid
Expand All @@ -224,6 +234,16 @@ class variant final
template <uint64_t TypeIndex>
const typename internal::get_type_at_index<0, TypeIndex, Types...>::type* get_at_index() const noexcept;

/// @brief returns a pointer to the type stored at index TypeIndex. (not stl compliant)
/// @tparam[in] TypeIndex index of the stored type
/// @return a const pointer to the type at index
/// @attention this function is unsafe and does not check if the type at the index is the current active one; only
/// use this function when it is ensured that the type at index is indeed the current active one; it is undefined
/// behavior to call this on an index which is not the active type
template <uint64_t TypeIndex>
const typename internal::get_type_at_index<0, TypeIndex, Types...>::type*
unsafe_get_at_index_unchecked() const noexcept;

/// @brief returns a pointer to the type T stored in the variant. (not stl compliant)
/// @tparam[in] T type of the returned pointer
/// @return if the variant does contain the type T it returns a valid pointer otherwise
Expand Down
Loading

0 comments on commit 90a9b03

Please sign in to comment.