Skip to content

Commit

Permalink
clang-tidy fixes for Common++
Browse files Browse the repository at this point in the history
  • Loading branch information
egecetin committed Nov 9, 2024
1 parent eaf6133 commit 7e9c12f
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 128 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ BreakBeforeBraces: Custom
BraceWrapping:
SplitEmptyFunction: false
AfterCaseLabel: true
QualifierAlignment: Left
...
25 changes: 25 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Checks: 'cert-*,
clang-analyzer-*,
concurrency-*,
cppcoreguidelines-*,
misc-*,
modernize-*,
performance-*,
portability-*,
readability-*,
-clang-analyzer-optin.cplusplus.VirtualCall,
-cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-pro-type-const-cast,
-cppcoreguidelines-pro-type-vararg,
-modernize-use-trailing-return-type,
-misc-header-include-cycle,
-misc-include-cleaner,
-misc-no-recursion,
-misc-use-anonymous-namespace,
-readability-function-cognitive-complexity,
-readability-magic-numbers'
2 changes: 1 addition & 1 deletion Common++/header/GeneralUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <string>
#include <stdint.h>
#include <cstdint>
#include <type_traits>

/// @file
Expand Down
58 changes: 27 additions & 31 deletions Common++/header/IpAddress.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <stdint.h>
#include <string.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <algorithm>
#include <ostream>
Expand Down Expand Up @@ -186,7 +186,7 @@ namespace pcpp

uint32_t IPv4Address::toInt() const
{
uint32_t addr;
uint32_t addr = 0;
memcpy(&addr, m_Bytes.data(), m_Bytes.size() * sizeof(uint8_t));
return addr;
}
Expand Down Expand Up @@ -518,7 +518,9 @@ namespace pcpp
bool IPAddress::operator==(const IPAddress& rhs) const
{
if (isIPv4())
{
return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
}

return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
}
Expand Down Expand Up @@ -561,7 +563,7 @@ namespace pcpp
*
* @param address An address representing the network prefix.
*/
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32u)
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32U)
{}

/**
Expand Down Expand Up @@ -657,10 +659,10 @@ namespace pcpp
std::string toString() const;

private:
uint32_t m_NetworkPrefix;
uint32_t m_Mask;
uint32_t m_NetworkPrefix{};
uint32_t m_Mask{};

bool isValidNetmask(const IPv4Address& netmaskAddress);
static bool isValidNetmask(const IPv4Address& netmaskAddress);
void initFromAddressAndPrefixLength(const IPv4Address& address, uint8_t prefixLen);
void initFromAddressAndNetmask(const IPv4Address& address, const IPv4Address& netmaskAddress);
};
Expand All @@ -678,7 +680,7 @@ namespace pcpp
*
* @param address An address representing the network prefix.
*/
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128u)
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128U)
{}

/**
Expand Down Expand Up @@ -733,7 +735,7 @@ namespace pcpp
*/
IPv6Address getNetworkPrefix() const
{
return IPv6Address(m_NetworkPrefix);
return { m_NetworkPrefix };
}

/**
Expand Down Expand Up @@ -774,10 +776,10 @@ namespace pcpp
std::string toString() const;

private:
uint8_t m_NetworkPrefix[16];
uint8_t m_Mask[16];
uint8_t m_NetworkPrefix[16]{};
uint8_t m_Mask[16]{};

bool isValidNetmask(const IPv6Address& netmaskAddress);
static bool isValidNetmask(const IPv6Address& netmaskAddress);
void initFromAddressAndPrefixLength(const IPv6Address& address, uint8_t prefixLen);
void initFromAddressAndNetmask(const IPv6Address& address, const IPv6Address& netmaskAddress);
};
Expand All @@ -795,7 +797,7 @@ namespace pcpp
*
* @param address An address representing the network prefix.
*/
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32u : 128u)
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32U : 128U)
{}

/**
Expand Down Expand Up @@ -892,10 +894,8 @@ namespace pcpp
{
return this->operator=(*other.m_IPv4Network);
}
else
{
return this->operator=(*other.m_IPv6Network);
}

return this->operator=(*other.m_IPv6Network);
}

/**
Expand Down Expand Up @@ -1032,15 +1032,13 @@ namespace pcpp

return m_IPv4Network->includes(address.getIPv4());
}
else
{
if (address.isIPv4())
{
return false;
}

return m_IPv6Network->includes(address.getIPv6());
if (address.isIPv4())
{
return false;
}

return m_IPv6Network->includes(address.getIPv6());
}

/**
Expand All @@ -1058,15 +1056,13 @@ namespace pcpp

return m_IPv4Network->includes(*network.m_IPv4Network);
}
else
{
if (network.isIPv4Network())
{
return false;
}

return m_IPv6Network->includes(*network.m_IPv6Network);
if (network.isIPv4Network())
{
return false;
}

return m_IPv6Network->includes(*network.m_IPv6Network);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Common++/header/IpUtils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <stdint.h>
#include <cstdint>
#ifdef __linux__
# include <netinet/in.h>
# include <arpa/inet.h>
Expand Down Expand Up @@ -95,7 +95,7 @@ namespace pcpp
* @throws std::invalid_argument Sockaddr family is not AF_INET or AF_INET6, sockaddr is nullptr or the result
* str buffer is insufficient.
*/
void sockaddr2string(sockaddr const* sa, char* resultString, size_t resultBufLen);
void sockaddr2string(const sockaddr* sa, char* resultString, size_t resultBufLen);

