Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: clang-format init #1257

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 146 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,149 @@
---
BasedOnStyle: Microsoft
UseTab: Always
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
NamespaceIndentation: All

AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
...

37 changes: 37 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: lint
Copy link
Collaborator

@egecetin egecetin Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-commit hooks already supports to clang-format check. Maybe instead of adding a new runner enabling it from config file might be better.

rev: v1.3.5
hooks:
- 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
People can run pre-commit run --all-files to check locally also they can see is there any other formatting issues. As a default it search .clang-format in workspace directory. This also eliminates the need of run_format.bash


on:
push:
pull_request:
schedule:
- cron: '34 17 * * *'

jobs:

clang_format_check:

runs-on: ubuntu-latest

strategy:
matrix:
path:
- 'Common++'
- 'Pcap++'
- 'Packet++'
- 'Examples++'

steps:

- uses: actions/checkout@v3

- name: event name
run: |
echo "github.event_name: ${{ github.event_name }}"

- name: Run clang-format style check for C/C++/Protobuf programs.
uses: jidicula/clang-format-action@v4.11.0
with:
clang-format-version: '14'
check-path: ${{ matrix.path }}
fallback-style: 'LLVM' # optional

113 changes: 62 additions & 51 deletions Common++/header/GeneralUtils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef PCAPPP_GENERAL_UTILS
#define PCAPPP_GENERAL_UTILS

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

/// @file

Expand All @@ -12,59 +12,70 @@
*/
namespace pcpp
{
/**
* Convert a byte array into a string of hex characters. For example: for the array { 0xaa, 0x2b, 0x10 } the string
* "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
* @return A string of hex characters representing the byte array
*/
std::string byteArrayToHexString(const uint8_t* byteArr, size_t byteArrSize, int stringSizeLimit = -1);
/**
* Convert a byte array into a string of hex characters. For example: for the
* array { 0xaa, 0x2b, 0x10 } the string "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
* @return A string of hex characters representing the byte array
*/
std::string byteArrayToHexString(const uint8_t* byteArr, size_t byteArrSize,
int stringSizeLimit = -1);

/**
* Convert a string of hex characters into a byte array. For example: for the string "aa2b10" an array of values
* { 0xaa, 0x2b, 0x10 } will be returned
* @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.
* 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);
/**
* Convert a string of hex characters into a byte array. For example: for the
* string "aa2b10" an array of values { 0xaa, 0x2b, 0x10 } will be returned
* @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. 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.
* @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
* @param[in] needleLen Length of the needle buffer
* @return A pointer to the beginning of the substring, or NULL if the substring is not found
*/
char* cross_platform_memmem(const char* haystack, size_t haystackLen, const char* needle, size_t needleLen);
/**
* 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
* @param[in] needleLen Length of the needle buffer
* @return A pointer to the beginning of the substring, or NULL if the substring
* is not found
*/
char* cross_platform_memmem(const char* haystack, size_t haystackLen,
const char* needle, size_t needleLen);

/**
* Calculates alignment.
* @param[in] number Given number
* @return The aligned 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);
static_assert(isPowerOfTwo, "Alignment must be a power of 2");
int mask = alignment - 1;
return (number + mask) & ~mask;
}
/**
* Calculates alignment.
* @param[in] number Given number
* @return The aligned 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);
static_assert(isPowerOfTwo, "Alignment must be a power of 2");
int mask = alignment - 1;
return (number + mask) & ~mask;
}
} // namespace pcpp

#endif // PCAPPP_GENERAL_UTILS
Loading
Loading