From 3806ccbde7deff7aad3d29201413b8b9ff166de2 Mon Sep 17 00:00:00 2001 From: Mats Nilsson Date: Tue, 11 Feb 2025 14:50:08 +0100 Subject: [PATCH] Enable clang-format and clang-tidy --- .clang-format | 12 ++++++ .clang-tidy | 21 ++++++++++ .github/workflows/clang-format.yaml | 45 -------------------- .github/workflows/clang-tidy.yaml | 65 ----------------------------- .github/workflows/code-checks.yaml | 63 ---------------------------- .github/workflows/cpp-linter.yaml | 44 +++++++++++++++++++ .github/workflows/cppcheck.yaml | 48 --------------------- atos/CMakeLists.txt | 1 + 8 files changed, 78 insertions(+), 221 deletions(-) create mode 100644 .clang-format create mode 100644 .clang-tidy delete mode 100644 .github/workflows/clang-format.yaml delete mode 100644 .github/workflows/clang-tidy.yaml delete mode 100644 .github/workflows/code-checks.yaml create mode 100644 .github/workflows/cpp-linter.yaml delete mode 100644 .github/workflows/cppcheck.yaml diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..d664b09fc --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +BasedOnStyle: Mozilla +InsertNewlineAtEOF: true +UseTab: Always +IndentWidth: 4 +TabWidth: 4 +ColumnLimit: 120 +BreakConstructorInitializers: AfterColon +PackConstructorInitializers: Never +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AllowShortBlocksOnASingleLine: Empty +AllowShortFunctionsOnASingleLine: True diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 000000000..42fc7a17e --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,21 @@ +# TODO: Enable the disabled checks. +Checks: + modernize*, + cppcoreguidelines*, + misc*, + google*, + clang-analyzer*, + hicpp*, + performance*, + readability*, + -google-readability-todo, + -hicpp-named-parameter, + -misc-include-cleaner, + -misc-non-private-member-variables-in-classes, + -cppcoreguidelines-avoid-magic-numbers, + -readability-magic-numbers, + -modernize-use-nodiscard, + -modernize-use-trailing-return-type, + -readability-avoid-const-params-in-decls, + -readability-identifier-length, + -readability-named-parameter, diff --git a/.github/workflows/clang-format.yaml b/.github/workflows/clang-format.yaml deleted file mode 100644 index 87ca862a8..000000000 --- a/.github/workflows/clang-format.yaml +++ /dev/null @@ -1,45 +0,0 @@ -name: clang-tidy-review - -# You can be more specific, but it currently only works on pull requests -on: - workflow_call: - inputs: - target_files: - description: 'The list of files to be checked by clang-tidy' - required: true - default: '' - type: string - -jobs: - clang-format: - runs-on: ubuntu-latest - container: ros:humble - steps: - - name: Check out repository - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Install clang-format - run: | - sudo apt-get -yqq update - sudo apt-get -yqq install clang-format - shell: bash - - - name: Run clang-format style check. - id: clang-format - env: - ALL_CHANGED_FILES: ${{ inputs.target_files }} - run: | - mkdir /tmp/clang-format-result - for file_path in ${ALL_CHANGED_FILES}; do - folder_name="${file_path%/*}/" - mkdir -p /tmp/clang-format-result/$folder_name - clang-format ${file_path} > /tmp/clang-format-result/${file_path} || true - done - - - name: Upload clang-format result - uses: actions/upload-artifact@v4 - with: - name: clang-format-result - path: /tmp/clang-format-result/ diff --git a/.github/workflows/clang-tidy.yaml b/.github/workflows/clang-tidy.yaml deleted file mode 100644 index 4a14b9ab0..000000000 --- a/.github/workflows/clang-tidy.yaml +++ /dev/null @@ -1,65 +0,0 @@ -name: clang-tidy-review - -# You can be more specific, but it currently only works on pull requests -on: - workflow_call: - inputs: - target_files: - description: 'The list of files to be checked by clang-tidy' - required: true - default: '' - type: string - -jobs: - clang-tidy: - runs-on: ubuntu-latest - container: ros:humble - steps: - - name: Check out repository - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Prepare atos workspace - run: | - mkdir -p $HOME/atos_ws/src - ln -s $GITHUB_WORKSPACE $HOME/atos_ws/src/atos - - - name: Download build folder - uses: actions/download-artifact@v4 - with: - name: build-folder - path: /github/home/atos_ws/build - - - name: Install Clang-Tidy - run: | - sudo apt-get -yqq update - sudo apt-get -yqq install clang-tidy libomp-dev - shell: bash - - - name: Run clang-tidy linter - if: ${{ inputs.target_files != '' }} - env: - ALL_CHANGED_FILES: ${{ inputs.target_files }} - run: | - echo "running clang-tidy on $ALL_CHANGED_FILES" - ls $HOME/atos_ws/build/ - mkdir /tmp/clang-tidy-result - clang-tidy -p $HOME/atos_ws/build/ -export-fixes /tmp/clang-tidy-result/fixes.yaml --checks=cppcoreguidelines-*,clang-analyzer-* ${ALL_CHANGED_FILES} || true - echo "${{ github.event.number }}" > /tmp/clang-tidy-result/pr-id.txt - echo "${{ github.event.pull_request.head.repo.full_name }}" > /tmp/clang-tidy-result/pr-head-repo.txt - echo "${{ github.event.pull_request.head.ref }}" > /tmp/clang-tidy-result/pr-head-ref.txt - shell: bash - - - name: Check if the fixes.yaml file exists - id: check-fixes-yaml-existence - uses: autowarefoundation/autoware-github-actions/check-file-existence@v1 - with: - files: /tmp/clang-tidy-result/fixes.yaml - - - name: Upload clang-tidy result - if: ${{ steps.check-fixes-yaml-existence.outputs.exists == 'true' }} - uses: actions/upload-artifact@v4 - with: - name: clang-tidy-result - path: /tmp/clang-tidy-result/ diff --git a/.github/workflows/code-checks.yaml b/.github/workflows/code-checks.yaml deleted file mode 100644 index af98cabd9..000000000 --- a/.github/workflows/code-checks.yaml +++ /dev/null @@ -1,63 +0,0 @@ -name: Clang - -on: [pull_request] - -jobs: - build: - uses: ./.github/workflows/build.yaml - - file-changes: - runs-on: ubuntu-latest - outputs: - all: ${{ steps.changed-files.outputs.all }} - cpp: ${{ steps.cpp-files.outputs.CPP }} - steps: - - name: Check out repository - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Get changed files - id: changed-files - uses: jitterbit/get-changed-files@v1 - - - name: Filter on only cpp, hpp, c, and h files - id: cpp-files - run: | - input_files=(${{ steps.changed-files.outputs.all }}) - echo "Input files: $input_files" - cpp_files="" - for f in "${input_files[@]}"; do - if [[ $f == *.cpp || $f == *.hpp || $f == *.c || $f == *.h ]]; then - echo "File ending correct: $f" - cpp_files+="$f " - else - echo "File ending incorrect: $f" - fi - done - echo "Target files: $cpp_files" - - { - echo 'CPP<> $GITHUB_OUTPUT - - - name: Print all files - run: | - echo ${{ steps.cpp-files.outputs.CPP }} - - clang-tidy: - uses: ./.github/workflows/clang-tidy.yaml - needs: [build, file-changes] - with: - target_files: ${{ needs.file-changes.outputs.cpp }} - - clang-format: - uses: ./.github/workflows/clang-format.yaml - needs: [file-changes] - with: - target_files: ${{ needs.file-changes.outputs.cpp }} - - cppcheck: - uses: ./.github/workflows/cppcheck.yaml diff --git a/.github/workflows/cpp-linter.yaml b/.github/workflows/cpp-linter.yaml new file mode 100644 index 000000000..35ab88d20 --- /dev/null +++ b/.github/workflows/cpp-linter.yaml @@ -0,0 +1,44 @@ +name: cpp-linter +on: + pull_request: + branches: [main, dev] + paths: ['**.c', '**.h', '**.cpp', '**.hpp', '**CMakeLists.txt'] + +jobs: + cpp-linter: + runs-on: ubuntu-24.04 + container: ros:humble + permissions: + contents: read + pull-requests: write + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + submodules: recursive + + - run: git config --system --add safe.directory "$PWD" + + - name: Install ATOS + run: | + ./setup_atos.sh + shell: bash + + - name: Add to PATH + shell: bash + run: | + echo "/github/home/.local/bin/" >> $GITHUB_PATH + + - uses: cpp-linter/cpp-linter-action@v2 + id: linter + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + style: 'file' + tidy-checks: '' + database: "build" + version: 18 + # lines-changed-only: true + files-changed-only: false + tidy-review: true + passive-reviews: true diff --git a/.github/workflows/cppcheck.yaml b/.github/workflows/cppcheck.yaml deleted file mode 100644 index 24f5c38e2..000000000 --- a/.github/workflows/cppcheck.yaml +++ /dev/null @@ -1,48 +0,0 @@ -name: cppcheck - -on: workflow_call - -jobs: - cppcheck: - runs-on: ubuntu-latest - container: ros:humble - steps: - - name: Check out repository - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Install cppcheck - run: | - sudo apt-get -yqq update - sudo apt-get -yqq install cppcheck - shell: bash - - - name: Run cppcheck. - id: cppcheck - run: | - mkdir /tmp/cppcheck-result - cppcheck --output-file=/tmp/cppcheck-result/cppcheck-result.txt . - - - name: Check if the cppcheck-result.txt file is empty - id: check-cppcheck-result-empty - run: | - if [ -s /tmp/cppcheck-result/cppcheck-result.txt ]; then - echo "empty=false" >> "$GITHUB_OUTPUT" - else - echo "empty=true" >> "$GITHUB_OUTPUT" - fi - shell: bash - - - name: Upload cppcheck result - if: ${{ steps.check-cppcheck-result-empty.outputs.empty == 'false' }} - uses: actions/upload-artifact@v4 - with: - name: cppcheck-result - path: /tmp/cppcheck-result/ - - - name: Mark the workflow as failed if the cppcheck-results.txt file contains errors - if: ${{ steps.check-cppcheck-result-empty.outputs.empty == 'false' }} - run: | - exit 1 - shell: bash diff --git a/atos/CMakeLists.txt b/atos/CMakeLists.txt index 0945133c7..a1dae3661 100644 --- a/atos/CMakeLists.txt +++ b/atos/CMakeLists.txt @@ -12,6 +12,7 @@ set(CMAKE_COLOR_MAKEFILE ON) set(COMPILER_WARNINGS "-Wall -Wextra") set(CMAKE_CXX_COMPILER "clang++") set(CMAKE_C_COMPILER "clang") +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) #Set preprocessor macros depending on used ros version if ($ENV{ROS_DISTRO} STREQUAL "foxy")