Skip to content

Commit

Permalink
Add CI action that reports clang-tidy warnings as review comments.
Browse files Browse the repository at this point in the history
 #xls-build-gardener
  • Loading branch information
hzeller committed Oct 10, 2024
1 parent 83d5b9e commit 7295f9c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/review-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Auto Review Analyzers

on:
push:
branches:
- main
paths-ignore:
# Do not trigger action when docs are updated.
- 'docs/**'
pull_request:
branches:
- main

jobs:
clang-tidy:
runs-on:
labels: ubuntu-22.04-64core

permissions:
# Open PR comments
pull-requests: write
# OPTIONAL: auto-closing conversations requires the `contents` permission
contents: write

steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

- name: Fetch base branch to compare to
run: |
git remote add review-base "https://github.com/${{ github.event.pull_request.base.repo.full_name }}"
git fetch --no-tags --no-recurse-submodules review-base "${{ github.event.pull_request.base.ref }}"
- name: Restore Nightly Bazel Cache
uses: actions/cache/restore@v4
with:
path: "~/.cache/bazel"
key: bazel-cache-nightly-${{ runner.os }}-${{ github.sha }}
restore-keys: bazel-cache-nightly-${{ runner.os }}-

- name: Create compilation DB
run: |
xls/dev_tools/make-compilation-db.sh
- name: Run clang-tidy on diff
run: |
mkdir clang-tidy-result
git diff -U1000 "$(git merge-base HEAD review-base/${{ github.event.pull_request.base.ref }})" | xls/dev_tools/run-clang-tidy-diff.sh -p1 -path build -export-fixes clang-tidy-result/fixes.yml
- name: Run clang-tidy-pr-comments action
uses: hzeller/clang-tidy-pr-comments@master
with:
# Token to allow commenting
github_token: ${{ secrets.GITHUB_TOKEN }}
clang_tidy_fixes: clang-tidy-result/fixes.yml
request_changes: true
suggestions_per_comment: 10
3 changes: 1 addition & 2 deletions xls/dev_tools/make-compilation-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set -e
readonly OUTPUT_BASE="$(bazel info output_base)"

readonly COMPDB_SCRIPT="${OUTPUT_BASE}/external/com_grail_bazel_compdb/generate.py"
[ -r "${COMPDB_SCRIPT}" ] || bazel fetch @com_grail_bazel_compdb//...
[ -r "${COMPDB_SCRIPT}" ] || bazel fetch --noshow_progress @com_grail_bazel_compdb//...

python3 "${COMPDB_SCRIPT}"

Expand All @@ -33,4 +33,3 @@ s/ -f[^ ]*/ /g # remove all -fxyz options
s/ --target[^ ]*/ /g # target platform not needed, might confuse
s/ -stdlib=libc++/ / # otherwise, clang-tidy does not find c++ headers
EOF

26 changes: 26 additions & 0 deletions xls/dev_tools/run-clang-tidy-diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Copyright 2023 The XLS Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Prepare compilation DB and run clang-tidy-diff that we already have
# bundled in our toolchain.
# Reads a unified diff from stdin. If a header is removed, we want to get
# enough context to see that it is not used anywhere; so invoke like
#
# git diff -U1000 | xls/dev_tools/run-clang-tidy-cached.sh -p1

CLANG_TIDY_LLVM_BASE="$(bazel info output_base)/external/llvm_toolchain_llvm/"

"${CLANG_TIDY_LLVM_BASE}/share/clang/clang-tidy-diff.py" \
-clang-tidy-binary="${CLANG_TIDY_LLVM_BASE}/bin/clang-tidy" "$@"
4 changes: 2 additions & 2 deletions xls/dslx/errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#include "xls/dslx/errors.h"

#include <string>
#include <string_view>
//#include <string> testing
//#include <string_view>

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
Expand Down
2 changes: 1 addition & 1 deletion xls/ir/function_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <memory>
#include <optional>
#include <ostream>
#include <string>
// #include <string> // testing clang tidy
#include <string_view>
#include <utility>
#include <variant>
Expand Down

0 comments on commit 7295f9c

Please sign in to comment.