Skip to content

Commit d60c699

Browse files
Clang-tidy fixes for Common++ (seladb#1639)
* apply fixes for Common++ * minor fixes * fix last issues * define a thread safe strerror * include array * format * not use scripts until all repo updated * revert unnecessary change * mark executable * add newline * rename getErrorString * deprecate multiplatformsleep * format * fix some weird struct braces * replace all multiplatformsleep calls * include thread for dpdk tests * include thread lib * format * fix braces for clang-format 19+ * fix new issues * fix comment * fix issues * fix for comment * oops one more * format --------- Co-authored-by: Liu, An-Chi <[email protected]>
1 parent bc5c08d commit d60c699

35 files changed

+423
-241
lines changed

.clang-tidy-new

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Checks: 'cert-*,
2+
clang-analyzer-*,
3+
concurrency-*,
4+
cppcoreguidelines-*,
5+
misc-*,
6+
modernize-*,
7+
performance-*,
8+
portability-*,
9+
readability-*,
10+
-cert-env33-c,
11+
-cert-err58-cpp,
12+
-clang-analyzer-optin.cplusplus.VirtualCall,
13+
-cppcoreguidelines-avoid-c-arrays,
14+
-cppcoreguidelines-avoid-do-while,
15+
-cppcoreguidelines-avoid-magic-numbers,
16+
-cppcoreguidelines-avoid-non-const-global-variables,
17+
-cppcoreguidelines-macro-usage,
18+
-cppcoreguidelines-owning-memory,
19+
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
20+
-cppcoreguidelines-pro-bounds-constant-array-index,
21+
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
22+
-cppcoreguidelines-pro-type-reinterpret-cast,
23+
-cppcoreguidelines-pro-type-const-cast,
24+
-cppcoreguidelines-pro-type-vararg,
25+
-cppcoreguidelines-special-member-functions,
26+
-modernize-avoid-c-arrays,
27+
-modernize-use-trailing-return-type,
28+
-misc-header-include-cycle,
29+
-misc-include-cleaner,
30+
-misc-no-recursion,
31+
-misc-non-private-member-variables-in-classes,
32+
-misc-use-anonymous-namespace,
33+
-readability-function-cognitive-complexity,
34+
-readability-magic-numbers'

Common++/header/GeneralUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <string>
4-
#include <stdint.h>
4+
#include <cstdint>
55
#include <type_traits>
66

77
/// @file

Common++/header/IpAddress.h

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include <stdint.h>
4-
#include <string.h>
3+
#include <cstdint>
4+
#include <cstring>
55
#include <string>
66
#include <algorithm>
77
#include <ostream>
@@ -145,7 +145,7 @@ namespace pcpp
145145

146146
uint32_t IPv4Address::toInt() const
147147
{
148-
uint32_t addr;
148+
uint32_t addr = 0;
149149
memcpy(&addr, m_Bytes.data(), m_Bytes.size() * sizeof(uint8_t));
150150
return addr;
151151
}
@@ -276,7 +276,7 @@ namespace pcpp
276276
{
277277
public:
278278
/// An enum representing the address type: IPv4 or IPv6
279-
enum AddressType
279+
enum AddressType : uint8_t
280280
{
281281
/// IPv4 address type
282282
IPv4AddressType,
@@ -395,7 +395,9 @@ namespace pcpp
395395
bool IPAddress::operator==(const IPAddress& rhs) const
396396
{
397397
if (isIPv4())
398+
{
398399
return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
400+
}
399401

400402
return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
401403
}
@@ -433,7 +435,7 @@ namespace pcpp
433435
/// A constructor that creates an instance of the class out of an address and a full prefix length,
434436
/// essentially making a network of consisting of only 1 address.
435437
/// @param address An address representing the network prefix.
436-
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32u)
438+
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32U)
437439
{}
438440

439441
/// A constructor that creates an instance of the class out of an address representing the network prefix
@@ -476,7 +478,7 @@ namespace pcpp
476478
/// @return The network prefix, for example: the network prefix of 10.10.10.10/16 is 10.10.0.0
477479
IPv4Address getNetworkPrefix() const
478480
{
479-
return IPv4Address(m_NetworkPrefix);
481+
return m_NetworkPrefix;
480482
}
481483

482484
/// @return The lowest non-reserved IPv4 address in this network, for example: the lowest address
@@ -505,10 +507,10 @@ namespace pcpp
505507
std::string toString() const;
506508

507509
private:
508-
uint32_t m_NetworkPrefix;
509-
uint32_t m_Mask;
510+
uint32_t m_NetworkPrefix{};
511+
uint32_t m_Mask{};
510512

511-
bool isValidNetmask(const IPv4Address& netmaskAddress);
513+
static bool isValidNetmask(const IPv4Address& netmaskAddress);
512514
void initFromAddressAndPrefixLength(const IPv4Address& address, uint8_t prefixLen);
513515
void initFromAddressAndNetmask(const IPv4Address& address, const IPv4Address& netmaskAddress);
514516
};
@@ -521,7 +523,7 @@ namespace pcpp
521523
/// A constructor that creates an instance of the class out of an address and a full prefix length,
522524
/// essentially making a network of consisting of only 1 address.
523525
/// @param address An address representing the network prefix.
524-
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128u)
526+
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128U)
525527
{}
526528