/**
* Convert a in_addr format address to 32bit representation
Expand Down
22 changes: 12 additions & 10 deletions Common++/header/LRUList.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ namespace pcpp
template <typename T> class LRUList
{
public:
typedef typename std::list<T>::iterator ListIterator;
typedef typename std::unordered_map<T, ListIterator>::iterator MapIterator;
using ListIterator = typename std::list<T>::iterator;
using MapIterator = typename std::unordered_map<T, ListIterator>::iterator;

/**
* A c'tor for this class
* @param[in] maxSize The max size this list can go
*/
explicit LRUList(size_t maxSize)
{
m_MaxSize = maxSize;
}
explicit LRUList(std::size_t maxSize) : m_MaxSize(maxSize)
{}

/**
* Puts an element in the list. This element will be inserted (or advanced if it already exists) to the head of
Expand All @@ -58,7 +56,7 @@ namespace pcpp
// iterator to the element that prevented the insertion
std::pair<MapIterator, bool> pair =
m_CacheItemsMap.insert(std::make_pair(element, m_CacheItemsList.begin()));
if (pair.second == false) // already exists
if (!static_cast<bool>(pair.second)) // already exists
{
m_CacheItemsList.erase(pair.first->second);
pair.first->second = m_CacheItemsList.begin();
Expand All @@ -70,11 +68,13 @@ namespace pcpp
--lruIter;

if (deletedValue != nullptr)
{
#if __cplusplus > 199711L || _MSC_VER >= 1800
*deletedValue = std::move(*lruIter);
#else
*deletedValue = *lruIter;
#endif
}
m_CacheItemsMap.erase(*lruIter);
m_CacheItemsList.erase(lruIter);
return 1;
Expand Down Expand Up @@ -109,7 +109,9 @@ namespace pcpp
{
MapIterator iter = m_CacheItemsMap.find(element);
if (iter == m_CacheItemsMap.end())
{
return;
}

m_CacheItemsList.erase(iter->second);
m_CacheItemsMap.erase(iter);
Expand All @@ -118,23 +120,23 @@ namespace pcpp
/**
* @return The max size of this list as determined in the c'tor
*/
size_t getMaxSize() const
std::size_t getMaxSize() const
{
return m_MaxSize;
}

/**
* @return The number of elements currently in this list
*/
size_t getSize() const
std::size_t getSize() const
{
return m_CacheItemsMap.size();
}

private:
std::list<T> m_CacheItemsList;
std::unordered_map<T, ListIterator> m_CacheItemsMap;
size_t m_MaxSize;
std::size_t m_MaxSize;
};

} // namespace pcpp
17 changes: 9 additions & 8 deletions Common++/header/Logger.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>
#include <cstdint>

#ifndef LOG_MODULE
# define LOG_MODULE UndefinedLogModule
Expand Down Expand Up @@ -159,8 +159,7 @@ namespace pcpp
* @param[in] method The method in PcapPlusPlus code the log message is coming from
* @param[in] line The line in PcapPlusPlus code the log message is coming from
*/
typedef void (*LogPrinter)(LogLevel logLevel, const std::string& logMessage, const std::string& file,
const std::string& method, const int line);
using LogPrinter = void (*)(LogLevel, const std::string&, const std::string&, const std::string&, const int);

/**
* A static method for converting the log level enum to a string.
Expand Down Expand Up @@ -206,7 +205,9 @@ namespace pcpp
void setAllModulesToLogLevel(LogLevel level)
{
for (int i = 1; i < NumOfLogModules; i++)
{
m_LogModulesArray[i] = level;
}
}

/**
Expand Down Expand Up @@ -265,7 +266,7 @@ namespace pcpp
return *this;
}

std::ostringstream* internalCreateLogStream();
static std::ostringstream* internalCreateLogStream();

/**
* An internal method to print log messages. Shouldn't be used externally.
Expand All @@ -286,15 +287,15 @@ namespace pcpp

private:
bool m_LogsEnabled;
Logger::LogLevel m_LogModulesArray[NumOfLogModules];
Logger::LogLevel m_LogModulesArray[NumOfLogModules]{};
LogPrinter m_LogPrinter;
std::string m_LastError;
std::ostringstream* m_LogStream;
std::ostringstream* m_LogStream{};

// private c'tor - this class is a singleton
Logger();

static void defaultLogPrinter(LogLevel logLevel, const std::string& logMessage, const std::string& file,
const std::string& method, const int line);
const std::string& method, int line);
};
} // namespace pcpp
4 changes: 2 additions & 2 deletions Common++/header/MacAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <initializer_list>
#include <iterator>
#include <ostream>
#include <stdint.h>
#include <string.h>
#include <cstdint>
#include <cstring>
#include <string>

/// @file
Expand Down
2 changes: 1 addition & 1 deletion Common++/header/OUILookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace pcpp
* MAC addresses with only first three octets. The first element is unsigned integer equivalent of "XX:XX:XX"
* formatted MAC address
*/
typedef std::unordered_map<uint64_t, VendorData> OUIVendorMap;
using OUIVendorMap = std::unordered_map<uint64_t, VendorData>;

/// Internal vendor list for MAC addresses
OUIVendorMap vendorMap;
Expand Down
Loading

0 comments on commit 7e9c12f

Please sign in to comment.