From b5256e31eec115a38b9dde70449d05002349583d Mon Sep 17 00:00:00 2001 From: Alex Bates Date: Mon, 17 Aug 2020 23:07:13 +0100 Subject: [PATCH] add astyle, update clang-tidy config --- .clang-format | 23 ------------------- .clang-tidy | 7 ++---- .github/workflows/lint.yaml | 39 +++++++++++++++++++++++++++++++ .vscode/settings.json | 1 + format.sh | 46 ++++++++++++++++++++++--------------- include/variables.h | 2 +- install.sh | 6 ++--- 7 files changed, 73 insertions(+), 51 deletions(-) delete mode 100644 .clang-format create mode 100644 .github/workflows/lint.yaml diff --git a/.clang-format b/.clang-format deleted file mode 100644 index c7b900f060..0000000000 --- a/.clang-format +++ /dev/null @@ -1,23 +0,0 @@ -IndentWidth: 4 -Language: Cpp -UseTab: Never -ColumnLimit: 120 -PointerAlignment: Left -BreakBeforeBraces: Attach -SpaceAfterCStyleCast: false -Cpp11BracedListStyle: false -IndentCaseLabels: true -BinPackArguments: true -BinPackParameters: true -AlignAfterOpenBracket: Align -AlignOperands: true -BreakBeforeTernaryOperators: true -BreakBeforeBinaryOperators: None -AllowShortBlocksOnASingleLine: true -AllowShortIfStatementsOnASingleLine: false -AllowShortLoopsOnASingleLine: false -AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: false -AlignEscapedNewlines: Left -AlignTrailingComments: true -SortIncludes: false diff --git a/.clang-tidy b/.clang-tidy index 4022a3f68a..d1f01520b0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,2 @@ -Checks: '-*,readability-braces-around-statements' -WarningsAsErrors: '' -HeaderFilterRegex: '(src|include)\/.*\.h$' -FormatStyle: 'file' -CheckOptions: +Checks: '-*,clang-analyzer-core.*,clang-analyzer-deadcode.*,readability-*,-readability-magic-numbers,-readability-else-after-return,-readability-named-parameter,-readability-braces-around-statements,-clang-diagnostic-error' +HeaderFilterRegex: '(src|include)\/.*\.h' diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000000..17f1c8b168 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,39 @@ +name: Lint +on: + push: + paths: + - 'src/*' + - 'include/*' + pull_request: + paths: + - 'src/*' + - 'include/*' + +jobs: + build: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - run: ./install.sh + + # lint files changed by the push/pr + - id: files + uses: trilom/file-changes-action@v1.2.4 + with: + output: ' ' + continue-on-error: true # see jitterbit/get-changed-files#7 + - run: ./format.sh ${{ steps.files.outputs.files}} + + # fail if any files were changed by ./format.sh + - id: files_formatted + uses: jackton1/find-changed-files@v1.1 + with: + files: src, include + + - name: Fail if any files reformatted + if: steps.files_formatted.outputs.files_changed == 'true' + run: | + git diff + exit 1 diff --git a/.vscode/settings.json b/.vscode/settings.json index c5dfc84c80..571f956be5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,4 +3,5 @@ "editor.insertSpaces": true, "files.eol": "\n", "files.insertFinalNewline": true, + "editor.rulers": [120], } diff --git a/format.sh b/format.sh index 90de3119e4..5f631f128b 100755 --- a/format.sh +++ b/format.sh @@ -1,26 +1,34 @@ -FORMAT_OPTS="-i -style=file" -TIDY_OPTS="-p . --fix --fix-errors" -COMPILER_OPTS="-fno-builtin -std=gnu90 -Iinclude -Isrc -D_LANGUAGE_C -DNON_MATCHING" +COMPILER_OPTS="-fno-builtin -std=gnu89 -Iinclude -Isrc -D_LANGUAGE_C" shopt -s globstar +FILES="src/**/*.c include/*.h" if (( $# > 0 )); then - echo "Formatting file(s) $*" - echo "Running clang-format..." - clang-format ${FORMAT_OPTS} "$@" - echo "Running clang-tidy..." - clang-tidy ${TIDY_OPTS} "$@" -- ${COMPILER_OPTS} &> /dev/null - echo "Adding missing final new lines..." - sed -i -e '$a\' "$@" - echo "Done formatting file(s) $*" + # 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 -echo "Formatting C files. This will take a bit" -echo "Running clang-format..." -clang-format ${FORMAT_OPTS} src/**/*.c -echo "Running clang-tidy..." -clang-tidy ${TIDY_OPTS} src/**/*.c -- ${COMPILER_OPTS} &> /dev/null -echo "Adding missing final new lines..." -find src/ -type f -name "*.c" -exec sed -i -e '$a\' {} \; -echo "Done formatting all files." +# format +astyle ${FILES} \ + --formatted --suffix=none \ + --lineend=linux \ + --convert-tabs \ + --max-code-length=120 \ + --min-conditional-indent=1 \ + --style=attach \ + --align-pointer=type --align-reference=name \ + --indent-switches \ + --indent-labels \ + --pad-oper --pad-comma --pad-header --unpad-paren \ + --attach-return-type \ + +# add newline at eof +find ${FILES} -exec sed -i -e '$a\' {} \; + +# lint +clang-tidy -p . ${FILES} -- ${COMPILER_OPTS} diff --git a/include/variables.h b/include/variables.h index 2051fff0a7..d3602c9944 100644 --- a/include/variables.h +++ b/include/variables.h @@ -48,7 +48,7 @@ extern s16 D_8010CD10; extern s16 D_8010CD12; extern s32 D_801595A0; extern char gCloudyFlowerFieldsBg[]; // "fla_bg" -extern char gSunnyFlowerFieldsBg[]; // "flb_bg" +extern char gSunnyFlowerFieldsBg[]; // "flb_bg" extern BackgroundHeader gBackgroundImage; extern s8 D_8014F12F; diff --git a/install.sh b/install.sh index 96f300b767..89d0498d3c 100755 --- a/install.sh +++ b/install.sh @@ -8,8 +8,8 @@ if command -v apt-install &> /dev/null; then if [[ $1 == "--extra" ]]; then echo "Installing extra" - sudo apt install -y python3 python3-pip clang-tidy clang-format - python3 -m pip install stringcase + sudo apt install -y python3 python3-pip clang-tidy astyle || exit 1 + python3 -m pip install stringcase || exit 1 fi echo "Done" @@ -48,7 +48,7 @@ if command -v pacman &> /dev/null; then if [[ $1 == "--extra" ]]; then echo "Installing extra" - sudo pacman -S --noconfirm --needed python python-pip clang || exit 1 + sudo pacman -S --noconfirm --needed python python-pip clang astyle || exit 1 python3 -m pip install stringcase || exit 1 fi