527529
/// A constructor that creates an instance of the class out of an address representing the network prefix
@@ -564,7 +566,7 @@ namespace pcpp
564566
/// @return The network prefix, for example: the network prefix of 3546:f321::/16 is 3546::
565567
IPv6Address getNetworkPrefix() const
566568
{
567-
return IPv6Address(m_NetworkPrefix);
569+
return { m_NetworkPrefix };
568570
}
569571

570572
/// @return The lowest non-reserved IPv6 address in this network, for example: the lowest address in 3546::/16
@@ -593,10 +595,10 @@ namespace pcpp
593595
std::string toString() const;
594596

595597
private:
596-
uint8_t m_NetworkPrefix[16];
597-
uint8_t m_Mask[16];
598+
uint8_t m_NetworkPrefix[16]{};
599+
uint8_t m_Mask[16]{};
598600

599-
bool isValidNetmask(const IPv6Address& netmaskAddress);
601+
static bool isValidNetmask(const IPv6Address& netmaskAddress);
600602
void initFromAddressAndPrefixLength(const IPv6Address& address, uint8_t prefixLen);
601603
void initFromAddressAndNetmask(const IPv6Address& address, const IPv6Address& netmaskAddress);
602604
};
@@ -609,7 +611,7 @@ namespace pcpp
609611
/// A constructor that creates an instance of the class out of an IP address and a full prefix length,
610612
/// essentially making a network of consisting of only 1 address.
611613
/// @param address An address representing the network prefix.
612-
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32u : 128u)
614+
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32U : 128U)
613615
{}
614616

615617
/// A constructor that creates an instance of the class out of an address representing the network prefix
@@ -692,14 +694,14 @@ namespace pcpp
692694
/// @return A reference to the assignee
693695
IPNetwork& operator=(const IPNetwork& other)
694696
{
697+
// NOLINTBEGIN(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
695698
if (other.isIPv4Network())
696699
{
697700
return this->operator=(*other.m_IPv4Network);
698701
}
699-
else
700-
{
701-
return this->operator=(*other.m_IPv6Network);
702-
}
702+
703+
return this->operator=(*other.m_IPv6Network);
704+
// NOLINTEND(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
703705
}
704706

705707
/// Overload of an assignment operator.
@@ -796,15 +798,13 @@ namespace pcpp
796798

797799
return m_IPv4Network->includes(address.getIPv4());
798800
}
799-
else
800-
{
801-
if (address.isIPv4())
802-
{
803-
return false;
804-
}
805801

806-
return m_IPv6Network->includes(address.getIPv6());
802+
if (address.isIPv4())
803+
{
804+
return false;
807805
}
806+
807+
return m_IPv6Network->includes(address.getIPv6());
808808
}
809809

810810
/// @param network An IP network
@@ -820,15 +820,13 @@ namespace pcpp
820820

821821
return m_IPv4Network->includes(*network.m_IPv4Network);
822822
}
823-
else
824-
{
825-
if (network.isIPv4Network())
826-
{
827-
return false;
828-
}
829823

830-
return m_IPv6Network->includes(*network.m_IPv6Network);
824+
if (network.isIPv4Network())
825+
{
826+
return false;
831827
}
828+
829+
return m_IPv6Network->includes(*network.m_IPv6Network);
832830
}
833831

834832
/// @return A string representation of the network in a format of NETWORK_PREFIX/PREFIX_LEN, for example:
@@ -843,40 +841,40 @@ namespace pcpp
843841
std::unique_ptr<IPv6Network> m_IPv6Network;
844842
};
845843

846-
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Address& ipv4Address)
844+
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Address& ipv4Address)
847845
{
848-
os << ipv4Address.toString();
849-
return os;
846+
oss << ipv4Address.toString();
847+
return oss;
850848
}
851849

852-
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Address& ipv6Address)
850+
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv6Address& ipv6Address)
853851
{
854-
os << ipv6Address.toString();
855-
return os;
852+
oss << ipv6Address.toString();
853+
return oss;
856854
}
857855

858-
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPAddress& ipAddress)
856+
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPAddress& ipAddress)
859857
{
860-
os << ipAddress.toString();
861-
return os;
858+
oss << ipAddress.toString();
859+
return oss;
862860
}
863861

864-
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Network& network)
862+
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Network& network)
865863
{
866-
os << network.toString();
867-
return os;
864+
oss << network.toString();
865+
return oss;
868866
}
869867

870-
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Network& network)
868+
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv6Network& network)
871869
{
872-
os << network.toString();
873-
return os;
870+
oss << network.toString();
871+
return oss;
874872
}
875873

876-
inline std::ostream& operator<<(std::ostream& os, const pcpp::IPNetwork& network)
874+
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPNetwork& network)
877875
{
878-
os << network.toString();
879-
return os;
876+
oss << network.toString();
877+
return oss;
880878
}
881879

882880
} // namespace pcpp

