Skip to content

Commit 2ed7b9f

Browse files
authored
[GitHub][CI] Add clang-tidy premerge workflow (#154829)
**KEY POINTS** - MVP workflow to automatically lint C++ files, located **only** in `clang-tools-extra/clang-tidy`. It's chosen this way as `clang-tools-extra/clang-tidy` is almost 100% `clang-tidy` complaint, thus we would automatically enforce a [high quality standard for clang-tidy source code](https://discourse.llvm.org/t/rfc-create-hardened-clang-tidy-config-for-clang-tidy-directory/87247). (#147793) - Implementation is very similar to code-format job, but without the ability to run `code-lint-helper.py` locally. **FOUND ISSUES** + open questions - Speed: it takes ~ 1m40sec to download and unpack `clang-tidy` plus additional ~4 mins to configure and CodeGen targets. I see that `premerge.yaml` runs on special `llvm-premerge-linux-runners` runners which can use `sccache` for speed. Can we use the same runners for this job? Exact timings can be found [here](https://github.com/llvm/llvm-project/actions/runs/17135749067/job/48611150680?pr=154223). **TO DO** - Place common components from `code-lint-helper.py` and `code-format-helper.py` into a separate file and reuse it in both CI's. - Compute CodeGen targets based on `projects_to_build`, for now `clang-tablegen-targets` is hardcoded for `clang-tools-extra/`. - Automatically generate and upload `.yaml` for `clang-apply-replacements` - Create an RFC with a plan how to enable `clang-tidy` in other projects so that Maintainers of LLVM components could choose if they want `clang-tidy` or not. - Add more linters like `pylint`, `ruff` in the future.
1 parent c506c28 commit 2ed7b9f

File tree

5 files changed

+778
-0
lines changed

5 files changed

+778
-0
lines changed

.github/workflows/issue-write.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- "Check code formatting"
77
- "Check for private emails used in PRs"
88
- "PR Request Release Note"
9+
- "Code lint"
910
types:
1011
- completed
1112

.github/workflows/pr-code-lint.yml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
name: "Code lint"
2+
3+
permissions:
4+
contents: read
5+
6+
on:
7+
pull_request:
8+
branches:
9+
- main
10+
- 'users/**'
11+
paths:
12+
- 'clang-tools-extra/clang-tidy/**'
13+
14+
jobs:
15+
code_linter:
16+
if: github.repository_owner == 'llvm'
17+
runs-on: ubuntu-24.04
18+
defaults:
19+
run:
20+
shell: bash
21+
container:
22+
image: 'ghcr.io/llvm/ci-ubuntu-24.04:latest'
23+
timeout-minutes: 60
24+
concurrency:
25+
group: ${{ github.workflow }}-${{ github.ref }}
26+
cancel-in-progress: true
27+
steps:
28+
- name: Fetch LLVM sources
29+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
30+
with:
31+
fetch-depth: 2
32+
33+
- name: Get changed files
34+
id: changed-files
35+
uses: step-security/changed-files@3dbe17c78367e7d60f00d78ae6781a35be47b4a1 # v45.0.1
36+
with:
37+
separator: ","
38+
skip_initial_fetch: true
39+
base_sha: 'HEAD~1'
40+
sha: 'HEAD'
41+
42+
- name: Listed files
43+
env:
44+
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
45+
run: |
46+
echo "Changed files:"
47+
echo "$CHANGED_FILES"
48+
49+
- name: Fetch code linting utils
50+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
51+
with:
52+
repository: ${{ github.repository }}
53+
ref: ${{ github.base_ref }}
54+
sparse-checkout: |
55+
llvm/utils/git/code-lint-helper.py
56+
llvm/utils/git/requirements_linting.txt
57+
clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
58+
sparse-checkout-cone-mode: false
59+
path: code-lint-tools
60+
61+
- name: Install clang-tidy
62+
uses: aminya/setup-cpp@17c11551771948abc5752bbf3183482567c7caf0 # v1.1.1
63+
with:
64+
clang-tidy: 20.1.8
65+
66+
- name: Setup Python env
67+
uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
68+
with:
69+
python-version: '3.12'
70+
71+
- name: Install Python dependencies
72+
run: python3 -m pip install -r code-lint-tools/llvm/utils/git/requirements_linting.txt
73+
74+
# TODO: create special mapping for 'codegen' targets, for now build predefined set
75+
# TODO: add entrypoint in 'compute_projects.py' that only adds a project and its direct dependencies
76+
- name: Configure and CodeGen
77+
run: |
78+
git config --global --add safe.directory '*'
79+
80+
. <(git diff --name-only HEAD~1...HEAD | python3 .ci/compute_projects.py)
81+
82+
if [[ "${projects_to_build}" == "" ]]; then
83+
echo "No projects to analyze"
84+
exit 0
85+
fi
86+
87+
cmake -G Ninja \
88+
-B build \
89+
-S llvm \
90+
-DLLVM_ENABLE_ASSERTIONS=OFF \
91+
-DLLVM_ENABLE_PROJECTS="${projects_to_build}" \
92+
-DCMAKE_CXX_COMPILER=clang++ \
93+
-DCMAKE_C_COMPILER=clang \
94+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
95+
-DLLVM_INCLUDE_TESTS=OFF \
96+
-DCLANG_INCLUDE_TESTS=OFF \
97+
-DCMAKE_BUILD_TYPE=Release
98+
99+
ninja -C build \
100+
clang-tablegen-targets \
101+
genconfusable # for "ConfusableIdentifierCheck.h"
102+
103+
- name: Run code linter
104+
env:
105+
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
106+
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
107+
run: |
108+
echo "[]" > comments &&
109+
python3 ./code-lint-tools/llvm/utils/git/code-lint-helper.py \
110+
--token ${{ secrets.GITHUB_TOKEN }} \
111+
--issue-number $GITHUB_PR_NUMBER \
112+
--start-rev HEAD~1 \
113+
--end-rev HEAD \
114+
--verbose \
115+
--changed-files "$CHANGED_FILES"
116+
117+
- name: Upload results
118+
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 #v4.3.0
119+
if: always()
120+
with:
121+
name: workflow-args
122+
path: |
123+
comments

0 commit comments

Comments
 (0)