diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..7e2b497f1a --- /dev/null +++ b/.clang-format @@ -0,0 +1,63 @@ +DisableFormat: true # clang-format supports many filetypes +--- +Language: Cpp +DisableFormat: false +BasedOnStyle: WebKit +IndentWidth: 4 +UseTab: Never +ColumnLimit: 120 + +AllowShortIfStatementsOnASingleLine: false +AllowShortBlocksOnASingleLine: false + +BreakBeforeBraces: Custom +BraceWrapping: + # Place opening brace on next line if the statement is multi-line + # e.g. + # if (very + # long + # condition) + # { + AfterControlStatement: MultiLine + +IndentCaseLabels: true + +BreakBeforeBinaryOperators: NonAssignment + +ExperimentalAutoDetectBinPacking: true +BinPackParameters: false +AlignAfterOpenBracket: BlockIndent + +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: true + PadOperators: true +AlignConsecutiveDeclarations: # structs etc + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignTrailingComments: true + +WhitespaceSensitiveMacros: ['STRINGIFY'] + +# Our include order is WIP so disable it +SortIncludes: false +IncludeCategories: + # common.h always comes first + - Regex: '^"common\.h"$' + Priority: 10 + # everything else + - Regex: '.*' + Priority: 1 + SortPriority: 0 + +AllowShortFunctionsOnASingleLine: None +--- + +... diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 29a5706a47..b5595d180e 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -1,29 +1,35 @@ name: Lint on: pull_request: - paths: - - 'src/*' - - 'include/*' - - 'format.sh' - - '.clang-tidy' jobs: - build: - name: Lint + cpp_lint: + name: Format and lint runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - - run: sudo apt-get install -y astyle clang-tidy - - - run: ./format.sh - - # Detect any files changed by ./format.sh - - id: files_formatted - uses: jackton1/find-changed-files@v1.1 + - name: Checkout + uses: actions/checkout@v2 with: - files: src, include - - - name: Fail if any files reformatted - if: steps.files_formatted.outputs.files_changed == 'true' + fetch-depth: 0 + - name: Git + run: | + git log --oneline --graph --max-count=100 + git log --oneline --graph --max-count=100 HEAD origin/main + - name: clang-format + run: | + git config --global color.ui always + git clang-format-14 --diff origin/main HEAD + git config --global color.ui auto + - name: Lint + uses: cpp-linter/cpp-linter-action@v2 + id: linter + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + style: file + lines-changed-only: diff + tidy-checks: '' + version: '15' + - name: Fail if lint failed + if: steps.linter.outputs.checks-failed > 0 run: exit 1 diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 2f631c13a7..09fea3e1af 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -4,6 +4,7 @@ "nanaian.vscode-star-rod", "notskm.clang-tidy", "EditorConfig.EditorConfig", + "xaver.clang-format" ], "unwantedRecommendations": [ "llvm-vs-code-extensions.vscode-clangd", diff --git a/.vscode/settings.json b/.vscode/settings.json index 9f4c463565..09222479f4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -35,6 +35,9 @@ ], "[c]": { "editor.wordSeparators": "`~!@#%^&*()-=+[{]}\\|;:'\",.<>/?", // no $, for scripts + "editor.formatOnSave": false, // TODO: enable this once we solve EVTs, see #1004 + "editor.formatOnSaveMode": "modifications", + "editor.defaultFormatter": "xaver.clang-format", }, "files.associations": { "*.h": "c", diff --git a/format.sh b/format.sh deleted file mode 100755 index 18385b414a..0000000000 --- a/format.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env bash - -COMPILER_OPTS="-std=gnu89 -Iinclude -Isrc -D_LANGUAGE_C" - -shopt -s globstar - -FILES="src/**/*.c include/*.h" -if (( $# > 0 )); then - # only process .c and .h files - FILES=$(echo "$@" | sed 's/ /\n/g' | grep '\.[ch]$') -fi - -if [[ -z $FILES ]]; then - echo "no .c or .h files specified" - exit -fi - -# format -astyle ${FILES} \ - --formatted --suffix=none \ - --lineend=linux \ - --convert-tabs \ - --max-code-length=120 \ - --min-conditional-indent=0 \ - --style=attach \ - --align-pointer=type --align-reference=name \ - --indent-switches \ - --pad-oper --pad-comma --pad-header --unpad-paren \ - --attach-return-type \ - --keep-one-line-blocks \ - --keep-one-line-statements - -# add newline at eof -find ${FILES} -exec sed -i -e '$a\' {} \; - -# lint -C_FILES=$(echo "$FILES" | grep '\.c$') -if [[ ! -z $C_FILES ]]; then - clang-tidy -p . ${C_FILES} -- ${COMPILER_OPTS} -fi