Common++/header/IpUtils.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <stdint.h>
3+
#include <cstdint>
44
#ifdef __linux__
55
# include <netinet/in.h>
66
# include <arpa/inet.h>
@@ -53,34 +53,34 @@ namespace pcpp
5353
namespace internal
5454
{
5555
/// Extract IPv4 address from sockaddr
56-
/// @param[in] sa - input sockaddr
56+
/// @param[in] sAddr - input sockaddr
5757
/// @return Address in in_addr format
5858
/// @throws std::invalid_argument Sockaddr family is not AF_INET or sockaddr is nullptr.
59-
in_addr* sockaddr2in_addr(sockaddr* sa);
59+
in_addr* sockaddr2in_addr(sockaddr* sAddr);
6060

6161
/// Attempt to extract IPv4 address from sockaddr
62-
/// @param[in] sa - input sockaddr
62+
/// @param[in] sAddr - input sockaddr
6363
/// @return Pointer to address in in_addr format or nullptr if extraction fails.
64-
in_addr* try_sockaddr2in_addr(sockaddr* sa);
64+
in_addr* try_sockaddr2in_addr(sockaddr* sAddr);
6565

6666
/// Extract IPv6 address from sockaddr
67-
/// @param[in] sa - input sockaddr
67+
/// @param[in] sAddr - input sockaddr
6868
/// @return Address in in6_addr format
6969
/// @throws std::invalid_argument Sockaddr family is not AF_INET6 or sockaddr is nullptr.
70-
in6_addr* sockaddr2in6_addr(sockaddr* sa);
70+
in6_addr* sockaddr2in6_addr(sockaddr* sAddr);
7171

7272
/// Attempt to extract IPv6 address from sockaddr
73-
/// @param[in] sa - input sockaddr
73+
/// @param[in] sAddr - input sockaddr
7474
/// @return Pointer to address in in6_addr format or nullptr if extraction fails.
75-
in6_addr* try_sockaddr2in6_addr(sockaddr* sa);
75+
in6_addr* try_sockaddr2in6_addr(sockaddr* sAddr);
7676

7777
/// Converts a sockaddr format address to its string representation
78-
/// @param[in] sa Address in sockaddr format
78+
/// @param[in] sAddr Address in sockaddr format
7979
/// @param[out] resultString String representation of the address
8080
/// @param[in] resultBufLen Length of the result buffer.
8181
/// @throws std::invalid_argument Sockaddr family is not AF_INET or AF_INET6, sockaddr is nullptr or the result
8282
/// str buffer is insufficient.
83-
void sockaddr2string(sockaddr const* sa, char* resultString, size_t resultBufLen);
83+
void sockaddr2string(const sockaddr* sAddr, char* resultString, size_t resultBufLen);
8484

8585
/// Convert a in_addr format address to 32bit representation
8686
/// @param[in] inAddr Address in in_addr format

Common++/header/LRUList.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ namespace pcpp
2323
template <typename T> class LRUList
2424
{
2525
public:
26-
typedef typename std::list<T>::iterator ListIterator;
27-
typedef typename std::unordered_map<T, ListIterator>::iterator MapIterator;
26+
using ListIterator = typename std::list<T>::iterator;
27+
using MapIterator = typename std::unordered_map<T, ListIterator>::iterator;
2828

2929
/// A c'tor for this class
3030
/// @param[in] maxSize The max size this list can go
31-
explicit LRUList(std::size_t maxSize)
32-
{
33-
m_MaxSize = maxSize;
34-
}
31+
explicit LRUList(std::size_t maxSize) : m_MaxSize(maxSize)
32+
{}
3533

3634
/// Puts an element in the list. This element will be inserted (or advanced if it already exists) to the head of
3735
/// the list as the most recently used element. If the list already reached its max size and the element is new
@@ -51,7 +49,7 @@ namespace pcpp
5149
// iterator to the element that prevented the insertion
5250
std::pair<MapIterator, bool> pair =
5351
m_CacheItemsMap.insert(std::make_pair(element, m_CacheItemsList.begin()));
54-
if (pair.second == false) // already exists
52+
if (!pair.second) // already exists
5553
{
5654
m_CacheItemsList.erase(pair.first->second);
5755
pair.first->second = m_CacheItemsList.begin();
@@ -63,11 +61,13 @@ namespace pcpp
6361
--lruIter;
6462

6563
if (deletedValue != nullptr)
64+
{
6665
#if __cplusplus > 199711L || _MSC_VER >= 1800
6766
*deletedValue = std::move(*lruIter);
6867
#else
6968
*deletedValue = *lruIter;
7069
#endif
70+
}
7171
m_CacheItemsMap.erase(*lruIter);
7272
m_CacheItemsList.erase(lruIter);
7373
return 1;
@@ -96,7 +96,9 @@ namespace pcpp
9696
{
9797
MapIterator iter = m_CacheItemsMap.find(element);
9898
if (iter == m_CacheItemsMap.end())
99+
{
99100
return;
101+
}
100102

101103
m_CacheItemsList.erase(iter->second);
102104
m_CacheItemsMap.erase(iter);

0 commit comments

Comments
 (0)