From c970fa9bad1af14e8dacf86e5ac81daba189beca Mon Sep 17 00:00:00 2001 From: Diogo Cavaleiro Date: Wed, 25 Feb 2026 11:48:06 +0000 Subject: [PATCH] Add clang-format v18 workflow and configuration --- .clang-format | 145 +++++++++++++++++++--------- .github/workflows/clang-format.yaml | 42 ++++++++ 2 files changed, 142 insertions(+), 45 deletions(-) create mode 100644 .github/workflows/clang-format.yaml diff --git a/.clang-format b/.clang-format index 39cbfaf3..19c23af2 100644 --- a/.clang-format +++ b/.clang-format @@ -1,57 +1,112 @@ --- -#------------------------------------ -# Configuration for clang-format v12 -#------------------------------------ +BasedOnStyle: LLVM +Standard: Auto # let the formatter accept any C++ standard -Language: Cpp +# ============================================================================= +# Indentation +# ============================================================================= +TabWidth: 2 +ContinuationIndentWidth: 2 +ConstructorInitializerIndentWidth: 2 +NamespaceIndentation: All # everything inside namespace is indented +IndentCaseLabels: true # case labels at switch-body indent level +IndentCaseBlocks: true # indent block inside case label +IndentExternBlock: Indent +IndentWrappedFunctionNames: true # keep function name at same indent as return type +IndentPPDirectives: BeforeHash # nested #if / #include get indented before the # +PPIndentWidth: 2 -BasedOnStyle: Chromium +# ============================================================================= +# Braces & line-break style +# ============================================================================= +BreakBeforeBraces: Allman +BreakConstructorInitializers: BeforeComma # colon on new line, commas lead +BreakInheritanceList: BeforeComma +BreakBeforeBinaryOperators: NonAssignment +BreakStringLiterals: false +AlwaysBreakTemplateDeclarations: Yes # template <…> always on its own line -SortIncludes: true -IncludeBlocks: Preserve +# ============================================================================= +# Short statements +# ============================================================================= +AllowShortBlocksOnASingleLine: Empty +AllowShortFunctionsOnASingleLine: Empty # only empty bodies: void f() {} +AllowShortCaseLabelsOnASingleLine: true # case X: stmt; break; on one line +AllowShortLoopsOnASingleLine: true +AllowShortLambdasOnASingleLine: Inline + +# ============================================================================= +# Empty lines +# ============================================================================= +KeepEmptyLinesAtTheStartOfBlocks: false +EmptyLineBeforeAccessModifier: Always # blank line before public:/private:/protected: +EmptyLineAfterAccessModifier: Always # blank line after public:/private:/protected: +InsertNewlineAtEOF: true + +# ============================================================================= +# Spacing +# ============================================================================= +SpacesInAngles: Leave # preserve C++03 "> >" vs C++11 ">>" +SpacesInContainerLiterals: false + +# ============================================================================= +# Alignment +# ============================================================================= +PointerAlignment: Left # T* ptr, const T& ref +ReferenceAlignment: Left +AlignConsecutiveAssignments: true +AlignConsecutiveDeclarations: true +AlignConsecutiveBitFields: true +AlignConsecutiveMacros: true # align macro bodies + +# ============================================================================= +# Line length and wrapping +# ============================================================================= +ColumnLimit: 0 +ReflowComments: true # preserve hand-formatted comment rulers + +# ============================================================================= +# Includes +# ============================================================================= IncludeCategories: - Regex: '^(<|")(.*/)?platform\.h(>|")$' - Priority: 1 - - Regex: '.*' - Priority: 2 + Priority: -1 + # everything else gets the default priority (0) -BinPackParameters: false -BitFieldColonSpacing: Both +# ============================================================================= +# Arguments, parameters and constructor initialisers +# ============================================================================= +PackConstructorInitializers: Never # each initialiser on its own line -BreakBeforeBraces: Allman -BreakConstructorInitializers: BeforeComma +# ============================================================================= +# Namespaces and using declarations +# ============================================================================= +SortUsingDeclarations: Lexicographic -AllowShortBlocksOnASingleLine: false -AllowShortCaseLabelsOnASingleLine: true -AllowShortFunctionsOnASingleLine: None -AllowShortIfStatementsOnASingleLine: false +# ============================================================================= +# Macro-aware formatting +# ============================================================================= +# ETL-specific macros that should be treated as statement-level constructs +StatementMacros: + - ETL_DECLARE_DEBUG_COUNT + - ETL_INCREMENT_DEBUG_COUNT + - ETL_DECREMENT_DEBUG_COUNT + - ETL_ADD_DEBUG_COUNT + - ETL_STATIC_ASSERT -AllowAllArgumentsOnNextLine: true -AllowAllParametersOfDeclarationOnNextLine: true +# Macros that behave like attributes or specifiers. +AttributeMacros: + - ETL_NODISCARD + - ETL_NORETURN + - ETL_DEPRECATED -ConstructorInitializerAllOnOneLineOrOnePerLine: 'true' -ConstructorInitializerIndentWidth: '2' +# Macros that behave like type names +TypenameMacros: + - ETL_OR_STD -NamespaceIndentation: All -IndentPPDirectives: BeforeHash -PointerAlignment: Left -ColumnLimit: '0' -ContinuationIndentWidth: '2' -UseTab: Never -TabWidth: '2' -IndentWidth: '2' -AccessModifierOffset : '-2' -IndentCaseLabels: false -Cpp11BracedListStyle: 'true' -AlignAfterOpenBracket: Align -AlignConsecutiveDeclarations: true - -#------------------------------------ -# Configurations not supported by clang-format v12 -#------------------------------------ -# BreakInheritanceList: AfterComma -# EmptyLineBeforeAccessModifier: Always -# EmptyLineAfterAccessModifier: Always -# ReferenceAlignment: Left - -... +# Do not reformat these macros — they contain DSL-like content +WhitespaceSensitiveMacros: + - ETL_ERROR_TEXT + - ETL_DECLARE_ENUM_TYPE + - ETL_ENUM_TYPE + - ETL_END_ENUM_TYPE diff --git a/.github/workflows/clang-format.yaml b/.github/workflows/clang-format.yaml new file mode 100644 index 00000000..9eb77ab9 --- /dev/null +++ b/.github/workflows/clang-format.yaml @@ -0,0 +1,42 @@ +name: clang-format + +on: + push: + branches: [ master, development, pull-request/* ] + pull_request: + branches: [ master, development, pull-request/* ] + types: [opened, synchronize, reopened] + +jobs: + clang-format: + name: clang-format + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install clang-format and git + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends git clang-format + clang-format --version + + - name: Run clang-format on added/changed files + run: | + if [ "${{ github.event_name }}" = "pull_request" ]; then + BEFORE="origin/${{ github.base_ref }}" + elif [ "${{ github.event_name }}" = "push" ]; then + BEFORE="${{ github.event.before }}" + else + echo "Unsupported event: ${{ github.event_name }}" + echo "This is likely a bug in the workflow configuration. Please report it to the maintainers." + exit 1 + fi + + git diff --name-only -z --diff-filter=AMCR "$BEFORE"..HEAD -- \ + '*.c' '*.cc' '*.cxx' '*.cpp' \ + '*.h' '*.hh' '*.hpp' '*.hxx' \ + '*.ipp' '*.inl' \ + ':(exclude)include/etl/generators/*' \ + | xargs -0 --no-run-if-empty clang-format --Werror -n --style=file