From 244974f153c0d7327a02091ca73e2cb91bd86f3b Mon Sep 17 00:00:00 2001 From: slali87 <109802779+slali87@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:56:14 +0200 Subject: [PATCH] TOOLS: Add clang-format Add clang-format --- .clang-format | 280 +++++++++++++++++++++++++++++++++++++++ .github/workflows/CI.yml | 8 +- README.md | 2 +- ToDo.md | 3 - cmake/FormatCode.cmake | 31 +++++ 5 files changed, 316 insertions(+), 8 deletions(-) create mode 100644 .clang-format create mode 100644 cmake/FormatCode.cmake diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..09b66bd --- /dev/null +++ b/.clang-format @@ -0,0 +1,280 @@ +--- +Language: Cpp +# BasedOnStyle: Google +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseColons: false +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: true +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^' + Priority: 3 #2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^<.*\.h>' + Priority: 2 #1 + SortPriority: 0 + CaseSensitive: false + - Regex: '^<.*' + Priority: 3 #2 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 #3 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '([-_](test|unittest))?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: true +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +KeepEmptyLinesAtEOF: false +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Never +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: NextLine +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakScopeResolution: 500 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +PPIndentWidth: -1 +QualifierAlignment: Leave +RawStringFormats: + - Language: Cpp + Delimiters: + - cc + - CC + - cpp + - Cpp + - CPP + - 'c++' + - 'C++' + CanonicalDelimiter: '' + BasedOnStyle: google + - Language: TextProto + Delimiters: + - pb + - PB + - proto + - PROTO + EnclosingFunctions: + - EqualsProto + - EquivToProto + - PARSE_PARTIAL_TEXT_PROTO + - PARSE_TEST_PROTO + - PARSE_TEXT_PROTO + - ParseTextOrDie + - ParseTextProtoOrDie + - ParseTestProto + - ParsePartialTestProto + CanonicalDelimiter: pb + BasedOnStyle: google +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SkipMacroDefinitionBody: false +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterPlacementOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Auto +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +... + diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 134b3a9..41ff6fd 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -83,10 +83,10 @@ jobs: # run: | # cd ${{ github.event.repository.name }} # ./run.sh analyseCode -# - name: Format the code -# run: | -# cd ${{ github.event.repository.name }} -# ./run.sh formatCode + - name: Format the code + run: | + cd ${{ github.event.repository.name }} + cmake -P cmake/FormatCode.cmake - name: Install Doxygen on Linux if: runner.os == 'Linux' run: | diff --git a/README.md b/README.md index fe7447a..5a11be9 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ The './run.sh config' command has to be ran before running this. ### **Command to format the code:** ``` -TODO: ./run.sh formatCode +cmake -P cmake/FormatCode.cmake ``` The **ClangFormat** program has to be available on the machine. diff --git a/ToDo.md b/ToDo.md index 1a2f33c..5d3dfc7 100644 --- a/ToDo.md +++ b/ToDo.md @@ -7,9 +7,6 @@ - **Implement analyseCode** with .clang-tidy -- **Implement formatCode** - with .clang-format - - **Implement all** . diff --git a/cmake/FormatCode.cmake b/cmake/FormatCode.cmake new file mode 100644 index 0000000..279ef09 --- /dev/null +++ b/cmake/FormatCode.cmake @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.28) + +# Check if there is any modified files +execute_process(COMMAND git diff --shortstat + OUTPUT_VARIABLE numBefore + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMAND_ERROR_IS_FATAL ANY) + +# Set list of source files and run the clang-format on them +set(EXPRESSION h hpp hh c cpp cc cxx) +list(TRANSFORM EXPRESSION PREPEND "${CMAKE_SOURCE_DIR}/*.") +file(GLOB_RECURSE SOURCE_FILES FOLLOW_SYMLINKS + LIST_DIRECTORIES false ${EXPRESSION} +) +list(FILTER SOURCE_FILES EXCLUDE REGEX ".*/build/.*") +execute_process(COMMAND clang-format -style=file -i ${SOURCE_FILES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMAND_ERROR_IS_FATAL ANY) + +# Check if there is any modified files +execute_process(COMMAND git diff --shortstat + OUTPUT_VARIABLE numAfter + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMAND_ERROR_IS_FATAL ANY) + +# Check if there is any modified file +if(NOT "${numBefore}" STREQUAL "${numAfter}") + message( FATAL_ERROR "There is modified file. Check them!!!") +endif() + +