Skip to content

Commit

Permalink
Merge pull request #1797 from ApexAI/iox-#1394-fix-axivion-violation-…
Browse files Browse the repository at this point in the history
…for-helplets

iox-#1394-fix-axivion-violation-for-helplets
  • Loading branch information
FerdinandSpitzschnueffler committed Dec 13, 2022
2 parents e0165a6 + efd1cc9 commit 6765566
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 61 deletions.
67 changes: 35 additions & 32 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/helplets.hpp
Expand Up @@ -64,18 +64,19 @@ struct BestFittingTypeImpl<true, true, false>
{
using Type_t = uint32_t;
};

constexpr char ASCII_A = 'a';
constexpr char ASCII_Z = 'z';
constexpr char ASCII_CAPITAL_A = 'A';
constexpr char ASCII_CAPITAL_Z = 'Z';
constexpr char ASCII_0 = '0';
constexpr char ASCII_9 = '9';
constexpr char ASCII_MINUS = '-';
constexpr char ASCII_DOT = '.';
constexpr char ASCII_COLON = ':';
constexpr char ASCII_UNDERSCORE = '_';
// AXIVION DISABLE STYLE AutosarC++19_03-A3.9.1: Not used as an integer but as actual character.
constexpr char ASCII_A{'a'};
constexpr char ASCII_Z{'z'};
constexpr char ASCII_CAPITAL_A{'A'};
constexpr char ASCII_CAPITAL_Z{'Z'};
constexpr char ASCII_0{'0'};
constexpr char ASCII_9{'9'};
constexpr char ASCII_MINUS{'-'};
constexpr char ASCII_DOT{'.'};
constexpr char ASCII_COLON{':'};
constexpr char ASCII_UNDERSCORE{'_'};
} // namespace internal
// AXIVION ENABLE STYLE AutosarC++19_03-A3.9.1

template <typename T, typename = typename std::enable_if<std::is_pointer<T>::value, void>::type>
struct not_null
Expand All @@ -90,8 +91,8 @@ struct not_null
Expects(t != nullptr);
}

