Skip to content

Commit

Permalink
Clang format for Common++ (#1451)
Browse files Browse the repository at this point in the history
* add clang-format in CI

* apply clang-format in Common++

* ignore format commit

* concate install

* Revert "ignore format commit"

This reverts commit 8531104.

* add indent for macro

* update

* rerun the clang-format

* tune

* Cpp11BracedListStyle: false

* fix ;

* change to use `AlignWithSpaces`

* BreakBeforeBraces: Custom
  • Loading branch information
tigercosmos authored Jun 21, 2024
1 parent 7ea5205 commit e3128c6
Show file tree
Hide file tree
Showing 28 changed files with 1,228 additions and 1,041 deletions.
Empty file added . git-blame-ignore-revs
Empty file.
23 changes: 20 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
---
BasedOnStyle: Microsoft
UseTab: Always
AllowShortFunctionsOnASingleLine: All
UseTab: AlignWithSpaces
AllowShortFunctionsOnASingleLine: None
NamespaceIndentation: All

SortIncludes: false
IndentPPDirectives: AfterHash
IndentWidth: 4
AllowShortBlocksOnASingleLine: Never
PointerAlignment: Left
AccessModifierOffset: -4
AlignTrailingComments: true
AllowShortEnumsOnASingleLine: false
BreakStringLiterals: true
AlignArrayOfStructures: Left
BreakArrays: true
SpacesBeforeTrailingComments: 2
IndentAccessModifiers: false
Cpp11BracedListStyle: false
BreakBeforeBraces: Custom
BraceWrapping:
SplitEmptyFunction: false
AfterCaseLabel: true
...
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install dependencies
run: |
apk update && apk add cppcheck python3-dev
python3 -m pip install cmake-format
python3 -m pip install cmake-format clang-format==18.1.6
- uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1

Expand Down
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ repos:
- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.5
hooks:
- id: clang-format
args: ["--style=file"] # Use the .clang-format file for configuration
files: ^Common\+\+/.*\.(cpp|h)$
- id: cppcheck
args: ["--std=c++11", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr", "--force"]
# - id: clang-format
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
Expand Down
52 changes: 27 additions & 25 deletions Common++/header/DeprecationUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@
/// @file

#ifndef PCPP_DEPRECATED
#if defined(__GNUC__) || defined(__clang__)
#define PCPP_DEPRECATED(msg) __attribute__((deprecated(msg)))
#elif defined(_MSC_VER)
#define PCPP_DEPRECATED(msg) __declspec(deprecated(msg))
#else
#pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
#define PCPP_DEPRECATED(msg)
#endif
# if defined(__GNUC__) || defined(__clang__)
# define PCPP_DEPRECATED(msg) __attribute__((deprecated(msg)))
# elif defined(_MSC_VER)
# define PCPP_DEPRECATED(msg) __declspec(deprecated(msg))
# else
# pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
# define PCPP_DEPRECATED(msg)
# endif
#endif

#if !defined(DISABLE_WARNING_PUSH) || !defined(DISABLE_WARNING_POP)
#if defined(_MSC_VER)
#define DISABLE_WARNING_PUSH __pragma(warning( push ))
#define DISABLE_WARNING_POP __pragma(warning( pop ))
#define DISABLE_WARNING(warningNumber) __pragma(warning( disable : warningNumber ))
# if defined(_MSC_VER)
# define DISABLE_WARNING_PUSH __pragma(warning(push))
# define DISABLE_WARNING_POP __pragma(warning(pop))
# define DISABLE_WARNING(warningNumber) __pragma(warning(disable : warningNumber))

#define DISABLE_WARNING_DEPRECATED DISABLE_WARNING(4996)
#elif defined(__GNUC__) || defined(__clang__)
#define DO_PRAGMA(X) _Pragma(#X)
#define DISABLE_WARNING_PUSH DO_PRAGMA(GCC diagnostic push)
#define DISABLE_WARNING_POP DO_PRAGMA(GCC diagnostic pop)
#define DISABLE_WARNING(warningName) DO_PRAGMA(GCC diagnostic ignored #warningName)
# define DISABLE_WARNING_DEPRECATED DISABLE_WARNING(4996)
# elif defined(__GNUC__) || defined(__clang__)
# define DO_PRAGMA(X) _Pragma(#X)
# define DISABLE_WARNING_PUSH DO_PRAGMA(GCC diagnostic push)
# define DISABLE_WARNING_POP DO_PRAGMA(GCC diagnostic pop)
# define DISABLE_WARNING(warningName) DO_PRAGMA(GCC diagnostic ignored #warningName)

#define DISABLE_WARNING_DEPRECATED DISABLE_WARNING(-Wdeprecated-declarations)
#else
#pragma message("WARNING: Disabling of warnings is not implemented for this compiler")
#define DISABLE_WARNING_PUSH
#define DISABLE_WARNING_POP
// clang-format off
# define DISABLE_WARNING_DEPRECATED DISABLE_WARNING(-Wdeprecated-declarations)
// clang-format on
# else
# pragma message("WARNING: Disabling of warnings is not implemented for this compiler")
# define DISABLE_WARNING_PUSH
# define DISABLE_WARNING_POP

#define DISABLE_WARNING_DEPRECATED
#endif
# define DISABLE_WARNING_DEPRECATED
# endif
#endif
31 changes: 15 additions & 16 deletions Common++/header/GeneralUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace pcpp
* "aa2b10" will be returned
* @param[in] byteArr A byte array
* @param[in] byteArrSize The size of the byte array [in bytes]
* @param[in] stringSizeLimit An optional parameter that enables to limit the returned string size. If set to a positive
* integer value the returned string size will be equal or less than this value. If the string representation of the
* whole array is longer than this size then only part of the array will be read. The default value is -1 which means no
* string size limitation
* @param[in] stringSizeLimit An optional parameter that enables to limit the returned string size. If set to a
* positive integer value the returned string size will be equal or less than this value. If the string
* representation of the whole array is longer than this size then only part of the array will be read. The default
* value is -1 which means no string size limitation
* @return A string of hex characters representing the byte array
*/
std::string byteArrayToHexString(const uint8_t* byteArr, size_t byteArrSize, int stringSizeLimit = -1);
Expand All @@ -31,18 +31,18 @@ namespace pcpp
* @param[in] hexString A string of hex characters
* @param[out] resultByteArr A pre-allocated byte array where the result will be written to
* @param[in] resultByteArrSize The size of the pre-allocated byte array
* @return The size of the result array. If the string represents an array that is longer than the pre-allocated size
* (resultByteArrSize) then the result array will contain only the part of the string that managed to fit into the
* array, and the returned size will be resultByteArrSize. However if the string represents an array that is shorter
* than the pre-allocated size then some of the cells will remain empty and contain zeros, and the returned size will
* be the part of the array that contain data. If the input is an illegal hex string 0 will be returned.
* @return The size of the result array. If the string represents an array that is longer than the pre-allocated
* size (resultByteArrSize) then the result array will contain only the part of the string that managed to fit into
* the array, and the returned size will be resultByteArrSize. However if the string represents an array that is
* shorter than the pre-allocated size then some of the cells will remain empty and contain zeros, and the returned
* size will be the part of the array that contain data. If the input is an illegal hex string 0 will be returned.
* Illegal hex string means odd number of characters or a string that contains non-hex characters
*/
size_t hexStringToByteArray(const std::string& hexString, uint8_t* resultByteArr, size_t resultByteArrSize);

/**
* This is a cross platform version of memmem (https://man7.org/linux/man-pages/man3/memmem.3.html) which is not supported
* on all platforms.
* This is a cross platform version of memmem (https://man7.org/linux/man-pages/man3/memmem.3.html) which is not
* supported on all platforms.
* @param[in] haystack A pointer to the buffer to be searched
* @param[in] haystackLen Length of the haystack buffer
* @param[in] needle A pointer to a buffer that will be searched for
Expand All @@ -55,9 +55,8 @@ namespace pcpp
* Calculates alignment.
* @param[in] number Given number
* @return The aligned number
*/
template <int alignment>
static int align(int number)
*/
template <int alignment> static int align(int number)
{
// Only works for alignment with power of 2
constexpr bool isPowerOfTwo = alignment && ((alignment & (alignment - 1)) == 0);
Expand All @@ -70,12 +69,12 @@ namespace pcpp
* A template class to calculate enum class hash
* @tparam EnumClass
*/
template<typename EnumClass, typename std::enable_if<std::is_enum<EnumClass>::value , bool>::type = false>
template <typename EnumClass, typename std::enable_if<std::is_enum<EnumClass>::value, bool>::type = false>
struct EnumClassHash
{
size_t operator()(EnumClass value) const
{
return static_cast<typename std::underlying_type<EnumClass>::type>(value);
}
};
}
} // namespace pcpp
Loading

0 comments on commit e3128c6

Please sign in to comment.