// this should behave like a pointer which never can be nullptr, adding explicit
// would defeat the purpose
// AXIVION Next Construct AutosarC++19_03-A13.5.2,AutosarC++19_03-A13.5.3:this should behave like a pointer which never can be nullptr,
// adding explicit would defeat the purpose
// NOLINTNEXTLINE(hicpp-explicit-conversions)
constexpr operator T() const noexcept
{
Expand All @@ -106,17 +107,17 @@ template <typename T, T Minimum>
struct greater_or_equal
{
public:
// this class should behave like a T but which never can be less than Minimum.
// Adding explicit would defeat the purpose.
// AXIVION Next Construct AutosarC++19_03-A12.1.4: this class should behave like a T but which never can be less
// than Minimum. Adding explicit would defeat the purpose.
// NOLINTNEXTLINE(hicpp-explicit-conversions)
greater_or_equal(T t) noexcept
: m_value(t)
{
Expects(t >= Minimum);
}

// this class should behave like a T but which never can be less than Minimum.
// Adding explicit would defeat the purpose.
// AXIVION Next Construct AutosarC++19_03-A13.5.2,AutosarC++19_03-A13.5.3:this class should behave like a T but
// which never can be less than Minimum. Adding explicit would defeat the purpose.
// NOLINTNEXTLINE(hicpp-explicit-conversions)
constexpr operator T() const noexcept
{
Expand All @@ -131,17 +132,17 @@ template <typename T, T Minimum, T Maximum>
struct range
{
public:
// this class should behave like a T but with values only in range [Minimum, Maximum]
// Adding explicit would defeat the purpose.
// AXIVION Next Construct AutosarC++19_03-A12.1.4: this class should behave like a T but with values only in
// range [Minimum, Maximum] Adding explicit would defeat the purpose.
// NOLINTNEXTLINE(hicpp-explicit-conversions)
range(T t) noexcept
: m_value(t)
{
Expects(t >= Minimum && t <= Maximum);
Expects((t >= Minimum) && (t <= Maximum));
}

// this class should behave like a T but with values only in range [Minimum, Maximum]
// Adding explicit would defeat the purpose.
// AXIVION Next Construct AutosarC++19_03-A13.5.2, AutosarC++19_03-A13.5.3: this class should behave like a T but
// with values only in range [Minimum, Maximum]. Adding explicit would defeat the purpose.
// NOLINTNEXTLINE(hicpp-explicit-conversions)
constexpr operator T() const noexcept
{
Expand All @@ -152,11 +153,13 @@ struct range
T m_value;
};

/// @note value + alignment - 1 must not exceed the maximum value for type T
/// @note alignment must be a power of two
template <typename T>
// AXIVION Next Construct AutosarC++19_03-A2.10.5, AutosarC++19_03-M17.0.3: The function is in the iox::cxx namespace which prevents easy misuse
T align(const T value, const T alignment) noexcept
{
T remainder = value % alignment;
return value + ((remainder == 0U) ? 0U : alignment - remainder);
return (value + (alignment - 1)) & (-alignment);
}

/// @brief allocates aligned memory which can only be free'd by alignedFree
Expand All @@ -180,7 +183,7 @@ constexpr size_t maxAlignment() noexcept
template <typename T, typename... Args>
constexpr size_t maxAlignment() noexcept
{
return alignof(T) > maxAlignment<Args...>() ? alignof(T) : maxAlignment<Args...>();
return (alignof(T) > maxAlignment<Args...>()) ? alignof(T) : maxAlignment<Args...>();
}

/// template recursion stopper for maximum size calculation
Expand All @@ -194,7 +197,7 @@ constexpr size_t maxSize() noexcept
template <typename T, typename... Args>
constexpr size_t maxSize() noexcept
{
return sizeof(T) > maxSize<Args...>() ? sizeof(T) : maxSize<Args...>();
return (sizeof(T) > maxSize<Args...>()) ? sizeof(T) : maxSize<Args...>();
}

/// @brief Get the capacity of a C array at compile time
Expand All @@ -207,7 +210,8 @@ constexpr size_t maxSize() noexcept
/// @param[in] The actual content of the array is not of interest. Its just the capacity of the array that matters.
/// @return Returns the capacity of the array at compile time.
template <typename T, uint64_t CapacityValue>
// returning capacity of C array at compile time is safe, no possibility of out of bounds access
// AXIVION Next Construct AutosarC++19_03-A18.1.1:returning capacity of C array at compile time is safe, no
// possibility of out of bounds access
// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
static constexpr uint64_t arrayCapacity(T const (&/*notInterested*/)[CapacityValue]) noexcept
{
Expand Down Expand Up @@ -246,7 +250,7 @@ constexpr bool isPowerOfTwo(const T n) noexcept
return n && ((n & (n - 1U)) == 0U);
}

enum class RelativePathComponents
enum class RelativePathComponents : std::uint32_t
{
REJECT,
ACCEPT
Expand All @@ -270,8 +274,7 @@ enum class RelativePathComponents
/// @param[in] relativePathComponents are relative path components are allowed for this path entry
/// @return true if it is valid, otherwise false
template <uint64_t StringCapacity>
bool isValidPathEntry(const string<StringCapacity>& name,
const RelativePathComponents& relativePathComponents) noexcept;
bool isValidPathEntry(const string<StringCapacity>& name, const RelativePathComponents relativePathComponents) noexcept;

/// @brief checks if the given string is a valid filename. It must fulfill the
/// requirements of a valid path entry (see, isValidPathEntry) and is not allowed
Expand Down Expand Up @@ -341,8 +344,8 @@ template <typename F, typename T>
constexpr T from(const F value) noexcept;

/// @brief Converts a value of type F to a corresponding value of type T. This is a convenience function which is
/// automatically available when `from` is implemented. This function shall therefore not be specialized but always the
/// `from` function.
/// automatically available when 'from' is implemented. This function shall therefore not be specialized but always the
/// 'from' function.
/// @code
/// Bar b = iox::cxx::into<Bar>(Foo::ENUM_VALUE);
/// @endcode
Expand Down
60 changes: 31 additions & 29 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/helplets.inl
Expand Up @@ -24,29 +24,30 @@ namespace cxx
{
template <uint64_t StringCapacity>
inline bool isValidPathEntry(const string<StringCapacity>& name,
const RelativePathComponents& relativePathComponents) noexcept
const RelativePathComponents relativePathComponents) noexcept
{
const string<StringCapacity> currentDirectory(".");
const string<StringCapacity> parentDirectory("..");
const string<StringCapacity> currentDirectory{"."};
const string<StringCapacity> parentDirectory{".."};

if (name == currentDirectory || name == parentDirectory)
if ((name == currentDirectory) || (name == parentDirectory))
{
return relativePathComponents == RelativePathComponents::ACCEPT;
}

const auto nameSize = name.size();

for (uint64_t i = 0; i < nameSize; ++i)
for (uint64_t i{0}; i < nameSize; ++i)
{
const char c = name[i];
// AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character
const char c{name[i]};

const bool isSmallLetter = (internal::ASCII_A <= c && c <= internal::ASCII_Z);
const bool isCapitalLetter = (internal::ASCII_CAPITAL_A <= c && c <= internal::ASCII_CAPITAL_Z);
const bool isNumber = (internal::ASCII_0 <= c && c <= internal::ASCII_9);
const bool isSpecialCharacter = (c == internal::ASCII_MINUS || c == internal::ASCII_DOT
|| c == internal::ASCII_COLON || c == internal::ASCII_UNDERSCORE);
const bool isSmallLetter{(internal::ASCII_A <= c) && (c <= internal::ASCII_Z)};
const bool isCapitalLetter{(internal::ASCII_CAPITAL_A <= c) && (c <= internal::ASCII_CAPITAL_Z)};
const bool isNumber{(internal::ASCII_0 <= c) && (c <= internal::ASCII_9)};
const bool isSpecialCharacter{((c == internal::ASCII_MINUS) || (c == internal::ASCII_DOT))
|| ((c == internal::ASCII_COLON) || (c == internal::ASCII_UNDERSCORE))};

if (!isSmallLetter && !isCapitalLetter && !isNumber && !isSpecialCharacter)
if ((!isSmallLetter && !isCapitalLetter) && (!isNumber && !isSpecialCharacter))
{
return false;
}
Expand Down Expand Up @@ -81,15 +82,15 @@ inline bool isValidPathToFile(const string<StringCapacity>& name) noexcept
return false;
}

string<StringCapacity> filePart = name;
string<StringCapacity> filePart{name};
string<StringCapacity> pathPart;

name.find_last_of(cxx::string<platform::IOX_NUMBER_OF_PATH_SEPARATORS>(cxx::TruncateToCapacity,
&platform::IOX_PATH_SEPARATORS[0],
platform::IOX_NUMBER_OF_PATH_SEPARATORS))
.and_then([&](auto position) {
name.substr(position + 1).and_then([&](auto& s) { filePart = s; });
name.substr(0, position).and_then([&](auto& s) { pathPart = s; });
name.substr(position + 1).and_then([&filePart](auto& s) { filePart = s; });
name.substr(0, position).and_then([&pathPart](auto& s) { pathPart = s; });
});

return (pathPart.empty() || isValidPathToDirectory(pathPart)) && isValidFileName(filePart);
Expand All @@ -105,8 +106,8 @@ inline bool isValidPathToDirectory(const string<StringCapacity>& name) noexcept

auto temp = name;

const string<StringCapacity> currentDirectory(".");
const string<StringCapacity> parentDirectory("..");
const string<StringCapacity> currentDirectory{"."};
const string<StringCapacity> parentDirectory{".."};

while (!temp.empty())
{
Expand All @@ -117,9 +118,9 @@ inline bool isValidPathToDirectory(const string<StringCapacity>& name) noexcept
// are equivalent:
// /some/fuu/bar
// //some///fuu////bar
if (separatorPosition && *separatorPosition == 0)
if (separatorPosition && (*separatorPosition == 0))
{
temp.substr(*separatorPosition + 1).and_then([&](auto& s) { temp = s; });
temp.substr(*separatorPosition + 1).and_then([&temp](auto& s) { temp = s; });
continue;
}

Expand All @@ -129,17 +130,18 @@ inline bool isValidPathToDirectory(const string<StringCapacity>& name) noexcept
if (separatorPosition)
{
auto filenameToVerify = temp.substr(0, *separatorPosition);
bool isValidDirectory = isValidFileName(*filenameToVerify) || *filenameToVerify == currentDirectory
|| *filenameToVerify == parentDirectory;
bool isValidDirectory{
(isValidFileName(*filenameToVerify))
|| ((*filenameToVerify == currentDirectory) || (*filenameToVerify == parentDirectory))};
if (!isValidDirectory)
{
return false;
}

temp.substr(*separatorPosition + 1).and_then([&](auto& s) { temp = s; });
temp.substr(*separatorPosition + 1).and_then([&temp](auto& s) { temp = s; });
}
// we reached the last entry, if its a valid file name the path is valid
else if (!separatorPosition)
else
{
return isValidPathEntry(temp, RelativePathComponents::ACCEPT);
}
Expand All @@ -155,8 +157,8 @@ inline bool doesEndWithPathSeparator(const string<StringCapacity>& name) noexcep
{
return false;
}

char lastCharacter = name[name.size() - 1U];
// AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character
char lastCharacter{name[name.size() - 1U]};

for (const auto separator : iox::platform::IOX_PATH_SEPARATORS)
{
Expand All @@ -169,16 +171,16 @@ inline bool doesEndWithPathSeparator(const string<StringCapacity>& name) noexcep
}

template <typename F, typename T>
inline constexpr T from(const F e IOX_MAYBE_UNUSED) noexcept
inline constexpr T from(const F) noexcept
{
static_assert(always_false_v<F> && always_false_v<T>, "Conversion for the specified types is not implemented!\
Please specialize `template <typename F, typename T> constexpr T from(const F) noexcept`!");
Please specialize 'template <typename F, typename T> constexpr T from(const F) noexcept'!");
}

template <typename T, typename F>
inline constexpr T into(const F e) noexcept
inline constexpr T into(const F value) noexcept
{
return from<F, T>(e);
return from<F, T>(value);
}
} // namespace cxx
} // namespace iox
Expand Down

0 comments on commit 6765566

Please sign in to comment.